file_locking.rs
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
172
173
174
175
176
pub mod errors; pub mod file_ext; #[cfg(unix)] mod unix; #[cfg(windows)] mod windows; pub use errors::{ Error, Result }; pub use file_ext::{ FileExt }; #[cfg(test)] mod tests { use super::*; use std::fs::{ File, self }; use std::path::{ Path, PathBuf }; struct TestFile { path: PathBuf, file: File, } impl TestFile { fn new<P: AsRef<Path>>(path: P) -> Result<TestFile> { let path = path.as_ref(); let file = File::options() .create_new(true) .write(true) .open(path)?; Ok(TestFile { path: path.to_path_buf(), file, }) } } impl Drop for TestFile { fn drop(&mut self) { let _ = fs::remove_file(&self.path); } } #[test] fn lock_shared_works() { const TEST_FILE_PATH: &str = "test_lock_shared.txt"; let f = TestFile::new(TEST_FILE_PATH).expect("File should be created and opened"); let _guard = f.file.lock_shared().expect("Shared file lock should be obtained"); } #[test] fn lock_exclusive_works() { const TEST_FILE_PATH: &str = "test_lock_exclusive.txt"; let f = TestFile::new(TEST_FILE_PATH).expect("File should be created and opened"); let _guard = f.file.lock_exclusive().expect("Exclusive file lock should be obtained"); } #[test] fn try_lock_shared_works() { const TEST_FILE_PATH: &str = "test_try_lock_shared.txt"; let f = TestFile::new(TEST_FILE_PATH).expect("File should be created and opened"); let _guard = f.file.try_lock_shared().expect("Shared file lock should be obtained"); } #[test] fn try_lock_exclusive_works() { const TEST_FILE_PATH: &str = "test_try_lock_exclusive.txt"; let f = TestFile::new(TEST_FILE_PATH).expect("File should be created and opened"); let _guard = f.file.try_lock_shared().expect("Shared file lock should be obtained"); } #[test] fn try_lock_shared_does_not_block() { const TEST_FILE_PATH: &str = "test_try_lock_shared_does_not_block.txt"; let f = TestFile::new(TEST_FILE_PATH).expect("File should be created and opened"); let _guard = f.file.lock_exclusive().expect("Exclusive file lock should be obtained"); assert_shared_lock_fails(TEST_FILE_PATH); } #[test] fn try_lock_exclusive_does_not_block() { const TEST_FILE_PATH: &str = "test_try_lock_exclusive_does_not_block.txt"; let f = TestFile::new(TEST_FILE_PATH).expect("File should be created and opened"); let _guard = f.file.lock_exclusive().expect("Exclusive file lock should be obtained"); assert_exclusive_lock_fails(TEST_FILE_PATH); } #[test] fn lock_guard_going_out_of_scope_releases_lock() { const TEST_FILE_PATH: &str = "test_lock_guard_going_out_of_scope_releases_lock.txt"; let f = TestFile::new(TEST_FILE_PATH).expect("File should be created and opened"); { let _guard = f.file.lock_exclusive().expect("Exclusive file lock should be obtained"); assert_shared_lock_fails(TEST_FILE_PATH); } assert_shared_lock_succeeds(TEST_FILE_PATH); assert_exclusive_lock_succeeds(TEST_FILE_PATH); } fn assert_shared_lock_succeeds<P: AsRef<Path>>(path: P) { // We need to open the file again to test exclusive locking because on Unix // systems, locking the same file descriptor twice will result in a no-op // for the second `flock()` let f2 = File::options() .read(true) .open(path) .expect("Second handle to the same file should be opened"); let result = f2.try_lock_shared(); match result { Ok(_) => {}, Err(_) => panic!("Shared lock should succeed"), } } fn assert_exclusive_lock_succeeds<P: AsRef<Path>>(path: P) { // We need to open the file again to test exclusive locking because on Unix // systems, locking the same file descriptor twice will result in a no-op // for the second `flock()` let f2 = File::options() .read(true) .open(path) .expect("Second handle to the same file should be opened"); let result = f2.try_lock_exclusive(); match result { Ok(_) => {}, Err(_) => panic!("Exclusive lock should succeed"), } } fn assert_shared_lock_fails<P: AsRef<Path>>(path: P) { // We need to open the file again to test exclusive locking because on Unix // systems, locking the same file descriptor twice will result in a no-op // for the second `flock()` let f2 = File::options() .read(true) .open(path) .expect("Second handle to the same file should be opened"); let result = f2.try_lock_shared(); match result { Ok(_) => panic!("Second file lock should not be obtained"), Err(Error::AlreadyLocked) => {}, Err(_) => panic!("Result of trying to obtain a second file lock should be Error::AlreadyLocked"), } } fn assert_exclusive_lock_fails<P: AsRef<Path>>(path: P) { // We need to open the file again to test exclusive locking because on Unix // systems, locking the same file descriptor twice will result in a no-op // for the second `flock()` let f2 = File::options() .read(true) .open(path) .expect("Second handle to the same file should be opened"); let result = f2.try_lock_exclusive(); match result { Ok(_) => panic!("Second file lock should not be obtained"), Err(Error::AlreadyLocked) => {}, Err(_) => panic!("Result of trying to obtain a second file lock should be Error::AlreadyLocked"), } } }