forked from microsoft/azure-pipelines-tasks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHelpers.cs
65 lines (55 loc) · 2.21 KB
/
Helpers.cs
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
namespace BuildConfigGen
{
internal static class Helpers
{
internal static bool FilesEqual(string sourcePath, string targetPath)
{
FileInfo fi = new FileInfo(sourcePath);
FileInfo fi2 = new FileInfo(targetPath);
int mismatchChars = 10;
bool ret = true;
if (!fi2.Exists)
{
return false;
}
if (fi.Length != fi2.Length)
{
Console.WriteLine($"mismatch {fi.Length}!={fi2.Length}");
return false;
}
bool eof = false;
const int bufferSize = 4096 * 255;
using (var fs1 = fi.Open(FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
using (var fs2 = fi2.Open(FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
using (var br1 = new BinaryReader(fs1))
using (var br2 = new BinaryReader(fs2))
{
do
{
// use BinaryReader.ReadBytes to guarantee the size of the buffer matches between buffer1 and buffer2. Stream.ReadBytes doesn’t guarantee reading up to bufferSize
byte[] buffer = br1.ReadBytes(bufferSize);
byte[] buffer2 = br2.ReadBytes(bufferSize);
if (buffer.Length != buffer2.Length)
{
throw new Exception($"unexpected mismatch between buffer and buffer2 lengths {buffer.Length} {buffer2.Length}");
}
for (int i = 0; i < buffer.Length; i++)
{
if (buffer[i] != buffer2[i])
{
Console.WriteLine($"mismatch buffer[i={i}]={(int)buffer[i]} != {(int)buffer2[i]}");
ret = false;
mismatchChars--;
if (mismatchChars < 1)
{
return false;
}
}
}
eof = buffer.Length == 0;
} while (!eof);
}
return ret;
}
}
}