toolkit/components/maintenanceservice/servicebase.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/components/maintenanceservice/servicebase.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,86 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +#include "servicebase.h"
     1.9 +#include "nsWindowsHelpers.h"
    1.10 +
    1.11 +// Shared code between applications and updater.exe
    1.12 +#include "nsWindowsRestart.cpp"
    1.13 +
    1.14 +/**
    1.15 + * Verifies if 2 files are byte for byte equivalent.
    1.16 + *
    1.17 + * @param  file1Path   The first file to verify.
    1.18 + * @param  file2Path   The second file to verify.
    1.19 + * @param  sameContent Out parameter, TRUE if the files are equal
    1.20 + * @return TRUE If there was no error checking the files.
    1.21 + */
    1.22 +BOOL
    1.23 +VerifySameFiles(LPCWSTR file1Path, LPCWSTR file2Path, BOOL &sameContent)
    1.24 +{
    1.25 +  sameContent = FALSE;
    1.26 +  nsAutoHandle file1(CreateFileW(file1Path, GENERIC_READ, FILE_SHARE_READ, 
    1.27 +                                 nullptr, OPEN_EXISTING, 0, nullptr));
    1.28 +  if (INVALID_HANDLE_VALUE == file1) {
    1.29 +    return FALSE;
    1.30 +  }
    1.31 +  nsAutoHandle file2(CreateFileW(file2Path, GENERIC_READ, FILE_SHARE_READ, 
    1.32 +                                 nullptr, OPEN_EXISTING, 0, nullptr));
    1.33 +  if (INVALID_HANDLE_VALUE == file2) {
    1.34 +    return FALSE;
    1.35 +  }
    1.36 +
    1.37 +  DWORD fileSize1 = GetFileSize(file1, nullptr);
    1.38 +  DWORD fileSize2 = GetFileSize(file2, nullptr);
    1.39 +  if (INVALID_FILE_SIZE == fileSize1 || INVALID_FILE_SIZE == fileSize2) {
    1.40 +    return FALSE;
    1.41 +  }
    1.42 +
    1.43 +  if (fileSize1 != fileSize2) {
    1.44 +    // sameContent is already set to FALSE
    1.45 +    return TRUE;
    1.46 +  }
    1.47 +
    1.48 +  char buf1[COMPARE_BLOCKSIZE];
    1.49 +  char buf2[COMPARE_BLOCKSIZE];
    1.50 +  DWORD numBlocks = fileSize1 / COMPARE_BLOCKSIZE;
    1.51 +  DWORD leftOver = fileSize1 % COMPARE_BLOCKSIZE;
    1.52 +  DWORD readAmount;
    1.53 +  for (DWORD i = 0; i < numBlocks; i++) {
    1.54 +    if (!ReadFile(file1, buf1, COMPARE_BLOCKSIZE, &readAmount, nullptr) ||
    1.55 +        readAmount != COMPARE_BLOCKSIZE) {
    1.56 +      return FALSE;
    1.57 +    }
    1.58 +
    1.59 +    if (!ReadFile(file2, buf2, COMPARE_BLOCKSIZE, &readAmount, nullptr) ||
    1.60 +        readAmount != COMPARE_BLOCKSIZE) {
    1.61 +      return FALSE;
    1.62 +    }
    1.63 +
    1.64 +    if (memcmp(buf1, buf2, COMPARE_BLOCKSIZE)) {
    1.65 +      // sameContent is already set to FALSE
    1.66 +      return TRUE;
    1.67 +    }
    1.68 +  }
    1.69 +
    1.70 +  if (leftOver) {
    1.71 +    if (!ReadFile(file1, buf1, leftOver, &readAmount, nullptr) ||
    1.72 +        readAmount != leftOver) {
    1.73 +      return FALSE;
    1.74 +    }
    1.75 +
    1.76 +    if (!ReadFile(file2, buf2, leftOver, &readAmount, nullptr) ||
    1.77 +        readAmount != leftOver) {
    1.78 +      return FALSE;
    1.79 +    }
    1.80 +
    1.81 +    if (memcmp(buf1, buf2, leftOver)) {
    1.82 +      // sameContent is already set to FALSE
    1.83 +      return TRUE;
    1.84 +    }
    1.85 +  }
    1.86 +
    1.87 +  sameContent = TRUE;
    1.88 +  return TRUE;
    1.89 +}

mercurial