LibreOffice Module onlineupdate (master) 1
servicebase.cxx
Go to the documentation of this file.
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/. */
4
5#include "servicebase.hxx"
6#include "windowsHelper.hxx"
7
8// Shared code between applications and updater.exe
9
18BOOL
19VerifySameFiles(LPCWSTR file1Path, LPCWSTR file2Path, BOOL &sameContent)
20{
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)
25 {
26 return FALSE;
27 }
28 AutoHandle file2(CreateFileW(file2Path, GENERIC_READ, FILE_SHARE_READ,
29 nullptr, OPEN_EXISTING, 0, nullptr));
30 if (file2 == INVALID_HANDLE_VALUE)
31 {
32 return FALSE;
33 }
34
35 DWORD fileSize1 = GetFileSize(file1.get(), nullptr);
36 DWORD fileSize2 = GetFileSize(file2.get(), nullptr);
37 if (INVALID_FILE_SIZE == fileSize1 || INVALID_FILE_SIZE == fileSize2)
38 {
39 return FALSE;
40 }
41
42 if (fileSize1 != fileSize2)
43 {
44 // sameContent is already set to FALSE
45 return TRUE;
46 }
47
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++)
54 {
55 if (!ReadFile(file1.get(), buf1, COMPARE_BLOCKSIZE, &readAmount, nullptr) ||
56 readAmount != COMPARE_BLOCKSIZE)
57 {
58 return FALSE;
59 }
60
61 if (!ReadFile(file2.get(), buf2, COMPARE_BLOCKSIZE, &readAmount, nullptr) ||
62 readAmount != COMPARE_BLOCKSIZE)
63 {
64 return FALSE;
65 }
66
67 if (memcmp(buf1, buf2, COMPARE_BLOCKSIZE))
68 {
69 // sameContent is already set to FALSE
70 return TRUE;
71 }
72 }
73
74 if (leftOver)
75 {
76 if (!ReadFile(file1.get(), buf1, leftOver, &readAmount, nullptr) ||
77 readAmount != leftOver)
78 {
79 return FALSE;
80 }
81
82 if (!ReadFile(file2.get(), buf2, leftOver, &readAmount, nullptr) ||
83 readAmount != leftOver)
84 {
85 return FALSE;
86 }
87
88 if (memcmp(buf1, buf2, leftOver))
89 {
90 // sameContent is already set to FALSE
91 return TRUE;
92 }
93 }
94
95 sameContent = TRUE;
96 return TRUE;
97}
#define TRUE
#define FALSE
int i
const wchar_t *typedef BOOL
BOOL VerifySameFiles(LPCWSTR file1Path, LPCWSTR file2Path, BOOL &sameContent)
Verifies if 2 files are byte for byte equivalent.
Definition: servicebase.cxx:19
#define COMPARE_BLOCKSIZE
Definition: servicebase.hxx:14
HANDLE get() const