second-brain-core 0.13.4

Core library for second-brain: SQLite storage with vector and lexical indexes, BGE embeddings, and hybrid retrieval
Documentation
use std::fs::{File, OpenOptions};
use std::path::Path;

use anyhow::{Result, bail};
use fs4::fs_std::FileExt;

/// Lock file a daemon holds in its index directory for its whole life,
/// including while it has released its writer, so any other process that
/// would open the directory as a writer can tell it is live.
pub const CLAIM_FILE: &str = "daemon.lock";

/// One process's exclusive hold on an index directory; released on drop.
pub struct IndexClaim {
    _file: File,
}

impl IndexClaim {
    /// Errors when another process holds the directory.
    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 })
    }
}