From: Joel Rosdahl Date: Fri, 15 May 2020 19:06:27 +0000 (+0200) Subject: Add and use win32_error_message function X-Git-Tag: v4.0~446 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=5062316b39a59e7674d04f9e79a2debe715f7bdf;p=thirdparty%2Fccache.git Add and use win32_error_message function --- diff --git a/src/Lockfile.cpp b/src/Lockfile.cpp index 7aba595e1..1ddd16ac4 100644 --- a/src/Lockfile.cpp +++ b/src/Lockfile.cpp @@ -22,6 +22,10 @@ #include "legacy_util.hpp" #include "logging.hpp" +#ifdef _WIN32 +# include "win32compat.hpp" +#endif + #include "third_party/fmt/core.h" namespace { @@ -148,8 +152,9 @@ do_acquire_win32(const std::string& lockfile, uint32_t staleness_limit) } DWORD error = GetLastError(); - cc_log("lockfile_acquire: CreateFile %s: error code %lu", + cc_log("lockfile_acquire: CreateFile %s: %s (%lu)", lockfile.c_str(), + win32_error_message(error).c_str(), error); if (error == ERROR_PATH_NOT_FOUND) { // Directory doesn't exist? diff --git a/src/Util.cpp b/src/Util.cpp index 10f1aca96..900b5e4c4 100644 --- a/src/Util.cpp +++ b/src/Util.cpp @@ -22,13 +22,13 @@ #include "Context.hpp" #include "FormatNonstdStringView.hpp" -#include -#include - #ifdef _WIN32 # include "win32compat.hpp" #endif +#include +#include + using nonstd::string_view; namespace { diff --git a/src/execute.cpp b/src/execute.cpp index 83c689c8d..0c25f0dba 100644 --- a/src/execute.cpp +++ b/src/execute.cpp @@ -26,6 +26,10 @@ #include "ccache.hpp" #include "logging.hpp" +#ifdef _WIN32 +# include "win32compat.hpp" +#endif + using nonstd::string_view; #ifdef _WIN32 @@ -214,35 +218,12 @@ win32execute(const char* path, } free(args); if (ret == 0) { - LPVOID lpMsgBuf; - DWORD dw = GetLastError(); - FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM - | FORMAT_MESSAGE_IGNORE_INSERTS, - NULL, - dw, - MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), - (LPTSTR)&lpMsgBuf, - 0, - NULL); - - LPVOID lpDisplayBuf = (LPVOID)LocalAlloc( - LMEM_ZEROINIT, - (lstrlen((LPCTSTR)lpMsgBuf) + lstrlen((LPCTSTR)__FILE__) + 200) - * sizeof(TCHAR)); - _snprintf((LPTSTR)lpDisplayBuf, - LocalSize(lpDisplayBuf) / sizeof(TCHAR), - TEXT("%s failed with error %lu: %s"), - __FILE__, - dw, - (const char*)lpMsgBuf); - - cc_log("can't execute %s; OS returned error: %s", + DWORD error = GetLastError(); + std::string error_message = win32_error_message(error); + cc_log("failed to execute %s: %s (%lu)", full_path_win_ext, - (char*)lpDisplayBuf); - - LocalFree(lpMsgBuf); - LocalFree(lpDisplayBuf); - + win32_error_message(error).c_str(), + error); return -1; } WaitForSingleObject(pi.hProcess, INFINITE); diff --git a/src/legacy_util.cpp b/src/legacy_util.cpp index f73d4c2c6..a088040eb 100644 --- a/src/legacy_util.cpp +++ b/src/legacy_util.cpp @@ -23,6 +23,10 @@ #include "exceptions.hpp" #include "logging.hpp" +#ifdef _WIN32 +# include "win32compat.hpp" +#endif + #include "third_party/fmt/core.h" #include @@ -692,35 +696,12 @@ x_rename(const char* oldpath, const char* newpath) // Windows' rename() refuses to overwrite an existing file. // If the function succeeds, the return value is nonzero. if (MoveFileExA(oldpath, newpath, MOVEFILE_REPLACE_EXISTING) == 0) { - LPVOID lp_msg_buf; - DWORD dw = GetLastError(); - FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM - | FORMAT_MESSAGE_IGNORE_INSERTS, - NULL, - dw, - MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), - (LPTSTR)&lp_msg_buf, - 0, - NULL); - - LPVOID lp_display_buf = (LPVOID)LocalAlloc( - LMEM_ZEROINIT, - (lstrlen((LPCTSTR)lp_msg_buf) + lstrlen((LPCTSTR)__FILE__) + 40) - * sizeof(TCHAR)); - _snprintf((LPTSTR)lp_display_buf, - LocalSize(lp_display_buf) / sizeof(TCHAR), - TEXT("%s failed with error %lu: %s"), - __FILE__, - dw, - (const char*)lp_msg_buf); - - cc_log("can't rename file %s to %s OS returned error: %s", + DWORD error = GetLastError(); + cc_log("failed to rename %s to %s: %s (%lu)", oldpath, newpath, - (char*)lp_display_buf); - - LocalFree(lp_msg_buf); - LocalFree(lp_display_buf); + win32_error_message(error).c_str(), + error); return -1; } else { return 0; diff --git a/src/win32compat.cpp b/src/win32compat.cpp index 7728b65d8..5da56c179 100644 --- a/src/win32compat.cpp +++ b/src/win32compat.cpp @@ -24,6 +24,24 @@ # include # include +std::string +win32_error_message(DWORD error_code) +{ + LPSTR buffer; + size_t size = + FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM + | FORMAT_MESSAGE_IGNORE_INSERTS, + nullptr, + error_code, + MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), + reinterpret_cast(&buffer), + 0, + nullptr); + std::string message(buffer, size); + LocalFree(buffer); + return message; +} + # if !defined(HAVE_REALPATH) && !defined(HAVE_GETFINALPATHNAMEBYHANDLEW) BOOL GetFileNameFromHandle(HANDLE file_handle, TCHAR* filename, WORD cch_filename) diff --git a/src/win32compat.hpp b/src/win32compat.hpp index 4d0e69988..e26622c87 100644 --- a/src/win32compat.hpp +++ b/src/win32compat.hpp @@ -21,6 +21,10 @@ #ifdef _WIN32 # include "system.hpp" +# include + +std::string win32_error_message(DWORD error_code); + BOOL GetFileNameFromHandle(HANDLE file_handle, TCHAR* filename, WORD cch_filename);