]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
C++-ify x_rename
authorJoel Rosdahl <joel@rosdahl.net>
Fri, 31 Jul 2020 17:59:49 +0000 (19:59 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Fri, 31 Jul 2020 18:31:54 +0000 (20:31 +0200)
src/AtomicFile.cpp
src/Util.cpp
src/Util.hpp
src/ccache.cpp
src/legacy_util.cpp
src/legacy_util.hpp

index 10920fc229aad82ab5343d0abd2105e3c395376d..1a2879a7c00beedb474670553eb81ac00d974dca 100644 (file)
@@ -70,7 +70,5 @@ AtomicFile::commit()
     throw Error(
       fmt::format("failed to write data to {}: {}", m_path, strerror(errno)));
   }
-  if (x_rename(m_tmp_path.c_str(), m_path.c_str()) != 0) {
-    throw Error(fmt::format("failed to rename {} to {}", m_tmp_path, m_path));
-  }
+  Util::rename(m_tmp_path, m_path);
 }
index a6f2ac73f02688ffa569d967dc7f5cbb5261428b..f18bed0d0e1c9f9960850b21480e1ed2f7c91f69 100644 (file)
 #  include <sys/param.h>
 #endif
 
+#ifdef _WIN32
+#  include "Win32Util.hpp"
+#endif
+
 #ifdef __linux__
 #  ifdef HAVE_SYS_IOCTL_H
 #    include <sys/ioctl.h>
@@ -200,8 +204,8 @@ clone_file(const std::string& src, const std::string& dest, bool via_tmp_file)
   dest_fd.close();
   src_fd.close();
 
-  if (via_tmp_file && x_rename(tmp_file.c_str(), dest.c_str()) != 0) {
-    throw Error(strerror(errno));
+  if (via_tmp_file) {
+    Util::rename(tmp_file, dest);
   }
 #  elif defined(__APPLE__)
   (void)via_tmp_file;
@@ -328,8 +332,8 @@ copy_file(const std::string& src, const std::string& dest, bool via_tmp_file)
   dest_fd.close();
   src_fd.close();
 
-  if (via_tmp_file && x_rename(tmp_file.c_str(), dest.c_str()) != 0) {
-    throw Error(strerror(errno));
+  if (via_tmp_file) {
+    Util::rename(tmp_file, dest);
   }
 }
 
@@ -1104,6 +1108,28 @@ remove_extension(string_view path)
   return path.substr(0, path.length() - get_extension(path).length());
 }
 
+void
+rename(const std::string& oldpath, const std::string& newpath)
+{
+#ifndef _WIN32
+  if (::rename(oldpath.c_str(), newpath.c_str()) != 0) {
+    throw Error(fmt::format(
+      "failed to rename {} to {}: {}", oldpath, newpath, strerror(errno)));
+  }
+#else
+  // Windows' rename() won't overwrite an existing file, so need to use
+  // MoveFileEx instead.
+  if (!MoveFileExA(
+        oldpath.c_str(), newpath.c_str(), MOVEFILE_REPLACE_EXISTING)) {
+    DWORD error = GetLastError();
+    throw Error(fmt::format("failed to rename {} to {}: {}",
+                            oldpath,
+                            newpath,
+                            Win32Util::error_message(error)));
+  }
+#endif
+}
+
 bool
 same_program_name(const std::string& program_name,
                   const std::string& canonical_program_name)
@@ -1261,17 +1287,19 @@ unlink_safe(const std::string& path, UnlinkLog unlink_log)
   std::string tmp_name = path + ".ccache.rm.tmp";
 
   bool success = true;
-  if (x_rename(path.c_str(), tmp_name.c_str()) != 0) {
+  try {
+    Util::rename(path, tmp_name);
+  } catch (Error&) {
     success = false;
     saved_errno = errno;
-  } else if (unlink(tmp_name.c_str()) != 0) {
+  }
+  if (success && unlink(tmp_name.c_str()) != 0) {
     // It's OK if it was unlinked in a race.
     if (errno != ENOENT && errno != ESTALE) {
       success = false;
       saved_errno = errno;
     }
   }
-
   if (success || unlink_log == UnlinkLog::log_failure) {
     cc_log("Unlink %s via %s", path.c_str(), tmp_name.c_str());
     if (!success) {
index 9434b8d0ff98d9689dc3c6ec3f8ddc603ffad93d..2c4a8d249351245499a79b097cfa4ff8e3cb985c 100644 (file)
@@ -352,6 +352,9 @@ std::string real_path(const std::string& path,
 // extension as determined by `get_extension()`.
 nonstd::string_view remove_extension(nonstd::string_view path);
 
+// Rename `oldpath` to `newpath` (deleting `newpath`). Throws `Error` on error.
+void rename(const std::string& oldpath, const std::string& newpath);
+
 // Detmine if `program_name` is equal to `canonical_program_name`. On Windows,
 // this means performing a case insensitive equality check with and without a
 // ".exe" suffix. On non-Windows, it is a simple equality check.
index b09e7411bd989061d0864d5206c9f67a12355a2b..a4f5cde097ab8029905e4b3456fe3c83994ca7d0 100644 (file)
@@ -633,30 +633,8 @@ use_relative_paths_in_depfile(const Context& ctx)
   }
 
   std::string tmp_file = output_dep + ".tmp";
-  try {
-    Util::write_file(tmp_file, adjusted_file_content);
-  } catch (const Error& e) {
-    cc_log(
-      "Error writing temporary dependency file %s (%s), skip relative path"
-      " usage",
-      tmp_file.c_str(),
-      e.what());
-    Util::unlink_safe(tmp_file);
-    return;
-  }
-
-  if (x_rename(tmp_file.c_str(), output_dep.c_str()) != 0) {
-    cc_log(
-      "Error renaming dependency file: %s -> %s (%s), skip relative path usage",
-      tmp_file.c_str(),
-      output_dep.c_str(),
-      strerror(errno));
-    Util::unlink_safe(tmp_file);
-  } else {
-    cc_log("Renamed dependency file: %s -> %s",
-           tmp_file.c_str(),
-           output_dep.c_str());
-  }
+  Util::write_file(tmp_file, adjusted_file_content);
+  Util::rename(tmp_file, output_dep);
 }
 
 // Extract the used includes from the dependency file. Note that we cannot
@@ -1095,7 +1073,7 @@ get_result_name_from_cpp(Context& ctx, Args& args, Hash& hash)
     // thing correctly
     ctx.i_tmpfile =
       fmt::format("{}.{}", stdout_path, ctx.config.cpp_extension());
-    x_rename(stdout_path.c_str(), ctx.i_tmpfile.c_str());
+    Util::rename(stdout_path, ctx.i_tmpfile);
     ctx.register_pending_tmp_file(ctx.i_tmpfile);
   }
 
index f8f5ef4d670acc9d1599dfaa70683c005680f15c..bd177844372f8a6d195f1cfd5140642f14b3f170 100644 (file)
 #  include <sys/time.h>
 #endif
 
-// Rename oldpath to newpath (deleting newpath).
-int
-x_rename(const char* oldpath, const char* newpath)
-{
-#ifndef _WIN32
-  return rename(oldpath, newpath);
-#else
-  // 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) {
-    DWORD error = GetLastError();
-    cc_log("failed to rename %s to %s: %s (%lu)",
-           oldpath,
-           newpath,
-           Win32Util::error_message(error).c_str(),
-           error);
-    return -1;
-  } else {
-    return 0;
-  }
-#endif
-}
-
 void
 set_cloexec_flag(int fd)
 {
index 2de2d15862811c79a94ea357d890e9e2ab7c6fb5..182579acd7124bd610e85e06a1f65199dafe3447 100644 (file)
@@ -22,6 +22,5 @@
 
 #include <string>
 
-int x_rename(const char* oldpath, const char* newpath);
 void set_cloexec_flag(int fd);
 double time_seconds();