1 unstable release

Uses old Rust 2015

0.5.0 Oct 24, 2018

#1499 in Filesystem

Download history 18/week @ 2024-07-20 28/week @ 2024-07-27 33/week @ 2024-08-03 12/week @ 2024-08-10 1/week @ 2024-08-17 6/week @ 2024-08-24 7/week @ 2024-08-31 21/week @ 2024-09-07 22/week @ 2024-09-14 56/week @ 2024-09-21 50/week @ 2024-09-28 38/week @ 2024-10-05 63/week @ 2024-10-12 58/week @ 2024-10-19 34/week @ 2024-10-26 58/week @ 2024-11-02

215 downloads per month

Unlicense

11KB
142 lines

dotlock

Crate Build Status

This crate contains support for creating lock files as are used on various UNIX type systems. This is similar to the lockfile program from procmail or the dotlockfile program from liblockfile.

Documentation

Usage

Add this to your Cargo.toml:

[dependencies]
dotlock = "0"

...and this to your crate root:

extern crate dotlock;

Example

extern crate dotlock;
use std::fs::File;
use std::io::{Write, Read, Seek, SeekFrom};

fn main() {
    let mut lock = dotlock::Dotlock::create("some.file.lock").unwrap();
    writeln!(lock, "Do not touch this file!").unwrap();
}

lib.rs:

Create ".lock" files atomically on any filesystem.

This crate contains support for creating lock files as are used on various UNIX type systems. This is similar to the lockfile program from procmail or the dotlockfile program from liblockfile.

They are called ".lock" files, because they are traditionally named the same as the file they are referencing with the extension of .lock.

The algorithm that is used to create a lock file in an atomic way is as follows:

  1. A unique file is created using tempfile.

  2. The destination lock file is created using the link system call. This operation is atomic across all filesystems including NFS. The result of this operation is ignored, as success is based on subsequent results.

  3. Delete the temporary file.

  4. The metadata of the destination is retrieved. If this fails, repeat the process.

  5. The metadata of the temporary file and the destination lock file are compared. If they are the same file, then we have successfully locked the file. Return the opened file.

  6. If the lock file is stale (older than a configured age), delete the existing lock file and retry immediately.

  7. Before retrying, sleep briefly (defaults to 5 seconds).

Examples

use dotlock::DotlockOptions;
use std::time::Duration;

let _lock = DotlockOptions::new()
    .tries(10)
    .pause(Duration::from_secs(1))
    .create("database.lock").unwrap();

Dependencies

~1.6–9.5MB
~111K SLoC