use std::ffi::OsStr;
use std::ffi::OsString;
use std::path::Component;
use std::path::Path;
use std::rc::Rc;
use std::sync::atomic::AtomicU64;
use std::sync::atomic::Ordering;
use super::file_identity::FileIdentity;
use super::output_root::OutputRoot;
use super::output_root::Place;
use super::run_record::RunRecord;
use crate::ArchiveError;
use crate::DestinationStep;
use crate::Result;
use crate::mark::MarkRun;
use crate::security::fs_view::Occupant;
use crate::types::DestDir;
#[cfg(windows)]
pub(crate) use imp::DaclHandle;
#[cfg(windows)]
pub(crate) use imp::DirRead;
#[cfg(windows)]
pub(crate) use imp::RootDacl;
pub(crate) use imp::VettedDir;
#[cfg(unix)]
pub(crate) use imp::open_directory_at;
#[cfg(windows)]
pub(crate) use nt::read_zone_stream;
#[cfg(windows)]
pub(crate) use nt::security::DirSecurity;
#[cfg(windows)]
pub(crate) use nt::write_zone_stream;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct Leaf<'a>(&'a OsStr);
impl<'a> Leaf<'a> {
pub(crate) fn split(rel: &'a Path) -> Result<(&'a Path, Self)> {
match (rel.parent(), rel.components().next_back()) {
(Some(parent), Some(Component::Normal(name))) => Ok((parent, Self(name))),
_ => Err(ArchiveError::PathTraversal {
path: rel.to_path_buf(),
}),
}
}
pub(super) const fn from_component(component: Component<'a>) -> Option<Self> {
match component {
Component::Normal(name) => Some(Self(name)),
Component::CurDir
| Component::ParentDir
| Component::RootDir
| Component::Prefix(_) => None,
}
}
pub(crate) fn of_name(name: &'a OsStr) -> Option<Self> {
let mut components = Path::new(name).components();
match (components.next(), components.next()) {
(Some(Component::Normal(only)), None) if only == name => Some(Self(name)),
_ => None,
}
}
pub(crate) const fn as_os_str(self) -> &'a OsStr {
self.0
}
}
static TEMP_COUNTER: AtomicU64 = AtomicU64::new(0);
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct TempLeaf(OsString);
impl TempLeaf {
pub(crate) fn next() -> Self {
let counter = TEMP_COUNTER.fetch_add(1, Ordering::Relaxed);
Self(OsString::from(format!(
".exarch-tmp-{}-{counter}",
std::process::id()
)))
}
#[cfg(test)]
pub(crate) fn named(name: &str) -> Self {
assert!(
!name.contains(['/', '\\']),
"not a single component: {name}"
);
Self(OsString::from(name))
}
pub(crate) fn as_leaf(&self) -> Leaf<'_> {
Leaf(&self.0)
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct LeafBuf(OsString);
impl LeafBuf {
pub(crate) fn of(path: &Path) -> Option<Self> {
let name = path.file_name()?;
Leaf::of_name(name).map(|leaf| Self(leaf.as_os_str().to_os_string()))
}
pub(crate) fn random(prefix: &str) -> Self {
use std::hash::BuildHasher;
use std::hash::RandomState;
let bits = RandomState::new().hash_one(TEMP_COUNTER.fetch_add(1, Ordering::Relaxed));
Self(OsString::from(format!("{prefix}{bits:016x}")))
}
pub(crate) fn as_leaf(&self) -> Leaf<'_> {
Leaf(&self.0)
}
}
impl AsRef<Path> for LeafBuf {
fn as_ref(&self) -> &Path {
Path::new(&self.0)
}
}
#[cfg(unix)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct Uid(u32);
#[cfg(unix)]
impl Uid {
pub(crate) fn of(metadata: &std::fs::Metadata) -> Self {
Self(std::os::unix::fs::MetadataExt::uid(metadata))
}
pub(crate) const fn from_raw(raw: u32) -> Self {
Self(raw)
}
pub(crate) const fn get(self) -> u32 {
self.0
}
}
#[cfg(unix)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct Gid(u32);
#[cfg(unix)]
impl Gid {
pub(crate) fn of(metadata: &std::fs::Metadata) -> Self {
Self(std::os::unix::fs::MetadataExt::gid(metadata))
}
pub(crate) const fn from_raw(raw: u32) -> Self {
Self(raw)
}
pub(crate) const fn get(self) -> u32 {
self.0
}
}
#[cfg(unix)]
#[derive(Debug, Clone, Copy)]
pub(crate) struct EntryMode {
bits: u32,
uid: Uid,
gid: Gid,
}
#[cfg(unix)]
impl EntryMode {
pub(crate) const fn bits(self) -> u32 {
self.bits
}
pub(crate) const fn uid(self) -> Uid {
self.uid
}
pub(crate) const fn gid(self) -> Gid {
self.gid
}
#[cfg(test)]
pub(crate) const fn for_test(bits: u32, uid: Uid, gid: Gid) -> Self {
Self { bits, uid, gid }
}
#[allow(
clippy::useless_conversion,
reason = "`st_mode` is `u16` on macOS and `u32` on Linux"
)]
fn of_stat(stat: &rustix::fs::Stat) -> Self {
Self {
bits: u32::from(stat.st_mode) & 0o7777,
uid: Uid::from_raw(stat.st_uid),
gid: Gid::from_raw(stat.st_gid),
}
}
}
#[derive(Debug, Clone, Copy)]
pub(crate) struct EntryStatus {
pub(crate) occupant: Occupant,
pub(crate) reparse: bool,
pub(crate) identity: FileIdentity,
#[cfg(unix)]
pub(crate) mode: EntryMode,
}
#[cfg(unix)]
#[derive(Debug)]
pub(crate) enum DirHandle {
Search(std::fs::File),
Read(std::fs::File),
}
#[cfg(unix)]
impl DirHandle {
pub(crate) const fn as_file(&self) -> &std::fs::File {
match self {
Self::Search(file) | Self::Read(file) => file,
}
}
}
#[cfg(target_os = "macos")]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum DeleteAccess {
Granted,
Denied,
Unknown,
}
const fn occupant(is_dir: bool, is_symlink: bool) -> Occupant {
if is_dir {
Occupant::Directory
} else if is_symlink {
Occupant::Symlink
} else {
Occupant::File
}
}
impl From<std::fs::FileType> for Occupant {
fn from(file_type: std::fs::FileType) -> Self {
occupant(file_type.is_dir(), file_type.is_symlink())
}
}
#[cfg(any(unix, target_os = "wasi"))]
impl From<rustix::fs::FileType> for Occupant {
fn from(file_type: rustix::fs::FileType) -> Self {
occupant(
file_type == rustix::fs::FileType::Directory,
file_type == rustix::fs::FileType::Symlink,
)
}
}
#[derive(Debug)]
pub(crate) struct PinnedRoot {
dest: DestDir,
dir: VettedDir,
created: Rc<RunRecord>,
mark: Option<Rc<MarkRun>>,
}
impl PinnedRoot {
pub(crate) fn open(dest: DestDir) -> Result<Self> {
let dir = VettedDir::open_path(dest.as_path())?;
Ok(Self {
dest,
dir,
created: Rc::default(),
mark: None,
})
}
pub(crate) fn open_output(output: OutputRoot<'_>) -> Result<Self> {
let mark = output.mark();
let root = match output.place() {
Place::Path(path) => Self::open(DestDir::new_or_create(path.to_path_buf())?)?,
Place::Staged(staged) => {
let dest = DestDir::new(staged.path().to_path_buf()).map_err(|e| match e {
ArchiveError::DestinationNotAccessible { error, .. } => {
ArchiveError::destination_not_accessible(
staged.destination(),
DestinationStep::Stage,
error,
)
}
other => other,
})?;
#[cfg(unix)]
let dir = VettedDir::reopen_at(staged.root()).map_err(ArchiveError::Io)?;
#[cfg(not(unix))]
let dir = VettedDir::open_path(dest.as_path())?;
Self {
dest,
dir,
created: staged.ledger(),
mark: None,
}
}
};
Ok(Self { mark, ..root })
}
pub(crate) fn created(&self) -> &RunRecord {
&self.created
}
pub(crate) fn marking(&self) -> Option<&MarkRun> {
self.mark.as_deref().filter(|run| run.is_marking())
}
#[cfg(target_os = "macos")]
pub(super) fn mark_created_dir(&self, dir: &VettedDir) {
if let Some(run) = self.marking() {
run.mark_dir(dir);
}
}
#[cfg(not(target_os = "macos"))]
#[expect(clippy::unused_self, reason = "only macOS marks directories")]
pub(super) fn mark_created_dir(&self, _dir: &VettedDir) {}
pub(crate) const fn dest(&self) -> &DestDir {
&self.dest
}
pub(super) const fn dir(&self) -> &VettedDir {
&self.dir
}
#[cfg(test)]
pub(crate) fn open_for_test(path: &Path) -> Result<Self> {
Self::open(DestDir::new(path.to_path_buf())?)
}
}
#[cfg(any(unix, target_os = "wasi"))]
#[derive(Debug)]
#[cfg_attr(not(feature = "testing"), allow(unreachable_pub))]
pub struct WalkerBacked<'r>(
#[expect(dead_code, reason = "ties the proof to its root")] &'r PinnedRoot,
);
#[cfg(any(unix, target_os = "wasi"))]
impl PinnedRoot {
pub(crate) const fn backs_parent_probe(&self) -> WalkerBacked<'_> {
WalkerBacked(self)
}
}
#[cfg(target_os = "macos")]
#[allow(unsafe_code)] pub(crate) mod darwin_acl;
#[cfg(any(unix, target_os = "wasi"))]
mod imp {
#[cfg(unix)]
use std::ffi::OsStr;
use std::fs::File;
use std::io;
#[cfg(unix)]
use std::os::fd::AsFd;
use std::os::fd::OwnedFd;
use std::path::Path;
#[cfg(target_os = "macos")]
use rustix::fs::Access;
use rustix::fs::AtFlags;
use rustix::fs::FileType;
use rustix::fs::Mode;
use rustix::fs::OFlags;
use rustix::io::Errno;
#[cfg(target_os = "macos")]
use super::DeleteAccess;
#[cfg(unix)]
use super::DirHandle;
use super::EntryStatus;
use super::Leaf;
use super::PendingMode;
use super::TempLeaf;
use crate::formats::file_identity::FileIdentity;
#[cfg(unix)]
use crate::formats::file_identity::IdentityCheck;
use crate::formats::file_identity::IdentityKey;
use crate::formats::occupant::ReplacePermit;
use crate::security::fs_view::Occupant;
use crate::security::permissions::SanitizedMode;
#[cfg(any(target_os = "linux", target_os = "android"))]
const DIR_ACCESS: OFlags = OFlags::PATH;
#[cfg(target_vendor = "apple")]
const DIR_ACCESS: OFlags = OFlags::from_bits_retain(libc::O_SEARCH.cast_unsigned());
#[cfg(not(any(target_os = "linux", target_os = "android", target_vendor = "apple")))]
const DIR_ACCESS: OFlags = OFlags::RDONLY;
#[cfg(any(target_os = "linux", target_os = "android", target_vendor = "apple"))]
const MODE_ACCESS: OFlags = DIR_ACCESS;
#[cfg(any(target_os = "freebsd", target_os = "netbsd", target_os = "illumos"))]
const MODE_ACCESS: OFlags = OFlags::from_bits_retain(libc::O_SEARCH.cast_unsigned());
#[cfg(any(
target_os = "linux",
target_os = "android",
target_vendor = "apple",
target_os = "freebsd",
target_os = "netbsd",
target_os = "illumos"
))]
const SEARCH_UNVERIFIED: bool = cfg!(any(target_os = "netbsd", target_os = "illumos"));
fn open_directory(open: impl Fn(OFlags) -> rustix::io::Result<OwnedFd>) -> io::Result<OwnedFd> {
open_directory_with(DIR_ACCESS, open)
}
fn open_directory_with(
access: OFlags,
open: impl Fn(OFlags) -> rustix::io::Result<OwnedFd>,
) -> io::Result<OwnedFd> {
let base = OFlags::DIRECTORY | OFlags::NOFOLLOW | OFlags::CLOEXEC;
let opened = open(base | access);
#[cfg(target_vendor = "apple")]
let opened = match opened {
Err(e) if e == Errno::INVAL => open(base | OFlags::RDONLY),
other => other,
};
Ok(opened?)
}
#[cfg(unix)]
pub(crate) fn open_directory_at(dir: &impl AsFd, name: &OsStr) -> io::Result<File> {
open_directory(|flags| rustix::fs::openat(dir, name, flags, Mode::empty())).map(File::from)
}
fn entry_status(stat: &rustix::fs::Stat) -> EntryStatus {
EntryStatus {
occupant: FileType::from_raw_mode(stat.st_mode).into(),
reparse: false,
identity: IdentityKey::of_stat(stat).map_or(FileIdentity::Unknown, FileIdentity::Known),
#[cfg(unix)]
mode: super::EntryMode::of_stat(stat),
}
}
#[derive(Debug)]
pub(crate) struct VettedDir(OwnedFd);
#[cfg(unix)]
impl AsFd for VettedDir {
fn as_fd(&self) -> std::os::fd::BorrowedFd<'_> {
self.0.as_fd()
}
}
impl VettedDir {
#[cfg(unix)]
pub(crate) fn open_path(path: &Path) -> io::Result<Self> {
open_directory(|flags| rustix::fs::open(path, flags, Mode::empty())).map(Self)
}
#[cfg(target_os = "wasi")]
pub(crate) fn open_path(path: &Path) -> io::Result<Self> {
File::open(path).map(|dir| Self(OwnedFd::from(dir)))
}
pub(crate) fn reopen(&self) -> io::Result<Self> {
open_directory(|flags| rustix::fs::openat(&self.0, ".", flags, Mode::empty())).map(Self)
}
#[cfg(unix)]
pub(crate) fn reopen_at(dir: &impl AsFd) -> io::Result<Self> {
open_directory(|flags| rustix::fs::openat(dir, ".", flags, Mode::empty())).map(Self)
}
pub(crate) fn open_dir(&self, name: Leaf<'_>) -> io::Result<Self> {
open_directory(|flags| {
rustix::fs::openat(&self.0, name.as_os_str(), flags, Mode::empty())
})
.map(Self)
}
pub(crate) fn make_dir(&self, name: Leaf<'_>) -> io::Result<()> {
Ok(rustix::fs::mkdirat(
&self.0,
name.as_os_str(),
Mode::from_bits_truncate(0o777),
)?)
}
pub(crate) fn status(&self, name: Leaf<'_>) -> io::Result<Option<EntryStatus>> {
match rustix::fs::statat(&self.0, name.as_os_str(), AtFlags::SYMLINK_NOFOLLOW) {
Ok(stat) => Ok(Some(entry_status(&stat))),
Err(e) if e == Errno::NOENT => Ok(None),
Err(e) => Err(e.into()),
}
}
#[cfg(unix)]
pub(crate) fn own_status(&self) -> io::Result<EntryStatus> {
#[cfg(any(target_os = "linux", target_os = "android"))]
let stat = rustix::fs::statat(&self.0, "", AtFlags::EMPTY_PATH)?;
#[cfg(not(any(target_os = "linux", target_os = "android")))]
let stat = rustix::fs::fstat(&self.0)?;
Ok(entry_status(&stat))
}
pub(crate) fn probe(&self, name: Leaf<'_>) -> io::Result<Option<Occupant>> {
Ok(self.status(name)?.map(|status| status.occupant))
}
pub(crate) fn identity(&self, name: Leaf<'_>) -> io::Result<Option<FileIdentity>> {
Ok(self.status(name)?.map(|status| status.identity))
}
pub(crate) fn open_dir_file(&self, name: Leaf<'_>) -> io::Result<File> {
let fd = rustix::fs::openat(
&self.0,
name.as_os_str(),
OFlags::RDONLY | OFlags::DIRECTORY | OFlags::NOFOLLOW | OFlags::CLOEXEC,
Mode::empty(),
)?;
Ok(File::from(fd))
}
#[cfg(target_vendor = "apple")]
pub(crate) fn open_dir_for_attributes(&self, name: Leaf<'_>) -> io::Result<File> {
open_directory_at(&self.0, name.as_os_str())
}
#[cfg(all(unix, not(target_vendor = "apple")))]
pub(crate) fn open_dir_for_attributes(&self, name: Leaf<'_>) -> io::Result<File> {
self.open_dir_file(name)
}
#[cfg(any(
target_os = "linux",
target_os = "android",
target_vendor = "apple",
target_os = "freebsd",
target_os = "netbsd",
target_os = "illumos"
))]
pub(crate) fn open_dir_for_metadata(&self, name: Leaf<'_>) -> io::Result<DirHandle> {
let searched = open_directory_with(MODE_ACCESS, |flags| {
rustix::fs::openat(&self.0, name.as_os_str(), flags, Mode::empty())
});
match searched {
Ok(fd) => Ok(DirHandle::Search(File::from(fd))),
Err(e) if SEARCH_UNVERIFIED || e.kind() == io::ErrorKind::PermissionDenied => {
self.open_dir_file(name).map(DirHandle::Read)
}
Err(e) => Err(e),
}
}
#[cfg(all(
unix,
not(any(
target_os = "linux",
target_os = "android",
target_vendor = "apple",
target_os = "freebsd",
target_os = "netbsd",
target_os = "illumos"
))
))]
pub(crate) fn open_dir_for_metadata(&self, name: Leaf<'_>) -> io::Result<DirHandle> {
self.open_dir_file(name).map(DirHandle::Read)
}
#[cfg(unix)]
pub(crate) fn set_dir_mode(
&self,
name: Leaf<'_>,
expected: FileIdentity,
mode: &std::fs::Permissions,
) -> io::Result<()> {
let dir = self.open_dir_for_metadata(name)?;
ensure_unreplaced(dir.as_file(), expected)?;
match dir.chmod(mode) {
#[cfg(any(target_os = "linux", target_os = "android"))]
Err(e) if e.kind() == io::ErrorKind::NotFound => {
self.set_dir_mode_through_read_handle(name, expected, mode)
}
#[cfg(any(target_os = "netbsd", target_os = "illumos"))]
Err(_) if matches!(dir, DirHandle::Search(_)) => {
self.set_dir_mode_through_read_handle(name, expected, mode)
}
other => other,
}
}
#[cfg(target_os = "macos")]
pub(crate) fn delete_access(&self, name: Leaf<'_>) -> DeleteAccess {
const DELETE_OK: i32 = 1 << 12;
match rustix::fs::accessat(
&self.0,
name.as_os_str(),
Access::from_bits_retain(DELETE_OK),
AtFlags::EACCESS,
) {
Ok(()) => DeleteAccess::Granted,
Err(Errno::ACCESS | Errno::PERM) => DeleteAccess::Denied,
Err(_) => DeleteAccess::Unknown,
}
}
#[cfg(any(
target_os = "linux",
target_os = "android",
target_os = "netbsd",
target_os = "illumos"
))]
pub(crate) fn set_dir_mode_through_read_handle(
&self,
name: Leaf<'_>,
expected: FileIdentity,
mode: &std::fs::Permissions,
) -> io::Result<()> {
let dir = self.open_dir_file(name)?;
ensure_unreplaced(&dir, expected)?;
dir.set_permissions(mode.clone())
}
pub(crate) fn entry_path(&self, name: Leaf<'_>) -> Option<std::path::PathBuf> {
self.open_dir_file(name)
.ok()
.and_then(|dir| current_path(&dir))
}
pub(crate) fn make_private_dir(&self, name: Leaf<'_>) -> io::Result<()> {
Ok(rustix::fs::mkdirat(&self.0, name.as_os_str(), Mode::RWXU)?)
}
#[cfg(unix)]
pub(crate) fn remove_dir(&self, name: Leaf<'_>) -> io::Result<()> {
Ok(rustix::fs::unlinkat(
&self.0,
name.as_os_str(),
AtFlags::REMOVEDIR,
)?)
}
pub(crate) fn rename_dir_within(&self, from: Leaf<'_>, to: Leaf<'_>) -> io::Result<()> {
Ok(rustix::fs::renameat(
&self.0,
from.as_os_str(),
&self.0,
to.as_os_str(),
)?)
}
pub(crate) fn create_new(
&self,
name: Leaf<'_>,
mode: Option<SanitizedMode>,
) -> io::Result<File> {
let (file, pending) = self.create_new_deferred(name, mode)?;
pending.apply(&file)?;
Ok(file)
}
pub(crate) fn create_new_deferred(
&self,
name: Leaf<'_>,
mode: Option<SanitizedMode>,
) -> io::Result<(File, PendingMode)> {
let creation =
mode.map_or(Mode::from_bits_truncate(0o666), |_| Mode::RUSR | Mode::WUSR);
let fd = rustix::fs::openat(
&self.0,
name.as_os_str(),
OFlags::WRONLY | OFlags::CREATE | OFlags::EXCL | OFlags::NOFOLLOW | OFlags::CLOEXEC,
creation,
)?;
Ok((File::from(fd), PendingMode(mode)))
}
pub(crate) fn open_read(&self, name: Leaf<'_>) -> io::Result<File> {
let fd = rustix::fs::openat(
&self.0,
name.as_os_str(),
OFlags::RDONLY | OFlags::NOFOLLOW | OFlags::NONBLOCK | OFlags::CLOEXEC,
Mode::empty(),
)?;
Ok(File::from(fd))
}
pub(crate) fn replace_entry(
&self,
from: &TempLeaf,
to: Leaf<'_>,
_permit: ReplacePermit,
) -> io::Result<()> {
Ok(rustix::fs::renameat(
&self.0,
from.as_leaf().as_os_str(),
&self.0,
to.as_os_str(),
)?)
}
#[cfg(unix)]
pub(crate) fn remove_occupant(
&self,
name: Leaf<'_>,
_permit: ReplacePermit,
) -> io::Result<()> {
self.unlink_own(name)
}
pub(super) fn unlink_own(&self, name: Leaf<'_>) -> io::Result<()> {
Ok(rustix::fs::unlinkat(
&self.0,
name.as_os_str(),
AtFlags::empty(),
)?)
}
pub(crate) fn read_link(&self, name: Leaf<'_>) -> io::Result<std::path::PathBuf> {
#[cfg(unix)]
use std::os::unix::ffi::OsStringExt;
#[cfg(target_os = "wasi")]
use std::os::wasi::ffi::OsStringExt;
let target = rustix::fs::readlinkat(&self.0, name.as_os_str(), Vec::new())?;
Ok(std::path::PathBuf::from(std::ffi::OsString::from_vec(
target.into_bytes(),
)))
}
#[cfg(unix)]
pub(crate) fn symlink(&self, target: &Path, name: Leaf<'_>) -> io::Result<()> {
Ok(rustix::fs::symlinkat(target, &self.0, name.as_os_str())?)
}
}
#[cfg(unix)]
impl DirHandle {
pub(crate) fn chmod(&self, mode: &std::fs::Permissions) -> io::Result<()> {
match self {
Self::Search(dir) => chmod_handle(dir, mode),
Self::Read(dir) => dir.set_permissions(mode.clone()),
}
}
}
#[cfg(unix)]
fn ensure_unreplaced(dir: &File, expected: FileIdentity) -> io::Result<()> {
if FileIdentity::of_file(dir).check(expected) == IdentityCheck::Different {
return Err(io::Error::other("the directory was replaced"));
}
Ok(())
}
#[cfg(any(target_os = "linux", target_os = "android"))]
fn chmod_handle(dir: &File, mode: &std::fs::Permissions) -> io::Result<()> {
use std::os::fd::AsRawFd;
use std::os::unix::fs::PermissionsExt;
Ok(rustix::fs::chmod(
format!("/proc/self/fd/{}", dir.as_raw_fd()),
Mode::from_bits_truncate(mode.mode()),
)?)
}
#[cfg(all(unix, not(any(target_os = "linux", target_os = "android"))))]
fn chmod_handle(dir: &File, mode: &std::fs::Permissions) -> io::Result<()> {
dir.set_permissions(mode.clone())
}
#[cfg(target_os = "linux")]
fn current_path(fd: &File) -> Option<std::path::PathBuf> {
use std::os::fd::AsRawFd;
std::fs::read_link(format!("/proc/self/fd/{}", fd.as_raw_fd())).ok()
}
#[cfg(target_os = "macos")]
fn current_path(fd: &File) -> Option<std::path::PathBuf> {
use std::os::unix::ffi::OsStrExt;
rustix::fs::getpath(fd)
.ok()
.map(|c_path| std::path::PathBuf::from(std::ffi::OsStr::from_bytes(c_path.as_bytes())))
}
#[cfg(not(any(target_os = "linux", target_os = "macos")))]
fn current_path(_fd: &File) -> Option<std::path::PathBuf> {
None
}
}
#[cfg(windows)]
#[allow(unsafe_code)] pub(in crate::formats) mod nt;
#[cfg(windows)]
mod dacl_walk;
#[cfg(any(windows, test))]
mod dir_info;
#[cfg(all(test, windows))]
#[allow(unsafe_code)] pub(crate) mod dacl_probe;
#[cfg(windows)]
mod imp {
use std::ffi::OsString;
use std::fs::File;
use std::io;
use std::os::windows::ffi::OsStringExt;
use std::os::windows::fs::MetadataExt;
use std::os::windows::fs::OpenOptionsExt;
use std::os::windows::io::AsHandle;
use std::path::Path;
use std::path::PathBuf;
use windows_sys::Win32::Storage::FileSystem::FILE_ATTRIBUTE_NORMAL;
use windows_sys::Win32::Storage::FileSystem::FILE_ATTRIBUTE_REPARSE_POINT;
use windows_sys::Win32::Storage::FileSystem::FILE_FLAG_BACKUP_SEMANTICS;
use windows_sys::Win32::Storage::FileSystem::FILE_FLAG_OPEN_REPARSE_POINT;
use windows_sys::Win32::Storage::FileSystem::FILE_GENERIC_READ;
use windows_sys::Win32::Storage::FileSystem::FILE_GENERIC_WRITE;
use windows_sys::Win32::Storage::FileSystem::FILE_READ_ATTRIBUTES;
use windows_sys::Win32::Storage::FileSystem::FILE_SHARE_DELETE;
use windows_sys::Win32::Storage::FileSystem::FILE_SHARE_READ;
use windows_sys::Win32::Storage::FileSystem::FILE_SHARE_WRITE;
use windows_sys::Win32::Storage::FileSystem::FILE_TRAVERSE;
use windows_sys::Win32::Storage::FileSystem::READ_CONTROL;
use windows_sys::Win32::Storage::FileSystem::SYNCHRONIZE;
use windows_sys::Win32::Storage::FileSystem::WRITE_DAC;
use super::EntryStatus;
use super::Leaf;
use super::PendingMode;
use super::TempLeaf;
use super::dacl_walk;
use super::nt;
use super::nt::Disposition;
use super::nt::Kind;
use super::nt::Request;
use super::nt::security;
use super::nt::security::DaclState;
use super::nt::security::Derived;
use super::nt::security::Descriptor;
use super::nt::security::DirSecurity;
use super::nt::security::EffectiveToken;
use super::nt::security::ObjectKind;
use super::nt::security::Part;
use super::nt::security::SecurityDescriptor;
use crate::formats::file_identity::FileIdentity;
use crate::formats::file_identity::IdentityCheck;
use crate::formats::occupant::ReplacePermit;
use crate::formats::reparse;
use crate::formats::reparse::ReparseTarget;
use crate::security::fs_view::Occupant;
use crate::security::permissions::SanitizedMode;
const DIR_ACCESS: u32 = FILE_TRAVERSE | FILE_READ_ATTRIBUTES | SYNCHRONIZE;
const NO_INHERITABLE_ENTRIES: &str =
"the parent has no inheritable entries; the root stays owner-only";
#[derive(Debug)]
pub(crate) enum RootDacl {
Inherit(DirSecurity),
Copy(DirSecurity),
}
#[derive(Debug)]
pub(crate) enum DirRead {
Read(DirSecurity),
Replaced,
}
#[derive(Debug)]
pub(crate) struct DaclHandle(File);
impl DaclHandle {
pub(crate) fn rewrite(&self, root: &RootDacl) -> io::Result<()> {
let token = EffectiveToken::open()?;
let handle = self.0.as_handle();
let descriptor = match root {
RootDacl::Copy(source) => source.descriptor(),
RootDacl::Inherit(parent) => {
let parent = parent.descriptor();
if !parent.has_inheritable_entries()? {
return Err(io::Error::other(NO_INHERITABLE_ENTRIES));
}
let creator = SecurityDescriptor::read(handle, Part::OwnerAndGroup)?;
let derived = Derived::new(&parent, &creator, ObjectKind::Container, &token)?;
match derived.dacl_state()? {
DaclState::Present { entries } if entries > 0 => {}
DaclState::Present { .. } | DaclState::Null => {
return Err(io::Error::other(NO_INHERITABLE_ENTRIES));
}
}
Descriptor::Derived(derived)
}
};
dacl_walk::rewrite_children(handle, &descriptor, &token)?;
security::apply_dacl(handle, &descriptor)
}
}
#[derive(Debug)]
pub(crate) struct VettedDir(File);
impl VettedDir {
fn into_dir(file: File) -> io::Result<Self> {
if file.metadata()?.is_dir() {
Ok(Self(file))
} else {
Err(io::Error::from(io::ErrorKind::NotADirectory))
}
}
#[expect(
clippy::disallowed_methods,
reason = "opens the destination root itself; no directory handle exists yet"
)]
pub(crate) fn open_path(path: &Path) -> io::Result<Self> {
let file = std::fs::OpenOptions::new()
.access_mode(DIR_ACCESS)
.share_mode(FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE)
.custom_flags(FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OPEN_REPARSE_POINT)
.open(path)?;
Self::into_dir(file)
}
pub(crate) fn reopen(&self) -> io::Result<Self> {
self.0.try_clone().map(Self)
}
pub(crate) fn open_dir(&self, name: Leaf<'_>) -> io::Result<Self> {
let request = Request {
kind: Kind::Directory,
..Request::open(DIR_ACCESS)
};
let handle = nt::create_at(self.0.as_handle(), name, request)?;
Self::into_dir(File::from(handle))
}
pub(crate) fn read_link(&self, name: Leaf<'_>) -> io::Result<PathBuf> {
let buffer = nt::reparse_data_at(self.0.as_handle(), name)?;
let (ReparseTarget::Relative(units) | ReparseTarget::Absolute(units)) =
reparse::parse(&buffer)?;
Ok(PathBuf::from(OsString::from_wide(&units)))
}
fn occupied_as_exists(&self, name: Leaf<'_>, err: io::Error) -> io::Error {
if err.kind() == io::ErrorKind::PermissionDenied
&& matches!(self.probe(name), Ok(Some(_)))
{
io::Error::from(io::ErrorKind::AlreadyExists)
} else {
err
}
}
pub(crate) fn make_dir(&self, name: Leaf<'_>) -> io::Result<()> {
let request = Request {
access: SYNCHRONIZE | FILE_READ_ATTRIBUTES,
disposition: Disposition::Create,
kind: Kind::Directory,
attributes: 0,
};
nt::create_at(self.0.as_handle(), name, request)
.map(drop)
.map_err(|err| self.occupied_as_exists(name, err))
}
pub(crate) fn status(&self, name: Leaf<'_>) -> io::Result<Option<EntryStatus>> {
let request = Request::open(FILE_READ_ATTRIBUTES);
let handle = match nt::create_at(self.0.as_handle(), name, request) {
Ok(handle) => handle,
Err(e) if e.kind() == io::ErrorKind::NotFound => return Ok(None),
Err(e) => return Err(e),
};
let file = File::from(handle);
let meta = file.metadata()?;
Ok(Some(EntryStatus {
occupant: meta.file_type().into(),
reparse: meta.file_attributes() & FILE_ATTRIBUTE_REPARSE_POINT != 0,
identity: FileIdentity::of_file(&file),
}))
}
pub(crate) fn probe(&self, name: Leaf<'_>) -> io::Result<Option<Occupant>> {
Ok(self.status(name)?.map(|status| status.occupant))
}
pub(crate) fn identity(&self, name: Leaf<'_>) -> io::Result<Option<FileIdentity>> {
Ok(self.status(name)?.map(|status| status.identity))
}
#[allow(
clippy::unused_self,
reason = "keeps the signature of the platforms that name the entry by path"
)]
pub(crate) fn entry_path(&self, _name: Leaf<'_>) -> Option<std::path::PathBuf> {
None
}
pub(crate) fn make_private_dir(&self, name: Leaf<'_>) -> io::Result<()> {
nt::create_private_dir_at(self.0.as_handle(), name)
.map_err(|err| self.occupied_as_exists(name, err))
}
pub(crate) fn open_dir_for_dacl(
&self,
name: Leaf<'_>,
expected: FileIdentity,
) -> io::Result<DaclHandle> {
let request = Request {
kind: Kind::Directory,
..Request::open(READ_CONTROL | WRITE_DAC | FILE_READ_ATTRIBUTES)
};
let file = File::from(nt::create_at(self.0.as_handle(), name, request)?);
if FileIdentity::of_file(&file).check(expected) == IdentityCheck::Different {
return Err(io::Error::from(io::ErrorKind::NotFound));
}
Ok(DaclHandle(file))
}
pub(crate) fn read_security(&self) -> io::Result<DirSecurity> {
let reopened = nt::reopen_dir(self.0.as_handle(), READ_CONTROL | FILE_READ_ATTRIBUTES)?;
SecurityDescriptor::read(reopened.as_handle(), Part::OwnerGroupAndDacl)
.map(DirSecurity::new)
}
pub(crate) fn read_dir_security(
&self,
name: Leaf<'_>,
expected: FileIdentity,
) -> io::Result<DirRead> {
let request = Request {
kind: Kind::Directory,
..Request::open(READ_CONTROL | FILE_READ_ATTRIBUTES)
};
let file = File::from(nt::create_at(self.0.as_handle(), name, request)?);
if FileIdentity::of_file(&file).check(expected) == IdentityCheck::Different {
return Ok(DirRead::Replaced);
}
SecurityDescriptor::read(file.as_handle(), Part::OwnerGroupAndDacl)
.map(|descriptor| DirRead::Read(DirSecurity::new(descriptor)))
}
pub(crate) fn rename_dir_within(&self, from: Leaf<'_>, to: Leaf<'_>) -> io::Result<()> {
nt::rename_with(self.0.as_handle(), from, to, nt::Replace::Never)
}
pub(crate) fn create_new(
&self,
name: Leaf<'_>,
mode: Option<SanitizedMode>,
) -> io::Result<File> {
self.create_new_deferred(name, mode).map(|(file, _)| file)
}
pub(crate) fn create_new_deferred(
&self,
name: Leaf<'_>,
mode: Option<SanitizedMode>,
) -> io::Result<(File, PendingMode)> {
let request = Request {
access: FILE_GENERIC_WRITE,
disposition: Disposition::Create,
kind: Kind::NonDirectory,
attributes: FILE_ATTRIBUTE_NORMAL,
};
nt::create_at(self.0.as_handle(), name, request)
.map(|handle| (File::from(handle), PendingMode(mode)))
.map_err(|err| self.occupied_as_exists(name, err))
}
pub(crate) fn open_read(&self, name: Leaf<'_>) -> io::Result<File> {
nt::create_at(self.0.as_handle(), name, Request::open(FILE_GENERIC_READ))
.map(File::from)
}
pub(crate) fn replace_entry(
&self,
from: &TempLeaf,
to: Leaf<'_>,
_permit: ReplacePermit,
) -> io::Result<()> {
nt::rename_within(self.0.as_handle(), from.as_leaf(), to)
}
#[cfg(test)]
pub(crate) fn remove_occupant(
&self,
name: Leaf<'_>,
_permit: ReplacePermit,
) -> io::Result<()> {
self.unlink_own(name)
}
pub(super) fn unlink_own(&self, name: Leaf<'_>) -> io::Result<()> {
nt::remove_file(self.0.as_handle(), name)
}
}
}
#[cfg(not(any(unix, target_os = "wasi", windows)))]
compile_error!("exarch-core has no directory-handle backend for this target");
#[derive(Debug)]
#[must_use = "the file stays owner-only until the mode is applied"]
pub(crate) struct PendingMode(Option<crate::security::permissions::SanitizedMode>);
impl PendingMode {
pub(crate) const fn applied() -> Self {
Self(None)
}
#[cfg(unix)]
pub(crate) fn apply(self, file: &std::fs::File) -> std::io::Result<()> {
use std::os::unix::fs::PermissionsExt;
self.0.map_or(Ok(()), |mode| {
file.set_permissions(std::fs::Permissions::from_mode(mode.as_u32()))
})
}
#[cfg(not(unix))]
#[expect(clippy::unnecessary_wraps, reason = "same signature as the Unix apply")]
pub(crate) fn apply(self, _file: &std::fs::File) -> std::io::Result<()> {
let Self(_mode) = self;
Ok(())
}
}
pub(crate) struct TempFileGuard<'d> {
dir: &'d VettedDir,
name: OsString,
should_cleanup: bool,
}
impl<'d> TempFileGuard<'d> {
pub(crate) fn new(dir: &'d VettedDir, name: Leaf<'_>) -> Self {
Self {
dir,
name: name.as_os_str().to_os_string(),
should_cleanup: true,
}
}
pub(crate) fn for_temp(dir: &'d VettedDir, name: TempLeaf) -> Self {
Self {
dir,
name: name.0,
should_cleanup: true,
}
}
pub(crate) fn persist(mut self) {
self.should_cleanup = false;
}
}
impl Drop for TempFileGuard<'_> {
fn drop(&mut self) {
if self.should_cleanup {
let _ = self.dir.unlink_own(Leaf(&self.name));
}
}
}
#[cfg(test)]
#[allow(clippy::unwrap_used)]
pub(crate) mod support {
use std::ffi::OsStr;
use std::path::Path;
use super::Leaf;
use super::PinnedRoot;
use super::VettedDir;
pub(crate) fn leaf(name: &str) -> Leaf<'_> {
Leaf::of_name(OsStr::new(name)).unwrap()
}
pub(crate) fn pinned_at(path: &Path) -> VettedDir {
PinnedRoot::open_for_test(path)
.unwrap()
.dir()
.reopen()
.unwrap()
}
pub(super) fn pinned(temp: &tempfile::TempDir) -> VettedDir {
pinned_at(temp.path())
}
#[cfg(windows)]
pub(crate) fn plain(path: &Path) -> std::path::PathBuf {
path.to_str()
.and_then(|text| text.strip_prefix(r"\\?\"))
.map_or_else(|| path.to_path_buf(), std::path::PathBuf::from)
}
#[cfg(windows)]
pub(crate) fn junction(link: &Path, target: &Path) -> bool {
std::process::Command::new("cmd")
.args(["/C", "mklink", "/J"])
.arg(link)
.arg(plain(target))
.output()
.is_ok_and(|out| out.status.success())
}
}
#[cfg(all(test, unix))]
#[allow(clippy::unwrap_used)]
mod tests {
use super::support::leaf;
use super::support::pinned;
use super::support::pinned_at;
use super::*;
use crate::formats::file_identity::IdentityCheck;
use crate::formats::occupant::ReplacePermit;
use crate::security::RawMode;
use std::assert_matches;
use std::fs;
use std::io;
use std::io::Write;
use std::os::unix::fs::MetadataExt;
use std::os::unix::fs::PermissionsExt;
use std::os::unix::fs::symlink;
use tempfile::TempDir;
#[test]
fn both_file_type_mappings_agree_on_the_occupant() {
use rustix::fs::FileType;
assert_eq!(Occupant::from(FileType::Directory), Occupant::Directory);
assert_eq!(Occupant::from(FileType::Symlink), Occupant::Symlink);
assert_eq!(Occupant::from(FileType::RegularFile), Occupant::File);
assert_eq!(Occupant::from(FileType::Fifo), Occupant::File);
let temp = TempDir::new().unwrap();
let std_type = |name: &str| {
fs::symlink_metadata(temp.path().join(name))
.unwrap()
.file_type()
};
fs::create_dir(temp.path().join("d")).unwrap();
fs::write(temp.path().join("f"), b"x").unwrap();
symlink("f", temp.path().join("l")).unwrap();
assert_eq!(Occupant::from(std_type("d")), Occupant::Directory);
assert_eq!(Occupant::from(std_type("l")), Occupant::Symlink);
assert_eq!(Occupant::from(std_type("f")), Occupant::File);
}
fn swapped(temp: &TempDir) -> (VettedDir, std::path::PathBuf) {
let base = temp.path().join("base");
let outside = temp.path().join("outside");
fs::create_dir_all(base.join("a")).unwrap();
fs::create_dir(&outside).unwrap();
let root = pinned_at(&base);
let a = root.open_dir(leaf("a")).unwrap();
fs::rename(base.join("a"), base.join("a_moved")).unwrap();
symlink(&outside, base.join("a")).unwrap();
(a, outside)
}
#[test]
fn create_new_after_a_swap_lands_in_the_moved_directory() {
let temp = TempDir::new().unwrap();
let (a, outside) = swapped(&temp);
let mut file = a.create_new(leaf("f"), None).unwrap();
file.write_all(b"x").unwrap();
assert!(temp.path().join("base/a_moved/f").is_file());
assert_eq!(fs::read_dir(&outside).unwrap().count(), 0);
}
#[test]
fn guard_after_a_swap_removes_from_the_moved_directory() {
let temp = TempDir::new().unwrap();
let (a, outside) = swapped(&temp);
fs::write(outside.join("f"), b"keep").unwrap();
a.create_new(leaf("f"), None).unwrap();
drop(TempFileGuard::new(&a, leaf("f")));
assert!(!temp.path().join("base/a_moved/f").exists());
assert_eq!(fs::read(outside.join("f")).unwrap(), b"keep");
}
#[test]
fn symlink_and_rename_after_a_swap_stay_in_the_moved_directory() {
let temp = TempDir::new().unwrap();
let (a, outside) = swapped(&temp);
a.create_new(leaf("tmp"), None).unwrap();
a.replace_entry(
&TempLeaf::named("tmp"),
leaf("final"),
ReplacePermit::for_test(),
)
.unwrap();
a.symlink(Path::new("final"), leaf("alias")).unwrap();
assert!(temp.path().join("base/a_moved/final").is_file());
assert!(
fs::symlink_metadata(temp.path().join("base/a_moved/alias"))
.unwrap()
.is_symlink()
);
assert_eq!(a.read_link(leaf("alias")).unwrap(), Path::new("final"));
assert_eq!(fs::read_dir(&outside).unwrap().count(), 0);
}
#[test]
fn open_dir_refuses_a_symlink_and_probe_names_it() {
let temp = TempDir::new().unwrap();
fs::create_dir(temp.path().join("real")).unwrap();
symlink("real", temp.path().join("alias")).unwrap();
let dir = pinned(&temp);
assert!(dir.open_dir(leaf("alias")).is_err());
assert_eq!(dir.probe(leaf("alias")).unwrap(), Some(Occupant::Symlink));
assert_eq!(dir.probe(leaf("real")).unwrap(), Some(Occupant::Directory));
assert_eq!(dir.probe(leaf("absent")).unwrap(), None);
}
#[test]
fn create_new_applies_the_exact_mode_and_refuses_an_existing_name() {
let temp = TempDir::new().unwrap();
let dir = pinned(&temp);
let config = crate::SecurityConfig::default();
let mode = crate::security::permissions::sanitize_permissions(RawMode::new(0o640), &config);
dir.create_new(leaf("f"), Some(mode)).unwrap();
let actual = fs::metadata(temp.path().join("f"))
.unwrap()
.permissions()
.mode();
assert_eq!(actual & 0o7777, 0o640);
let err = dir.create_new(leaf("f"), None).unwrap_err();
assert_eq!(err.kind(), io::ErrorKind::AlreadyExists);
}
#[test]
fn leaf_split_accepts_only_a_normal_final_component() {
let (parent, name) = Leaf::split(Path::new("a/b/c.txt")).unwrap();
assert_eq!(parent, Path::new("a/b"));
assert_eq!(name.as_os_str(), "c.txt");
let (parent, name) = Leaf::split(Path::new("top")).unwrap();
assert_eq!(parent, Path::new(""));
assert_eq!(name.as_os_str(), "top");
for bad in ["", "/", "..", "a/..", "../x/.."] {
assert_matches!(
Leaf::split(Path::new(bad)),
Err(ArchiveError::PathTraversal { .. }),
"{bad:?}"
);
}
}
#[test]
fn works_in_a_directory_without_read_permission() {
let temp = TempDir::new().unwrap();
let locked = temp.path().join("locked");
fs::create_dir(&locked).unwrap();
fs::create_dir(locked.join("sub")).unwrap();
fs::set_permissions(&locked, fs::Permissions::from_mode(0o311)).unwrap();
if fs::read_dir(&locked).is_ok() {
return;
}
let result = (|| -> io::Result<()> {
let dir = VettedDir::open_path(&locked)?;
dir.make_dir(leaf("made"))?;
dir.open_dir(leaf("sub"))?;
dir.create_new(leaf("f"), None)?;
assert_eq!(dir.probe(leaf("f"))?, Some(Occupant::File));
Ok(())
})();
fs::set_permissions(&locked, fs::Permissions::from_mode(0o755)).unwrap();
result.unwrap();
assert!(locked.join("made").is_dir());
assert!(locked.join("f").is_file());
}
#[test]
fn destination_without_read_permission_is_usable() {
for mode in [0o733, 0o333] {
let temp = TempDir::new().unwrap();
let dest = temp.path().join("dest");
fs::create_dir(&dest).unwrap();
fs::set_permissions(&dest, fs::Permissions::from_mode(mode)).unwrap();
let result = PinnedRoot::open_for_test(&dest).and_then(|root| {
let dir = root.dir().reopen()?;
dir.make_dir(leaf("d"))?;
dir.create_new(leaf("f"), None)?;
Ok(())
});
fs::set_permissions(&dest, fs::Permissions::from_mode(0o755)).unwrap();
result.unwrap();
assert!(dest.join("d").is_dir() && dest.join("f").is_file());
}
}
fn identity_of_name(dir: &VettedDir, name: &str) -> FileIdentity {
dir.status(leaf(name)).unwrap().unwrap().identity
}
#[cfg(any(
target_os = "linux",
target_os = "android",
target_vendor = "apple",
target_os = "freebsd",
target_os = "netbsd",
target_os = "illumos"
))]
#[test]
fn a_directory_without_read_access_is_opened_search_only() {
let temp = TempDir::new().unwrap();
let locked = temp.path().join("locked");
fs::create_dir(&locked).unwrap();
fs::set_permissions(&locked, fs::Permissions::from_mode(0o311)).unwrap();
let dir = pinned(&temp);
let handle = dir.open_dir_for_metadata(leaf("locked"));
fs::set_permissions(&locked, fs::Permissions::from_mode(0o755)).unwrap();
if cfg!(target_os = "netbsd") && fs::metadata(temp.path()).unwrap().uid() != 0 {
assert_matches!(handle, Err(ref e) if e.kind() == io::ErrorKind::PermissionDenied);
} else {
assert_matches!(handle, Ok(DirHandle::Search(_)));
}
}
#[cfg(any(
target_os = "linux",
target_os = "android",
target_vendor = "apple",
target_os = "freebsd",
target_os = "netbsd",
target_os = "illumos"
))]
#[test]
fn set_dir_mode_reaches_a_directory_whatever_access_its_mode_grants() {
for mode in [0o311, 0o600, 0o644, 0o400, 0o000] {
let temp = TempDir::new().unwrap();
let locked = temp.path().join("locked");
fs::create_dir(&locked).unwrap();
fs::set_permissions(&locked, fs::Permissions::from_mode(mode)).unwrap();
let dir = pinned(&temp);
let id = identity_of_name(&dir, "locked");
let result = dir.set_dir_mode(leaf("locked"), id, &fs::Permissions::from_mode(0o700));
let no_handle = (mode == 0o000
&& cfg!(any(
target_vendor = "apple",
target_os = "freebsd",
target_os = "netbsd",
target_os = "illumos"
)))
|| (mode == 0o311 && cfg!(target_os = "netbsd"));
if no_handle && fs::metadata(temp.path()).unwrap().uid() != 0 {
assert!(result.is_err(), "{mode:o}");
fs::set_permissions(&locked, fs::Permissions::from_mode(0o700)).unwrap();
continue;
}
result.unwrap_or_else(|e| panic!("{mode:o}: {e}"));
assert_eq!(
fs::metadata(&locked).unwrap().permissions().mode() & 0o7777,
0o700,
"{mode:o}"
);
}
}
#[cfg(any(target_os = "linux", target_os = "android"))]
#[test]
fn set_dir_mode_without_proc_falls_back_to_a_read_handle() {
let temp = TempDir::new().unwrap();
let readable = temp.path().join("readable");
fs::create_dir(&readable).unwrap();
fs::set_permissions(&readable, fs::Permissions::from_mode(0o555)).unwrap();
let dir = pinned(&temp);
let id = identity_of_name(&dir, "readable");
dir.set_dir_mode_through_read_handle(
leaf("readable"),
id,
&fs::Permissions::from_mode(0o700),
)
.unwrap();
assert_eq!(
fs::metadata(&readable).unwrap().permissions().mode() & 0o7777,
0o700
);
}
#[test]
fn set_dir_mode_leaves_a_replaced_directory_unchanged() {
let temp = TempDir::new().unwrap();
fs::create_dir(temp.path().join("d")).unwrap();
let dir = pinned(&temp);
let original = identity_of_name(&dir, "d");
fs::rename(temp.path().join("d"), temp.path().join("moved")).unwrap();
fs::create_dir(temp.path().join("d")).unwrap();
fs::set_permissions(temp.path().join("d"), fs::Permissions::from_mode(0o755)).unwrap();
let result = dir.set_dir_mode(leaf("d"), original, &fs::Permissions::from_mode(0o700));
assert!(result.is_err());
assert_eq!(
fs::metadata(temp.path().join("d"))
.unwrap()
.permissions()
.mode()
& 0o7777,
0o755
);
}
#[test]
fn own_status_describes_the_handle_whatever_its_name_became() {
let temp = TempDir::new().unwrap();
fs::create_dir(temp.path().join("d")).unwrap();
let dir = pinned(&temp);
let held = dir.open_dir(leaf("d")).unwrap();
let by_name = dir.status(leaf("d")).unwrap().unwrap();
fs::rename(temp.path().join("d"), temp.path().join("moved")).unwrap();
let own = held.own_status().unwrap();
assert_eq!(own.occupant, Occupant::Directory);
assert_eq!(own.identity.check(by_name.identity), IdentityCheck::Same);
assert_eq!(own.mode.bits(), by_name.mode.bits());
}
}
#[cfg(all(test, windows))]
#[allow(clippy::unwrap_used)]
mod windows_tests {
use super::support::junction;
use super::support::leaf;
use super::support::pinned;
use super::support::pinned_at;
use super::*;
use crate::formats::file_identity::IdentityCheck;
use crate::formats::occupant::ReplacePermit;
use std::fs;
use std::io;
use std::io::Write;
use std::path::PathBuf;
use std::process::Command;
use tempfile::TempDir;
fn run(program: &str, args: &[&std::ffi::OsStr]) -> bool {
Command::new(program)
.args(args)
.output()
.is_ok_and(|out| out.status.success())
}
fn entries(dir: &Path) -> usize {
fs::read_dir(dir).unwrap().count()
}
#[test]
fn a_handle_relative_status_carries_the_file_identity_of_the_path() {
let temp = TempDir::new().unwrap();
let dir = pinned(&temp);
dir.make_dir(leaf("sub")).unwrap();
fs::write(temp.path().join("other"), b"x").unwrap();
let sub = dir.status(leaf("sub")).unwrap().unwrap();
let other = dir.status(leaf("other")).unwrap().unwrap();
let by_path = crate::formats::file_identity::identity_of(&temp.path().join("sub")).unwrap();
if matches!(
sub.identity,
crate::formats::file_identity::FileIdentity::Unknown
) {
eprintln!("skipping: the volume reports no file id");
return;
}
assert_eq!(sub.identity.check(by_path), IdentityCheck::Same);
assert_eq!(sub.identity.check(other.identity), IdentityCheck::Different);
assert!(!sub.reparse);
}
#[test]
fn a_private_dir_grants_only_the_current_user_and_ignores_the_parent() {
use super::dacl_probe;
let temp = TempDir::new().unwrap();
let dir = pinned(&temp);
dir.make_private_dir(leaf("stage")).unwrap();
let dacl = dacl_probe::read(&temp.path().join("stage"));
assert!(dacl.protected, "the DACL must not inherit from the parent");
let [ace] = dacl.aces.as_slice() else {
panic!("expected exactly one entry, got {:?}", dacl.aces);
};
assert!(ace.allows);
assert!(!ace.inherited());
assert_eq!(ace.flags, dacl_probe::OICI);
assert_eq!(ace.mask, dacl_probe::FILE_ALL_ACCESS);
assert_eq!(ace.sid, dacl_probe::current_user());
}
#[test]
fn what_is_created_in_a_private_dir_is_owner_only_too() {
use super::dacl_probe;
let temp = TempDir::new().unwrap();
let dir = pinned(&temp);
dir.make_private_dir(leaf("stage")).unwrap();
let file = temp.path().join("stage").join("child.txt");
fs::write(&file, b"x").unwrap();
fs::create_dir(temp.path().join("stage").join("sub")).unwrap();
for path in [file, temp.path().join("stage").join("sub")] {
let dacl = dacl_probe::read(&path);
assert!(!dacl.protected);
let [ace] = dacl.aces.as_slice() else {
panic!("expected exactly one entry, got {:?}", dacl.aces);
};
assert!(ace.allows && ace.inherited());
assert_eq!(ace.mask, dacl_probe::FILE_ALL_ACCESS);
assert_eq!(ace.sid, dacl_probe::current_user());
}
}
#[test]
fn a_private_dir_refuses_a_taken_name() {
let temp = TempDir::new().unwrap();
let dir = pinned(&temp);
dir.make_private_dir(leaf("stage")).unwrap();
let err = dir.make_private_dir(leaf("stage")).unwrap_err();
assert_eq!(err.kind(), io::ErrorKind::AlreadyExists);
}
#[test]
fn rewriting_with_the_parent_dacl_inherits_it_down_the_tree() {
use super::dacl_probe;
let temp = TempDir::new().unwrap();
let dir = pinned(&temp);
dir.make_private_dir(leaf("stage")).unwrap();
let stage = temp.path().join("stage");
fs::write(stage.join("child.txt"), b"x").unwrap();
let id = dir.identity(leaf("stage")).unwrap().unwrap();
dir.open_dir_for_dacl(leaf("stage"), id)
.unwrap()
.rewrite(&RootDacl::Inherit(dir.read_security().unwrap()))
.unwrap();
let parent = dacl_probe::read(temp.path());
for path in [stage.clone(), stage.join("child.txt")] {
let dacl = dacl_probe::read(&path);
assert!(!dacl.protected, "{path:?} still ignores the parent");
assert!(
!dacl.aces.is_empty(),
"{path:?} was left with an empty DACL"
);
assert!(dacl.aces.iter().all(dacl_probe::Ace::inherited));
}
let inherited_sids = |aces: &[dacl_probe::Ace]| {
aces.iter()
.filter(|ace| ace.allows)
.map(|ace| ace.sid.clone())
.collect::<std::collections::BTreeSet<_>>()
};
let reset = dacl_probe::read(&stage);
assert!(inherited_sids(&reset.aces).is_subset(&inherited_sids(&parent.aces)));
}
#[test]
fn rewriting_under_a_parent_without_inheritable_entries_keeps_the_dir_owner_only() {
use super::dacl_probe;
let temp = TempDir::new().unwrap();
let parent = temp.path().join("closed");
fs::create_dir(&parent).unwrap();
dacl_probe::restrict_to_current_user_without_inheritance(&parent);
let dir = pinned_at(&parent);
dir.make_private_dir(leaf("stage")).unwrap();
let stage = parent.join("stage");
fs::write(stage.join("child.txt"), b"x").unwrap();
let id = dir.identity(leaf("stage")).unwrap().unwrap();
let result = dir
.open_dir_for_dacl(leaf("stage"), id)
.unwrap()
.rewrite(&RootDacl::Inherit(dir.read_security().unwrap()));
let dacl = dacl_probe::read(&stage);
assert!(
result.is_err(),
"the reset succeeded; stage is protected: {}, entries: {:?}",
dacl.protected,
dacl.aces
);
assert!(dacl.protected, "entries: {:?}", dacl.aces);
let [ace] = dacl.aces.as_slice() else {
panic!("expected exactly one entry, got {:?}", dacl.aces);
};
assert_eq!(ace.sid, dacl_probe::current_user());
assert!(fs::read(stage.join("child.txt")).is_ok());
}
#[test]
fn a_dacl_handle_is_refused_for_another_directory() {
let temp = TempDir::new().unwrap();
let dir = pinned(&temp);
dir.make_private_dir(leaf("stage")).unwrap();
dir.make_dir(leaf("other")).unwrap();
let other = dir.identity(leaf("other")).unwrap().unwrap();
if matches!(other, crate::formats::file_identity::FileIdentity::Unknown) {
eprintln!("skipping: the volume reports no file id");
return;
}
let err = dir.open_dir_for_dacl(leaf("stage"), other).unwrap_err();
assert_eq!(err.kind(), io::ErrorKind::NotFound);
}
#[test]
fn a_junction_is_reported_as_a_reparse_point() {
let temp = TempDir::new().unwrap();
let dir = pinned(&temp);
fs::create_dir(temp.path().join("target")).unwrap();
if !junction(&temp.path().join("link"), &temp.path().join("target")) {
eprintln!("skipping: cannot create a junction");
return;
}
let status = dir.status(leaf("link")).unwrap().unwrap();
assert!(status.reparse);
}
#[test]
fn a_junction_reads_back_as_its_verbatim_target() {
let temp = TempDir::new().unwrap();
let dir = pinned(&temp);
let target = temp.path().join("target");
fs::create_dir(&target).unwrap();
let target = target.canonicalize().unwrap();
if !junction(&temp.path().join("link"), &target) {
eprintln!("skipping: cannot create a junction");
return;
}
let read = dir.read_link(leaf("link")).unwrap();
assert_eq!(read, target);
}
#[test]
fn reading_a_link_that_is_not_one_fails() {
let temp = TempDir::new().unwrap();
let dir = pinned(&temp);
dir.make_dir(leaf("plain")).unwrap();
fs::write(temp.path().join("file"), b"x").unwrap();
assert!(dir.read_link(leaf("plain")).is_err());
assert!(dir.read_link(leaf("file")).is_err());
assert!(dir.read_link(leaf("missing")).is_err());
}
#[test]
fn renaming_a_directory_onto_an_existing_name_is_refused() {
let temp = TempDir::new().unwrap();
let dir = pinned(&temp);
dir.make_dir(leaf("from")).unwrap();
dir.make_dir(leaf("taken")).unwrap();
assert!(dir.rename_dir_within(leaf("from"), leaf("taken")).is_err());
assert_eq!(entries(temp.path()), 2);
dir.rename_dir_within(leaf("from"), leaf("free")).unwrap();
assert_eq!(dir.probe(leaf("free")).unwrap(), Some(Occupant::Directory));
}
#[test]
fn basic_operations_stay_relative_to_the_handle() {
let temp = TempDir::new().unwrap();
let dir = pinned(&temp);
dir.make_dir(leaf("sub")).unwrap();
assert_eq!(dir.probe(leaf("sub")).unwrap(), Some(Occupant::Directory));
assert_eq!(dir.probe(leaf("absent")).unwrap(), None);
let mut file = dir.create_new(leaf("a"), None).unwrap();
file.write_all(b"one").unwrap();
drop(file);
let err = dir.create_new(leaf("a"), None).unwrap_err();
assert_eq!(err.kind(), io::ErrorKind::AlreadyExists);
assert_eq!(dir.probe(leaf("a")).unwrap(), Some(Occupant::File));
assert!(
dir.open_read(leaf("a"))
.unwrap()
.metadata()
.unwrap()
.is_file()
);
fs::write(temp.path().join("b"), b"old").unwrap();
dir.replace_entry(&TempLeaf::named("a"), leaf("b"), ReplacePermit::for_test())
.unwrap();
assert_eq!(fs::read(temp.path().join("b")).unwrap(), b"one");
assert!(!temp.path().join("a").exists());
assert!(
dir.remove_occupant(leaf("sub"), ReplacePermit::for_test())
.is_err()
);
assert!(temp.path().join("sub").is_dir());
dir.remove_occupant(leaf("b"), ReplacePermit::for_test())
.unwrap();
assert_eq!(dir.probe(leaf("b")).unwrap(), None);
dir.reopen().unwrap().create_new(leaf("c"), None).unwrap();
assert!(temp.path().join("c").is_file());
assert!(dir.open_dir(leaf("c")).is_err());
assert!(dir.open_dir(leaf("sub")).is_ok());
}
#[test]
fn an_occupied_name_of_any_kind_is_already_exists() {
let temp = TempDir::new().unwrap();
fs::create_dir(temp.path().join("d")).unwrap();
fs::write(temp.path().join("f"), b"x").unwrap();
let dir = pinned(&temp);
let err = dir.create_new(leaf("d"), None).unwrap_err();
assert_eq!(err.kind(), io::ErrorKind::AlreadyExists);
let err = dir.create_new(leaf("f"), None).unwrap_err();
assert_eq!(err.kind(), io::ErrorKind::AlreadyExists);
let err = dir.make_dir(leaf("d")).unwrap_err();
assert_eq!(err.kind(), io::ErrorKind::AlreadyExists);
}
#[test]
fn a_preexisting_junction_is_never_followed() {
let temp = TempDir::new().unwrap();
let outside = temp.path().join("outside");
fs::create_dir(&outside).unwrap();
fs::write(outside.join("keep"), b"k").unwrap();
let link = temp.path().join("j");
assert!(junction(&link, &outside), "mklink /J failed");
let dir = pinned(&temp);
let err = dir.open_dir(leaf("j")).unwrap_err();
assert_eq!(err.kind(), io::ErrorKind::NotADirectory);
assert_eq!(dir.probe(leaf("j")).unwrap(), Some(Occupant::Symlink));
let err = dir.create_new(leaf("j"), None).unwrap_err();
assert_eq!(err.kind(), io::ErrorKind::AlreadyExists);
assert!(
!dir.open_read(leaf("j"))
.unwrap()
.metadata()
.unwrap()
.is_file()
);
dir.remove_occupant(leaf("j"), ReplacePermit::for_test())
.unwrap();
assert!(fs::symlink_metadata(&link).is_err());
assert_eq!(fs::read(outside.join("keep")).unwrap(), b"k");
}
#[test]
fn a_grandparent_of_held_handles_cannot_be_renamed_away() {
let temp = TempDir::new().unwrap();
let base = temp.path().join("base");
fs::create_dir_all(base.join("a/b/c")).unwrap();
let root = pinned_at(&base);
let b = {
let a = root.open_dir(leaf("a")).unwrap();
a.open_dir(leaf("b")).unwrap()
};
let c = b.open_dir(leaf("c")).unwrap();
let renamed = fs::rename(base.join("a"), base.join("a_moved"));
assert!(
renamed.is_err(),
"NTFS is expected to refuse renaming an ancestor of an open handle"
);
c.create_new(leaf("tmp"), None).unwrap();
c.replace_entry(
&TempLeaf::named("tmp"),
leaf("final"),
ReplacePermit::for_test(),
)
.unwrap();
assert!(base.join("a/b/c/final").is_file());
}
#[test]
fn a_deleted_and_replaced_directory_receives_nothing() {
let temp = TempDir::new().unwrap();
let base = temp.path().join("base");
let outside = temp.path().join("outside");
fs::create_dir_all(base.join("c")).unwrap();
fs::create_dir(&outside).unwrap();
let c = pinned_at(&base).open_dir(leaf("c")).unwrap();
fs::remove_dir(base.join("c")).unwrap();
assert!(junction(&base.join("c"), &outside), "mklink /J failed");
assert!(c.create_new(leaf("f"), None).is_err());
assert!(c.make_dir(leaf("d")).is_err());
assert_eq!(entries(&outside), 0);
}
struct MountGuard {
mount_dir: PathBuf,
escaped: Vec<PathBuf>,
}
impl Drop for MountGuard {
fn drop(&mut self) {
run("mountvol", &[self.mount_dir.as_ref(), "/D".as_ref()]);
for path in &self.escaped {
let _ = fs::remove_file(path);
let _ = fs::remove_dir(path);
}
}
}
fn volume_guid(drive_root: &Path) -> Option<String> {
let out = Command::new("mountvol")
.arg(drive_root)
.arg("/L")
.output()
.ok()?;
out.status
.success()
.then(|| String::from_utf8_lossy_owned(out.stdout).trim().to_owned())
}
#[test]
fn a_directory_converted_in_place_into_a_mount_point_receives_nothing() {
let temp = TempDir::new().unwrap();
let drive_root = temp
.path()
.ancestors()
.last()
.map(Path::to_path_buf)
.unwrap();
let c_path = temp.path().join("c");
fs::create_dir(&c_path).unwrap();
let held = pinned(&temp).open_dir(leaf("c")).unwrap();
let mount_dir = c_path.join("");
let mounted = volume_guid(&drive_root)
.is_some_and(|guid| run("mountvol", &[mount_dir.as_ref(), guid.as_ref()]));
if !mounted {
assert!(
std::env::var_os("EXARCH_REQUIRE_PRIVILEGED_TESTS").is_none(),
"mountvol failed but EXARCH_REQUIRE_PRIVILEGED_TESTS is set"
);
eprintln!("skipped: mountvol needs administrator rights");
return;
}
let pid = std::process::id();
let names = [
format!(".exarch-escape-tmp-{pid}"),
format!(".exarch-escape-final-{pid}"),
format!(".exarch-escape-dir-{pid}"),
];
let _guard = MountGuard {
mount_dir,
escaped: names.iter().map(|name| drive_root.join(name)).collect(),
};
let [first, second, third] = &names;
let created = held.create_new(leaf(first), None);
let renamed = created.is_ok().then(|| {
held.replace_entry(
&TempLeaf::named(first),
leaf(second),
ReplacePermit::for_test(),
)
});
let made = held.make_dir(leaf(third));
let opened = made.is_ok().then(|| held.open_dir(leaf(third)).map(drop));
for name in &names {
assert!(
!drive_root.join(name).exists(),
"{name} escaped: create={created:?} rename={renamed:?} make={made:?} open={opened:?}"
);
}
}
}