|
3 | 3 | using System; |
4 | 4 | using System.Collections.Generic; |
5 | 5 | using System.IO; |
| 6 | + using System.IO.MemoryMappedFiles; |
6 | 7 | using System.Linq; |
7 | 8 | using System.Text; |
8 | 9 | using Microsoft.Build.Framework; |
@@ -64,66 +65,23 @@ private bool IsContentOfFilesTheSame(string file1, string file2) |
64 | 65 | if (File.Exists(file1) ^ File.Exists(file2)) return false; |
65 | 66 | // If both are missing, that's the same. |
66 | 67 | if (!File.Exists(file1)) return true; |
67 | | - // If both are present, we need to do a content comparison. |
68 | | - using (FileStream fileStream1 = File.OpenRead(file1)) |
69 | | - { |
70 | | - using (FileStream fileStream2 = File.OpenRead(file2)) |
71 | | - { |
72 | | - if (fileStream1.Length != fileStream2.Length) return false; |
73 | | - byte[] buffer1 = new byte[4096]; |
74 | | - byte[] buffer2 = new byte[buffer1.Length]; |
75 | | - int bytesRead; |
76 | | - do |
77 | | - { |
78 | | - bytesRead = fileStream1.Read(buffer1, 0, buffer1.Length); |
79 | | - if (fileStream2.Read(buffer2, 0, buffer2.Length) != bytesRead) |
80 | | - { |
81 | | - // We should never get here since we compared file lengths, but |
82 | | - // this is a sanity check. |
83 | | - return false; |
84 | | - } |
85 | | - for (int i = 0; i < bytesRead; i++) |
86 | | - { |
87 | | - if (buffer1[i] != buffer2[i]) |
88 | | - { |
89 | | - return false; |
90 | | - } |
91 | | - } |
92 | | - } while (bytesRead == buffer1.Length); |
93 | | - } |
94 | | - } |
95 | 68 |
|
96 | | - return true; |
97 | | - } |
| 69 | + if (new FileInfo(file1).Length != new FileInfo(file2).Length) return false; |
98 | 70 |
|
99 | | - /// <summary> |
100 | | - /// Tests whether a file is up to date with respect to another, |
101 | | - /// based on existence, last write time and file size. |
102 | | - /// </summary> |
103 | | - /// <param name="sourcePath">The source path.</param> |
104 | | - /// <param name="destPath">The dest path.</param> |
105 | | - /// <returns><c>true</c> if the files are the same; <c>false</c> if the files are different</returns> |
106 | | - internal static bool FastFileEqualityCheck(string sourcePath, string destPath) |
107 | | - { |
108 | | - FileInfo sourceInfo = new FileInfo(sourcePath); |
109 | | - FileInfo destInfo = new FileInfo(destPath); |
| 71 | + // If both are present, we need to do a content comparison. |
| 72 | + // Keep our comparison simple by loading both in memory. |
| 73 | + byte[] file1Content = File.ReadAllBytes(file1); |
| 74 | + byte[] file2Content = File.ReadAllBytes(file2); |
110 | 75 |
|
111 | | - if (sourceInfo.Exists ^ destInfo.Exists) |
112 | | - { |
113 | | - // Either the source file or the destination file is missing. |
114 | | - return false; |
115 | | - } |
| 76 | + // One more sanity check. |
| 77 | + if (file1Content.Length != file2Content.Length) return false; |
116 | 78 |
|
117 | | - if (!sourceInfo.Exists) |
| 79 | + for (int i = 0; i < file1Content.Length; i++) |
118 | 80 | { |
119 | | - // Neither file exists. |
120 | | - return true; |
| 81 | + if (file1Content[i] != file2Content[i]) return false; |
121 | 82 | } |
122 | 83 |
|
123 | | - // We'll say the files are the same if their modification date and length are the same. |
124 | | - return |
125 | | - sourceInfo.LastWriteTimeUtc == destInfo.LastWriteTimeUtc && |
126 | | - sourceInfo.Length == destInfo.Length; |
| 84 | + return true; |
127 | 85 | } |
128 | 86 | } |
129 | 87 | } |
0 commit comments