diff options
Diffstat (limited to 'src/WixToolset.Core/Bind/FileSystem.cs')
-rw-r--r-- | src/WixToolset.Core/Bind/FileSystem.cs | 87 |
1 files changed, 87 insertions, 0 deletions
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 @@ | |||
1 | // 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. | ||
2 | |||
3 | namespace WixToolset.Core.Bind | ||
4 | { | ||
5 | using System; | ||
6 | using System.Collections.Generic; | ||
7 | using System.IO; | ||
8 | using System.Runtime.InteropServices; | ||
9 | using WixToolset.Extensibility; | ||
10 | |||
11 | internal class FileSystem | ||
12 | { | ||
13 | public FileSystem(IEnumerable<IFileSystemExtension> extensions) | ||
14 | { | ||
15 | this.Extensions = extensions ?? Array.Empty<IFileSystemExtension>(); | ||
16 | } | ||
17 | |||
18 | private IEnumerable<IFileSystemExtension> Extensions { get; } | ||
19 | |||
20 | /// <summary> | ||
21 | /// Copies a file. | ||
22 | /// </summary> | ||
23 | /// <param name="source">The file to copy.</param> | ||
24 | /// <param name="destination">The destination file.</param> | ||
25 | /// <param name="overwrite">true if the destination file can be overwritten; otherwise, false.</param> | ||
26 | public bool CopyFile(string source, string destination, bool overwrite) | ||
27 | { | ||
28 | foreach (var extension in this.Extensions) | ||
29 | { | ||
30 | if (extension.CopyFile(source, destination, overwrite)) | ||
31 | { | ||
32 | return true; | ||
33 | } | ||
34 | } | ||
35 | |||
36 | if (overwrite && File.Exists(destination)) | ||
37 | { | ||
38 | File.Delete(destination); | ||
39 | } | ||
40 | |||
41 | if (!CreateHardLink(destination, source, IntPtr.Zero)) | ||
42 | { | ||
43 | #if DEBUG | ||
44 | int er = Marshal.GetLastWin32Error(); | ||
45 | #endif | ||
46 | |||
47 | File.Copy(source, destination, overwrite); | ||
48 | } | ||
49 | |||
50 | return true; | ||
51 | } | ||
52 | |||
53 | /// <summary> | ||
54 | /// Moves a file. | ||
55 | /// </summary> | ||
56 | /// <param name="source">The file to move.</param> | ||
57 | /// <param name="destination">The destination file.</param> | ||
58 | public bool MoveFile(string source, string destination, bool overwrite) | ||
59 | { | ||
60 | foreach (var extension in this.Extensions) | ||
61 | { | ||
62 | if (extension.MoveFile(source, destination, overwrite)) | ||
63 | { | ||
64 | return true; | ||
65 | } | ||
66 | } | ||
67 | |||
68 | if (overwrite && File.Exists(destination)) | ||
69 | { | ||
70 | File.Delete(destination); | ||
71 | } | ||
72 | |||
73 | var directory = Path.GetDirectoryName(destination); | ||
74 | if (!String.IsNullOrEmpty(directory)) | ||
75 | { | ||
76 | Directory.CreateDirectory(directory); | ||
77 | } | ||
78 | |||
79 | File.Move(source, destination); | ||
80 | |||
81 | return true; | ||
82 | } | ||
83 | |||
84 | [DllImport("Kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)] | ||
85 | private static extern bool CreateHardLink(string lpFileName, string lpExistingFileName, IntPtr lpSecurityAttributes); | ||
86 | } | ||
87 | } | ||