#file-lock #windows #unix #file

fd-lock

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

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

#4 in Windows APIs

Download history 429278/week @ 2025-09-18 470184/week @ 2025-09-25 434991/week @ 2025-10-02 417118/week @ 2025-10-09 458256/week @ 2025-10-16 497930/week @ 2025-10-23 495560/week @ 2025-10-30 470843/week @ 2025-11-06 467759/week @ 2025-11-13 579201/week @ 2025-11-20 322377/week @ 2025-11-27 378195/week @ 2025-12-04 366876/week @ 2025-12-11 309399/week @ 2025-12-18 180648/week @ 2025-12-25 301696/week @ 2026-01-01

1,226,883 downloads per month
Used in 598 crates (51 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–15MB
~159K SLoC