aboutsummaryrefslogtreecommitdiff
path: root/src/dtf/WixToolset.Dtf.WindowsInstaller.Package/InstallPackage.cs
blob: 276732b7b08b2bbaaf4c21bb41d826a12dadfc42 (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
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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
// 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.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Text.RegularExpressions;
using WixToolset.Dtf.Compression;
using WixToolset.Dtf.Compression.Cab;

/// <summary>
/// Handles status messages generated when operations are performed on an
/// <see cref="InstallPackage"/> or <see cref="PatchPackage"/>.
/// </summary>
/// <example>
/// <c>installPackage.Message += new InstallPackageMessageHandler(Console.WriteLine);</c>
/// </example>
public delegate void InstallPackageMessageHandler(string format, params object[] args);

/// <summary>
/// Provides access to powerful build, maintenance, and analysis operations on an
/// installation package (.MSI or .MSM).
/// </summary>
public class InstallPackage : Database
{
    private string cabName;
    private string cabMsg;

    /// <summary>
    /// Creates a new InstallPackage object.  The file source directory and working
    /// directory are the same as the location as the package file.
    /// </summary>
    /// <param name="packagePath">Path to the install package to be created or opened</param>
    /// <param name="openMode">Open mode for the database</param>
    public InstallPackage(string packagePath, DatabaseOpenMode openMode)
        : this(packagePath, openMode, null, null)
    {
    }
    /// <summary>
    /// Creates a new InstallPackage object, specifying an alternate file source
    /// directory and/or working directory.
    /// </summary>
    /// <param name="packagePath">Path to the install package to be created or opened</param>
    /// <param name="openMode">Open mode for the database</param>
    /// <param name="sourceDir">Location to obtain source files and cabinets when extracting
    /// or updating files in the working directory. This is often the location of an original
    /// copy of the package that is not meant to be modified. If this parameter is null, it
    /// defaults to the directory of <paramref name="packagePath"/>.</param>
    /// <param name="workingDir">Location where files will be extracted to/updated from. Also
    /// the location where a temporary folder is created during some operations. If this
    /// parameter is null, it defaults to the directory of <paramref name="packagePath"/>.</param>
    /// <remarks>If the source location is different than the working directory, then
    /// no files will be modified at the source location.
    /// </remarks>
    public InstallPackage(string packagePath, DatabaseOpenMode openMode,
        string sourceDir, string workingDir) : base(packagePath, openMode)
    {
        this.sourceDir  = (sourceDir  != null ? sourceDir  : Path.GetDirectoryName(packagePath));
        this.workingDir = (workingDir != null ? workingDir : Path.GetDirectoryName(packagePath));
        this.compressionLevel = CompressionLevel.Normal;

        this.DeleteOnClose(this.TempDirectory);
    }

    /// <summary>
    /// Handle this event to receive status messages when operations are performed
    /// on the install package.
    /// </summary>
    /// <example>
    /// <c>installPackage.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 or sets the location to obtain source files and cabinets when
    /// extracting or updating files in the working directory. This is often
    /// the location of an original copy of the package that is not meant
    /// to be modified.
    /// </summary>
    public string SourceDirectory
    {
        get { return this.sourceDir; }
        set { this.sourceDir = value; }
    }
    private string sourceDir;

    /// <summary>
    /// Gets or sets the location where files will be extracted to/updated from. Also
    /// the location where a temporary folder is created during some operations.
    /// </summary>
    public string WorkingDirectory
    {
        get { return this.workingDir; }
        set { this.workingDir = value; }
    }
    private string workingDir;

    private const string TEMP_DIR_NAME = "WITEMP";

    private string TempDirectory
    {
        get { return Path.Combine(this.WorkingDirectory, TEMP_DIR_NAME); }
    }

    /// <summary>
    /// Gets the list of file keys that have the specified long file name.
    /// </summary>
    /// <param name="longFileName">File name to search for (case-insensitive)</param>
    /// <returns>Array of file keys, or a 0-length array if none are found</returns>
    [SuppressMessage("Microsoft.Globalization", "CA1308:NormalizeStringsToUppercase")]
    public string[] FindFiles(string longFileName)
    {
        longFileName = longFileName.ToLowerInvariant();
        ArrayList fileList = new ArrayList();
        foreach(KeyValuePair<string, InstallPath> entry in this.Files)
        {
            if(((InstallPath) entry.Value).TargetName.ToLowerInvariant()
               == longFileName)
            {
                fileList.Add(entry.Key);
            }
        }
        return (string[]) fileList.ToArray(typeof(string));
    }

    /// <summary>
    /// Gets the list of file keys whose long file names match a specified
    /// regular-expression search pattern.
    /// </summary>
    /// <param name="pattern">Regular expression search pattern</param>
    /// <returns>Array of file keys, or a 0-length array if none are found</returns>
    public string[] FindFiles(Regex pattern)
    {
        ArrayList fileList = new ArrayList();
        foreach (KeyValuePair<string, InstallPath> entry in this.Files)
        {
            if(pattern.IsMatch(((InstallPath) entry.Value).TargetName))
            {
                fileList.Add(entry.Key);
            }
        }
        return (string[]) fileList.ToArray(typeof(string));
    }

    /// <summary>
    /// Extracts all files to the <see cref="WorkingDirectory"/>. The files are extracted
    /// to the relative directory matching their <see cref="InstallPath.SourcePath"/>.
    /// </summary>
    /// <remarks>If any files have the uncompressed attribute, they will be copied
    /// from the <see cref="SourceDirectory"/>.</remarks>
    public void ExtractFiles()
    {
        this.ExtractFiles(null);
    }
    /// <summary>
    /// Extracts a specified list of files to the <see cref="WorkingDirectory"/>. The files
    /// are extracted to the relative directory matching their <see cref="InstallPath.SourcePath"/>.
    /// </summary>
    /// <param name="fileKeys">List of file key strings to extract</param>
    /// <remarks>If any files have the uncompressed attribute, they will be copied
    /// from the <see cref="SourceDirectory"/>.</remarks>
    public void ExtractFiles(ICollection<string> fileKeys)
    {
        this.ProcessFilesByMediaDisk(fileKeys,
            new ProcessFilesOnOneMediaDiskHandler(this.ExtractFilesOnOneMediaDisk));
    }

    private bool IsMergeModule()
    {
        return this.CountRows("Media", "`LastSequence` >= 0") == 0 &&
            this.CountRows("_Streams", "`Name` = 'MergeModule.CABinet'") != 0;
    }

    private delegate void ProcessFilesOnOneMediaDiskHandler(string mediaCab,
        InstallPathMap compressedFileMap, InstallPathMap uncompressedFileMap);

    private void ProcessFilesByMediaDisk(ICollection<string> fileKeys,
        ProcessFilesOnOneMediaDiskHandler diskHandler)
    {
        if(this.IsMergeModule())
        {
            InstallPathMap files = new InstallPathMap();
            foreach(string fileKey in this.Files.Keys)
            {
                if(fileKeys == null || fileKeys.Contains(fileKey))
                {
                    files[fileKey] = this.Files[fileKey];
                }
            }
            diskHandler("#MergeModule.CABinet", files, new InstallPathMap());
        }
        else
        {
            bool defaultCompressed = ((this.SummaryInfo.WordCount & 0x2) != 0);

            View fileView = null, mediaView = null;
            Record fileRec = null;
            try
            {
                fileView = this.OpenView("SELECT `File`, `Attributes`, `Sequence` " +
                    "FROM `File` ORDER BY `Sequence`");
                mediaView = this.OpenView("SELECT `DiskId`, `LastSequence`, `Cabinet` " +
                    "FROM `Media` ORDER BY `DiskId`");
                fileView.Execute();
                mediaView.Execute();

                int currentMediaDiskId = -1;
                int currentMediaMaxSequence = -1;
                string currentMediaCab = null;
                InstallPathMap compressedFileMap = new InstallPathMap();
                InstallPathMap uncompressedFileMap = new InstallPathMap();

                while((fileRec = fileView.Fetch()) != null)
                {
                    string fileKey = (string) fileRec[1];

                    if(fileKeys == null || fileKeys.Contains(fileKey))
                    {
                        int fileAttributes = fileRec.GetInteger(2);
                        int fileSequence = fileRec.GetInteger(3);

                        InstallPath fileInstallPath = this.Files[fileKey];
                        if(fileInstallPath == null)
                        {
                            this.LogMessage("Could not get install path for source file: {0}", fileKey);
                            throw new InstallerException("Could not get install path for source file: " + fileKey);
                        }

                        if(fileSequence > currentMediaMaxSequence)
                        {
                            if(currentMediaDiskId != -1)
                            {
                                diskHandler(currentMediaCab,
                                    compressedFileMap, uncompressedFileMap);
                                compressedFileMap.Clear();
                                uncompressedFileMap.Clear();
                            }

                            while(fileSequence > currentMediaMaxSequence)
                            {
                                Record mediaRec = mediaView.Fetch();
                                if(mediaRec == null)
                                {
                                    currentMediaDiskId = -1;
                                    break;
                                }
                                using(mediaRec)
                                {
                                    currentMediaDiskId = mediaRec.GetInteger(1);
                                    currentMediaMaxSequence = mediaRec.GetInteger(2);
                                    currentMediaCab = (string) mediaRec[3];
                                }
                            }
                            if(fileSequence > currentMediaMaxSequence) break;
                        }

                        if((fileAttributes & (int) WixToolset.Dtf.WindowsInstaller.FileAttributes.Compressed) != 0)
                        {
                            compressedFileMap[fileKey] = fileInstallPath;
                        }
                        else if ((fileAttributes & (int) WixToolset.Dtf.WindowsInstaller.FileAttributes.NonCompressed) != 0)
                        {
                            // Non-compressed files are located
                            // in the same directory as the MSI, without any path.
                            uncompressedFileMap[fileKey] = new InstallPath(fileInstallPath.SourceName);
                        }
                        else if(defaultCompressed)
                        {
                            compressedFileMap[fileKey] = fileInstallPath;
                        }
                        else
                        {
                            uncompressedFileMap[fileKey] = fileInstallPath;
                        }
                    }
                    fileRec.Close();
                    fileRec = null;
                }
                if(currentMediaDiskId != -1)
                {
                    diskHandler(currentMediaCab,
                        compressedFileMap, uncompressedFileMap);
                }
            }
            finally
            {
                if (fileRec != null) fileRec.Close();
                if (fileView != null) fileView.Close();
                if (mediaView != null) mediaView.Close();
            }
        }
    }

    [SuppressMessage("Microsoft.Globalization", "CA1308:NormalizeStringsToUppercase")]
    private void ExtractFilesOnOneMediaDisk(string mediaCab,
        InstallPathMap compressedFileMap, InstallPathMap uncompressedFileMap)
    {
        if(compressedFileMap.Count > 0)
        {
            string cabFile = null;
            if(mediaCab.StartsWith("#", StringComparison.Ordinal))
            {
                mediaCab = mediaCab.Substring(1);

                using(View streamView = this.OpenView("SELECT `Name`, `Data` FROM `_Streams` " +
                      "WHERE `Name` = '{0}'", mediaCab))
                {
                    streamView.Execute();
                    Record streamRec = streamView.Fetch();
                    if(streamRec == null)
                    {
                        this.LogMessage("Stream not found: {0}", mediaCab);
                        throw new InstallerException("Stream not found: " + mediaCab);
                    }
                    using(streamRec)
                    {
                        this.LogMessage("extract cab {0}", mediaCab);
                        Directory.CreateDirectory(this.TempDirectory);
                        cabFile = Path.Combine(this.TempDirectory,
                            Path.GetFileNameWithoutExtension(mediaCab) + ".cab");
                        streamRec.GetStream("Data", cabFile);
                    }
                }
            }
            else
            {
                cabFile = Path.Combine(this.SourceDirectory, mediaCab);
            }

            this.cabName = mediaCab;
            this.cabMsg = "extract {0}\\{1} {2}";
            new CabInfo(cabFile).UnpackFileSet(compressedFileMap.SourcePaths, this.WorkingDirectory,
                this.CabinetProgress);
            ClearReadOnlyAttribute(this.WorkingDirectory, compressedFileMap.Values);
        }
        foreach(InstallPath fileInstallPath in uncompressedFileMap.Values)
        {
            string sourcePath = Path.Combine(this.SourceDirectory, fileInstallPath.SourcePath);
            string extractPath = Path.Combine(this.WorkingDirectory, fileInstallPath.SourcePath);
            if(Path.GetFullPath(sourcePath).ToLowerInvariant() !=
                Path.GetFullPath(extractPath).ToLowerInvariant())
            {
                if(!File.Exists(sourcePath))
                {
                    this.LogMessage("Error: Uncompressed file not found: {0}", sourcePath);
                    throw new FileNotFoundException("Uncompressed file not found.", sourcePath);
                }
                else
                {
                    this.LogMessage("copy {0} {1}", sourcePath, extractPath);
                    Directory.CreateDirectory(Path.GetDirectoryName(extractPath));
                    File.Copy(sourcePath, extractPath, true);
                }
            }
            else
            {
                if(!File.Exists(extractPath))
                {
                    this.LogMessage("Error: Uncompressed file not found: {0}", extractPath);
                    throw new FileNotFoundException("Uncompressed file not found.", extractPath);
                }
            }
        }
    }

    private void CabinetProgress(object sender, ArchiveProgressEventArgs e)
    {
        switch(e.ProgressType)
        {
            case ArchiveProgressType.StartFile:
            {
                string filePath = e.CurrentFileName;
                if(this.filePathMap != null)
                {
                    InstallPath fileInstallPath = this.Files[e.CurrentFileName];
                    if(fileInstallPath != null)
                    {
                        filePath = fileInstallPath.SourcePath;
                    }
                }
                this.LogMessage(this.cabMsg, this.cabName, e.CurrentFileName,
                    Path.Combine(this.WorkingDirectory, filePath));
            }
            break;
        }
    }

    /// <summary>
    /// Updates the install package with new files from the <see cref="WorkingDirectory"/>.  The
    /// files must be in the relative directory matching their <see cref="InstallPath.SourcePath"/>.
    /// This method re-compresses and packages the files if necessary, and also updates the
    /// following data: File.FileSize, File.Version, File.Language, MsiFileHash.HashPart*
    /// </summary>
    /// <remarks>
    /// The cabinet compression level used during re-cabbing can be configured with the
    /// <see cref="CompressionLevel"/> property.
    /// </remarks>
    public void UpdateFiles()
    {
        this.UpdateFiles(null);
    }
    /// <summary>
    /// Updates the install package with new files from the <see cref="WorkingDirectory"/>.  The
    /// files must be in the relative directory matching their <see cref="InstallPath.SourcePath"/>.
    /// This method re-compresses and packages the files if necessary, and also updates the
    /// following data: File.FileSize, File.Version, File.Language, MsiFileHash.HashPart?.
    /// </summary>
    /// <param name="fileKeys">List of file key strings to update</param>
    /// <remarks>
    /// This method does not change the media structure of the package, so it may require extracting
    /// and re-compressing a large cabinet just to update one file.
    /// <p>The cabinet compression level used during re-cabbing can be configured with the
    /// <see cref="CompressionLevel"/> property.</p>
    /// </remarks>
    public void UpdateFiles(ICollection<string> fileKeys)
    {
        this.ProcessFilesByMediaDisk(fileKeys,
            new ProcessFilesOnOneMediaDiskHandler(this.UpdateFilesOnOneMediaDisk));
    }

    private void UpdateFilesOnOneMediaDisk(string mediaCab,
        InstallPathMap compressedFileMap, InstallPathMap uncompressedFileMap)
    {
        if(compressedFileMap.Count > 0)
        {
            string cabFile = null;
            bool cabFileIsTemp = false;
            if(mediaCab.StartsWith("#", StringComparison.Ordinal))
            {
                cabFileIsTemp = true;
                mediaCab = mediaCab.Substring(1);

                using(View streamView = this.OpenView("SELECT `Name`, `Data` FROM `_Streams` " +
                      "WHERE `Name` = '{0}'", mediaCab))
                {
                    streamView.Execute();
                    Record streamRec = streamView.Fetch();
                    if(streamRec == null)
                    {
                        this.LogMessage("Stream not found: {0}", mediaCab);
                        throw new InstallerException("Stream not found: " + mediaCab);
                    }
                    using(streamRec)
                    {
                        this.LogMessage("extract cab {0}", mediaCab);
                        Directory.CreateDirectory(this.TempDirectory);
                        cabFile = Path.Combine(this.TempDirectory,
                            Path.GetFileNameWithoutExtension(mediaCab) + ".cab");
                        streamRec.GetStream("Data", cabFile);
                    }
                }
            }
            else
            {
                cabFile = Path.Combine(this.SourceDirectory, mediaCab);
            }

            CabInfo cab = new CabInfo(cabFile);
            ArrayList fileKeyList = new ArrayList();
            foreach (CabFileInfo fileInCab in cab.GetFiles())
            {
                string fileKey = fileInCab.Name;
                if(this.Files[fileKey] != null)
                {
                    fileKeyList.Add(fileKey);
                }
            }
            string[] fileKeys = (string[]) fileKeyList.ToArray(typeof(string));

            Directory.CreateDirectory(this.TempDirectory);

            ArrayList remainingFileKeys = new ArrayList(fileKeys);
            foreach(string fileKey in fileKeys)
            {
                InstallPath fileInstallPath = compressedFileMap[fileKey];
                if(fileInstallPath != null)
                {
                    UpdateFileStats(fileKey, fileInstallPath);

                    string filePath = Path.Combine(this.WorkingDirectory, fileInstallPath.SourcePath);
                    this.LogMessage("copy {0} {1}", filePath, fileKey);
                    File.Copy(filePath, Path.Combine(this.TempDirectory, fileKey), true);
                    remainingFileKeys.Remove(fileKey);
                }
            }

            if(remainingFileKeys.Count > 0)
            {
                this.cabName = mediaCab;
                this.cabMsg = "extract {0}\\{1}";
                string[] remainingFileKeysArray = (string[]) remainingFileKeys.ToArray(typeof(string));
                cab.UnpackFiles(remainingFileKeysArray, this.TempDirectory, remainingFileKeysArray,
                    this.CabinetProgress);
            }

            ClearReadOnlyAttribute(this.TempDirectory, fileKeys);

            if(!cabFileIsTemp)
            {
                cab = new CabInfo(Path.Combine(this.WorkingDirectory, mediaCab));
            }
            this.cabName = mediaCab;
            this.cabMsg = "compress {0}\\{1}";
            cab.PackFiles(this.TempDirectory, fileKeys, fileKeys,
                this.CompressionLevel, this.CabinetProgress);

            if(cabFileIsTemp)
            {
              using (Record streamRec = new Record(1))
              {
                  streamRec.SetStream(1, cabFile);
                  this.Execute(String.Format(
                      "UPDATE `_Streams` SET `Data` = ? WHERE `Name` = '{0}'", mediaCab),
                      streamRec);
              }
            }
        }

        foreach (KeyValuePair<string, InstallPath> entry in uncompressedFileMap)
        {
            UpdateFileStats((string) entry.Key, (InstallPath) entry.Value);
        }
    }

    private void UpdateFileStats(string fileKey, InstallPath fileInstallPath)
    {
        string filePath = Path.Combine(this.WorkingDirectory, fileInstallPath.SourcePath);
        if(!File.Exists(filePath))
        {
            this.LogMessage("Updated source file not found: {0}", filePath);
            throw new FileNotFoundException("Updated source file not found: " + filePath);
        }

        this.LogMessage("updatestats {0}", fileKey);

        string version = Installer.GetFileVersion(filePath);
        string language = Installer.GetFileLanguage(filePath);
        long size = new FileInfo(filePath).Length;

        this.Execute("UPDATE `File` SET `Version` = '{0}', `Language` = '{1}', " +
            "`FileSize` = {2} WHERE `File` = '{3}'", version, language, size, fileKey);

        if ((version == null || version.Length == 0) && this.Tables.Contains("MsiFileHash"))
        {
            int[] hash = new int[4];
            Installer.GetFileHash(filePath, hash);
            this.Execute("DELETE FROM `MsiFileHash` WHERE `File_` = '{0}'", fileKey);
            this.Execute("INSERT INTO `MsiFileHash` (`File_`, `Options`, `HashPart1`, `HashPart2`, " +
                "`HashPart3`, `HashPart4`) VALUES ('" + fileKey + "', 0, {0}, {1}, {2}, {3})",
                hash[0], hash[1], hash[2], hash[3]);
        }
    }

    /// <summary>
    /// Consolidates a package by combining and re-compressing all files into a single
    /// internal or external cabinet.
    /// </summary>
    /// <param name="mediaCabinet"></param>
    /// <remarks>If an installation package was built from many merge modules, this
    /// method can somewhat decrease package size, complexity, and installation time.
    /// <p>This method will also convert a package with all or mostly uncompressed
    /// files into a package where all files are compressed.</p>
    /// <p>If the package contains any not-yet-applied binary file patches (for
    /// example, a package generated by a call to <see cref="ApplyPatch"/>) then
    /// this method will apply the patches before compressing the updated files.</p>
    /// <p>This method edits the database summary information and the File, Media
    /// and Patch tables as necessary to maintain a valid installation package.</p>
    /// <p>The cabinet compression level used during re-cabbing can be configured with the
    /// <see cref="CompressionLevel"/> property.</p>
    /// </remarks>
    public void Consolidate(string mediaCabinet)
    {
        this.LogMessage("Consolidating package");

        Directory.CreateDirectory(this.TempDirectory);

        this.LogMessage("Extracting/preparing files");
        this.ProcessFilesByMediaDisk(null,
            new ProcessFilesOnOneMediaDiskHandler(this.PrepareOneMediaDiskForConsolidation));

        this.LogMessage("Applying any file patches");
        ApplyFilePatchesForConsolidation();

        this.LogMessage("Clearing PatchPackage, Patch, MsiPatchHeaders tables");
        if (this.Tables.Contains("PatchPackage"))
        {
            this.Execute("DELETE FROM `PatchPackage` WHERE `PatchId` <> ''");
        }
        if (this.Tables.Contains("Patch"))
        {
            this.Execute("DELETE FROM `Patch` WHERE `File_` <> ''");
        }
        if (this.Tables.Contains("MsiPatchHeaders"))
        {
            this.Execute("DELETE FROM `MsiPatchHeaders` WHERE `StreamRef` <> ''");
        }

        this.LogMessage("Resequencing files");
        ArrayList files = new ArrayList();
        using(View fileView = this.OpenView("SELECT `File`, `Attributes`, `Sequence` " +
              "FROM `File` ORDER BY `Sequence`"))
        {
            fileView.Execute();
            
            foreach (Record fileRec in fileView) using(fileRec)
            {
                files.Add(fileRec[1]);
                int fileAttributes = fileRec.GetInteger(2);
                fileAttributes &= ~(int) (WixToolset.Dtf.WindowsInstaller.FileAttributes.Compressed
                    | WixToolset.Dtf.WindowsInstaller.FileAttributes.NonCompressed | WixToolset.Dtf.WindowsInstaller.FileAttributes.PatchAdded);
                fileRec[2] = fileAttributes;
                fileRec[3] = files.Count;
                fileView.Update(fileRec);
            }
        }

        bool internalCab = false;
        if(mediaCabinet.StartsWith("#", StringComparison.Ordinal))
        {
            internalCab = true;
            mediaCabinet = mediaCabinet.Substring(1);
        }

        this.LogMessage("Cabbing files");
        string[] fileKeys = (string[]) files.ToArray(typeof(string));
        string cabPath = Path.Combine(internalCab ? this.TempDirectory
            : this.WorkingDirectory, mediaCabinet);
        this.cabName = mediaCabinet;
        this.cabMsg = "compress {0}\\{1}";
        new CabInfo(cabPath).PackFiles(this.TempDirectory, fileKeys,
            fileKeys, this.CompressionLevel, this.CabinetProgress);

        this.DeleteEmbeddedCabs();

        if(internalCab)
        {
            this.LogMessage("Inserting cab stream into MSI");
            Record cabRec = new Record(1);
            cabRec.SetStream(1, cabPath);
            this.Execute("INSERT INTO `_Streams` (`Name`, `Data`) VALUES ('" + mediaCabinet + "', ?)", cabRec);
        }

        this.LogMessage("Inserting cab media record into MSI");
        this.Execute("DELETE FROM `Media` WHERE `DiskId` <> 0");
        this.Execute("INSERT INTO `Media` (`DiskId`, `LastSequence`, `Cabinet`) " +
            "VALUES (1, " + files.Count + ", '" + (internalCab ? "#" : "") + mediaCabinet + "')");


        this.LogMessage("Setting compressed flag on package summary info");
        this.SummaryInfo.WordCount = this.SummaryInfo.WordCount | 2;
        this.SummaryInfo.Persist();
    }

    private void DeleteEmbeddedCabs()
    {
        using (View view = this.OpenView("SELECT `Cabinet` FROM `Media` WHERE `Cabinet` <> ''"))
        {
            view.Execute();
            
            foreach (Record rec in view) using(rec)
            {
                string cab = rec.GetString(1);
                if(cab.StartsWith("#", StringComparison.Ordinal))
                {
                    cab = cab.Substring(1);
                    this.LogMessage("Deleting embedded cab stream: {0}", cab);
                    this.Execute("DELETE FROM `_Streams` WHERE `Name` = '{0}'", cab);
                }
            }
        }
    }

    private void PrepareOneMediaDiskForConsolidation(string mediaCab,
        InstallPathMap compressedFileMap, InstallPathMap uncompressedFileMap)
    {
        if(compressedFileMap.Count > 0)
        {
            string cabFile = null;
            if(mediaCab.StartsWith("#", StringComparison.Ordinal))
            {
                mediaCab = mediaCab.Substring(1);

                using (View streamView = this.OpenView("SELECT `Name`, `Data` FROM `_Streams` " +
                      "WHERE `Name` = '{0}'", mediaCab))
                {
                    streamView.Execute();
                    Record streamRec = streamView.Fetch();
                    if(streamRec == null)
                    {
                        this.LogMessage("Stream not found: {0}", mediaCab);
                        throw new InstallerException("Stream not found: " + mediaCab);
                    }
                    using(streamRec)
                    {
                        this.LogMessage("extract cab {0}", mediaCab);
                        cabFile = Path.Combine(this.TempDirectory,
                            Path.GetFileNameWithoutExtension(mediaCab) + ".cab");
                        streamRec.GetStream("Data", cabFile);
                    }
                }
            }
            else
            {
                cabFile = Path.Combine(this.SourceDirectory, mediaCab);
            }
            string[] fileKeys = new string[compressedFileMap.Keys.Count];
            compressedFileMap.Keys.CopyTo(fileKeys, 0);
            this.cabName = mediaCab;
            this.cabMsg = "extract {0}\\{1}";
            new CabInfo(cabFile).UnpackFiles(fileKeys, this.TempDirectory, fileKeys,
                this.CabinetProgress);
            ClearReadOnlyAttribute(this.TempDirectory, fileKeys);
        }
        foreach (KeyValuePair<string, InstallPath> entry in uncompressedFileMap)
        {
            string fileKey = (string) entry.Key;
            InstallPath fileInstallPath = (InstallPath) entry.Value;

            string filePath = Path.Combine(this.SourceDirectory, fileInstallPath.SourcePath);
            this.LogMessage("copy {0} {1}", filePath, fileKey);
            File.Copy(filePath, Path.Combine(this.TempDirectory, fileKey));
        }
    }

    private void ClearReadOnlyAttribute(string baseDirectory, IEnumerable filePaths)
    {
        foreach(object filePath in filePaths)
        {
            string fullFilePath = Path.Combine(baseDirectory, filePath.ToString());
            if (File.Exists(fullFilePath))
            {
                System.IO.FileAttributes fileAttributes = File.GetAttributes(fullFilePath);
                if ((fileAttributes & System.IO.FileAttributes.ReadOnly) != 0)
                {
                    fileAttributes &= ~System.IO.FileAttributes.ReadOnly;
                    File.SetAttributes(fullFilePath, fileAttributes);
                }
            }
        }
    }

    private void ApplyFilePatchesForConsolidation()
    {
        if(this.Tables.Contains("Patch"))
        {
            using(View patchView = this.OpenView("SELECT `File_`, `Sequence` " +
                  "FROM `Patch` ORDER BY `Sequence`"))
            {
                patchView.Execute();
                Hashtable extractedPatchCabs = new Hashtable();
                
                foreach (Record patchRec in patchView) using(patchRec)
                {
                    string fileKey = (string) patchRec[1];
                    int sequence = patchRec.GetInteger(2);
                    this.LogMessage("patch {0}", fileKey);

                    string tempPatchFile = Path.Combine(this.TempDirectory, fileKey + ".pat");
                    ExtractFilePatch(fileKey, sequence, tempPatchFile, extractedPatchCabs);
                    string filePath = Path.Combine(this.TempDirectory, fileKey);
                    string oldFilePath = filePath + ".old";
                    if(File.Exists(oldFilePath)) File.Delete(oldFilePath);
                    File.Move(filePath, oldFilePath);
                    Type.GetType("WixToolset.Dtf.WindowsInstaller.FilePatch")
                        .GetMethod("ApplyPatchToFile",
                        new Type[] { typeof(string), typeof(string), typeof(string) })
                        .Invoke(null, new object[] { tempPatchFile, oldFilePath, filePath });
                }
            }
        }
    }

    private void ExtractFilePatch(string fileKey, int sequence, string extractPath,
        IDictionary extractedCabs)
    {
        string mediaCab = null;
        using(View mediaView = this.OpenView("SELECT `DiskId`, `LastSequence`, `Cabinet` " +
              "FROM `Media` ORDER BY `DiskId`"))
        {
            mediaView.Execute();
            
            foreach (Record mediaRec in mediaView) using(mediaRec)
            {
                int mediaMaxSequence = mediaRec.GetInteger(2);
                if(mediaMaxSequence >= sequence)
                {
                    mediaCab = mediaRec.GetString(3);
                    break;
                }
            }
        }

        if(mediaCab == null || mediaCab.Length == 0)
        {
            this.LogMessage("Could not find cabinet for file patch: {0}", fileKey);
            throw new InstallerException("Could not find cabinet for file patch: " + fileKey);
        }

        if(!mediaCab.StartsWith("#", StringComparison.Ordinal))
        {
            this.LogMessage("Error: Patch cabinet {0} must be embedded", mediaCab);
            throw new InstallerException("Patch cabinet " + mediaCab + " must be embedded.");
        }
        mediaCab = mediaCab.Substring(1);

        string cabFile = (string) extractedCabs[mediaCab];
        if(cabFile == null)
        {
            using(View streamView = this.OpenView("SELECT `Name`, `Data` FROM `_Streams` " +
                  "WHERE `Name` = '{0}'", mediaCab))
            {
                streamView.Execute();
                Record streamRec = streamView.Fetch();
                if(streamRec == null)
                {
                    this.LogMessage("Stream not found: {0}", mediaCab);
                    throw new InstallerException("Stream not found: " + mediaCab);
                }
                using(streamRec)
                {
                    this.LogMessage("extract cab {0}", mediaCab);
                    Directory.CreateDirectory(this.TempDirectory);
                    cabFile = Path.Combine(this.TempDirectory,
                        Path.GetFileNameWithoutExtension(mediaCab) + ".cab");
                    streamRec.GetStream("Data", cabFile);
                }
            }
            extractedCabs[mediaCab] = cabFile;
        }

        this.LogMessage("extract patch {0}\\{1}", mediaCab, fileKey);
        new CabInfo(cabFile).UnpackFile(fileKey, extractPath);
    }

    /// <summary>
    /// Rebuilds the cached directory structure information accessed by the
    /// <see cref="Directories"/> and <see cref="Files"/> properties. This
    /// should be done after modifying the File, Component, or Directory
    /// tables, or else the cached information may no longer be accurate.
    /// </summary>
    public void UpdateDirectories()
    {
        this.dirPathMap = null;
        this.filePathMap = InstallPathMap.BuildFilePathMap(this,
            InstallPathMap.BuildComponentPathMap(this, this.Directories), false);
    }

    /// <summary>
    /// Gets a mapping from Directory keys to source/target paths.
    /// </summary>
    /// <remarks>
    /// If the Directory table is modified, this mapping
    /// will be outdated until you call <see cref="UpdateDirectories"/>.
    /// </remarks>
    public InstallPathMap Directories
    {
        get
        {
            if(this.dirPathMap == null)
            {
                this.dirPathMap = InstallPathMap.BuildDirectoryPathMap(this, false);
            }
            return this.dirPathMap;
        }
    }
    private InstallPathMap dirPathMap;

    /// <summary>
    /// Gets a mapping from File keys to source/target paths.
    /// </summary>
    /// <remarks>
    /// If the File, Component, or Directory tables are modified, this mapping
    /// may be outdated until you call <see cref="UpdateDirectories"/>.
    /// </remarks>
    public InstallPathMap Files
    {
        get
        {
            if(this.filePathMap == null)
            {
                this.filePathMap = InstallPathMap.BuildFilePathMap(this,
                    InstallPathMap.BuildComponentPathMap(this, this.Directories), false);
            }
            return this.filePathMap;
        }
    }
    private InstallPathMap filePathMap;

    /// <summary>
    /// Gets or sets the compression level used by <see cref="UpdateFiles()"/>
    /// and <see cref="Consolidate"/>.
    /// </summary>
    /// <remarks>
    /// If the Directory table is modified, this mapping will be outdated
    /// until you close and reopen the install package.
    /// </remarks>
    public CompressionLevel CompressionLevel
    {
        get { return this.compressionLevel; }
        set { this.compressionLevel = value; }
    }
    private CompressionLevel compressionLevel;

    /// <summary>
    /// Applies a patch package to the database, resulting in an installation package that
    /// has the patch built-in.
    /// </summary>
    /// <param name="patchPackage">The patch package to be applied</param>
    /// <param name="transform">Optional name of the specific transform to apply.
    /// This parameter is usually left null, which causes the patch to be searched for
    /// a transform that is valid to apply to this database.</param>
    /// <remarks>
    /// If the patch contains any binary file patches, they will not immediately be applied
    /// to the target files, though they will at installation time.
    /// <p>After calling this method you can use <see cref="Consolidate"/> to apply
    /// the file patches immediately and also discard any outdated files from the package.</p>
    /// </remarks>
    public void ApplyPatch(PatchPackage patchPackage, string transform)
    {
        if(patchPackage == null) throw new ArgumentNullException("patchPackage");

        this.LogMessage("Applying patch file {0} to database {1}",
            patchPackage.FilePath, this.FilePath);

        if(transform == null)
        {
            this.LogMessage("No transform specified; searching for valid patch transform");
            string[] validTransforms = patchPackage.GetValidTransforms(this);
            if(validTransforms.Length == 0)
            {
                this.LogMessage("No valid patch transform was found");
                throw new InvalidOperationException("No valid patch transform was found.");
            }
            transform = validTransforms[0];
        }
        this.LogMessage("Patch transform = {0}", transform);

        string patchPrefix = Path.GetFileNameWithoutExtension(patchPackage.FilePath) + "_";

        string specialTransform = "#" + transform;
        Directory.CreateDirectory(this.TempDirectory);
        this.LogMessage("Extracting substorage {0}", transform);
        string transformFile = Path.Combine(this.TempDirectory,
            patchPrefix + Path.GetFileNameWithoutExtension(transform) + ".mst");
        patchPackage.ExtractTransform(transform, transformFile);
        this.LogMessage("Extracting substorage {0}", specialTransform);
        string specialTransformFile = Path.Combine(this.TempDirectory,
            patchPrefix + Path.GetFileNameWithoutExtension(specialTransform) + ".mst");
        patchPackage.ExtractTransform(specialTransform, specialTransformFile);

        if (this.Tables.Contains("Patch") && !this.Tables["Patch"].Columns.Contains("_StreamRef"))
        {
            if(this.CountRows("Patch") > 0)
            {
                this.LogMessage("Warning: non-empty Patch table exists without StreamRef_ column; " +
                    "patch transform may fail");
            }
            else
            {
                this.Execute("DROP TABLE `Patch`");
                this.Execute("CREATE TABLE `Patch` (`File_` CHAR(72) NOT NULL, " +
                    "`Sequence` INTEGER NOT NULL, `PatchSize` LONG NOT NULL, " +
                    "`Attributes` INTEGER NOT NULL, `Header` OBJECT, `StreamRef_` CHAR(72)  " +
                    "PRIMARY KEY `File_`, `Sequence`)");
            }
        }

        this.LogMessage("Applying transform {0} to database", transform);
        this.ApplyTransform(transformFile);
        this.LogMessage("Applying transform {0} to database", specialTransform);
        this.ApplyTransform(specialTransformFile);

        if (this.Tables.Contains("MsiPatchHeaders") && this.CountRows("MsiPatchHeaders") > 0 &&
            (!this.Tables.Contains("Patch") || this.CountRows("Patch", "`StreamRef_` <> ''") == 0))
        {
            this.LogMessage("Error: patch transform failed because of missing Patch.StreamRef_ column");
            throw new InstallerException("Patch transform failed because of missing Patch.StreamRef_ column");
        }

        IList<int> mediaIds = this.ExecuteIntegerQuery("SELECT `Media_` FROM `PatchPackage` " +
            "WHERE `PatchId` = '{0}'", patchPackage.PatchCode);
        if (mediaIds.Count == 0)
        {
            this.LogMessage("Warning: PatchPackage Media record not found -- " +
                "skipping inclusion of patch cabinet");
        }
        else
        {
            int patchMediaDiskId = mediaIds[0];
            IList<string> patchCabinets = this.ExecuteStringQuery("SELECT `Cabinet` FROM `Media` " +
                "WHERE `DiskId` = {0}", patchMediaDiskId);
            if(patchCabinets.Count == 0)
            {
                this.LogMessage("Patch cabinet record not found");
                throw new InstallerException("Patch cabinet record not found.");
            }
            string patchCabinet = patchCabinets[0];
            this.LogMessage("Patch cabinet = {0}", patchCabinet);
            if(!patchCabinet.StartsWith("#", StringComparison.Ordinal))
            {
                this.LogMessage("Error: Patch cabinet must be embedded");
                throw new InstallerException("Patch cabinet must be embedded.");
            }
            patchCabinet = patchCabinet.Substring(1);

            string renamePatchCabinet = patchPrefix + patchCabinet;

            const int HIGH_DISKID = 30000; // Must not collide with other patch media DiskIDs
            int renamePatchMediaDiskId = HIGH_DISKID;
            while (this.CountRows("Media", "`DiskId` = " + renamePatchMediaDiskId) > 0) renamePatchMediaDiskId++;

            // Since the patch cab is now embedded in the MSI, it shouldn't have a separate disk prompt/source
            this.LogMessage("Renaming the patch media record");
            int lastSeq = Convert.ToInt32(this.ExecuteScalar("SELECT `LastSequence` FROM `Media` WHERE `DiskId` = {0}", patchMediaDiskId));
            this.Execute("DELETE FROM `Media` WHERE `DiskId` = {0}", patchMediaDiskId);
            this.Execute("INSERT INTO `Media` (`DiskId`, `LastSequence`, `Cabinet`) VALUES ({0}, '{1}', '#{2}')",
                renamePatchMediaDiskId, lastSeq, renamePatchCabinet);
            this.Execute("UPDATE `PatchPackage` SET `Media_` = {0} WHERE `PatchId` = '{1}'", renamePatchMediaDiskId, patchPackage.PatchCode);

            this.LogMessage("Copying patch cabinet: {0}", patchCabinet);
            string patchCabFile = Path.Combine(this.TempDirectory,
                Path.GetFileNameWithoutExtension(patchCabinet) + ".cab");
            using(View streamView = patchPackage.OpenView("SELECT `Name`, `Data` FROM `_Streams` " +
                  "WHERE `Name` = '{0}'", patchCabinet))
            {
                streamView.Execute();
                Record streamRec = streamView.Fetch();
                if(streamRec == null)
                {
                    this.LogMessage("Error: Patch cabinet not found");
                    throw new InstallerException("Patch cabinet not found.");
                }
                using(streamRec)
                {
                    streamRec.GetStream(2, patchCabFile);
                }
            }
            using(Record patchCabRec = new Record(2))
            {
                patchCabRec[1] = patchCabinet;
                patchCabRec.SetStream(2, patchCabFile);
                this.Execute("INSERT INTO `_Streams` (`Name`, `Data`) VALUES (?, ?)", patchCabRec);
            }

            this.LogMessage("Ensuring PatchFiles action exists in InstallExecuteSequence table");
            if (this.Tables.Contains("InstallExecuteSequence"))
            {
                if(this.CountRows("InstallExecuteSequence", "`Action` = 'PatchFiles'") == 0)
                {
                    IList<int> installFilesSeqList = this.ExecuteIntegerQuery("SELECT `Sequence` " +
                        "FROM `InstallExecuteSequence` WHERE `Action` = 'InstallFiles'");
                    short installFilesSeq = (short) (installFilesSeqList.Count != 0 ?
                        installFilesSeqList[0] : 0);
                    this.Execute("INSERT INTO `InstallExecuteSequence` (`Action`, `Sequence`) " +
                        "VALUES ('PatchFiles', {0})", installFilesSeq + 1);
                }
            }

            // Patch-added files need to be marked always-compressed
            this.LogMessage("Adjusting attributes of patch-added files");
            using(View fileView = this.OpenView("SELECT `File`, `Attributes`, `Sequence` " +
                  "FROM `File` ORDER BY `Sequence`"))
            {
                fileView.Execute();
                
                foreach (Record fileRec in fileView) using(fileRec)
                {
                    int fileAttributes = fileRec.GetInteger(2);
                    if ((fileAttributes & (int) WixToolset.Dtf.WindowsInstaller.FileAttributes.PatchAdded) != 0)
                    {
                        fileAttributes = (fileAttributes | (int) WixToolset.Dtf.WindowsInstaller.FileAttributes.Compressed)
                            & ~(int) WixToolset.Dtf.WindowsInstaller.FileAttributes.NonCompressed
                            & ~(int) WixToolset.Dtf.WindowsInstaller.FileAttributes.PatchAdded;
                        fileRec[2] = fileAttributes;
                        fileView.Update(fileRec);
                    }
                }
            }
        }

        this.LogMessage("Applying new summary info from patch package");
        this.SummaryInfo.RevisionNumber = this.Property["PATCHNEWPACKAGECODE"];
        this.SummaryInfo.Subject = this.Property["PATCHNEWSUMMARYSUBJECT"];
        this.SummaryInfo.Comments = this.Property["PATCHNEWSUMMARYCOMMENTS"];
        this.SummaryInfo.Persist();
        this.Property["PATCHNEWPACKAGECODE"    ] = null;
        this.Property["PATCHNEWSUMMARYSUBJECT" ] = null;
        this.Property["PATCHNEWSUMMARYCOMMENTS"] = null;

        this.LogMessage("Patch application finished");
    }

    /// <summary>
    /// Accessor for getting and setting properties of the InstallPackage database.
    /// </summary>
    public InstallPackageProperties Property
    {
        get
        {
            if(this.properties == null)
            {
                this.properties = new InstallPackageProperties(this);
            }
            return this.properties;
        }
    }
    private InstallPackageProperties properties = null;
}

/// <summary>
/// Accessor for getting and setting properties of the <see cref="InstallPackage"/> database.
/// </summary>
public class InstallPackageProperties
{
    internal InstallPackageProperties(InstallPackage installPackage)
    {
        this.installPackage = installPackage;
    }
    private InstallPackage installPackage;

    /// <summary>
    /// Gets or sets a property in the database. When getting a property
    /// that does not exist in the database, an empty string is returned.
    /// To remove a property from the database, set it to an empty string.
    /// </summary>
    /// <remarks>
    /// This has the same results as direct SQL queries on the Property table; it's only
    /// meant to be a more convenient way of access.
    /// </remarks>
    public string this[string name]
    {
        get
        {
            IList<string> values = installPackage.ExecuteStringQuery(
                "SELECT `Value` FROM `Property` WHERE `Property` = '{0}'", name);
            return (values.Count != 0 ? values[0] : "");
        }
        set
        {
            Record propRec = new Record(name, (value != null ? value : ""));
            installPackage.Execute("DELETE FROM `Property` WHERE `Property` = ?", propRec);
            if(value != null && value.Length != 0)
            {
                installPackage.Execute("INSERT INTO `Property` (`Property`, `Value`) VALUES (?, ?)",
                    propRec);
            }
        }
    }
}

}