23 stable releases (3 major)

4.0.4 Mar 10, 2025
4.0.2 Dec 29, 2023
4.0.0 Jun 30, 2023
3.0.13 Jun 29, 2023
1.1.0 Jun 28, 2019

#12 in Filesystem

Download history 172693/week @ 2024-12-06 165378/week @ 2024-12-13 100891/week @ 2024-12-20 96030/week @ 2024-12-27 159310/week @ 2025-01-03 198044/week @ 2025-01-10 173332/week @ 2025-01-17 180143/week @ 2025-01-24 201875/week @ 2025-01-31 204884/week @ 2025-02-07 181345/week @ 2025-02-14 196158/week @ 2025-02-21 195158/week @ 2025-02-28 204250/week @ 2025-03-07 219328/week @ 2025-03-14 183995/week @ 2025-03-21

837,053 downloads per month
Used in 453 crates (43 directly)

MIT/Apache

26KB
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

~2–11MB
~144K SLoC