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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
use std::fs::*;
use std::io;
use std::path::Path;
use sysinfo::*;
use super::queue::{recv_lock_filename, send_lock_filename};
use super::state::{QueueState, QueueStatePersistence};
use super::sync::{FileGuard, UNIQUE_PROCESS_TOKEN};
pub fn unlock<P: AsRef<Path>>(lock_filename: P) -> io::Result<()> {
let contents = match read_to_string(&lock_filename) {
Ok(contents) => contents,
Err(err) if err.kind() == io::ErrorKind::NotFound => return Ok(()),
Err(err) => return Err(err),
};
let owner_pid = contents
.split("pid=")
.collect::<Vec<_>>()
.get(1)
.map(|token| {
token
.chars()
.take_while(|ch| ch.is_digit(10))
.collect::<String>()
.parse::<sysinfo::Pid>()
})
.expect("failed to parse recv lock file: no pid")
.expect("failed to parse recv lock file: bad pid");
let owner_token = contents
.split("token=")
.collect::<Vec<_>>()
.get(1)
.map(|token| {
token
.chars()
.take_while(|ch| ch.is_digit(10))
.collect::<String>()
.parse::<u64>()
})
.expect("failed to parse recv lock file: no token")
.expect("failed to parse recv lock file: bad token");
let system = System::new_with_specifics(RefreshKind::new().with_processes());
let process_exists_and_is_not_me =
owner_pid as u32 != std::process::id() && system.get_processes().get(&owner_pid).is_some();
let lock_is_the_same_and_is_me =
owner_pid as u32 == std::process::id() && owner_token == *UNIQUE_PROCESS_TOKEN;
if process_exists_and_is_not_me {
return Err(io::Error::new(
io::ErrorKind::Other,
format!(
"another process, of id {}, is still locking `{:?}`",
owner_pid,
lock_filename.as_ref()
),
));
} else if lock_is_the_same_and_is_me {
return Err(io::Error::new(
io::ErrorKind::Other,
format!(
"current process is still locking `{:?}`",
lock_filename.as_ref()
),
));
} else {
remove_file(lock_filename)?;
Ok(())
}
}
pub fn unlock_for_sending<P: AsRef<Path>>(base: P) -> io::Result<()> {
unlock(send_lock_filename(base.as_ref()))
}
pub fn unlock_for_receiving<P: AsRef<Path>>(base: P) -> io::Result<()> {
unlock(recv_lock_filename(base.as_ref()))
}
pub fn unlock_queue<P: AsRef<Path>>(base: P) -> io::Result<()> {
unlock_for_sending(base.as_ref())?;
unlock_for_receiving(base.as_ref())?;
Ok(())
}
pub fn guess_recv_metadata<P: AsRef<Path>>(base: P) -> io::Result<()> {
let lock = FileGuard::try_lock(recv_lock_filename(base.as_ref()))?;
let mut min_segment = std::u64::MAX;
for maybe_entry in read_dir(base.as_ref())? {
let path = maybe_entry?.path();
if path.extension().map(|ext| ext == "q").unwrap_or(false) {
let segment = path
.file_stem()
.expect("has extension, therefore has stem")
.to_string_lossy()
.parse::<u64>()
.expect("failed to parse segment filename");
min_segment = u64::min(segment, min_segment);
}
}
let queue_state = QueueState {
segment: min_segment,
..QueueState::default()
};
let mut persistence = QueueStatePersistence::new();
let old_state = persistence.open(base.as_ref())?;
persistence.save(if queue_state > old_state {
&queue_state
} else {
&old_state
})?;
drop(lock);
Ok(())
}
pub fn guess_recv_metadata_with_loss<P: AsRef<Path>>(base: P) -> io::Result<()> {
let lock = FileGuard::try_lock(recv_lock_filename(base.as_ref()))?;
let mut min_segment = std::u64::MAX;
for maybe_entry in read_dir(base.as_ref())? {
let path = maybe_entry?.path();
if path.extension().map(|ext| ext == "q").unwrap_or(false) {
let segment = path
.file_stem()
.expect("has extension, therefore has stem")
.to_string_lossy()
.parse::<u64>()
.expect("failed to parse segment filename");
min_segment = u64::min(segment, min_segment);
}
}
remove_file(base.as_ref().join(format!("{}.q", min_segment)))?;
let queue_state = QueueState {
segment: min_segment + 1,
..QueueState::default()
};
let mut persistence = QueueStatePersistence::new();
let _ = persistence.open(base.as_ref())?;
persistence.save(&queue_state)?;
drop(lock);
Ok(())
}
pub fn recover<P: AsRef<Path>>(base: P) -> io::Result<()> {
unlock_queue(base.as_ref())?;
guess_recv_metadata(base.as_ref())?;
Ok(())
}
pub fn recover_with_loss<P: AsRef<Path>>(base: P) -> io::Result<()> {
unlock_queue(base.as_ref())?;
guess_recv_metadata_with_loss(base.as_ref())?;
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_unlock() {
let guard = FileGuard::try_lock("data/test-unlock.lock").unwrap();
std::mem::forget(guard);
assert_eq!(
io::ErrorKind::Other,
unlock("data/test-unlock.lock").unwrap_err().kind()
);
remove_file("data/test-unlock.lock").unwrap();
}
#[test]
fn test_unlock_inexistent() {
unlock("data/inexistent-lock.lock").unwrap();
}
#[test]
#[should_panic]
fn test_recover_while_open() {
let channel = crate::channel("data/recover-while-open").unwrap();
recover("data/recover-while-open").unwrap();
drop(channel);
}
#[test]
#[should_panic]
fn test_recover_inexistent() {
recover("data/recover-inexistent").unwrap();
}
}