From dc9f4c329e6f55ce7595970463e0caf148096f4b Mon Sep 17 00:00:00 2001 From: Rob Mensching Date: Thu, 21 Dec 2017 13:42:52 -0800 Subject: Support wixout and extract Resolve and Layout from Binder --- src/WixToolset.Core/Bind/FileSystem.cs | 87 ++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 src/WixToolset.Core/Bind/FileSystem.cs (limited to 'src/WixToolset.Core/Bind/FileSystem.cs') diff --git a/src/WixToolset.Core/Bind/FileSystem.cs b/src/WixToolset.Core/Bind/FileSystem.cs new file mode 100644 index 00000000..7d1b223e --- /dev/null +++ b/src/WixToolset.Core/Bind/FileSystem.cs @@ -0,0 +1,87 @@ +// 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); + } +} -- cgit v1.2.3-55-g6feb