use std::io;
use std::path::{Path, PathBuf};
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
enum Phase {
Write,
Flush,
Sync,
Validate,
Commit,
SyncParent,
}
trait Backend {
type Staged;
fn stage(&mut self, path: &Path) -> io::Result<Self::Staged>;
fn same_parent(&mut self, a: &Self::Staged, b: &Self::Staged) -> io::Result<bool>;
fn claim_name(&mut self, holder: &Self::Staged, staged: &Self::Staged) -> io::Result<bool>;
fn step(&mut self, staged: &mut Self::Staged, phase: Phase, bytes: &[u8]) -> io::Result<()>;
fn cleanup(&mut self, staged: &mut Self::Staged) -> io::Result<()>;
}
#[derive(Debug, Default)]
pub struct PublishReport {
pub warnings: Vec<String>,
}
#[derive(Debug)]
pub struct PublishError {
pub committed: Vec<PathBuf>,
pub durability_uncertain: bool,
pub source: io::Error,
pub warnings: Vec<String>,
}
impl std::fmt::Display for PublishError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{}; committed outputs: {:?}{}",
self.source,
self.committed,
if self.durability_uncertain {
"; durability uncertain"
} else {
""
}
)
}
}
impl std::error::Error for PublishError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
Some(&self.source)
}
}
fn finish(
result: Result<(), PublishError>,
warnings: Vec<String>,
) -> Result<PublishReport, PublishError> {
match result {
Ok(()) => Ok(PublishReport { warnings }),
Err(mut error) => {
error.warnings = warnings;
Err(error)
}
}
}
fn transaction<B: Backend>(
backend: &mut B,
files: &[(PathBuf, &[u8])],
) -> Result<PublishReport, PublishError> {
let mut staged: Vec<(PathBuf, B::Staged)> = Vec::with_capacity(files.len());
let mut committed = Vec::new();
let failure = |source: io::Error, path: &Path, committed: &[PathBuf], durability_uncertain| {
PublishError {
source: io::Error::new(
source.kind(),
format!("writing {}: {source}", path.display()),
),
committed: committed.to_vec(),
durability_uncertain,
warnings: vec![],
}
};
let result = (|| {
let mut holders: Vec<usize> = Vec::new();
for (path, bytes) in files {
let held = backend
.stage(path)
.map_err(|e| failure(e, path, &committed, false))?;
staged.push((path.clone(), held));
let index = staged.len() - 1;
let mut holder = None;
for &candidate in &holders {
if backend
.same_parent(&staged[candidate].1, &staged[index].1)
.map_err(|e| failure(e, path, &committed, false))?
{
holder = Some(candidate);
break;
}
}
let holder = holder.unwrap_or_else(|| {
holders.push(index);
index
});
if backend
.claim_name(&staged[holder].1, &staged[index].1)
.map_err(|e| failure(e, path, &committed, false))?
{
return Err(failure(
io::Error::new(io::ErrorKind::InvalidInput, "duplicate output path"),
path,
&committed,
false,
));
}
let (_, held) = staged.last_mut().expect("just staged");
for phase in [Phase::Write, Phase::Flush, Phase::Sync, Phase::Validate] {
backend
.step(held, phase, bytes)
.map_err(|e| failure(e, path, &committed, false))?;
}
}
for (path, held) in &mut staged {
backend
.step(held, Phase::Commit, &[])
.map_err(|e| failure(e, path, &committed, false))?;
committed.push(path.clone());
backend
.step(held, Phase::SyncParent, &[])
.map_err(|e| failure(e, path, &committed, true))?;
}
Ok(())
})();
let warnings = staged
.iter_mut()
.filter_map(|(path, held)| {
backend
.cleanup(held)
.err()
.map(|error| format!("cleaning staging for {}: {error}", path.display()))
})
.collect();
finish(result, warnings)
}
pub struct OutputRoot {
dir: cap_std::fs::Dir,
}
impl OutputRoot {
pub fn open(root: &Path) -> io::Result<Self> {
Ok(Self {
dir: cap_std::fs::Dir::open_ambient_dir(root, cap_std::ambient_authority())?,
})
}
pub fn read(&self, relative: &Path) -> io::Result<Vec<u8>> {
self.dir.read(relative)
}
pub fn publish(
&self,
files: &[(PathBuf, &[u8])],
overwrite: bool,
) -> Result<PublishReport, PublishError> {
for (path, _) in files {
validate_relative(path).map_err(|source| PublishError {
committed: vec![],
durability_uncertain: false,
warnings: vec![],
source,
})?;
}
transaction(
&mut FsBackend {
root: Some(&self.dir),
overwrite,
#[cfg(test)]
fail: None,
#[cfg(test)]
fail_cleanup: false,
},
files,
)
}
}
pub fn publish_paths(
files: &[(PathBuf, &[u8])],
overwrite: bool,
) -> Result<PublishReport, PublishError> {
transaction(
&mut FsBackend {
root: None,
overwrite,
#[cfg(test)]
fail: None,
#[cfg(test)]
fail_cleanup: false,
},
files,
)
}
fn validate_relative(path: &Path) -> io::Result<()> {
let bad = || {
io::Error::new(
io::ErrorKind::InvalidInput,
format!("unsafe export path: {}", path.display()),
)
};
let text = path.to_str().ok_or_else(bad)?;
if text.is_empty() {
return Err(bad());
}
for part in text.split('/') {
if part.is_empty()
|| part == "."
|| part == ".."
|| part.len() > 255
|| part.ends_with(['.', ' '])
|| part.chars().any(|c| {
c.is_control()
|| matches!(
c,
'\\' | ':' | '%' | '<' | '>' | '"' | '|' | '?' | '*'
| '\u{061c}' | '\u{200e}' | '\u{200f}'
| '\u{202a}'..='\u{202e}' | '\u{2066}'..='\u{2069}'
| '\u{feff}'
)
})
{
return Err(bad());
}
let stem = part.split('.').next().unwrap_or("").to_ascii_uppercase();
if matches!(
stem.as_str(),
"CON" | "PRN" | "AUX" | "NUL" | "CONIN$" | "CONOUT$"
) || ["COM", "LPT"].iter().any(|prefix| {
stem.strip_prefix(prefix).is_some_and(|n| {
matches!(
n,
"1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" | "¹" | "²" | "³"
)
})
}) {
return Err(bad());
}
}
Ok(())
}
struct FsBackend<'a> {
root: Option<&'a cap_std::fs::Dir>,
overwrite: bool,
#[cfg(test)]
fail: Option<Phase>,
#[cfg(test)]
fail_cleanup: bool,
}
pub struct StagedFile {
path: PathBuf,
parent: cap_std::fs::Dir,
scratch: Option<cap_std::fs::Dir>,
scratch_name: Option<String>,
file: Option<cap_std::fs::File>,
target: std::ffi::OsString,
overwrite: bool,
#[cfg(test)]
fail_cleanup: bool,
}
impl StagedFile {
pub fn prepare(path: &Path, bytes: &[u8], overwrite: bool) -> io::Result<Self> {
let mut backend = FsBackend {
root: None,
overwrite,
#[cfg(test)]
fail: None,
#[cfg(test)]
fail_cleanup: false,
};
let mut staged = backend.stage(path)?;
for phase in [Phase::Write, Phase::Flush, Phase::Sync, Phase::Validate] {
backend.step(&mut staged, phase, bytes)?;
}
Ok(staged)
}
pub fn commit(mut self) -> Result<PublishReport, PublishError> {
let mut backend = FsBackend {
root: None,
overwrite: self.overwrite,
#[cfg(test)]
fail: None,
#[cfg(test)]
fail_cleanup: false,
};
let result = (|| {
backend
.step(&mut self, Phase::Commit, &[])
.map_err(|source| PublishError {
committed: vec![],
durability_uncertain: false,
source,
warnings: vec![],
})?;
backend
.step(&mut self, Phase::SyncParent, &[])
.map_err(|source| PublishError {
committed: vec![self.path.clone()],
durability_uncertain: true,
source,
warnings: vec![],
})
})();
let warnings = self
.cleanup()
.err()
.map(|error| format!("cleaning staging for {}: {error}", self.path.display()))
.into_iter()
.collect();
finish(result, warnings)
}
fn cleanup(&mut self) -> io::Result<()> {
drop(self.file.take());
drop(self.scratch.take());
let Some(name) = self.scratch_name.as_ref() else {
return Ok(());
};
#[cfg(test)]
if self.fail_cleanup {
return Err(io::Error::other("injected cleanup"));
}
match self.parent.symlink_metadata(name) {
Ok(metadata) if metadata.is_symlink() => {
return Err(io::Error::new(
io::ErrorKind::PermissionDenied,
"refusing staging cleanup through a symlink",
));
}
Err(error) if error.kind() == io::ErrorKind::NotFound => {
self.scratch_name = None;
return Ok(());
}
Err(error) => return Err(error),
Ok(_) => (),
}
self.parent.remove_dir_all(name)?;
self.scratch_name = None;
Ok(())
}
}
fn sync_directory_for_platform(
windows: bool,
sync: impl FnOnce() -> io::Result<()>,
) -> io::Result<()> {
if windows { Ok(()) } else { sync() }
}
fn sync_directory_handle(dir: &cap_std::fs::Dir) -> io::Result<()> {
sync_directory_for_platform(cfg!(windows), || {
dir.open_with(".", cap_std::fs::OpenOptions::new().read(true))?
.into_std()
.sync_all()
})
}
pub fn sync_directory(path: &Path) -> io::Result<()> {
let dir = cap_std::fs::Dir::open_ambient_dir(path, cap_std::ambient_authority())?;
sync_directory_handle(&dir)
}
impl Drop for StagedFile {
fn drop(&mut self) {
let _ = self.cleanup();
}
}
#[cfg(unix)]
fn copy_replaced_xattrs(from: &cap_std::fs::File, to: &cap_std::fs::File) -> io::Result<()> {
use std::os::unix::io::{AsRawFd, RawFd};
use xattr::FileExt;
struct Handle<'a>(&'a cap_std::fs::File);
impl AsRawFd for Handle<'_> {
fn as_raw_fd(&self) -> RawFd {
self.0.as_raw_fd()
}
}
impl xattr::FileExt for Handle<'_> {}
let from = Handle(from);
let to = Handle(to);
let names = match from.list_xattr() {
Ok(names) => names,
Err(error) if error.kind() == io::ErrorKind::Unsupported => return Ok(()),
Err(error) => return Err(error),
};
for name in names {
if !replaced_xattr(&name) {
continue;
}
if let Some(value) = from.get_xattr(&name)? {
to.set_xattr(&name, &value)?;
}
}
Ok(())
}
#[cfg(all(unix, target_os = "linux"))]
fn replaced_xattr(name: &std::ffi::OsStr) -> bool {
let bytes = name.as_encoded_bytes();
bytes.starts_with(b"user.") || bytes.starts_with(b"system.posix_acl_")
}
#[cfg(all(unix, not(target_os = "linux")))]
fn replaced_xattr(name: &std::ffi::OsStr) -> bool {
let bytes = name.as_encoded_bytes();
!bytes.starts_with(b"system.") && !macos_kernel_label(bytes)
}
#[cfg(all(unix, not(target_os = "linux")))]
fn macos_kernel_label(name: &[u8]) -> bool {
name.starts_with(b"com.apple.system.")
|| matches!(
name,
b"com.apple.rootless" | b"com.apple.provenance" | b"com.apple.macl"
)
}
impl Backend for FsBackend<'_> {
type Staged = StagedFile;
fn stage(&mut self, path: &Path) -> io::Result<StagedFile> {
use cap_std::fs::{Dir, DirBuilder, OpenOptions};
let parent = path
.parent()
.filter(|p| !p.as_os_str().is_empty())
.unwrap_or(Path::new("."));
let target = path
.file_name()
.ok_or_else(|| io::Error::new(io::ErrorKind::InvalidInput, "output has no filename"))?
.to_os_string();
let parent = if let Some(root) = self.root {
let mut dir = root.try_clone()?;
for component in parent.components() {
if let std::path::Component::Normal(name) = component {
match dir.create_dir(name) {
Ok(()) => sync_directory_handle(&dir)?,
Err(e) if e.kind() == io::ErrorKind::AlreadyExists => (),
Err(e) => return Err(e),
}
dir = dir.open_dir(name)?;
}
}
dir
} else {
Dir::open_ambient_dir(parent, cap_std::ambient_authority())?
};
let mut permissions = None;
match parent.symlink_metadata(&target) {
Ok(metadata) => {
if !self.overwrite {
return Err(io::Error::new(
io::ErrorKind::AlreadyExists,
format!(
"{} exists; pass overwrite=true or --force to replace it",
path.display()
),
));
}
if metadata.is_dir() || (self.root.is_some() && metadata.is_symlink()) {
return Err(io::Error::new(
io::ErrorKind::PermissionDenied,
format!("output is a directory or symlink: {}", path.display()),
));
}
if metadata.is_file() {
permissions = Some(metadata.permissions());
}
}
Err(e) if e.kind() == io::ErrorKind::NotFound => (),
Err(e) => return Err(e),
}
#[cfg(unix)]
let source = if permissions.is_some() {
Some(parent.open(&target)?)
} else {
None
};
let builder = DirBuilder::new();
#[cfg(unix)]
let builder = {
use cap_std::fs::DirBuilderExt;
let mut builder = builder;
builder.mode(0o700);
builder
};
let name = format!(".jubarte-{}.partial", staging_suffix());
parent.create_dir_with(&name, &builder)?;
let scratch = parent.open_dir(&name).inspect_err(|_| {
let _ = parent.remove_dir(&name);
})?;
let mut staged = StagedFile {
path: path.to_path_buf(),
parent,
scratch: Some(scratch),
scratch_name: Some(name),
file: None,
target,
overwrite: self.overwrite,
#[cfg(test)]
fail_cleanup: self.fail_cleanup,
};
staged.file = Some(
staged
.scratch
.as_ref()
.expect("open staging directory")
.open_with(
"bytes",
OpenOptions::new().read(true).write(true).create_new(true),
)?,
);
if let Some(permissions) = permissions {
let file = staged.file.as_ref().expect("open staging file");
file.set_permissions(permissions)?;
#[cfg(unix)]
if let Some(source) = source.as_ref() {
copy_replaced_xattrs(source, file)?;
}
}
Ok(staged)
}
fn cleanup(&mut self, staged: &mut StagedFile) -> io::Result<()> {
staged.cleanup()
}
fn same_parent(&mut self, a: &StagedFile, b: &StagedFile) -> io::Result<bool> {
let Some(name) = a.scratch_name.as_deref() else {
return Ok(false);
};
match b.parent.symlink_metadata(name) {
Ok(_) => Ok(true),
Err(error) if error.kind() == io::ErrorKind::NotFound => Ok(false),
Err(error) => Err(error),
}
}
fn claim_name(&mut self, holder: &StagedFile, staged: &StagedFile) -> io::Result<bool> {
const NAMES: &str = "names";
let scratch = holder.scratch.as_ref().expect("open staging directory");
match scratch.create_dir(NAMES) {
Ok(()) => (),
Err(error) if error.kind() == io::ErrorKind::AlreadyExists => (),
Err(error) => return Err(error),
}
let names = scratch.open_dir(NAMES)?;
match names.symlink_metadata(&staged.target) {
Ok(_) => return Ok(true),
Err(error) if error.kind() == io::ErrorKind::NotFound => (),
Err(error) => return Err(error),
}
names.open_with(
&staged.target,
cap_std::fs::OpenOptions::new().write(true).create_new(true),
)?;
Ok(false)
}
fn step(&mut self, staged: &mut StagedFile, phase: Phase, bytes: &[u8]) -> io::Result<()> {
use std::io::{Read, Seek, SeekFrom, Write};
#[cfg(test)]
if self.fail == Some(phase) {
if phase == Phase::Commit {
staged
.scratch
.as_ref()
.expect("open staging directory")
.remove_file("bytes")?;
} else {
return Err(io::Error::other(format!("injected {phase:?}")));
}
}
let file = staged.file.as_mut().expect("open staging file");
match phase {
Phase::Write => file.write_all(bytes),
Phase::Flush => file.flush(),
Phase::Sync => file.sync_all(),
Phase::Validate => {
file.seek(SeekFrom::Start(0))?;
if file.metadata()?.len() != bytes.len() as u64 {
return Err(io::Error::new(
io::ErrorKind::InvalidData,
"staged output length mismatch",
));
}
let mut buffer = [0; 8192];
for chunk in bytes.chunks(buffer.len()) {
file.read_exact(&mut buffer[..chunk.len()])?;
if buffer[..chunk.len()] != *chunk {
return Err(io::Error::new(
io::ErrorKind::InvalidData,
"staged output bytes mismatch",
));
}
}
Ok(())
}
Phase::Commit if staged.overwrite => staged
.scratch
.as_ref()
.expect("open staging directory")
.rename("bytes", &staged.parent, &staged.target),
Phase::Commit => staged
.scratch
.as_ref()
.expect("open staging directory")
.hard_link("bytes", &staged.parent, &staged.target),
Phase::SyncParent => sync_directory_handle(&staged.parent),
}
}
}
fn staging_suffix() -> String {
use std::hash::BuildHasher;
use std::sync::atomic::{AtomicU64, Ordering};
use std::time::{SystemTime, UNIX_EPOCH};
static COUNTER: AtomicU64 = AtomicU64::new(0);
let nanos = SystemTime::now()
.duration_since(UNIX_EPOCH)
.map_or(0, |d| d.as_nanos() as u64);
let pid = u64::from(std::process::id());
let count = COUNTER.fetch_add(1, Ordering::Relaxed);
let one = std::hash::RandomState::new().hash_one((pid, count, nanos));
let two = std::hash::RandomState::new().hash_one((nanos, count, pid));
format!("{one:016x}{two:016x}")
}
#[cfg(test)]
mod tests {
use super::*;
use std::collections::BTreeMap;
#[test]
fn staging_suffixes_are_unique_and_hex() {
let suffixes: Vec<String> = (0..10_000).map(|_| staging_suffix()).collect();
let distinct: std::collections::HashSet<&String> = suffixes.iter().collect();
assert_eq!(distinct.len(), suffixes.len(), "every suffix is distinct");
for suffix in &suffixes {
assert_eq!(suffix.len(), 32, "{suffix}: 32 characters");
assert!(
suffix
.bytes()
.all(|b| b.is_ascii_digit() || (b'a'..=b'f').contains(&b)),
"{suffix}: lowercase hex only"
);
}
}
#[test]
fn cleanup_failure_is_reported_after_install_and_alongside_publish_failure() {
for phase in [
None,
Some(Phase::Write),
Some(Phase::Commit),
Some(Phase::SyncParent),
] {
let temp = tempfile::tempdir().unwrap();
let root = OutputRoot::open(temp.path()).unwrap();
let mut backend = FsBackend {
root: Some(&root.dir),
overwrite: false,
fail: phase,
fail_cleanup: true,
};
let result = transaction(&mut backend, &[("output".into(), b"Alice")]);
let installed = phase.is_none() || phase == Some(Phase::SyncParent);
assert_eq!(temp.path().join("output").exists(), installed);
if installed {
assert_eq!(root.read(Path::new("output")).unwrap(), b"Alice");
}
let warnings = match result {
Ok(report) => {
assert!(phase.is_none());
report.warnings
}
Err(error) => {
assert!(phase.is_some());
assert_eq!(
error.committed,
if installed {
vec![PathBuf::from("output")]
} else {
vec![]
}
);
assert_eq!(error.durability_uncertain, phase == Some(Phase::SyncParent));
if phase == Some(Phase::Commit) {
assert_eq!(error.source.kind(), io::ErrorKind::NotFound);
} else {
assert!(
error
.source
.to_string()
.contains(&format!("injected {:?}", phase.unwrap()))
);
}
error.warnings
}
};
assert_eq!(warnings.len(), 1);
assert!(
warnings[0].contains("injected cleanup"),
"cleanup failure was swallowed: {warnings:?}"
);
}
}
#[test]
fn prepared_commit_reports_cleanup_failure_after_install() {
let temp = tempfile::tempdir().unwrap();
let path = temp.path().join("output");
let mut staged = StagedFile::prepare(&path, b"Bob", false).unwrap();
staged.fail_cleanup = true;
let result = staged.commit();
assert_eq!(std::fs::read(path).unwrap(), b"Bob");
let report = result.unwrap();
assert_eq!(report.warnings.len(), 1);
assert!(report.warnings[0].contains("injected cleanup"));
}
#[test]
fn every_publish_path_removes_partial_directories() {
let temp = tempfile::tempdir().unwrap();
let output = temp.path().join("output");
for overwrite in [true, true, false] {
if !overwrite {
std::fs::remove_file(&output).unwrap();
}
let report = publish_paths(&[(output.clone(), b"Carol Diaz")], overwrite).unwrap();
assert!(report.warnings.is_empty(), "{report:?}");
assert_no_partials(temp.path());
assert_eq!(std::fs::read(&output).unwrap(), b"Carol Diaz");
}
for phase in [
Phase::Write,
Phase::Flush,
Phase::Sync,
Phase::Validate,
Phase::Commit,
Phase::SyncParent,
] {
let mut backend = FsBackend {
root: None,
overwrite: true,
fail: Some(phase),
fail_cleanup: false,
};
let error = transaction(
&mut backend,
&[
(output.clone(), b"Alice"),
(temp.path().join("second"), b"Bob"),
],
)
.unwrap_err();
assert!(error.warnings.is_empty(), "{error:?}");
assert_no_partials(temp.path());
}
}
fn assert_no_partials(parent: &Path) {
let partials: Vec<_> = std::fs::read_dir(parent)
.unwrap()
.map(|entry| entry.unwrap().file_name())
.filter(|name| {
let name = name.to_string_lossy();
name.starts_with(".jubarte-") && name.ends_with(".partial")
})
.collect();
assert!(partials.is_empty(), "staging leftovers: {partials:?}");
}
#[cfg(unix)]
#[test]
fn cleanup_refuses_a_swapped_scratch_symlink() {
let temp = tempfile::tempdir().unwrap();
let outside = tempfile::tempdir().unwrap();
std::fs::write(outside.path().join("keep"), b"Carol Diaz").unwrap();
let path = temp.path().join("output");
let staged = StagedFile::prepare(&path, b"Alice", false).unwrap();
let name = std::fs::read_dir(temp.path())
.unwrap()
.next()
.unwrap()
.unwrap()
.file_name();
std::fs::rename(temp.path().join(&name), temp.path().join("held")).unwrap();
std::os::unix::fs::symlink(outside.path(), temp.path().join(&name)).unwrap();
let result = staged.commit();
assert_eq!(
std::fs::read(outside.path().join("keep")).unwrap(),
b"Carol Diaz"
);
assert!(
temp.path()
.join(&name)
.symlink_metadata()
.unwrap()
.is_symlink()
);
let report = format!("{result:?}");
assert!(
report.contains("symlink"),
"swapped symlink was not reported: {report}"
);
}
#[cfg(unix)]
#[test]
fn a_forced_replacement_keeps_the_destination_user_xattr() {
let temp = tempfile::tempdir().unwrap();
let output = temp.path().join("output");
std::fs::write(&output, b"old").unwrap();
match xattr::set(&output, "user.jubarte", b"kept") {
Ok(()) => {}
Err(error) if error.kind() == std::io::ErrorKind::Unsupported => return,
Err(error) => panic!("could not set the user attribute: {error}"),
}
let report = publish_paths(&[(output.clone(), b"new")], true).unwrap();
assert!(report.warnings.is_empty(), "{report:?}");
assert_eq!(std::fs::read(&output).unwrap(), b"new");
assert_eq!(
xattr::get(&output, "user.jubarte").unwrap().as_deref(),
Some(b"kept".as_slice()),
"overwrite dropped the destination extended attribute"
);
}
#[cfg(target_os = "macos")]
#[test]
fn macos_kernel_labels_are_left_for_the_kernel() {
use std::ffi::OsStr;
for name in [
"com.apple.rootless",
"com.apple.system.Security",
"com.apple.provenance",
"com.apple.macl",
] {
assert!(!replaced_xattr(OsStr::new(name)), "{name} must stay behind");
}
for name in ["user.jubarte", "com.apple.quarantine", "com.example.tag"] {
assert!(replaced_xattr(OsStr::new(name)), "{name} must move");
}
}
#[test]
fn windows_directory_sync_is_a_noop() {
let mut called = false;
let result = sync_directory_for_platform(true, || {
called = true;
Err(io::Error::new(
io::ErrorKind::PermissionDenied,
"read-only directory",
))
});
assert!(
result.is_ok(),
"Windows directory sync must succeed: {result:?}"
);
assert!(!called, "Windows must not attempt the directory sync");
}
#[test]
fn unix_directory_sync_calls_the_operation_and_preserves_its_result() {
for kind in [None, Some(io::ErrorKind::PermissionDenied)] {
let mut called = false;
let result = sync_directory_for_platform(false, || {
called = true;
match kind {
None => Ok(()),
Some(kind) => Err(io::Error::new(kind, "read-only directory")),
}
});
assert!(called, "Unix must attempt the directory sync");
assert_eq!(result.as_ref().err().map(io::Error::kind), kind);
if let Err(error) = result {
assert_eq!(error.to_string(), "read-only directory");
}
}
}
#[test]
fn a_cap_std_directory_handle_can_be_synced() {
let temp = tempfile::tempdir().unwrap();
std::fs::create_dir(temp.path().join("sub")).unwrap();
let dir =
cap_std::fs::Dir::open_ambient_dir(temp.path(), cap_std::ambient_authority()).unwrap();
sync_directory_handle(&dir).expect("an ambient directory handle syncs");
sync_directory_handle(&dir.open_dir("sub").unwrap())
.expect("a directory opened through a handle syncs");
sync_directory(temp.path()).expect("sync_directory syncs");
}
struct Memory {
visible: BTreeMap<PathBuf, Vec<u8>>,
fail: Option<(usize, Phase)>,
count: usize,
order: Vec<(usize, Phase)>,
fail_stage: Option<usize>,
claimed: std::collections::BTreeSet<PathBuf>,
}
impl Memory {
fn new(fail: Option<(usize, Phase)>) -> Self {
Self {
visible: [
(PathBuf::from("a"), b"old a".to_vec()),
(PathBuf::from("b"), b"old b".to_vec()),
]
.into(),
fail,
count: 0,
order: vec![],
fail_stage: None,
claimed: std::collections::BTreeSet::new(),
}
}
}
impl Backend for Memory {
type Staged = (usize, PathBuf, Vec<u8>);
fn stage(&mut self, path: &Path) -> io::Result<Self::Staged> {
let id = self.count;
self.count += 1;
if self.fail_stage == Some(id) {
return Err(io::Error::other("injected staging creation failure"));
}
Ok((id, path.into(), vec![]))
}
fn cleanup(&mut self, _staged: &mut Self::Staged) -> io::Result<()> {
Ok(())
}
fn same_parent(&mut self, a: &Self::Staged, b: &Self::Staged) -> io::Result<bool> {
Ok(a.1.parent() == b.1.parent())
}
fn claim_name(
&mut self,
_holder: &Self::Staged,
staged: &Self::Staged,
) -> io::Result<bool> {
Ok(!self.claimed.insert(staged.1.clone()))
}
fn step(
&mut self,
staged: &mut Self::Staged,
phase: Phase,
bytes: &[u8],
) -> io::Result<()> {
let (id, path, held) = staged;
self.order.push((*id, phase));
if self.fail == Some((*id, phase)) {
return Err(io::Error::other(format!("injected {phase:?}")));
}
match phase {
Phase::Write => *held = bytes.to_vec(),
Phase::Commit => {
self.visible.insert(path.clone(), held.clone());
}
_ => (),
}
Ok(())
}
}
fn files() -> [(PathBuf, &'static [u8]); 2] {
[("a".into(), b"new a"), ("b".into(), b"new b")]
}
fn fs_failure(phase: Phase) {
let temp = tempfile::tempdir().unwrap();
std::fs::write(temp.path().join("output"), b"old output").unwrap();
let root = OutputRoot::open(temp.path()).unwrap();
let mut backend = FsBackend {
root: Some(&root.dir),
overwrite: true,
fail: Some(phase),
fail_cleanup: false,
};
let error = transaction(&mut backend, &[("output".into(), b"new output")])
.expect_err("configured filesystem fault must fail publication");
let installed = phase == Phase::SyncParent;
assert_eq!(
root.read(Path::new("output")).unwrap(),
if installed {
b"new output"
} else {
b"old output"
}
);
assert_eq!(
error.committed,
if installed {
vec![PathBuf::from("output")]
} else {
vec![]
}
);
assert_eq!(error.durability_uncertain, installed);
if installed {
assert!(
error.to_string().contains("durability uncertain"),
"{error}"
);
assert!(
error.source.to_string().contains("injected SyncParent"),
"{error}"
);
} else {
assert_eq!(error.source.kind(), io::ErrorKind::NotFound);
}
let remaining: Vec<_> = std::fs::read_dir(temp.path())
.unwrap()
.map(|entry| entry.unwrap().file_name())
.collect();
assert_eq!(remaining, [std::ffi::OsString::from("output")]);
}
#[test]
fn audit_d13_fs_rename_failure_preserves_prior_output_and_cleans_staging() {
fs_failure(Phase::Commit);
}
#[test]
fn audit_d13_fs_parent_sync_failure_reports_installed_output_and_cleans_staging() {
fs_failure(Phase::SyncParent);
}
#[test]
fn audit_d13_all_precommit_failures_preserve_every_prior_output() {
for phase in [Phase::Write, Phase::Flush, Phase::Sync, Phase::Validate] {
for index in 0..2 {
let mut fs = Memory::new(Some((index, phase)));
let error = transaction(&mut fs, &files()).unwrap_err();
assert!(error.source.to_string().contains("injected"), "{error}");
assert!(error.committed.is_empty());
assert!(!error.durability_uncertain);
assert_eq!(fs.visible, Memory::new(None).visible);
}
}
}
#[test]
fn audit_d13_rename_failure_never_deletes_old_and_reports_prior_commits() {
for index in 0..2 {
let mut fs = Memory::new(Some((index, Phase::Commit)));
let error = transaction(&mut fs, &files()).unwrap_err();
assert!(error.source.to_string().contains("injected"), "{error}");
assert_eq!(error.committed.len(), index);
assert_eq!(fs.visible[Path::new("b")], b"old b");
assert_eq!(
fs.visible[Path::new("a")],
if index == 0 { b"old a" } else { b"new a" }
);
}
}
#[test]
fn audit_d13_postcommit_sync_failure_reports_visible_output_and_uncertain_durability() {
let mut fs = Memory::new(Some((0, Phase::SyncParent)));
let error = transaction(&mut fs, &files()).unwrap_err();
assert_eq!(error.committed, [PathBuf::from("a")]);
assert!(error.durability_uncertain);
assert_eq!(fs.visible[Path::new("a")], b"new a");
assert_eq!(fs.visible[Path::new("b")], b"old b");
}
#[test]
fn audit_d13_validates_the_entire_set_before_the_first_commit() {
let mut fs = Memory::new(None);
transaction(&mut fs, &files()).unwrap();
assert_eq!(fs.visible[Path::new("b")], b"new b");
let first_commit = fs
.order
.iter()
.position(|(_, p)| *p == Phase::Commit)
.unwrap();
let last_validate = fs
.order
.iter()
.rposition(|(_, p)| *p == Phase::Validate)
.unwrap();
assert!(last_validate < first_commit);
}
#[test]
fn audit_d35_portable_names_cannot_smuggle_extra_path_authority() {
for path in [
"",
"PRN",
"AUX",
"CONIN$",
"CONOUT$",
"COM¹.txt",
"LPT².docx",
"a<b",
"a>b",
"a\"b",
"a|b",
"a?b",
"a*b",
"../escape",
"/absolute",
"ok/../../escape",
"C:\\escape",
"a\\b",
"NUL.txt",
"con",
"COM1.docx",
"LPT9",
"file:stream",
"trailing.",
"trailing ",
"a/../b",
"a//b",
"./a",
"a/%2e%2e/b",
"a/%2Ftmp",
"a/line\n",
"a\u{061c}b",
"a\u{200e}b",
"a\u{200f}b",
"a\u{202a}b",
"a\u{202b}b",
"a\u{202c}b",
"a\u{202d}b",
"a\u{202e}b",
"a\u{2066}b",
"a\u{2067}b",
"a\u{2068}b",
"a\u{2069}b",
"a\u{feff}b",
] {
assert!(
validate_relative(Path::new(path)).is_err(),
"accepted {path:?}"
);
}
assert!(validate_relative(Path::new(&"x".repeat(256))).is_err());
for path in [
"page-01.png",
"media/0123456789abcdef.png",
"new folder/redline.docx",
"نام\u{200c}نامه.docx",
"नाम\u{200d}पत्र.docx",
] {
assert!(validate_relative(Path::new(path)).is_ok(), "refused {path}");
}
}
#[test]
fn staging_creation_failure_and_duplicate_names_never_commit() {
let mut fs = Memory::new(None);
fs.fail_stage = Some(1);
let error = transaction(&mut fs, &files()).unwrap_err();
assert!(error.committed.is_empty());
assert_eq!(fs.visible, Memory::new(None).visible);
let mut fs = Memory::new(None);
let error = transaction(
&mut fs,
&[("a".into(), b"first".as_slice()), ("a".into(), b"second")],
)
.unwrap_err();
assert_eq!(error.source.kind(), io::ErrorKind::InvalidInput);
assert!(error.committed.is_empty());
assert_eq!(fs.visible, Memory::new(None).visible);
assert!(transaction(&mut fs, &[]).is_ok());
}
fn refused(dir: &Path, files: &[(PathBuf, &[u8])], overwrite: bool) {
let error = publish_paths(files, overwrite).unwrap_err();
assert!(
error.source.to_string().contains("duplicate output path"),
"{error}"
);
assert!(error.committed.is_empty(), "{error}");
assert_no_partials(dir);
}
fn one_name(dir: &Path, a: &str, b: &str) -> bool {
let probe = tempfile::tempdir_in(dir).unwrap();
std::fs::write(probe.path().join(a), b"").unwrap();
probe.path().join(b).exists()
}
#[test]
fn a_parent_step_spelling_of_one_destination_publishes_nothing() {
let temp = tempfile::tempdir().unwrap();
std::fs::create_dir(temp.path().join("x")).unwrap();
let (a, stepped) = (temp.path().join("a"), temp.path().join("x/../a"));
assert_ne!(a, stepped);
refused(temp.path(), &[(a.clone(), b"1"), (stepped, b"2")], false);
assert!(!a.exists(), "a refused set must not publish");
}
#[cfg(unix)]
#[test]
fn a_parent_reached_through_a_directory_symlink_is_one_destination() {
let temp = tempfile::tempdir().unwrap();
std::fs::create_dir(temp.path().join("real")).unwrap();
std::os::unix::fs::symlink("real", temp.path().join("alias")).unwrap();
refused(
temp.path(),
&[
(temp.path().join("real/x"), b"1"),
(temp.path().join("alias/x"), b"2"),
],
true,
);
assert!(!temp.path().join("real/x").exists());
}
#[cfg(unix)]
#[test]
fn a_parent_step_through_a_symlink_follows_the_filesystem() {
let temp = tempfile::tempdir().unwrap();
std::fs::create_dir_all(temp.path().join("elsewhere/sub")).unwrap();
std::fs::create_dir(temp.path().join("left")).unwrap();
std::os::unix::fs::symlink(
temp.path().join("elsewhere/sub"),
temp.path().join("left/jump"),
)
.unwrap();
refused(
temp.path(),
&[
(temp.path().join("left/jump/../out"), b"1"),
(temp.path().join("elsewhere/out"), b"2"),
],
true,
);
publish_paths(
&[
(temp.path().join("left/jump/../out"), b"1"),
(temp.path().join("left/out"), b"2"),
],
true,
)
.unwrap();
assert_eq!(
std::fs::read(temp.path().join("elsewhere/out")).unwrap(),
b"1"
);
assert_eq!(std::fs::read(temp.path().join("left/out")).unwrap(), b"2");
}
#[cfg(unix)]
#[test]
fn a_symlink_and_its_referent_are_two_destinations() {
let temp = tempfile::tempdir().unwrap();
let (link, target) = (temp.path().join("link"), temp.path().join("target"));
std::fs::write(&target, b"old").unwrap();
std::os::unix::fs::symlink("target", &link).unwrap();
publish_paths(
&[(link.clone(), b"by link"), (target.clone(), b"by target")],
true,
)
.unwrap();
assert!(!std::fs::symlink_metadata(&link).unwrap().is_symlink());
assert_eq!(std::fs::read(&link).unwrap(), b"by link");
assert_eq!(std::fs::read(&target).unwrap(), b"by target");
}
#[test]
fn case_follows_the_volume() {
let temp = tempfile::tempdir().unwrap();
let files = [
(temp.path().join("Report.txt"), &b"1"[..]),
(temp.path().join("report.txt"), &b"2"[..]),
];
if one_name(temp.path(), "Report.txt", "report.txt") {
refused(temp.path(), &files, false);
} else {
publish_paths(&files, false).unwrap();
}
}
#[test]
fn unicode_normalization_follows_the_volume() {
let temp = tempfile::tempdir().unwrap();
let (composed, decomposed) = ("caf\u{e9}", "cafe\u{301}");
let files = [
(temp.path().join(composed), &b"1"[..]),
(temp.path().join(decomposed), &b"2"[..]),
];
if one_name(temp.path(), composed, decomposed) {
refused(temp.path(), &files, false);
} else {
publish_paths(&files, false).unwrap();
}
}
#[cfg(target_os = "linux")]
#[test]
fn distinct_non_utf8_names_are_two_destinations() {
use std::os::unix::ffi::OsStrExt;
let temp = tempfile::tempdir().unwrap();
let name = |byte: u8| temp.path().join(std::ffi::OsStr::from_bytes(&[b'x', byte]));
publish_paths(&[(name(0xff), b"1"), (name(0xfe), b"2")], false).unwrap();
assert_eq!(std::fs::read(name(0xff)).unwrap(), b"1");
assert_eq!(std::fs::read(name(0xfe)).unwrap(), b"2");
}
#[cfg(unix)]
#[test]
fn an_output_root_resolves_aliases_from_the_root() {
let temp = tempfile::tempdir().unwrap();
std::fs::create_dir(temp.path().join("real")).unwrap();
std::os::unix::fs::symlink("real", temp.path().join("alias")).unwrap();
let root = OutputRoot::open(temp.path()).unwrap();
let error = root
.publish(
&[
(PathBuf::from("real/x"), b"1"),
(PathBuf::from("alias/x"), b"2"),
],
true,
)
.unwrap_err();
assert!(
error.source.to_string().contains("duplicate output path"),
"{error}"
);
assert!(error.committed.is_empty(), "{error}");
assert!(!temp.path().join("real/x").exists());
assert_no_partials(temp.path());
}
}