]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
refactor: Move Fd to util
authorJoel Rosdahl <joel@rosdahl.net>
Sat, 5 Aug 2023 08:49:04 +0000 (10:49 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Sat, 5 Aug 2023 08:49:04 +0000 (10:49 +0200)
22 files changed:
src/CMakeLists.txt
src/Fd.cpp [deleted file]
src/Hash.cpp
src/InodeCache.cpp
src/InodeCache.hpp
src/Util.cpp
src/ccache.cpp
src/core/Result.cpp
src/core/ResultExtractor.hpp
src/core/ResultRetriever.cpp
src/core/ResultRetriever.hpp
src/core/mainoptions.cpp
src/execute.cpp
src/execute.hpp
src/storage/local/LocalStorage.cpp
src/util/Fd.hpp [moved from src/Fd.hpp with 84% similarity]
src/util/TemporaryFile.hpp
src/util/file.cpp
unittest/test_InodeCache.cpp
unittest/test_Util.cpp
unittest/test_bsdmkstemp.cpp
unittest/test_util_file.cpp

index a5df950cec258856b1062cdc5de2ffbd1fc9729f..9e51429cd9ec1d88f4cf46625f56cd25db1baabb 100644 (file)
@@ -5,7 +5,6 @@ set(
   Config.cpp
   Context.cpp
   Depfile.cpp
-  Fd.cpp
   Hash.cpp
   Logging.cpp
   ProgressBar.cpp
diff --git a/src/Fd.cpp b/src/Fd.cpp
deleted file mode 100644 (file)
index 9a0448b..0000000
+++ /dev/null
@@ -1,31 +0,0 @@
-// Copyright (C) 2021 Joel Rosdahl and other contributors
-//
-// See doc/AUTHORS.adoc for a complete list of contributors.
-//
-// This program is free software; you can redistribute it and/or modify it
-// under the terms of the GNU General Public License as published by the Free
-// Software Foundation; either version 3 of the License, or (at your option)
-// any later version.
-//
-// This program is distributed in the hope that it will be useful, but WITHOUT
-// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
-// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
-// more details.
-//
-// You should have received a copy of the GNU General Public License along with
-// this program; if not, write to the Free Software Foundation, Inc., 51
-// Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
-
-#include "Fd.hpp"
-
-#include <util/wincompat.hpp>
-
-#ifdef HAVE_UNISTD_H
-#  include <unistd.h>
-#endif
-
-bool
-Fd::close()
-{
-  return m_fd != -1 && ::close(release()) == 0;
-}
index 6e8985f6a770b68e3c6783143114cb5f1bb9d4f3..749d0ba0f1edd02812b7684514d8ceebdfd9e879 100644 (file)
 
 #include "Hash.hpp"
 
-#include "Fd.hpp"
 #include "Logging.hpp"
 #include "fmtmacros.hpp"
 
+#include <util/Fd.hpp>
 #include <util/file.hpp>
 #include <util/string.hpp>
 #include <util/wincompat.hpp>
@@ -116,7 +116,7 @@ Hash::hash_fd(int fd)
 tl::expected<void, std::string>
 Hash::hash_file(const std::string& path)
 {
-  Fd fd(open(path.c_str(), O_RDONLY | O_BINARY));
+  util::Fd fd(open(path.c_str(), O_RDONLY | O_BINARY));
   if (!fd) {
     LOG("Failed to open {}: {}", path, strerror(errno));
     return tl::unexpected(strerror(errno));
index 8fd1be75bb0dbf9cf3ce4fc02a7b0e98015a2667..4add4bd9ec4ffed71556f77ba400dbb63ffc93be 100644 (file)
@@ -26,6 +26,7 @@
 #include "fmtmacros.hpp"
 
 #include <util/DirEntry.hpp>
+#include <util/Fd.hpp>
 #include <util/TemporaryFile.hpp>
 #include <util/conversion.hpp>
 #include <util/file.hpp>
@@ -237,7 +238,7 @@ InodeCache::mmap_file(const std::string& inode_cache_file)
     munmap(m_sr, sizeof(SharedRegion));
     m_sr = nullptr;
   }
-  m_fd = Fd(open(inode_cache_file.c_str(), O_RDWR));
+  m_fd = util::Fd(open(inode_cache_file.c_str(), O_RDWR));
   if (!m_fd) {
     LOG("Failed to open inode cache {}: {}", inode_cache_file, strerror(errno));
     return false;
index 94dd00895c75306bb189ae925ea990b4e93bdfa1..a5f75ec689b935ba14a6a994f6c8d4430fa10ac0 100644 (file)
 
 #pragma once
 
-#include <Fd.hpp>
 #include <Hash.hpp>
 #include <hashutil.hpp>
 #include <util/Duration.hpp>
+#include <util/Fd.hpp>
 #include <util/TimePoint.hpp>
 
 #include <cstdint>
@@ -135,7 +135,7 @@ private:
 
   const Config& m_config;
   util::Duration m_min_age;
-  Fd m_fd;
+  util::Fd m_fd;
   struct SharedRegion* m_sr = nullptr;
   bool m_failed = false;
   const pid_t m_self_pid;
index d48eec21acb543793ebe2e1ea2fa60e1fafbf71b..9281775fd449dd0161709322be0fad77f8263299 100644 (file)
@@ -20,7 +20,6 @@
 
 #include "Config.hpp"
 #include "Context.hpp"
-#include "Fd.hpp"
 #include "Logging.hpp"
 #include "Win32Util.hpp"
 
index 2b820fd53a18128ec5d80e2cbe903dddae9955c0..f607275e4e15f1dc4ffcb2d8926c627ba9767d68 100644 (file)
@@ -23,7 +23,6 @@
 #include "ArgsInfo.hpp"
 #include "Context.hpp"
 #include "Depfile.hpp"
-#include "Fd.hpp"
 #include "File.hpp"
 #include "Finalizer.hpp"
 #include "Hash.hpp"
@@ -52,6 +51,7 @@
 #include <core/mainoptions.hpp>
 #include <core/types.hpp>
 #include <storage/Storage.hpp>
+#include <util/Fd.hpp>
 #include <util/TemporaryFile.hpp>
 #include <util/UmaskScope.hpp>
 #include <util/environment.hpp>
@@ -707,7 +707,7 @@ result_key_from_depfile(Context& ctx, Hash& hash)
 
 struct GetTmpFdResult
 {
-  Fd fd;
+  util::Fd fd;
   fs::path path;
 };
 
@@ -724,7 +724,7 @@ get_tmp_fd(Context& ctx,
     return {std::move(tmp_stdout.fd), std::move(tmp_stdout.path)};
   } else {
     const auto dev_null_path = util::get_dev_null_path();
-    return {Fd(open(dev_null_path, O_WRONLY | O_BINARY)), dev_null_path};
+    return {util::Fd(open(dev_null_path, O_WRONLY | O_BINARY)), dev_null_path};
   }
 }
 
index 31b3513c86b623abae3bdf07a33385e9ec88fd01..4de7c729fdc705533fef148571c1ed6479db13bb 100644 (file)
@@ -20,7 +20,6 @@
 
 #include "Config.hpp"
 #include "Context.hpp"
-#include "Fd.hpp"
 #include "File.hpp"
 #include "Logging.hpp"
 #include "Util.hpp"
index 916bea7589aee11dbb0c6aa3daab7a5d4c789008..33e91a3f2dc4900bbe835de7d17f3d314ef8ebcb 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.
 //
@@ -18,8 +18,6 @@
 
 #pragma once
 
-#include "Fd.hpp"
-
 #include <core/Result.hpp>
 
 #include <functional>
index 83993331bfd84e20b4971f8b18e5a9a95411200c..0f48c8ecb49bec5826259ac6673f15ea3d43c4f4 100644 (file)
@@ -29,6 +29,7 @@
 #include <core/exceptions.hpp>
 #include <fmtmacros.hpp>
 #include <util/DirEntry.hpp>
+#include <util/Fd.hpp>
 #include <util/expected.hpp>
 #include <util/file.hpp>
 #include <util/path.hpp>
@@ -205,7 +206,8 @@ ResultRetriever::write_dependency_file(const std::string& path,
 {
   ASSERT(m_ctx.args_info.dependency_target);
 
-  Fd fd(open(path.c_str(), O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666));
+  util::Fd fd(
+    open(path.c_str(), O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666));
   if (!fd) {
     throw WriteError(FMT("Failed to open {} for writing", path));
   }
index fb16857c7be1aeba007f5c247614f408b2342539..5632a19b5d52989d62c8a521569fe42e966a7296 100644 (file)
@@ -18,8 +18,6 @@
 
 #pragma once
 
-#include "Fd.hpp"
-
 #include <Hash.hpp>
 #include <core/Result.hpp>
 #include <core/exceptions.hpp>
index b7f67a9857eba236856c9e01d3ff66c8ceb44eaa..02341b47f96e1f04de67e356a74e3d3815d697e0 100644 (file)
@@ -19,7 +19,6 @@
 #include "mainoptions.hpp"
 
 #include <Config.hpp>
-#include <Fd.hpp>
 #include <File.hpp>
 #include <Hash.hpp>
 #include <InodeCache.hpp>
@@ -41,6 +40,7 @@
 #include <fmtmacros.hpp>
 #include <storage/Storage.hpp>
 #include <storage/local/LocalStorage.hpp>
+#include <util/Fd.hpp>
 #include <util/TemporaryFile.hpp>
 #include <util/TextTable.hpp>
 #include <util/UmaskScope.hpp>
@@ -571,7 +571,7 @@ process_main_options(int argc, const char* const* argv)
 
     case CHECKSUM_FILE: {
       util::XXH3_128 checksum;
-      Fd fd(arg == "-" ? STDIN_FILENO : open(arg.c_str(), O_RDONLY));
+      util::Fd fd(arg == "-" ? STDIN_FILENO : open(arg.c_str(), O_RDONLY));
       if (fd) {
         util::read_fd(*fd, [&checksum](auto data) { checksum.update(data); });
         const auto digest = checksum.digest();
index 37dba2c2f744049401b0123e02c6006c9e3ab961..63f64a51c152840dee049b6d8632f54adee14c86 100644 (file)
@@ -21,7 +21,6 @@
 
 #include "Config.hpp"
 #include "Context.hpp"
-#include "Fd.hpp"
 #include "Logging.hpp"
 #include "SignalHandler.hpp"
 #include "Util.hpp"
@@ -31,6 +30,7 @@
 #include <core/exceptions.hpp>
 #include <fmtmacros.hpp>
 #include <util/DirEntry.hpp>
+#include <util/Fd.hpp>
 #include <util/TemporaryFile.hpp>
 #include <util/expected.hpp>
 #include <util/file.hpp>
@@ -64,7 +64,10 @@ static int win32execute(const char* path,
                         const std::string& temp_dir);
 
 int
-execute(Context& ctx, const char* const* argv, Fd&& fd_out, Fd&& fd_err)
+execute(Context& ctx,
+        const char* const* argv,
+        util::Fd&& fd_out,
+        util::Fd&& fd_err)
 {
   LOG("Executing {}", util::format_argv_for_logging(argv));
 
@@ -289,7 +292,10 @@ win32execute(const char* path,
 // Execute a compiler backend, capturing all output to the given paths the full
 // path to the compiler to run is in argv[0].
 int
-execute(Context& ctx, const char* const* argv, Fd&& fd_out, Fd&& fd_err)
+execute(Context& ctx,
+        const char* const* argv,
+        util::Fd&& fd_out,
+        util::Fd&& fd_err)
 {
   LOG("Executing {}", util::format_argv_for_logging(argv));
 
index 5d0527dfbb8d4142a7ce472b3da6e898f7880fe6..df5668e31cdceefe903b54070ee8280d9bed2d23 100644 (file)
@@ -18,7 +18,7 @@
 
 #pragma once
 
-#include "Fd.hpp"
+#include <util/Fd.hpp>
 
 #include <filesystem>
 #include <optional>
 
 class Context;
 
-int execute(Context& ctx, const char* const* argv, Fd&& fd_out, Fd&& fd_err);
+int execute(Context& ctx,
+            const char* const* argv,
+            util::Fd&& fd_out,
+            util::Fd&& fd_err);
 
 void execute_noreturn(const char* const* argv, const std::string& temp_dir);
 
index f0cd9b36aba171ef5a1604e1802eb1a914fc6beb..fea093cdb83c20b7587ebd8b0232ff707b5f45b5 100644 (file)
@@ -244,12 +244,12 @@ static void
 clone_file(const std::string& src, const std::string& dest, bool via_tmp_file)
 {
 #  if defined(__linux__)
-  Fd src_fd(open(src.c_str(), O_RDONLY));
+  util::Fd src_fd(open(src.c_str(), O_RDONLY));
   if (!src_fd) {
     throw core::Error(FMT("{}: {}", src, strerror(errno)));
   }
 
-  Fd dest_fd;
+  util::Fd dest_fd;
   std::string tmp_file;
   if (via_tmp_file) {
     auto temp_file =
@@ -257,8 +257,8 @@ clone_file(const std::string& src, const std::string& dest, bool via_tmp_file)
     dest_fd = std::move(temp_file.fd);
     tmp_file = std::move(temp_file.path);
   } else {
-    dest_fd =
-      Fd(open(dest.c_str(), O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666));
+    dest_fd = util::Fd(
+      open(dest.c_str(), O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666));
     if (!dest_fd) {
       throw core::Error(FMT("{}: {}", src, strerror(errno)));
     }
similarity index 84%
rename from src/Fd.hpp
rename to src/util/Fd.hpp
index 3a47278c03714b299576e548010d384c64f43567..14d5aa27feae241150c3aec5ee375e2439d7cbe7 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2020-2021 Joel Rosdahl and other contributors
+// Copyright (C) 2020-2023 Joel Rosdahl and other contributors
 //
 // See doc/AUTHORS.adoc for a complete list of contributors.
 //
 
 #pragma once
 
-#include "NonCopyable.hpp"
-#include "assertions.hpp"
+#include <NonCopyable.hpp>
+#include <assertions.hpp>
+#include <util/wincompat.hpp>
+
+#ifdef HAVE_UNISTD_H
+#  include <unistd.h>
+#endif
+
+namespace util {
 
 class Fd : NonCopyable
 {
@@ -85,6 +92,12 @@ Fd::operator=(Fd&& other_fd) noexcept
   return *this;
 }
 
+inline bool
+Fd::close()
+{
+  return m_fd != -1 && ::close(release()) == 0;
+}
+
 inline int
 Fd::release()
 {
@@ -92,3 +105,5 @@ Fd::release()
   m_fd = -1;
   return fd;
 }
+
+} // namespace util
index f7d36ba00fc66a27128b7040094f3fb3d52416ae..8e121c5cb8e329790153726e7ffc74cc37d05b34 100644 (file)
@@ -18,7 +18,7 @@
 
 #pragma once
 
-#include <Fd.hpp>
+#include <util/Fd.hpp>
 
 #include <third_party/tl/expected.hpp>
 
index 220c7b8194e7250dbf2d3bedf2a1184fad0af6e1..8099a102277cd6dc90d26ac9ad5516ac1f87afc9 100644 (file)
 
 #include "file.hpp"
 
-#include <Fd.hpp>
 #include <Finalizer.hpp>
 #include <Logging.hpp>
 #include <Win32Util.hpp>
 #include <fmtmacros.hpp>
 #include <util/Bytes.hpp>
 #include <util/DirEntry.hpp>
+#include <util/Fd.hpp>
 #include <util/TemporaryFile.hpp>
 #include <util/expected.hpp>
 #include <util/file.hpp>
index c5bbf796ec91375ba179c972b4bcf1a772473723..12980ee779c0faf311bfdfb13c83793fdbad3628 100644 (file)
@@ -22,7 +22,7 @@
 #include "../src/InodeCache.hpp"
 #include "TestUtil.hpp"
 
-#include <Fd.hpp>
+#include <util/Fd.hpp>
 #include <util/file.hpp>
 #include <util/path.hpp>
 
@@ -39,7 +39,7 @@ namespace {
 bool
 inode_cache_available()
 {
-  Fd fd(open(util::actual_cwd().c_str(), O_RDONLY));
+  util::Fd fd(open(util::actual_cwd().c_str(), O_RDONLY));
   return fd && InodeCache::available(*fd);
 }
 
index f0c355bb19611414108208c5bf20c2098459fde7..da77138046fdd32b0a3e233b810956d269ab7be0 100644 (file)
@@ -17,7 +17,6 @@
 // Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
 #include "../src/Config.hpp"
-#include "../src/Fd.hpp"
 #include "../src/Util.hpp"
 #include "../src/fmtmacros.hpp"
 #include "TestUtil.hpp"
index 6cda0f9ce06984261a3fe463c7a386f5d8e65d0a..30aaa7fe492f9aae17c46936c84d9aeac052627c 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.
 //
 // this program; if not, write to the Free Software Foundation, Inc., 51
 // Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
-#include "../src/Fd.hpp"
 #include "../src/Finalizer.hpp"
 #include "TestUtil.hpp"
 
+#include <util/Fd.hpp>
 #include <util/wincompat.hpp>
 
 #include "third_party/doctest.h"
@@ -33,6 +33,7 @@
 #include <utility>
 
 using TestUtil::TestContext;
+using util::Fd;
 
 namespace {
 
index ed78dff2e8202360c676e4fa2588a38f80d17bac..4af0b07586f1f5e1f95148d4447551415f4c8082 100644 (file)
 
 #include "TestUtil.hpp"
 
-#include <Fd.hpp>
 #include <fmtmacros.hpp>
 #include <util/Bytes.hpp>
 #include <util/DirEntry.hpp>
+#include <util/Fd.hpp>
 #include <util/file.hpp>
 #include <util/filesystem.hpp>
 #include <util/string.hpp>
@@ -46,15 +46,16 @@ TEST_CASE("util::fallocate")
 
   const char filename[] = "test-file";
 
-  CHECK(util::fallocate(Fd(creat(filename, S_IRUSR | S_IWUSR)).get(), 10000));
+  CHECK(
+    util::fallocate(util::Fd(creat(filename, S_IRUSR | S_IWUSR)).get(), 10000));
   CHECK(DirEntry(filename).size() == 10000);
 
-  CHECK(
-    util::fallocate(Fd(open(filename, O_RDWR, S_IRUSR | S_IWUSR)).get(), 5000));
+  CHECK(util::fallocate(
+    util::Fd(open(filename, O_RDWR, S_IRUSR | S_IWUSR)).get(), 5000));
   CHECK(DirEntry(filename).size() == 10000);
 
-  CHECK(util::fallocate(Fd(open(filename, O_RDWR, S_IRUSR | S_IWUSR)).get(),
-                        20000));
+  CHECK(util::fallocate(
+    util::Fd(open(filename, O_RDWR, S_IRUSR | S_IWUSR)).get(), 20000));
   CHECK(DirEntry(filename).size() == 20000);
 }