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
|
// 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.Cab
{
using System;
using System.Globalization;
using System.IO;
using System.Runtime.InteropServices;
using WixToolset.Bind.Databases;
using WixToolset.Core.Native;
using WixToolset.Data;
/// <summary>
/// Wrapper class around interop with wixcab.dll to compress files into a cabinet.
/// </summary>
public sealed class WixCreateCab : IDisposable
{
private static readonly string CompressionLevelVariable = "WIX_COMPRESSION_LEVEL";
private IntPtr handle = IntPtr.Zero;
private bool disposed;
private int maxSize;
/// <summary>
/// Creates a cabinet.
/// </summary>
/// <param name="cabName">Name of cabinet to create.</param>
/// <param name="cabDir">Directory to create cabinet in.</param>
/// <param name="maxFiles">Maximum number of files that will be added to cabinet.</param>
/// <param name="maxSize">Maximum size of cabinet.</param>
/// <param name="maxThresh">Maximum threshold for each cabinet.</param>
/// <param name="compressionLevel">Level of compression to apply.</param>
public WixCreateCab(string cabName, string cabDir, int maxFiles, int maxSize, int maxThresh, CompressionLevel compressionLevel)
{
string compressionLevelVariable = Environment.GetEnvironmentVariable(CompressionLevelVariable);
this.maxSize = maxSize;
try
{
// Override authored compression level if environment variable is present.
if (!String.IsNullOrEmpty(compressionLevelVariable))
{
compressionLevel = WixCreateCab.CompressionLevelFromString(compressionLevelVariable);
}
}
catch (WixException)
{
throw new WixException(WixErrors.IllegalEnvironmentVariable(CompressionLevelVariable, compressionLevelVariable));
}
if (String.IsNullOrEmpty(cabDir))
{
cabDir = Directory.GetCurrentDirectory();
}
try
{
NativeMethods.CreateCabBegin(cabName, cabDir, (uint)maxFiles, (uint)maxSize, (uint)maxThresh, (uint)compressionLevel, out this.handle);
}
catch (COMException ce)
{
// If we get a "the file exists" error, we must have a full temp directory - so report the issue
if (0x80070050 == unchecked((uint)ce.ErrorCode))
{
throw new WixException(WixErrors.FullTempDirectory("WSC", Path.GetTempPath()));
}
throw;
}
}
/// <summary>
/// Destructor for cabinet creation.
/// </summary>
~WixCreateCab()
{
this.Dispose();
}
/// <summary>
/// Converts a compression level from its string to its enum value.
/// </summary>
/// <param name="compressionLevel">Compression level as a string.</param>
/// <returns>CompressionLevel enum value</returns>
public static CompressionLevel CompressionLevelFromString(string compressionLevel)
{
switch (compressionLevel.ToLower(CultureInfo.InvariantCulture))
{
case "low":
return CompressionLevel.Low;
case "medium":
return CompressionLevel.Medium;
case "high":
return CompressionLevel.High;
case "none":
return CompressionLevel.None;
case "mszip":
return CompressionLevel.Mszip;
default:
throw new WixException(WixErrors.IllegalCompressionLevel(compressionLevel));
}
}
/// <summary>
/// Adds a file to the cabinet.
/// </summary>
/// <param name="fileFacade">The file facade of the file to add.</param>
public void AddFile(FileFacade fileFacade)
{
MsiInterop.MSIFILEHASHINFO hashInterop = new MsiInterop.MSIFILEHASHINFO();
if (null != fileFacade.Hash)
{
hashInterop.FileHashInfoSize = 20;
hashInterop.Data0 = (int)fileFacade.Hash[2];
hashInterop.Data1 = (int)fileFacade.Hash[3];
hashInterop.Data2 = (int)fileFacade.Hash[4];
hashInterop.Data3 = (int)fileFacade.Hash[5];
this.AddFile(fileFacade.WixFile.Source, fileFacade.File.File, hashInterop);
}
else
{
this.AddFile(fileFacade.WixFile.Source, fileFacade.File.File);
}
}
/// <summary>
/// Adds a file to the cabinet.
/// </summary>
/// <param name="file">The file to add.</param>
/// <param name="token">The token for the file.</param>
public void AddFile(string file, string token)
{
this.AddFile(file, token, null);
}
/// <summary>
/// Adds a file to the cabinet with an optional MSI file hash.
/// </summary>
/// <param name="file">The file to add.</param>
/// <param name="token">The token for the file.</param>
/// <param name="fileHash">The MSI file hash of the file.</param>
private void AddFile(string file, string token, MsiInterop.MSIFILEHASHINFO fileHash)
{
try
{
NativeMethods.CreateCabAddFile(file, token, fileHash, this.handle);
}
catch (COMException ce)
{
if (0x80004005 == unchecked((uint)ce.ErrorCode)) // E_FAIL
{
throw new WixException(WixErrors.CreateCabAddFileFailed());
}
else if (0x80070070 == unchecked((uint)ce.ErrorCode)) // ERROR_DISK_FULL
{
throw new WixException(WixErrors.CreateCabInsufficientDiskSpace());
}
else
{
throw;
}
}
catch (DirectoryNotFoundException)
{
throw new WixFileNotFoundException(file);
}
catch (FileNotFoundException)
{
throw new WixFileNotFoundException(file);
}
}
/// <summary>
/// Complete/commit the cabinet - this must be called before Dispose so that errors will be
/// reported on the same thread.
/// This Complete should be used with no Cabinet splitting as it has the split cabinet names callback address as Zero
/// </summary>
public void Complete()
{
this.Complete(IntPtr.Zero);
}
/// <summary>
/// Complete/commit the cabinet - this must be called before Dispose so that errors will be
/// reported on the same thread.
/// </summary>
/// <param name="newCabNamesCallBackAddress">Address of Binder's callback function for Cabinet Splitting</param>
public void Complete(IntPtr newCabNamesCallBackAddress)
{
if (IntPtr.Zero != this.handle)
{
try
{
if (newCabNamesCallBackAddress != IntPtr.Zero && this.maxSize != 0)
{
NativeMethods.CreateCabFinish(this.handle, newCabNamesCallBackAddress);
}
else
{
NativeMethods.CreateCabFinish(this.handle, IntPtr.Zero);
}
GC.SuppressFinalize(this);
this.disposed = true;
}
catch (COMException ce)
{
if (0x80004005 == unchecked((uint)ce.ErrorCode)) // E_FAIL
{
// This error seems to happen, among other situations, when cabbing more than 0xFFFF files
throw new WixException(WixErrors.FinishCabFailed());
}
else if (0x80070070 == unchecked((uint)ce.ErrorCode)) // ERROR_DISK_FULL
{
throw new WixException(WixErrors.CreateCabInsufficientDiskSpace());
}
else
{
throw;
}
}
finally
{
this.handle = IntPtr.Zero;
}
}
}
/// <summary>
/// Cancels ("rolls back") the creation of the cabinet.
/// Don't throw WiX errors from here, because we're in a different thread, and they won't be reported correctly.
/// </summary>
public void Dispose()
{
if (!this.disposed)
{
if (IntPtr.Zero != this.handle)
{
NativeMethods.CreateCabCancel(this.handle);
this.handle = IntPtr.Zero;
}
GC.SuppressFinalize(this);
this.disposed = true;
}
}
}
}
|