Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / onlineupdate / source / service / servicebase.cxx
blob4fb632878cec5e780c3db45e2dd144f26b223985
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
10 /**
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.
18 BOOL
19 VerifySameFiles(LPCWSTR file1Path, LPCWSTR file2Path, BOOL &sameContent)
21 sameContent = FALSE;
22 AutoHandle file1(CreateFileW(file1Path, GENERIC_READ, FILE_SHARE_READ,
23 nullptr, OPEN_EXISTING, 0, nullptr));
24 if (file1 == INVALID_HANDLE_VALUE)
26 return FALSE;
28 AutoHandle file2(CreateFileW(file2Path, GENERIC_READ, FILE_SHARE_READ,
29 nullptr, OPEN_EXISTING, 0, nullptr));
30 if (file2 == INVALID_HANDLE_VALUE)
32 return FALSE;
35 DWORD fileSize1 = GetFileSize(file1.get(), nullptr);
36 DWORD fileSize2 = GetFileSize(file2.get(), nullptr);
37 if (INVALID_FILE_SIZE == fileSize1 || INVALID_FILE_SIZE == fileSize2)
39 return FALSE;
42 if (fileSize1 != fileSize2)
44 // sameContent is already set to FALSE
45 return TRUE;
48 char buf1[COMPARE_BLOCKSIZE];
49 char buf2[COMPARE_BLOCKSIZE];
50 DWORD numBlocks = fileSize1 / COMPARE_BLOCKSIZE;
51 DWORD leftOver = fileSize1 % COMPARE_BLOCKSIZE;
52 DWORD readAmount;
53 for (DWORD i = 0; i < numBlocks; i++)
55 if (!ReadFile(file1.get(), buf1, COMPARE_BLOCKSIZE, &readAmount, nullptr) ||
56 readAmount != COMPARE_BLOCKSIZE)
58 return FALSE;
61 if (!ReadFile(file2.get(), buf2, COMPARE_BLOCKSIZE, &readAmount, nullptr) ||
62 readAmount != COMPARE_BLOCKSIZE)
64 return FALSE;
67 if (memcmp(buf1, buf2, COMPARE_BLOCKSIZE))
69 // sameContent is already set to FALSE
70 return TRUE;
74 if (leftOver)
76 if (!ReadFile(file1.get(), buf1, leftOver, &readAmount, nullptr) ||
77 readAmount != leftOver)
79 return FALSE;
82 if (!ReadFile(file2.get(), buf2, leftOver, &readAmount, nullptr) ||
83 readAmount != leftOver)
85 return FALSE;
88 if (memcmp(buf1, buf2, leftOver))
90 // sameContent is already set to FALSE
91 return TRUE;
95 sameContent = TRUE;
96 return TRUE;