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)
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
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;
}
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;
}
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;
}
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
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;
}
#include <util/LongLivedLockFileManager.hpp>
#include <util/TimePoint.hpp>
+#include <filesystem>
#include <optional>
-#include <string>
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;
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;
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)