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
LockBackend | Unix mechanism | Windows | Network filesystems |
|---|---|---|---|
Flock | flock(2) | LockFileEx | Local Unix only |
Fcntl | OFD locks where supported | LockFileEx | Preferred 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§
- File
Lock - An RAII guard that holds a file lock.
- Lock
Options - Builder for acquiring a
FileLockwith non-default options.
Enums§
- Error
- Errors that can occur when acquiring or releasing a file lock.
- Lock
Backend - Which OS locking primitive is used when acquiring a
crate::FileLock. - Lock
Kind - The kind of lock to acquire.
- Lock
Mode - Whether the locking call should block until the lock is available.
Type Aliases§
- Result
- The result type used throughout this crate.