use std::io;
#[cfg(any(not(unix), test))]
pub mod other;
#[cfg(unix)]
mod unix;
#[cfg(not(unix))]
pub use other::{checked_target, put, remove};
#[cfg(unix)]
pub use unix::{checked_target, put, remove};
pub fn is_plain(rel: &str) -> bool {
!rel.is_empty()
&& !rel.starts_with('/')
&& !rel.contains('\\')
&& !rel.contains('\0')
&& rel
.split('/')
.all(|part| !part.is_empty() && part != "." && part != "..")
}
pub fn in_state_folder(rel: &str) -> bool {
rel.split('/')
.any(|part| part.eq_ignore_ascii_case(".git") || part.eq_ignore_ascii_case(".moat"))
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Blocked {
Parent(String),
Folder,
Name,
}
impl Blocked {
pub fn words(&self) -> String {
match self {
Self::Parent(part) => {
format!(
"`{part}` on its path is a link or a file now, so nothing is written through it"
)
}
Self::Folder => "it is a folder now".to_owned(),
Self::Name => "its name is not a plain path inside the project".to_owned(),
}
}
}
pub enum Content<'a> {
File {
bytes: &'a [u8],
executable: bool,
},
Link {
#[cfg_attr(
not(unix),
expect(
dead_code,
reason = "only Unix writes a link; elsewhere the restore names it as skipped"
)
)]
target: &'a str,
},
}
fn temp_name(name: &str) -> io::Result<String> {
let suffix = crate::guard_relay::random_hex(6).map_err(|e| io::Error::other(e.to_string()))?;
Ok(format!(".{name}.openmoat-restore-{}", suffix.as_str()))
}