// Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information. namespace WixToolset.Core.Bind { using System; using System.Collections.Generic; using System.IO; using System.Runtime.InteropServices; using WixToolset.Extensibility; internal class FileSystem { public FileSystem(IEnumerable extensions) { this.Extensions = extensions ?? Array.Empty(); } private IEnumerable Extensions { get; } /// /// Copies a file. /// /// The file to copy. /// The destination file. /// true if the destination file can be overwritten; otherwise, false. public bool CopyFile(string source, string destination, bool overwrite) { foreach (var extension in this.Extensions) { if (extension.CopyFile(source, destination, overwrite)) { return true; } } if (overwrite && File.Exists(destination)) { File.Delete(destination); } if (!CreateHardLink(destination, source, IntPtr.Zero)) { #if DEBUG int er = Marshal.GetLastWin32Error(); #endif File.Copy(source, destination, overwrite); } return true; } /// /// Moves a file. /// /// The file to move. /// The destination file. public bool MoveFile(string source, string destination, bool overwrite) { foreach (var extension in this.Extensions) { if (extension.MoveFile(source, destination, overwrite)) { return true; } } if (overwrite && File.Exists(destination)) { File.Delete(destination); } var directory = Path.GetDirectoryName(destination); if (!String.IsNullOrEmpty(directory)) { Directory.CreateDirectory(directory); } File.Move(source, destination); return true; } [DllImport("Kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)] private static extern bool CreateHardLink(string lpFileName, string lpExistingFileName, IntPtr lpSecurityAttributes); } }