#mutex #flock

filelock

Cross-platform file locking library for Rust

9 releases (5 breaking)

Uses new Rust 2024

0.5.0 Feb 15, 2026
0.4.2 Dec 4, 2025
0.4.1 Sep 27, 2025
0.3.0 Aug 18, 2022
0.0.1 Sep 6, 2021

#590 in Filesystem


Used in pearls

MIT license

12KB
195 lines

filelock

Rust

Simple filelock library for rust, using flock on Unix-like systems and LockFileEx on Windows under the hood.

Image by Homutan, source: https://www.pixiv.net/artworks/128080460

Installation

$ cargo add filelock

Usage

use filelock;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let mut lock = filelock::new("myfile.lock");
    let _guard = lock.lock()?;

    // Perform critical operations

    // Lock is automatically released when _guard goes out of scope
    Ok(())
}

For manual control:

use filelock;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let mut lock = filelock::new("myfile.lock");
    let guard = lock.lock()?;

    // Perform critical operations

    // Manually unlock with error handling
    guard.unlock()?;

    Ok(())
}

Documentation

See https://docs.rs/filelock/latest/filelock/.

License

Filelock is distributed by a MIT license.

Dependencies

~0–1.8MB
~31K SLoC