#file-descriptor #file-lock #lock-file #file #lock #fd #file-io

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

#11 in Filesystem

Download history 155837/week @ 2024-03-14 164396/week @ 2024-03-21 146318/week @ 2024-03-28 143958/week @ 2024-04-04 148447/week @ 2024-04-11 160293/week @ 2024-04-18 150705/week @ 2024-04-25 152130/week @ 2024-05-02 154750/week @ 2024-05-09 164429/week @ 2024-05-16 146551/week @ 2024-05-23 161428/week @ 2024-05-30 144452/week @ 2024-06-06 158012/week @ 2024-06-13 159745/week @ 2024-06-20 126873/week @ 2024-06-27

620,059 downloads per month
Used in 368 crates (34 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–12MB
~135K SLoC