aboutsummaryrefslogtreecommitdiff
path: root/src/WixToolset.Core.WindowsInstaller/Bind/GetFileFacadesFromTransforms.cs
blob: 585bdac03f0159277fbcc41428830a114a9bedf0 (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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// 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.WindowsInstaller.Bind
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using WixToolset.Core.Bind;
    using WixToolset.Data;
    using WixToolset.Data.WindowsInstaller;
    using WixToolset.Data.WindowsInstaller.Rows;
    using WixToolset.Extensibility;
    using WixToolset.Extensibility.Services;

    internal class GetFileFacadesFromTransforms
    {
        public GetFileFacadesFromTransforms(IMessaging messaging, FileSystemManager fileSystemManager, IEnumerable<SubStorage> subStorages)
        {
            this.Messaging = messaging;
            this.FileSystemManager = fileSystemManager;
            this.SubStorages = subStorages;
        }

        private IMessaging Messaging { get; }

        private FileSystemManager FileSystemManager { get; }

        private IEnumerable<SubStorage> SubStorages { get; }

        public List<FileFacade> FileFacades { get; private set; }

        public void Execute()
        {
            var allFileRows = new List<FileFacade>();

            var patchMediaRows = new RowDictionary<MediaRow>();

            var patchMediaFileRows = new Dictionary<int, RowDictionary<FileRow>>();

            //var patchActualFileTable = this.Output.EnsureTable(this.TableDefinitions["File"]);

            // Index paired transforms by name without their "#" prefix.
            var pairedTransforms = this.SubStorages.Where(s => s.Name.StartsWith("#")).ToDictionary(s => s.Name, s => s.Data);

            // Enumerate through main transforms.
            foreach (var substorage in this.SubStorages.Where(s => !s.Name.StartsWith("#")))
            {
                var mainTransform = substorage.Data;
                var mainFileTable = mainTransform.Tables["File"];

                if (null == mainFileTable)
                {
                    continue;
                }

                // Index File table of pairedTransform
                var pairedTransform = pairedTransforms["#" + substorage.Name];
                var pairedFileRows = new RowDictionary<FileRow>(pairedTransform.Tables["File"]);

                foreach (FileRow mainFileRow in mainFileTable.Rows.Where(f => f.Operation != RowOperation.Delete))
                {
                    var mainFileId = mainFileRow.File;

                    // We need compare the underlying files and include all file changes.
                    var objectField = (ObjectField)mainFileRow.Fields[9];
                    var pairedFileRow = pairedFileRows.Get(mainFileId);

                    // If the file is new, we always need to add it to the patch.
                    if (mainFileRow.Operation == RowOperation.Add)
                    {
                        if (null != pairedFileRow) // RowOperation.Add
                        {
                            // Always patch-added, but never non-compressed.
                            pairedFileRow.Attributes |= WindowsInstallerConstants.MsidbFileAttributesPatchAdded;
                            pairedFileRow.Attributes &= ~WindowsInstallerConstants.MsidbFileAttributesNoncompressed;
                            pairedFileRow.Fields[6].Modified = true;
                            pairedFileRow.Operation = RowOperation.Add;
                        }
                    }
                    else
                    {
                        // If PreviousData doesn't exist, target and upgrade layout point to the same location. No need to compare.
                        if (null == objectField.PreviousData)
                        {
                            if (mainFileRow.Operation == RowOperation.None)
                            {
                                continue;
                            }
                        }
                        else
                        {
                            // TODO: should this entire condition be placed in the binder file manager?
                            if (/*(0 == (PatchAttributeType.Ignore & mainWixFileRow.PatchAttributes)) &&*/
                                !this.FileSystemManager.CompareFiles(objectField.PreviousData.ToString(), objectField.Data.ToString()))
                            {
                                // If the file is different, we need to mark the mainFileRow and pairedFileRow as modified.
                                mainFileRow.Operation = RowOperation.Modify;
                                if (null != pairedFileRow)
                                {
                                    // Always patch-added, but never non-compressed.
                                    pairedFileRow.Attributes |= WindowsInstallerConstants.MsidbFileAttributesPatchAdded;
                                    pairedFileRow.Attributes &= ~WindowsInstallerConstants.MsidbFileAttributesNoncompressed;
                                    pairedFileRow.Fields[6].Modified = true;
                                    pairedFileRow.Operation = RowOperation.Modify;
                                }
                            }
                            else
                            {
                                // The File is same. We need mark all the attributes as unchanged.
                                mainFileRow.Operation = RowOperation.None;
                                foreach (var field in mainFileRow.Fields)
                                {
                                    field.Modified = false;
                                }

                                if (null != pairedFileRow)
                                {
                                    pairedFileRow.Attributes &= ~WindowsInstallerConstants.MsidbFileAttributesPatchAdded;
                                    pairedFileRow.Fields[6].Modified = false;
                                    pairedFileRow.Operation = RowOperation.None;
                                }
                                continue;
                            }
                        }
                    }

                    // index patch files by diskId+fileId
                    var diskId = mainFileRow.DiskId;

                    if (!patchMediaFileRows.TryGetValue(diskId, out var mediaFileRows))
                    {
                        mediaFileRows = new RowDictionary<FileRow>();
                        patchMediaFileRows.Add(diskId, mediaFileRows);
                    }

                    var patchFileRow = mediaFileRows.Get(mainFileId);

                    if (null == patchFileRow)
                    {
                        //patchFileRow = (FileRow)patchFileTable.CreateRow(mainFileRow.SourceLineNumbers);
                        patchFileRow = (FileRow)mainFileRow.TableDefinition.CreateRow(mainFileRow.SourceLineNumbers);
                        mainFileRow.CopyTo(patchFileRow);

                        mediaFileRows.Add(patchFileRow);

                        allFileRows.Add(new FileFacade(patchFileRow)); // TODO: should we be passing along delta information? Probably, right?
                    }
                    else
                    {
                        // TODO: confirm the rest of data is identical?

                        // make sure Source is same. Otherwise we are silently ignoring a file.
                        if (0 != String.Compare(patchFileRow.Source, mainFileRow.Source, StringComparison.OrdinalIgnoreCase))
                        {
                            this.Messaging.Write(ErrorMessages.SameFileIdDifferentSource(mainFileRow.SourceLineNumbers, mainFileId, patchFileRow.Source, mainFileRow.Source));
                        }

#if TODO_PATCHING_DELTA
                        // capture the previous file versions (and associated data) from this targeted instance of the baseline into the current filerow.
                        patchFileRow.AppendPreviousDataFrom(mainFileRow);
#endif
                    }
                }
            }

            this.FileFacades = allFileRows;
        }
    }
}