Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 #include "servicebase.h"
6 #include "nsWindowsHelpers.h"
8 // Shared code between applications and updater.exe
9 #include "nsWindowsRestart.cpp"
11 /**
12 * Verifies if 2 files are byte for byte equivalent.
13 *
14 * @param file1Path The first file to verify.
15 * @param file2Path The second file to verify.
16 * @param sameContent Out parameter, TRUE if the files are equal
17 * @return TRUE If there was no error checking the files.
18 */
19 BOOL
20 VerifySameFiles(LPCWSTR file1Path, LPCWSTR file2Path, BOOL &sameContent)
21 {
22 sameContent = FALSE;
23 nsAutoHandle file1(CreateFileW(file1Path, GENERIC_READ, FILE_SHARE_READ,
24 nullptr, OPEN_EXISTING, 0, nullptr));
25 if (INVALID_HANDLE_VALUE == file1) {
26 return FALSE;
27 }
28 nsAutoHandle file2(CreateFileW(file2Path, GENERIC_READ, FILE_SHARE_READ,
29 nullptr, OPEN_EXISTING, 0, nullptr));
30 if (INVALID_HANDLE_VALUE == file2) {
31 return FALSE;
32 }
34 DWORD fileSize1 = GetFileSize(file1, nullptr);
35 DWORD fileSize2 = GetFileSize(file2, nullptr);
36 if (INVALID_FILE_SIZE == fileSize1 || INVALID_FILE_SIZE == fileSize2) {
37 return FALSE;
38 }
40 if (fileSize1 != fileSize2) {
41 // sameContent is already set to FALSE
42 return TRUE;
43 }
45 char buf1[COMPARE_BLOCKSIZE];
46 char buf2[COMPARE_BLOCKSIZE];
47 DWORD numBlocks = fileSize1 / COMPARE_BLOCKSIZE;
48 DWORD leftOver = fileSize1 % COMPARE_BLOCKSIZE;
49 DWORD readAmount;
50 for (DWORD i = 0; i < numBlocks; i++) {
51 if (!ReadFile(file1, buf1, COMPARE_BLOCKSIZE, &readAmount, nullptr) ||
52 readAmount != COMPARE_BLOCKSIZE) {
53 return FALSE;
54 }
56 if (!ReadFile(file2, buf2, COMPARE_BLOCKSIZE, &readAmount, nullptr) ||
57 readAmount != COMPARE_BLOCKSIZE) {
58 return FALSE;
59 }
61 if (memcmp(buf1, buf2, COMPARE_BLOCKSIZE)) {
62 // sameContent is already set to FALSE
63 return TRUE;
64 }
65 }
67 if (leftOver) {
68 if (!ReadFile(file1, buf1, leftOver, &readAmount, nullptr) ||
69 readAmount != leftOver) {
70 return FALSE;
71 }
73 if (!ReadFile(file2, buf2, leftOver, &readAmount, nullptr) ||
74 readAmount != leftOver) {
75 return FALSE;
76 }
78 if (memcmp(buf1, buf2, leftOver)) {
79 // sameContent is already set to FALSE
80 return TRUE;
81 }
82 }
84 sameContent = TRUE;
85 return TRUE;
86 }