Skip to content

Commit

Permalink
Merge pull request #1915 from Klaim/klaim/lockfile-no-pid-check
Browse files Browse the repository at this point in the history
Lockfiles don't rely on PID (#1834)
  • Loading branch information
wolfv authored Oct 4, 2022
2 parents 30a3ca4 + 77bb95b commit 4faa748
Show file tree
Hide file tree
Showing 4 changed files with 282 additions and 217 deletions.
11 changes: 11 additions & 0 deletions libmamba/include/mamba/core/mamba_fs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1323,5 +1323,16 @@ namespace fs

}

template <>
struct std::hash<::fs::u8path>
{
std::size_t operator()(const ::fs::u8path& path) const noexcept
{
return std::filesystem::hash_value(
path.std_path()); // TODO: once we stop using gcc < 12 we can properly use
// std::hash<std::filesystem::path>{}(path.std_path());
}
};


#endif
42 changes: 16 additions & 26 deletions libmamba/include/mamba/core/util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,18 @@ namespace mamba

const std::size_t MAMBA_LOCK_POS = 21;

class LockFileOwner;

class LockFile
{
public:
~LockFile();

LockFile(const LockFile&) = delete;
LockFile& operator=(const LockFile&) = delete;
LockFile& operator=(LockFile&&) = default;

LockFile(LockFile&&);
LockFile& operator=(LockFile&&);

static std::unique_ptr<LockFile> create_lock(const fs::u8path& path);
static std::unique_ptr<LockFile> try_lock(const fs::u8path& path) noexcept;
Expand All @@ -124,35 +128,21 @@ namespace mamba
// Opening a new file descriptor on Unix would clear locks
static bool is_locked(int fd);
#endif
static int read_pid(int fd);

private:
LockFile(const fs::u8path& path);
LockFile(const fs::u8path& path, const std::chrono::seconds& timeout);

fs::u8path m_path;
fs::u8path m_lock;
std::chrono::seconds m_timeout;
int m_fd = -1;
bool m_locked;
bool m_lockfile_existed;

#if defined(__APPLE__) || defined(__linux__)
pid_t m_pid;
static bool is_locked(const LockFile& lockfile)
{
#ifdef _WIN32
return is_locked(lockfile.lockfile_path());
#else
int m_pid;
// Opening a new file descriptor on Unix would clear locks
return is_locked(lockfile.fd());
#endif
int read_pid() const;
bool write_pid(int pid) const;

bool set_fd_lock(bool blocking) const;
bool lock_non_blocking();
bool lock_blocking();
bool lock(int pid, bool blocking) const;
}

void remove_lockfile() noexcept;
int close_fd();
bool unlock();
private:
LockFile(const fs::u8path& path);
LockFile(const fs::u8path& path, const std::chrono::seconds& timeout);
std::shared_ptr<LockFileOwner> impl;
};


Expand Down
Loading

0 comments on commit 4faa748

Please sign in to comment.