Expand description
§Simple, Lightweight File Locking
This crate provides an extremely lean implementation of file locking for
#[cfg(unix)] (using the libc crate)
and #[cfg(windows)] (using the
windows-sys crate) systems.
The API is exposed as a single extension trait for std::fs::File called
FileExt. Simply bring the trait into scope, and you can lock files to
your heart’s content:
use std::fs::{ File };
use file_locking::{ FileExt };
let f = File::options()
.create(true)
.write(true)
.open("database.lock")?;
{
let _guard = f.lock_exclusive()?;
// ... do some stuff with the knowledge that nobody else can lock the file ...
}
// the lock is now released and can be taken again by somebody else c:Some unavoidable behavior differences exist between platforms. To summarize:
- Don’t count on file locks to prevent read or write access to a file, but be prepared for them to.
- Don’t lock the same on-disk file through the same
Fileobject more than once.
Detailed differences are documented on the FileExt trait.