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
|
// 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.Bundles
{
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Text;
using System.Xml;
using WixToolset.Data;
using WixToolset.Data.Rows;
using Dtf = WixToolset.Dtf.WindowsInstaller;
/// <summary>
/// Initializes package state from the Msp contents.
/// </summary>
internal class ProcessMspPackageCommand : ICommand
{
private const string PatchMetadataFormat = "SELECT `Value` FROM `MsiPatchMetadata` WHERE `Property` = '{0}'";
private static readonly Encoding XmlOutputEncoding = new UTF8Encoding(false);
public RowDictionary<WixBundlePayloadRow> AuthoredPayloads { private get; set; }
public PackageFacade Facade { private get; set; }
public Table WixBundlePatchTargetCodeTable { private get; set; }
/// <summary>
/// Processes the Msp packages to add properties and payloads from the Msp packages.
/// </summary>
public void Execute()
{
WixBundlePayloadRow packagePayload = this.AuthoredPayloads.Get(this.Facade.Package.PackagePayload);
string sourcePath = packagePayload.FullFileName;
try
{
// Read data out of the msp database...
using (Dtf.SummaryInfo sumInfo = new Dtf.SummaryInfo(sourcePath, false))
{
this.Facade.MspPackage.PatchCode = sumInfo.RevisionNumber.Substring(0, 38);
}
using (Dtf.Database db = new Dtf.Database(sourcePath))
{
if (String.IsNullOrEmpty(this.Facade.Package.DisplayName))
{
this.Facade.Package.DisplayName = ProcessMspPackageCommand.GetPatchMetadataProperty(db, "DisplayName");
}
if (String.IsNullOrEmpty(this.Facade.Package.Description))
{
this.Facade.Package.Description = ProcessMspPackageCommand.GetPatchMetadataProperty(db, "Description");
}
this.Facade.MspPackage.Manufacturer = ProcessMspPackageCommand.GetPatchMetadataProperty(db, "ManufacturerName");
}
this.ProcessPatchXml(packagePayload, sourcePath);
}
catch (Dtf.InstallerException e)
{
Messaging.Instance.OnMessage(WixErrors.UnableToReadPackageInformation(packagePayload.SourceLineNumbers, sourcePath, e.Message));
return;
}
if (String.IsNullOrEmpty(this.Facade.Package.CacheId))
{
this.Facade.Package.CacheId = this.Facade.MspPackage.PatchCode;
}
}
private void ProcessPatchXml(WixBundlePayloadRow packagePayload, string sourcePath)
{
HashSet<string> uniqueTargetCodes = new HashSet<string>();
string patchXml = Dtf.Installer.ExtractPatchXmlData(sourcePath);
XmlDocument doc = new XmlDocument();
doc.LoadXml(patchXml);
XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("p", "http://www.microsoft.com/msi/patch_applicability.xsd");
// Determine target ProductCodes and/or UpgradeCodes.
foreach (XmlNode node in doc.SelectNodes("/p:MsiPatch/p:TargetProduct", nsmgr))
{
// If this patch targets a product code, this is the best case.
XmlNode targetCodeElement = node.SelectSingleNode("p:TargetProductCode", nsmgr);
WixBundlePatchTargetCodeAttributes attributes = WixBundlePatchTargetCodeAttributes.None;
if (ProcessMspPackageCommand.TargetsCode(targetCodeElement))
{
attributes = WixBundlePatchTargetCodeAttributes.TargetsProductCode;
}
else // maybe targets an upgrade code?
{
targetCodeElement = node.SelectSingleNode("p:UpgradeCode", nsmgr);
if (ProcessMspPackageCommand.TargetsCode(targetCodeElement))
{
attributes = WixBundlePatchTargetCodeAttributes.TargetsUpgradeCode;
}
else // this patch targets an unknown number of products
{
this.Facade.MspPackage.Attributes |= WixBundleMspPackageAttributes.TargetUnspecified;
}
}
string targetCode = targetCodeElement.InnerText;
if (uniqueTargetCodes.Add(targetCode))
{
WixBundlePatchTargetCodeRow row = (WixBundlePatchTargetCodeRow)this.WixBundlePatchTargetCodeTable.CreateRow(packagePayload.SourceLineNumbers);
row.MspPackageId = packagePayload.Id;
row.TargetCode = targetCode;
row.Attributes = attributes;
}
}
// Suppress patch sequence data for improved performance.
XmlNode root = doc.DocumentElement;
foreach (XmlNode node in root.SelectNodes("p:SequenceData", nsmgr))
{
root.RemoveChild(node);
}
// Save the XML as compact as possible.
using (StringWriter writer = new StringWriter())
{
XmlWriterSettings settings = new XmlWriterSettings()
{
Encoding = ProcessMspPackageCommand.XmlOutputEncoding,
Indent = false,
NewLineChars = string.Empty,
NewLineHandling = NewLineHandling.Replace,
};
using (XmlWriter xmlWriter = XmlWriter.Create(writer, settings))
{
doc.WriteTo(xmlWriter);
}
this.Facade.MspPackage.PatchXml = writer.ToString();
}
}
/// <summary>
/// Queries a Windows Installer patch database for a Property value from the MsiPatchMetadata table.
/// </summary>
/// <param name="db">Database to query.</param>
/// <param name="property">Property to examine.</param>
/// <returns>String value for result or null if query doesn't match a single result.</returns>
private static string GetPatchMetadataProperty(Dtf.Database db, string property)
{
try
{
return db.ExecuteScalar(PatchMetadataPropertyQuery(property)).ToString();
}
catch (Dtf.InstallerException)
{
}
return null;
}
private static string PatchMetadataPropertyQuery(string property)
{
// quick sanity check that we'll be creating a valid query...
// TODO: Are there any other special characters we should be looking for?
Debug.Assert(!property.Contains("'"));
return String.Format(CultureInfo.InvariantCulture, ProcessMspPackageCommand.PatchMetadataFormat, property);
}
private static bool TargetsCode(XmlNode node)
{
if (null != node)
{
XmlAttribute attr = node.Attributes["Validate"];
return null != attr && "true".Equals(attr.Value);
}
return false;
}
}
}
|