Acquire file lock to prevent race conditions accessing the cache folder by concurrent SM processes
(
log: &Logger,
target: &Path,
single_file: Option<String>,
)
| 37 | impl Lock { |
| 38 | // Acquire file lock to prevent race conditions accessing the cache folder by concurrent SM processes |
| 39 | pub fn acquire( |
| 40 | log: &Logger, |
| 41 | target: &Path, |
| 42 | single_file: Option<String>, |
| 43 | ) -> Result<Self, Error> { |
| 44 | let lock_folder = if single_file.is_some() { |
| 45 | create_parent_path_if_not_exists(target)?; |
| 46 | target.parent().unwrap() |
| 47 | } else { |
| 48 | create_path_if_not_exists(target)?; |
| 49 | target |
| 50 | }; |
| 51 | let path = lock_folder.join(LOCK_FILE); |
| 52 | let file = File::create(&path)?; |
| 53 | |
| 54 | log.debug(format!("Acquiring lock: {}", path.display())); |
| 55 | file.lock_exclusive().unwrap_or_default(); |
| 56 | set_lock_path(Some(path.to_path_buf())); |
| 57 | |
| 58 | Ok(Self { |
| 59 | file: Some(file), |
| 60 | path, |
| 61 | }) |
| 62 | } |
| 63 | |
| 64 | pub fn release(&mut self) { |
| 65 | if let Some(file) = self.file.take() { |
nothing calls this directly
no test coverage detected