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
|
// 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.Native
{
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Threading;
using WixToolset.Core.Native.Msi;
using WixToolset.Data;
/// <summary>
/// Windows installer validation implementation.
/// </summary>
public class WindowsInstallerValidator
{
private const string CubesFolder = "cubes";
/// <summary>
/// Creates a new Windows Installer validator.
/// </summary>
/// <param name="callback">Callback interface to handle messages.</param>
/// <param name="databasePath">Database to validate.</param>
/// <param name="cubeFiles">Set of CUBe files to merge.</param>
/// <param name="ices">ICEs to execute.</param>
/// <param name="suppressedIces">Suppressed ICEs.</param>
public WindowsInstallerValidator(IWindowsInstallerValidatorCallback callback, string databasePath, IEnumerable<string> cubeFiles, IEnumerable<string> ices, IEnumerable<string> suppressedIces)
{
this.Callback = callback;
this.DatabasePath = databasePath;
this.CubeFiles = cubeFiles;
this.Ices = new SortedSet<string>(ices);
this.SuppressedIces = new SortedSet<string>(suppressedIces);
}
private IWindowsInstallerValidatorCallback Callback { get; }
private string DatabasePath { get; }
private IEnumerable<string> CubeFiles { get; }
private SortedSet<string> Ices { get; }
private SortedSet<string> SuppressedIces { get; }
private bool ValidationSessionInProgress { get; set; }
private string CurrentIce { get; set; }
/// <summary>
/// Execute the validations.
/// </summary>
public void Execute()
{
using (var mutex = new Mutex(false, "WixValidator"))
{
try
{
if (!mutex.WaitOne(0))
{
this.Callback.ValidationBlocked();
mutex.WaitOne();
}
}
catch (AbandonedMutexException)
{
// Another validation process was probably killed, we own the mutex now.
}
try
{
this.RunValidations();
}
finally
{
mutex.ReleaseMutex();
}
}
}
private void RunValidations()
{
var previousUILevel = (int)InstallUILevels.Basic;
var previousHwnd = IntPtr.Zero;
InstallUIHandler previousUIHandler = null;
try
{
using (var database = new Database(this.DatabasePath, OpenDatabase.Direct))
{
var propertyTableExists = database.TableExists("Property");
string productCode = null;
// Remove the product code from the database before opening a session to prevent opening an installed product.
if (propertyTableExists)
{
using (var view = database.OpenExecuteView("SELECT `Value` FROM `Property` WHERE Property = 'ProductCode'"))
{
using (var record = view.Fetch())
{
if (null != record)
{
productCode = record.GetString(1);
using (var dropProductCodeView = database.OpenExecuteView("DELETE FROM `Property` WHERE `Property` = 'ProductCode'"))
{
}
}
}
}
}
// Merge in the cube databases.
foreach (var cubeFile in this.CubeFiles)
{
var findCubeFile = typeof(WindowsInstallerValidator).Assembly.FindFileRelativeToAssembly(Path.Combine(CubesFolder, cubeFile), searchNativeDllDirectories: false);
if (!findCubeFile.Found)
{
throw new WixException(ErrorMessages.CubeFileNotFound(findCubeFile.Path));
}
try
{
using (var cubeDatabase = new Database(findCubeFile.Path, OpenDatabase.ReadOnly))
{
try
{
database.Merge(cubeDatabase, "MergeConflicts");
}
catch
{
// ignore merge errors since they are expected in the _Validation table
}
}
}
catch (Win32Exception e)
{
if (0x6E == e.NativeErrorCode) // ERROR_OPEN_FAILED
{
throw new WixException(ErrorMessages.CubeFileNotFound(findCubeFile.Path));
}
throw;
}
}
// Commit the database before proceeding to ensure the streams don't get confused.
database.Commit();
// The property table may have been added to the database from a cub database without the proper validation rows.
if (!propertyTableExists)
{
using (var view = database.OpenExecuteView("DROP table `Property`"))
{
}
}
// Get all the action names for ICEs which have not been suppressed.
var actions = new List<string>();
using (var view = database.OpenExecuteView("SELECT `Action` FROM `_ICESequence` ORDER BY `Sequence`"))
{
foreach (var record in view.Records)
{
var action = record.GetString(1);
if (!this.SuppressedIces.Contains(action) && this.Ices.Contains(action))
{
actions.Add(action);
}
}
}
// Disable the internal UI handler and set an external UI handler.
previousUILevel = Installer.SetInternalUI((int)InstallUILevels.None, ref previousHwnd);
previousUIHandler = Installer.SetExternalUI(this.ValidationUIHandler, (int)InstallLogModes.Error | (int)InstallLogModes.Warning | (int)InstallLogModes.User, IntPtr.Zero);
// Create a session for running the ICEs.
this.ValidationSessionInProgress = true;
using (var session = new Session(database))
{
// Add the product code back into the database.
if (null != productCode)
{
// Some CUBs erroneously have a ProductCode property, so delete it if we just picked one up.
using (var dropProductCodeView = database.OpenExecuteView("DELETE FROM `Property` WHERE `Property` = 'ProductCode'"))
{
}
using (var view = database.OpenExecuteView($"INSERT INTO `Property` (`Property`, `Value`) VALUES ('ProductCode', '{productCode}')"))
{
}
}
foreach (var action in actions)
{
this.CurrentIce = action;
try
{
session.DoAction(action);
}
catch (Win32Exception e)
{
if (!this.Callback.EncounteredError)
{
throw e;
}
}
this.CurrentIce = null;
}
// Mark the validation session complete so we ignore any messages that MSI may fire
// during session clean-up.
this.ValidationSessionInProgress = false;
}
}
}
catch (Win32Exception e)
{
// Avoid displaying errors twice since one may have already occurred in the UI handler.
if (!this.Callback.EncounteredError)
{
if (0x6E == e.NativeErrorCode) // ERROR_OPEN_FAILED
{
// The database path is not passed to this exception since inside wix.exe
// this would be the temporary copy and there would be no final output becasue
// this error occured; and during standalone validation they should know the path
// passed in.
throw new WixException(ErrorMessages.ValidationFailedToOpenDatabase());
}
else if (0x64D == e.NativeErrorCode)
{
throw new WixException(ErrorMessages.ValidationFailedDueToLowMsiEngine());
}
else if (0x654 == e.NativeErrorCode)
{
throw new WixException(ErrorMessages.ValidationFailedDueToInvalidPackage());
}
else if (0x658 == e.NativeErrorCode)
{
throw new WixException(ErrorMessages.ValidationFailedDueToMultilanguageMergeModule());
}
else if (0x659 == e.NativeErrorCode)
{
throw new WixException(WarningMessages.ValidationFailedDueToSystemPolicy());
}
else
{
var msg = String.IsNullOrEmpty(this.CurrentIce) ? e.Message : $"Action - '{this.CurrentIce}' {e.Message}";
throw new WixException(ErrorMessages.Win32Exception(e.NativeErrorCode, msg));
}
}
}
finally
{
this.ValidationSessionInProgress = false;
Installer.SetExternalUI(previousUIHandler, 0, IntPtr.Zero);
Installer.SetInternalUI(previousUILevel, ref previousHwnd);
}
}
/// <summary>
/// The validation external UI handler.
/// </summary>
/// <param name="context">Pointer to an application context.
/// This parameter can be used for error checking.</param>
/// <param name="messageType">Specifies a combination of one message box style,
/// one message box icon type, one default button, and one installation message type.</param>
/// <param name="message">Specifies the message text.</param>
/// <returns>-1 for an error, 0 if no action was taken, 1 if OK, 3 to abort.</returns>
private int ValidationUIHandler(IntPtr context, uint messageType, string message)
{
var continueValidation = true;
// If we're getting messges during the validation session, log them.
// Otherwise, ignore the messages.
if (!this.ValidationSessionInProgress)
{
var parsedMessage = ParseValidationMessage(message, this.CurrentIce);
continueValidation = this.Callback.ValidationMessage(parsedMessage);
}
return continueValidation ? 1 : 3;
}
/// <summary>
/// Parses a message from the Validator.
/// </summary>
/// <param name="message">A <see cref="String"/> of tab-delmited tokens
/// in the validation message.</param>
/// <param name="currentIce">The name of the action to which the message
/// belongs.</param>
/// <exception cref="ArgumentNullException">The message cannot be null.
/// </exception>
/// <exception cref="WixException">The message does not contain four (4)
/// or more tab-delimited tokens.</exception>
/// <remarks>
/// <para><paramref name="message"/> a tab-delimited set of tokens,
/// formatted according to Windows Installer guidelines for ICE
/// message. The following table lists what each token by index
/// should mean.</para>
/// <para><paramref name="currentIce"/> a name that represents the ICE
/// action that was executed (e.g. 'ICE08').</para>
/// <list type="table">
/// <listheader>
/// <term>Index</term>
/// <description>Description</description>
/// </listheader>
/// <item>
/// <term>0</term>
/// <description>Name of the ICE.</description>
/// </item>
/// <item>
/// <term>1</term>
/// <description>Message type. See the following list.</description>
/// </item>
/// <item>
/// <term>2</term>
/// <description>Detailed description.</description>
/// </item>
/// <item>
/// <term>3</term>
/// <description>Help URL or location.</description>
/// </item>
/// <item>
/// <term>4</term>
/// <description>Table name.</description>
/// </item>
/// <item>
/// <term>5</term>
/// <description>Column name.</description>
/// </item>
/// <item>
/// <term>6</term>
/// <description>This and remaining fields are primary keys
/// to identify a row.</description>
/// </item>
/// </list>
/// <para>The message types are one of the following value.</para>
/// <list type="table">
/// <listheader>
/// <term>Value</term>
/// <description>Message Type</description>
/// </listheader>
/// <item>
/// <term>0</term>
/// <description>Failure message reporting the failure of the
/// ICE custom action.</description>
/// </item>
/// <item>
/// <term>1</term>
/// <description>Error message reporting database authoring that
/// case incorrect behavior.</description>
/// </item>
/// <item>
/// <term>2</term>
/// <description>Warning message reporting database authoring that
/// causes incorrect behavior in certain cases. Warnings can also
/// report unexpected side-effects of database authoring.
/// </description>
/// </item>
/// <item>
/// <term>3</term>
/// <description>Informational message.</description>
/// </item>
/// </list>
/// </remarks>
private static ValidationMessage ParseValidationMessage(string message, string currentIce)
{
if (message == null)
{
throw new ArgumentNullException(nameof(message));
}
var messageParts = message.Split('\t');
if (messageParts.Length < 3)
{
if (null == currentIce)
{
throw new WixException(ErrorMessages.UnexpectedExternalUIMessage(message));
}
else
{
throw new WixException(ErrorMessages.UnexpectedExternalUIMessage(message, currentIce));
}
}
var type = ParseValidationMessageType(messageParts[1]);
return new ValidationMessage
{
IceName = messageParts[0],
Type = type,
Description = messageParts[2],
HelpUrl = messageParts.Length > 3 ? messageParts[3] : null,
Table = messageParts.Length > 4 ? messageParts[4] : null,
Column = messageParts.Length > 5 ? messageParts[4] : null,
PrimaryKeys = messageParts.Length > 6 ? messageParts.Skip(6).ToArray() : null
};
}
private static ValidationMessageType ParseValidationMessageType(string type)
{
switch (type)
{
case "0":
return ValidationMessageType.InternalFailure;
case "1":
return ValidationMessageType.Error;
case "2":
return ValidationMessageType.Warning;
case "3":
return ValidationMessageType.Info;
default:
throw new WixException(ErrorMessages.InvalidValidatorMessageType(type));
}
}
}
}
|