diff options
Diffstat (limited to 'zen')
-rw-r--r-- | zen/crc.h | 48 | ||||
-rw-r--r-- | zen/file_access.cpp | 52 | ||||
-rw-r--r-- | zen/file_access.h | 6 | ||||
-rw-r--r-- | zen/i18n.h | 7 | ||||
-rw-r--r-- | zen/serialize.h | 2 | ||||
-rw-r--r-- | zen/shell_execute.h | 9 | ||||
-rw-r--r-- | zen/symlink_target.h | 2 |
7 files changed, 100 insertions, 26 deletions
diff --git a/zen/crc.h b/zen/crc.h new file mode 100644 index 00000000..0b22c7c3 --- /dev/null +++ b/zen/crc.h @@ -0,0 +1,48 @@ +// ************************************************************************** +// * This file is part of the FreeFileSync project. It is distributed under * +// * GNU General Public License: http://www.gnu.org/licenses/gpl-3.0 * +// * Copyright (C) Zenju (zenju AT gmx DOT de) - All Rights Reserved * +// ************************************************************************** + +#ifndef CRC_H_23489275827847235 +#define CRC_H_23489275827847235 + +#ifdef _MSC_VER + #pragma warning(push) + #pragma warning(disable: 4245) //'conversion' : conversion from 'int' to 'const boost::detail::mask_uint_t<0x08>::least', signed/unsigned mismatch +#endif +#include <boost/crc.hpp> +#ifdef _MSC_VER + #pragma warning(pop) +#endif + + +namespace zen +{ +template <class ByteIterator> inline +uint16_t getCrc16(ByteIterator first, ByteIterator last) +{ + static_assert(sizeof(typename std::iterator_traits<ByteIterator>::value_type) == 1, ""); + boost::crc_16_type result; + if (first != last) + result.process_bytes(&*first, last - first); + auto rv = result.checksum(); + static_assert(sizeof(rv) == sizeof(uint16_t), ""); + return rv; +} + + +template <class ByteIterator> inline +uint32_t getCrc32(ByteIterator first, ByteIterator last) +{ + static_assert(sizeof(typename std::iterator_traits<ByteIterator>::value_type) == 1, ""); + boost::crc_32_type result; + if (first != last) + result.process_bytes(&*first, last - first); + auto rv = result.checksum(); + static_assert(sizeof(rv) == sizeof(uint32_t), ""); + return rv; +} +} + +#endif //CRC_H_23489275827847235 diff --git a/zen/file_access.cpp b/zen/file_access.cpp index 11f61291..c2072a26 100644 --- a/zen/file_access.cpp +++ b/zen/file_access.cpp @@ -309,6 +309,28 @@ VolumeId zen::getVolumeId(const Zstring& itemPath) //throw FileError } +Zstring zen::getTempFolderPath() //throw FileError +{ +#ifdef ZEN_WIN + const DWORD bufSize = MAX_PATH + 1; //MSDN: maximum value + std::vector<wchar_t> buf(bufSize); + const DWORD rv = ::GetTempPath(bufSize, //_In_ DWORD nBufferLength, + &buf[0]); //_Out_ LPTSTR lpBuffer + if (rv == 0) + THROW_LAST_FILE_ERROR(_("Cannot get process information."), L"GetTempPath"); + if (rv >= bufSize) + throw FileError(_("Cannot get process information."), L"GetTempPath: insufficient buffer size."); + return &buf[0]; + +#elif defined ZEN_LINUX || defined ZEN_MAC + const char* buf = ::getenv("TMPDIR"); //no extended error reporting + if (!buf) + throw FileError(_("Cannot get process information."), L"getenv: TMPDIR not found."); + return buf; +#endif +} + + bool zen::removeFile(const Zstring& filePath) //throw FileError { #ifdef ZEN_WIN @@ -738,9 +760,9 @@ void setFileTimeByHandle(HANDLE hFile, //throw FileError //add more meaningful message: FAT accepts only a subset of the NTFS date range #ifdef ZEN_WIN_VISTA_AND_LATER - if (ec == ERROR_INVALID_PARAMETER && isFatDrive(hFile)) + if (ec == ERROR_INVALID_PARAMETER && isFatDrive(hFile)) #else - if (ec == ERROR_INVALID_PARAMETER && isFatDrive(filePath)) + if (ec == ERROR_INVALID_PARAMETER && isFatDrive(filePath)) #endif { //let's not fail due to an invalid creation time on FAT: http://www.freefilesync.org/forum/viewtopic.php?t=2278 @@ -961,7 +983,7 @@ void setWriteTimeNative(const Zstring& filePath, std::int64_t modTime, ProcSymli //=> using open()/futimens() for regular files and utimensat(AT_SYMLINK_NOFOLLOW) for symlinks is consistent with "cp" and "touch"! if (procSl == ProcSymlink::FOLLOW) { - const int fdFile = ::open(filePath.c_str(), O_WRONLY, 0); //"if O_CREAT is not specified, then mode is ignored" + const int fdFile = ::open(filePath.c_str(), O_WRONLY); if (fdFile == -1) { if (errno == EACCES) //bullshit, access denied even with 0777 permissions! => utimes should work! @@ -1099,16 +1121,16 @@ void zen::setFileTime(const Zstring& filePath, std::int64_t modTime, ProcSymlink } -bool zen::supportsPermissions(const Zstring& dirpath) //throw FileError +bool zen::supportsPermissions(const Zstring& dirPath) //throw FileError { #ifdef ZEN_WIN const DWORD bufferSize = MAX_PATH + 1; std::vector<wchar_t> buffer(bufferSize); - if (!::GetVolumePathName(dirpath.c_str(), //__in LPCTSTR lpszFileName, + if (!::GetVolumePathName(dirPath.c_str(), //__in LPCTSTR lpszFileName, &buffer[0], //__out LPTSTR lpszVolumePathName, bufferSize)) //__in DWORD cchBufferLength - THROW_LAST_FILE_ERROR(replaceCpy(_("Cannot read file attributes of %x."), L"%x", fmtPath(dirpath)), L"GetVolumePathName"); + THROW_LAST_FILE_ERROR(replaceCpy(_("Cannot read file attributes of %x."), L"%x", fmtPath(dirPath)), L"GetVolumePathName"); const Zstring volumePath = appendSeparator(&buffer[0]); @@ -1121,7 +1143,7 @@ bool zen::supportsPermissions(const Zstring& dirpath) //throw FileError &fsFlags, //__out_opt LPDWORD lpFileSystemFlags, nullptr, //__out LPTSTR lpFileSystemNameBuffer, 0)) //__in DWORD nFileSystemNameSize - THROW_LAST_FILE_ERROR(replaceCpy(_("Cannot read file attributes of %x."), L"%x", fmtPath(dirpath)), L"GetVolumeInformation"); + THROW_LAST_FILE_ERROR(replaceCpy(_("Cannot read file attributes of %x."), L"%x", fmtPath(dirPath)), L"GetVolumeInformation"); return (fsFlags & FILE_PERSISTENT_ACLS) != 0; @@ -1422,12 +1444,12 @@ void makeDirectoryRecursivelyImpl(const Zstring& directory) //FileError } -void zen::makeDirectoryRecursively(const Zstring& dirpath) //throw FileError +void zen::makeDirectoryRecursively(const Zstring& dirPath) //throw FileError { //remove trailing separator (even for C:\ root directories) - const Zstring dirFormatted = endsWith(dirpath, FILE_NAME_SEPARATOR) ? - beforeLast(dirpath, FILE_NAME_SEPARATOR, IF_MISSING_RETURN_NONE) : - dirpath; + const Zstring dirFormatted = endsWith(dirPath, FILE_NAME_SEPARATOR) ? + beforeLast(dirPath, FILE_NAME_SEPARATOR, IF_MISSING_RETURN_NONE) : + dirPath; makeDirectoryRecursivelyImpl(dirFormatted); //FileError } @@ -1727,10 +1749,10 @@ bool canCopyAsSparse(DWORD fileAttrSource, Function getTargetFsFlags) //throw () DWORD targetFsFlags = 0; if (!getTargetFsFlags(targetFsFlags)) - { - assert(false); - return false; - } + { + assert(false); + return false; + } assert(targetFsFlags != 0); return (targetFsFlags & FILE_SUPPORTS_SPARSE_FILES) != 0; diff --git a/zen/file_access.h b/zen/file_access.h index 9c1b37ef..fb99ce33 100644 --- a/zen/file_access.h +++ b/zen/file_access.h @@ -34,6 +34,8 @@ void setFileTime(const Zstring& filePath, std::int64_t modificationTime, ProcSym std::uint64_t getFilesize(const Zstring& filePath); //throw FileError std::uint64_t getFreeDiskSpace(const Zstring& path); //throw FileError, returns 0 if not available VolumeId getVolumeId(const Zstring& itemPath); //throw FileError +//get per-user directory designated for temporary files: +Zstring getTempFolderPath(); //throw FileError bool removeFile(const Zstring& filePath); //throw FileError; return "false" if file is not existing @@ -44,11 +46,11 @@ void removeDirectoryRecursively(const Zstring& dirPath); //throw FileError //rename file or directory: no copying!!! void renameFile(const Zstring& itemPathOld, const Zstring& itemPathNew); //throw FileError, ErrorDifferentVolume, ErrorTargetExisting -bool supportsPermissions(const Zstring& dirpath); //throw FileError, dereferences symlinks +bool supportsPermissions(const Zstring& dirPath); //throw FileError, dereferences symlinks //- no error if already existing //- create recursively if parent directory is not existing -void makeDirectoryRecursively(const Zstring& dirpath); //throw FileError +void makeDirectoryRecursively(const Zstring& dirPath); //throw FileError //fail if already existing or parent directory not existing: //source path is optional (may be empty) @@ -115,8 +115,9 @@ struct CleanUpTranslationHandler { std::shared_ptr<const TranslationHandler>*& handler = getTranslationInstance(); assert(!handler); //clean up at a better time rather than during static destruction! MT issues! - delete handler; + auto oldHandler = handler; handler = nullptr; //getTranslator() may be called even after static objects of this translation unit are destroyed! + delete oldHandler; } }; } @@ -132,9 +133,9 @@ void setTranslator(std::unique_ptr<const TranslationHandler>&& newHandler) static implementation::CleanUpTranslationHandler cuth; //external linkage even in header! std::shared_ptr<const TranslationHandler>*& handler = implementation::getTranslationInstance(); - auto tmp = handler; + auto oldHandler = handler; handler = nullptr; - delete tmp; + delete oldHandler; if (newHandler) handler = new std::shared_ptr<const TranslationHandler>(std::move(newHandler)); } diff --git a/zen/serialize.h b/zen/serialize.h index d7b4cd22..aee650e6 100644 --- a/zen/serialize.h +++ b/zen/serialize.h @@ -268,7 +268,7 @@ void writeArray(BufferedOutputStream& stream, const void* data, size_t len) template <class N, class BufferedOutputStream> inline void writeNumber(BufferedOutputStream& stream, const N& num) { - static_assert(IsArithmetic<N>::value || IsSameType<N, bool>::value, ""); + static_assert(IsArithmetic<N>::value || IsSameType<N, bool>::value, "not a number!"); writeArray(stream, &num, sizeof(N)); } diff --git a/zen/shell_execute.h b/zen/shell_execute.h index ce34f067..813de479 100644 --- a/zen/shell_execute.h +++ b/zen/shell_execute.h @@ -82,6 +82,7 @@ void shellExecute(const Zstring& command, ExecutionType type) //throw FileError trim(commandTmp, true, false); //CommandLineToArgvW() does not like leading spaces std::vector<Zstring> argv; + if (!commandTmp.empty()) //::CommandLineToArgvW returns the path to the current executable if empty string is passed { int argc = 0; LPWSTR* tmp = ::CommandLineToArgvW(commandTmp.c_str(), &argc); @@ -91,11 +92,11 @@ void shellExecute(const Zstring& command, ExecutionType type) //throw FileError std::copy(tmp, tmp + argc, std::back_inserter(argv)); } - Zstring filepath; + Zstring filePath; Zstring arguments; if (!argv.empty()) { - filepath = argv[0]; + filePath = argv[0]; for (auto it = argv.begin() + 1; it != argv.end(); ++it) arguments += (it != argv.begin() ? L" " : L"") + (it->empty() || std::any_of(it->begin(), it->end(), &isWhiteSpace<wchar_t>) ? L"\"" + *it + L"\"" : *it); @@ -103,12 +104,12 @@ void shellExecute(const Zstring& command, ExecutionType type) //throw FileError auto fillExecInfo = [&](SHELLEXECUTEINFO& execInfo) { - execInfo.lpFile = filepath.c_str(); + execInfo.lpFile = filePath.c_str(); execInfo.lpParameters = arguments.c_str(); }; if (!shellExecuteImpl(fillExecInfo, type)) - THROW_LAST_FILE_ERROR(_("Incorrect command line:") + L"\nFile: " + fmtPath(filepath) + L"\nArg: " + arguments.c_str(), L"ShellExecuteEx"); + THROW_LAST_FILE_ERROR(_("Incorrect command line:") + L"\nFile: " + fmtPath(filePath) + L"\nArg: " + arguments.c_str(), L"ShellExecuteEx"); #elif defined ZEN_LINUX || defined ZEN_MAC /* diff --git a/zen/symlink_target.h b/zen/symlink_target.h index a1f48884..17168a03 100644 --- a/zen/symlink_target.h +++ b/zen/symlink_target.h @@ -187,7 +187,7 @@ Zstring getResolvedSymlinkPath_impl(const Zstring& linkPath) //throw FileError if (charsWritten == 0) THROW_LAST_FILE_ERROR(replaceCpy(_("Cannot determine final path for %x."), L"%x", fmtPath(linkPath)), L"GetFinalPathNameByHandle"); if (charsWritten >= bufferSize) - throw FileError(replaceCpy(_("Cannot determine final path for %x."), L"%x", fmtPath(linkPath)), L"GetFinalPathNameByHandle: buffer overflow."); + throw FileError(replaceCpy(_("Cannot determine final path for %x."), L"%x", fmtPath(linkPath)), L"GetFinalPathNameByHandle: insufficient buffer size."); return removeLongPathPrefix(Zstring(&targetPath[0], charsWritten)); //MSDN: GetFinalPathNameByHandle() always prepends "\\?\" |