diff options
author | Daniel Wilhelm <daniel@wili.li> | 2014-04-18 17:11:56 +0200 |
---|---|---|
committer | Daniel Wilhelm <daniel@wili.li> | 2014-04-18 17:11:56 +0200 |
commit | 98ecf620f7de377dc8ae9ad7fbd1e3b24477e138 (patch) | |
tree | faadc6d8822c20cd3bc6f50b2a98e6c580585949 /shared/file_io.cpp | |
parent | 3.16 (diff) | |
download | FreeFileSync-98ecf620f7de377dc8ae9ad7fbd1e3b24477e138.tar.gz FreeFileSync-98ecf620f7de377dc8ae9ad7fbd1e3b24477e138.tar.bz2 FreeFileSync-98ecf620f7de377dc8ae9ad7fbd1e3b24477e138.zip |
3.17
Diffstat (limited to 'shared/file_io.cpp')
-rw-r--r-- | shared/file_io.cpp | 93 |
1 files changed, 47 insertions, 46 deletions
diff --git a/shared/file_io.cpp b/shared/file_io.cpp index 0afe17dd..51f81da9 100644 --- a/shared/file_io.cpp +++ b/shared/file_io.cpp @@ -6,7 +6,7 @@ // #include "file_io.h" #include "string_conv.h" -#include "system_func.h" +#include "last_error.h" #include "i18n.h" #ifdef FFS_WIN @@ -15,25 +15,21 @@ #include <cerrno> #endif -using namespace ffs3; +using namespace zen; FileInput::FileInput(FileHandle handle, const Zstring& filename) : -#ifdef FFS_WIN eofReached(false), -#endif fileHandle(handle), filename_(filename) {} FileInput::FileInput(const Zstring& filename) : //throw (FileError, ErrorNotExisting) -#ifdef FFS_WIN eofReached(false), -#endif filename_(filename) { #ifdef FFS_WIN - fileHandle = ::CreateFile(ffs3::applyLongPathPrefix(filename).c_str(), + fileHandle = ::CreateFile(zen::applyLongPathPrefix(filename).c_str(), GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, //all shared modes are required to read files that are open in other applications NULL, @@ -67,7 +63,10 @@ FileInput::FileInput(const Zstring& filename) : //throw (FileError, ErrorNotExi if (fileHandle == INVALID_HANDLE_VALUE) { const DWORD lastError = ::GetLastError(); - const wxString& errorMessage = wxString(_("Error opening file:")) + wxT("\n\"") + zToWx(filename_) + wxT("\"") + wxT("\n\n") + ffs3::getLastErrorFormatted(lastError); + + wxString errorMessage = wxString(_("Error opening file:")) + wxT("\n\"") + zToWx(filename_) + wxT("\""); + errorMessage += wxT("\n\n") + zen::getLastErrorFormatted(lastError); + if (lastError == ERROR_FILE_NOT_FOUND || lastError == ERROR_PATH_NOT_FOUND) throw ErrorNotExisting(errorMessage); @@ -79,7 +78,10 @@ FileInput::FileInput(const Zstring& filename) : //throw (FileError, ErrorNotExi if (fileHandle == NULL) { const int lastError = errno; - const wxString& errorMessage = wxString(_("Error opening file:")) + wxT("\n\"") + zToWx(filename_) + wxT("\"") + wxT("\n\n") + ffs3::getLastErrorFormatted(lastError); + + wxString errorMessage = wxString(_("Error opening file:")) + wxT("\n\"") + zToWx(filename_) + wxT("\""); + errorMessage += wxT("\n\n") + zen::getLastErrorFormatted(lastError); + if (lastError == ENOENT) throw ErrorNotExisting(errorMessage); @@ -103,41 +105,40 @@ size_t FileInput::read(void* buffer, size_t bytesToRead) //returns actual number { #ifdef FFS_WIN DWORD bytesRead = 0; - if (!::ReadFile(fileHandle, //__in HANDLE hFile, buffer, //__out LPVOID lpBuffer, static_cast<DWORD>(bytesToRead), //__in DWORD nNumberOfBytesToRead, &bytesRead, //__out_opt LPDWORD lpNumberOfBytesRead, - NULL)) //__inout_opt LPOVERLAPPED lpOverlapped - throw FileError(wxString(_("Error reading file:")) + wxT("\n\"") + zToWx(filename_) + wxT("\"") + - wxT("\n\n") + ffs3::getLastErrorFormatted()); - - if (bytesRead > bytesToRead) - throw FileError(wxString(_("Error reading file:")) + wxT("\n\"") + zToWx(filename_) + wxT("\"") + - wxT("\n\n") + wxT("buffer overflow")); - - if (bytesRead < bytesToRead) - eofReached = bytesRead < bytesToRead; - - return bytesRead; - + NULL)) //__inout_opt LPOVERLAPPED lpOverlapped #elif defined FFS_LINUX const size_t bytesRead = ::fread(buffer, 1, bytesToRead, fileHandle); if (::ferror(fileHandle) != 0) - throw FileError(wxString(_("Error reading file:")) + wxT("\n\"") + zToWx(filename_) + wxT("\"") + - wxT("\n\n") + ffs3::getLastErrorFormatted()); - return bytesRead; #endif + { + wxString errorMessage = wxString(_("Error reading file:")) + wxT("\n\"") + zToWx(filename_) + wxT("\""); + throw FileError(errorMessage + wxT("\n\n") + zen::getLastErrorFormatted()); + } + +#ifdef FFS_WIN + if (bytesRead < bytesToRead) //falsify only! +#elif defined FFS_LINUX + if (::feof(fileHandle) != 0) +#endif + eofReached = true; + + if (bytesRead > bytesToRead) + { + wxString errorMessage = wxString(_("Error reading file:")) + wxT("\n\"") + zToWx(filename_) + wxT("\""); + throw FileError(errorMessage + wxT("\n\n") + wxT("buffer overflow")); + } + + return bytesRead; } bool FileInput::eof() //end of file reached { -#ifdef FFS_WIN return eofReached; -#elif defined FFS_LINUX - return ::feof(fileHandle) != 0; -#endif } @@ -148,7 +149,7 @@ FileOutput::FileOutput(const Zstring& filename, AccessFlag access) : //throw (Fi filename_(filename) { #ifdef FFS_WIN - fileHandle = ::CreateFile(ffs3::applyLongPathPrefix(filename).c_str(), + fileHandle = ::CreateFile(zen::applyLongPathPrefix(filename).c_str(), GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, //note: FILE_SHARE_DELETE is required to rename file while handle is open! NULL, @@ -158,8 +159,8 @@ FileOutput::FileOutput(const Zstring& filename, AccessFlag access) : //throw (Fi if (fileHandle == INVALID_HANDLE_VALUE) { const DWORD lastError = ::GetLastError(); - const wxString& errorMessage = wxString(_("Error writing file:")) + wxT("\n\"") + zToWx(filename_) + wxT("\"") + - wxT("\n\n") + ffs3::getLastErrorFormatted(lastError); + wxString errorMessage = wxString(_("Error writing file:")) + wxT("\n\"") + zToWx(filename_) + wxT("\""); + errorMessage += wxT("\n\n") + zen::getLastErrorFormatted(lastError); if (lastError == ERROR_FILE_EXISTS) throw ErrorTargetExisting(errorMessage); @@ -177,8 +178,8 @@ FileOutput::FileOutput(const Zstring& filename, AccessFlag access) : //throw (Fi if (fileHandle == NULL) { const int lastError = errno; - const wxString& errorMessage = wxString(_("Error writing file:")) + wxT("\n\"") + zToWx(filename_) + wxT("\"") + - wxT("\n\n") + ffs3::getLastErrorFormatted(lastError); + wxString errorMessage = wxString(_("Error writing file:")) + wxT("\n\"") + zToWx(filename_) + wxT("\""); + errorMessage += wxT("\n\n") + zen::getLastErrorFormatted(lastError); if (lastError == EEXIST) throw ErrorTargetExisting(errorMessage); @@ -205,23 +206,23 @@ void FileOutput::write(const void* buffer, size_t bytesToWrite) //throw (FileErr { #ifdef FFS_WIN DWORD bytesWritten = 0; - if (!::WriteFile(fileHandle, //__in HANDLE hFile, buffer, //__out LPVOID lpBuffer, static_cast<DWORD>(bytesToWrite), //__in DWORD nNumberOfBytesToWrite, &bytesWritten, //__out_opt LPDWORD lpNumberOfBytesWritten, NULL)) //__inout_opt LPOVERLAPPED lpOverlapped - throw FileError(wxString(_("Error writing file:")) + wxT("\n\"") + zToWx(filename_) + wxT("\"") + - wxT("\n\n") + ffs3::getLastErrorFormatted() + wxT(" (w)")); //w -> distinguish from fopen error message! - - if (bytesWritten != bytesToWrite) //must be fulfilled for synchronous writes! - throw FileError(wxString(_("Error writing file:")) + wxT("\n\"") + zToWx(filename_) + wxT("\"") + - wxT("\n\n") + wxT("incomplete write")); - #elif defined FFS_LINUX const size_t bytesWritten = ::fwrite(buffer, 1, bytesToWrite, fileHandle); - if (::ferror(fileHandle) != 0 || bytesWritten != bytesToWrite) - throw FileError(wxString(_("Error writing file:")) + wxT("\n\"") + zToWx(filename_) + wxT("\"") + - wxT("\n\n") + ffs3::getLastErrorFormatted() + wxT(" (w)")); //w -> distinguish from fopen error message! + if (::ferror(fileHandle) != 0) #endif + { + wxString errorMessage = wxString(_("Error writing file:")) + wxT("\n\"") + zToWx(filename_) + wxT("\""); + throw FileError(errorMessage + wxT("\n\n") + zen::getLastErrorFormatted() + wxT(" (w)")); //w -> distinguish from fopen error message! + } + + if (bytesWritten != bytesToWrite) //must be fulfilled for synchronous writes! + { + wxString errorMessage = wxString(_("Error writing file:")) + wxT("\n\"") + zToWx(filename_) + wxT("\""); + throw FileError(errorMessage + wxT("\n\n") + wxT("incomplete write")); + } } |