use std::future::Future;
use std::os::fd::{AsFd, BorrowedFd, OwnedFd};
use std::path::Path;
use std::pin::Pin;
use ostrya_core::{DirMeta, ObjectType, RepoMode, Xattrs};
use ostrya_rt::File as RtFile;
use rustix::fs::{AtFlags, Dir, FileType, Mode, OFlags};
use rustix::io::Errno;
use crate::error::{Error, Result};
use crate::modifier::{
CommitModifier, CommitModifierFlags, FilterResult, with_selinux, without_selinux,
};
use crate::mtree::MutableTree;
use crate::transaction::Transaction;
use crate::write::FileMeta;
const S_IFMT: u32 = 0o170000;
const CANONICAL_PERM_MASK: u32 = 0o755;
impl Transaction {
pub async fn write_dfd_to_mtree(
&self,
dfd: BorrowedFd<'_>,
path: &Path,
mtree: &mut MutableTree,
modifier: Option<&mut CommitModifier>,
) -> Result<()> {
if self.repo().mode() == RepoMode::BareSplitXattrs {
return Err(Error::Unsupported(
"bare-split-xattrs is read-only; the port does not write it".into(),
));
}
let flags = modifier
.as_deref()
.map_or(CommitModifierFlags::empty(), |m| m.flags);
if flags.contains(CommitModifierFlags::GENERATE_SIZES)
&& self.repo().mode() == RepoMode::Archive
{
self.mark_generate_sizes();
}
let root_fd = open_walk_root(dfd, path)?;
walk_dir(self, root_fd, mtree, modifier, "/".to_owned(), None).await?;
if flags.contains(CommitModifierFlags::CONSUME) {
remove_walk_root(dfd, path);
}
Ok(())
}
}
struct EntryInfo {
name: String,
kind: EntryKind,
dev: u64,
ino: u64,
uid: u32,
gid: u32,
mode: u32,
xattrs: Xattrs,
}
enum EntryKind {
Dir,
Regular,
Symlink(String),
}
struct DirSnapshot {
uid: u32,
gid: u32,
mode: u32,
xattrs: Xattrs,
entries: Vec<EntryInfo>,
}
type WalkFuture<'a> = Pin<Box<dyn Future<Output = Result<()>> + Send + 'a>>;
fn walk_dir<'a>(
txn: &'a Transaction,
dir_fd: OwnedFd,
node: &'a mut MutableTree,
mut modifier: Option<&'a mut CommitModifier>,
path: String,
dir_meta: Option<FileMeta>,
) -> WalkFuture<'a> {
Box::pin(async move {
let flags = modifier
.as_deref()
.map_or(CommitModifierFlags::empty(), |m| m.flags);
let skip_xattrs = flags.contains(CommitModifierFlags::SKIP_XATTRS);
let snap = {
let dir = dir_fd.as_fd().try_clone_to_owned()?;
ostrya_rt::unblock(move || snapshot_dir(dir.as_fd(), skip_xattrs)).await?
};
let dir_meta = match dir_meta {
Some(m) => m,
None => {
let base = FileMeta {
uid: snap.uid,
gid: snap.gid,
mode: snap.mode,
xattrs: snap.xattrs,
};
let canon = apply_canonical(flags, base, false);
finalize_meta(modifier.as_deref_mut(), Path::new(&path), canon)?
}
};
let dirmeta_bytes = to_dirmeta(&dir_meta).serialize()?;
let dirmeta = txn
.write_metadata(ObjectType::DirMeta, None, &dirmeta_bytes)
.await?;
node.set_metadata_checksum(dirmeta);
let consume = flags.contains(CommitModifierFlags::CONSUME);
for entry in snap.entries {
let cb_path = join_path(&path, &entry.name);
let is_symlink = matches!(entry.kind, EntryKind::Symlink(_));
let base = FileMeta {
uid: entry.uid,
gid: entry.gid,
mode: entry.mode,
xattrs: entry.xattrs,
};
let filter_meta = apply_canonical(flags, base, is_symlink);
if let Some(m) = modifier.as_deref_mut()
&& let Some(filter) = &mut m.filter
&& filter(Path::new(&cb_path), &filter_meta) == FilterResult::Skip
{
txn.note_filtered();
continue;
}
match entry.kind {
EntryKind::Regular => {
if let Some(checksum) =
devino_hit(txn, modifier.as_deref(), entry.dev, entry.ino)
{
node.replace_file(&entry.name, checksum)?;
if consume {
unlink(dir_fd.as_fd(), &entry.name, false)?;
}
continue;
}
let meta =
finalize_meta(modifier.as_deref_mut(), Path::new(&cb_path), filter_meta)?;
let fd = rustix::fs::openat(
dir_fd.as_fd(),
entry.name.as_str(),
OFlags::RDONLY | OFlags::NOFOLLOW | OFlags::CLOEXEC,
Mode::empty(),
)?;
let checksum = txn.write_content(None, &meta, RtFile::from(fd)).await?;
node.replace_file(&entry.name, checksum)?;
if consume {
unlink(dir_fd.as_fd(), &entry.name, false)?;
}
}
EntryKind::Symlink(target) => {
let meta =
finalize_meta(modifier.as_deref_mut(), Path::new(&cb_path), filter_meta)?;
let checksum = txn.write_symlink(&target, &meta, None).await?;
node.replace_file(&entry.name, checksum)?;
if consume {
unlink(dir_fd.as_fd(), &entry.name, false)?;
}
}
EntryKind::Dir => {
let meta =
finalize_meta(modifier.as_deref_mut(), Path::new(&cb_path), filter_meta)?;
let child_fd = rustix::fs::openat(
dir_fd.as_fd(),
entry.name.as_str(),
OFlags::RDONLY | OFlags::DIRECTORY | OFlags::NOFOLLOW | OFlags::CLOEXEC,
Mode::empty(),
)?;
let child = node.ensure_dir(&entry.name).await?;
walk_dir(
txn,
child_fd,
child,
modifier.as_deref_mut(),
cb_path,
Some(meta),
)
.await?;
if consume {
match rustix::fs::unlinkat(
dir_fd.as_fd(),
entry.name.as_str(),
AtFlags::REMOVEDIR,
) {
Ok(()) | Err(Errno::NOTEMPTY) => {}
Err(e) => return Err(e.into()),
}
}
}
}
}
Ok(())
})
}
fn devino_hit(
txn: &Transaction,
modifier: Option<&CommitModifier>,
dev: u64,
ino: u64,
) -> Option<ostrya_core::Checksum> {
let m = modifier?;
if !m.flags.contains(CommitModifierFlags::DEVINO_CANONICAL) {
return None;
}
let checksum = m.devino_cache.as_ref()?.get(dev, ino)?;
txn.note_devino_hit();
Some(checksum)
}
pub(crate) fn apply_canonical(
flags: CommitModifierFlags,
mut meta: FileMeta,
is_symlink: bool,
) -> FileMeta {
if flags.contains(CommitModifierFlags::CANONICAL_PERMISSIONS) {
meta.uid = 0;
meta.gid = 0;
if !is_symlink {
meta.mode = (meta.mode & S_IFMT) | (meta.mode & CANONICAL_PERM_MASK);
}
}
meta
}
fn apply_callbacks(m: &mut CommitModifier, path: &Path, mut meta: FileMeta) -> Result<FileMeta> {
if let Some(callback) = &mut m.xattr_callback {
meta.xattrs = callback(path, &meta);
}
if let Some(callback) = &mut m.label_callback {
meta.xattrs = without_selinux(&meta.xattrs)?;
match callback(path, &meta) {
Some(label) => meta.xattrs = with_selinux(&meta.xattrs, label)?,
None => {
if m.flags.contains(CommitModifierFlags::ERROR_ON_UNLABELED) {
return Err(Error::InvalidFormat(format!(
"no SELinux label for {}",
path.display()
)));
}
}
}
}
Ok(meta)
}
pub(crate) fn finalize_meta(
modifier: Option<&mut CommitModifier>,
path: &Path,
meta: FileMeta,
) -> Result<FileMeta> {
match modifier {
Some(m) => apply_callbacks(m, path, meta),
None => Ok(meta),
}
}
pub(crate) fn to_dirmeta(meta: &FileMeta) -> DirMeta {
DirMeta {
uid: meta.uid,
gid: meta.gid,
mode: meta.mode,
xattrs: meta.xattrs.clone(),
}
}
fn snapshot_dir(dir: BorrowedFd<'_>, skip_xattrs: bool) -> Result<DirSnapshot> {
let stat = rustix::fs::fstat(dir)?;
let dir_xattrs = if skip_xattrs {
Xattrs::empty()
} else {
crate::object::read_all_xattrs(dir)?
};
let mut entries = Vec::new();
for entry in Dir::read_from(dir)? {
let entry = entry?;
let name = entry.file_name();
if name == c"." || name == c".." {
continue;
}
let name = name
.to_str()
.map_err(|_| Error::InvalidFormat("directory entry name is not valid UTF-8".into()))?
.to_owned();
let stat = rustix::fs::statat(dir, name.as_str(), AtFlags::SYMLINK_NOFOLLOW)?;
let kind = match FileType::from_raw_mode(stat.st_mode) {
FileType::Directory => EntryKind::Dir,
FileType::RegularFile => EntryKind::Regular,
FileType::Symlink => {
let target = rustix::fs::readlinkat(dir, name.as_str(), Vec::new())?
.into_string()
.map_err(|_| {
Error::InvalidFormat("symlink target is not valid UTF-8".into())
})?;
EntryKind::Symlink(target)
}
_ => {
return Err(Error::Unsupported(format!(
"unsupported file type for entry {name:?}"
)));
}
};
let xattrs = if skip_xattrs {
Xattrs::empty()
} else {
read_entry_xattrs(dir, name.as_str(), &kind)?
};
entries.push(EntryInfo {
name,
kind,
dev: stat.st_dev,
ino: stat.st_ino,
uid: stat.st_uid,
gid: stat.st_gid,
mode: stat.st_mode,
xattrs,
});
}
Ok(DirSnapshot {
uid: stat.st_uid,
gid: stat.st_gid,
mode: stat.st_mode,
xattrs: dir_xattrs,
entries,
})
}
fn read_entry_xattrs(dir: BorrowedFd<'_>, name: &str, kind: &EntryKind) -> Result<Xattrs> {
let oflags = match kind {
EntryKind::Regular => OFlags::RDONLY | OFlags::NOFOLLOW | OFlags::CLOEXEC,
EntryKind::Dir => OFlags::RDONLY | OFlags::DIRECTORY | OFlags::NOFOLLOW | OFlags::CLOEXEC,
EntryKind::Symlink(_) => return crate::object::read_link_xattrs(dir, name),
};
let fd = rustix::fs::openat(dir, name, oflags, Mode::empty())?;
crate::object::read_all_xattrs(fd.as_fd())
}
fn open_walk_root(dfd: BorrowedFd<'_>, path: &Path) -> Result<OwnedFd> {
let name = if path.as_os_str().is_empty() {
Path::new(".")
} else {
path
};
Ok(rustix::fs::openat(
dfd,
name,
OFlags::RDONLY | OFlags::DIRECTORY | OFlags::NOFOLLOW | OFlags::CLOEXEC,
Mode::empty(),
)?)
}
fn remove_walk_root(dfd: BorrowedFd<'_>, path: &Path) {
if path.as_os_str().is_empty() || path == Path::new(".") {
return;
}
let _ = rustix::fs::unlinkat(dfd, path, AtFlags::REMOVEDIR);
}
fn unlink(dir: BorrowedFd<'_>, name: &str, is_dir: bool) -> Result<()> {
let flags = if is_dir {
AtFlags::REMOVEDIR
} else {
AtFlags::empty()
};
rustix::fs::unlinkat(dir, name, flags)?;
Ok(())
}
pub(crate) fn join_path(parent: &str, name: &str) -> String {
if parent == "/" {
format!("/{name}")
} else {
format!("{parent}/{name}")
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{CreateOptions, Repo};
use ostrya_rt::block_on;
struct Scratch(std::path::PathBuf);
impl Scratch {
fn new(tag: &str) -> Scratch {
use std::sync::atomic::{AtomicU64, Ordering};
static COUNTER: AtomicU64 = AtomicU64::new(0);
let n = COUNTER.fetch_add(1, Ordering::Relaxed);
let dir = std::env::temp_dir()
.join(format!("ostrya-ingest-{}-{tag}-{n}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(&dir).unwrap();
Scratch(dir)
}
}
impl Drop for Scratch {
fn drop(&mut self) {
let _ = std::fs::remove_dir_all(&self.0);
}
}
#[test]
fn generate_sizes_flag_marks_the_transaction() {
let scratch = Scratch::new("gensizes");
block_on(async {
let repo = Repo::create(
&scratch.0.join("repo"),
CreateOptions::new(RepoMode::Archive),
)
.await
.unwrap();
let txn = repo.transaction().await.unwrap();
assert!(!txn.generate_sizes(), "not marked until a walk requests it");
let src = scratch.0.join("src");
std::fs::create_dir_all(&src).unwrap();
let dfd = std::fs::File::open(&scratch.0).unwrap();
let mut mtree = MutableTree::new();
let mut modifier = CommitModifier::new(CommitModifierFlags::GENERATE_SIZES);
txn.write_dfd_to_mtree(
dfd.as_fd(),
Path::new("src"),
&mut mtree,
Some(&mut modifier),
)
.await
.unwrap();
assert!(txn.generate_sizes(), "GENERATE_SIZES marks the transaction");
txn.abort().await.unwrap();
let repo = Repo::create(
&scratch.0.join("repo-bare"),
CreateOptions::new(RepoMode::BareUser),
)
.await
.unwrap();
let txn = repo.transaction().await.unwrap();
let dfd = std::fs::File::open(&scratch.0).unwrap();
let mut mtree = MutableTree::new();
let mut modifier = CommitModifier::new(CommitModifierFlags::GENERATE_SIZES);
txn.write_dfd_to_mtree(
dfd.as_fd(),
Path::new("src"),
&mut mtree,
Some(&mut modifier),
)
.await
.unwrap();
assert!(
!txn.generate_sizes(),
"GENERATE_SIZES is a no-op outside archive mode"
);
txn.abort().await.unwrap();
});
}
}