| 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 | |
| 7 | namespace WixToolset.Dtf.Tools.DDiff |
| 8 | { |
| 9 | public class VersionedFileDiffEngine : IDiffEngine |
| 10 | { |
| 11 | public VersionedFileDiffEngine() |
| 12 | { |
| 13 | } |
| 14 | |
| 15 | private bool IsVersionedFile(string file) |
| 16 | { |
| 17 | return Installer.GetFileVersion(file) != ""; |
| 18 | } |
| 19 | |
| 20 | public float GetDiffQuality(string diffInput1, string diffInput2, string[] options, IDiffEngineFactory diffFactory) |
| 21 | { |
| 22 | if(diffInput1 != null && File.Exists(diffInput1) && |
| 23 | diffInput2 != null && File.Exists(diffInput2) && |
| 24 | (IsVersionedFile(diffInput1) || IsVersionedFile(diffInput2))) |
| 25 | { |
| 26 | return .20f; |
| 27 | } |
| 28 | else |
| 29 | { |
| 30 | return 0; |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | public bool GetDiff(string diffInput1, string diffInput2, string[] options, TextWriter diffOutput, string linePrefix, IDiffEngineFactory diffFactory) |
| 35 | { |
| 36 | bool difference = false; |
| 37 | |
| 38 | string ver1 = Installer.GetFileVersion(diffInput1); |
| 39 | string ver2 = Installer.GetFileVersion(diffInput2); |
| 40 | |
| 41 | if(ver1 != ver2) |
| 42 | { |
| 43 | diffOutput.WriteLine("{0}File version: {1} -> {2}", linePrefix, ver1, ver2); |
| 44 | difference = true; |
| 45 | } |
| 46 | else |
| 47 | { |
| 48 | FileStream stream1 = new FileStream(diffInput1, FileMode.Open, FileAccess.Read, FileShare.Read); |
| 49 | FileStream stream2 = new FileStream(diffInput2, FileMode.Open, FileAccess.Read, FileShare.Read); |
| 50 | |
| 51 | byte[] buf1 = new byte[512]; |
| 52 | byte[] buf2 = new byte[512]; |
| 53 | |
| 54 | while(!difference) |
| 55 | { |
| 56 | int count1 = stream1.Read(buf1, 0, buf1.Length); |
| 57 | int count2 = stream2.Read(buf2, 0, buf2.Length); |
| 58 | |
| 59 | for(int i = 0; i < count1; i++) |
| 60 | { |
| 61 | if(i == count2 || buf1[i] != buf2[i]) |
| 62 | { |
| 63 | difference = true; |
| 64 | break; |
| 65 | } |
| 66 | } |
| 67 | if(count1 < buf1.Length) // EOF |
| 68 | { |
| 69 | break; |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | stream1.Close(); |
| 74 | stream2.Close(); |
| 75 | |
| 76 | if(difference) |
| 77 | { |
| 78 | diffOutput.WriteLine("{0}File versions match but bits differ.", linePrefix); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | return difference; |
| 83 | } |
| 84 | |
| 85 | public IDiffEngine Clone() |
| 86 | { |
| 87 | return new VersionedFileDiffEngine(); |
| 88 | } |
| 89 | } |
| 90 | } |