]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
Extract copy_raw_file into Util::clone_hard_link_or_copy_file
authorJoel Rosdahl <joel@rosdahl.net>
Wed, 1 Jul 2020 14:28:28 +0000 (16:28 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Mon, 6 Jul 2020 17:42:41 +0000 (19:42 +0200)
src/Util.cpp
src/Util.hpp
src/result.cpp

index 488631fc0065fd3e1b517170a031f630d0252fde..e0897f4a7a6ffe5c7970fe8b61ffc5e0ed6f9c6e 100644 (file)
@@ -107,6 +107,36 @@ change_extension(string_view path, string_view new_ext)
   return std::string(without_ext).append(new_ext.data(), new_ext.length());
 }
 
+bool
+clone_hard_link_or_copy_file(const Context& ctx,
+                             const std::string& source,
+                             const std::string& dest,
+                             bool via_tmp_file)
+{
+  if (ctx.config.file_clone()) {
+    cc_log("Cloning %s to %s", source.c_str(), dest.c_str());
+    if (clone_file(source.c_str(), dest.c_str(), via_tmp_file)) {
+      return true;
+    }
+    cc_log("Failed to clone: %s", strerror(errno));
+  }
+  if (ctx.config.hard_link()) {
+    unlink(dest.c_str());
+    cc_log("Hard linking %s to %s", source.c_str(), dest.c_str());
+    int ret = link(source.c_str(), dest.c_str());
+    if (ret == 0) {
+      if (chmod(dest.c_str(), 0444) != 0) {
+        cc_log("Failed to chmod: %s", strerror(errno));
+      }
+      return true;
+    }
+    cc_log("Failed to hard link: %s", strerror(errno));
+  }
+
+  cc_log("Copying %s to %s", source.c_str(), dest.c_str());
+  return copy_file(source.c_str(), dest.c_str(), via_tmp_file);
+}
+
 size_t
 common_dir_prefix_length(string_view dir, string_view path)
 {
index a879080f5b53cf853c0defefc214d10c31a9d890..59e0852a2a12aa8b28a90463e9c4e38ad3d81161 100644 (file)
@@ -83,6 +83,14 @@ big_endian_to_int(const uint8_t* buffer, uint8_t& value)
 std::string change_extension(nonstd::string_view path,
                              nonstd::string_view new_ext);
 
+// Clone, hard link or copy a file from `source` to `dest` depending on settings
+// in `ctx`. If cloning or hard linking cannot and should not be done the file
+// will be copied instead. Returns true if successful otherwise false.
+bool clone_hard_link_or_copy_file(const Context& ctx,
+                                  const std::string& source,
+                                  const std::string& dest,
+                                  bool via_tmp_file);
+
 // Compute the length of the longest directory path that is common to paths
 // `dir` (a directory) and `path` (any path).
 size_t common_dir_prefix_length(nonstd::string_view dir,
index ad858a873cc1d7435c415f15793cefd68d71f107..da90d75276445f91bb09c4b56717dbd95c7d98cb 100644 (file)
@@ -219,36 +219,6 @@ get_raw_file_path(const std::string& result_path_in_cache,
                      entry_number);
 }
 
-static bool
-copy_raw_file(const Context& ctx,
-              const std::string& source,
-              const std::string& dest,
-              bool to_cache)
-{
-  if (ctx.config.file_clone()) {
-    cc_log("Cloning %s to %s", source.c_str(), dest.c_str());
-    if (clone_file(source.c_str(), dest.c_str(), to_cache)) {
-      return true;
-    }
-    cc_log("Failed to clone: %s", strerror(errno));
-  }
-  if (ctx.config.hard_link()) {
-    unlink(dest.c_str());
-    cc_log("Hard linking %s to %s", source.c_str(), dest.c_str());
-    int ret = link(source.c_str(), dest.c_str());
-    if (ret == 0) {
-      if (chmod(dest.c_str(), 0444) != 0) {
-        cc_log("Failed to chmod: %s", strerror(errno));
-      }
-      return true;
-    }
-    cc_log("Failed to hard link: %s", strerror(errno));
-  }
-
-  cc_log("Copying %s to %s", source.c_str(), dest.c_str());
-  return copy_file(source.c_str(), dest.c_str(), to_cache);
-}
-
 static void
 read_raw_file_entry(const Context& ctx,
                     CacheEntryReader& reader,
@@ -292,7 +262,8 @@ read_raw_file_entry(const Context& ctx,
       cc_log("Not copying to /dev/null");
     } else {
       const auto& dest_path = it->second;
-      if (!copy_raw_file(ctx, raw_path, dest_path, false)) {
+      if (!Util::clone_hard_link_or_copy_file(
+            ctx, raw_path, dest_path, false)) {
         throw Error(
           fmt::format("Failed to copy raw file {} to {}", raw_path, dest_path));
       }
@@ -422,7 +393,7 @@ write_raw_file_entry(Context& ctx,
 
   auto raw_file = get_raw_file_path(result_path_in_cache, entry_number);
   auto old_stat = Stat::stat(raw_file);
-  if (!copy_raw_file(ctx, source_path, raw_file, true)) {
+  if (!Util::clone_hard_link_or_copy_file(ctx, source_path, raw_file, true)) {
     throw Error(
       fmt::format("Failed to store {} as raw file {}", source_path, raw_file));
   }