From: Joel Rosdahl Date: Sat, 5 Aug 2023 09:01:09 +0000 (+0200) Subject: refactor: fs::path-ify LockFile X-Git-Tag: v4.9~65 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=38a342cb2f6ae594a6dc08c33fdbad8d36ef3437;p=thirdparty%2Fccache.git refactor: fs::path-ify LockFile --- diff --git a/src/util/LockFile.cpp b/src/util/LockFile.cpp index 139b17dad..eb1fd9eae 100644 --- a/src/util/LockFile.cpp +++ b/src/util/LockFile.cpp @@ -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; } diff --git a/src/util/LockFile.hpp b/src/util/LockFile.hpp index 0176eca40..63daa4681 100644 --- a/src/util/LockFile.hpp +++ b/src/util/LockFile.hpp @@ -22,8 +22,8 @@ #include #include +#include #include -#include 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; diff --git a/src/util/filesystem.hpp b/src/util/filesystem.hpp index 49b333622..edea2c66d 100644 --- a/src/util/filesystem.hpp +++ b/src/util/filesystem.hpp @@ -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)