diff --git a/repertory/librepertory/include/platform/unix_platform.hpp b/repertory/librepertory/include/platform/unix_platform.hpp index 158be5ad..23eaf714 100644 --- a/repertory/librepertory/include/platform/unix_platform.hpp +++ b/repertory/librepertory/include/platform/unix_platform.hpp @@ -41,7 +41,7 @@ private: const std::string unique_id_; const std::string mutex_id_; int lock_fd_; - int lock_status_ = EWOULDBLOCK; + int lock_status_{EWOULDBLOCK}; private: [[nodiscard]] static auto get_state_directory() -> std::string; @@ -51,8 +51,11 @@ private: [[nodiscard]] auto get_lock_file() -> std::string; private: - [[nodiscard]] static auto - wait_for_lock(int fd, std::uint8_t retry_count = 30u) -> int; + void release(); + + [[nodiscard]] static auto wait_for_lock(int fd, + std::uint8_t retry_count = 30u) + -> int; public: [[nodiscard]] auto get_mount_state(json &mount_state) -> bool; @@ -60,8 +63,8 @@ public: [[nodiscard]] auto grab_lock(std::uint8_t retry_count = 30u) -> lock_result; [[nodiscard]] auto set_mount_state(bool active, - const std::string &mount_location, - int pid) -> bool; + const std::string &mount_location, int pid) + -> bool; }; [[nodiscard]] auto create_meta_attributes( diff --git a/repertory/librepertory/src/platform/unix_platform.cpp b/repertory/librepertory/src/platform/unix_platform.cpp index 13ff360b..74358e8a 100644 --- a/repertory/librepertory/src/platform/unix_platform.cpp +++ b/repertory/librepertory/src/platform/unix_platform.cpp @@ -47,16 +47,7 @@ lock_data::lock_data(const provider_type &pt, std::string unique_id /*= ""*/) lock_data::lock_data() : pt_(provider_type::sia), unique_id_(""), mutex_id_(""), lock_fd_(-1) {} -lock_data::~lock_data() { - if (lock_fd_ != -1) { - if (lock_status_ == 0) { - unlink(get_lock_file().c_str()); - flock(lock_fd_, LOCK_UN); - } - - close(lock_fd_); - } -} +lock_data::~lock_data() { release(); } auto lock_data::get_lock_data_file() -> std::string { const auto dir = get_state_directory(); @@ -125,6 +116,20 @@ auto lock_data::grab_lock(std::uint8_t retry_count) -> lock_result { } } +void lock_data::release() { + if (lock_fd_ == -1) { + return; + } + + if (lock_status_ == 0) { + unlink(get_lock_file().c_str()); + flock(lock_fd_, LOCK_UN); + } + + close(lock_fd_); + lock_fd_ = -1; +} + auto lock_data::set_mount_state(bool active, const std::string &mount_location, int pid) -> bool { REPERTORY_USES_FUNCTION_NAME();