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
//! Cross-process locks, one per file path.
use crate::{Store, StoreError};
use griz_core::content_hash;
use std::{fs::File, path::PathBuf};
/// Held locks; dropping them releases every path.
#[derive(Debug)]
pub struct PathLocks {
_files: Vec<File>,
}
impl Store {
/// Blocks until every path is locked. Paths are locked in sorted order, so
/// two callers with overlapping sets cannot deadlock.
///
/// # Errors
/// Returns an error when a lock file cannot be opened or locked.
pub fn lock_paths(&self, paths: &[PathBuf]) -> Result<PathLocks, StoreError> {
let mut resolver = crate::PathResolver::default();
let mut keys: Vec<String> = paths
.iter()
.map(|path| {
resolver
.resolve_file(path)
.map(|path| content_hash(&path.to_string_lossy()))
})
.collect::<Result<_, _>>()?;
keys.sort();
keys.dedup();
let dir = self.home().join("locks");
std::fs::create_dir_all(&dir)?;
let files = keys
.iter()
.map(|key| {
let file = File::options()
.create(true)
.truncate(false)
.write(true)
.open(dir.join(format!("{key}.lock")))?;
file.lock()?;
Ok(file)
})
.collect::<Result<_, StoreError>>()?;
Ok(PathLocks { _files: files })
}
}