]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
Add and use win32_error_message function
authorJoel Rosdahl <joel@rosdahl.net>
Fri, 15 May 2020 19:06:27 +0000 (21:06 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Wed, 20 May 2020 18:07:12 +0000 (20:07 +0200)
src/Lockfile.cpp
src/Util.cpp
src/execute.cpp
src/legacy_util.cpp
src/win32compat.cpp
src/win32compat.hpp

index 7aba595e1901fe3e9b6eaacb2720861e1ad0af4f..1ddd16ac41aa9a90f9e5e71319aa3d7ae6279675 100644 (file)
 #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?
index 10f1aca96b3c014b9f8cac553543bf169034faed..900b5e4c466e3393157d0db2dfd82f88af5eb019 100644 (file)
 #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 {
index 83c689c8db1ceda83815e4d8081c247bfb5660f5..0c25f0dba97762051012338dccbe99a3c8cb4f60 100644 (file)
 #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);
index f73d4c2c68fdc81ae34516203406bdabd7c9490b..a088040eb886af1696c0d7f2570083262df155c9 100644 (file)
 #include "exceptions.hpp"
 #include "logging.hpp"
 
+#ifdef _WIN32
+#  include "win32compat.hpp"
+#endif
+
 #include "third_party/fmt/core.h"
 
 #include <string>
@@ -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;
index 7728b65d8a26ecddc007e75e1032722f9ad06fda..5da56c1797c55c19c86a1e8c8be245c386980821 100644 (file)
 #  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)
index 4d0e699887ca2f3fc2ae28d51a2af9b96b6740ea..e26622c872d3e02b51a988f65fac0664a409210e 100644 (file)
 #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);