#file-descriptor #file-lock #file-access #lock #fd #file

fd-lock

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

21 stable releases (3 major)

4.0.2 Dec 29, 2023
4.0.0 Jun 30, 2023
3.0.13 Jun 29, 2023
3.0.11 Mar 29, 2023
1.1.0 Jun 28, 2019

#14 in Filesystem

Download history 154328/week @ 2024-08-02 152663/week @ 2024-08-09 154658/week @ 2024-08-16 159610/week @ 2024-08-23 152117/week @ 2024-08-30 157097/week @ 2024-09-06 146917/week @ 2024-09-13 154883/week @ 2024-09-20 172997/week @ 2024-09-27 167341/week @ 2024-10-04 151932/week @ 2024-10-11 168601/week @ 2024-10-18 164417/week @ 2024-10-25 164785/week @ 2024-11-01 169756/week @ 2024-11-08 144765/week @ 2024-11-15

673,644 downloads per month
Used in 418 crates (36 directly)

MIT/Apache

25KB
503 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 std::io::prelude::*;
use std::fs::File;
use fd_lock::RwLock;

// Lock a file and write to it.
let mut f = RwLock::new(File::open("foo.txt")?);
write!(f.write()?, "chashu cat")?;

// A lock can also be held across multiple operations.
let mut f = f.write()?;
write!(f, "nori cat")?;
write!(f, "bird!")?;

Installation

$ cargo add fd-lock

Safety

This crate uses unsafe on Windows to interface with windows-sys. 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

~1–11MB
~130K SLoC