use std::io;
use std::ops::Range;
use std::path::{Path, PathBuf};
use futures_lite::io::{AsyncReadExt, Cursor};
use ostrya_core::{
Checksum, DirMeta, DirTree, FileHeader, MAX_METADATA_SIZE, ObjectName, ObjectType, Xattrs,
};
use ostrya_gvariant::Value;
use super::hash::{FileId, open_regular};
use super::{EntryMeta, ScanOptions, invalid_data, walk};
use crate::error::{Error, Result};
use crate::proto::Encoding;
use crate::session::{BoxFuture, ObjectData, ObjectSource, PushProgress};
#[derive(Debug, Clone, Copy)]
pub(super) struct Hashed {
pub(super) checksum: Checksum,
pub(super) size: u64,
}
struct Meta {
uid: u32,
gid: u32,
mode: u32,
xattrs: Xattrs,
}
enum NodeData {
Dir {
meta: Meta,
children: Range<u32>,
sums: Option<(Checksum, Checksum)>,
},
Content {
meta: Meta,
id: FileId,
target: Option<Box<str>>,
hashed: Option<Hashed>,
},
}
struct Node {
parent: u32,
name: Box<str>,
data: NodeData,
}
pub struct TreeModel {
root: PathBuf,
nodes: Vec<Node>,
sources: Sources,
commit: Option<CommitObject>,
}
struct CommitObject {
checksum: Checksum,
bytes: Vec<u8>,
detached: Option<Value>,
}
#[derive(Default)]
struct Sources {
files: Vec<u32>,
dirtrees: Vec<u32>,
dirmetas: Vec<u32>,
}
impl TreeModel {
pub async fn scan(root: &Path, options: ScanOptions) -> Result<TreeModel> {
walk::scan(root, options, None).await
}
pub(crate) async fn scan_with(
root: &Path,
options: ScanOptions,
progress: Option<&PushProgress>,
) -> Result<TreeModel> {
walk::scan(root, options, progress).await
}
pub fn root_dirtree(&self) -> Checksum {
self.dir_sums(0).0
}
pub fn root_dirmeta(&self) -> Checksum {
self.dir_sums(0).1
}
pub fn object_names(&self) -> Vec<ObjectName> {
let Sources {
files,
dirtrees,
dirmetas,
} = &self.sources;
let mut names = Vec::with_capacity(files.len() + dirtrees.len() + dirmetas.len());
for (index, node) in (0u32..).zip(&self.nodes) {
for name in node_names(node).into_iter().flatten() {
if self.source(&name) == Some(index) {
names.push(name);
}
}
}
names
}
pub fn set_commit(&mut self, checksum: Checksum, bytes: Vec<u8>, detached: Option<Value>) {
self.commit = Some(CommitObject {
checksum,
bytes,
detached,
});
}
pub(super) fn new(root: &Path) -> TreeModel {
TreeModel {
root: root.to_path_buf(),
nodes: Vec::new(),
sources: Sources::default(),
commit: None,
}
}
pub(super) fn root(&self) -> &Path {
&self.root
}
pub(super) fn next_index(&self) -> io::Result<u32> {
u32::try_from(self.nodes.len())
.map_err(|_| invalid_data("the tree holds more entries than the model can index"))
}
pub(super) fn push_root(&mut self, meta: EntryMeta, id: FileId) {
self.push(0, String::new(), meta, id);
}
pub(super) fn push(&mut self, parent: u32, name: String, meta: EntryMeta, id: FileId) {
let EntryMeta {
kind,
uid,
gid,
mode,
xattrs,
symlink_target,
} = meta;
let meta = Meta {
uid,
gid,
mode,
xattrs,
};
let data = match kind {
super::EntryKind::Dir => NodeData::Dir {
meta,
children: 0..0,
sums: None,
},
super::EntryKind::File | super::EntryKind::Symlink => NodeData::Content {
meta,
id,
target: symlink_target.map(String::into_boxed_str),
hashed: None,
},
};
self.nodes.push(Node {
parent,
name: name.into_boxed_str(),
data,
});
}
pub(super) fn hash_symlink(&mut self, index: u32) -> io::Result<()> {
let checksum = ostrya_core::ContentHasher::new(&self.header(index))
.map_err(|e| invalid_data(e.to_string()))?
.finish();
self.set_hashed(index, Hashed { checksum, size: 0 });
Ok(())
}
pub(super) fn set_children(&mut self, dir: u32, range: Range<u32>) {
if let NodeData::Dir { children, .. } = &mut self.node_mut(dir).data {
*children = range;
}
}
pub(super) fn children(&self, dir: u32) -> Range<u32> {
match &self.node(dir).data {
NodeData::Dir { children, .. } => children.clone(),
NodeData::Content { .. } => 0..0,
}
}
pub(super) fn is_dir(&self, index: u32) -> bool {
matches!(self.node(index).data, NodeData::Dir { .. })
}
pub(super) fn rel_path(&self, index: u32, out: &mut String) {
out.clear();
for i in self.chain(index) {
if !out.is_empty() {
out.push('/');
}
out.push_str(&self.node(i).name);
}
}
pub(super) fn os_path(&self, index: u32) -> PathBuf {
let mut path = self.root.clone();
for i in self.chain(index) {
path.push(&*self.node(i).name);
}
path
}
pub(super) fn file_id(&self, index: u32) -> FileId {
match &self.node(index).data {
NodeData::Content { id, .. } => *id,
NodeData::Dir { .. } => unreachable!("a directory is not opened as a file"),
}
}
pub(super) fn header(&self, index: u32) -> FileHeader {
match &self.node(index).data {
NodeData::Content { meta, target, .. } => FileHeader {
uid: meta.uid,
gid: meta.gid,
mode: meta.mode,
symlink_target: target.as_deref().unwrap_or_default().to_owned(),
xattrs: meta.xattrs.clone(),
},
NodeData::Dir { .. } => unreachable!("a directory has no file header"),
}
}
pub(super) fn finish(&mut self) -> Result<()> {
for index in (0..self.next_index_unchecked()).rev() {
if !self.is_dir(index) {
continue;
}
let sums = self.hash_dir(index).map_err(|source| Error::Walk {
path: self.os_path(index),
source,
})?;
if let NodeData::Dir { sums: slot, .. } = &mut self.node_mut(index).data {
*slot = Some(sums);
}
}
let mut sources = Sources::default();
for (index, node) in (0u32..).zip(&self.nodes) {
match node.data {
NodeData::Content { .. } => sources.files.push(index),
NodeData::Dir { .. } => {
sources.dirtrees.push(index);
sources.dirmetas.push(index);
}
}
}
for (list, ty) in [
(&mut sources.files, ObjectType::File),
(&mut sources.dirtrees, ObjectType::DirTree),
(&mut sources.dirmetas, ObjectType::DirMeta),
] {
list.sort_by_key(|&index| self.object_checksum(index, ty));
list.dedup_by_key(|index| self.object_checksum(*index, ty));
list.shrink_to_fit();
}
self.sources = sources;
Ok(())
}
fn source(&self, name: &ObjectName) -> Option<u32> {
let list = match name.ty {
ObjectType::File => &self.sources.files,
ObjectType::DirTree => &self.sources.dirtrees,
ObjectType::DirMeta => &self.sources.dirmetas,
_ => return None,
};
list.binary_search_by_key(&name.checksum, |&index| {
self.object_checksum(index, name.ty)
})
.ok()
.map(|k| list[k])
}
fn object_checksum(&self, index: u32, ty: ObjectType) -> Checksum {
match (&self.node(index).data, ty) {
(
NodeData::Content {
hashed: Some(hashed),
..
},
ObjectType::File,
) => hashed.checksum,
(NodeData::Dir { .. }, ObjectType::DirTree) => self.dir_sums(index).0,
(NodeData::Dir { .. }, ObjectType::DirMeta) => self.dir_sums(index).1,
_ => unreachable!("each source entry is hashed and gives an object of its type"),
}
}
fn dirtree(&self, dir: u32) -> DirTree {
let mut tree = DirTree::default();
for index in self.children(dir) {
let node = self.node(index);
let name = node.name.to_string();
match &node.data {
NodeData::Content { hashed, .. } => {
let hashed = hashed.expect("each kept file is hashed before its directory");
tree.files.push((name, hashed.checksum));
}
NodeData::Dir { .. } => {
let (dirtree, dirmeta) = self.dir_sums(index);
tree.dirs.push((name, dirtree, dirmeta));
}
}
}
tree.files.sort_unstable_by(|a, b| a.0.cmp(&b.0));
tree.dirs.sort_unstable_by(|a, b| a.0.cmp(&b.0));
tree
}
fn dirmeta(&self, dir: u32) -> DirMeta {
match &self.node(dir).data {
NodeData::Dir { meta, .. } => DirMeta {
uid: meta.uid,
gid: meta.gid,
mode: meta.mode,
xattrs: meta.xattrs.clone(),
},
NodeData::Content { .. } => unreachable!("a file has no dirmeta"),
}
}
fn hash_dir(&self, dir: u32) -> io::Result<(Checksum, Checksum)> {
let dirtree = self
.dirtree(dir)
.serialize()
.map_err(|e| invalid_data(e.to_string()))?;
check_metadata_size(&dirtree, "dirtree", MAX_METADATA_SIZE)?;
let dirmeta = self
.dirmeta(dir)
.serialize()
.map_err(|e| invalid_data(e.to_string()))?;
check_metadata_size(&dirmeta, "dirmeta", MAX_METADATA_SIZE)?;
Ok((Checksum::sha256(&dirtree), Checksum::sha256(&dirmeta)))
}
fn commit_object(&self, checksum: &Checksum) -> Result<&CommitObject> {
match &self.commit {
Some(commit) if commit.checksum == *checksum => Ok(commit),
Some(commit) => Err(Error::InvalidInput(format!(
"commit {checksum} is not the commit {} of the tree model",
commit.checksum
))),
None => Err(Error::InvalidInput(format!(
"commit {checksum} is not in the tree model, which has no commit"
))),
}
}
async fn open_object(&self, name: &ObjectName) -> Result<ObjectData> {
if name.ty == ObjectType::Commit {
let commit = self.commit_object(&name.checksum)?;
return Ok(encoded(commit.bytes.clone()));
}
let index = self.source(name).ok_or_else(|| {
Error::InvalidInput(format!(
"object {} of type {:?} is not in the tree model",
name.checksum, name.ty
))
})?;
let bytes = match name.ty {
ObjectType::File => return self.open_content(index).await,
ObjectType::DirTree => self.dirtree(index).serialize(),
ObjectType::DirMeta => self.dirmeta(index).serialize(),
_ => unreachable!("the model is the source of three object types alone"),
};
let bytes = bytes.map_err(|e| Error::Walk {
path: self.os_path(index),
source: invalid_data(e.to_string()),
})?;
Ok(encoded(bytes))
}
fn content_bytes(&self, names: &[ObjectName]) -> Result<u64> {
let mut total = 0u64;
for name in names.iter().filter(|n| n.ty == ObjectType::File) {
let hashed = self
.source(name)
.and_then(|index| match &self.node(index).data {
NodeData::Content { hashed, .. } => *hashed,
NodeData::Dir { .. } => None,
});
let Some(hashed) = hashed else {
return Err(Error::InvalidInput(format!(
"object {} of type {:?} is not in the tree model",
name.checksum, name.ty
)));
};
total = total.saturating_add(hashed.size);
}
Ok(total)
}
async fn open_content(&self, index: u32) -> Result<ObjectData> {
let NodeData::Content {
target,
hashed: Some(hashed),
..
} = &self.node(index).data
else {
unreachable!("each source of a content object is a hashed file or symlink");
};
let header = self.header(index);
if target.is_some() {
return Ok(ObjectData::Content {
header,
size: 0,
payload: None,
});
}
let path = self.os_path(index);
let id = self.file_id(index);
let open_path = path.clone();
let (file, _len) = ostrya_rt::unblock(move || open_regular(&open_path, &id))
.await
.map_err(|source| Error::Walk { path, source })?;
Ok(ObjectData::Content {
header,
size: hashed.size,
payload: Some(Box::new(
ostrya_rt::FileReader::with_len_hint(file, hashed.size)
.take(hashed.size.saturating_add(1)),
)),
})
}
fn dir_sums(&self, dir: u32) -> (Checksum, Checksum) {
match &self.node(dir).data {
NodeData::Dir {
sums: Some(sums), ..
} => *sums,
_ => unreachable!("each directory is hashed after its subdirectories"),
}
}
pub(super) fn set_hashed(&mut self, index: u32, result: Hashed) {
if let NodeData::Content { hashed, .. } = &mut self.node_mut(index).data {
*hashed = Some(result);
}
}
fn next_index_unchecked(&self) -> u32 {
self.nodes.len() as u32
}
fn chain(&self, mut index: u32) -> Vec<u32> {
let mut chain = Vec::new();
while index != 0 {
chain.push(index);
index = self.node(index).parent;
}
chain.reverse();
chain
}
fn node(&self, index: u32) -> &Node {
&self.nodes[index as usize]
}
fn node_mut(&mut self, index: u32) -> &mut Node {
&mut self.nodes[index as usize]
}
}
fn node_names(node: &Node) -> [Option<ObjectName>; 2] {
match &node.data {
NodeData::Content { hashed, .. } => [
hashed.map(|h| ObjectName::new(h.checksum, ObjectType::File)),
None,
],
NodeData::Dir { sums, .. } => match sums {
Some((dirtree, dirmeta)) => [
Some(ObjectName::new(*dirtree, ObjectType::DirTree)),
Some(ObjectName::new(*dirmeta, ObjectType::DirMeta)),
],
None => [None, None],
},
}
}
impl ObjectSource for TreeModel {
fn objects<'a>(&'a self, commit: &'a Checksum) -> BoxFuture<'a, Result<Vec<ObjectName>>> {
Box::pin(async move {
let commit = self.commit_object(commit)?;
let mut names = self.object_names();
names.push(ObjectName::new(commit.checksum, ObjectType::Commit));
Ok(names)
})
}
fn open<'a>(
&'a self,
name: &'a ObjectName,
_encoding: Encoding,
) -> BoxFuture<'a, Result<ObjectData>> {
Box::pin(self.open_object(name))
}
fn detached_metadata<'a>(
&'a self,
commit: &'a Checksum,
) -> BoxFuture<'a, Result<Option<Value>>> {
Box::pin(async move { Ok(self.commit_object(commit)?.detached.clone()) })
}
fn content_size<'a>(
&'a self,
names: &'a [ObjectName],
_encoding: Encoding,
) -> BoxFuture<'a, Result<Option<u64>>> {
Box::pin(async move { self.content_bytes(names).map(Some) })
}
}
fn encoded(bytes: Vec<u8>) -> ObjectData {
ObjectData::Encoded {
encoding: Encoding::Raw,
reader: Box::new(Cursor::new(bytes)),
}
}
fn check_metadata_size(bytes: &[u8], what: &str, limit: u64) -> io::Result<()> {
if bytes.len() as u64 > limit {
return Err(invalid_data(format!(
"the {what} object of the directory is {} bytes, over the limit of {limit}",
bytes.len()
)));
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
fn assert_limit(bytes: &[u8], what: &str) {
let len = bytes.len() as u64;
check_metadata_size(bytes, what, len).unwrap();
let err = check_metadata_size(bytes, what, len - 1).unwrap_err();
assert_eq!(err.kind(), io::ErrorKind::InvalidData);
assert!(err.to_string().contains(what), "{err}");
}
#[test]
fn a_dirtree_one_byte_over_the_limit_is_invalid_data() {
let dirtree = DirTree {
files: ["a", "b", "c"]
.into_iter()
.map(|name| (name.to_owned(), Checksum::sha256(name.as_bytes())))
.collect(),
dirs: vec![(
"d".to_owned(),
Checksum::sha256(b"dirtree"),
Checksum::sha256(b"dirmeta"),
)],
};
assert_limit(&dirtree.serialize().unwrap(), "dirtree");
}
#[test]
fn a_dirmeta_one_byte_over_the_limit_is_invalid_data() {
let dirmeta = DirMeta {
uid: 0,
gid: 0,
mode: 0o40755,
xattrs: Xattrs::new([(b"user.test\0".to_vec(), vec![7; 100])]).unwrap(),
};
assert_limit(&dirmeta.serialize().unwrap(), "dirmeta");
}
}