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