Skip to content

Commit c29317c

Browse files
committed
[rust] Store reference to lock file to clear folder when necessary
1 parent f8fd5fe commit c29317c

File tree

1 file changed

+17
-16
lines changed

1 file changed

+17
-16
lines changed

rust/src/lock.rs

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -53,21 +53,15 @@ impl Lock {
5353

5454
log.debug(format!("Acquiring lock: {}", path.display()));
5555
file.lock_exclusive().unwrap_or_default();
56-
57-
LOCK_PATH.with(|option| {
58-
*option.borrow_mut() = Some(lock_folder.to_path_buf());
59-
});
56+
set_lock_path(Some(path.to_path_buf()));
6057

6158
Ok(Self { file, path })
6259
}
6360

6461
pub fn release(&mut self) {
6562
fs::remove_file(&self.path).unwrap_or_default();
6663
self.file.unlock().unwrap_or_default();
67-
68-
LOCK_PATH.with(|option| {
69-
*option.borrow_mut() = None;
70-
});
64+
set_lock_path(None);
7165
}
7266

7367
pub fn exists(&mut self) -> bool {
@@ -76,14 +70,21 @@ impl Lock {
7670
}
7771

7872
pub fn clear_lock_if_required() {
79-
let mut lock_path: Option<PathBuf> = None;
80-
LOCK_PATH.with(|option| {
81-
let optional_path = &*option.borrow();
82-
if optional_path.is_some() {
83-
lock_path = Some(optional_path.as_ref().unwrap().to_path_buf());
84-
}
85-
});
73+
let lock_path = get_lock_path();
8674
if lock_path.is_some() {
87-
fs::remove_dir_all(lock_path.unwrap()).unwrap_or_default();
75+
let lock = lock_path.unwrap();
76+
if lock.exists() {
77+
fs::remove_dir_all(lock.parent().unwrap()).unwrap_or_default();
78+
}
8879
}
8980
}
81+
82+
fn set_lock_path(path: Option<PathBuf>) {
83+
LOCK_PATH.with(|lock_path| {
84+
*lock_path.borrow_mut() = path;
85+
});
86+
}
87+
88+
fn get_lock_path() -> Option<PathBuf> {
89+
LOCK_PATH.with(|lock_path| lock_path.borrow().clone())
90+
}

0 commit comments

Comments
 (0)