#file #fd #lock #windows #unix

fd-lock

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

5 stable releases

3.0.0 Jul 17, 2021
2.0.0 Dec 21, 2020
1.1.1 Jan 9, 2020
1.1.0 Jun 28, 2019
1.0.0 Jun 28, 2019

#36 in Filesystem

Download history 586/week @ 2021-04-09 600/week @ 2021-04-16 567/week @ 2021-04-23 1137/week @ 2021-04-30 1157/week @ 2021-05-07 3529/week @ 2021-05-14 9092/week @ 2021-05-21 8394/week @ 2021-05-28 8790/week @ 2021-06-04 9012/week @ 2021-06-11 10009/week @ 2021-06-18 9215/week @ 2021-06-25 7374/week @ 2021-07-02 9370/week @ 2021-07-09 9791/week @ 2021-07-16 7016/week @ 2021-07-23

26,667 downloads per month
Used in 55 crates (7 directly)

MIT/Apache

25KB
501 lines

fd-lock

crates.io version 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::RwLock;
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 = RwLock::new(tempfile()?);
    f.write()?.write_all(b"chashu cat")?;

    // A lock can also be held across multiple operations.
    let mut f = f.write()?;
    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

Dependencies

~59KB