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
|
// 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.Dtf.WindowsInstaller.Package
{
using System;
using System.IO;
using System.Text;
using System.Collections;
using System.Globalization;
using System.Runtime.InteropServices;
/// <summary>
/// Provides access to convenient properties and operations on a patch package (.MSP).
/// </summary>
public class PatchPackage : Database
{
/// <summary>
/// Creates a new patch package object; opening the patch database in read-only mode.
/// </summary>
/// <param name="packagePath">Path to the patch package (.MSP)</param>
/// <remarks>The PatchPackage object only opens the patch database in read-only mode, because
/// transforms (sub-storages) cannot be read if the database is open in read-write mode.</remarks>
public PatchPackage(string packagePath)
: base(packagePath, (DatabaseOpenMode) ((int) DatabaseOpenMode.ReadOnly | 32))
// TODO: figure out what to do about DatabaseOpenMode.Patch
{
}
/// <summary>
/// Handle this event to receive status messages when operations are performed on the patch package.
/// </summary>
/// <example>
/// <c>patchPackage.Message += new InstallPackageMessageHandler(Console.WriteLine);</c>
/// </example>
public event InstallPackageMessageHandler Message;
/// <summary>
/// Sends a message to the <see cref="Message"/> event-handler.
/// </summary>
/// <param name="format">Message string, containing 0 or more format items</param>
/// <param name="args">Items to be formatted</param>
protected void LogMessage(string format, params object[] args)
{
if(this.Message != null)
{
this.Message(format, args);
}
}
/// <summary>
/// Gets the patch code (GUID) of the patch package.
/// </summary>
/// <remarks>
/// The patch code is stored in the RevisionNumber field of the patch summary information.
/// </remarks>
public string PatchCode
{
get
{
string guids = this.SummaryInfo.RevisionNumber;
return guids.Substring(0, guids.IndexOf('}') + 1);
}
}
/// <summary>
/// Gets the list of patch codes that are replaced by this patch package.
/// </summary>
/// <returns>Array of replaced patch codes (GUIDs)</returns>
/// <remarks>
/// The list of replaced patch codes is stored in the RevisionNumber field of the patch summary information.
/// </remarks>
public string[] GetReplacedPatchCodes()
{
ArrayList patchCodeList = new ArrayList();
string guids = this.SummaryInfo.RevisionNumber;
int thisGuid = guids.IndexOf('}') + 1;
int nextGuid = guids.IndexOf('}', thisGuid) + 1;
while(nextGuid > 0)
{
patchCodeList.Add(guids.Substring(thisGuid, (nextGuid - thisGuid)));
thisGuid = nextGuid;
nextGuid = guids.IndexOf('}', thisGuid) + 1;
}
return (string[]) patchCodeList.ToArray(typeof(string));
}
/// <summary>
/// Gets the list of product codes of products targeted by this patch package.
/// </summary>
/// <returns>Array of product codes (GUIDs)</returns>
/// <remarks>
/// The list of target product codes is stored in the Template field of the patch summary information.
/// </remarks>
public string[] GetTargetProductCodes()
{
string productList = this.SummaryInfo.Template;
return productList.Split(';');
}
/// <summary>
/// Gets the names of the transforms included in the patch package.
/// </summary>
/// <returns>Array of transform names</returns>
/// <remarks>
/// The returned list does not include the "patch special transforms" that are prefixed with "#"
/// <p>The list of transform names is stored in the LastSavedBy field of the patch summary information.</p>
/// </remarks>
public string[] GetTransforms()
{
return this.GetTransforms(false);
}
/// <summary>
/// Gets the names of the transforms included in the patch package.
/// </summary>
/// <param name="includeSpecialTransforms">Specifies whether to include the
/// "patch special transforms" that are prefixed with "#"</param>
/// <returns>Array of transform names</returns>
/// <remarks>
/// The list of transform names is stored in the LastSavedBy field of the patch summary information.
/// </remarks>
public string[] GetTransforms(bool includeSpecialTransforms)
{
ArrayList transformArray = new ArrayList();
string transformList = this.SummaryInfo.LastSavedBy;
foreach(string transform in transformList.Split(';', ':'))
{
if(transform.Length != 0 && (includeSpecialTransforms || !transform.StartsWith("#", StringComparison.Ordinal)))
{
transformArray.Add(transform);
}
}
return (string[]) transformArray.ToArray(typeof(string));
}
/// <summary>
/// Gets information about the transforms included in the patch package.
/// </summary>
/// <returns>Array containing information about each transform</returns>
/// <remarks>
/// The returned info does not include the "patch special transforms" that are prefixed with "#"
/// </remarks>
public TransformInfo[] GetTransformsInfo()
{
return this.GetTransformsInfo(false);
}
/// <summary>
/// Gets information about the transforms included in the patch package.
/// </summary>
/// <param name="includeSpecialTransforms">Specifies whether to include the
/// "patch special transforms" that are prefixed with "#"</param>
/// <returns>Array containing information about each transform</returns>
public TransformInfo[] GetTransformsInfo(bool includeSpecialTransforms)
{
string[] transforms = this.GetTransforms(includeSpecialTransforms);
ArrayList transformInfoArray = new ArrayList(transforms.Length);
foreach(string transform in transforms)
{
transformInfoArray.Add(this.GetTransformInfo(transform));
}
return (TransformInfo[]) transformInfoArray.ToArray(typeof(TransformInfo));
}
/// <summary>
/// Gets information about a transforms included in the patch package.
/// </summary>
/// <param name="transform">Name of the transform to extract; this may optionally be a
/// special transform prefixed by "#"</param>
/// <returns>Information about the transform</returns>
public TransformInfo GetTransformInfo(string transform)
{
string tempTransformFile = null;
try
{
tempTransformFile = Path.GetTempFileName();
this.ExtractTransform(transform, tempTransformFile);
using(SummaryInfo transformSummInfo = new SummaryInfo(tempTransformFile, false))
{
return new TransformInfo(transform, transformSummInfo);
}
}
finally
{
if(tempTransformFile != null && File.Exists(tempTransformFile))
{
File.Delete(tempTransformFile);
}
}
}
/// <summary>
/// Analyzes the transforms included in the patch package to find the ones that
/// are applicable to an install package.
/// </summary>
/// <param name="installPackage">The install package to validate the transforms against</param>
/// <returns>Array of valid transform names</returns>
/// <remarks>
/// The returned list does not include the "patch special transforms" that
/// are prefixed with "#" If a transform is valid, then its corresponding
/// special transform is assumed to be valid as well.
/// </remarks>
public string[] GetValidTransforms(InstallPackage installPackage)
{
ArrayList transformArray = new ArrayList();
string transformList = this.SummaryInfo.LastSavedBy;
foreach(string transform in transformList.Split(';', ':'))
{
if(transform.Length != 0 && !transform.StartsWith("#", StringComparison.Ordinal))
{
this.LogMessage("Checking validity of transform {0}", transform);
string tempTransformFile = null;
try
{
tempTransformFile = Path.GetTempFileName();
this.ExtractTransform(transform, tempTransformFile);
if(installPackage.IsTransformValid(tempTransformFile))
{
this.LogMessage("Found valid transform: {0}", transform);
transformArray.Add(transform);
}
}
finally
{
if(tempTransformFile != null && File.Exists(tempTransformFile))
{
try { File.Delete(tempTransformFile); }
catch(IOException) { }
}
}
}
}
return (string[]) transformArray.ToArray(typeof(string));
}
/// <summary>
/// Extracts a transform (.MST) from a patch package.
/// </summary>
/// <param name="transform">Name of the transform to extract; this may optionally be a
/// special transform prefixed by "#"</param>
/// <param name="extractFile">Location where the transform will be extracted</param>
public void ExtractTransform(string transform, string extractFile)
{
using(View stgView = this.OpenView("SELECT `Name`, `Data` FROM `_Storages` WHERE `Name` = '{0}'", transform))
{
stgView.Execute();
Record stgRec = stgView.Fetch();
if(stgRec == null)
{
this.LogMessage("Transform not found: {0}", transform);
throw new InstallerException("Transform not found: " + transform);
}
using(stgRec)
{
stgRec.GetStream("Data", extractFile);
}
}
}
}
}
|