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.hxx"
6 #include "windowsHelper.hxx"
8 // Shared code between applications and updater.exe
11 * Verifies if 2 files are byte for byte equivalent.
13 * @param file1Path The first file to verify.
14 * @param file2Path The second file to verify.
15 * @param sameContent Out parameter, TRUE if the files are equal
16 * @return TRUE If there was no error checking the files.
19 VerifySameFiles(LPCWSTR file1Path
, LPCWSTR file2Path
, BOOL
&sameContent
)
22 AutoHandle
file1(CreateFileW(file1Path
, GENERIC_READ
, FILE_SHARE_READ
,
23 nullptr, OPEN_EXISTING
, 0, nullptr));
24 if (file1
== INVALID_HANDLE_VALUE
)
28 AutoHandle
file2(CreateFileW(file2Path
, GENERIC_READ
, FILE_SHARE_READ
,
29 nullptr, OPEN_EXISTING
, 0, nullptr));
30 if (file2
== INVALID_HANDLE_VALUE
)
35 DWORD fileSize1
= GetFileSize(file1
.get(), nullptr);
36 DWORD fileSize2
= GetFileSize(file2
.get(), nullptr);
37 if (INVALID_FILE_SIZE
== fileSize1
|| INVALID_FILE_SIZE
== fileSize2
)
42 if (fileSize1
!= fileSize2
)
44 // sameContent is already set to FALSE
48 char buf1
[COMPARE_BLOCKSIZE
];
49 char buf2
[COMPARE_BLOCKSIZE
];
50 DWORD numBlocks
= fileSize1
/ COMPARE_BLOCKSIZE
;
51 DWORD leftOver
= fileSize1
% COMPARE_BLOCKSIZE
;
53 for (DWORD i
= 0; i
< numBlocks
; i
++)
55 if (!ReadFile(file1
.get(), buf1
, COMPARE_BLOCKSIZE
, &readAmount
, nullptr) ||
56 readAmount
!= COMPARE_BLOCKSIZE
)
61 if (!ReadFile(file2
.get(), buf2
, COMPARE_BLOCKSIZE
, &readAmount
, nullptr) ||
62 readAmount
!= COMPARE_BLOCKSIZE
)
67 if (memcmp(buf1
, buf2
, COMPARE_BLOCKSIZE
))
69 // sameContent is already set to FALSE
76 if (!ReadFile(file1
.get(), buf1
, leftOver
, &readAmount
, nullptr) ||
77 readAmount
!= leftOver
)
82 if (!ReadFile(file2
.get(), buf2
, leftOver
, &readAmount
, nullptr) ||
83 readAmount
!= leftOver
)
88 if (memcmp(buf1
, buf2
, leftOver
))
90 // sameContent is already set to FALSE