#[cfg(any(target_os = "linux", target_os = "macos", target_os = "windows"))]
use std::path::{Path, PathBuf};
use prikk_error::{PrikkError, Result};
mod anchored;
mod contract;
#[cfg(all(test, any(target_os = "linux", target_os = "macos")))]
mod caller_tests;
#[cfg(all(
test,
any(target_os = "linux", target_os = "macos", target_os = "windows")
))]
mod tests;
pub(crate) use anchored::{
EntryKind, MutationRoot, RootFileStat, append_file_required, create_new_file_required,
ensure_directory_required, inspect_entry, list_directory, read_file_if_exists,
read_file_required, remove_file_cleanup_best_effort, remove_worktree_file_required,
set_regular_file_mode_required, stat_file_state_if_exists, sync_directory_required,
truncate_existing_file_required, truncate_file_empty_required, write_file_atomically,
write_worktree_file_atomically,
};
#[cfg(all(test, target_os = "linux"))]
pub(crate) use anchored::LinuxDurability;
#[cfg(all(test, target_os = "macos"))]
pub(crate) use anchored::MacosDurability;
#[cfg(all(test, target_os = "windows"))]
pub(crate) use anchored::WindowsDurability;
#[cfg(all(test, any(target_os = "linux", target_os = "macos")))]
pub(crate) use anchored::NoDurability;
pub(crate) use anchored::remove_file_required;
#[cfg(all(
test,
any(target_os = "linux", target_os = "macos", target_os = "windows")
))]
pub(crate) use contract::DurabilityContract;
#[cfg(all(
test,
any(target_os = "linux", target_os = "macos", target_os = "windows")
))]
pub(crate) use anchored::{TestFailPoint, fail_once_for_test};
#[cfg(all(
test,
any(target_os = "linux", target_os = "macos", target_os = "windows")
))]
pub(crate) use anchored::fail_after_for_test;
#[cfg(all(
test,
any(target_os = "linux", target_os = "macos", target_os = "windows")
))]
pub(crate) use anchored::set_directory_create_barrier_for_test;
#[cfg(all(test, target_os = "windows"))]
pub(crate) use anchored::set_anchor_verification_barrier_for_test;
#[cfg(any(target_os = "linux", target_os = "macos", target_os = "windows"))]
pub(crate) fn temporary_path(path: &Path) -> Result<PathBuf> {
let Some(file_name) = path.file_name() else {
return Err(PrikkError::Io(
"temporary path destination has no file name".to_string(),
));
};
let mut random = [0_u8; 16];
getrandom::fill(&mut random)
.map_err(|error| PrikkError::Io(format!("temporary path randomness failed: {error}")))?;
let mut name = file_name.to_os_string();
name.push(format!(
".tmp.{}.{:032x}",
std::process::id(),
u128::from_le_bytes(random)
));
Ok(path.with_file_name(name))
}
pub(crate) fn len_to_u16(len: usize) -> Result<u16> {
u16::try_from(len).map_err(|_| PrikkError::MalformedData("length exceeds u16".to_string()))
}
pub(crate) fn len_to_u32(len: usize) -> Result<u32> {
u32::try_from(len).map_err(|_| PrikkError::MalformedData("length exceeds u32".to_string()))
}
pub(crate) fn len_to_u64(len: usize) -> Result<u64> {
u64::try_from(len).map_err(|_| PrikkError::MalformedData("length exceeds u64".to_string()))
}