#file #fd #lock #windows #unix

fd-lock

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

14 stable releases

new 3.0.9 Jan 23, 2023
3.0.8 Nov 9, 2022
3.0.6 Jun 21, 2022
3.0.5 Mar 23, 2022
1.1.0 Jun 28, 2019

#12 in Operating systems

Download history 49207/week @ 2022-10-07 49206/week @ 2022-10-14 50299/week @ 2022-10-21 51280/week @ 2022-10-28 59555/week @ 2022-11-04 56189/week @ 2022-11-11 49703/week @ 2022-11-18 52163/week @ 2022-11-25 56157/week @ 2022-12-02 57206/week @ 2022-12-09 51880/week @ 2022-12-16 32363/week @ 2022-12-23 40043/week @ 2022-12-30 58999/week @ 2023-01-06 56924/week @ 2023-01-13 66847/week @ 2023-01-20

229,602 downloads per month
Used in 256 crates (17 directly)

MIT/Apache

25KB
497 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

~0–7MB
~117K SLoC