cleanup lock acquisition

This commit is contained in:
Cyryl Płotnicki 2021-10-22 22:14:51 +01:00
parent 5ec63e9e0c
commit 8b4b53a9fc

View file

@ -71,26 +71,25 @@ impl Lock {
fn sole_lock(lock_id: Uuid, index_directory: &Path) -> Result<bool> { fn sole_lock(lock_id: Uuid, index_directory: &Path) -> Result<bool> {
let my_lock_file_path = Lock::lock_file_path(index_directory, lock_id)?; let my_lock_file_path = Lock::lock_file_path(index_directory, lock_id)?;
let walker = WalkDir::new(index_directory); let all_locks_count =
let all_locks: Vec<_> = walker Lock::count_files(|e| e.file_name().to_string_lossy().ends_with(FILE_EXTENSION), index_directory)?;
.into_iter() if all_locks_count != 1 {
.filter_map(|e| e.ok())
.filter(|e| e.file_name().to_string_lossy().ends_with(FILE_EXTENSION))
.collect();
if all_locks.len() != 1 {
return Ok(false); return Ok(false);
} }
let walker = WalkDir::new(index_directory); let my_locks_count = Lock::count_files(|e| e.path() == my_lock_file_path, index_directory)?;
let my_locks: Vec<_> = walker if my_locks_count != 1 {
.into_iter()
.filter_map(|e| e.ok())
.filter(|e| e.path() == my_lock_file_path)
.collect();
if my_locks.len() != 1 {
return Ok(false); return Ok(false);
} }
let result = all_locks.first().unwrap().path() == my_locks.first().unwrap().path(); Ok(true)
Ok(result) }
fn count_files<P>(predicate: P, directory: &Path) -> Result<usize>
where
P: Fn(&walkdir::DirEntry) -> bool,
{
let walker = WalkDir::new(directory);
let matching = walker.into_iter().filter_map(|e| e.ok()).filter(predicate);
Ok(matching.count())
} }
fn create_lock_file(lock_id: Uuid, index_directory: &Path) -> Result<()> { fn create_lock_file(lock_id: Uuid, index_directory: &Path) -> Result<()> {