use std::fs::{File, OpenOptions};
use std::path::Path;
use anyhow::{Result, bail};
use fs4::fs_std::FileExt;
pub const CLAIM_FILE: &str = "daemon.lock";
pub struct IndexClaim {
_file: File,
}
impl IndexClaim {
pub fn acquire(dir: &Path) -> Result<Self> {
std::fs::create_dir_all(dir)?;
let path = dir.join(CLAIM_FILE);
let file = OpenOptions::new()
.create(true)
.truncate(false)
.write(true)
.open(&path)?;
if !file.try_lock_exclusive()? {
bail!("{} is held by another daemon", path.display());
}
Ok(Self { _file: file })
}
}