]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
refactor: Let util::rename return std::error_code
authorJoel Rosdahl <joel@rosdahl.net>
Tue, 18 Jul 2023 08:19:05 +0000 (10:19 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Tue, 18 Jul 2023 19:37:35 +0000 (21:37 +0200)
src/AtomicFile.cpp
src/storage/local/LocalStorage.cpp
src/util/file.cpp
src/util/file.hpp

index ef6fe8e12d173364a3a8873728a427f0f31edcee..73c5682bb1ad341e7c5d711b76d5ced8276d841f 100644 (file)
@@ -82,7 +82,9 @@ AtomicFile::commit()
   }
   const auto result = util::rename(m_tmp_path, m_path);
   if (!result) {
-    throw core::Error(
-      FMT("failed to rename {} to {}: {}", m_tmp_path, m_path, result.error()));
+    throw core::Error(FMT("failed to rename {} to {}: {}",
+                          m_tmp_path,
+                          m_path,
+                          result.error().message()));
   }
 }
index 4513444d52d8b0129d0e681c95582d52877ef766..f6f7a7558cb6677e1de459f588babc5a0dee3392 100644 (file)
@@ -270,8 +270,10 @@ clone_file(const std::string& src, const std::string& dest, bool via_tmp_file)
   if (via_tmp_file) {
     const auto result = util::rename(tmp_file, dest);
     if (!result) {
-      throw core::Error(
-        FMT("failed to rename {} to {}: {}", tmp_file, dest, result.error()));
+      throw core::Error(FMT("failed to rename {} to {}: {}",
+                            tmp_file,
+                            dest,
+                            result.error().message()));
     }
   }
 #  elif defined(__APPLE__)
index b0a718c4483dd6298193bf8015d51f3ac259b169..d7f67d0d608566c590e1aa8e75dc97d9ecc2500c 100644 (file)
@@ -53,6 +53,7 @@
 
 #include <cerrno>
 #include <cstring>
+#include <filesystem>
 #include <fstream>
 #include <locale>
 #include <type_traits>
@@ -97,8 +98,10 @@ copy_file(const std::string& src,
   if (via_tmp_file == ViaTmpFile::yes) {
     const auto result = util::rename(tmp_file, dest);
     if (!result) {
-      return nonstd::make_unexpected(
-        FMT("Failed to rename {} to {}: {}", tmp_file, dest, result.error()));
+      return nonstd::make_unexpected(FMT("Failed to rename {} to {}: {}",
+                                         tmp_file,
+                                         dest,
+                                         result.error().message()));
     }
   }
 
@@ -369,12 +372,14 @@ read_file_part(const std::string& path, size_t pos, size_t count);
 template nonstd::expected<std::vector<uint8_t>, std::string>
 read_file_part(const std::string& path, size_t pos, size_t count);
 
-nonstd::expected<void, std::string>
+nonstd::expected<void, std::error_code>
 rename(const std::string& oldpath, const std::string& newpath)
 {
 #ifndef _WIN32
-  if (::rename(oldpath.c_str(), newpath.c_str()) != 0) {
-    return nonstd::make_unexpected(strerror(errno));
+  std::error_code ec;
+  std::filesystem::rename(oldpath, newpath, ec);
+  if (ec) {
+    return nonstd::make_unexpected(ec);
   }
 #else
   // Windows' rename() won't overwrite an existing file, so need to use
@@ -382,7 +387,9 @@ rename(const std::string& oldpath, const std::string& newpath)
   if (!MoveFileExA(
         oldpath.c_str(), newpath.c_str(), MOVEFILE_REPLACE_EXISTING)) {
     DWORD error = GetLastError();
-    return nonstd::make_unexpected(Win32Util::error_message(error));
+    // TODO: How should the Win32 error be mapped to std::error_code?
+    return nonstd::make_unexpected(
+      std::error_code(error, std::system_category()));
   }
 #endif
   return {};
index 858083c21fd204520bbc3734d7604128ccf36764..347f5d91de048f23be05af934b3ebefffc796647 100644 (file)
@@ -89,8 +89,8 @@ read_file_part(const std::string& path, size_t pos, size_t count);
 // Note: Mingw-w64's std::filesystem::rename is buggy and doesn't properly
 // overwrite an existing file, at least in version 9.1.0, hence this utility
 // function.
-nonstd::expected<void, std::string> rename(const std::string& oldpath,
-                                           const std::string& newpath);
+nonstd::expected<void, std::error_code> rename(const std::string& oldpath,
+                                               const std::string& newpath);
 
 // Set the FD_CLOEXEC on file descriptor `fd`. This is a NOP on Windows.
 void set_cloexec_flag(int fd);