diff options
Diffstat (limited to 'src/WixToolset.Data/Bind/FileTransfer.cs')
| -rw-r--r-- | src/WixToolset.Data/Bind/FileTransfer.cs | 111 |
1 files changed, 111 insertions, 0 deletions
diff --git a/src/WixToolset.Data/Bind/FileTransfer.cs b/src/WixToolset.Data/Bind/FileTransfer.cs new file mode 100644 index 00000000..178934e8 --- /dev/null +++ b/src/WixToolset.Data/Bind/FileTransfer.cs | |||
| @@ -0,0 +1,111 @@ | |||
| 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.Data.Bind | ||
| 4 | { | ||
| 5 | using System; | ||
| 6 | using System.IO; | ||
| 7 | |||
| 8 | /// <summary> | ||
| 9 | /// Structure used for all file transfer information. | ||
| 10 | /// </summary> | ||
| 11 | public class FileTransfer | ||
| 12 | { | ||
| 13 | /// <summary>Source path to file.</summary> | ||
| 14 | public string Source { get; set; } | ||
| 15 | |||
| 16 | /// <summary>Destination path for file.</summary> | ||
| 17 | public string Destination { get; set; } | ||
| 18 | |||
| 19 | /// <summary>Flag if file should be moved (optimal).</summary> | ||
| 20 | public bool Move { get; set; } | ||
| 21 | |||
| 22 | /// <summary>Optional source line numbers where this file transfer orginated.</summary> | ||
| 23 | public SourceLineNumber SourceLineNumbers { get; set; } | ||
| 24 | |||
| 25 | /// <summary>Optional type of file this transfer is moving or copying.</summary> | ||
| 26 | public string Type { get; set; } | ||
| 27 | |||
| 28 | /// <summary>Indicates whether the file transer was a built by this build or copied from other some build.</summary> | ||
| 29 | public bool Built { get; set; } | ||
| 30 | |||
| 31 | /// <summary>Set during layout of media when the file transfer when the source and target resolve to the same path.</summary> | ||
| 32 | public bool Redundant { get; set; } | ||
| 33 | |||
| 34 | /// <summary> | ||
| 35 | /// Prefer the TryCreate() method to create FileTransfer objects. | ||
| 36 | /// </summary> | ||
| 37 | /// <param name="source">Source path to file.</param> | ||
| 38 | /// <param name="destination">Destination path for file.</param> | ||
| 39 | /// <param name="move">File if file should be moved (optimal).</param> | ||
| 40 | /// <param name="type">Optional type of file this transfer is transferring.</param> | ||
| 41 | /// <param name="sourceLineNumbers">Optional source line numbers wher this transfer originated.</param> | ||
| 42 | public FileTransfer(string source, string destination, bool move, string type = null, SourceLineNumber sourceLineNumbers = null) | ||
| 43 | { | ||
| 44 | this.Source = source; | ||
| 45 | this.Destination = destination; | ||
| 46 | this.Move = move; | ||
| 47 | |||
| 48 | this.Type = type; | ||
| 49 | this.SourceLineNumbers = sourceLineNumbers; | ||
| 50 | } | ||
| 51 | |||
| 52 | /// <summary> | ||
| 53 | /// Creates a file transfer if the source and destination are different. | ||
| 54 | /// </summary> | ||
| 55 | /// <param name="source">Source path to file.</param> | ||
| 56 | /// <param name="destination">Destination path for file.</param> | ||
| 57 | /// <param name="move">File if file should be moved (optimal).</param> | ||
| 58 | /// <param name="type">Optional type of file this transfer is transferring.</param> | ||
| 59 | /// <param name="sourceLineNumbers">Optional source line numbers where this transfer originated.</param> | ||
| 60 | /// <returns>true if the source and destination are the different, false if no file transfer is created.</returns> | ||
| 61 | public static bool TryCreate(string source, string destination, bool move, string type, SourceLineNumber sourceLineNumbers, out FileTransfer transfer) | ||
| 62 | { | ||
| 63 | string sourceFullPath = GetValidatedFullPath(sourceLineNumbers, source); | ||
| 64 | |||
| 65 | string fileLayoutFullPath = GetValidatedFullPath(sourceLineNumbers, destination); | ||
| 66 | |||
| 67 | // if the current source path (where we know that the file already exists) and the resolved | ||
| 68 | // path as dictated by the Directory table are not the same, then propagate the file. The | ||
| 69 | // image that we create may have already been done by some other process other than the linker, so | ||
| 70 | // there is no reason to copy the files to the resolved source if they are already there. | ||
| 71 | if (String.Equals(sourceFullPath, fileLayoutFullPath, StringComparison.OrdinalIgnoreCase)) | ||
| 72 | { | ||
| 73 | transfer = null; | ||
| 74 | return false; | ||
| 75 | } | ||
| 76 | |||
| 77 | transfer = new FileTransfer(source, destination, move, type, sourceLineNumbers); | ||
| 78 | return true; | ||
| 79 | } | ||
| 80 | |||
| 81 | private static string GetValidatedFullPath(SourceLineNumber sourceLineNumbers, string path) | ||
| 82 | { | ||
| 83 | string result; | ||
| 84 | |||
| 85 | try | ||
| 86 | { | ||
| 87 | result = Path.GetFullPath(path); | ||
| 88 | |||
| 89 | string filename = Path.GetFileName(result); | ||
| 90 | |||
| 91 | foreach (string reservedName in Common.ReservedFileNames) | ||
| 92 | { | ||
| 93 | if (reservedName.Equals(filename, StringComparison.OrdinalIgnoreCase)) | ||
| 94 | { | ||
| 95 | throw new WixException(WixDataErrors.InvalidFileName(sourceLineNumbers, path)); | ||
| 96 | } | ||
| 97 | } | ||
| 98 | } | ||
| 99 | catch (System.ArgumentException) | ||
| 100 | { | ||
| 101 | throw new WixException(WixDataErrors.InvalidFileName(sourceLineNumbers, path)); | ||
| 102 | } | ||
| 103 | catch (System.IO.PathTooLongException) | ||
| 104 | { | ||
| 105 | throw new WixException(WixDataErrors.PathTooLong(sourceLineNumbers, path)); | ||
| 106 | } | ||
| 107 | |||
| 108 | return result; | ||
| 109 | } | ||
| 110 | } | ||
| 111 | } | ||
