use crate::fidelity::{Property, Synthesis};
use crate::finding::Family;
use crate::source::Metadata;
use crate::xattr::Xattr;
#[derive(Clone, PartialEq, Eq, Debug, thiserror::Error)]
#[non_exhaustive]
pub enum TreeError {
#[error("i/o error: {message}")]
#[non_exhaustive]
Io {
kind: std::io::ErrorKind,
message: String,
},
#[error("malformed {family} filesystem: {detail}", family = .family.as_str())]
#[non_exhaustive]
Malformed {
family: Family,
detail: String,
},
#[error("unsupported {family} filesystem: {detail}", family = .family.as_str())]
#[non_exhaustive]
Unsupported {
family: Family,
detail: String,
},
#[error("{family} read exceeded a limit: {detail}", family = .family.as_str())]
#[non_exhaustive]
LimitExceeded {
family: Family,
detail: String,
},
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[non_exhaustive]
pub enum NodeKind {
Directory,
File {
size: u64,
},
Symlink,
CharDevice {
major: u32,
minor: u32,
},
BlockDevice {
major: u32,
minor: u32,
},
Fifo,
Socket,
}
#[derive(Clone, PartialEq, Eq, Debug)]
#[non_exhaustive]
pub struct TreeEntry<N> {
pub path: Vec<u8>,
pub kind: NodeKind,
pub shared: Option<u64>,
pub node: N,
}
impl<N> TreeEntry<N> {
#[must_use]
pub fn new(path: Vec<u8>, kind: NodeKind, node: N) -> Self {
Self {
path,
kind,
shared: None,
node,
}
}
#[must_use]
pub fn shared(mut self, id: u64) -> Self {
self.shared = Some(id);
self
}
}
#[derive(Clone, PartialEq, Eq, Debug)]
#[non_exhaustive]
pub struct Attributes {
pub meta: Metadata,
pub xattrs: Vec<Xattr>,
pub synthesized: Vec<Property>,
}
impl Attributes {
#[must_use]
pub fn read(meta: Metadata, xattrs: Vec<Xattr>) -> Self {
Self {
meta,
xattrs,
synthesized: Vec::new(),
}
}
#[cfg(any(feature = "fat", feature = "exfat"))]
pub(crate) fn from_read_only_bit(
synthesis: &Synthesis,
is_dir: bool,
read_only: bool,
times: Option<(crate::time::Timestamp, crate::time::Timestamp)>,
) -> Self {
let mut mode = if is_dir {
synthesis.dir_mode
} else {
synthesis.file_mode
};
if read_only {
mode &= !crate::fidelity::WRITE_BITS;
}
let zero = crate::time::Timestamp::from_secs(0);
let (atime, mtime) = times.unwrap_or((zero, zero));
let mut synthesized = vec![
Property::Ownership,
Property::Permissions,
Property::ChangeTime,
];
if times.is_none() {
synthesized.push(Property::AccessTime);
synthesized.push(Property::ModificationTime);
}
Self {
meta: Metadata {
mode,
uid: synthesis.uid,
gid: synthesis.gid,
atime,
ctime: mtime,
mtime,
},
xattrs: Vec::new(),
synthesized,
}
}
}
pub trait FsTree {
type Node;
fn family(&self) -> Family;
fn max_file_bytes(&self) -> u64;
fn walk_tree<E, F>(&mut self, visit: F) -> Result<(), E>
where
E: From<TreeError>,
F: FnMut(&mut Self, TreeEntry<Self::Node>) -> Result<(), E>;
fn stat(&mut self, node: &Self::Node, synthesis: &Synthesis) -> Result<Attributes, TreeError>;
fn read_bytes(
&mut self,
node: &Self::Node,
offset: u64,
buf: &mut [u8],
) -> Result<usize, TreeError>;
fn link_target(&mut self, node: &Self::Node) -> Result<Vec<u8>, TreeError>;
#[cfg_attr(
not(any(feature = "ext", feature = "fat", feature = "exfat", feature = "btrfs")),
allow(unreachable_code, unused_variables)
)]
fn check_file_size(&self, path: &[u8], size: u64) -> Result<(), TreeError> {
let cap = self.max_file_bytes();
if size > cap {
return Err(TreeError::LimitExceeded {
family: self.family(),
detail: format!(
"{}: the file is {size} bytes, more than the {cap}-byte cap this read \
is held to",
crate::escape::printable(path)
),
});
}
Ok(())
}
}
#[cfg(any(feature = "ext", feature = "fat", feature = "exfat", feature = "btrfs"))]
pub(crate) enum WalkFail<R, E> {
Read(R),
#[cfg_attr(not(feature = "ext"), allow(dead_code))]
Tree(TreeError),
Visitor(E),
}
#[cfg(test)]
mod tests {
use super::{NodeKind, TreeEntry};
#[test]
fn an_entry_has_no_second_name_until_one_is_declared() {
let entry = TreeEntry::new(b"/etc/hostname".to_vec(), NodeKind::File { size: 9 }, 12u32);
assert_eq!(entry.shared, None);
assert_eq!(entry.node, 12);
assert_eq!(entry.shared(12).shared, Some(12));
}
}