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
170
171
172
173
174
175
176
177
178
|
// 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.IO;
using System.Linq;
using WixToolset.Bind;
using WixToolset.Core.Native;
using WixToolset.Data;
using WixToolset.Data.Tuples;
using WixToolset.Extensibility.Services;
/// <summary>
/// Set the guids for components with generatable guids.
/// </summary>
internal class CalculateComponentGuids
{
public CalculateComponentGuids(IMessaging messaging, IntermediateSection section)
{
this.Messaging = messaging;
this.Section = section;
}
private IMessaging Messaging { get; }
private IntermediateSection Section { get; }
public void Execute()
{
Dictionary<string, RegistryTuple> registryKeyRows = null;
Dictionary<string, ResolvedDirectory> targetPathsByDirectoryId = null;
Dictionary<string, string> componentIdGenSeeds = null;
Dictionary<string, List<FileTuple>> filesByComponentId = null;
// Find components with generatable guids.
foreach (var componentRow in this.Section.Tuples.OfType<ComponentTuple>())
{
// Skip components that do not specify generate guid.
if (componentRow.ComponentId != "*")
{
continue;
}
var odbcDataSourceKeyPath = (componentRow.Attributes & MsiInterop.MsidbComponentAttributesODBCDataSource) != 0;
if (String.IsNullOrEmpty(componentRow.KeyPath) || odbcDataSourceKeyPath)
{
this.Messaging.Write(ErrorMessages.IllegalComponentWithAutoGeneratedGuid(componentRow.SourceLineNumbers));
continue;
}
var registryKeyPath = (componentRow.Attributes & MsiInterop.MsidbComponentAttributesRegistryKeyPath) != 0;
if (registryKeyPath)
{
if (registryKeyRows is null)
{
registryKeyRows = this.Section.Tuples.OfType<RegistryTuple>().ToDictionary(t => t.Registry);
}
if (registryKeyRows.TryGetValue(componentRow.KeyPath, out var foundRow))
{
var is64Bit = (componentRow.Attributes & MsiInterop.MsidbComponentAttributes64bit) != 0;
var bitness = is64Bit ? "64" : String.Empty;
var regkey = String.Concat(bitness, foundRow[1], "\\", foundRow[2], "\\", foundRow[3]);
componentRow.ComponentId = Uuid.NewUuid(BindDatabaseCommand.WixComponentGuidNamespace, regkey.ToLowerInvariant()).ToString("B").ToUpperInvariant();
}
}
else // must be a File KeyPath.
{
// If the directory table hasn't been loaded into an indexed hash
// of directory ids to target names do that now.
if (targetPathsByDirectoryId is null)
{
var directories = this.Section.Tuples.OfType<DirectoryTuple>().ToList();
targetPathsByDirectoryId = new Dictionary<string, ResolvedDirectory>(directories.Count);
// Get the target paths for all directories.
foreach (var row in directories)
{
// If the directory Id already exists, we will skip it here since
// checking for duplicate primary keys is done later when importing tables
// into database
if (targetPathsByDirectoryId.ContainsKey(row.Directory))
{
continue;
}
var targetName = Common.GetName(row.DefaultDir, false, true);
targetPathsByDirectoryId.Add(row.Directory, new ResolvedDirectory(row.Directory_Parent, targetName));
}
}
// If the component id generation seeds have not been indexed
// from the WixDirectory table do that now.
if (componentIdGenSeeds is null)
{
// If there are any WixDirectory rows, build up the Component Guid
// generation seeds indexed by Directory/@Id.
componentIdGenSeeds = this.Section.Tuples.OfType<WixDirectoryTuple>()
.Where(t => !String.IsNullOrEmpty(t.ComponentGuidGenerationSeed))
.ToDictionary(t => t.Directory_, t => t.ComponentGuidGenerationSeed);
}
// if the file rows have not been indexed by File.Component yet
// then do that now
if (filesByComponentId is null)
{
var files = this.Section.Tuples.OfType<FileTuple>().ToList();
filesByComponentId = new Dictionary<string, List<FileTuple>>(files.Count);
foreach (var file in files)
{
if (!filesByComponentId.TryGetValue(file.Component_, out var componentFiles))
{
componentFiles = new List<FileTuple>();
filesByComponentId.Add(file.Component_, componentFiles);
}
componentFiles.Add(file);
}
}
// validate component meets all the conditions to have a generated guid
var currentComponentFiles = filesByComponentId[componentRow.Component];
var numFilesInComponent = currentComponentFiles.Count;
string path = null;
foreach (var fileRow in currentComponentFiles)
{
if (fileRow.File == componentRow.KeyPath)
{
// calculate the key file's canonical target path
string directoryPath = Binder.GetDirectoryPath(targetPathsByDirectoryId, componentIdGenSeeds, componentRow.Directory_, true);
string fileName = Common.GetName(fileRow.LongFileName, false, true).ToLowerInvariant();
path = Path.Combine(directoryPath, fileName);
// find paths that are not canonicalized
if (path.StartsWith(@"PersonalFolder\my pictures", StringComparison.Ordinal) ||
path.StartsWith(@"ProgramFilesFolder\common files", StringComparison.Ordinal) ||
path.StartsWith(@"ProgramMenuFolder\startup", StringComparison.Ordinal) ||
path.StartsWith("TARGETDIR", StringComparison.Ordinal) ||
path.StartsWith(@"StartMenuFolder\programs", StringComparison.Ordinal) ||
path.StartsWith(@"WindowsFolder\fonts", StringComparison.Ordinal))
{
this.Messaging.Write(ErrorMessages.IllegalPathForGeneratedComponentGuid(componentRow.SourceLineNumbers, fileRow.Component_, path));
}
// if component has more than one file, the key path must be versioned
if (1 < numFilesInComponent && String.IsNullOrEmpty(fileRow.Version))
{
this.Messaging.Write(ErrorMessages.IllegalGeneratedGuidComponentUnversionedKeypath(componentRow.SourceLineNumbers));
}
}
else
{
// not a key path, so it must be an unversioned file if component has more than one file
if (1 < numFilesInComponent && !String.IsNullOrEmpty(fileRow.Version))
{
this.Messaging.Write(ErrorMessages.IllegalGeneratedGuidComponentVersionedNonkeypath(componentRow.SourceLineNumbers));
}
}
}
// if the rules were followed, reward with a generated guid
if (!this.Messaging.EncounteredError)
{
componentRow.ComponentId = Uuid.NewUuid(BindDatabaseCommand.WixComponentGuidNamespace, path).ToString("B").ToUpperInvariant();
}
}
}
}
}
}
|