]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
refactor: fs::path-ify TemporaryFile
authorJoel Rosdahl <joel@rosdahl.net>
Fri, 4 Aug 2023 05:57:25 +0000 (07:57 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Fri, 4 Aug 2023 15:03:07 +0000 (17:03 +0200)
src/MiniTrace.cpp
src/TemporaryFile.cpp
src/TemporaryFile.hpp
src/storage/local/LocalStorage.cpp

index 3e7e6e320c98a6563bbbe3083d82387fcbec7e08..ca65a90ab880a28d56a2aebdbe45f91a5f4b420e 100644 (file)
@@ -41,7 +41,7 @@ MiniTrace::MiniTrace(const ArgsInfo& args_info)
   if (!tmp_dir) {
     tmp_dir = "/tmp";
   }
-  TemporaryFile tmp_file((*tmp_dir / "ccache-trace").string());
+  TemporaryFile tmp_file(*tmp_dir / "ccache-trace");
   m_tmp_trace_file = tmp_file.path;
 
   mtr_init(m_tmp_trace_file.c_str());
index e82c8d3d1c9e9c0d9a4546156bc414194d28d558..c933fd2e9a7480b1dfd3a4c017b863d0d2533999 100644 (file)
@@ -24,6 +24,7 @@
 #include <core/exceptions.hpp>
 #include <fmtmacros.hpp>
 #include <util/file.hpp>
+#include <util/filesystem.hpp>
 #include <util/process.hpp>
 
 #include <cstdlib>
 #  include "third_party/win32/mktemp.h"
 #endif
 
-TemporaryFile::TemporaryFile(std::string_view path_prefix,
+namespace fs = util::filesystem;
+
+TemporaryFile::TemporaryFile(const fs::path& path_prefix,
                              std::string_view suffix)
-  : path(FMT("{}{}XXXXXX{}", path_prefix, tmp_file_infix, suffix))
 {
-  core::ensure_dir_exists(Util::dir_name(path));
+  if (path_prefix.has_parent_path()) {
+    core::ensure_dir_exists(path_prefix.parent_path());
+  }
+  std::string path_template =
+    FMT("{}{}XXXXXX{}", path_prefix, tmp_file_infix, suffix);
 #ifdef _WIN32
   // MSVC lacks mkstemps() and Mingw-w64's implementation[1] is problematic, as
   // it can reuse the names of recently-deleted files unless the caller
@@ -48,14 +54,15 @@ TemporaryFile::TemporaryFile(std::string_view path_prefix,
 
   // [1]: <https://github.com/Alexpux/mingw-w64/blob/
   // d0d7f784833bbb0b2d279310ddc6afb52fe47a46/mingw-w64-crt/misc/mkstemp.c>
-  fd = Fd(bsd_mkstemps(&path[0], suffix.length()));
+  fd = Fd(bsd_mkstemps(&path_template[0], suffix.length()));
 #else
-  fd = Fd(mkstemps(&path[0], suffix.length()));
+  fd = Fd(mkstemps(&path_template[0], suffix.length()));
 #endif
   if (!fd) {
     throw core::Fatal(
       FMT("Failed to create temporary file for {}: {}", path, strerror(errno)));
   }
+  path = path_template;
 
   util::set_cloexec_flag(*fd);
 #ifndef _WIN32
@@ -64,7 +71,7 @@ TemporaryFile::TemporaryFile(std::string_view path_prefix,
 }
 
 bool
-TemporaryFile::is_tmp_file(std::string_view path)
+TemporaryFile::is_tmp_file(const fs::path& path)
 {
-  return Util::base_name(path).find(tmp_file_infix) != std::string::npos;
+  return path.filename().string().find(tmp_file_infix) != std::string::npos;
 }
index b773c0a4b203c19a1f2193dcc6c19b66c5d5fff5..089d92c900ed86a648fe98b9ae127c880e9d8e86 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,9 @@
 
 #pragma once
 
-#include "Fd.hpp"
+#include <Fd.hpp>
 
+#include <filesystem>
 #include <string>
 #include <string_view>
 
@@ -33,17 +34,18 @@ public:
   // `path_prefix` is the base path. The resulting filename will be this path
   // plus a unique string plus `suffix`. If `path_prefix` refers to a
   // nonexistent directory the directory will be created if possible.
-  TemporaryFile(std::string_view path_prefix, std::string_view suffix = ".tmp");
+  TemporaryFile(const std::filesystem::path& path_prefix,
+                std::string_view suffix = ".tmp");
 
   TemporaryFile(TemporaryFile&& other) noexcept = default;
 
   TemporaryFile& operator=(TemporaryFile&& other) noexcept = default;
 
-  static bool is_tmp_file(std::string_view path);
+  static bool is_tmp_file(const std::filesystem::path& path);
 
   // The resulting open file descriptor in read/write mode. Unset on error.
   Fd fd;
 
   // The actual filename. Empty on error.
-  std::string path;
+  std::filesystem::path path;
 };
index e97ac996adfcf9c41ff6118a68846b838f08c5fd..55640f93d946353e3c8ac7f0b89c74a66b69f236 100644 (file)
@@ -332,7 +332,7 @@ clean_dir(
 
     // Delete any tmp files older than 1 hour right away.
     if (file.mtime() + util::Duration(3600) < current_time
-        && TemporaryFile::is_tmp_file(file.path().string())) {
+        && TemporaryFile::is_tmp_file(file.path())) {
       util::remove(file.path().string());
       continue;
     }
@@ -893,7 +893,7 @@ LocalStorage::recompress(const std::optional<int8_t> level,
                   incompressible_size += file.size_on_disk();
                 }
               });
-            } else if (!TemporaryFile::is_tmp_file(file.path().string())) {
+            } else if (!TemporaryFile::is_tmp_file(file.path())) {
               incompressible_size += file.size_on_disk();
             }