use std::collections::BTreeMap;
use std::collections::btree_map::Entry;
use std::ffi::CString;
use std::os::unix::ffi::OsStrExt;
use std::os::unix::fs::MetadataExt;
use std::path::{Path, PathBuf};
use crate::acl::Acl;
use crate::ondisk::{Timestamp, Xattr};
use crate::source::{EntryKind, FileContent, FileRange, Metadata, Source, SourceEntry};
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum HostError {
#[error("{}: {source}", path.display())]
#[non_exhaustive]
Io {
path: PathBuf,
#[source]
source: std::io::Error,
},
#[error("{}: not a directory", path.display())]
#[non_exhaustive]
NotADirectory {
path: PathBuf,
},
#[error("{}: has a file type that cannot be written to a filesystem", path.display())]
#[non_exhaustive]
Unsupported {
path: PathBuf,
},
#[error("{}: has an invalid ACL: {source}", path.display())]
#[non_exhaustive]
Acl {
path: PathBuf,
#[source]
source: crate::acl::AclError,
},
#[error(
"{}: is the same directory as {} — walking it again would write that subtree \
into the image a second time, and a mount of an ancestor would never end",
path.display(),
first.display()
)]
#[non_exhaustive]
RepeatedDirectory {
path: PathBuf,
first: PathBuf,
},
}
pub struct DirectorySource {
entries: Vec<SourceEntry>,
}
impl DirectorySource {
pub fn from_path(root: impl AsRef<Path>) -> Result<Self, HostError> {
let root = root.as_ref();
let meta = std::fs::metadata(root).map_err(io_at(root))?;
if !meta.is_dir() {
return Err(HostError::NotADirectory {
path: root.to_path_buf(),
});
}
let mut entered: BTreeMap<(u64, u64), PathBuf> =
BTreeMap::from([((meta.dev(), meta.ino()), root.to_path_buf())]);
let mut found: Vec<(Vec<u8>, PathBuf, std::fs::Metadata)> =
vec![(b"/".to_vec(), root.to_path_buf(), meta)];
let mut pending: Vec<(PathBuf, Vec<u8>)> = vec![(root.to_path_buf(), b"/".to_vec())];
while let Some((host_dir, image_dir)) = pending.pop() {
for entry in std::fs::read_dir(&host_dir).map_err(io_at(&host_dir))? {
let entry = entry.map_err(io_at(&host_dir))?;
let host_path = entry.path();
let image_path = join(&image_dir, entry.file_name().as_bytes());
let meta = entry.metadata().map_err(io_at(&host_path))?;
if meta.is_dir() {
match entered.entry((meta.dev(), meta.ino())) {
Entry::Occupied(first) => {
return Err(HostError::RepeatedDirectory {
path: host_path,
first: first.get().clone(),
});
}
Entry::Vacant(slot) => {
slot.insert(host_path.clone());
}
}
pending.push((host_path.clone(), image_path.clone()));
}
found.push((image_path, host_path, meta));
}
}
found.sort_by(|a, b| a.0.cmp(&b.0));
let mut first_name: BTreeMap<(u64, u64), Vec<u8>> = BTreeMap::new();
let mut entries = Vec::with_capacity(found.len());
for (image_path, host_path, meta) in found {
let mut linked: Option<Vec<u8>> = None;
if !meta.is_dir() && meta.nlink() > 1 {
match first_name.entry((meta.dev(), meta.ino())) {
Entry::Occupied(held) => linked = Some(held.get().clone()),
Entry::Vacant(slot) => {
slot.insert(image_path.clone());
}
}
}
let kind = match linked {
Some(target) => EntryKind::HardLink { target },
None => kind_of(&host_path, &meta)?,
};
let (meta, xattrs) = if matches!(kind, EntryKind::HardLink { .. }) {
(metadata_of(&meta), Vec::new())
} else {
(metadata_of(&meta), xattrs_of(&host_path)?)
};
entries.push(SourceEntry {
path: image_path,
kind,
meta,
xattrs,
});
}
Ok(Self { entries })
}
#[must_use]
pub fn owner(mut self, uid: u32, gid: u32) -> Self {
for entry in &mut self.entries {
entry.meta.uid = uid;
entry.meta.gid = gid;
}
self
}
#[must_use]
pub fn len(&self) -> usize {
self.entries.len()
}
#[must_use]
pub fn is_empty(&self) -> bool {
self.entries.is_empty()
}
}
impl Source for DirectorySource {
fn into_entries(self) -> Vec<SourceEntry> {
self.entries
}
}
fn io_at(path: &Path) -> impl Fn(std::io::Error) -> HostError + '_ {
move |source| HostError::Io {
path: path.to_path_buf(),
source,
}
}
fn join(parent: &[u8], name: &[u8]) -> Vec<u8> {
let mut path = Vec::with_capacity(parent.len() + 1 + name.len());
path.extend_from_slice(parent);
if parent != b"/" {
path.push(b'/');
}
path.extend_from_slice(name);
path
}
fn kind_of(host_path: &Path, meta: &std::fs::Metadata) -> Result<EntryKind, HostError> {
use std::os::unix::fs::FileTypeExt;
let file_type = meta.file_type();
let kind = if file_type.is_dir() {
EntryKind::Directory
} else if file_type.is_file() {
EntryKind::File(FileContent::Range(FileRange::at_path(
host_path,
0,
meta.len(),
)))
} else if file_type.is_symlink() {
let target = std::fs::read_link(host_path).map_err(io_at(host_path))?;
EntryKind::Symlink(target.as_os_str().as_bytes().to_vec())
} else if file_type.is_char_device() {
let (major, minor) = device_numbers(meta.rdev());
EntryKind::CharDevice { major, minor }
} else if file_type.is_block_device() {
let (major, minor) = device_numbers(meta.rdev());
EntryKind::BlockDevice { major, minor }
} else if file_type.is_fifo() {
EntryKind::Fifo
} else if file_type.is_socket() {
EntryKind::Socket
} else {
return Err(HostError::Unsupported {
path: host_path.to_path_buf(),
});
};
Ok(kind)
}
fn metadata_of(meta: &std::fs::Metadata) -> Metadata {
let mode = (meta.mode() & 0o7777) as u16;
Metadata::new(mode, time_of(meta.mtime(), meta.mtime_nsec()))
.owned_by(meta.uid(), meta.gid())
.with_times(
time_of(meta.atime(), meta.atime_nsec()),
time_of(meta.ctime(), meta.ctime_nsec()),
time_of(meta.mtime(), meta.mtime_nsec()),
)
}
fn time_of(secs: i64, nanos: i64) -> Timestamp {
let nanos = u32::try_from(nanos).unwrap_or(0).min(999_999_999);
Timestamp { secs, nanos }
}
fn device_numbers(rdev: u64) -> (u32, u32) {
let major = ((rdev >> 8) & 0xfff) | ((rdev >> 32) & !0xfff);
let minor = (rdev & 0xff) | ((rdev >> 12) & !0xff);
(major as u32, minor as u32)
}
fn xattrs_of(host_path: &Path) -> Result<Vec<Xattr>, HostError> {
let mut xattrs = Vec::new();
for name in list_xattrs(host_path)? {
let Some(value) = get_xattr(host_path, &name)? else {
continue;
};
let value = if name == Acl::ACCESS_NAME || name == Acl::DEFAULT_NAME {
Acl::decode_xattr_v2(&value)
.map_err(|source| HostError::Acl {
path: host_path.to_path_buf(),
source,
})?
.encode()
} else {
value
};
xattrs.push(Xattr { name, value });
}
xattrs.sort_by(|a, b| a.name.cmp(&b.name));
Ok(xattrs)
}
const XATTR_ATTEMPTS: usize = 4;
fn list_xattrs(host_path: &Path) -> Result<Vec<Vec<u8>>, HostError> {
for _ in 0..XATTR_ATTEMPTS {
let size = match rustix::fs::llistxattr(host_path, &mut [0u8; 0][..]) {
Ok(size) => size,
Err(e) if unsupported(e) => return Ok(Vec::new()),
Err(e) => return Err(io_at(host_path)(e.into())),
};
if size == 0 {
return Ok(Vec::new());
}
let mut buf = vec![0u8; size];
match rustix::fs::llistxattr(host_path, &mut buf[..]) {
Ok(len) => {
return Ok(buf[..len]
.split(|&b| b == 0)
.filter(|name| !name.is_empty())
.map(<[u8]>::to_vec)
.collect());
}
Err(rustix::io::Errno::RANGE) => continue,
Err(e) if unsupported(e) => return Ok(Vec::new()),
Err(e) => return Err(io_at(host_path)(e.into())),
}
}
Err(io_at(host_path)(std::io::Error::other(
"the extended attributes kept changing while they were being read",
)))
}
fn get_xattr(host_path: &Path, name: &[u8]) -> Result<Option<Vec<u8>>, HostError> {
let name = CString::new(name).map_err(|_| HostError::Io {
path: host_path.to_path_buf(),
source: std::io::Error::other("an extended-attribute name holds a NUL byte"),
})?;
for _ in 0..XATTR_ATTEMPTS {
let size = match rustix::fs::lgetxattr(host_path, &name, &mut [0u8; 0][..]) {
Ok(size) => size,
Err(e) if gone(e) => return Ok(None),
Err(e) if unsupported(e) => return Ok(None),
Err(e) => return Err(io_at(host_path)(e.into())),
};
if size == 0 {
return Ok(Some(Vec::new()));
}
let mut buf = vec![0u8; size];
match rustix::fs::lgetxattr(host_path, &name, &mut buf[..]) {
Ok(len) => {
buf.truncate(len);
return Ok(Some(buf));
}
Err(rustix::io::Errno::RANGE) => continue,
Err(e) if gone(e) => return Ok(None),
Err(e) => return Err(io_at(host_path)(e.into())),
}
}
Err(io_at(host_path)(std::io::Error::other(
"an extended attribute kept changing while it was being read",
)))
}
fn unsupported(e: rustix::io::Errno) -> bool {
e == rustix::io::Errno::NOTSUP || e == rustix::io::Errno::OPNOTSUPP
}
fn gone(e: rustix::io::Errno) -> bool {
e == rustix::io::Errno::NODATA
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_child_path_joins_under_the_root() {
assert_eq!(join(b"/", b"etc"), b"/etc");
assert_eq!(join(b"/etc", b"hostname"), b"/etc/hostname");
assert_eq!(join(b"/", b"od\xffd"), b"/od\xffd");
}
#[test]
fn device_numbers_are_unpacked_from_the_interleaved_encoding() {
assert_eq!(device_numbers((1 << 8) | 3), (1, 3));
assert_eq!(device_numbers(8 << 8), (8, 0));
let rdev = ((259u64 & 0xfff) << 8) | (0x1_2345 & 0xff) | ((0x1_2345 & !0xffu64) << 12);
assert_eq!(device_numbers(rdev), (259, 0x1_2345));
}
#[test]
fn an_out_of_range_nanosecond_field_does_not_reach_the_encoding() {
assert_eq!(
time_of(5, 999_999_999),
Timestamp {
secs: 5,
nanos: 999_999_999,
}
);
assert_eq!(time_of(5, 2_000_000_000).nanos, 999_999_999);
assert_eq!(time_of(5, -1).nanos, 0);
}
fn paths(source: &DirectorySource) -> Vec<String> {
source
.entries
.iter()
.map(|e| String::from_utf8_lossy(&e.path).into_owned())
.collect()
}
fn at<'a>(source: &'a DirectorySource, path: &[u8]) -> &'a SourceEntry {
source
.entries
.iter()
.find(|e| e.path == path)
.unwrap_or_else(|| panic!("{} was not walked", String::from_utf8_lossy(path)))
}
#[test]
fn a_tree_walks_to_the_root_and_everything_under_it_in_path_order() {
let dir = tempfile::tempdir().expect("temp dir");
let root = dir.path();
std::fs::create_dir(root.join("etc")).expect("etc");
std::fs::create_dir_all(root.join("var/log")).expect("var/log");
std::fs::write(root.join("etc/hostname"), b"ferrosys\n").expect("hostname");
std::os::unix::fs::symlink("/proc/mounts", root.join("etc/mtab")).expect("mtab");
let source = DirectorySource::from_path(root).expect("walk the tree");
assert_eq!(
paths(&source),
[
"/",
"/etc",
"/etc/hostname",
"/etc/mtab",
"/var",
"/var/log"
]
);
assert_eq!(source.len(), 6);
assert!(!source.is_empty());
assert!(matches!(at(&source, b"/").kind, EntryKind::Directory));
assert!(matches!(
at(&source, b"/var/log").kind,
EntryKind::Directory
));
match &at(&source, b"/etc/mtab").kind {
EntryKind::Symlink(target) => assert_eq!(target, b"/proc/mounts"),
other => panic!("expected a symlink, got {other:?}"),
}
}
#[test]
fn a_regular_files_contents_are_named_rather_than_read() {
let dir = tempfile::tempdir().expect("temp dir");
std::fs::write(dir.path().join("motd"), b"welcome\n").expect("motd");
let source = DirectorySource::from_path(dir.path()).expect("walk the tree");
match &at(&source, b"/motd").kind {
EntryKind::File(FileContent::Range(range)) => {
assert_eq!(range.len(), 8);
assert_eq!(range.offset(), 0);
assert_eq!(range.path(), dir.path().join("motd"));
assert_eq!(range.read().expect("read the file"), b"welcome\n");
}
other => panic!("expected a located file, got {other:?}"),
}
}
#[test]
fn several_names_for_one_inode_become_hard_links_to_the_first() {
let dir = tempfile::tempdir().expect("temp dir");
let root = dir.path();
std::fs::write(root.join("b-original"), b"shared\n").expect("write");
std::fs::hard_link(root.join("b-original"), root.join("a-link")).expect("hard link");
std::fs::hard_link(root.join("b-original"), root.join("c-link")).expect("hard link");
let source = DirectorySource::from_path(root).expect("walk the tree");
assert!(matches!(
at(&source, b"/a-link").kind,
EntryKind::File(FileContent::Range(_))
));
for name in [&b"/b-original"[..], b"/c-link"] {
match &at(&source, name).kind {
EntryKind::HardLink { target } => assert_eq!(target, b"/a-link"),
other => panic!(
"{} should be a hard link, got {other:?}",
String::from_utf8_lossy(name)
),
}
}
}
#[test]
fn a_directory_is_never_taken_for_a_hard_link() {
let dir = tempfile::tempdir().expect("temp dir");
std::fs::create_dir(dir.path().join("etc")).expect("etc");
std::fs::create_dir(dir.path().join("etc/ssl")).expect("ssl");
let source = DirectorySource::from_path(dir.path()).expect("walk the tree");
for entry in &source.entries {
assert!(
matches!(entry.kind, EntryKind::Directory),
"{} is not a directory",
String::from_utf8_lossy(&entry.path)
);
}
}
#[test]
fn metadata_and_ownership_come_from_the_host_until_owner_replaces_them() {
use std::os::unix::fs::PermissionsExt;
let dir = tempfile::tempdir().expect("temp dir");
let file = dir.path().join("sh");
std::fs::write(&file, b"#!/bin/sh\n").expect("write");
std::fs::set_permissions(&file, std::fs::Permissions::from_mode(0o4755)).expect("chmod");
let source = DirectorySource::from_path(dir.path()).expect("walk the tree");
let entry = at(&source, b"/sh");
assert_eq!(entry.meta.mode, 0o4755);
let meta = std::fs::symlink_metadata(&file).expect("stat");
assert_eq!(entry.meta.mtime, time_of(meta.mtime(), meta.mtime_nsec()));
assert_eq!(entry.meta.atime, time_of(meta.atime(), meta.atime_nsec()));
assert_eq!(entry.meta.ctime, time_of(meta.ctime(), meta.ctime_nsec()));
assert_eq!(entry.meta.uid, meta.uid());
let owned = DirectorySource::from_path(dir.path())
.expect("walk the tree")
.owner(0, 0);
assert!(
owned
.entries
.iter()
.all(|e| e.meta.uid == 0 && e.meta.gid == 0)
);
}
#[test]
fn a_fifo_and_a_socket_are_carried_as_themselves() {
let dir = tempfile::tempdir().expect("temp dir");
rustix::fs::mknodat(
rustix::fs::CWD,
dir.path().join("pipe"),
rustix::fs::FileType::Fifo,
rustix::fs::Mode::from_raw_mode(0o644),
0,
)
.expect("make a fifo");
let _socket =
std::os::unix::net::UnixListener::bind(dir.path().join("sock")).expect("bind a socket");
let source = DirectorySource::from_path(dir.path()).expect("walk the tree");
assert!(matches!(at(&source, b"/pipe").kind, EntryKind::Fifo));
assert!(matches!(at(&source, b"/sock").kind, EntryKind::Socket));
}
#[test]
fn a_root_that_is_not_a_directory_is_refused_by_name() {
let dir = tempfile::tempdir().expect("temp dir");
let file = dir.path().join("plain");
std::fs::write(&file, b"x").expect("write");
match DirectorySource::from_path(&file).err() {
Some(HostError::NotADirectory { path }) => assert_eq!(path, file),
other => panic!("expected a refusal, got {other:?}"),
}
let missing = dir.path().join("nowhere");
match DirectorySource::from_path(&missing).err() {
Some(HostError::Io { path, .. }) => assert_eq!(path, missing),
other => panic!("expected an I/O failure, got {other:?}"),
}
}
#[test]
fn extended_attributes_are_carried_in_name_order_and_an_acl_is_translated() {
use crate::acl::{AclEntry, AclQualifier, EXEC, READ, WRITE};
let dir = tempfile::tempdir().expect("temp dir");
let file = dir.path().join("ping");
std::fs::write(&file, b"elf").expect("write");
let set = |name: &str, value: &[u8]| {
rustix::fs::lsetxattr(&file, name, value, rustix::fs::XattrFlags::empty())
};
if let Err(e) = set("user.note", b"second") {
eprintln!(
"SKIPPED: {} holds no extended attributes: {e}",
file.display()
);
return;
}
set("security.capability", b"first")
.or_else(|e| {
eprintln!("note: security.capability not writable here: {e}");
Ok::<(), rustix::io::Errno>(())
})
.expect("the fallback cannot fail");
let acl = Acl::new(vec![
AclEntry {
who: AclQualifier::UserObj,
perm: READ | WRITE | EXEC,
},
AclEntry {
who: AclQualifier::User(1000),
perm: READ | WRITE,
},
AclEntry {
who: AclQualifier::GroupObj,
perm: READ | EXEC,
},
AclEntry {
who: AclQualifier::Mask,
perm: READ | WRITE | EXEC,
},
AclEntry {
who: AclQualifier::Other,
perm: READ,
},
])
.expect("a well-formed ACL");
let v2 = acl.encode_xattr_v2();
let has_acl = set("system.posix_acl_access", &v2).is_ok();
let source = DirectorySource::from_path(dir.path()).expect("walk the tree");
let entry = at(&source, b"/ping");
let names: Vec<&[u8]> = entry.xattrs.iter().map(|x| x.name.as_slice()).collect();
assert!(
names.windows(2).all(|w| w[0] < w[1]),
"the attributes are in name order: {names:?}"
);
let note = entry
.xattrs
.iter()
.find(|x| x.name == b"user.note")
.expect("the attribute set on the file");
assert_eq!(note.value, b"second");
if has_acl {
let stored = entry
.xattrs
.iter()
.find(|x| x.name == Acl::ACCESS_NAME)
.expect("the ACL set on the file");
assert_ne!(
stored.value, v2,
"the version-2 form must not reach the image verbatim"
);
assert_eq!(
stored.value,
acl.encode(),
"it is the compact form ext stores"
);
} else {
eprintln!("SKIPPED: {} holds no POSIX ACL", file.display());
}
}
#[test]
fn every_directory_in_an_ordinary_tree_is_its_own() {
let dir = tempfile::tempdir().expect("temp dir");
let root = dir.path();
std::fs::create_dir_all(root.join("a/log")).expect("a/log");
std::fs::create_dir_all(root.join("b/log")).expect("b/log");
std::fs::create_dir(root.join("empty")).expect("empty");
std::fs::write(root.join("a/log/messages"), b"\n").expect("messages");
let source = DirectorySource::from_path(root).expect("walk the tree");
assert_eq!(
paths(&source),
[
"/",
"/a",
"/a/log",
"/a/log/messages",
"/b",
"/b/log",
"/empty"
]
);
}
#[test]
fn one_directory_under_two_paths_names_both_of_them() {
let said = HostError::RepeatedDirectory {
path: PathBuf::from("/staging/rootfs/mnt"),
first: PathBuf::from("/staging/rootfs"),
}
.to_string();
assert!(said.contains("/staging/rootfs/mnt"), "{said}");
assert!(said.contains("/staging/rootfs"), "{said}");
}
}