1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
use std::fs::{File, OpenOptions};
use std::io;
use std::path::Path;
use thiserror::Error;
#[derive(Debug, Error)]
pub enum FileLockError {
#[error("the file is already locked")]
AlreadyLocked,
#[error("I/O error: {0}")]
IOError(#[from] io::Error),
}
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
pub enum FileLockMode {
Exclusive,
Shared,
}
pub struct AdvisoryFileLock {
file: File,
file_lock_mode: FileLockMode,
}
impl AdvisoryFileLock {
pub fn new<P: AsRef<Path>>(
path: P,
file_lock_mode: FileLockMode,
) -> Result<Self, FileLockError> {
let is_exclusive = file_lock_mode == FileLockMode::Exclusive;
let file = OpenOptions::new()
.read(true)
.create(is_exclusive)
.write(is_exclusive)
.open(path)?;
Ok(AdvisoryFileLock {
file,
file_lock_mode,
})
}
pub fn is_shared(&self) -> bool {
self.file_lock_mode == FileLockMode::Shared
}
pub fn is_exclusive(&self) -> bool {
self.file_lock_mode == FileLockMode::Exclusive
}
pub fn lock(&mut self) -> Result<(), FileLockError> {
self.lock_impl()
}
pub fn try_lock(&mut self) -> Result<(), FileLockError> {
self.try_lock_impl()
}
pub fn unlock(&mut self) -> Result<(), FileLockError> {
self.unlock_impl()
}
}
impl Drop for AdvisoryFileLock {
fn drop(&mut self) {
if let Err(err) = self.unlock() {
log::error!(
"[AdvisoryFileLock] unlock_file failed during dropping: {}",
err
);
}
}
}
#[cfg(windows)]
mod windows;
#[cfg(unix)]
mod unix;
#[cfg(test)]
mod tests {
use super::*;
use std::env::temp_dir;
#[test]
fn simple_shared_lock() {
let mut test_file = temp_dir();
test_file.push("shared_lock");
File::create(&test_file).unwrap();
{
let mut f1 = AdvisoryFileLock::new(&test_file, FileLockMode::Shared).unwrap();
f1.lock().unwrap();
let mut f2 = AdvisoryFileLock::new(&test_file, FileLockMode::Shared).unwrap();
f2.lock().unwrap();
}
std::fs::remove_file(&test_file).unwrap();
}
#[test]
fn simple_exclusive_lock() {
let mut test_file = temp_dir();
test_file.push("exclusive_lock");
{
let mut f1 = AdvisoryFileLock::new(&test_file, FileLockMode::Exclusive).unwrap();
f1.lock().unwrap();
let f2 = AdvisoryFileLock::new(&test_file, FileLockMode::Exclusive)
.unwrap()
.try_lock();
assert!(f2.is_err());
}
std::fs::remove_file(&test_file).unwrap();
}
#[test]
fn simple_shared_exclusive_lock() {
let mut test_file = temp_dir();
test_file.push("shared_exclusive_lock");
File::create(&test_file).unwrap();
{
let mut f1 = AdvisoryFileLock::new(&test_file, FileLockMode::Shared).unwrap();
f1.lock().unwrap();
let mut f2 = AdvisoryFileLock::new(&test_file, FileLockMode::Exclusive).unwrap();
assert!(f2.try_lock().is_err());
}
std::fs::remove_file(&test_file).unwrap();
}
#[test]
fn simple_exclusive_shared_lock() {
let mut test_file = temp_dir();
test_file.push("exclusive_shared_lock");
{
let mut f1 = AdvisoryFileLock::new(&test_file, FileLockMode::Exclusive).unwrap();
f1.lock().unwrap();
let mut f2 = AdvisoryFileLock::new(&test_file, FileLockMode::Exclusive).unwrap();
assert!(f2.try_lock().is_err());
}
std::fs::remove_file(&test_file).unwrap();
}
}