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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
|
// 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.Data;
using WixToolset.Data.Symbols;
using WixToolset.Data.WindowsInstaller;
using WixToolset.Extensibility;
using WixToolset.Extensibility.Data;
using WixToolset.Extensibility.Services;
/// <summary>
/// Binds a databse.
/// </summary>
internal class BindDatabaseCommand
{
// As outlined in RFC 4122, this is our namespace for generating name-based (version 3) UUIDs.
internal static readonly Guid WixComponentGuidNamespace = new Guid("{3064E5C6-FB63-4FE9-AC49-E446A792EFA5}");
public BindDatabaseCommand(IBindContext context, IEnumerable<IWindowsInstallerBackendBinderExtension> backendExtension, string cubeFile) : this(context, backendExtension, null, cubeFile)
{
}
public BindDatabaseCommand(IBindContext context, IEnumerable<IWindowsInstallerBackendBinderExtension> backendExtension, IEnumerable<SubStorage> subStorages, string cubeFile)
{
this.ServiceProvider = context.ServiceProvider;
this.Messaging = context.ServiceProvider.GetService<IMessaging>();
this.WindowsInstallerBackendHelper = context.ServiceProvider.GetService<IWindowsInstallerBackendHelper>();
this.PathResolver = this.ServiceProvider.GetService<IPathResolver>();
this.CabbingThreadCount = context.CabbingThreadCount;
this.CabCachePath = context.CabCachePath;
this.DefaultCompressionLevel = context.DefaultCompressionLevel;
this.DelayedFields = context.DelayedFields;
this.ExpectedEmbeddedFiles = context.ExpectedEmbeddedFiles;
this.FileSystemManager = new FileSystemManager(context.FileSystemExtensions);
this.Intermediate = context.IntermediateRepresentation;
this.IntermediateFolder = context.IntermediateFolder;
this.OutputPath = context.OutputPath;
this.OutputPdbPath = context.PdbPath;
this.PdbType = context.PdbType;
this.ResolvedCodepage = context.ResolvedCodepage;
this.ResolvedSummaryInformationCodepage = context.ResolvedSummaryInformationCodepage;
this.ResolvedLcid = context.ResolvedLcid;
this.SuppressLayout = context.SuppressLayout;
this.SubStorages = subStorages;
this.SuppressValidation = context.SuppressValidation;
this.Ices = context.Ices;
this.SuppressedIces = context.SuppressIces;
this.CubeFiles = String.IsNullOrEmpty(cubeFile) ? null : new[] { cubeFile };
this.BackendExtensions = backendExtension;
}
public IServiceProvider ServiceProvider { get; }
private IMessaging Messaging { get; }
private IWindowsInstallerBackendHelper WindowsInstallerBackendHelper { get; }
private IPathResolver PathResolver { get; }
private int CabbingThreadCount { get; }
private string CabCachePath { get; }
private CompressionLevel? DefaultCompressionLevel { get; }
public IEnumerable<IDelayedField> DelayedFields { get; }
public IEnumerable<IExpectedExtractFile> ExpectedEmbeddedFiles { get; }
public FileSystemManager FileSystemManager { get; }
public bool DeltaBinaryPatch { get; set; }
private IEnumerable<IWindowsInstallerBackendBinderExtension> BackendExtensions { get; }
private IEnumerable<SubStorage> SubStorages { get; }
private Intermediate Intermediate { get; }
private string OutputPath { get; }
public PdbType PdbType { get; set; }
private string OutputPdbPath { get; }
private int? ResolvedCodepage { get; }
private int? ResolvedSummaryInformationCodepage { get; }
private int? ResolvedLcid { get; }
private bool SuppressAddingValidationRows { get; }
private bool SuppressLayout { get; }
private string IntermediateFolder { get; }
private bool SuppressValidation { get; }
private IEnumerable<string> Ices { get; }
private IEnumerable<string> SuppressedIces { get; }
private IEnumerable<string> CubeFiles { get; }
public IBindResult Execute()
{
if (!this.Intermediate.HasLevel(Data.IntermediateLevels.Linked) || !this.Intermediate.HasLevel(Data.IntermediateLevels.Resolved))
{
this.Messaging.Write(ErrorMessages.IntermediatesMustBeResolved(this.Intermediate.Id));
}
var section = this.Intermediate.Sections.Single();
var packageSymbol = (section.Type == SectionType.Product) ? this.GetSingleSymbol<WixPackageSymbol>(section) : null;
var moduleSymbol = (section.Type == SectionType.Module) ? this.GetSingleSymbol<WixModuleSymbol>(section) : null;
var patchSymbol = (section.Type == SectionType.Patch) ? this.GetSingleSymbol<WixPatchSymbol>(section) : null;
var fileTransfers = new List<IFileTransfer>();
var trackedFiles = new List<ITrackedFile>();
var containsMergeModules = false;
// Load standard tables, authored custom tables, and extension custom tables.
TableDefinitionCollection tableDefinitions;
{
var command = new LoadTableDefinitionsCommand(this.Messaging, section, this.BackendExtensions);
command.Execute();
tableDefinitions = command.TableDefinitions;
}
// Calculate codepage
var codepage = this.CalculateCodepage(packageSymbol, moduleSymbol, patchSymbol);
// Process properties and create the delayed variable cache if needed.
Dictionary<string, string> variableCache = null;
string productLanguage = null;
{
var command = new ProcessPropertiesCommand(section, packageSymbol, this.ResolvedLcid ?? 0, this.DelayedFields.Any(), this.WindowsInstallerBackendHelper);
command.Execute();
variableCache = command.DelayedVariablesCache;
productLanguage = command.ProductLanguage;
}
// Process the summary information table after properties are processed.
bool compressed;
bool longNames;
int installerVersion;
Platform platform;
string modularizationSuffix;
{
var branding = this.ServiceProvider.GetService<IWixBranding>();
var command = new BindSummaryInfoCommand(section, this.ResolvedSummaryInformationCodepage, productLanguage, this.WindowsInstallerBackendHelper, branding);
command.Execute();
compressed = command.Compressed;
longNames = command.LongNames;
installerVersion = command.InstallerVersion;
platform = command.Platform;
modularizationSuffix = command.ModularizationSuffix;
}
// Sequence all the actions.
{
var command = new SequenceActionsCommand(this.Messaging, section);
command.Execute();
}
if (section.Type == SectionType.Product || section.Type == SectionType.Module)
{
var command = new AddRequiredStandardDirectories(section, platform);
command.Execute();
}
{
var command = new CreateSpecialPropertiesCommand(section);
command.Execute();
}
#if TODO_PATCHING
////if (OutputType.Patch == this.Output.Type)
////{
//// foreach (SubStorage substorage in this.Output.SubStorages)
//// {
//// Output transform = substorage.Data;
//// ResolveFieldsCommand command = new ResolveFieldsCommand();
//// command.Tables = transform.Tables;
//// command.FilesWithEmbeddedFiles = filesWithEmbeddedFiles;
//// command.FileManagerCore = this.FileManagerCore;
//// command.FileManagers = this.FileManagers;
//// command.SupportDelayedResolution = false;
//// command.TempFilesLocation = this.TempFilesLocation;
//// command.WixVariableResolver = this.WixVariableResolver;
//// command.Execute();
//// this.MergeUnrealTables(transform.Tables);
//// }
////}
#endif
if (this.Messaging.EncounteredError)
{
return null;
}
this.Intermediate.UpdateLevel(Data.WindowsInstaller.IntermediateLevels.FullyBound);
this.Messaging.Write(VerboseMessages.UpdatingFileInformation());
// Extract files that come from binary .wixlibs and WixExtensions (this does not extract files from merge modules).
{
var extractedFiles = this.WindowsInstallerBackendHelper.ExtractEmbeddedFiles(this.ExpectedEmbeddedFiles);
trackedFiles.AddRange(extractedFiles);
}
// This must occur after all variables and source paths have been resolved.
List<IFileFacade> fileFacades;
if (SectionType.Patch == section.Type)
{
var command = new GetFileFacadesFromTransforms(this.Messaging, this.WindowsInstallerBackendHelper, this.FileSystemManager, this.SubStorages);
command.Execute();
fileFacades = command.FileFacades;
}
else
{
var command = new GetFileFacadesCommand(section, this.WindowsInstallerBackendHelper);
command.Execute();
fileFacades = command.FileFacades;
}
// Retrieve file information from merge modules.
if (SectionType.Product == section.Type)
{
var wixMergeSymbols = section.Symbols.OfType<WixMergeSymbol>().ToList();
if (wixMergeSymbols.Any())
{
containsMergeModules = true;
var command = new ExtractMergeModuleFilesCommand(this.Messaging, this.WindowsInstallerBackendHelper, wixMergeSymbols, fileFacades, installerVersion, this.IntermediateFolder, this.SuppressLayout);
command.Execute();
fileFacades.AddRange(command.MergeModulesFileFacades);
}
}
// stop processing if an error previously occurred
if (this.Messaging.EncounteredError)
{
return null;
}
// Process SoftwareTags in MSI packages.
if (SectionType.Product == section.Type)
{
var softwareTags = section.Symbols.OfType<WixProductTagSymbol>().ToList();
if (softwareTags.Any())
{
var command = new ProcessPackageSoftwareTagsCommand(section, softwareTags, this.IntermediateFolder);
command.Execute();
}
}
// Gather information about files that do not come from merge modules.
{
var command = new UpdateFileFacadesCommand(this.Messaging, section, fileFacades, fileFacades.Where(f => !f.FromModule), variableCache, overwriteHash: true);
command.Execute();
}
// stop processing if an error previously occurred
if (this.Messaging.EncounteredError)
{
return null;
}
// Now that the variable cache is populated, resolve any delayed fields.
if (this.DelayedFields.Any())
{
this.WindowsInstallerBackendHelper.ResolveDelayedFields(this.DelayedFields, variableCache);
}
// Update symbols that reference text files on disk.
{
var command = new UpdateFromTextFilesCommand(this.Messaging, section);
command.Execute();
}
// Add missing CreateFolder symbols to null-keypath components.
{
var command = new AddCreateFoldersCommand(section);
command.Execute();
}
// Process dependency references.
if (SectionType.Product == section.Type || SectionType.Module == section.Type)
{
var dependencyRefs = section.Symbols.OfType<WixDependencyRefSymbol>().ToList();
if (dependencyRefs.Any())
{
var command = new ProcessDependencyReferencesCommand(this.WindowsInstallerBackendHelper, section, dependencyRefs);
command.Execute();
}
}
// If there are any backend extensions, give them the opportunity to process
// the section now that the fields have all be resolved.
//
if (this.BackendExtensions.Any())
{
using (new IntermediateFieldContext("wix.bind.finalize"))
{
foreach (var extension in this.BackendExtensions)
{
extension.SymbolsFinalized(section);
}
var reresolvedFiles = section.Symbols
.OfType<FileSymbol>()
.Where(s => s.Fields.Any(f => f?.Context == "wix.bind.finalize"))
.ToList();
if (reresolvedFiles.Any())
{
var updatedFacades = reresolvedFiles.Select(f => fileFacades.First(ff => ff.Id == f.Id?.Id));
var command = new UpdateFileFacadesCommand(this.Messaging, section, fileFacades, updatedFacades, variableCache, overwriteHash: false);
command.Execute();
}
}
if (this.Messaging.EncounteredError)
{
return null;
}
}
// Set generated component guids.
{
var command = new CalculateComponentGuids(this.Messaging, this.WindowsInstallerBackendHelper, this.PathResolver, section, platform);
command.Execute();
}
{
var command = new ValidateComponentGuidsCommand(this.Messaging, section);
command.Execute();
}
// Assign files to media and update file sequences.
Dictionary<MediaSymbol, IEnumerable<IFileFacade>> filesByCabinetMedia;
IEnumerable<IFileFacade> uncompressedFiles;
{
var order = new OptimizeFileFacadesOrderCommand(this.WindowsInstallerBackendHelper, this.PathResolver, section, platform, fileFacades);
order.Execute();
fileFacades = order.FileFacades;
var assign = new AssignMediaCommand(section, this.Messaging, fileFacades, compressed);
assign.Execute();
filesByCabinetMedia = assign.FileFacadesByCabinetMedia;
uncompressedFiles = assign.UncompressedFileFacades;
var update = new UpdateMediaSequencesCommand(section, fileFacades);
update.Execute();
}
// stop processing if an error previously occurred
if (this.Messaging.EncounteredError)
{
return null;
}
// Time to create the WindowsInstallerData object. Try to put as much above here as possible, updating the IR is better.
WindowsInstallerData data;
{
var command = new CreateWindowsInstallerDataFromIRCommand(this.Messaging, section, tableDefinitions, codepage, this.BackendExtensions, this.WindowsInstallerBackendHelper);
data = command.Execute();
}
IEnumerable<string> suppressedTableNames = null;
if (data.Type == OutputType.Module)
{
// Modularize identifiers.
var modularize = new ModularizeCommand(this.WindowsInstallerBackendHelper, data, modularizationSuffix, section.Symbols.OfType<WixSuppressModularizationSymbol>());
modularize.Execute();
// Ensure all sequence tables in place because, mergemod.dll requires them.
var unsuppress = new AddBackSuppressedSequenceTablesCommand(data, tableDefinitions);
suppressedTableNames = unsuppress.Execute();
}
else if (data.Type == OutputType.Patch)
{
foreach (var storage in this.SubStorages)
{
data.SubStorages.Add(storage);
}
}
// Stop processing if an error previously occurred.
if (this.Messaging.EncounteredError)
{
return null;
}
// Ensure the intermediate folder is created since delta patches will be
// created there.
Directory.CreateDirectory(this.IntermediateFolder);
if (SectionType.Patch == section.Type && this.DeltaBinaryPatch)
{
var command = new CreateDeltaPatchesCommand(fileFacades, this.IntermediateFolder, section.Symbols.OfType<WixPatchSymbol>().FirstOrDefault());
command.Execute();
}
// create cabinet files and process uncompressed files
var layoutDirectory = Path.GetDirectoryName(this.OutputPath);
if (!this.SuppressLayout || OutputType.Module == data.Type)
{
this.Messaging.Write(VerboseMessages.CreatingCabinetFiles());
var mediaTemplate = section.Symbols.OfType<WixMediaTemplateSymbol>().FirstOrDefault();
var command = new CreateCabinetsCommand(this.ServiceProvider, this.WindowsInstallerBackendHelper, mediaTemplate);
command.CabbingThreadCount = this.CabbingThreadCount;
command.CabCachePath = this.CabCachePath;
command.DefaultCompressionLevel = this.DefaultCompressionLevel;
command.Data = data;
command.Messaging = this.Messaging;
command.BackendExtensions = this.BackendExtensions;
command.LayoutDirectory = layoutDirectory;
command.Compressed = compressed;
command.ModularizationSuffix = modularizationSuffix;
command.FileFacadesByCabinet = filesByCabinetMedia;
command.ResolveMedia = this.ResolveMedia;
command.TableDefinitions = tableDefinitions;
command.IntermediateFolder = this.IntermediateFolder;
command.Execute();
fileTransfers.AddRange(command.FileTransfers);
trackedFiles.AddRange(command.TrackedFiles);
}
// stop processing if an error previously occurred
if (this.Messaging.EncounteredError)
{
return null;
}
// We can create instance transforms since Component Guids and Outputs are created.
if (data.Type == OutputType.Product)
{
var command = new CreateInstanceTransformsCommand(section, data, tableDefinitions, this.WindowsInstallerBackendHelper);
command.Execute();
}
else if (data.Type == OutputType.Patch)
{
// Copy output data back into the transforms.
var command = new UpdateTransformsWithFileFacades(this.Messaging, data, this.SubStorages, tableDefinitions, fileFacades);
command.Execute();
}
// Generate database file.
{
this.Messaging.Write(VerboseMessages.GeneratingDatabase());
var trackMsi = this.WindowsInstallerBackendHelper.TrackFile(this.OutputPath, TrackedFileType.Final);
trackedFiles.Add(trackMsi);
var command = new GenerateDatabaseCommand(this.Messaging, this.WindowsInstallerBackendHelper, this.FileSystemManager, data, trackMsi.Path, tableDefinitions, this.IntermediateFolder, keepAddedColumns: false, this.SuppressAddingValidationRows, useSubdirectory: false);
command.Execute();
trackedFiles.AddRange(command.GeneratedTemporaryFiles);
}
// Stop processing if an error previously occurred.
if (this.Messaging.EncounteredError)
{
return null;
}
// Merge modules.
if (containsMergeModules)
{
this.Messaging.Write(VerboseMessages.MergingModules());
var command = new MergeModulesCommand(this.Messaging, fileFacades, section, suppressedTableNames, this.OutputPath, this.IntermediateFolder);
command.Execute();
}
if (this.Messaging.EncounteredError)
{
return null;
}
// Validate the output if there are CUBe files and we're not explicitly suppressing validation.
if (this.CubeFiles != null && !this.SuppressValidation)
{
var command = new ValidateDatabaseCommand(this.Messaging, this.WindowsInstallerBackendHelper, this.IntermediateFolder, data, this.OutputPath, this.CubeFiles, this.Ices, this.SuppressedIces);
command.Execute();
trackedFiles.AddRange(command.TrackedFiles);
}
if (this.Messaging.EncounteredError)
{
return null;
}
// Process uncompressed files.
if (!this.SuppressLayout && uncompressedFiles.Any())
{
var command = new ProcessUncompressedFilesCommand(section, this.WindowsInstallerBackendHelper, this.PathResolver);
command.Compressed = compressed;
command.FileFacades = uncompressedFiles;
command.LayoutDirectory = layoutDirectory;
command.LongNamesInImage = longNames;
command.ResolveMedia = this.ResolveMedia;
command.DatabasePath = this.OutputPath;
command.Execute();
fileTransfers.AddRange(command.FileTransfers);
trackedFiles.AddRange(command.TrackedFiles);
}
// TODO: this is not sufficient to collect all Input files (for example, it misses Binary and Icon tables).
trackedFiles.AddRange(fileFacades.Select(f => this.WindowsInstallerBackendHelper.TrackFile(f.SourcePath, TrackedFileType.Input, f.SourceLineNumber)));
var result = this.ServiceProvider.GetService<IBindResult>();
result.FileTransfers = fileTransfers;
result.TrackedFiles = trackedFiles;
result.Wixout = this.CreateWixout(trackedFiles, this.Intermediate, data);
return result;
}
private int CalculateCodepage(WixPackageSymbol packageSymbol, WixModuleSymbol moduleSymbol, WixPatchSymbol patchSymbol)
{
var codepage = packageSymbol?.Codepage ?? moduleSymbol?.Codepage ?? patchSymbol?.Codepage;
if (String.IsNullOrEmpty(codepage))
{
codepage = this.ResolvedCodepage?.ToString() ?? "65001";
if (packageSymbol != null)
{
packageSymbol.Codepage = codepage;
}
else if (moduleSymbol != null)
{
moduleSymbol.Codepage = codepage;
}
else if (patchSymbol != null)
{
patchSymbol.Codepage = codepage;
}
}
return this.WindowsInstallerBackendHelper.GetValidCodePage(codepage);
}
private T GetSingleSymbol<T>(IntermediateSection section) where T : IntermediateSymbol
{
var symbols = section.Symbols.OfType<T>().ToList();
if (1 != symbols.Count)
{
throw new WixException(ErrorMessages.MissingBundleInformation(nameof(T)));
}
return symbols[0];
}
private WixOutput CreateWixout(List<ITrackedFile> trackedFiles, Intermediate intermediate, WindowsInstallerData data)
{
WixOutput wixout;
if (String.IsNullOrEmpty(this.OutputPdbPath))
{
wixout = WixOutput.Create();
}
else
{
var trackPdb = this.WindowsInstallerBackendHelper.TrackFile(this.OutputPdbPath, TrackedFileType.Final);
trackedFiles.Add(trackPdb);
wixout = WixOutput.Create(trackPdb.Path);
}
intermediate.Save(wixout);
data.Save(wixout);
wixout.Reopen();
return wixout;
}
private string ResolveMedia(MediaSymbol media, string mediaLayoutDirectory, string layoutDirectory)
{
string layout = null;
foreach (var extension in this.BackendExtensions)
{
layout = extension.ResolveMedia(media, mediaLayoutDirectory, layoutDirectory);
if (!String.IsNullOrEmpty(layout))
{
break;
}
}
// If no binder file manager resolved the layout, do the default behavior.
if (String.IsNullOrEmpty(layout))
{
if (String.IsNullOrEmpty(mediaLayoutDirectory))
{
layout = layoutDirectory;
}
else if (Path.IsPathRooted(mediaLayoutDirectory))
{
layout = mediaLayoutDirectory;
}
else
{
layout = Path.Combine(layoutDirectory, mediaLayoutDirectory);
}
}
return layout;
}
}
}
|