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"
12 * Verifies if 2 files are byte for byte equivalent.
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.
19 BOOL
VerifySameFiles(LPCWSTR file1Path
, LPCWSTR file2Path
, BOOL
& sameContent
) {
21 nsAutoHandle
file1(CreateFileW(file1Path
, GENERIC_READ
, FILE_SHARE_READ
,
22 nullptr, OPEN_EXISTING
, 0, nullptr));
23 if (INVALID_HANDLE_VALUE
== file1
) {
26 nsAutoHandle
file2(CreateFileW(file2Path
, GENERIC_READ
, FILE_SHARE_READ
,
27 nullptr, OPEN_EXISTING
, 0, nullptr));
28 if (INVALID_HANDLE_VALUE
== file2
) {
32 DWORD fileSize1
= GetFileSize(file1
, nullptr);
33 DWORD fileSize2
= GetFileSize(file2
, nullptr);
34 if (INVALID_FILE_SIZE
== fileSize1
|| INVALID_FILE_SIZE
== fileSize2
) {
38 if (fileSize1
!= fileSize2
) {
39 // sameContent is already set to FALSE
43 char buf1
[COMPARE_BLOCKSIZE
];
44 char buf2
[COMPARE_BLOCKSIZE
];
45 DWORD numBlocks
= fileSize1
/ COMPARE_BLOCKSIZE
;
46 DWORD leftOver
= fileSize1
% COMPARE_BLOCKSIZE
;
48 for (DWORD i
= 0; i
< numBlocks
; i
++) {
49 if (!ReadFile(file1
, buf1
, COMPARE_BLOCKSIZE
, &readAmount
, nullptr) ||
50 readAmount
!= COMPARE_BLOCKSIZE
) {
54 if (!ReadFile(file2
, buf2
, COMPARE_BLOCKSIZE
, &readAmount
, nullptr) ||
55 readAmount
!= COMPARE_BLOCKSIZE
) {
59 if (memcmp(buf1
, buf2
, COMPARE_BLOCKSIZE
)) {
60 // sameContent is already set to FALSE
66 if (!ReadFile(file1
, buf1
, leftOver
, &readAmount
, nullptr) ||
67 readAmount
!= leftOver
) {
71 if (!ReadFile(file2
, buf2
, leftOver
, &readAmount
, nullptr) ||
72 readAmount
!= leftOver
) {
76 if (memcmp(buf1
, buf2
, leftOver
)) {
77 // sameContent is already set to FALSE