Bug 1921551 - React to sync sign in flow correctly r=android-reviewers,matt-tighe
[gecko.git] / toolkit / components / maintenanceservice / servicebase.cpp
blobd6004234c7cfc2676375fbbbe887d66328436003
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.
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) {
20 sameContent = FALSE;
21 nsAutoHandle file1(CreateFileW(file1Path, GENERIC_READ, FILE_SHARE_READ,
22 nullptr, OPEN_EXISTING, 0, nullptr));
23 if (INVALID_HANDLE_VALUE == file1) {
24 return FALSE;
26 nsAutoHandle file2(CreateFileW(file2Path, GENERIC_READ, FILE_SHARE_READ,
27 nullptr, OPEN_EXISTING, 0, nullptr));
28 if (INVALID_HANDLE_VALUE == file2) {
29 return FALSE;
32 DWORD fileSize1 = GetFileSize(file1, nullptr);
33 DWORD fileSize2 = GetFileSize(file2, nullptr);
34 if (INVALID_FILE_SIZE == fileSize1 || INVALID_FILE_SIZE == fileSize2) {
35 return FALSE;
38 if (fileSize1 != fileSize2) {
39 // sameContent is already set to FALSE
40 return TRUE;
43 char buf1[COMPARE_BLOCKSIZE];
44 char buf2[COMPARE_BLOCKSIZE];
45 DWORD numBlocks = fileSize1 / COMPARE_BLOCKSIZE;
46 DWORD leftOver = fileSize1 % COMPARE_BLOCKSIZE;
47 DWORD readAmount;
48 for (DWORD i = 0; i < numBlocks; i++) {
49 if (!ReadFile(file1, buf1, COMPARE_BLOCKSIZE, &readAmount, nullptr) ||
50 readAmount != COMPARE_BLOCKSIZE) {
51 return FALSE;
54 if (!ReadFile(file2, buf2, COMPARE_BLOCKSIZE, &readAmount, nullptr) ||
55 readAmount != COMPARE_BLOCKSIZE) {
56 return FALSE;
59 if (memcmp(buf1, buf2, COMPARE_BLOCKSIZE)) {
60 // sameContent is already set to FALSE
61 return TRUE;
65 if (leftOver) {
66 if (!ReadFile(file1, buf1, leftOver, &readAmount, nullptr) ||
67 readAmount != leftOver) {
68 return FALSE;
71 if (!ReadFile(file2, buf2, leftOver, &readAmount, nullptr) ||
72 readAmount != leftOver) {
73 return FALSE;
76 if (memcmp(buf1, buf2, leftOver)) {
77 // sameContent is already set to FALSE
78 return TRUE;
82 sameContent = TRUE;
83 return TRUE;