From 003312621ff83e6879908ca03e89118eaeb84faf Mon Sep 17 00:00:00 2001 From: Luc Guyot Date: Thu, 21 Nov 2024 16:16:27 +0100 Subject: [PATCH] Fixes Windows build failure --- extensions/windows/cfapi/Common/utilities.cpp | 9 +-- src/libcommon/CMakeLists.txt | 2 +- src/libcommon/utility/utility.cpp | 7 -- src/libcommon/utility/utility.h | 11 ++- src/libcommon/utility/utility_base.h | 75 +++++++++++++++++++ src/libcommon/utility/utility_win.cpp | 30 -------- 6 files changed, 87 insertions(+), 47 deletions(-) create mode 100644 src/libcommon/utility/utility_base.h diff --git a/extensions/windows/cfapi/Common/utilities.cpp b/extensions/windows/cfapi/Common/utilities.cpp index e6f2ab611..3a734fce1 100644 --- a/extensions/windows/cfapi/Common/utilities.cpp +++ b/extensions/windows/cfapi/Common/utilities.cpp @@ -17,8 +17,7 @@ */ #include "utilities.h" - -#include "../../../../src/libcommon/utility/utility.h" +#include "../../../../src/libcommon/utility/utility_base.h" #include @@ -354,7 +353,7 @@ bool Utilities::checkIfIsLink(const wchar_t *path, bool &isSymlink, bool &isJunc HANDLE hFile = CreateFileW(path, FILE_WRITE_ATTRIBUTES, FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OPEN_REPARSE_POINT, NULL); if (hFile == INVALID_HANDLE_VALUE) { - exists = KDC::CommonUtility::isLikeFileNotFoundError(GetLastError()); + exists = KDC::utility_base::isLikeFileNotFoundError(GetLastError()); return !exists; } @@ -370,7 +369,7 @@ bool Utilities::checkIfIsLink(const wchar_t *path, bool &isSymlink, bool &isJunc return true; } - exists = KDC::CommonUtility::isLikeFileNotFoundError(GetLastError()); + exists = KDC::utility_base::isLikeFileNotFoundError(GetLastError()); return !exists; } @@ -388,7 +387,7 @@ bool Utilities::checkIfIsDirectory(const wchar_t *path, bool &isDirectory, bool std::error_code ec; isDirectory = std::filesystem::is_directory(std::filesystem::path(path), ec); if (!isDirectory && ec.value() != 0) { - exists = KDC::CommonUtility::isLikeFileNotFoundError(ec); + exists = KDC::utility_base::isLikeFileNotFoundError(ec); if (!exists) { return true; } diff --git a/src/libcommon/CMakeLists.txt b/src/libcommon/CMakeLists.txt index 7225d27b3..5091d96ba 100644 --- a/src/libcommon/CMakeLists.txt +++ b/src/libcommon/CMakeLists.txt @@ -21,7 +21,7 @@ set(libcommon_SRCS comm.h utility/types.h utility/types.cpp - utility/utility.h utility/utility.cpp + utility/utility_base.h utility/utility.h utility/utility.cpp utility/jsonparserutility.h keychainmanager/apitoken.h keychainmanager/apitoken.cpp keychainmanager/keychainmanager.h keychainmanager/keychainmanager.cpp diff --git a/src/libcommon/utility/utility.cpp b/src/libcommon/utility/utility.cpp index 4f9ad66fd..63ccc5922 100644 --- a/src/libcommon/utility/utility.cpp +++ b/src/libcommon/utility/utility.cpp @@ -937,11 +937,4 @@ bool CommonUtility::isLiteSyncExtFullDiskAccessAuthOk(std::string &errorDescr) { } #endif - - -#if defined(__APPLE__) || defined(__unix__) -bool CommonUtility::isLikeFileNotFoundError(const std::error_code &ec) noexcept { - return ec.value() != static_cast(std::errc::no_such_file_or_directory); -} -#endif } // namespace KDC diff --git a/src/libcommon/utility/utility.h b/src/libcommon/utility/utility.h index 3ebeab650..a632d2634 100644 --- a/src/libcommon/utility/utility.h +++ b/src/libcommon/utility/utility.h @@ -20,6 +20,7 @@ #include "libcommon/libcommon.h" #include "types.h" +#include "utility_base.h" #include @@ -117,12 +118,14 @@ struct COMMON_EXPORT CommonUtility { static void writeSignalFile(AppType appType, SignalType signalType) noexcept; static void clearSignalFile(AppType appType, SignalCategory signalCategory, SignalType &signalType) noexcept; - static bool isLikeFileNotFoundError(const std::error_code &ec) noexcept; + static bool isLikeFileNotFoundError(const std::error_code &ec) noexcept { + return utility_base::isLikeFileNotFoundError(ec); + }; #ifdef _WIN32 - static std::wstring getErrorMessage(DWORD dwordError); - static std::wstring getLastErrorMessage(); - static bool isLikeFileNotFoundError(DWORD dwError) noexcept; + static std::wstring getErrorMessage(DWORD errorMessageId) { return utility_base::getErrorMessage(errorMessageId); } + static std::wstring getLastErrorMessage() { return utility_base::getLastErrorMessage(); }; + static bool isLikeFileNotFoundError(DWORD dwError) noexcept { return utility_base::isLikeFileNotFoundError(dwError); }; #endif private: diff --git a/src/libcommon/utility/utility_base.h b/src/libcommon/utility/utility_base.h new file mode 100644 index 000000000..99e08a74d --- /dev/null +++ b/src/libcommon/utility/utility_base.h @@ -0,0 +1,75 @@ +/* + * Infomaniak kDrive - Desktop + * Copyright (C) 2023-2024 Infomaniak Network SA + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#pragma once + + +#ifdef _WIN32 +#include +#include +#include +#include +#include + +#include +#endif + +#include +#include + +namespace KDC::utility_base { + +#ifdef _WIN32 +inline std::wstring getErrorMessage(DWORD errorMessageID) { + LPWSTR messageBuffer = nullptr; + const size_t size = + FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, nullptr, + errorMessageID, NULL, (LPWSTR) &messageBuffer, 0, nullptr); + + // Escape quotes + const auto msg = std::wstring(messageBuffer, size); + std::wostringstream message; + message << L"(" << errorMessageID << L") - " << msg; + + LocalFree(messageBuffer); + + return message.str(); +} + +inline std::wstring getLastErrorMessage() { + return getErrorMessage(GetLastError()); +} +inline bool isLikeFileNotFoundError(DWORD dwError) noexcept { + return (dwError != ERROR_FILE_NOT_FOUND) && (dwError != ERROR_PATH_NOT_FOUND) && (dwError != ERROR_INVALID_DRIVE) && + (dwError != ERROR_BAD_NETPATH); +} + +inline bool isLikeFileNotFoundError(const std::error_code &ec) noexcept { + return isLikeFileNotFoundError(static_cast(ec.value())); +} + +#endif + +#if defined(__APPLE__) || defined(__unix__) +bool isLikeFileNotFoundError(const std::error_code &ec) noexcept { + return ec.value() != static_cast(std::errc::no_such_file_or_directory); +} +#endif + + +} // namespace KDC::utility_base diff --git a/src/libcommon/utility/utility_win.cpp b/src/libcommon/utility/utility_win.cpp index f692c7d65..ff8b85260 100644 --- a/src/libcommon/utility/utility_win.cpp +++ b/src/libcommon/utility/utility_win.cpp @@ -63,34 +63,4 @@ static inline bool hasDarkSystray_private() { return !settings.value(QLatin1String(lightThemeKeyC), true).toBool(); } - -bool CommonUtility::isLikeFileNotFoundError(DWORD dwError) noexcept { - return (dwError != ERROR_FILE_NOT_FOUND) && (dwError != ERROR_PATH_NOT_FOUND) && (dwError != ERROR_INVALID_DRIVE) && - (dwError != ERROR_BAD_NETPATH); -} - -bool CommonUtility::isLikeFileNotFoundError(const std::error_code &code) noexcept { - return isLikeFileNotFoundError(static_cast(code.value())); -} - -std::wstring CommonUtility::getErrorMessage(DWORD errorMessageID) { - LPWSTR messageBuffer = nullptr; - const size_t size = - FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, nullptr, - errorMessageID, NULL, (LPWSTR) &messageBuffer, 0, nullptr); - - // Escape quotes - const auto msg = std::wstring(messageBuffer, size); - std::wostringstream message; - message << L"(" << errorMessageID << L") - " << msg; - - LocalFree(messageBuffer); - - return message.str(); -} - -std::wstring CommonUtility::getLastErrorMessage() { - return getErrorMessage(GetLastError()); -} - } // namespace KDC