| 503 | |
| 504 | impl RegistryFileLock { |
| 505 | fn acquire(path: &Path) -> io::Result<Self> { |
| 506 | #[cfg(unix)] |
| 507 | { |
| 508 | if let Some(parent) = path.parent() { |
| 509 | fs::create_dir_all(parent)?; |
| 510 | } |
| 511 | let lock_path = path.with_extension("json.lock"); |
| 512 | let file = fs::OpenOptions::new() |
| 513 | .create(true) |
| 514 | .write(true) |
| 515 | .truncate(false) |
| 516 | .open(lock_path)?; |
| 517 | let result = unsafe { libc::flock(file.as_raw_fd(), libc::LOCK_EX) }; |
| 518 | if result != 0 { |
| 519 | return Err(io::Error::last_os_error()); |
| 520 | } |
| 521 | Ok(Self { file }) |
| 522 | } |
| 523 | #[cfg(not(unix))] |
| 524 | { |
| 525 | let _ = path; |
| 526 | Ok(Self {}) |
| 527 | } |
| 528 | } |
| 529 | } |
| 530 | |
| 531 | impl Drop for RegistryFileLock { |