]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
refactor: Move copy_file to util
authorJoel Rosdahl <joel@rosdahl.net>
Sun, 9 Jul 2023 17:08:20 +0000 (19:08 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Wed, 12 Jul 2023 09:07:57 +0000 (11:07 +0200)
src/MiniTrace.cpp
src/Util.cpp
src/Util.hpp
src/storage/local/LocalStorage.cpp
src/util/file.cpp
src/util/file.hpp

index cd9f4f80989bc88a2d9a8619bf6b469141f93d70..cbda28ad45e0683fba9b257f117eeadaa024c06c 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2020-2022 Joel Rosdahl and other contributors
+// Copyright (C) 2020-2023 Joel Rosdahl and other contributors
 //
 // See doc/AUTHORS.adoc for a complete list of contributors.
 //
@@ -25,6 +25,7 @@
 
 #include <core/wincompat.hpp>
 #include <util/TimePoint.hpp>
+#include <util/file.hpp>
 
 #include <limits.h> // NOLINT: PATH_MAX is defined in limits.h
 
@@ -76,7 +77,7 @@ MiniTrace::~MiniTrace()
   mtr_shutdown();
 
   if (!m_args_info.output_obj.empty()) {
-    Util::copy_file(m_tmp_trace_file, m_args_info.output_obj + ".ccache-trace");
+    util::copy_file(m_tmp_trace_file, m_args_info.output_obj + ".ccache-trace");
   }
   Util::unlink_tmp(m_tmp_trace_file);
 }
index 8c253de232ce4bd88579cd610be6f2042dd91731..ee0890fdc1502d29541132ae8dcc200ed199181c 100644 (file)
@@ -223,49 +223,6 @@ common_dir_prefix_length(std::string_view dir, std::string_view path)
   return i;
 }
 
-void
-copy_fd(int fd_in, int fd_out)
-{
-  util::read_fd(fd_in, [=](nonstd::span<const uint8_t> data) {
-    util::write_fd(fd_out, data.data(), data.size());
-  });
-}
-
-void
-copy_file(const std::string& src, const std::string& dest, bool via_tmp_file)
-{
-  Fd src_fd(open(src.c_str(), O_RDONLY | O_BINARY));
-  if (!src_fd) {
-    throw core::Error(
-      FMT("Failed to open {} for reading: {}", src, strerror(errno)));
-  }
-
-  unlink(dest.c_str());
-
-  Fd dest_fd;
-  std::string tmp_file;
-  if (via_tmp_file) {
-    TemporaryFile temp_file(dest);
-    dest_fd = std::move(temp_file.fd);
-    tmp_file = temp_file.path;
-  } else {
-    dest_fd =
-      Fd(open(dest.c_str(), O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666));
-    if (!dest_fd) {
-      throw core::Error(
-        FMT("Failed to open {} for writing: {}", dest, strerror(errno)));
-    }
-  }
-
-  copy_fd(*src_fd, *dest_fd);
-  dest_fd.close();
-  src_fd.close();
-
-  if (via_tmp_file) {
-    Util::rename(tmp_file, dest);
-  }
-}
-
 bool
 create_dir(std::string_view dir)
 {
index d157808d13ffa8d2cdba7e1437563a1184bdf8e5..bafd185a849b3a8c458cb48fb2be36d1b50a59d8 100644 (file)
@@ -53,15 +53,6 @@ std::string change_extension(std::string_view path, std::string_view new_ext);
 // `dir` (a directory) and `path` (any path).
 size_t common_dir_prefix_length(std::string_view dir, std::string_view path);
 
-// Copy all data from `fd_in` to `fd_out`. Throws `core::Error` on error.
-void copy_fd(int fd_in, int fd_out);
-
-// Copy a file from `src` to `dest`. If via_tmp_file is true, `src` is copied to
-// a temporary file and then renamed to dest. Throws `core::Error` on error.
-void copy_file(const std::string& src,
-               const std::string& dest,
-               bool via_tmp_file = false);
-
 // Create a directory if needed, including its parents if needed.
 //
 // Returns true if the directory exists or could be created, otherwise false.
index ebc13ed438faa87962d53ed40f3a8a835ba4e9d7..bfb20ae3a7d1177ead826c6546d4a01dd65a0bd3 100644 (file)
@@ -654,7 +654,8 @@ LocalStorage::clone_hard_link_or_copy_file(const std::string& source,
   }
 
   LOG("Copying {} to {}", source, dest);
-  Util::copy_file(source, dest, via_tmp_file);
+  util::copy_file(
+    source, dest, via_tmp_file ? util::ViaTmpFile::yes : util::ViaTmpFile::no);
 }
 
 void
index 7b80b584b214f9ed6d90db2ac47dbb7ead41d060..48d987af621cc36001de0b462c4dd992458feaac 100644 (file)
 #include <Fd.hpp>
 #include <Logging.hpp>
 #include <Stat.hpp>
+#include <TemporaryFile.hpp>
+#include <Util.hpp>
 #include <Win32Util.hpp>
 #include <fmtmacros.hpp>
 #include <util/Bytes.hpp>
+#include <util/expected.hpp>
 
 #ifdef HAVE_UNISTD_H
 #  include <unistd.h>
 
 namespace util {
 
+nonstd::expected<void, std::string>
+copy_file(const std::string& src,
+          const std::string& dest,
+          ViaTmpFile via_tmp_file)
+{
+  Fd src_fd(open(src.c_str(), O_RDONLY | O_BINARY));
+  if (!src_fd) {
+    return nonstd::make_unexpected(
+      FMT("Failed to open {} for reading: {}", src, strerror(errno)));
+  }
+
+  unlink(dest.c_str());
+
+  Fd dest_fd;
+  std::string tmp_file;
+  if (via_tmp_file == ViaTmpFile::yes) {
+    TemporaryFile temp_file(dest);
+    dest_fd = std::move(temp_file.fd);
+    tmp_file = temp_file.path;
+  } else {
+    dest_fd =
+      Fd(open(dest.c_str(), O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666));
+    if (!dest_fd) {
+      return nonstd::make_unexpected(
+        FMT("Failed to open {} for writing: {}", dest, strerror(errno)));
+    }
+  }
+  TRY(util::read_fd(*src_fd, [&](nonstd::span<const uint8_t> data) {
+    util::write_fd(*dest_fd, data.data(), data.size());
+  }));
+
+  dest_fd.close();
+  src_fd.close();
+
+  if (via_tmp_file == ViaTmpFile::yes) {
+    Util::rename(tmp_file, dest);
+  }
+
+  return {};
+}
+
 void
 create_cachedir_tag(const std::string& dir)
 {
index 0ae0a57f2ed105c364e57bf3a00c3aa0112dce26..286974e00d98b54e84bc70a2bb6423d58ed7dd18 100644 (file)
@@ -36,6 +36,14 @@ namespace util {
 // --- Interface ---
 
 enum class InPlace { yes, no };
+enum class ViaTmpFile { yes, no };
+
+// Copy a file from `src` to `dest`. If `via_tmp_file` is yes, `src` is copied
+// to a temporary file and then renamed to dest.
+nonstd::expected<void, std::string>
+copy_file(const std::string& src,
+          const std::string& dest,
+          ViaTmpFile via_tmp_file = ViaTmpFile::no);
 
 void create_cachedir_tag(const std::string& dir);