use crate::logging::{debug, info, warn};
use rand::Rng;
use serde::{Deserialize, Serialize};
use std::fs::{File, TryLockError};
use std::io::{self, Read, Write};
use std::path::{Path, PathBuf};
use std::time::{SystemTime, UNIX_EPOCH};
pub const MARKER_FILE: &str = ".ant-devnet-run.json";
pub const LOCK_FILE: &str = ".ant-devnet-run.lock";
const MARKER_TEMP_FILE: &str = ".ant-devnet-run.json.tmp";
const MAX_MARKER_LEN: usize = 4096;
const CREATE_ATTEMPTS: usize = 8;
#[derive(Debug, Serialize, Deserialize)]
struct Marker {
run_id: String,
file_id: u64,
lock_id: u64,
pid: u32,
started_at: u64,
version: String,
keep: bool,
}
#[derive(Debug, PartialEq, Eq)]
struct FileId {
device: String,
file: u64,
}
pub struct RunDir {
path: PathBuf,
name: String,
run_id: String,
parent: Parent,
run: Run,
lock: File,
}
impl RunDir {
pub fn create(parent: &Path, keep: bool) -> io::Result<Self> {
let parent = Parent::open(parent)?;
sweep(&parent);
let pid = std::process::id();
let mut rng = rand::thread_rng();
let mut attempt = 0;
let name = loop {
let name = format!("{pid}-{:08x}", rng.gen::<u32>());
match parent.create_run(&name) {
Ok(()) => break name,
Err(e) if e.kind() == io::ErrorKind::AlreadyExists => {
attempt += 1;
if attempt == CREATE_ATTEMPTS {
return Err(e);
}
}
Err(e) => return Err(e),
}
};
#[cfg(test)]
after_create(&parent.path.join(&name))?;
let run = parent.open_run(&name)?;
#[cfg(unix)]
let replaced = !run.is_empty()? || !run.is_owned_by_this_user()?;
#[cfg(windows)]
let replaced = !run.is_empty()?;
if replaced {
return Err(io::Error::new(
io::ErrorKind::AlreadyExists,
format!(
"{} was replaced before this run could open it",
parent.path.join(&name).display()
),
));
}
let claimed = run.id().and_then(|id| {
let mut marker = Marker {
run_id: hex::encode(rng.gen::<[u8; 16]>()),
file_id: id.file,
lock_id: 0,
pid,
started_at: SystemTime::now()
.duration_since(UNIX_EPOCH)
.map_or(0, |since| since.as_secs()),
version: env!("CARGO_PKG_VERSION").to_string(),
keep,
};
let lock = claim(&run, &mut marker)?;
Ok((marker.run_id, lock))
});
let (run_id, lock) = match claimed {
Ok(claimed) => claimed,
Err(e) => {
if let Err(cleanup) =
same_dir(&parent, &name, &run).and_then(|()| parent.remove_empty(&name, run))
{
warn!("Could not remove half-created devnet run directory {name}: {cleanup}");
}
return Err(e);
}
};
Ok(Self {
path: parent.path.join(&name),
name,
run_id,
parent,
run,
lock,
})
}
pub fn path(&self) -> &Path {
&self.path
}
pub fn remove(self) -> io::Result<()> {
let Self {
path,
name,
run_id,
parent,
run,
lock,
} = self;
let marker = read_marker(&run)?;
if marker.run_id != run_id || marker.file_id != run.id()?.file {
return Err(io::Error::new(
io::ErrorKind::InvalidData,
format!(
"{} is no longer this run's directory: its marker names another run",
path.display()
),
));
}
delete(&parent, &name, run, lock)
}
pub fn keep_locked_until_exit(self) {
let Self { lock, .. } = self;
std::mem::forget(lock);
}
}
fn claim(run: &Run, marker: &mut Marker) -> io::Result<File> {
let lock = run.create_file(LOCK_FILE)?;
let outcome = lock.try_lock().map_err(io::Error::from).and_then(|()| {
marker.lock_id = file_id(&lock)?.file;
write_marker(run, marker)
});
match outcome {
Ok(()) => Ok(lock),
Err(e) => {
drop(lock);
if let Err(cleanup) = run.remove_file(LOCK_FILE) {
warn!("Could not remove the lock file of a devnet run that failed to start: {cleanup}");
}
Err(e)
}
}
}
fn write_marker(run: &Run, marker: &Marker) -> io::Result<()> {
let json = serde_json::to_vec_pretty(marker).map_err(io::Error::other)?;
let mut temp = run.create_file(MARKER_TEMP_FILE)?;
let written = temp.write_all(&json).and_then(|()| temp.sync_all());
drop(temp);
let renamed = written.and_then(|()| run.rename(MARKER_TEMP_FILE, MARKER_FILE));
if renamed.is_err() {
if let Err(cleanup) = run.remove_file(MARKER_TEMP_FILE) {
warn!("Could not remove the marker of a devnet run that failed to start: {cleanup}");
}
}
renamed
}
fn read_marker(run: &Run) -> io::Result<Marker> {
let mut json = Vec::new();
run.open_file(MARKER_FILE)?
.take(MAX_MARKER_LEN as u64 + 1)
.read_to_end(&mut json)?;
if json.len() > MAX_MARKER_LEN {
return Err(io::Error::new(
io::ErrorKind::InvalidData,
"the marker is too large to be one",
));
}
serde_json::from_slice(&json).map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))
}
fn same_dir(parent: &Parent, name: &str, run: &Run) -> io::Result<()> {
if parent.id_of(name)? == run.id()? {
Ok(())
} else {
Err(io::Error::new(
io::ErrorKind::InvalidData,
"it has been moved or replaced since it was opened",
))
}
}
fn delete(parent: &Parent, name: &str, run: Run, lock: File) -> io::Result<()> {
same_dir(parent, name, &run)?;
run.remove_all_but(&[MARKER_FILE, LOCK_FILE])?;
run.remove_file(MARKER_FILE)?;
drop(lock);
run.remove_file(LOCK_FILE)?;
same_dir(parent, name, &run)?;
parent.remove_empty(name, run)
}
fn is_run_name(name: &str) -> bool {
name.split_once('-').is_some_and(|(pid, random)| {
!pid.is_empty()
&& pid.bytes().all(|b| b.is_ascii_digit())
&& random.len() == 8
&& random
.bytes()
.all(|b| b.is_ascii_digit() || (b'a'..=b'f').contains(&b))
})
}
fn sweep(parent: &Parent) {
let names = match parent.names() {
Ok(names) => names,
Err(e) => {
warn!(
"Could not list {} for finished devnet runs: {e}",
parent.path.display()
);
return;
}
};
let mut removed = 0_usize;
for name in names.iter().filter(|name| is_run_name(name)) {
match finished_run(parent, name) {
Ok(Some((run, lock))) => {
#[cfg(test)]
after_found(&parent.path.join(name));
match delete(parent, name, run, lock) {
Ok(()) => removed += 1,
Err(e) => warn!(
"Could not remove the finished devnet run directory {}: {e}",
parent.path.join(name).display()
),
}
}
Ok(None) => {}
Err(e) => debug!("Leaving {}: {e}", parent.path.join(name).display()),
}
}
if removed > 0 {
info!(
"Removed {removed} directories left by earlier devnet runs from {}",
parent.path.display()
);
}
}
fn finished_run(parent: &Parent, name: &str) -> io::Result<Option<(Run, File)>> {
let run = parent.open_run(name)?;
#[cfg(unix)]
if !run.is_owned_by_this_user()? {
return Ok(None);
}
let marker = read_marker(&run)?;
let pid_in_name = name.split_once('-').map(|(pid, _)| pid);
let id = run.id()?;
if marker.keep
|| pid_in_name != Some(marker.pid.to_string().as_str())
|| marker.file_id != id.file
|| id.device != parent.id()?.device
{
return Ok(None);
}
#[cfg(unix)]
if run.mount()? != parent.mount()? {
return Ok(None);
}
let lock = run.open_file(LOCK_FILE)?;
let lock_id = file_id(&lock)?;
let the_runs_lock = lock_id.file == marker.lock_id;
if !the_runs_lock || lock_id.device != id.device {
return Ok(None);
}
match lock.try_lock() {
Ok(()) => Ok(Some((run, lock))),
Err(TryLockError::WouldBlock) => Ok(None),
Err(TryLockError::Error(e)) => Err(e),
}
}
#[cfg(test)]
thread_local! {
static FAIL_AFTER: std::cell::Cell<Option<usize>> = const { std::cell::Cell::new(None) };
static AFTER_CREATE: std::cell::RefCell<Option<AfterCreate>> =
const { std::cell::RefCell::new(None) };
static AFTER_FOUND: std::cell::RefCell<Option<AfterFound>> =
const { std::cell::RefCell::new(None) };
#[cfg(unix)]
static MOUNT_POINT: std::cell::RefCell<Option<String>> = const { std::cell::RefCell::new(None) };
}
#[cfg(test)]
type AfterCreate = Box<dyn FnOnce(&Path) -> io::Result<()>>;
#[cfg(test)]
type AfterFound = Box<dyn FnOnce(&Path)>;
#[cfg(test)]
fn after_create(path: &Path) -> io::Result<()> {
AFTER_CREATE
.with(|act| act.borrow_mut().take())
.map_or(Ok(()), |act| act(path))
}
#[cfg(test)]
fn after_found(path: &Path) {
if let Some(act) = AFTER_FOUND.with(|act| act.borrow_mut().take()) {
act(path);
}
}
#[cfg(all(test, unix))]
fn is_test_mount_point(name: &std::ffi::CStr) -> bool {
MOUNT_POINT.with(|mount| mount.borrow().as_deref().map(str::as_bytes) == Some(name.to_bytes()))
}
#[cfg(test)]
fn injected_failure() -> io::Result<()> {
FAIL_AFTER.with(|left| match left.get() {
Some(0) => Err(io::Error::other("injected failure")),
Some(n) => {
left.set(Some(n - 1));
Ok(())
}
None => Ok(()),
})
}
#[cfg(unix)]
use unix::{file_id, Parent, Run};
#[cfg(windows)]
use windows::{file_id, Parent, Run};
#[cfg(unix)]
mod unix {
use super::FileId;
use rustix::fs::{AtFlags, Dev, FileType, Mode, OFlags, Stat};
use rustix::io::Errno;
use std::ffi::{CStr, CString};
use std::fs::File;
use std::io;
use std::os::fd::OwnedFd;
use std::path::{Path, PathBuf};
const DIR_FLAGS: OFlags = OFlags::RDONLY
.union(OFlags::DIRECTORY)
.union(OFlags::NOFOLLOW)
.union(OFlags::CLOEXEC);
pub struct Parent {
pub path: PathBuf,
fd: OwnedFd,
}
pub struct Run {
fd: OwnedFd,
}
impl Parent {
pub fn open(path: &Path) -> io::Result<Self> {
std::fs::create_dir_all(path)?;
let real = std::fs::canonicalize(path)?;
no_one_else_can_change(&real)?;
let fd = rustix::fs::open(
&real,
OFlags::RDONLY | OFlags::DIRECTORY | OFlags::CLOEXEC,
Mode::empty(),
)?;
if std::fs::canonicalize(&real)? != real
|| id(&rustix::fs::stat(&real)?) != id(&rustix::fs::fstat(&fd)?)
{
return Err(io::Error::new(
io::ErrorKind::InvalidData,
format!("{} changed while it was being opened", path.display()),
));
}
Ok(Self { path: real, fd })
}
pub fn mount(&self) -> io::Result<Mount> {
mount_of(&self.fd)
}
pub fn create_run(&self, name: &str) -> io::Result<()> {
Ok(rustix::fs::mkdirat(
&self.fd,
name,
Mode::from_raw_mode(0o755),
)?)
}
pub fn open_run(&self, name: &str) -> io::Result<Run> {
let fd = rustix::fs::openat(&self.fd, name, DIR_FLAGS, Mode::empty())?;
Ok(Run { fd })
}
pub fn names(&self) -> io::Result<Vec<String>> {
Ok(read_names(&self.fd)?
.into_iter()
.filter_map(|name| name.into_string().ok())
.collect())
}
pub fn id(&self) -> io::Result<FileId> {
Ok(id(&rustix::fs::fstat(&self.fd)?))
}
pub fn id_of(&self, name: &str) -> io::Result<FileId> {
let stat = rustix::fs::statat(&self.fd, name, AtFlags::SYMLINK_NOFOLLOW)?;
if FileType::from_raw_mode(stat.st_mode) != FileType::Directory {
return Err(io::Error::new(
io::ErrorKind::InvalidData,
format!("{name} is not a directory"),
));
}
Ok(id(&stat))
}
pub fn remove_empty(&self, name: &str, run: Run) -> io::Result<()> {
drop(run);
gone_is_fine(rustix::fs::unlinkat(&self.fd, name, AtFlags::REMOVEDIR))
}
}
impl Run {
pub fn id(&self) -> io::Result<FileId> {
Ok(id(&rustix::fs::fstat(&self.fd)?))
}
pub fn is_empty(&self) -> io::Result<bool> {
Ok(read_names(&self.fd)?.is_empty())
}
pub fn is_owned_by_this_user(&self) -> io::Result<bool> {
Ok(rustix::fs::fstat(&self.fd)?.st_uid == rustix::process::geteuid().as_raw())
}
pub fn mount(&self) -> io::Result<Mount> {
mount_of(&self.fd)
}
pub fn create_file(&self, name: &str) -> io::Result<File> {
let fd = rustix::fs::openat(
&self.fd,
name,
OFlags::RDWR | OFlags::CREATE | OFlags::EXCL | OFlags::NOFOLLOW | OFlags::CLOEXEC,
Mode::from_raw_mode(0o644),
)?;
Ok(File::from(fd))
}
pub fn open_file(&self, name: &str) -> io::Result<File> {
let fd = rustix::fs::openat(
&self.fd,
name,
OFlags::RDWR | OFlags::NOFOLLOW | OFlags::NONBLOCK | OFlags::CLOEXEC,
Mode::empty(),
)?;
if FileType::from_raw_mode(rustix::fs::fstat(&fd)?.st_mode) != FileType::RegularFile {
return Err(io::Error::new(
io::ErrorKind::InvalidData,
format!("{name} is not a regular file"),
));
}
Ok(File::from(fd))
}
pub fn rename(&self, from: &str, to: &str) -> io::Result<()> {
Ok(rustix::fs::renameat(&self.fd, from, &self.fd, to)?)
}
pub fn remove_file(&self, name: &str) -> io::Result<()> {
gone_is_fine(rustix::fs::unlinkat(&self.fd, name, AtFlags::empty()))
}
pub fn remove_all_but(&self, keep: &[&str]) -> io::Result<()> {
let mount = mount_of(&self.fd)?;
for name in read_names(&self.fd)? {
if keep.iter().any(|keep| keep.as_bytes() == name.as_bytes()) {
continue;
}
#[cfg(test)]
super::injected_failure()?;
remove_entry(&self.fd, &name, &mount)?;
}
Ok(())
}
}
pub fn file_id(file: &File) -> io::Result<FileId> {
Ok(id(&rustix::fs::fstat(file)?))
}
fn id(stat: &Stat) -> FileId {
FileId {
device: stat.st_dev.to_string(),
file: stat.st_ino,
}
}
fn no_one_else_can_change(real: &Path) -> io::Result<()> {
let uid = rustix::process::geteuid().as_raw();
for dir in real.ancestors() {
let stat = rustix::fs::lstat(dir)?;
if FileType::from_raw_mode(stat.st_mode) != FileType::Directory {
return Err(io::Error::new(
io::ErrorKind::InvalidData,
format!("{} changed while it was being opened", real.display()),
));
}
let mode = Mode::from_raw_mode(stat.st_mode);
let who = if stat.st_uid != 0 && stat.st_uid != uid {
Some(format!("belongs to user {}", stat.st_uid))
} else if mode.contains(Mode::SVTX) {
None
} else if mode.contains(Mode::WOTH) {
Some("can be written to by every user".to_string())
} else if mode.contains(Mode::WGRP) {
group_writers(dir, &stat)?
} else {
None
};
if let Some(who) = who {
return Err(io::Error::new(
io::ErrorKind::PermissionDenied,
format!(
"{} {who}, so another user could move or redirect a devnet's \
data under it: use a data directory that only you and root \
can change, with only such directories above it",
dir.display()
),
));
}
}
Ok(())
}
fn group_writers(dir: &Path, stat: &Stat) -> io::Result<Option<String>> {
#[cfg(target_os = "linux")]
if stat.st_gid == rustix::process::getegid().as_raw() {
return match rustix::fs::lgetxattr(dir, "system.posix_acl_access", &mut [0_u8; 0]) {
Ok(_) => Ok(Some(
"has an ACL, which can let other users write to it".to_string(),
)),
Err(Errno::NODATA | Errno::OPNOTSUPP) => Ok(None),
Err(e) => Err(e.into()),
};
}
#[cfg(not(target_os = "linux"))]
let _ = dir;
Ok(Some(format!("can be written to by group {}", stat.st_gid)))
}
#[derive(Debug, PartialEq, Eq)]
pub struct Mount {
device: Dev,
#[cfg(target_os = "linux")]
id: u64,
}
pub(super) fn mount_of(dir: &OwnedFd) -> io::Result<Mount> {
let device = rustix::fs::fstat(dir)?.st_dev;
#[cfg(target_os = "linux")]
{
use rustix::fs::StatxFlags;
let unsupported = || {
io::Error::new(
io::ErrorKind::Unsupported,
"cannot tell a bind mount from the directory it is in: that needs \
mount ids, from Linux 5.8, and `statx` allowed",
)
};
let statx = match rustix::fs::statx(dir, c"", AtFlags::EMPTY_PATH, StatxFlags::MNT_ID) {
Ok(statx) => statx,
Err(Errno::NOSYS | Errno::PERM) => return Err(unsupported()),
Err(e) => return Err(e.into()),
};
if statx.stx_mask & StatxFlags::MNT_ID.bits() == 0 {
return Err(unsupported());
}
Ok(Mount {
device,
id: statx.stx_mnt_id,
})
}
#[cfg(target_os = "macos")]
{
Ok(Mount { device })
}
#[cfg(not(any(target_os = "linux", target_os = "macos")))]
{
let _ = device;
Err(io::Error::new(
io::ErrorKind::Unsupported,
"cannot tell mounts apart on this system",
))
}
}
fn remove_entry(dir: &OwnedFd, name: &CStr, mount: &Mount) -> io::Result<()> {
let stat = match rustix::fs::statat(dir, name, AtFlags::SYMLINK_NOFOLLOW) {
Ok(stat) => stat,
Err(Errno::NOENT) => return Ok(()),
Err(e) => return Err(e.into()),
};
if FileType::from_raw_mode(stat.st_mode) != FileType::Directory {
return gone_is_fine(rustix::fs::unlinkat(dir, name, AtFlags::empty()));
}
let child = match rustix::fs::openat(dir, name, DIR_FLAGS, Mode::empty()) {
Ok(child) => child,
Err(Errno::NOENT) => return Ok(()),
Err(e) => return Err(e.into()),
};
#[cfg(not(test))]
let mount_point = mount_of(&child)? != *mount;
#[cfg(test)]
let mount_point = mount_of(&child)? != *mount || super::is_test_mount_point(name);
if mount_point {
return Err(io::Error::new(
io::ErrorKind::InvalidData,
format!(
"{} is a mount point, so it and what is in it are left",
name.to_string_lossy()
),
));
}
for entry in read_names(&child)? {
remove_entry(&child, &entry, mount)?;
}
gone_is_fine(rustix::fs::unlinkat(dir, name, AtFlags::REMOVEDIR))
}
fn read_names(dir: &OwnedFd) -> io::Result<Vec<CString>> {
let mut names = Vec::new();
for entry in rustix::fs::Dir::read_from(dir)? {
let name = entry?.file_name().to_owned();
if name.as_bytes() != b"." && name.as_bytes() != b".." {
names.push(name);
}
}
Ok(names)
}
fn gone_is_fine(result: rustix::io::Result<()>) -> io::Result<()> {
match result {
Ok(()) | Err(Errno::NOENT) => Ok(()),
Err(e) => Err(e.into()),
}
}
}
#[cfg(windows)]
mod windows {
use super::FileId;
use std::fs::{File, OpenOptions};
use std::io;
use std::os::windows::fs::{MetadataExt, OpenOptionsExt};
use std::path::{Path, PathBuf};
const FILE_FLAG_BACKUP_SEMANTICS: u32 = 0x0200_0000;
const FILE_FLAG_OPEN_REPARSE_POINT: u32 = 0x0020_0000;
const FILE_ATTRIBUTE_DIRECTORY: u32 = 0x10;
const FILE_ATTRIBUTE_REPARSE_POINT: u32 = 0x400;
const SHARE_ALL_BUT_DELETE: u32 = 0x1 | 0x2;
pub struct Parent {
pub path: PathBuf,
real: PathBuf,
held: File,
}
pub struct Run {
path: PathBuf,
held: File,
}
impl Parent {
pub fn open(path: &Path) -> io::Result<Self> {
std::fs::create_dir_all(path)?;
let real = std::fs::canonicalize(path)?;
let held = hold_dir(&real)?;
if std::fs::canonicalize(&real)? != real {
return Err(io::Error::new(
io::ErrorKind::InvalidData,
format!("{} changed while it was being opened", path.display()),
));
}
Ok(Self {
path: plain(&real),
real,
held,
})
}
pub fn create_run(&self, name: &str) -> io::Result<()> {
std::fs::create_dir(self.real.join(name))
}
pub fn open_run(&self, name: &str) -> io::Result<Run> {
let path = self.real.join(name);
let held = hold_dir(&path)?;
Ok(Run { path, held })
}
pub fn names(&self) -> io::Result<Vec<String>> {
let mut names = Vec::new();
for entry in std::fs::read_dir(&self.real)? {
if let Ok(name) = entry?.file_name().into_string() {
names.push(name);
}
}
Ok(names)
}
pub fn id(&self) -> io::Result<FileId> {
file_id(&self.held)
}
pub fn id_of(&self, name: &str) -> io::Result<FileId> {
file_id(&hold_dir(&self.real.join(name))?)
}
pub fn remove_empty(&self, name: &str, run: Run) -> io::Result<()> {
drop(run);
gone_is_fine(std::fs::remove_dir(self.real.join(name)))
}
}
impl Run {
pub fn id(&self) -> io::Result<FileId> {
file_id(&self.held)
}
pub fn is_empty(&self) -> io::Result<bool> {
Ok(std::fs::read_dir(&self.path)?.next().is_none())
}
pub fn create_file(&self, name: &str) -> io::Result<File> {
OpenOptions::new()
.read(true)
.write(true)
.create_new(true)
.open(self.path.join(name))
}
pub fn open_file(&self, name: &str) -> io::Result<File> {
let file = OpenOptions::new()
.read(true)
.write(true)
.custom_flags(FILE_FLAG_OPEN_REPARSE_POINT)
.open(self.path.join(name))?;
let meta = file.metadata()?;
if !meta.is_file() || meta.file_attributes() & FILE_ATTRIBUTE_REPARSE_POINT != 0 {
return Err(io::Error::new(
io::ErrorKind::InvalidData,
format!("{name} is not a regular file"),
));
}
Ok(file)
}
pub fn rename(&self, from: &str, to: &str) -> io::Result<()> {
std::fs::rename(self.path.join(from), self.path.join(to))
}
pub fn remove_file(&self, name: &str) -> io::Result<()> {
gone_is_fine(std::fs::remove_file(self.path.join(name)))
}
pub fn remove_all_but(&self, keep: &[&str]) -> io::Result<()> {
for entry in std::fs::read_dir(&self.path)? {
let entry = entry?;
if keep.iter().any(|keep| entry.file_name() == *keep) {
continue;
}
#[cfg(test)]
super::injected_failure()?;
let path = entry.path();
gone_is_fine(
if entry.metadata()?.file_attributes() & FILE_ATTRIBUTE_DIRECTORY == 0 {
std::fs::remove_file(path)
} else {
std::fs::remove_dir_all(path)
},
)?;
}
Ok(())
}
}
fn plain(real: &Path) -> PathBuf {
real.to_str()
.and_then(|text| text.strip_prefix(r"\\?\"))
.map(PathBuf::from)
.filter(|plain| std::fs::canonicalize(plain).is_ok_and(|again| again == real))
.unwrap_or_else(|| real.to_path_buf())
}
fn hold_dir(path: &Path) -> io::Result<File> {
let dir = OpenOptions::new()
.read(true)
.share_mode(SHARE_ALL_BUT_DELETE)
.custom_flags(FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OPEN_REPARSE_POINT)
.open(path)?;
let meta = dir.metadata()?;
if !meta.is_dir() || meta.file_attributes() & FILE_ATTRIBUTE_REPARSE_POINT != 0 {
return Err(io::Error::new(
io::ErrorKind::InvalidData,
format!("{} is not a directory", path.display()),
));
}
Ok(dir)
}
pub fn file_id(file: &File) -> io::Result<FileId> {
let info = winapi_util::file::information(file)?;
Ok(FileId {
device: info.volume_serial_number().to_string(),
file: info.file_index(),
})
}
fn gone_is_fine(result: io::Result<()>) -> io::Result<()> {
match result {
Err(e) if e.kind() == io::ErrorKind::NotFound => Ok(()),
result => result,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
type TestResult = std::result::Result<(), Box<dyn std::error::Error>>;
fn marker_in(dir: &Path) -> std::result::Result<Marker, Box<dyn std::error::Error>> {
Ok(serde_json::from_slice(&std::fs::read(
dir.join(MARKER_FILE),
)?)?)
}
fn lock_is_free(dir: &Path) -> std::result::Result<bool, Box<dyn std::error::Error>> {
let lock = File::options()
.read(true)
.write(true)
.open(dir.join(LOCK_FILE))?;
match lock.try_lock() {
Ok(()) => Ok(true),
Err(TryLockError::WouldBlock) => Ok(false),
Err(TryLockError::Error(e)) => Err(e.into()),
}
}
fn file_id_of(path: &Path) -> std::result::Result<u64, Box<dyn std::error::Error>> {
Ok(file_id(&File::open(path)?)?.file)
}
fn finished(
parent: &Path,
keep: bool,
) -> std::result::Result<PathBuf, Box<dyn std::error::Error>> {
let run = RunDir::create(parent, keep)?;
let path = run.path().to_path_buf();
std::fs::create_dir_all(path.join("nodes").join("peer"))?;
std::fs::write(path.join("nodes").join("peer").join("chunk"), b"data")?;
drop(run);
Ok(path)
}
fn forged(
parent: &Path,
name: &str,
edit: impl FnOnce(&mut Marker),
) -> std::result::Result<PathBuf, Box<dyn std::error::Error>> {
let dir = parent.join(name);
std::fs::create_dir_all(&dir)?;
let file_id = Parent::open(parent)?.open_run(name)?.id()?.file;
std::fs::write(dir.join(LOCK_FILE), b"")?;
let lock_id = file_id_of(&dir.join(LOCK_FILE))?;
let mut marker = Marker {
run_id: "forged".to_string(),
file_id,
lock_id,
pid: std::process::id(),
started_at: 0,
version: env!("CARGO_PKG_VERSION").to_string(),
keep: false,
};
edit(&mut marker);
std::fs::write(dir.join(MARKER_FILE), serde_json::to_vec(&marker)?)?;
std::fs::write(dir.join("sentinel"), b"keep")?;
Ok(dir)
}
#[test]
fn create_makes_a_locked_marked_directory_inside_the_parent() -> TestResult {
let temp = tempfile::tempdir()?;
let parent = temp.path().join("not").join("there").join("yet");
let run = RunDir::create(&parent, false)?;
assert_eq!(
run.path().parent().map(std::fs::canonicalize).transpose()?,
Some(std::fs::canonicalize(&parent)?)
);
#[cfg(windows)]
assert!(
!run.path().to_string_lossy().starts_with(r"\\?\"),
"{}",
run.path().display()
);
let name = run
.path()
.file_name()
.and_then(|name| name.to_str())
.ok_or("no name")?;
assert!(is_run_name(name), "{name}");
let marker = marker_in(run.path())?;
assert_eq!(marker.pid, std::process::id());
assert_eq!(marker.run_id, run.run_id);
assert_eq!(marker.file_id, run.run.id()?.file);
assert_eq!(marker.lock_id, file_id(&run.lock)?.file);
assert!(!marker.keep);
assert!(!run.path().join(MARKER_TEMP_FILE).exists());
assert!(
!lock_is_free(run.path())?,
"the lock is free while the run lasts"
);
Ok(())
}
#[test]
fn remove_deletes_the_run_directory_and_nothing_beside_it() -> TestResult {
let temp = tempfile::tempdir()?;
let parent = temp.path().join("devnets");
std::fs::create_dir_all(parent.join("mine"))?;
std::fs::write(parent.join("mine").join("sentinel"), b"keep")?;
std::fs::write(temp.path().join("sentinel"), b"keep")?;
let run = RunDir::create(&parent, false)?;
let path = run.path().to_path_buf();
std::fs::create_dir_all(path.join("nodes").join("peer").join("deeper"))?;
std::fs::write(
path.join("nodes").join("peer").join("deeper").join("f"),
b"x",
)?;
run.remove()?;
assert!(!path.exists());
assert!(parent.is_dir(), "the parent is not the run's to delete");
assert!(parent.join("mine").join("sentinel").is_file());
assert!(temp.path().join("sentinel").is_file());
Ok(())
}
#[test]
fn remove_refuses_a_directory_another_run_has_marked() -> TestResult {
let temp = tempfile::tempdir()?;
let run = RunDir::create(temp.path(), false)?;
let path = run.path().to_path_buf();
let mut marker = marker_in(&path)?;
marker.run_id = "someone-else".to_string();
std::fs::write(path.join(MARKER_FILE), serde_json::to_vec(&marker)?)?;
assert!(run.remove().is_err());
assert!(path.join(MARKER_FILE).is_file());
Ok(())
}
#[cfg(unix)]
#[test]
fn remove_refuses_a_replacement_carrying_a_copy_of_the_marker() -> TestResult {
let temp = tempfile::tempdir()?;
let run = RunDir::create(temp.path(), false)?;
let path = run.path().to_path_buf();
std::fs::write(path.join("data"), b"the run's")?;
let moved = temp.path().join("moved");
std::fs::rename(&path, &moved)?;
std::fs::create_dir(&path)?;
std::fs::copy(moved.join(MARKER_FILE), path.join(MARKER_FILE))?;
std::fs::copy(moved.join(LOCK_FILE), path.join(LOCK_FILE))?;
std::fs::write(path.join("sentinel"), b"not the run's")?;
assert!(run.remove().is_err());
assert!(path.join("sentinel").is_file());
assert!(path.join(MARKER_FILE).is_file());
assert!(moved.join("data").is_file());
Ok(())
}
#[cfg(windows)]
#[test]
fn a_run_directory_and_every_directory_above_it_cannot_be_moved_while_the_run_lasts(
) -> TestResult {
let temp = tempfile::tempdir()?;
let above = temp.path().join("above");
let parent = above.join("devnets");
let run = RunDir::create(&parent, false)?;
let path = run.path().to_path_buf();
assert!(std::fs::rename(&path, parent.join("moved")).is_err());
assert!(std::fs::rename(&parent, above.join("moved")).is_err());
assert!(std::fs::rename(&above, temp.path().join("moved")).is_err());
let temp_moved = temp.path().with_extension("moved");
assert!(std::fs::rename(temp.path(), &temp_moved).is_err());
run.remove()?;
assert!(!path.exists());
Ok(())
}
#[test]
fn the_next_run_sweeps_a_finished_run() -> TestResult {
let temp = tempfile::tempdir()?;
let old = finished(temp.path(), false)?;
assert!(old.is_dir());
let run = RunDir::create(temp.path(), false)?;
assert!(!old.exists(), "{} was not swept", old.display());
assert!(run.path().is_dir());
Ok(())
}
#[test]
fn a_removal_that_stops_part_way_is_finished_by_the_next_run() -> TestResult {
let temp = tempfile::tempdir()?;
let run = RunDir::create(temp.path(), false)?;
let path = run.path().to_path_buf();
for i in 0..4 {
std::fs::create_dir_all(path.join(format!("node-{i}")))?;
std::fs::write(path.join(format!("node-{i}")).join("chunk"), b"data")?;
}
FAIL_AFTER.with(|left| left.set(Some(2)));
let stopped = run.remove();
FAIL_AFTER.with(|left| left.set(None));
assert!(stopped.is_err());
assert!(path.join(MARKER_FILE).is_file());
assert!(path.join(LOCK_FILE).is_file());
let _next = RunDir::create(temp.path(), false)?;
assert!(!path.exists(), "{} was not swept", path.display());
Ok(())
}
#[test]
fn a_live_run_is_never_swept() -> TestResult {
let temp = tempfile::tempdir()?;
let first = RunDir::create(temp.path(), false)?;
let second = RunDir::create(temp.path(), false)?;
assert!(first.path().join(MARKER_FILE).is_file());
assert!(second.path().join(MARKER_FILE).is_file());
assert_ne!(first.path(), second.path());
Ok(())
}
#[test]
fn a_run_kept_locked_until_exit_is_never_swept() -> TestResult {
let temp = tempfile::tempdir()?;
let run = RunDir::create(temp.path(), false)?;
let path = run.path().to_path_buf();
run.keep_locked_until_exit();
let _next = RunDir::create(temp.path(), false)?;
assert!(path.join(MARKER_FILE).is_file());
assert!(!lock_is_free(&path)?);
Ok(())
}
#[test]
fn a_live_run_whose_lock_file_is_replaced_is_never_swept() -> TestResult {
let temp = tempfile::tempdir()?;
let run = RunDir::create(temp.path(), false)?;
let path = run.path().to_path_buf();
std::fs::write(path.join("data"), b"the run's")?;
std::fs::rename(path.join(LOCK_FILE), path.join("moved.lock"))?;
std::fs::write(path.join(LOCK_FILE), b"")?;
assert!(lock_is_free(&path)?);
let _next = RunDir::create(temp.path(), false)?;
assert!(path.join("data").is_file(), "{} was swept", path.display());
drop(run);
Ok(())
}
#[test]
fn a_directory_put_in_place_of_a_new_run_directory_is_left_alone() -> TestResult {
for with_lock in [false, true] {
let temp = tempfile::tempdir()?;
let parent = temp.path().join("devnets");
let moved = temp.path().join("moved");
let replaced = std::rc::Rc::new(std::cell::RefCell::new(None));
let record = std::rc::Rc::clone(&replaced);
AFTER_CREATE.with(|act| {
*act.borrow_mut() = Some(Box::new(move |path: &Path| {
std::fs::rename(path, &moved)?;
std::fs::create_dir(path)?;
std::fs::write(path.join("sentinel"), b"not the run's")?;
if with_lock {
std::fs::write(path.join(LOCK_FILE), b"not the run's")?;
}
*record.borrow_mut() = Some(path.to_path_buf());
Ok(())
}));
});
let created = RunDir::create(&parent, false);
AFTER_CREATE.with(|act| act.borrow_mut().take());
assert!(created.is_err(), "with_lock: {with_lock}");
let path = replaced.borrow_mut().take().ok_or("never replaced")?;
assert!(path.join("sentinel").is_file(), "with_lock: {with_lock}");
assert!(!path.join(MARKER_FILE).exists(), "with_lock: {with_lock}");
assert_eq!(path.join(LOCK_FILE).is_file(), with_lock);
}
Ok(())
}
#[test]
fn a_kept_run_is_never_swept() -> TestResult {
let temp = tempfile::tempdir()?;
let kept = finished(temp.path(), true)?;
assert!(marker_in(&kept)?.keep);
let _run = RunDir::create(temp.path(), false)?;
assert!(kept.join("nodes").join("peer").join("chunk").is_file());
Ok(())
}
#[test]
fn a_copy_of_a_finished_runs_marker_is_never_swept() -> TestResult {
let temp = tempfile::tempdir()?;
let old = finished(temp.path(), false)?;
let moved = temp.path().join("moved");
std::fs::rename(&old, &moved)?;
std::fs::create_dir(&old)?;
std::fs::copy(moved.join(MARKER_FILE), old.join(MARKER_FILE))?;
std::fs::rename(moved.join(LOCK_FILE), old.join(LOCK_FILE))?;
std::fs::write(old.join("sentinel"), b"keep")?;
let _run = RunDir::create(temp.path(), false)?;
assert!(old.join("sentinel").is_file());
assert!(moved.join("nodes").join("peer").join("chunk").is_file());
Ok(())
}
#[test]
fn a_finished_run_moved_once_it_is_found_is_left() -> TestResult {
let temp = tempfile::tempdir()?;
let old = finished(temp.path(), false)?;
let moved = temp.path().join("moved");
let to = moved.clone();
let outcome = std::rc::Rc::new(std::cell::RefCell::new(None));
let record = std::rc::Rc::clone(&outcome);
AFTER_FOUND.with(|act| {
*act.borrow_mut() = Some(Box::new(move |path: &Path| {
*record.borrow_mut() = Some(std::fs::rename(path, &to).is_ok());
}));
});
let created = RunDir::create(temp.path(), false);
AFTER_FOUND.with(|act| act.borrow_mut().take());
let _run = created?;
let was_moved = outcome.borrow_mut().take().ok_or("never found")?;
if cfg!(windows) {
assert!(!was_moved, "moved while the sweep held it");
assert!(!old.exists(), "{} was not swept", old.display());
} else {
assert!(was_moved);
assert!(
moved.join("nodes").join("peer").join("chunk").is_file(),
"emptied where it was moved to"
);
assert!(moved.join(MARKER_FILE).is_file());
}
Ok(())
}
#[test]
fn directories_without_a_matching_marker_are_never_swept() -> TestResult {
let temp = tempfile::tempdir()?;
let parent = temp.path();
let pid = std::process::id();
let survivors = [
{
let dir = parent.join("1-deadbeef");
std::fs::create_dir_all(&dir)?;
std::fs::write(dir.join("sentinel"), b"keep")?;
dir
},
forged(parent, "other", |_| {})?,
forged(parent, &format!("{pid}-DEADBEEF"), |_| {})?,
forged(parent, &format!("{pid}-deadbeef0"), |_| {})?,
forged(parent, &format!("{}-deadbeef", pid.wrapping_add(1)), |_| {})?,
forged(parent, &format!("{pid}-d1ffe4e7"), |marker| {
marker.file_id = marker.file_id.wrapping_add(1);
})?,
forged(parent, &format!("{pid}-10c4ed00"), |marker| {
marker.lock_id = marker.lock_id.wrapping_add(1);
})?,
forged(parent, &format!("{pid}-cee9cee9"), |marker| {
marker.keep = true;
})?,
{
let dir = forged(parent, &format!("{pid}-0badf00d"), |_| {})?;
std::fs::write(dir.join(MARKER_FILE), b"{ not json")?;
dir
},
{
let dir = forged(parent, &format!("{pid}-0ddba11a"), |_| {})?;
let mut big = std::fs::read(dir.join(MARKER_FILE))?;
big.resize(MAX_MARKER_LEN + 1, b' ');
std::fs::write(dir.join(MARKER_FILE), big)?;
dir
},
{
let dir = forged(parent, &format!("{pid}-ca11ab1e"), |_| {})?;
std::fs::remove_file(dir.join(LOCK_FILE))?;
dir
},
];
let control = forged(parent, &format!("{pid}-c0ffee00"), |_| {})?;
let _run = RunDir::create(parent, false)?;
for dir in &survivors {
assert!(
dir.join("sentinel").is_file(),
"{} was swept",
dir.display()
);
}
assert!(!control.exists(), "{} was not swept", control.display());
Ok(())
}
#[test]
fn run_names() {
assert!(is_run_name("1-deadbeef"));
assert!(is_run_name("123456-0000000a"));
for name in [
"",
"-deadbeef",
"1-",
"1-deadbee",
"1-deadbeef0",
"1-DEADBEEF",
"a-deadbeef",
"1_deadbeef",
"1-deadbeeg",
"1-dead-bee",
] {
assert!(!is_run_name(name), "{name}");
}
}
#[cfg(unix)]
#[test]
fn removal_does_not_cross_into_another_mount() -> TestResult {
let temp = tempfile::tempdir()?;
let run = RunDir::create(temp.path(), false)?;
let path = run.path().to_path_buf();
let mounted = path.join("nodes").join("mounted");
std::fs::create_dir_all(&mounted)?;
std::fs::write(mounted.join("data"), b"not the run's")?;
MOUNT_POINT.with(|mount| *mount.borrow_mut() = Some("mounted".to_string()));
let removed = run.remove();
MOUNT_POINT.with(|mount| mount.borrow_mut().take());
assert!(removed.is_err());
assert!(mounted.join("data").is_file());
assert!(path.join(MARKER_FILE).is_file());
Ok(())
}
#[cfg(target_os = "linux")]
fn sudo(args: &[&std::ffi::OsStr], path: &Path) -> io::Result<()> {
let status = std::process::Command::new("sudo")
.arg("-n")
.args(args)
.arg(path)
.status()?;
if status.success() {
Ok(())
} else {
Err(io::Error::other(format!(
"sudo {args:?} {} failed",
path.display()
)))
}
}
#[cfg(target_os = "linux")]
struct Mounted(PathBuf);
#[cfg(target_os = "linux")]
impl Mounted {
fn new(at: &Path, how: &[&std::ffi::OsStr]) -> io::Result<Self> {
sudo(&[&[std::ffi::OsStr::new("mount")], how].concat(), at)?;
Ok(Self(at.to_path_buf()))
}
}
#[cfg(target_os = "linux")]
impl Drop for Mounted {
fn drop(&mut self) {
if let Err(e) = sudo(&[std::ffi::OsStr::new("umount")], &self.0) {
eprintln!("{e}");
}
}
}
#[cfg(target_os = "linux")]
fn nobody_moves(from: &Path, to: &Path) -> io::Result<bool> {
Ok(std::process::Command::new("sudo")
.args(["-n", "-u", "nobody", "mv"])
.arg(from)
.arg(to)
.stderr(std::process::Stdio::null())
.status()?
.success())
}
#[cfg(target_os = "linux")]
#[test]
#[ignore = "mounts through sudo; CI runs it"]
fn with_sudo_mounts_inside_a_run_directory_are_not_crossed() -> TestResult {
use std::ffi::OsStr;
for bind in [false, true] {
let temp = tempfile::tempdir()?;
let source = temp.path().join("source");
std::fs::create_dir(&source)?;
std::fs::write(source.join("data"), b"not the run's")?;
let run = RunDir::create(&temp.path().join("devnets"), false)?;
let path = run.path().to_path_buf();
let at = path.join("nodes").join("mounted");
std::fs::create_dir_all(&at)?;
let _mounted = if bind {
Mounted::new(&at, &[OsStr::new("--bind"), source.as_os_str()])?
} else {
let tmpfs = ["-t", "tmpfs", "-o", "mode=1777", "none"].map(OsStr::new);
let mounted = Mounted::new(&at, &tmpfs)?;
std::fs::write(at.join("data"), b"not the run's")?;
mounted
};
assert!(run.remove().is_err(), "bind: {bind}");
assert!(at.join("data").is_file(), "bind: {bind}");
assert!(source.join("data").is_file(), "bind: {bind}");
assert!(path.join(MARKER_FILE).is_file(), "bind: {bind}");
}
Ok(())
}
#[cfg(target_os = "linux")]
#[test]
#[ignore = "mounts through sudo; CI runs it"]
fn with_sudo_a_mount_wearing_a_finished_runs_name_is_not_swept() -> TestResult {
let temp = tempfile::tempdir()?;
let outside = finished(&temp.path().join("outside"), false)?;
let parent = temp.path().join("devnets");
let at = parent.join(outside.file_name().ok_or("no name")?);
std::fs::create_dir_all(&at)?;
let _mounted = Mounted::new(&at, &[std::ffi::OsStr::new("--bind"), outside.as_os_str()])?;
assert!(at.join(MARKER_FILE).is_file());
let _run = RunDir::create(&parent, false)?;
assert!(
outside.join("nodes").join("peer").join("chunk").is_file(),
"swept through the mount"
);
Ok(())
}
#[cfg(target_os = "linux")]
#[test]
#[ignore = "makes a directory another user owns through sudo; CI runs it"]
fn with_sudo_an_empty_directory_of_another_user_is_not_claimed() -> TestResult {
let temp = tempfile::tempdir()?;
let parent = temp.path().join("devnets");
let moved = temp.path().join("moved");
let replaced = std::rc::Rc::new(std::cell::RefCell::new(None));
let record = std::rc::Rc::clone(&replaced);
AFTER_CREATE.with(|act| {
*act.borrow_mut() = Some(Box::new(move |path: &Path| {
std::fs::rename(path, &moved)?;
sudo(&["mkdir", "-m", "0777"].map(std::ffi::OsStr::new), path)?;
*record.borrow_mut() = Some(path.to_path_buf());
Ok(())
}));
});
let created = RunDir::create(&parent, false);
AFTER_CREATE.with(|act| act.borrow_mut().take());
assert!(created.is_err());
let path = replaced.borrow_mut().take().ok_or("never replaced")?;
assert!(!path.join(MARKER_FILE).exists());
assert!(!path.join(LOCK_FILE).exists());
Ok(())
}
#[cfg(target_os = "linux")]
#[test]
#[ignore = "acts as another user through sudo; CI runs it"]
fn with_sudo_another_user_cannot_redirect_what_a_run_writes() -> TestResult {
use std::ffi::OsStr;
use std::os::unix::fs::PermissionsExt;
let temp = tempfile::tempdir()?;
std::fs::set_permissions(temp.path(), std::fs::Permissions::from_mode(0o755))?;
let open = temp.path().join("open");
sudo(&["mkdir", "-m", "0777"].map(OsStr::new), &open)?;
std::fs::create_dir(open.join("mine"))?;
assert!(nobody_moves(&open.join("mine"), &open.join("moved"))?);
let refused = RunDir::create(&open.join("moved").join("devnets"), false);
assert_eq!(
refused.err().map(|e| e.kind()),
Some(io::ErrorKind::PermissionDenied)
);
let sticky = temp.path().join("sticky");
sudo(&["mkdir", "-m", "1777"].map(OsStr::new), &sticky)?;
let mine = sticky.join("mine");
let run = RunDir::create(&mine.join("devnets"), false)?;
assert!(!nobody_moves(&mine, &sticky.join("moved"))?);
std::fs::write(run.path().join("written"), b"the run's")?;
let name = run.path().file_name().ok_or("no name")?;
assert!(mine.join("devnets").join(name).join("written").is_file());
run.remove()?;
Ok(())
}
#[cfg(target_os = "linux")]
#[test]
#[ignore = "makes directories another user owns through sudo; CI runs it"]
fn with_sudo_a_parent_below_another_users_or_groups_directory_is_refused() -> TestResult {
use std::ffi::OsStr;
let temp = tempfile::tempdir()?;
let me = rustix::process::geteuid().as_raw().to_string();
for (name, how) in [
("theirs", ["-o", "65534", "-m", "0755"]),
("their-group", ["-g", "65534", "-m", "0775"]),
] {
let above = temp.path().join(name);
let parent = above.join("devnets");
let make: Vec<&OsStr> = ["install", "-d"]
.into_iter()
.chain(how)
.map(OsStr::new)
.collect();
sudo(&make, &above)?;
sudo(
&["install", "-d", "-o", me.as_str()].map(OsStr::new),
&parent,
)?;
let refused = RunDir::create(&parent, false);
assert_eq!(
refused.err().map(|e| e.kind()),
Some(io::ErrorKind::PermissionDenied),
"{name}"
);
assert!(std::fs::read_dir(&parent)?.next().is_none(), "{name}");
}
Ok(())
}
#[cfg(target_os = "linux")]
#[test]
#[ignore = "hands a directory to another user through sudo; CI runs it"]
fn with_sudo_another_users_finished_run_is_not_swept() -> TestResult {
use std::ffi::OsStr;
let temp = tempfile::tempdir()?;
let old = finished(temp.path(), false)?;
sudo(&["chown", "-R", "65534"].map(OsStr::new), &old)?;
sudo(&["chmod", "-R", "a+rwX"].map(OsStr::new), &old)?;
let _run = RunDir::create(temp.path(), false)?;
assert!(old.join("nodes").join("peer").join("chunk").is_file());
Ok(())
}
#[cfg(any(target_os = "linux", target_os = "macos"))]
#[test]
fn a_directory_on_another_mount_is_told_apart() -> TestResult {
use rustix::fs::{Mode, OFlags};
let open = |path: &Path| {
rustix::fs::open(
path,
OFlags::RDONLY | OFlags::DIRECTORY | OFlags::CLOEXEC,
Mode::empty(),
)
};
let temp = tempfile::tempdir()?;
std::fs::create_dir(temp.path().join("sub"))?;
let other = if cfg!(target_os = "linux") {
"/proc"
} else {
"/dev"
};
let here = unix::mount_of(&open(temp.path())?)?;
assert_eq!(unix::mount_of(&open(&temp.path().join("sub"))?)?, here);
assert_ne!(unix::mount_of(&open(Path::new(other))?)?, here);
Ok(())
}
#[cfg(unix)]
#[test]
fn a_parent_behind_a_dangling_symlink_fails_closed() -> TestResult {
let temp = tempfile::tempdir()?;
let victim = temp.path().join("victim");
std::fs::create_dir_all(&victim)?;
std::fs::write(victim.join("sentinel"), b"keep")?;
std::os::unix::fs::symlink(victim.join("not-yet"), temp.path().join("alias"))?;
assert!(RunDir::create(&temp.path().join("alias").join("devnets"), false).is_err());
assert!(victim.join("sentinel").is_file());
assert!(!victim.join("not-yet").exists());
Ok(())
}
#[cfg(unix)]
fn parent_with(
which: &str,
mode: u32,
) -> std::result::Result<(tempfile::TempDir, PathBuf), Box<dyn std::error::Error>> {
use std::os::unix::fs::PermissionsExt;
let temp = tempfile::tempdir()?;
let above = temp.path().join("above");
let parent = above.join("devnets");
std::fs::create_dir_all(&parent)?;
let dir = if which == "above" { &above } else { &parent };
std::fs::set_permissions(dir, std::fs::Permissions::from_mode(mode))?;
Ok((temp, parent))
}
#[cfg(unix)]
#[test]
fn a_parent_another_user_could_change_is_refused() -> TestResult {
let mut cases = vec![("above", 0o777), ("parent", 0o777)];
if cfg!(not(target_os = "linux")) {
cases.push(("above", 0o775));
}
for (which, mode) in cases {
let (_temp, parent) = parent_with(which, mode)?;
let refused = RunDir::create(&parent, false);
assert_eq!(
refused.err().map(|e| e.kind()),
Some(io::ErrorKind::PermissionDenied),
"{which} {mode:o}"
);
assert!(
std::fs::read_dir(&parent)?.next().is_none(),
"{which} {mode:o}"
);
}
Ok(())
}
#[cfg(unix)]
#[test]
fn a_parent_only_this_user_can_change_is_accepted() -> TestResult {
let mut cases = vec![("above", 0o1777), ("parent", 0o1777)];
if cfg!(target_os = "linux") {
cases.push(("above", 0o775));
}
for (which, mode) in cases {
let (_temp, parent) = parent_with(which, mode)?;
let run =
RunDir::create(&parent, false).map_err(|e| format!("{which} {mode:o}: {e}"))?;
assert!(run.path().join(MARKER_FILE).is_file(), "{which} {mode:o}");
}
Ok(())
}
#[cfg(target_os = "linux")]
#[test]
fn a_parent_below_a_directory_with_an_acl_is_refused() -> TestResult {
let (_temp, parent) = parent_with("above", 0o755)?;
let above = parent.parent().ok_or("no parent")?;
let mut acl = 2_u32.to_le_bytes().to_vec();
for (tag, perm, id) in [
(0x01_u16, 7_u16, u32::MAX),
(0x02, 7, 65534),
(0x04, 5, u32::MAX),
(0x10, 7, u32::MAX),
(0x20, 5, u32::MAX),
] {
acl.extend_from_slice(&tag.to_le_bytes());
acl.extend_from_slice(&perm.to_le_bytes());
acl.extend_from_slice(&id.to_le_bytes());
}
rustix::fs::lsetxattr(
above,
"system.posix_acl_access",
&acl,
rustix::fs::XattrFlags::empty(),
)?;
let mode = rustix::fs::Mode::from_raw_mode(rustix::fs::lstat(above)?.st_mode);
assert!(mode.contains(rustix::fs::Mode::WGRP), "{mode:?}");
let refused = RunDir::create(&parent, false);
assert_eq!(
refused.err().map(|e| e.kind()),
Some(io::ErrorKind::PermissionDenied)
);
Ok(())
}
#[cfg(unix)]
#[test]
fn a_link_named_like_a_run_is_never_followed() -> TestResult {
let temp = tempfile::tempdir()?;
let parent = temp.path().join("devnets");
let outside = finished(&temp.path().join("outside"), false)?;
std::fs::write(outside.join("sentinel"), b"keep")?;
std::fs::create_dir_all(&parent)?;
let link = parent.join(outside.file_name().ok_or("no name")?);
std::os::unix::fs::symlink(&outside, &link)?;
let _run = RunDir::create(&parent, false)?;
assert!(outside.join("sentinel").is_file());
assert!(std::fs::symlink_metadata(&link)?.file_type().is_symlink());
Ok(())
}
#[cfg(unix)]
#[test]
fn retargeting_a_symlink_above_the_run_does_not_redirect_removal() -> TestResult {
let temp = tempfile::tempdir()?;
let first = temp.path().join("first");
let second = temp.path().join("second");
std::fs::create_dir_all(&first)?;
std::fs::create_dir_all(&second)?;
let link = temp.path().join("link");
std::os::unix::fs::symlink(&first, &link)?;
let run = RunDir::create(&link.join("devnets"), false)?;
let name = run.path().file_name().ok_or("no name")?.to_owned();
let decoy = second.join("devnets").join(&name);
std::fs::create_dir_all(&decoy)?;
std::fs::copy(run.path().join(MARKER_FILE), decoy.join(MARKER_FILE))?;
std::fs::write(decoy.join("sentinel"), b"keep")?;
std::fs::remove_file(&link)?;
std::os::unix::fs::symlink(&second, &link)?;
std::fs::write(run.path().join("written"), b"the run's")?;
assert!(first.join("devnets").join(&name).join("written").is_file());
assert!(!decoy.join("written").exists(), "path() followed the link");
run.remove()?;
assert!(!first.join("devnets").join(&name).exists());
assert!(
decoy.join("sentinel").is_file(),
"the removal followed the retargeted link"
);
Ok(())
}
#[cfg(windows)]
fn junction(link: &Path, target: &Path) -> TestResult {
use std::os::windows::process::CommandExt;
let status = std::process::Command::new("cmd")
.raw_arg(r#"/D /V:OFF /S /C "mklink /J "%ANT_TEST_LINK%" "%ANT_TEST_TARGET%"""#)
.env("ANT_TEST_LINK", link)
.env("ANT_TEST_TARGET", target)
.stdout(std::process::Stdio::null())
.status()?;
if status.success() {
Ok(())
} else {
Err(format!("mklink /J {} {} failed", link.display(), target.display()).into())
}
}
#[cfg(windows)]
#[test]
fn retargeting_a_junction_above_the_run_does_not_redirect_removal() -> TestResult {
let temp = tempfile::tempdir()?;
let first = temp.path().join("first & %USERNAME% ^(1)!");
let second = temp.path().join("second ; %PATH% = ^&");
std::fs::create_dir_all(&first)?;
std::fs::create_dir_all(&second)?;
let link = temp.path().join("link & %TEMP%");
junction(&link, &first)?;
let run = RunDir::create(&link.join("devnets"), false)?;
let name = run.path().file_name().ok_or("no name")?.to_owned();
let decoy = second.join("devnets").join(&name);
std::fs::create_dir_all(&decoy)?;
std::fs::copy(run.path().join(MARKER_FILE), decoy.join(MARKER_FILE))?;
std::fs::write(decoy.join("sentinel"), b"keep")?;
std::fs::remove_dir(&link)?;
junction(&link, &second)?;
std::fs::write(run.path().join("written"), b"the run's")?;
assert!(first.join("devnets").join(&name).join("written").is_file());
assert!(
!decoy.join("written").exists(),
"path() followed the junction"
);
run.remove()?;
assert!(!first.join("devnets").join(&name).exists());
assert!(
decoy.join("sentinel").is_file(),
"the removal followed the retargeted junction"
);
Ok(())
}
}