Create ".lock" files atomically on any filesystem.
They are called ".lock" files, because they are traditionally named
the same as the file they are referencing with the extension of
The algorithm that is used to create a lock file in an atomic way is as follows:
A unique file is created using
The destination lock file is created using the
linksystem call. This operation is atomic across all filesystems including NFS. The result of this operation is ignored, as success is based on subsequent results.
Delete the temporary file.
The metadata of the destination is retrieved. If this fails, repeat the process.
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.
If the lock file is stale (older than a configured age), delete the existing lock file and retry immediately.
Before retrying, sleep briefly (defaults to 5 seconds).
use dotlock::DotlockOptions; use std::time::Duration; let _lock = DotlockOptions::new() .tries(10) .pause(Duration::from_secs(1)) .create("database.lock").unwrap();
A created ".lock" file.
Options which can be used to configure how a lock file is created.