]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
refactor: fs::path-ify LockFile
authorJoel Rosdahl <joel@rosdahl.net>
Sat, 5 Aug 2023 09:01:09 +0000 (11:01 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Sat, 5 Aug 2023 09:01:09 +0000 (11:01 +0200)
src/util/LockFile.cpp
src/util/LockFile.hpp
src/util/filesystem.hpp

index 139b17dad554940a59b5cbbe719532cf974d6e6d..eb1fd9eae824ec192d8a2464201cc7b1864cbd43 100644 (file)
@@ -76,10 +76,10 @@ private:
 
 namespace util {
 
-LockFile::LockFile(const std::string& path)
-  : m_lock_file(path + ".lock"),
+LockFile::LockFile(const fs::path& path)
+  : m_lock_file(path.string() + ".lock"),
 #ifndef _WIN32
-    m_alive_file(path + ".alive"),
+    m_alive_file(path.string() + ".alive"),
     m_acquired(false)
 #else
     m_handle(INVALID_HANDLE_VALUE)
@@ -162,8 +162,8 @@ LockFile::release()
   if (m_lock_manager) {
     m_lock_manager->deregister_alive_file(m_alive_file);
   }
-  remove(m_alive_file);
-  remove(m_lock_file);
+  fs::remove(m_alive_file);
+  fs::remove(m_lock_file);
 #else
   CloseHandle(m_handle);
 #endif
@@ -238,7 +238,7 @@ LockFile::do_acquire(const bool blocking)
     const auto my_content =
       FMT("{}-{}.{}", content_prefix, now.sec(), now.nsec_decimal_part());
 
-    if (symlink(my_content.c_str(), m_lock_file.c_str()) == 0) {
+    if (fs::create_symlink(my_content, m_lock_file)) {
       // We got the lock.
       return true;
     }
@@ -246,7 +246,7 @@ LockFile::do_acquire(const bool blocking)
     int saved_errno = errno;
     if (saved_errno == ENOENT) {
       // Directory doesn't exist?
-      if (fs::create_directories(Util::dir_name(m_lock_file))) {
+      if (fs::create_directories(m_lock_file.parent_path())) {
         // OK. Retry.
         continue;
       }
@@ -312,7 +312,7 @@ LockFile::do_acquire(const bool blocking)
           m_lock_file,
           inactive_duration.sec(),
           inactive_duration.nsec_decimal_part() / 1'000'000);
-      if (!remove(m_alive_file) || !remove(m_lock_file)) {
+      if (!fs::remove(m_alive_file) || !fs::remove(m_lock_file)) {
         return false;
       }
 
@@ -363,7 +363,7 @@ LockFile::do_acquire(const bool blocking)
 
   while (true) {
     DWORD flags = FILE_ATTRIBUTE_NORMAL | FILE_FLAG_DELETE_ON_CLOSE;
-    handle = CreateFile(m_lock_file.c_str(),
+    handle = CreateFile(m_lock_file.string().c_str(),
                         GENERIC_WRITE, // desired access
                         0,             // shared mode (0 = not shared)
                         nullptr,       // security attributes
@@ -378,7 +378,7 @@ LockFile::do_acquire(const bool blocking)
     DWORD error = GetLastError();
     if (error == ERROR_PATH_NOT_FOUND) {
       // Directory doesn't exist?
-      if (fs::create_directories(Util::dir_name(m_lock_file))) {
+      if (fs::create_directories(m_lock_file.parent_path())) {
         // OK. Retry.
         continue;
       }
index 0176eca40ca52ef09219072625318b7e7075561c..63daa468166bcee0900f2e4e46f8fdfc2b00a96d 100644 (file)
@@ -22,8 +22,8 @@
 #include <util/LongLivedLockFileManager.hpp>
 #include <util/TimePoint.hpp>
 
+#include <filesystem>
 #include <optional>
-#include <string>
 
 namespace util {
 
@@ -33,7 +33,7 @@ namespace util {
 class LockFile : NonCopyable
 {
 public:
-  explicit LockFile(const std::string& path);
+  explicit LockFile(const std::filesystem::path& path);
   LockFile(LockFile&& other) noexcept;
 
   LockFile& operator=(LockFile&& other) noexcept;
@@ -58,10 +58,10 @@ public:
   bool acquired() const;
 
 private:
-  std::string m_lock_file;
+  std::filesystem::path m_lock_file;
 #ifndef _WIN32
   LongLivedLockFileManager* m_lock_manager = nullptr;
-  std::string m_alive_file;
+  std::filesystem::path m_alive_file;
   bool m_acquired;
 #else
   void* m_handle;
index 49b333622601661a64abba02ce23082a8cf27c1a..edea2c66d6584dd16f91b43a303efdb4de1d196b 100644 (file)
@@ -105,6 +105,7 @@ DEF_WRAP_1_R(canonical,           path,           const path&, p)
 DEF_WRAP_1_R(create_directories,  bool,           const path&, p)
 DEF_WRAP_1_R(create_directory,    bool,           const path&, p)
 DEF_WRAP_2_V(create_hard_link,    void,           const path&, target, const path&, link)
+DEF_WRAP_2_V(create_symlink,      void,           const path&, target, const path&, link)
 DEF_WRAP_0_R(current_path,        path)
 DEF_WRAP_1_V(current_path,        void,           const path&, p)
 DEF_WRAP_1_P(exists,              bool,           const path&, p)