Debian Codegraph @

src/lib.rs

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 //! Advisory lock provides simple and convenient API for using file locks.
2 //!
3 //! These are called advisory because they don't prevent other processes from
4 //! accessing the files directly, bypassing the locks.
5 //! However, if multiple processes agree on acquiring file locks, they should
6 //! work as expected.
7 //!
8 //! The main entity of the crate is [`AdvisoryFileLock`] which is effectively
9 //! a [`RwLock`] but for [`File`].
10 //!
11 //! Example:
12 //! ```
13 //! use std::fs::File;
14 //! use advisory_lock::{AdvisoryFileLock, FileLockMode, FileLockError};
15 //! #
16 //! #
17 //! // Create the file and obtain its exclusive advisory lock
18 //! let exclusive_file = File::create("foo.txt").unwrap();
•19 //! AdvisoryFileLock::lock(&exclusive_file, FileLockMode::Exclusive)?;
20 //!
21 //! let shared_file = File::open("foo.txt")?;
22 //!
23 //! // Try to acquire the lock in non-blocking way
•24 //! assert!(matches!(AdvisoryFileLock::try_lock(&shared_file, FileLockMode::Shared), Err(FileLockError::AlreadyLocked)));
25 //!
•26 //! AdvisoryFileLock::unlock(&exclusive_file)?;
27 //!
•28 //! AdvisoryFileLock::try_lock(&shared_file, FileLockMode::Shared).expect("Works, because the exclusive lock was released");
29 //!
30 //! let shared_file_2 = File::open("foo.txt")?;
31 //!
•32 //! AdvisoryFileLock::lock(&shared_file_2, FileLockMode::Shared).expect("Should be fine to have multiple shared locks");
33 //!
34 //! // Nope, now we have to wait until all shared locks are released...
•35 //! assert!(matches!(AdvisoryFileLock::try_lock(&exclusive_file, FileLockMode::Exclusive), Err(FileLockError::AlreadyLocked)));
36 //!
37 //! // We can unlock them explicitly and handle the potential error
•38 //! AdvisoryFileLock::unlock(&shared_file)?;
39 //! // Or drop the lock, such that we `log::error!()` if it happens and discard it
40 //! drop(shared_file_2);
41 //!
•42 //! AdvisoryFileLock::lock(&exclusive_file, FileLockMode::Exclusive).expect("All other locks should have been released");
43 //! #
44 //! # std::fs::remove_file("foo.txt")?;
45 //! # Ok::<(), Box<dyn std::error::Error>>(())
46 //! ```
47 //!
48 //! [`AdvisoryFileLock`]: struct.AdvisoryFileLock.html
49 //! [`RwLock`]: https://doc.rust-lang.org/stable/std/sync/struct.RwLock.html
50 //! [`File`]: https://doc.rust-lang.org/stable/std/fs/struct.File.html
51 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::{fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/, iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/};
52
53 #[cfg(windows)]
54 mod windows;
55
56 #[cfg(unix)]
57 mod unixrust-analyzer cargo advisory-lock 0.3.0 unix/;
58
59 /// An enumeration of possible errors which can occur while trying to acquire a lock.
60 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:)]
61 pub enum FileLockErrorrust-analyzer cargo advisory-lock 0.3.0 FileLockError# {
62 /// The file is already locked by other process.
63 AlreadyLockedrust-analyzer cargo advisory-lock 0.3.0 FileLockError#AlreadyLocked#,
64 /// The error occurred during I/O operations.
65 Iorust-analyzer cargo advisory-lock 0.3.0 FileLockError#Io#(iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#),
66 }
67
68 impl fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Displayrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Display# for FileLockErrorrust-analyzer cargo advisory-lock 0.3.0 FileLockError# {
69 fn fmtrust-analyzer cargo advisory-lock 0.3.0 impl#[FileLockError][Display]fmt().(&self, f: &mut fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Formatterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Formatter#<'_>) -> fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Result# {
70 match self {
71 FileLockErrorrust-analyzer cargo advisory-lock 0.3.0 FileLockError#::AlreadyLockedrust-analyzer cargo advisory-lock 0.3.0 FileLockError#AlreadyLocked# => f.write_strrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/impl#[`Formatter<'a>`]write_str().("the file is already locked"),
72 FileLockErrorrust-analyzer cargo advisory-lock 0.3.0 FileLockError#::Iorust-analyzer cargo advisory-lock 0.3.0 FileLockError#Io#(err) => writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(f, "I/O error: {}", err),
73 }
74 }
75 }
76
77 impl stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std error/::Errorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core error/Error# for FileLockErrorrust-analyzer cargo advisory-lock 0.3.0 FileLockError# {}
78
79 /// An enumeration of types which represents how to acquire an advisory lock.
80 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Copy:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:, Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:)]
81 pub enum FileLockModerust-analyzer cargo advisory-lock 0.3.0 FileLockMode# {
82 /// Obtain an exclusive file lock.
83 Exclusiverust-analyzer cargo advisory-lock 0.3.0 FileLockMode#Exclusive#,
84 /// Obtain a shared file lock.
85 Sharedrust-analyzer cargo advisory-lock 0.3.0 FileLockMode#Shared#,
86 }
87
88 /// An advisory lock for files.
89 ///
90 /// An advisory lock provides a mutual-exclusion mechanism among processes which explicitly
91 /// acquires and releases the lock. Processes that are not aware of the lock will ignore it.
92 ///
93 /// `AdvisoryFileLock` provides following features:
94 /// - Blocking or non-blocking operations.
95 /// - Shared or exclusive modes.
96 /// - All operations are thread-safe.
97 ///
98 /// ## Notes
99 ///
100 /// `AdvisoryFileLock` has following limitations:
101 /// - Locks are allowed only on files, but not directories.
102 pub trait AdvisoryFileLockrust-analyzer cargo advisory-lock 0.3.0 AdvisoryFileLock# {
103 /// Acquire the advisory file lock.
104 ///
105 /// `lock` is blocking; it will block the current thread until it succeeds or errors.
106 fn lockrust-analyzer cargo advisory-lock 0.3.0 AdvisoryFileLock#lock().(&self, file_lock_mode: FileLockModerust-analyzer cargo advisory-lock 0.3.0 FileLockMode#) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), FileLockErrorrust-analyzer cargo advisory-lock 0.3.0 FileLockError#>;
107 /// Try to acquire the advisory file lock.
108 ///
109 /// `try_lock` returns immediately.
110 fn try_lockrust-analyzer cargo advisory-lock 0.3.0 AdvisoryFileLock#try_lock().(&self, file_lock_mode: FileLockModerust-analyzer cargo advisory-lock 0.3.0 FileLockMode#) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), FileLockErrorrust-analyzer cargo advisory-lock 0.3.0 FileLockError#>;
111 /// Unlock this advisory file lock.
112 fn unlockrust-analyzer cargo advisory-lock 0.3.0 AdvisoryFileLock#unlock().(&self) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), FileLockErrorrust-analyzer cargo advisory-lock 0.3.0 FileLockError#>;
113 }
114
115 #[cfg(test)]
116 mod testsrust-analyzer cargo advisory-lock 0.3.0 tests/ {
117 use superrust-analyzer cargo advisory-lock 0.3.0 crate/::*;
118 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::envrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/::temp_dirrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/temp_dir().;
119 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/::Filerust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/File#;
120
121 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
122 fn simple_shared_lockrust-analyzer cargo advisory-lock 0.3.0 tests/simple_shared_lock().() {
123 let mut test_file = temp_dirrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/temp_dir().();
124 test_file.pushrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[PathBuf]push().("shared_lock");
125 Filerust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/File#::createrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[File]create().(&test_file).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
126 {
127 let f1 = Filerust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/File#::openrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[File]open().(&test_file).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
•128 AdvisoryFileLockrust-analyzer cargo advisory-lock 0.3.0 AdvisoryFileLock#::lockrust-analyzer cargo advisory-lock 0.3.0 unix/impl#[File][AdvisoryFileLock]lock().(&f1, FileLockModerust-analyzer cargo advisory-lock 0.3.0 FileLockMode#::Sharedrust-analyzer cargo advisory-lock 0.3.0 FileLockMode#Shared#).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
129 let f2 = Filerust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/File#::openrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[File]open().(&test_file).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
•130 AdvisoryFileLockrust-analyzer cargo advisory-lock 0.3.0 AdvisoryFileLock#::lockrust-analyzer cargo advisory-lock 0.3.0 unix/impl#[File][AdvisoryFileLock]lock().(&f2, FileLockModerust-analyzer cargo advisory-lock 0.3.0 FileLockMode#::Sharedrust-analyzer cargo advisory-lock 0.3.0 FileLockMode#Shared#).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
131 }
132 stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/::remove_filerust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/remove_file().(&test_file).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
133 }
134
135 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
136 fn simple_exclusive_lockrust-analyzer cargo advisory-lock 0.3.0 tests/simple_exclusive_lock().() {
137 let mut test_file = temp_dirrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/temp_dir().();
138 test_file.pushrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[PathBuf]push().("exclusive_lock");
139 Filerust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/File#::createrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[File]create().(&test_file).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
140 {
141 let f1 = Filerust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/File#::openrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[File]open().(&test_file).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
•142 AdvisoryFileLockrust-analyzer cargo advisory-lock 0.3.0 AdvisoryFileLock#::lockrust-analyzer cargo advisory-lock 0.3.0 unix/impl#[File][AdvisoryFileLock]lock().(&f1, FileLockModerust-analyzer cargo advisory-lock 0.3.0 FileLockMode#::Exclusiverust-analyzer cargo advisory-lock 0.3.0 FileLockMode#Exclusive#).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
143 let f2 = Filerust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/File#::openrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[File]open().(&test_file).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
•144 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(AdvisoryFileLockrust-analyzer cargo advisory-lock 0.3.0 AdvisoryFileLock#::try_lockrust-analyzer cargo advisory-lock 0.3.0 unix/impl#[File][AdvisoryFileLock]try_lock().(&f2, FileLockModerust-analyzer cargo advisory-lock 0.3.0 FileLockMode#::Exclusiverust-analyzer cargo advisory-lock 0.3.0 FileLockMode#Exclusive#).is_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_err().());
145 }
146 stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/::remove_filerust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/remove_file().(&test_file).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
147 }
148
149 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
150 fn simple_shared_exclusive_lockrust-analyzer cargo advisory-lock 0.3.0 tests/simple_shared_exclusive_lock().() {
151 let mut test_file = temp_dirrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/temp_dir().();
152 test_file.pushrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[PathBuf]push().("shared_exclusive_lock");
153 Filerust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/File#::createrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[File]create().(&test_file).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
154 {
155 let f1 = Filerust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/File#::openrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[File]open().(&test_file).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
•156 AdvisoryFileLockrust-analyzer cargo advisory-lock 0.3.0 AdvisoryFileLock#::lockrust-analyzer cargo advisory-lock 0.3.0 unix/impl#[File][AdvisoryFileLock]lock().(&f1, FileLockModerust-analyzer cargo advisory-lock 0.3.0 FileLockMode#::Sharedrust-analyzer cargo advisory-lock 0.3.0 FileLockMode#Shared#).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
157 let f2 = Filerust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/File#::openrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[File]open().(&test_file).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
158 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(matchesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/matches!!(
•159 AdvisoryFileLockrust-analyzer cargo advisory-lock 0.3.0 AdvisoryFileLock#::try_lockrust-analyzer cargo advisory-lock 0.3.0 unix/impl#[File][AdvisoryFileLock]try_lock().(&f2, FileLockModerust-analyzer cargo advisory-lock 0.3.0 FileLockMode#::Exclusiverust-analyzer cargo advisory-lock 0.3.0 FileLockMode#Exclusive#),
160 Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(FileLockErrorrust-analyzer cargo advisory-lock 0.3.0 FileLockError#::AlreadyLockedrust-analyzer cargo advisory-lock 0.3.0 FileLockError#AlreadyLocked#)
161 ));
162 }
163 stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/::remove_filerust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/remove_file().(&test_file).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
164 }
165
166 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
167 fn simple_exclusive_shared_lockrust-analyzer cargo advisory-lock 0.3.0 tests/simple_exclusive_shared_lock().() {
168 let mut test_file = temp_dirrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/temp_dir().();
169 test_file.pushrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[PathBuf]push().("exclusive_shared_lock");
170 Filerust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/File#::createrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[File]create().(&test_file).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
171 {
172 let f1 = Filerust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/File#::openrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[File]open().(&test_file).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
•173 AdvisoryFileLockrust-analyzer cargo advisory-lock 0.3.0 AdvisoryFileLock#::lockrust-analyzer cargo advisory-lock 0.3.0 unix/impl#[File][AdvisoryFileLock]lock().(&f1, FileLockModerust-analyzer cargo advisory-lock 0.3.0 FileLockMode#::Exclusiverust-analyzer cargo advisory-lock 0.3.0 FileLockMode#Exclusive#).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
174 let f2 = Filerust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/File#::openrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[File]open().(&test_file).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
•175 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(AdvisoryFileLockrust-analyzer cargo advisory-lock 0.3.0 AdvisoryFileLock#::try_lockrust-analyzer cargo advisory-lock 0.3.0 unix/impl#[File][AdvisoryFileLock]try_lock().(&f2, FileLockModerust-analyzer cargo advisory-lock 0.3.0 FileLockMode#::Sharedrust-analyzer cargo advisory-lock 0.3.0 FileLockMode#Shared#).is_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_err().());
176 }
177 stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/::remove_filerust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/remove_file().(&test_file).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
178 }
179 }