aboutsummaryrefslogtreecommitdiff
path: root/src/tools/Dtf/DDiff/VersionedFileDiffEngine.cs
blob: 9a3e399fb02766c4e9973fad82dc84094d68a4ee (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
// 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.

using System;
using System.IO;
using WixToolset.Dtf.WindowsInstaller;

namespace WixToolset.Dtf.Tools.DDiff
{
	public class VersionedFileDiffEngine : IDiffEngine
	{
		public VersionedFileDiffEngine()
		{
		}

		private bool IsVersionedFile(string file)
		{
			return Installer.GetFileVersion(file) != "";
		}

		public float GetDiffQuality(string diffInput1, string diffInput2, string[] options, IDiffEngineFactory diffFactory)
		{
			if(diffInput1 != null && File.Exists(diffInput1) &&
			   diffInput2 != null && File.Exists(diffInput2) &&
			   (IsVersionedFile(diffInput1) || IsVersionedFile(diffInput2)))
			{
				return .20f;
			}
			else
			{
				return 0;
			}
		}

		public bool GetDiff(string diffInput1, string diffInput2, string[] options, TextWriter diffOutput, string linePrefix, IDiffEngineFactory diffFactory)
		{
			bool difference = false;

			string ver1 = Installer.GetFileVersion(diffInput1);
			string ver2 = Installer.GetFileVersion(diffInput2);

			if(ver1 != ver2)
			{
				diffOutput.WriteLine("{0}File version: {1} -> {2}", linePrefix, ver1, ver2);
				difference = true;
			}
			else
			{
				FileStream stream1 = new FileStream(diffInput1, FileMode.Open, FileAccess.Read, FileShare.Read);
				FileStream stream2 = new FileStream(diffInput2, FileMode.Open, FileAccess.Read, FileShare.Read);

				byte[] buf1 = new byte[512];
				byte[] buf2 = new byte[512];

				while(!difference)
				{
					int count1 = stream1.Read(buf1, 0, buf1.Length);
					int count2 = stream2.Read(buf2, 0, buf2.Length);

					for(int i = 0; i < count1; i++)
					{
						if(i == count2 || buf1[i] != buf2[i])
						{
							difference = true;
							break;
						}
					}
					if(count1 < buf1.Length) // EOF
					{
						break;
					}
				}

				stream1.Close();
				stream2.Close();

				if(difference)
				{
					diffOutput.WriteLine("{0}File versions match but bits differ.", linePrefix);
				}
			}

			return difference;
		}

		public IDiffEngine Clone()
		{
			return new VersionedFileDiffEngine();
		}
	}
}