mod hash;
mod model;
mod walk;
use std::fmt;
use std::io;
use std::path::Path;
use ostrya_core::Xattrs;
pub use model::TreeModel;
const S_IFMT: u32 = 0o170000;
const S_IFDIR: u32 = 0o040000;
const S_IFREG: u32 = 0o100000;
const S_IFLNK: u32 = 0o120000;
const MODE_BITS: u32 = 0o177777;
const SYMLINK_MODE: u32 = S_IFLNK | 0o777;
#[cfg(windows)]
const FILE_ATTRIBUTE_REPARSE_POINT: u32 = 0x400;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum EntryKind {
File,
Dir,
Symlink,
}
impl EntryKind {
fn type_bits(self) -> u32 {
match self {
EntryKind::File => S_IFREG,
EntryKind::Dir => S_IFDIR,
EntryKind::Symlink => S_IFLNK,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct EntryMeta {
pub kind: EntryKind,
pub uid: u32,
pub gid: u32,
pub mode: u32,
pub xattrs: Xattrs,
pub symlink_target: Option<String>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum EntryAction {
Keep,
Skip,
}
#[derive(Debug)]
pub struct EntryPath {
path: String,
}
impl EntryPath {
pub fn as_str(&self) -> &str {
&self.path
}
pub fn as_path(&self) -> &Path {
Path::new(&self.path)
}
pub fn is_root(&self) -> bool {
self.path.is_empty()
}
}
impl fmt::Display for EntryPath {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.path)
}
}
pub type EntryFilter = Box<dyn FnMut(&EntryPath, &mut EntryMeta) -> EntryAction + Send>;
#[derive(Default)]
pub struct ScanOptions {
pub entry_filter: Option<EntryFilter>,
pub hash_jobs: Option<usize>,
}
fn invalid_data(message: impl Into<String>) -> io::Error {
io::Error::new(io::ErrorKind::InvalidData, message.into())
}
fn classify(md: &std::fs::Metadata) -> io::Result<EntryKind> {
#[cfg(windows)]
{
use std::os::windows::fs::MetadataExt;
if md.file_attributes() & FILE_ATTRIBUTE_REPARSE_POINT != 0 && !md.is_symlink() {
return Err(io::Error::new(
io::ErrorKind::Unsupported,
"the entry is a reparse point that is not a symlink",
));
}
}
let ty = md.file_type();
if ty.is_symlink() {
Ok(EntryKind::Symlink)
} else if ty.is_dir() {
Ok(EntryKind::Dir)
} else if ty.is_file() {
Ok(EntryKind::File)
} else {
Err(io::Error::new(
io::ErrorKind::Unsupported,
"the entry is not a regular file, a directory, or a symlink",
))
}
}
#[cfg(unix)]
fn default_meta(kind: EntryKind, md: &std::fs::Metadata, target: Option<String>) -> EntryMeta {
use std::os::unix::fs::MetadataExt;
unix_meta(kind, md.uid(), md.gid(), md.mode(), target)
}
#[cfg(unix)]
fn unix_meta(kind: EntryKind, uid: u32, gid: u32, mode: u32, target: Option<String>) -> EntryMeta {
let mode = match kind {
EntryKind::Symlink => SYMLINK_MODE,
EntryKind::File | EntryKind::Dir => mode,
};
EntryMeta {
kind,
uid,
gid,
mode,
xattrs: Xattrs::empty(),
symlink_target: target,
}
}
#[cfg(not(unix))]
fn default_meta(kind: EntryKind, _md: &std::fs::Metadata, target: Option<String>) -> EntryMeta {
let mode = match kind {
EntryKind::File => S_IFREG | 0o644,
EntryKind::Dir => S_IFDIR | 0o755,
EntryKind::Symlink => SYMLINK_MODE,
};
EntryMeta {
kind,
uid: 0,
gid: 0,
mode,
xattrs: Xattrs::empty(),
symlink_target: target,
}
}
fn check_filtered(kind: EntryKind, meta: &EntryMeta) -> io::Result<()> {
if meta.kind != kind {
return Err(invalid_data(
"the entry filter changed the kind of the entry",
));
}
if meta.mode & S_IFMT != kind.type_bits() {
return Err(invalid_data(
"the entry filter changed the file-type bits of the mode",
));
}
if meta.mode & !MODE_BITS != 0 {
return Err(invalid_data(
"the entry filter set a bit above the file-type bits of the mode",
));
}
match (kind, &meta.symlink_target) {
(EntryKind::Symlink, Some(target)) => check_target(target),
(EntryKind::Symlink, None) => Err(invalid_data(
"the entry filter removed the target of a symlink",
)),
(EntryKind::File | EntryKind::Dir, Some(_)) => Err(invalid_data(
"the entry filter gave a symlink target to an entry that is not a symlink",
)),
(EntryKind::File | EntryKind::Dir, None) => Ok(()),
}
}
#[cfg(windows)]
fn check_target(target: &str) -> io::Result<()> {
let bytes = target.as_bytes();
let drive = bytes.len() >= 2 && bytes[0].is_ascii_alphabetic() && bytes[1] == b':';
if drive || target.contains('\\') {
return Err(invalid_data(
"the symlink target holds `\\` or a drive prefix, and it does not resolve on Linux",
));
}
Ok(())
}
#[cfg(not(windows))]
fn check_target(_target: &str) -> io::Result<()> {
Ok(())
}
#[cfg(all(test, unix))]
mod tests {
use super::*;
#[test]
fn a_symlink_gets_the_mode_0o120777_whatever_the_read_gives() {
let target = Some("target".to_owned());
let meta = unix_meta(EntryKind::Symlink, 7, 8, 0o120755, target.clone());
assert_eq!(meta.mode, 0o120777);
assert_eq!((meta.uid, meta.gid), (7, 8));
assert_eq!(meta.symlink_target, target);
let meta = unix_meta(EntryKind::File, 7, 8, 0o100600, None);
assert_eq!(meta.mode, 0o100600);
}
}