use std::fs::{File, OpenOptions};
use std::io::{self, Write};
use std::os::fd::AsRawFd;
use std::path::Path;
#[derive(Debug)]
pub struct Held {
file: File,
}
impl Drop for Held {
fn drop(&mut self) {
release(&self.file);
}
}
pub fn take(path: &Path) -> Result<Option<Held>, String> {
let file = opened(path).map_err(|error| format!("{}: {error}", path.display()))?;
if !locked(&file, libc::LOCK_EX) {
return Ok(None);
}
stamp(&file, std::process::id()).map_err(|error| format!("{}: {error}", path.display()))?;
Ok(Some(Held { file }))
}
pub fn held(path: &Path) -> bool {
File::open(path).is_ok_and(|file| !free(&file))
}
fn free(file: &File) -> bool {
let taken = locked(file, libc::LOCK_SH);
if taken {
release(file);
}
taken
}
fn opened(path: &Path) -> io::Result<File> {
path.parent().map_or(Ok(()), std::fs::create_dir_all)?;
OpenOptions::new()
.write(true)
.create(true)
.truncate(false)
.open(path)
}
fn stamp(mut file: &File, pid: u32) -> io::Result<()> {
file.set_len(0)?;
file.write_all(format!("{pid}\n").as_bytes())
}
fn release(file: &File) {
locked(file, libc::LOCK_UN);
}
fn locked(file: &File, operation: i32) -> bool {
unsafe { libc::flock(file.as_raw_fd(), operation | libc::LOCK_NB) == 0 }
}
#[cfg(test)]
mod tests {
use std::fs;
use crate::scratch::Scratch;
use super::*;
const STALE: &str = "2147483646\n";
fn lock(scratch: &Scratch) -> std::path::PathBuf {
scratch.paths().tui_lock()
}
#[test]
fn a_lock_lasts_as_long_as_the_value_holding_it() {
let scratch = Scratch::new();
let path = lock(&scratch);
let held = take(&path).expect("a directory it can create");
assert!(held.is_some());
assert!(super::held(&path), "this process is holding it");
drop(held);
assert!(!super::held(&path), "and every exit path gives it up");
}
#[test]
fn a_lock_names_the_process_that_took_it() {
let scratch = Scratch::new();
let path = lock(&scratch);
let _held = take(&path).expect("a lock");
assert_eq!(
fs::read_to_string(&path).expect("a written lock").trim(),
std::process::id().to_string()
);
}
#[test]
fn a_lock_another_blubat_holds_is_not_handed_out_twice() {
let scratch = Scratch::new();
let path = lock(&scratch);
let first = take(&path).expect("a lock").expect("nobody else has it");
assert!(
take(&path).expect("a readable file").is_none(),
"the first one up owns it"
);
drop(first);
assert!(
take(&path).expect("a lock").is_some(),
"and the next one up takes it once that is over"
);
}
#[test]
fn a_file_left_by_a_process_that_has_gone_is_no_lock_at_all() {
let scratch = Scratch::new();
let path = lock(&scratch);
fs::create_dir_all(path.parent().expect("a parent")).expect("a state directory");
fs::write(&path, STALE).expect("a lock left behind");
assert!(!held(&path), "a killed dashboard must not silence a daemon");
assert!(take(&path).expect("a lock").is_some());
}
#[test]
fn a_lock_nothing_has_ever_written_is_absent_rather_than_an_error() {
let scratch = Scratch::new();
assert!(!held(&scratch.join("never-written.lock")));
}
#[test]
fn a_lock_that_cannot_be_written_comes_back_as_the_reason() {
let scratch = Scratch::new();
let path = lock(&scratch);
fs::create_dir_all(&path).expect("a directory where the file belongs");
assert!(take(&path).is_err());
}
}