gix_lock/
lib.rs

1//! git-style registered lock files to make altering resources atomic.
2//!
3//! In this model, reads are always atomic and can be performed directly while writes are facilitated by the locking mechanism
4//! implemented here. Locks are acquired atomically, then written to, to finally atomically overwrite the actual resource.
5//!
6//! Lock files are wrapped [`gix-tempfile`](gix_tempfile)-handles and add the following:
7//!
8//! * consistent naming of lock files
9//! * block the thread (with timeout) or fail immediately if a lock cannot be obtained right away
10//! * commit lock files to atomically put them into the location of the originally locked file
11//!
12//! # Limitations
13//!
14//! * [All limitations of `gix-tempfile`](gix_tempfile) apply. **A highlight of such a limitation is resource leakage
15//!   which results in them being permanently locked unless there is user-intervention.**
16//! * As the lock file is separate from the actual resource, locking is merely a convention rather than being enforced.
17#![deny(missing_docs, rust_2018_idioms, unsafe_code)]
18
19use std::path::PathBuf;
20
21pub use gix_tempfile as tempfile;
22use gix_tempfile::handle::{Closed, Writable};
23
24const DOT_LOCK_SUFFIX: &str = ".lock";
25
26///
27pub mod acquire;
28
29pub use gix_utils::backoff;
30///
31pub mod commit;
32
33/// Locks a resource to eventually be overwritten with the content of this file.
34///
35/// Dropping the file without [committing][File::commit] will delete it, leaving the underlying resource unchanged.
36#[must_use = "A File that is immediately dropped doesn't allow resource updates"]
37#[derive(Debug)]
38pub struct File {
39    inner: gix_tempfile::Handle<Writable>,
40    lock_path: PathBuf,
41}
42
43/// Locks a resource to allow related resources to be updated using [files][File].
44///
45/// As opposed to the [File] type this one won't keep the tempfile open for writing and thus consumes no
46/// system resources, nor can it be persisted.
47#[must_use = "A Marker that is immediately dropped doesn't lock a resource meaningfully"]
48#[derive(Debug)]
49pub struct Marker {
50    inner: gix_tempfile::Handle<Closed>,
51    created_from_file: bool,
52    lock_path: PathBuf,
53}
54
55///
56pub mod file;