Skip to content

Engine → Core → file_lock.h

file_lock

Syntax

class CONJURE_API file_lock

Constructors

AccessSignatureDescription
publicfile_lock();
publicfile_lock( file_lock &&other) noexcept;

Important

Copy constructor and assignment has been deleted.

Destructors

AccessSignatureDescription
public~file_lock();

Variables

AccessNameTypeDefaultDescription
privateINVALID_NATIVEstatic constexpr std::intptr_t-1
privatemNativestd::intptr_tINVALID_NATIVE
privatemPathstd::filesystem::path{}

Functions

Public

held

[[nodiscard]] bool held() const noexcept;

True while this object holds the lock.

path

[[nodiscard]] const std::filesystem::path &path() const noexcept;

The path given to try_acquire; empty when nothing is held.

release

void release() noexcept;

Releases the lock and closes the file. Safe when nothing is held.

release_and_remove

[[nodiscard]] result<void> release_and_remove();

Deletes the file while still holding it, then releases. Where Windows cannot delete with POSIX semantics (before Windows 10 1809, or on a file system without them such as FAT) the name stays, pending deletion, until every other handle to the file has closed. INVALID_STATE when nothing is held. NOT_FOUND (POSIX) when the path no longer names the held file, which is left alone. On an OS failure the lock is released, the file stays and the error names it. Nothing is held afterwards, whatever the outcome.

try_acquire

[[nodiscard]] static result<file_lock> try_acquire(
    const std::filesystem::path &path,
    file_lock_open open);

Opens (or, with CREATE, creates) path and tries to lock it without waiting. Success with held() == false when another holder has it (Windows: also a foreign handle opened with delete access or whose share mode refuses reading, writing or deletion, or one holding a byte-range lock in the file). Fails with INVALID_ARGUMENT for an empty path, one containing NUL or an open value outside file_lock_open; NOT_FOUND for a missing file under EXISTING, a missing parent folder, or a file removed while it was being locked; PERMISSION_DENIED when access is refused (Windows: mostly also for a file whose deletion is pending), the path is a directory, or (POSIX) a symbolic link; UNSUPPORTED when the file system has no locks (POSIX only; on Windows the share mode alone holds the file and the result is held); IO_FAILURE when the file kept being replaced under CREATE; BACKEND_FAILURE otherwise. Messages name the path (when there is one); those from a failed system call also carry the operating system's text. Never removes a file, on any outcome: a file it created stays when it then fails.

Operator Overloads

Public

operator=

file_lock &operator=(
    file_lock &&other) noexcept;