Advisory cross-platform file locks using file descriptors. Adapted from mafintosh/fd-lock.
Note that advisory lock compliance is opt-in, and can freely be ignored by other parties. This means this crate should never be used for security purposes, but solely to coordinate file access.
Basic usage
use fd_lock::FdLock;
use tempfile::tempfile;
use std::io::prelude::*;
use std::fs::File;
fn main() -> Result<(), failure::Error> {
// Lock a file and write to it.
let mut f = FdLock::new(tempfile()?);
f.try_lock()?.write_all(b"chashu cat")?;
// Locks can also be held for extended durations.
let mut f = f.try_lock()?;
f.write_all(b"nori cat")?;
f.write_all(b"bird!")?;
Ok(())
}
$ cargo add fd-lock
This crate uses unsafe
to interface with libc
and winapi
. All invariants
have been carefully checked, and are manually enforced.
Want to join us? Check out our "Contributing" guide and take a look at some of these issues:
MIT OR Apache-2.0