| 1 | // 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. |
| 2 | |
| 3 | using System; |
| 4 | using System.IO; |
| 5 | using WixToolset.Dtf.WindowsInstaller; |
| 6 | using WixToolset.Dtf.WindowsInstaller.Package; |
| 7 | |
| 8 | namespace WixToolset.Dtf.Tools.DDiff |
| 9 | { |
| 10 | public class MspDiffEngine : MsiDiffEngine |
| 11 | { |
| 12 | public MspDiffEngine() |
| 13 | { |
| 14 | } |
| 15 | |
| 16 | private string GetPatchTargetOption(string[] options) |
| 17 | { |
| 18 | for(int i = 0; i < options.Length - 1; i++) |
| 19 | { |
| 20 | switch(options[i].ToLower()) |
| 21 | { |
| 22 | case "/p": goto case "-patchtarget"; |
| 23 | case "-p": goto case "-patchtarget"; |
| 24 | case "/patchtarget": goto case "-patchtarget"; |
| 25 | case "-patchtarget": return options[i+1]; |
| 26 | } |
| 27 | } |
| 28 | return null; |
| 29 | } |
| 30 | |
| 31 | public override float GetDiffQuality(string diffInput1, string diffInput2, string[] options, IDiffEngineFactory diffFactory) |
| 32 | { |
| 33 | if(diffInput1 != null && File.Exists(diffInput1) && |
| 34 | diffInput2 != null && File.Exists(diffInput2) && |
| 35 | GetPatchTargetOption(options) != null && |
| 36 | (IsMspPatch(diffInput1) && IsMspPatch(diffInput2))) |
| 37 | { |
| 38 | return .80f; |
| 39 | } |
| 40 | else if(diffInput1 != null && File.Exists(diffInput1) && |
| 41 | diffInput2 != null && File.Exists(diffInput2) && |
| 42 | GetPatchTargetOption(options) == null && |
| 43 | (IsMspPatch(diffInput1) && IsMsiDatabase(diffInput2)) || |
| 44 | (IsMsiDatabase(diffInput1) && IsMspPatch(diffInput2))) |
| 45 | { |
| 46 | return .75f; |
| 47 | } |
| 48 | else |
| 49 | { |
| 50 | return 0; |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | public override bool GetDiff(string diffInput1, string diffInput2, string[] options, TextWriter diffOutput, string linePrefix, IDiffEngineFactory diffFactory) |
| 55 | { |
| 56 | bool difference = false; |
| 57 | |
| 58 | InstallPackage db1, db2; |
| 59 | if(IsMspPatch(diffInput1)) |
| 60 | { |
| 61 | string patchTargetDbFile = GetPatchTargetOption(options); |
| 62 | if(patchTargetDbFile == null) patchTargetDbFile = diffInput2; |
| 63 | string tempPatchedDbFile = Path.GetTempFileName(); |
| 64 | File.Copy(patchTargetDbFile, tempPatchedDbFile, true); |
| 65 | File.SetAttributes(tempPatchedDbFile, File.GetAttributes(tempPatchedDbFile) & ~System.IO.FileAttributes.ReadOnly); |
| 66 | db1 = new InstallPackage(tempPatchedDbFile, DatabaseOpenMode.Direct); |
| 67 | db1.ApplyPatch(new PatchPackage(diffInput1), null); |
| 68 | db1.Commit(); |
| 69 | } |
| 70 | else |
| 71 | { |
| 72 | db1 = new InstallPackage(diffInput1, DatabaseOpenMode.ReadOnly); |
| 73 | } |
| 74 | if(IsMspPatch(diffInput2)) |
| 75 | { |
| 76 | string patchTargetDbFile = GetPatchTargetOption(options); |
| 77 | if(patchTargetDbFile == null) patchTargetDbFile = diffInput1; |
| 78 | string tempPatchedDbFile = Path.GetTempFileName(); |
| 79 | File.Copy(patchTargetDbFile, tempPatchedDbFile, true); |
| 80 | File.SetAttributes(tempPatchedDbFile, File.GetAttributes(tempPatchedDbFile) & ~System.IO.FileAttributes.ReadOnly); |
| 81 | db2 = new InstallPackage(tempPatchedDbFile, DatabaseOpenMode.Direct); |
| 82 | db2.ApplyPatch(new PatchPackage(diffInput2), null); |
| 83 | db2.Commit(); |
| 84 | } |
| 85 | else |
| 86 | { |
| 87 | db2 = new InstallPackage(diffInput2, DatabaseOpenMode.ReadOnly); |
| 88 | } |
| 89 | |
| 90 | if(GetSummaryInfoDiff(db1, db2, options, diffOutput, linePrefix, diffFactory)) difference = true; |
| 91 | if(GetDatabaseDiff(db1, db2, options, diffOutput, linePrefix, diffFactory)) difference = true; |
| 92 | if(GetStreamsDiff(db1, db2, options, diffOutput, linePrefix, diffFactory)) difference = true; |
| 93 | |
| 94 | db1.Close(); |
| 95 | db2.Close(); |
| 96 | |
| 97 | try |
| 98 | { |
| 99 | if(IsMspPatch(diffInput1)) File.Delete(db1.FilePath); |
| 100 | if(IsMspPatch(diffInput1)) File.Delete(db2.FilePath); |
| 101 | } |
| 102 | catch(IOException) |
| 103 | { |
| 104 | #if DEBUG |
| 105 | Console.WriteLine("Could not delete temporary files {0} and {1}", db1.FilePath, db2.FilePath); |
| 106 | #endif |
| 107 | } |
| 108 | |
| 109 | if(IsMspPatch(diffInput1) && IsMspPatch(diffInput2)) |
| 110 | { |
| 111 | Database dbp1 = new Database(diffInput1, DatabaseOpenMode.ReadOnly); |
| 112 | Database dbp2 = new Database(diffInput2, DatabaseOpenMode.ReadOnly); |
| 113 | |
| 114 | if(GetStreamsDiff(dbp1, dbp2, options, diffOutput, linePrefix, diffFactory)) difference = true; |
| 115 | dbp1.Close(); |
| 116 | dbp2.Close(); |
| 117 | } |
| 118 | |
| 119 | return difference; |
| 120 | } |
| 121 | |
| 122 | public override IDiffEngine Clone() |
| 123 | { |
| 124 | return new MspDiffEngine(); |
| 125 | } |
| 126 | } |
| 127 | } |