Skip to main content

git_workflow/pool/
lock.rs

1//! File-based locking for the worktree pool using flock (via fs2)
2
3use std::fs::{self, File};
4use std::path::{Path, PathBuf};
5
6use fs2::FileExt;
7
8use crate::error::{GwError, Result};
9
10/// A held file lock on the pool
11pub struct PoolLock {
12    _file: File,
13    _path: PathBuf,
14}
15
16impl PoolLock {
17    /// Acquire an exclusive lock on the pool directory.
18    /// Creates the pool directory and lock file if they don't exist.
19    pub fn acquire(pool_dir: &Path) -> Result<Self> {
20        fs::create_dir_all(pool_dir)?;
21        let lock_path = pool_dir.join("pool.lock");
22        let file = File::create(&lock_path)?;
23        file.try_lock_exclusive()
24            .map_err(|_| GwError::PoolLockTimeout)?;
25        Ok(Self {
26            _file: file,
27            _path: lock_path,
28        })
29    }
30}
31
32// Lock is released when PoolLock is dropped (File close releases flock)