Skip to content

Commit

Permalink
Fixes Windows build failure
Browse files Browse the repository at this point in the history
  • Loading branch information
luc-guyot-infomaniak committed Nov 26, 2024
1 parent 09afbf3 commit 0033126
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 47 deletions.
9 changes: 4 additions & 5 deletions extensions/windows/cfapi/Common/utilities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@
*/

#include "utilities.h"

#include "../../../../src/libcommon/utility/utility.h"
#include "../../../../src/libcommon/utility/utility_base.h"

#include <winrt\base.h>

Expand Down Expand Up @@ -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;
}

Expand All @@ -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;
}

Expand All @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion src/libcommon/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 0 additions & 7 deletions src/libcommon/utility/utility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<int>(std::errc::no_such_file_or_directory);
}
#endif
} // namespace KDC
11 changes: 7 additions & 4 deletions src/libcommon/utility/utility.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

#include "libcommon/libcommon.h"
#include "types.h"
#include "utility_base.h"

#include <string>

Expand Down Expand Up @@ -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:
Expand Down
75 changes: 75 additions & 0 deletions src/libcommon/utility/utility_base.h
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
*/

#pragma once


#ifdef _WIN32
#include <shlobj.h>
#include <winbase.h>
#include <windows.h>
#include <winerror.h>
#include <strsafe.h>

#include <sstream>
#endif

#include <string>
#include <system_error>

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<DWORD>(ec.value()));
}

#endif

#if defined(__APPLE__) || defined(__unix__)
bool isLikeFileNotFoundError(const std::error_code &ec) noexcept {
return ec.value() != static_cast<int>(std::errc::no_such_file_or_directory);
}
#endif


} // namespace KDC::utility_base
30 changes: 0 additions & 30 deletions src/libcommon/utility/utility_win.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<DWORD>(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

0 comments on commit 0033126

Please sign in to comment.