fd-lock 1.0.0

Advisory cross-platform lock on a file using a file descriptor to it.
Documentation

fd-lock

crates.io version build status downloads docs.rs docs

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.

Examples

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(())
}

Installation

$ cargo add fd-lock

Safety

This crate uses unsafe to interface with libc and winapi. All invariants have been carefully checked, and are manually enforced.

Contributing

Want to join us? Check out our "Contributing" guide and take a look at some of these issues:

References

License

MIT OR Apache-2.0