file_locking
Type | Name |
---|---|
tree | source |
blob | .gitignore |
blob | Cargo.toml |
blob | LICENSE.md |
blob | README.md |
Simple, Lightweight File Locking
This crate provides an extremely lean implementation of file locking for POSIX and Windows systems.
The locking primitives used are flock()
on POSIX (called through the
libc
crate), and LockFileEx()
/
UnlockFile()
on Windows (called through the
windows-sys
crate).
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:
Portability
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
File
object more than once. - Don't count on file locks working through network shares (NFS, SMB, etc.)
License
This project is made available under the terms of the MIT license. See LICENSE.md
for details.
Contributing
Bug reports, feature requests, and patch submissions are welcome at contact@echowritescode.dev.