#include "legacy_util.hpp"
#include "logging.hpp"
+#ifdef _WIN32
+# include "win32compat.hpp"
+#endif
+
#include "third_party/fmt/core.h"
namespace {
}
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?
#include "Context.hpp"
#include "FormatNonstdStringView.hpp"
-#include <algorithm>
-#include <fstream>
-
#ifdef _WIN32
# include "win32compat.hpp"
#endif
+#include <algorithm>
+#include <fstream>
+
using nonstd::string_view;
namespace {
#include "ccache.hpp"
#include "logging.hpp"
+#ifdef _WIN32
+# include "win32compat.hpp"
+#endif
+
using nonstd::string_view;
#ifdef _WIN32
}
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);
#include "exceptions.hpp"
#include "logging.hpp"
+#ifdef _WIN32
+# include "win32compat.hpp"
+#endif
+
#include "third_party/fmt/core.h"
#include <string>
// 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;
# include <sys/locking.h>
# include <tchar.h>
+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<LPSTR>(&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)
#ifdef _WIN32
# include "system.hpp"
+# include <string>
+
+std::string win32_error_message(DWORD error_code);
+
BOOL
GetFileNameFromHandle(HANDLE file_handle, TCHAR* filename, WORD cch_filename);