From 7302bb4484d517a72cdffbd13ec7a9f2324cde01 Mon Sep 17 00:00:00 2001 From: Daniel Wilhelm Date: Sat, 29 Oct 2016 11:41:53 +0200 Subject: 8.6 --- zen/file_io.cpp | 58 ++++++++++++++++++++++++++++----------------------------- 1 file changed, 29 insertions(+), 29 deletions(-) (limited to 'zen/file_io.cpp') diff --git a/zen/file_io.cpp b/zen/file_io.cpp index 6243980b..440a5d0d 100644 --- a/zen/file_io.cpp +++ b/zen/file_io.cpp @@ -26,13 +26,13 @@ using namespace zen; namespace { #if defined ZEN_LINUX || defined ZEN_MAC -//- "filepath" could be a named pipe which *blocks* forever for open()! +//- "filePath" could be a named pipe which *blocks* forever for open()! //- open() with O_NONBLOCK avoids the block, but opens successfully //- create sample pipe: "sudo mkfifo named_pipe" -void checkForUnsupportedType(const Zstring& filepath) //throw FileError +void checkForUnsupportedType(const Zstring& filePath) //throw FileError { struct ::stat fileInfo = {}; - if (::stat(filepath.c_str(), &fileInfo) != 0) //follows symlinks + if (::stat(filePath.c_str(), &fileInfo) != 0) //follows symlinks return; //let the caller handle errors like "not existing" if (!S_ISREG(fileInfo.st_mode) && @@ -49,7 +49,7 @@ void checkForUnsupportedType(const Zstring& filepath) //throw FileError const std::wstring numFmt = printNumber(L"0%06o", m & S_IFMT); return name ? numFmt + L", " + name : numFmt; }; - throw FileError(replaceCpy(_("Type of item %x is not supported:"), L"%x", fmtPath(filepath)) + L" " + getTypeName(fileInfo.st_mode)); + throw FileError(replaceCpy(_("Type of item %x is not supported:"), L"%x", fmtPath(filePath)) + L" " + getTypeName(fileInfo.st_mode)); } } #endif @@ -66,11 +66,11 @@ FileHandle getInvalidHandle() } -FileInput::FileInput(FileHandle handle, const Zstring& filepath) : FileBase(filepath), fileHandle(handle) {} +FileInput::FileInput(FileHandle handle, const Zstring& filePath) : FileBase(filePath), fileHandle(handle) {} -FileInput::FileInput(const Zstring& filepath) : //throw FileError, ErrorFileLocked - FileBase(filepath), fileHandle(getInvalidHandle()) +FileInput::FileInput(const Zstring& filePath) : //throw FileError, ErrorFileLocked + FileBase(filePath), fileHandle(getInvalidHandle()) { #ifdef ZEN_WIN try { activatePrivilege(PrivilegeName::BACKUP); } @@ -78,12 +78,12 @@ FileInput::FileInput(const Zstring& filepath) : //throw FileError, ErrorFileLock auto createHandle = [&](DWORD dwShareMode) { - return ::CreateFile(applyLongPathPrefix(filepath).c_str(), //_In_ LPCTSTR lpFileName, - GENERIC_READ, //_In_ DWORD dwDesiredAccess, - dwShareMode, //_In_ DWORD dwShareMode, - nullptr, //_In_opt_ LPSECURITY_ATTRIBUTES lpSecurityAttributes, - OPEN_EXISTING, //_In_ DWORD dwCreationDisposition, - FILE_FLAG_SEQUENTIAL_SCAN | //_In_ DWORD dwFlagsAndAttributes, + return ::CreateFile(applyLongPathPrefix(filePath).c_str(), //_In_ LPCTSTR lpFileName, + GENERIC_READ, //_In_ DWORD dwDesiredAccess, + dwShareMode, //_In_ DWORD dwShareMode, + nullptr, //_In_opt_ LPSECURITY_ATTRIBUTES lpSecurityAttributes, + OPEN_EXISTING, //_In_ DWORD dwCreationDisposition, + FILE_FLAG_SEQUENTIAL_SCAN | //_In_ DWORD dwFlagsAndAttributes, /* possible values: (Reference https://msdn.microsoft.com/en-us/library/aa363858#caching_behavior) FILE_FLAG_NO_BUFFERING FILE_FLAG_RANDOM_ACCESS @@ -122,14 +122,14 @@ FileInput::FileInput(const Zstring& filepath) : //throw FileError, ErrorFileLock if (fileHandle == INVALID_HANDLE_VALUE) { const DWORD ec = ::GetLastError(); //copy before directly/indirectly making other system calls! - const std::wstring errorMsg = replaceCpy(_("Cannot open file %x."), L"%x", fmtPath(filepath)); + const std::wstring errorMsg = replaceCpy(_("Cannot open file %x."), L"%x", fmtPath(filePath)); std::wstring errorDescr = formatSystemError(L"CreateFile", ec); if (ec == ERROR_SHARING_VIOLATION || //-> enhance error message! ec == ERROR_LOCK_VIOLATION) { #ifdef ZEN_WIN_VISTA_AND_LATER //(try to) enhance error message - const std::wstring procList = vista::getLockingProcesses(filepath); //noexcept + const std::wstring procList = vista::getLockingProcesses(filePath); //noexcept if (!procList.empty()) errorDescr = _("The file is locked by another process:") + L"\n" + procList; #endif @@ -140,12 +140,12 @@ FileInput::FileInput(const Zstring& filepath) : //throw FileError, ErrorFileLock } #elif defined ZEN_LINUX || defined ZEN_MAC - checkForUnsupportedType(filepath); //throw FileError; opening a named pipe would block forever! + checkForUnsupportedType(filePath); //throw FileError; opening a named pipe would block forever! //don't use O_DIRECT: http://yarchive.net/comp/linux/o_direct.html - fileHandle = ::open(filepath.c_str(), O_RDONLY); + fileHandle = ::open(filePath.c_str(), O_RDONLY); if (fileHandle == -1) //don't check "< 0" -> docu seems to allow "-2" to be a valid file handle - THROW_LAST_FILE_ERROR(replaceCpy(_("Cannot open file %x."), L"%x", fmtPath(filepath)), L"open"); + THROW_LAST_FILE_ERROR(replaceCpy(_("Cannot open file %x."), L"%x", fmtPath(filePath)), L"open"); #endif //------------------------------------------------------------------------------------------------------ @@ -160,7 +160,7 @@ FileInput::FileInput(const Zstring& filepath) : //throw FileError, ErrorFileLock #ifdef ZEN_LINUX //handle still un-owned => need constructor guard //optimize read-ahead on input file: if (::posix_fadvise(fileHandle, 0, 0, POSIX_FADV_SEQUENTIAL) != 0) - THROW_LAST_FILE_ERROR(replaceCpy(_("Cannot read file %x."), L"%x", fmtPath(filepath)), L"posix_fadvise"); + THROW_LAST_FILE_ERROR(replaceCpy(_("Cannot read file %x."), L"%x", fmtPath(filePath)), L"posix_fadvise"); #elif defined ZEN_MAC //"dtruss" doesn't show use of "fcntl() F_RDAHEAD/F_RDADVISE" for "cp") @@ -215,11 +215,11 @@ size_t FileInput::tryRead(void* buffer, size_t bytesToRead) //throw FileError; m //---------------------------------------------------------------------------------------------------- -FileOutput::FileOutput(FileHandle handle, const Zstring& filepath) : FileBase(filepath), fileHandle(handle) {} +FileOutput::FileOutput(FileHandle handle, const Zstring& filePath) : FileBase(filePath), fileHandle(handle) {} -FileOutput::FileOutput(const Zstring& filepath, AccessFlag access) : //throw FileError, ErrorTargetExisting - FileBase(filepath), fileHandle(getInvalidHandle()) +FileOutput::FileOutput(const Zstring& filePath, AccessFlag access) : //throw FileError, ErrorTargetExisting + FileBase(filePath), fileHandle(getInvalidHandle()) { #ifdef ZEN_WIN try { activatePrivilege(PrivilegeName::BACKUP); } @@ -231,7 +231,7 @@ FileOutput::FileOutput(const Zstring& filepath, AccessFlag access) : //throw Fil auto createHandle = [&](DWORD dwFlagsAndAttributes) { - return ::CreateFile(applyLongPathPrefix(filepath).c_str(), //_In_ LPCTSTR lpFileName, + return ::CreateFile(applyLongPathPrefix(filePath).c_str(), //_In_ LPCTSTR lpFileName, GENERIC_READ | GENERIC_WRITE, //_In_ DWORD dwDesiredAccess, /* https://msdn.microsoft.com/en-us/library/windows/desktop/aa363858#files quote: When an application creates a file across a network, it is better @@ -256,7 +256,7 @@ FileOutput::FileOutput(const Zstring& filepath, AccessFlag access) : //throw Fil //CREATE_ALWAYS fails with ERROR_ACCESS_DENIED if the existing file is hidden or "system": https://msdn.microsoft.com/en-us/library/windows/desktop/aa363858#files if (ec == ERROR_ACCESS_DENIED && dwCreationDisposition == CREATE_ALWAYS) { - const DWORD attrib = ::GetFileAttributes(applyLongPathPrefix(filepath).c_str()); + const DWORD attrib = ::GetFileAttributes(applyLongPathPrefix(filePath).c_str()); if (attrib != INVALID_FILE_ATTRIBUTES) { fileHandle = createHandle(attrib); //retry: alas this may still fail for hidden file, e.g. accessing shared folder in XP as Virtual Box guest! @@ -267,14 +267,14 @@ FileOutput::FileOutput(const Zstring& filepath, AccessFlag access) : //throw Fil //begin of "regular" error reporting if (fileHandle == INVALID_HANDLE_VALUE) { - const std::wstring errorMsg = replaceCpy(_("Cannot write file %x."), L"%x", fmtPath(filepath)); + const std::wstring errorMsg = replaceCpy(_("Cannot write file %x."), L"%x", fmtPath(filePath)); std::wstring errorDescr = formatSystemError(L"CreateFile", ec); #ifdef ZEN_WIN_VISTA_AND_LATER //(try to) enhance error message if (ec == ERROR_SHARING_VIOLATION || //-> enhance error message! ec == ERROR_LOCK_VIOLATION) { - const std::wstring procList = vista::getLockingProcesses(filepath); //noexcept + const std::wstring procList = vista::getLockingProcesses(filePath); //noexcept if (!procList.empty()) errorDescr = _("The file is locked by another process:") + L"\n" + procList; } @@ -289,14 +289,14 @@ FileOutput::FileOutput(const Zstring& filepath, AccessFlag access) : //throw Fil } #elif defined ZEN_LINUX || defined ZEN_MAC - //checkForUnsupportedType(filepath); -> not needed, open() + O_WRONLY should fail fast + //checkForUnsupportedType(filePath); -> not needed, open() + O_WRONLY should fail fast - fileHandle = ::open(filepath.c_str(), O_WRONLY | O_CREAT | (access == FileOutput::ACC_CREATE_NEW ? O_EXCL : O_TRUNC), + fileHandle = ::open(filePath.c_str(), O_WRONLY | O_CREAT | (access == FileOutput::ACC_CREATE_NEW ? O_EXCL : O_TRUNC), S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); //0666 if (fileHandle == -1) { const int ec = errno; //copy before making other system calls! - const std::wstring errorMsg = replaceCpy(_("Cannot write file %x."), L"%x", fmtPath(filepath)); + const std::wstring errorMsg = replaceCpy(_("Cannot write file %x."), L"%x", fmtPath(filePath)); const std::wstring errorDescr = formatSystemError(L"open", ec); if (ec == EEXIST) -- cgit