diff options
author | Daniel Wilhelm <daniel@wili.li> | 2014-04-18 17:05:53 +0200 |
---|---|---|
committer | Daniel Wilhelm <daniel@wili.li> | 2014-04-18 17:05:53 +0200 |
commit | 618dfb51d93898632830f1b87443d3f748780871 (patch) | |
tree | bac520a2e261154f8d35b0cb8aa345f5ab373811 /library/binary.cpp | |
parent | 3.4 (diff) | |
download | FreeFileSync-618dfb51d93898632830f1b87443d3f748780871.tar.gz FreeFileSync-618dfb51d93898632830f1b87443d3f748780871.tar.bz2 FreeFileSync-618dfb51d93898632830f1b87443d3f748780871.zip |
3.5
Diffstat (limited to 'library/binary.cpp')
-rw-r--r-- | library/binary.cpp | 99 |
1 files changed, 9 insertions, 90 deletions
diff --git a/library/binary.cpp b/library/binary.cpp index bc5ba814..409b024a 100644 --- a/library/binary.cpp +++ b/library/binary.cpp @@ -6,103 +6,23 @@ // #include "binary.h" #include <boost/scoped_array.hpp> -#include <wx/intl.h> -#include "../shared/stringConv.h" +#include "../shared/fileIO.h" -#ifdef FFS_WIN -#include "../shared/longPathPrefix.h" -#include <wx/msw/wrapwin.h> //includes "windows.h" -#include <boost/shared_ptr.hpp> -#elif defined FFS_LINUX -#include <wx/ffile.h> -#endif - - -bool FreeFileSync::filesHaveSameContent(const Zstring& filename1, const Zstring& filename2, CompareCallback* callback) +bool FreeFileSync::filesHaveSameContent(const Zstring& filename1, const Zstring& filename2, CompareCallback& callback) { const size_t BUFFER_SIZE = 512 * 1024; //512 kb seems to be the perfect buffer size static boost::scoped_array<unsigned char> memory1(new unsigned char[BUFFER_SIZE]); static boost::scoped_array<unsigned char> memory2(new unsigned char[BUFFER_SIZE]); -#ifdef FFS_WIN - const HANDLE hFile1 = ::CreateFile(FreeFileSync::applyLongPathPrefix(filename1).c_str(), - GENERIC_READ, - FILE_SHARE_READ, - NULL, - OPEN_EXISTING, - FILE_FLAG_OPEN_REPARSE_POINT | FILE_FLAG_SEQUENTIAL_SCAN, - NULL); - if (hFile1 == INVALID_HANDLE_VALUE) - throw FileError(wxString(_("Error opening file:")) + wxT(" \"") + zToWx(filename1) + wxT("\"")); - - boost::shared_ptr<void> dummy1(hFile1, &::CloseHandle); - - const HANDLE hFile2 = ::CreateFile(FreeFileSync::applyLongPathPrefix(filename2).c_str(), - GENERIC_READ, - FILE_SHARE_READ, - NULL, - OPEN_EXISTING, - FILE_FLAG_OPEN_REPARSE_POINT | FILE_FLAG_SEQUENTIAL_SCAN, - NULL); - if (hFile2 == INVALID_HANDLE_VALUE) - throw FileError(wxString(_("Error opening file:")) + wxT(" \"") + zToWx(filename2) + wxT("\"")); - - boost::shared_ptr<void> dummy2(hFile2, &::CloseHandle); - - wxLongLong bytesCompared; - DWORD length1 = 0; - do - { - if (!::ReadFile( - hFile1, //__in HANDLE hFile, - memory1.get(), //__out LPVOID lpBuffer, - BUFFER_SIZE, //__in DWORD nNumberOfBytesToRead, - &length1, //__out_opt LPDWORD lpNumberOfBytesRead, - NULL)) //__inout_opt LPOVERLAPPED lpOverlapped - throw FileError(wxString(_("Error reading file:")) + wxT(" \"") + zToWx(filename1) + wxT("\"")); - - DWORD length2 = 0; - if (!::ReadFile( - hFile2, //__in HANDLE hFile, - memory2.get(), //__out LPVOID lpBuffer, - BUFFER_SIZE, //__in DWORD nNumberOfBytesToRead, - &length2, //__out_opt LPDWORD lpNumberOfBytesRead, - NULL)) //__inout_opt LPOVERLAPPED lpOverlapped - throw FileError(wxString(_("Error reading file:")) + wxT(" \"") + zToWx(filename2) + wxT("\"")); - - if (length1 != length2 || ::memcmp(memory1.get(), memory2.get(), length1) != 0) - return false; - - bytesCompared += length1 * 2; - - //send progress updates - callback->updateCompareStatus(bytesCompared); - } - while (length1 == BUFFER_SIZE); - - return true; - - -#elif defined FFS_LINUX - wxFFile file1(::fopen(filename1.c_str(), DefaultStr("rb,type=record,noseek"))); //utilize UTF-8 filename - if (!file1.IsOpened()) - throw FileError(wxString(_("Error opening file:")) + wxT(" \"") + zToWx(filename1) + wxT("\"")); - - wxFFile file2(::fopen(filename2.c_str(), DefaultStr("rb,type=record,noseek"))); //utilize UTF-8 filename - if (!file2.IsOpened()) - throw FileError(wxString(_("Error opening file:")) + wxT(" \"") + zToWx(filename2) + wxT("\"")); + FileInput file1(filename1); //throw FileError() + FileInput file2(filename2); //throw FileError() wxLongLong bytesCompared; do { - const size_t length1 = file1.Read(memory1.get(), BUFFER_SIZE); - if (file1.Error()) - throw FileError(wxString(_("Error reading file:")) + wxT(" \"") + zToWx(filename1) + wxT("\"")); - - const size_t length2 = file2.Read(memory2.get(), BUFFER_SIZE); - if (file2.Error()) - throw FileError(wxString(_("Error reading file:")) + wxT(" \"") + zToWx(filename2) + wxT("\"")); + const size_t length1 = file1.read(memory1.get(), BUFFER_SIZE); //returns actual number of bytes read; throw FileError() + const size_t length2 = file2.read(memory2.get(), BUFFER_SIZE); // if (length1 != length2 || ::memcmp(memory1.get(), memory2.get(), length1) != 0) return false; @@ -110,13 +30,12 @@ bool FreeFileSync::filesHaveSameContent(const Zstring& filename1, const Zstring& bytesCompared += length1 * 2; //send progress updates - callback->updateCompareStatus(bytesCompared); + callback.updateCompareStatus(bytesCompared); } - while (!file1.Eof()); + while (!file1.eof()); - if (!file2.Eof()) //highly unlikely, but theoretically possible! (but then again, not in this context where both files have same size...) + if (!file1.eof()) //highly unlikely, but theoretically possible! (but then again, not in this context where both files have same size...) return false; return true; -#endif } |