aboutsummaryrefslogtreecommitdiff
path: root/src/WixToolset.Core.Native/Cabinet.cs
blob: 27b0ec749d130de32e1846eefcb50ad4fe60dc45 (plain)
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
// 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.Linq;

    /// <summary>
    /// Wrapper class around interop with wixcab.dll to compress files into a cabinet.
    /// </summary>
    public sealed class Cabinet
    {
        private const string CompressionLevelVariable = "WIX_COMPRESSION_LEVEL";
        private static readonly char[] TextLineSplitter = new[] { '\t' };

        public Cabinet(string path)
        {
            this.Path = path;
        }

        public string Path { get; }

        /// <summary>
        /// Creates a cabinet.
        /// </summary>
        /// <param name="cabPath">Path of cabinet to create.</param>
        /// <param name="compressionLevel">Level of compression to apply.</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>
        public void Compress(IEnumerable<CabinetCompressFile> files, CabinetCompressionLevel compressionLevel, int maxSize = 0, int maxThresh = 0)
        {
            var compressionLevelVariable = Environment.GetEnvironmentVariable(CompressionLevelVariable);

            // Override authored compression level if environment variable is present.
            if (!String.IsNullOrEmpty(compressionLevelVariable))
            {
                if (!Enum.TryParse(compressionLevelVariable, true, out compressionLevel))
                {
                    //throw new WixException(WixErrors.IllegalEnvironmentVariable(CompressionLevelVariable, compressionLevelVariable));
                    throw new ArgumentException();
                }
            }

            var wixnative = new WixNativeExe("smartcab", this.Path, Convert.ToInt32(compressionLevel), files.Count(), maxSize, maxThresh);

            foreach (var file in files)
            {
                wixnative.AddStdinLine(file.ToWixNativeStdinLine());
            }

            wixnative.Run();

#if TOOD_ERROR_HANDLING
            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;
            }
#endif
        }

        /// <summary>
        /// Enumerates all files in a cabinet.
        /// </summary>
        /// <returns>>List of CabinetFileInfo</returns>
        public List<CabinetFileInfo> Enumerate()
        {
            var wixnative = new WixNativeExe("enumcab", this.Path);
            var lines = wixnative.Run();

            var fileInfoList = new List<CabinetFileInfo>();

            foreach (var line in lines)
            {
                if (String.IsNullOrEmpty(line))
                {
                    continue;
                }

                var data = line.Split(TextLineSplitter, StringSplitOptions.None);

                var size = Convert.ToInt32(data[1]);
                var date = Convert.ToInt32(data[2]);
                var time = Convert.ToInt32(data[3]);

                fileInfoList.Add(new CabinetFileInfo(data[0], size, date, time));
            }

            return fileInfoList;
        }

        /// <summary>
        /// Extracts all the files from a cabinet to a directory.
        /// </summary>
        /// <param name="outputFolder">Directory to extract files to.</param>
        public void Extract(string outputFolder)
        {
            if (!outputFolder.EndsWith("\\", StringComparison.Ordinal))
            {
                outputFolder += "\\";
            }

            var wixnative = new WixNativeExe("extractcab", this.Path, outputFolder);
            wixnative.Run();
        }

#if TOOD_ERROR_HANDLING
        /// <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.
        /// </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;
                }
            }
        }
#endif
    }
}