aboutsummaryrefslogtreecommitdiff
path: root/src/WixToolset.Core/Bind/FileTransfer.cs
blob: 64bbc5f186041888e57c00dd23c67cca4c88db34 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// 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.Bind
{
    using System;
    using System.IO;
    using WixToolset;
    using WixToolset.Data;

    /// <summary>
    /// Structure used for all file transfer information.
    /// </summary>
    internal class FileTransfer
    {
        /// <summary>Source path to file.</summary>
        public string Source { get; set; }

        /// <summary>Destination path for file.</summary>
        public string Destination { get; set; }

        /// <summary>Flag if file should be moved (optimal).</summary>
        public bool Move { get; set; }

        /// <summary>Optional source line numbers where this file transfer orginated.</summary>
        public SourceLineNumber SourceLineNumbers { get; set; }

        /// <summary>Optional type of file this transfer is moving or copying.</summary>
        public string Type { get; set; }

        /// <summary>Indicates whether the file transer was a built by this build or copied from other some build.</summary>
        internal bool Built { get; set; }

        /// <summary>Set during layout of media when the file transfer when the source and target resolve to the same path.</summary>
        internal bool Redundant { get; set; }

        /// <summary>
        /// Prefer the TryCreate() method to create FileTransfer objects.
        /// </summary>
        /// <param name="source">Source path to file.</param>
        /// <param name="destination">Destination path for file.</param>
        /// <param name="move">File if file should be moved (optimal).</param>
        /// <param name="type">Optional type of file this transfer is transferring.</param>
        /// <param name="sourceLineNumbers">Optional source line numbers wher this transfer originated.</param>
        public FileTransfer(string source, string destination, bool move, string type = null, SourceLineNumber sourceLineNumbers = null)
        {
            this.Source = source;
            this.Destination = destination;
            this.Move = move;

            this.Type = type;
            this.SourceLineNumbers = sourceLineNumbers;
        }

        /// <summary>
        /// Creates a file transfer if the source and destination are different.
        /// </summary>
        /// <param name="source">Source path to file.</param>
        /// <param name="destination">Destination path for file.</param>
        /// <param name="move">File if file should be moved (optimal).</param>
        /// <param name="type">Optional type of file this transfer is transferring.</param>
        /// <param name="sourceLineNumbers">Optional source line numbers wher this transfer originated.</param>
        /// <returns>true if the source and destination are the different, false if no file transfer is created.</returns>
        public static bool TryCreate(string source, string destination, bool move, string type, SourceLineNumber sourceLineNumbers, out FileTransfer transfer)
        {
            string sourceFullPath = GetValidatedFullPath(sourceLineNumbers, source);

            string fileLayoutFullPath = GetValidatedFullPath(sourceLineNumbers, destination);

            // if the current source path (where we know that the file already exists) and the resolved
            // path as dictated by the Directory table are not the same, then propagate the file.  The
            // image that we create may have already been done by some other process other than the linker, so 
            // there is no reason to copy the files to the resolved source if they are already there.
            if (String.Equals(sourceFullPath, fileLayoutFullPath, StringComparison.OrdinalIgnoreCase))
            {
                transfer = null;
                return false;
            }

            transfer = new FileTransfer(source, destination, move, type, sourceLineNumbers);
            return true;
        }

        private static string GetValidatedFullPath(SourceLineNumber sourceLineNumbers, string path)
        {
            string result;

            try
            {
                result = Path.GetFullPath(path);

                string filename = Path.GetFileName(result);

                foreach (string reservedName in Common.ReservedFileNames)
                {
                    if (reservedName.Equals(filename, StringComparison.OrdinalIgnoreCase))
                    {
                        throw new WixException(WixErrors.InvalidFileName(sourceLineNumbers, path));
                    }
                }
            }
            catch (System.ArgumentException)
            {
                throw new WixException(WixErrors.InvalidFileName(sourceLineNumbers, path));
            }
            catch (System.IO.PathTooLongException)
            {
                throw new WixException(WixErrors.PathTooLong(sourceLineNumbers, path));
            }

            return result;
        }
    }
}