Expand description
File locking via POSIX advisory record locks.
This functionality has now been added to the Rust standard library with
File::lock and associated methods. As such this create will not see
future updates and should only be used for backwards compatibility with
older versions of Rust.
This crate provides the facility to obtain a write-lock and unlock a file
following the advisory record lock scheme as specified by UNIX IEEE Std 1003.1-2001
(POSIX.1) via fcntl().
§Examples
Please note that the examples use tempfile merely to quickly create a file
which is removed automatically. In the common case, you would want to lock
a file which is known to multiple processes.
use file_locker::FileLock;
use std::io::prelude::*;
use std::io::Result;
fn main() -> Result<()> {
let mut filelock = FileLock::new("myfile.txt")
.blocking(true)
.writeable(true)
.lock()?;
filelock.file.write_all(b"Hello, World!")?;
// Manually unlocking is optional as we unlock on Drop
filelock.unlock()?;
Ok(())
}Structs§
- File
Lock - Represents the actually locked file
- File
Lock Builder - Builder to create
FileLock