use crate::recovery_error::{RecoveryError, io_failure};
use std::path::{Path, PathBuf};
pub fn recovery_path_exists(path: &Path) -> Result<bool, RecoveryError> {
match std::fs::metadata(path) {
Ok(_) => return Ok(true),
Err(error) if error.kind() == std::io::ErrorKind::NotFound => return Ok(false),
Err(error) => return Err(io_failure("probing", path, &error)),
}
}
pub fn path_present(path: &Path) -> Result<bool, RecoveryError> {
match std::fs::symlink_metadata(path) {
Ok(_) => return Ok(true),
Err(error) if error.kind() == std::io::ErrorKind::NotFound => return Ok(false),
Err(error) => return Err(io_failure("probing", path, &error)),
}
}
pub fn read_optional(path: &Path) -> Result<Option<Vec<u8>>, RecoveryError> {
match std::fs::read(path) {
Ok(bytes) => return Ok(Some(bytes)),
Err(error) if error.kind() == std::io::ErrorKind::NotFound => return Ok(None),
Err(error) => return Err(io_failure("reading", path, &error)),
}
}
pub fn remove_file_if_present(path: &Path) -> Result<(), RecoveryError> {
match std::fs::remove_file(path) {
Ok(()) => return Ok(()),
Err(error) if error.kind() == std::io::ErrorKind::NotFound => return Ok(()),
Err(error) => return Err(io_failure("removing", path, &error)),
}
}
pub fn lock_pid_path(real_index: &Path) -> PathBuf {
let mut name: std::ffi::OsString = real_index.as_os_str().to_owned();
name.push("~pid.lock");
return PathBuf::from(name);
}
#[cfg(test)]
#[path = "recovery_files_tests.rs"]
pub(crate) mod tests;