use std::borrow::Cow;
use std::fs::File;
use std::io::{Read, Seek, SeekFrom};
use std::path::{Path, PathBuf};
use std::sync::Arc;
use crate::ondisk::{Timestamp, Xattr};
#[derive(Clone, PartialEq, Eq, Debug)]
pub enum FileContent {
Owned(Vec<u8>),
Range(FileRange),
}
impl FileContent {
#[must_use]
pub fn len(&self) -> u64 {
match self {
Self::Owned(bytes) => bytes.len() as u64,
Self::Range(range) => range.len(),
}
}
#[must_use]
pub fn is_empty(&self) -> bool {
self.len() == 0
}
pub fn read(&self) -> std::io::Result<Cow<'_, [u8]>> {
match self {
Self::Owned(bytes) => Ok(Cow::Borrowed(bytes)),
Self::Range(range) => Ok(Cow::Owned(range.read()?)),
}
}
}
impl From<Vec<u8>> for FileContent {
fn from(bytes: Vec<u8>) -> Self {
Self::Owned(bytes)
}
}
impl From<FileRange> for FileContent {
fn from(range: FileRange) -> Self {
Self::Range(range)
}
}
impl From<&[u8]> for FileContent {
fn from(bytes: &[u8]) -> Self {
Self::Owned(bytes.to_vec())
}
}
impl<const N: usize> From<&[u8; N]> for FileContent {
fn from(bytes: &[u8; N]) -> Self {
Self::Owned(bytes.to_vec())
}
}
impl From<&str> for FileContent {
fn from(text: &str) -> Self {
Self::Owned(text.as_bytes().to_vec())
}
}
impl From<String> for FileContent {
fn from(text: String) -> Self {
Self::Owned(text.into_bytes())
}
}
#[derive(Clone)]
pub struct FileRange {
file: Option<Arc<File>>,
path: Arc<PathBuf>,
offset: u64,
len: u64,
}
impl FileRange {
#[must_use]
pub fn new(file: Arc<File>, path: impl Into<PathBuf>, offset: u64, len: u64) -> Self {
Self {
file: Some(file),
path: Arc::new(path.into()),
offset,
len,
}
}
#[must_use]
pub fn at_path(path: impl Into<PathBuf>, offset: u64, len: u64) -> Self {
Self {
file: None,
path: Arc::new(path.into()),
offset,
len,
}
}
#[must_use]
pub fn path(&self) -> &Path {
self.path.as_path()
}
#[must_use]
pub const fn offset(&self) -> u64 {
self.offset
}
#[must_use]
pub const fn len(&self) -> u64 {
self.len
}
#[must_use]
pub const fn is_empty(&self) -> bool {
self.len == 0
}
pub fn read(&self) -> std::io::Result<Vec<u8>> {
let context = |e: std::io::Error| {
std::io::Error::new(
e.kind(),
format!(
"{}: reading {} bytes at offset {}: {e}",
self.path.display(),
self.len,
self.offset
),
)
};
let len = usize::try_from(self.len).map_err(|_| {
std::io::Error::other(format!(
"{}: {} bytes at offset {} is more than this platform addresses",
self.path.display(),
self.len,
self.offset
))
})?;
let mut handle = match &self.file {
Some(file) => file.try_clone().map_err(context)?,
None => File::open(self.path.as_path()).map_err(context)?,
};
handle.seek(SeekFrom::Start(self.offset)).map_err(context)?;
let mut buf = vec![0u8; len];
handle.read_exact(&mut buf).map_err(context)?;
Ok(buf)
}
}
impl PartialEq for FileRange {
fn eq(&self, other: &Self) -> bool {
self.path == other.path && self.offset == other.offset && self.len == other.len
}
}
impl Eq for FileRange {}
impl std::fmt::Debug for FileRange {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("FileRange")
.field("path", &self.path)
.field("offset", &self.offset)
.field("len", &self.len)
.finish()
}
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct Metadata {
pub mode: u16,
pub uid: u32,
pub gid: u32,
pub atime: Timestamp,
pub ctime: Timestamp,
pub mtime: Timestamp,
}
impl Metadata {
#[must_use]
pub fn new(mode: u16, mtime: Timestamp) -> Self {
Self {
mode,
uid: 0,
gid: 0,
atime: mtime,
ctime: mtime,
mtime,
}
}
#[must_use]
pub fn owned_by(mut self, uid: u32, gid: u32) -> Self {
self.uid = uid;
self.gid = gid;
self
}
#[must_use]
pub fn with_times(mut self, atime: Timestamp, ctime: Timestamp, mtime: Timestamp) -> Self {
self.atime = atime;
self.ctime = ctime;
self.mtime = mtime;
self
}
}
#[derive(Clone, PartialEq, Eq, Debug)]
pub enum EntryKind {
Directory,
File(FileContent),
Symlink(Vec<u8>),
HardLink {
target: Vec<u8>,
},
CharDevice {
major: u32,
minor: u32,
},
BlockDevice {
major: u32,
minor: u32,
},
Fifo,
Socket,
}
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct SourceEntry {
pub path: Vec<u8>,
pub kind: EntryKind,
pub meta: Metadata,
pub xattrs: Vec<Xattr>,
}
pub trait Source {
fn into_entries(self) -> Vec<SourceEntry>;
}
#[derive(Clone, Default, Debug)]
pub struct TreeBuilder {
entries: Vec<SourceEntry>,
}
impl TreeBuilder {
#[must_use]
pub fn new() -> Self {
Self::default()
}
#[must_use]
pub fn directory(mut self, path: impl Into<Vec<u8>>, meta: Metadata) -> Self {
self.push(path, EntryKind::Directory, meta);
self
}
#[must_use]
pub fn root(mut self, meta: Metadata) -> Self {
self.push(b"/".to_vec(), EntryKind::Directory, meta);
self
}
#[must_use]
pub fn file(
mut self,
path: impl Into<Vec<u8>>,
contents: impl Into<FileContent>,
meta: Metadata,
) -> Self {
self.push(path, EntryKind::File(contents.into()), meta);
self
}
#[must_use]
pub fn symlink(
mut self,
path: impl Into<Vec<u8>>,
target: impl Into<Vec<u8>>,
meta: Metadata,
) -> Self {
self.push(path, EntryKind::Symlink(target.into()), meta);
self
}
#[must_use]
pub fn hardlink(
mut self,
path: impl Into<Vec<u8>>,
target: impl Into<Vec<u8>>,
meta: Metadata,
) -> Self {
self.push(
path,
EntryKind::HardLink {
target: target.into(),
},
meta,
);
self
}
#[must_use]
pub fn char_device(
mut self,
path: impl Into<Vec<u8>>,
major: u32,
minor: u32,
meta: Metadata,
) -> Self {
self.push(path, EntryKind::CharDevice { major, minor }, meta);
self
}
#[must_use]
pub fn block_device(
mut self,
path: impl Into<Vec<u8>>,
major: u32,
minor: u32,
meta: Metadata,
) -> Self {
self.push(path, EntryKind::BlockDevice { major, minor }, meta);
self
}
#[must_use]
pub fn fifo(mut self, path: impl Into<Vec<u8>>, meta: Metadata) -> Self {
self.push(path, EntryKind::Fifo, meta);
self
}
#[must_use]
pub fn socket(mut self, path: impl Into<Vec<u8>>, meta: Metadata) -> Self {
self.push(path, EntryKind::Socket, meta);
self
}
#[must_use]
pub fn xattr(mut self, name: impl Into<Vec<u8>>, value: impl Into<Vec<u8>>) -> Self {
if let Some(entry) = self.entries.last_mut() {
entry.xattrs.push(Xattr {
name: name.into(),
value: value.into(),
});
}
self
}
fn push(&mut self, path: impl Into<Vec<u8>>, kind: EntryKind, meta: Metadata) {
self.entries.push(SourceEntry {
path: path.into(),
kind,
meta,
xattrs: Vec::new(),
});
}
}
impl Source for TreeBuilder {
fn into_entries(self) -> Vec<SourceEntry> {
self.entries
}
}
#[cfg(test)]
mod tests {
use super::*;
fn meta() -> Metadata {
Metadata::new(0o644, Timestamp::from_secs(1_700_000_000))
}
#[test]
fn builder_collects_entries_in_addition_order() {
let src = TreeBuilder::new()
.directory(b"/etc".to_vec(), Metadata::new(0o755, meta().mtime))
.file(b"/etc/hostname".to_vec(), b"host\n".to_vec(), meta())
.symlink(b"/etc/mtab".to_vec(), b"/proc/mounts".to_vec(), meta());
let entries = src.into_entries();
assert_eq!(entries.len(), 3);
assert_eq!(entries[0].path, b"/etc");
assert!(matches!(entries[1].kind, EntryKind::File(_)));
assert!(matches!(entries[2].kind, EntryKind::Symlink(_)));
}
#[test]
fn metadata_ownership_builder() {
let m = Metadata::new(0o600, Timestamp::from_secs(0)).owned_by(1000, 1000);
assert_eq!(m.uid, 1000);
assert_eq!(m.gid, 1000);
assert_eq!(m.mode, 0o600);
}
#[test]
fn hardlink_records_its_target() {
let src = TreeBuilder::new()
.file(b"/a".to_vec(), b"x".to_vec(), meta())
.hardlink(b"/b".to_vec(), b"/a".to_vec(), meta());
let entries = src.into_entries();
match &entries[1].kind {
EntryKind::HardLink { target } => assert_eq!(target, b"/a"),
other => panic!("expected hardlink, got {other:?}"),
}
}
#[test]
fn device_fifo_and_socket_kinds_are_recorded() {
let entries = TreeBuilder::new()
.char_device(b"/dev/null".to_vec(), 1, 3, meta())
.block_device(b"/dev/sda".to_vec(), 8, 0, meta())
.fifo(b"/run/pipe".to_vec(), meta())
.socket(b"/run/sock".to_vec(), meta())
.into_entries();
assert!(matches!(
entries[0].kind,
EntryKind::CharDevice { major: 1, minor: 3 }
));
assert!(matches!(
entries[1].kind,
EntryKind::BlockDevice { major: 8, minor: 0 }
));
assert!(matches!(entries[2].kind, EntryKind::Fifo));
assert!(matches!(entries[3].kind, EntryKind::Socket));
}
#[test]
fn xattr_attaches_to_the_most_recent_entry() {
let entries = TreeBuilder::new()
.file(b"/bin/ping".to_vec(), b"elf".to_vec(), meta())
.xattr(b"security.capability".to_vec(), vec![1, 2, 3, 4])
.xattr(b"user.note".to_vec(), b"hi".to_vec())
.file(b"/plain".to_vec(), b"x".to_vec(), meta())
.into_entries();
assert_eq!(entries[0].xattrs.len(), 2);
assert_eq!(entries[0].xattrs[0].name, b"security.capability");
assert!(entries[1].xattrs.is_empty());
}
#[test]
fn xattr_without_a_preceding_entry_is_a_no_op() {
let entries = TreeBuilder::new()
.xattr(b"user.orphan".to_vec(), b"v".to_vec())
.into_entries();
assert!(entries.is_empty());
}
#[test]
fn every_borrowed_form_of_a_files_bytes_converts() {
let want = EntryKind::File(FileContent::Owned(b"hi".to_vec()));
let entries = TreeBuilder::new()
.file(b"/vec".to_vec(), b"hi".to_vec(), meta())
.file(b"/array".to_vec(), b"hi", meta())
.file(b"/slice".to_vec(), &b"hi"[..], meta())
.file(b"/str".to_vec(), "hi", meta())
.file(b"/string".to_vec(), String::from("hi"), meta())
.into_entries();
assert_eq!(entries.len(), 5);
for entry in &entries {
assert_eq!(
entry.kind,
want,
"{} converted to something else",
String::from_utf8_lossy(&entry.path)
);
}
}
#[test]
fn reading_owned_contents_borrows_rather_than_copies() {
let content = FileContent::Owned(vec![9u8; 64]);
let FileContent::Owned(held) = &content else {
unreachable!("the content is owned")
};
let read = content.read().expect("an owned read cannot fail");
assert!(matches!(read, std::borrow::Cow::Borrowed(_)));
assert!(std::ptr::eq(read.as_ptr(), held.as_ptr()));
}
#[test]
fn a_short_backing_file_names_itself_and_the_range() {
let dir = tempfile::tempdir().expect("temp dir");
let path = dir.path().join("shrunk.tar");
std::fs::write(&path, b"only eight").expect("write the backing file");
let file = Arc::new(File::open(&path).expect("open the backing file"));
let range = FileRange::new(Arc::clone(&file), &path, 4, 4096);
let err = range
.read()
.expect_err("a range past the end cannot be read");
let text = err.to_string();
assert!(text.contains("shrunk.tar"), "{text}");
assert!(text.contains("4096"), "{text}");
assert!(text.contains("offset 4"), "{text}");
assert_eq!(err.kind(), std::io::ErrorKind::UnexpectedEof);
let content = FileContent::Range(FileRange::new(file, &path, 5, 5));
let read = content.read().expect("read the range");
assert!(matches!(read, std::borrow::Cow::Owned(_)));
assert_eq!(read.as_ref(), b"eight");
}
#[test]
fn distinct_times_are_preserved() {
let m = Metadata::new(0o644, Timestamp::from_secs(100)).with_times(
Timestamp::from_secs(1),
Timestamp::from_secs(2),
Timestamp::from_secs(3),
);
assert_eq!(m.atime, Timestamp::from_secs(1));
assert_eq!(m.ctime, Timestamp::from_secs(2));
assert_eq!(m.mtime, Timestamp::from_secs(3));
}
}