use std::fs;
use std::path::{Path, PathBuf};
use crate::runtime::error::{Result, RuntimeError};
pub(crate) struct PidFile {
path: PathBuf,
}
impl PidFile {
pub(crate) fn new(runtime_dir: &Path) -> Self {
Self {
path: runtime_dir.join("bitrouter.pid"),
}
}
pub(crate) fn read(&self) -> Result<Option<u32>> {
match fs::read_to_string(&self.path) {
Ok(content) => {
let pid = content.trim().parse::<u32>().map_err(|_| {
RuntimeError::Daemon(format!("corrupt PID file: {}", self.path.display()))
})?;
Ok(Some(pid))
}
Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(None),
Err(e) => Err(e.into()),
}
}
pub(crate) fn write(&self, pid: u32) -> Result<()> {
if let Some(parent) = self.path.parent() {
fs::create_dir_all(parent)?;
}
fs::write(&self.path, pid.to_string())?;
Ok(())
}
pub(crate) fn remove(&self) -> Result<()> {
match fs::remove_file(&self.path) {
Ok(()) => Ok(()),
Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(()),
Err(e) => Err(e.into()),
}
}
}