Skip to main content

Crate filelocks

Crate filelocks 

Source
Expand description

§filelocks

Platform-native advisory file locking for Unix and Windows, including a backend intended for NFS and CIFS/SMB network file systems.

§Backends

LockBackendUnix mechanismWindowsNetwork filesystems
Flockflock(2)LockFileExLocal Unix only
FcntlOFD locks where supportedLockFileExPreferred backend

See LockBackend::Fcntl for platform support details. This crate does not silently fall back to process-scoped POSIX locks on Unix targets where OFD locks are unavailable.

§Quick start — local files

use std::fs::OpenOptions;
use filelocks::{FileLock, LockKind, LockMode};

let file = OpenOptions::new()
    .read(true).write(true).create(true)
    .open("my.lock")
    ?;

let lock = FileLock::lock(file, LockKind::Exclusive, LockMode::Blocking)?;
// lock released on drop

§Quick start — NFS / CIFS network mounts

use std::fs::OpenOptions;
use filelocks::{LockBackend, LockKind, LockMode, LockOptions};

let file = OpenOptions::new()
    .read(true).write(true).create(true)
    .open("/mnt/nfs/shared.lock")
    ?;

// LockBackend::Fcntl uses OFD locks on supported Unix targets and
// LockFileEx on Windows. Unsupported Unix targets return Error::Unsupported.
let lock = LockOptions::new()
    .backend(LockBackend::Fcntl)
    .lock(file, LockKind::Exclusive, LockMode::Blocking)
    ?;

Structs§

FileLock
An RAII guard that holds a file lock.
LockOptions
Builder for acquiring a FileLock with non-default options.

Enums§

Error
Errors that can occur when acquiring or releasing a file lock.
LockBackend
Which OS locking primitive is used when acquiring a crate::FileLock.
LockKind
The kind of lock to acquire.
LockMode
Whether the locking call should block until the lock is available.

Type Aliases§

Result
The result type used throughout this crate.