michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "servicebase.h" michael@0: #include "nsWindowsHelpers.h" michael@0: michael@0: // Shared code between applications and updater.exe michael@0: #include "nsWindowsRestart.cpp" michael@0: michael@0: /** michael@0: * Verifies if 2 files are byte for byte equivalent. michael@0: * michael@0: * @param file1Path The first file to verify. michael@0: * @param file2Path The second file to verify. michael@0: * @param sameContent Out parameter, TRUE if the files are equal michael@0: * @return TRUE If there was no error checking the files. michael@0: */ michael@0: BOOL michael@0: VerifySameFiles(LPCWSTR file1Path, LPCWSTR file2Path, BOOL &sameContent) michael@0: { michael@0: sameContent = FALSE; michael@0: nsAutoHandle file1(CreateFileW(file1Path, GENERIC_READ, FILE_SHARE_READ, michael@0: nullptr, OPEN_EXISTING, 0, nullptr)); michael@0: if (INVALID_HANDLE_VALUE == file1) { michael@0: return FALSE; michael@0: } michael@0: nsAutoHandle file2(CreateFileW(file2Path, GENERIC_READ, FILE_SHARE_READ, michael@0: nullptr, OPEN_EXISTING, 0, nullptr)); michael@0: if (INVALID_HANDLE_VALUE == file2) { michael@0: return FALSE; michael@0: } michael@0: michael@0: DWORD fileSize1 = GetFileSize(file1, nullptr); michael@0: DWORD fileSize2 = GetFileSize(file2, nullptr); michael@0: if (INVALID_FILE_SIZE == fileSize1 || INVALID_FILE_SIZE == fileSize2) { michael@0: return FALSE; michael@0: } michael@0: michael@0: if (fileSize1 != fileSize2) { michael@0: // sameContent is already set to FALSE michael@0: return TRUE; michael@0: } michael@0: michael@0: char buf1[COMPARE_BLOCKSIZE]; michael@0: char buf2[COMPARE_BLOCKSIZE]; michael@0: DWORD numBlocks = fileSize1 / COMPARE_BLOCKSIZE; michael@0: DWORD leftOver = fileSize1 % COMPARE_BLOCKSIZE; michael@0: DWORD readAmount; michael@0: for (DWORD i = 0; i < numBlocks; i++) { michael@0: if (!ReadFile(file1, buf1, COMPARE_BLOCKSIZE, &readAmount, nullptr) || michael@0: readAmount != COMPARE_BLOCKSIZE) { michael@0: return FALSE; michael@0: } michael@0: michael@0: if (!ReadFile(file2, buf2, COMPARE_BLOCKSIZE, &readAmount, nullptr) || michael@0: readAmount != COMPARE_BLOCKSIZE) { michael@0: return FALSE; michael@0: } michael@0: michael@0: if (memcmp(buf1, buf2, COMPARE_BLOCKSIZE)) { michael@0: // sameContent is already set to FALSE michael@0: return TRUE; michael@0: } michael@0: } michael@0: michael@0: if (leftOver) { michael@0: if (!ReadFile(file1, buf1, leftOver, &readAmount, nullptr) || michael@0: readAmount != leftOver) { michael@0: return FALSE; michael@0: } michael@0: michael@0: if (!ReadFile(file2, buf2, leftOver, &readAmount, nullptr) || michael@0: readAmount != leftOver) { michael@0: return FALSE; michael@0: } michael@0: michael@0: if (memcmp(buf1, buf2, leftOver)) { michael@0: // sameContent is already set to FALSE michael@0: return TRUE; michael@0: } michael@0: } michael@0: michael@0: sameContent = TRUE; michael@0: return TRUE; michael@0: }