use std::collections::{BTreeMap, VecDeque};
use std::ffi::{CStr, CString, OsStr, c_char, c_int};
use std::os::unix::ffi::OsStrExt;
use std::path::{Component, Path, PathBuf};
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::{Arc, Mutex, OnceLock, Weak};
use std::time::{SystemTime, UNIX_EPOCH};
use glob::Pattern;
use snare_interpose::{Fs, NetResult as FsResult, SetTimes};
use crate::HashMap;
use crate::clock::Clock;
use crate::fs_locks::{self, Locks, Target};
use crate::fs_mmap::{self, Backing};
use crate::fs_perm::{self, Credentials, Kind, Owner, SET_GID, SET_UID};
pub(crate) struct OpenFiles<T> {
handles: HashMap<c_int, u64>,
descriptions: HashMap<u64, (usize, T)>,
next: u64,
}
impl<T> Default for OpenFiles<T> {
fn default() -> Self {
Self {
handles: HashMap::default(),
descriptions: HashMap::default(),
next: 0,
}
}
}
impl<T> OpenFiles<T> {
pub(crate) fn contains_key(&self, fd: &c_int) -> bool {
self.handles.contains_key(fd)
}
pub(crate) fn get(&self, fd: &c_int) -> Option<&T> {
self.descriptions
.get(self.handles.get(fd)?)
.map(|(_, value)| value)
}
pub(crate) fn get_mut(&mut self, fd: &c_int) -> Option<&mut T> {
self.descriptions
.get_mut(self.handles.get(fd)?)
.map(|(_, value)| value)
}
pub(crate) fn insert(&mut self, fd: c_int, value: T) {
self.remove(&fd);
let id = self.next;
self.next += 1;
self.descriptions.insert(id, (1, value));
self.handles.insert(fd, id);
}
pub(crate) fn remove(&mut self, fd: &c_int) -> Option<Option<T>> {
let id = self.handles.remove(fd)?;
let (refs, _) = self.descriptions.get_mut(&id).unwrap();
*refs -= 1;
Some(if *refs == 0 {
self.descriptions.remove(&id).map(|(_, value)| value)
} else {
None
})
}
pub(crate) fn alias(&mut self, oldfd: c_int, newfd: c_int) -> Option<Option<T>> {
let id = *self.handles.get(&oldfd)?;
if oldfd == newfd {
return Some(None);
}
let released = self.remove(&newfd).flatten();
self.descriptions.get_mut(&id).unwrap().0 += 1;
self.handles.insert(newfd, id);
Some(released)
}
}
pub(crate) fn status_flags(flags: c_int) -> c_int {
flags & !(libc::O_CLOEXEC | libc::O_CREAT | libc::O_EXCL | libc::O_TRUNC | libc::O_NOCTTY)
}
pub(crate) fn set_cloexec(fd: c_int, flags: c_int) {
if flags & libc::O_CLOEXEC != 0 {
snare_interpose::real(|| unsafe { libc::fcntl(fd, libc::F_SETFD, libc::FD_CLOEXEC) });
}
}
pub(crate) fn descriptor_fcntl(fd: c_int, cmd: c_int, arg: i64) -> Option<FsResult> {
let result = snare_interpose::real(|| unsafe { libc::fcntl(fd, cmd, arg as c_int) });
if result < 0 {
err(std::io::Error::last_os_error()
.raw_os_error()
.unwrap_or(libc::EINVAL))
} else {
ok(result as i64)
}
}
#[derive(Clone)]
enum VNode {
File(Arc<VInode>),
Dir(Arc<VInode>),
Symlink(Arc<VInode>, Arc<Vec<u8>>),
}
struct VInode {
number: u64,
kind: Kind,
data: Mutex<SparseFile>,
links: AtomicU64,
times: Mutex<Times>,
owner: Mutex<Owner>,
locks: Locks,
}
pub(crate) struct Attributes {
pub(crate) mode: u32,
pub(crate) size: u64,
pub(crate) ino: u64,
pub(crate) nlink: u64,
pub(crate) uid: u32,
pub(crate) gid: u32,
}
pub(crate) type Stamp = i128;
const DAY: Stamp = 86_400 * 1_000_000_000;
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub(crate) struct Times {
pub(crate) accessed: Stamp,
pub(crate) modified: Stamp,
pub(crate) changed: Stamp,
pub(crate) born: Stamp,
}
impl Times {
pub(crate) fn at(now: Stamp) -> Self {
Self {
accessed: now,
modified: now,
changed: now,
born: now,
}
}
fn modify(&mut self, now: Stamp) {
self.modified = now;
self.changed = now;
}
fn access(&mut self, now: Stamp) {
if self.accessed <= self.modified
|| self.accessed <= self.changed
|| now - self.accessed >= DAY
{
self.accessed = now;
}
}
fn set(&mut self, set: &snare_interpose::SetTimes, now: Stamp) {
use snare_interpose::TimeSet;
let value = |time: TimeSet| match time {
TimeSet::Omit => None,
TimeSet::Now => Some(now),
TimeSet::At(at) => Some(at),
};
let (accessed, modified, created) =
(value(set.accessed), value(set.modified), value(set.created));
if let Some(at) = accessed {
self.accessed = at;
}
if let Some(at) = modified {
self.modified = at;
}
let changed = if cfg!(target_os = "macos") {
if let Some(at) = created {
self.born = at;
}
if let Some(at) = modified {
self.born = self.born.min(at);
}
modified.is_some() || created.is_some()
} else {
accessed.is_some() || modified.is_some()
};
if changed {
self.changed = now;
}
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct NodeTimes {
pub accessed: Option<SystemTime>,
pub modified: Option<SystemTime>,
pub changed: Option<SystemTime>,
pub created: Option<SystemTime>,
}
impl NodeTimes {
fn resolve(&self, now: Stamp) -> Times {
let stamp = |time: Option<SystemTime>| time.map_or(now, stamp_of);
Times {
accessed: stamp(self.accessed),
modified: stamp(self.modified),
changed: stamp(self.changed),
born: stamp(self.created),
}
}
}
pub(crate) fn stamp_of(time: SystemTime) -> Stamp {
match time.duration_since(UNIX_EPOCH) {
Ok(after) => after.as_nanos() as Stamp,
Err(before) => -(before.duration().as_nanos() as Stamp),
}
}
pub(crate) fn clock_now(clock: Option<&Clock>) -> Stamp {
match clock {
Some(clock) => clock.realtime_peek().as_nanos() as Stamp,
None => stamp_of(snare_interpose::real(SystemTime::now)),
}
}
const PAGE_SIZE: usize = 4096;
struct Volume {
capacity: u64,
used: AtomicU64,
files: AtomicU64,
}
impl Volume {
#[allow(deprecated)]
fn reserve(&self) -> bool {
self.used
.fetch_update(Ordering::AcqRel, Ordering::Acquire, |used| {
(used < self.capacity).then_some(used + 1)
})
.is_ok()
}
}
struct SparseFile {
pages: HashMap<usize, Box<[u8]>>,
length: usize,
volume: Option<Arc<Volume>>,
backing: Option<Backing>,
}
impl SparseFile {
fn new(bytes: Vec<u8>) -> Self {
let mut pages = HashMap::default();
for (index, chunk) in bytes.chunks(PAGE_SIZE).enumerate() {
let mut page = vec![0; PAGE_SIZE].into_boxed_slice();
page[..chunk.len()].copy_from_slice(chunk);
pages.insert(index, page);
}
Self {
pages,
length: bytes.len(),
volume: None,
backing: None,
}
}
fn attach(&mut self, volume: &Arc<Volume>) {
assert!(self.volume.is_none());
let allocated = self.pages.len() as u64;
let previous = volume.used.fetch_add(allocated, Ordering::AcqRel);
assert!(
previous + allocated <= volume.capacity,
"declared files exceed the storage limit"
);
volume.files.fetch_add(1, Ordering::AcqRel);
self.volume = Some(volume.clone());
}
fn len(&self) -> usize {
self.length
}
fn clear(&mut self) -> Result<(), c_int> {
self.resize(0)
}
fn resize(&mut self, length: usize) -> Result<(), c_int> {
if let Some(backing) = &mut self.backing {
backing.resize(length)?;
}
let before = self.pages.len();
self.pages
.retain(|index, _| *index < length.div_ceil(PAGE_SIZE));
if self.backing.is_none()
&& let Some(page) = self.pages.get_mut(&(length / PAGE_SIZE))
{
page[length % PAGE_SIZE..].fill(0);
}
if let Some(volume) = &self.volume {
volume
.used
.fetch_sub((before - self.pages.len()) as u64, Ordering::AcqRel);
}
self.length = length;
Ok(())
}
unsafe fn read(&self, start: usize, buf: *mut u8, len: usize) -> Result<usize, c_int> {
let count = len.min(self.length.saturating_sub(start));
if let Some(backing) = &self.backing {
return unsafe { backing.read(start, buf, count) };
}
let mut read = 0;
while read < count {
let offset = start + read;
let within = offset % PAGE_SIZE;
let n = (PAGE_SIZE - within).min(count - read);
unsafe {
if let Some(page) = self.pages.get(&(offset / PAGE_SIZE)) {
std::ptr::copy_nonoverlapping(page.as_ptr().add(within), buf.add(read), n);
} else {
buf.add(read).write_bytes(0, n);
}
}
read += n;
}
Ok(count)
}
fn write(&mut self, start: usize, bytes: &[u8]) -> Result<usize, c_int> {
if start
.checked_add(bytes.len())
.is_none_or(|end| end > i64::MAX as usize)
{
return Err(libc::EFBIG);
}
let mut allocated = 0;
while allocated < bytes.len() {
let offset = start + allocated;
if let Err(error) = self.allocate(offset / PAGE_SIZE) {
if allocated == 0 {
return Err(error);
}
break;
}
allocated += (PAGE_SIZE - offset % PAGE_SIZE).min(bytes.len() - allocated);
}
let bytes = &bytes[..allocated];
if let Some(backing) = &mut self.backing {
let written = backing.write(start, bytes)?;
self.length = self.length.max(start + written);
return Ok(written);
}
let mut written = 0;
while written < bytes.len() {
let offset = start + written;
let within = offset % PAGE_SIZE;
let count = (PAGE_SIZE - within).min(bytes.len() - written);
self.pages.get_mut(&(offset / PAGE_SIZE)).unwrap()[within..within + count]
.copy_from_slice(&bytes[written..written + count]);
written += count;
}
self.length = self.length.max(start + written);
Ok(written)
}
fn allocate(&mut self, index: usize) -> Result<(), c_int> {
if self.pages.contains_key(&index) {
return Ok(());
}
if self.volume.as_ref().is_some_and(|volume| !volume.reserve()) {
return Err(libc::ENOSPC);
}
let mut page = Vec::new();
let size = if self.backing.is_some() { 0 } else { PAGE_SIZE };
if page.try_reserve_exact(size).is_err() || self.pages.try_reserve(1).is_err() {
if let Some(volume) = &self.volume {
volume.used.fetch_sub(1, Ordering::AcqRel);
}
return Err(libc::ENOMEM);
}
page.resize(size, 0);
self.pages.insert(index, page.into_boxed_slice());
Ok(())
}
fn share(&mut self) -> Result<&mut Backing, c_int> {
if self.backing.is_none() {
let backing = Backing::new(
self.length,
self.pages.iter().map(|(index, page)| (*index, &page[..])),
)?;
for page in self.pages.values_mut() {
*page = Box::default();
}
self.backing = Some(backing);
}
Ok(self.backing.as_mut().unwrap())
}
fn settle(&mut self) -> bool {
let Some(backing) = &mut self.backing else {
return false;
};
let mut written = backing.written_through_mapping();
let filled: Vec<usize> = backing
.exposed(self.length.div_ceil(PAGE_SIZE))
.filter(|index| {
!self.pages.contains_key(index) && backing.page_has_data(*index, self.length)
})
.collect();
for index in filled {
self.pages.insert(index, Box::default());
if let Some(volume) = &self.volume {
volume.used.fetch_add(1, Ordering::AcqRel);
}
written = true;
}
written
}
}
impl Drop for SparseFile {
fn drop(&mut self) {
if let Some(volume) = &self.volume {
volume
.used
.fetch_sub(self.pages.len() as u64, Ordering::AcqRel);
volume.files.fetch_sub(1, Ordering::AcqRel);
}
}
}
impl VInode {
fn new(number: u64, data: Vec<u8>, kind: Kind, now: Stamp, owner: Owner) -> Arc<Self> {
Arc::new(Self {
number,
kind,
data: Mutex::new(SparseFile::new(data)),
links: AtomicU64::new(if kind == Kind::Directory { 2 } else { 1 }),
times: Mutex::new(Times::at(now)),
owner: Mutex::new(owner),
locks: Locks::default(),
})
}
fn times(&self) -> Times {
*self.times.lock().unwrap()
}
fn touch(&self, change: impl FnOnce(&mut Times)) {
change(&mut self.times.lock().unwrap());
}
fn owner(&self) -> Owner {
*self.owner.lock().unwrap()
}
fn attributes(&self, size: u64) -> Attributes {
let owner = self.owner();
let (kind, size) = match self.kind {
Kind::File => (libc::S_IFREG, self.data.lock().unwrap().len() as u64),
Kind::Directory => (libc::S_IFDIR, 0),
Kind::Symlink => (libc::S_IFLNK, size),
};
#[allow(clippy::unnecessary_cast)]
let kind = kind as u32;
Attributes {
mode: kind | owner.mode,
size,
ino: self.number,
nlink: self.links.load(Ordering::Acquire),
uid: owner.uid,
gid: owner.gid,
}
}
fn wrote(&self, truncate: bool) {
let mut owner = self.owner.lock().unwrap();
if owner.mode & (SET_UID | SET_GID) != 0 {
Credentials::current().wrote(&mut owner, truncate);
}
}
}
impl VNode {
fn inode(&self) -> &Arc<VInode> {
match self {
Self::File(inode) | Self::Dir(inode) | Self::Symlink(inode, _) => inode,
}
}
fn attributes(&self) -> Attributes {
match self {
Self::Symlink(inode, target) => inode.attributes(target.len() as u64),
node => node.inode().attributes(0),
}
}
}
struct OpenFile {
inode: Arc<VInode>,
cursor: usize,
writable: bool,
append: bool,
flags: c_int,
is_dir: bool,
directory: Option<Arc<Mutex<DirStream>>>,
description: u64,
}
impl Drop for OpenFile {
fn drop(&mut self) {
self.inode.locks.release_description(self.description);
}
}
#[derive(Default)]
pub struct FsBuilder {
tree: BTreeMap<PathBuf, VNode>,
next_inode: u64,
owned_prefixes: Vec<PathBuf>,
passthrough: Vec<Pattern>,
deny: Vec<Pattern>,
storage_limit: Option<u64>,
times: BTreeMap<PathBuf, NodeTimes>,
modes: BTreeMap<PathBuf, u32>,
owners: BTreeMap<PathBuf, (u32, u32)>,
}
fn declared_mode(kind: Kind) -> u32 {
match kind {
Kind::File => 0o644,
Kind::Directory => 0o755,
Kind::Symlink if cfg!(target_os = "macos") => 0o755,
Kind::Symlink => 0o777,
}
}
impl FsBuilder {
pub fn new() -> Self {
Self::default()
}
pub fn storage_limit(mut self, bytes: u64) -> Self {
self.storage_limit = Some(bytes);
self
}
pub fn file(mut self, path: impl AsRef<Path>, contents: impl Into<Vec<u8>>) -> Self {
let path = normalize(path.as_ref());
let mut dir = path.clone();
while dir.pop() {
if !self.tree.contains_key(&dir) {
let inode = self.inode(Vec::new(), Kind::Directory);
self.tree.insert(dir.clone(), VNode::Dir(inode));
}
}
let inode = self.inode(contents.into(), Kind::File);
self.tree.insert(path, VNode::File(inode));
self
}
pub fn dir(mut self, path: impl AsRef<Path>) -> Self {
let inode = self.inode(Vec::new(), Kind::Directory);
self.tree
.insert(normalize(path.as_ref()), VNode::Dir(inode));
self
}
pub fn symlink(mut self, path: impl AsRef<Path>, target: impl AsRef<Path>) -> Self {
let path = normalize(path.as_ref());
let mut parent = path.clone();
while parent.pop() {
if !self.tree.contains_key(&parent) {
let inode = self.inode(Vec::new(), Kind::Directory);
self.tree.insert(parent.clone(), VNode::Dir(inode));
}
}
let inode = self.inode(Vec::new(), Kind::Symlink);
self.tree.insert(
path,
VNode::Symlink(
inode,
Arc::new(target.as_ref().as_os_str().as_bytes().to_vec()),
),
);
self
}
pub fn times(mut self, path: impl AsRef<Path>, times: NodeTimes) -> Self {
self.times.insert(normalize(path.as_ref()), times);
self
}
pub fn mode(mut self, path: impl AsRef<Path>, mode: u32) -> Self {
self.modes.insert(normalize(path.as_ref()), mode & 0o7777);
self
}
pub fn owner(mut self, path: impl AsRef<Path>, uid: u32, gid: u32) -> Self {
self.owners.insert(normalize(path.as_ref()), (uid, gid));
self
}
pub fn own_prefix(mut self, path: impl AsRef<Path>) -> Self {
self.owned_prefixes.push(normalize(path.as_ref()));
self
}
pub fn passthrough(mut self, glob: &str) -> Self {
if let Ok(pattern) = Pattern::new(glob) {
self.passthrough.push(pattern);
}
self
}
pub fn deny(mut self, glob: &str) -> Self {
if let Ok(pattern) = Pattern::new(glob) {
self.deny.push(pattern);
}
self
}
pub fn build(self) -> std::sync::Arc<VirtualFs> {
std::sync::Arc::new(VirtualFs::new(self))
}
fn inode(&mut self, data: Vec<u8>, kind: Kind) -> Arc<VInode> {
let number = self.next_inode | 1;
self.next_inode = number + 2;
let owner = Owner {
mode: declared_mode(kind),
uid: 0,
gid: 0,
};
VInode::new(number, data, kind, 0, owner)
}
}
type Declared = (Arc<VInode>, NodeTimes, Option<(u32, u32)>);
pub struct VirtualFs {
tree: Mutex<BTreeMap<PathBuf, VNode>>,
next_inode: AtomicU64,
cwd: Mutex<Option<Arc<VInode>>>,
volume: Arc<Volume>,
open: Mutex<OpenFiles<OpenFile>>,
dirs: Mutex<OpenFiles<Arc<Mutex<DirStream>>>>,
owned_prefixes: Vec<PathBuf>,
passthrough: Vec<Pattern>,
deny: Vec<Pattern>,
devnull: c_int,
device: u64,
clock: OnceLock<Option<Arc<Clock>>>,
declared: Mutex<Vec<Declared>>,
mapped: Mutex<Vec<Weak<VInode>>>,
}
pub(crate) struct DirStream {
pub(crate) entries: Vec<DirEntry>,
pub(crate) cursor: usize,
pub(crate) scratch: Box<Dirent>,
inode: Option<Arc<VInode>>,
#[cfg(target_os = "linux")]
pub(crate) path: Option<PathBuf>,
#[cfg(target_os = "linux")]
pub(crate) description: u64,
}
impl DirStream {
pub(crate) fn new(entries: Vec<DirEntry>) -> Self {
let scratch = Box::new(unsafe { core::mem::zeroed::<Dirent>() });
DirStream {
entries,
cursor: 0,
scratch,
inode: None,
#[cfg(target_os = "linux")]
path: None,
#[cfg(target_os = "linux")]
description: fs_locks::description(),
}
}
}
pub(crate) struct DirEntry {
pub(crate) name: Vec<u8>,
pub(crate) ino: u64,
pub(crate) dtype: u8,
}
#[cfg(target_os = "linux")]
pub(crate) type Dirent = libc::dirent64;
#[cfg(not(target_os = "linux"))]
pub(crate) type Dirent = libc::dirent;
impl VirtualFs {
fn volume_stats(&self, buf: *mut u8) -> Option<FsResult> {
if buf.is_null() {
return err(libc::EFAULT);
}
let mapped: Vec<Arc<VInode>> = {
let mut mapped = self.mapped.lock().unwrap();
mapped.retain(|inode| inode.strong_count() > 0);
mapped.iter().filter_map(Weak::upgrade).collect()
};
for inode in &mapped {
self.settle(inode);
}
let mut stat: libc::statfs = unsafe { std::mem::zeroed() };
stat.f_bsize = PAGE_SIZE as _;
stat.f_blocks = self.volume.capacity as _;
let free = self
.volume
.capacity
.saturating_sub(self.volume.used.load(Ordering::Acquire));
stat.f_bfree = free as _;
stat.f_bavail = free as _;
stat.f_files = u64::MAX as _;
stat.f_ffree = u64::MAX.saturating_sub(self.volume.files.load(Ordering::Acquire)) as _;
#[cfg(target_os = "linux")]
{
stat.f_type = 0x0102_1994;
stat.f_frsize = PAGE_SIZE as _;
stat.f_namelen = 255;
}
#[cfg(target_os = "macos")]
{
stat.f_iosize = PAGE_SIZE as _;
stat.f_flags = libc::MNT_LOCAL as _;
for (out, byte) in stat.f_fstypename.iter_mut().zip(b"snare") {
*out = *byte as c_char;
}
stat.f_mntonname[0] = b'/' as c_char;
for (out, byte) in stat.f_mntfromname.iter_mut().zip(b"snare") {
*out = *byte as c_char;
}
}
unsafe {
std::ptr::from_mut(&mut stat.f_fsid).cast::<i32>().write(1);
buf.cast::<libc::statfs>().write(stat);
}
ok(0)
}
fn new(b: FsBuilder) -> Self {
let volume = Arc::new(Volume {
capacity: b.storage_limit.unwrap_or(1 << 30) / PAGE_SIZE as u64,
used: AtomicU64::new(0),
files: AtomicU64::new(0),
});
for node in b.tree.values() {
node.inode().data.lock().unwrap().attach(&volume);
}
let devnull = snare_interpose::real(|| unsafe {
libc::open(c"/dev/null".as_ptr(), libc::O_RDWR | libc::O_CLOEXEC)
});
let mut times = b.times;
let mut modes = b.modes;
let mut owners = b.owners;
let caller = Credentials::current();
let declared = b
.tree
.iter()
.map(|(path, node)| {
let named = owners.remove(path);
let (uid, gid) = named.unwrap_or((caller.uid, caller.gid));
let mut owner = node.inode().owner.lock().unwrap();
(owner.uid, owner.gid) = (uid, gid);
if let Some(mode) = modes.remove(path) {
owner.mode = mode;
}
(
node.inode().clone(),
times.remove(path).unwrap_or_default(),
named,
)
})
.collect();
for (call, path) in [
("times", times.keys().next()),
("mode", modes.keys().next()),
("owner", owners.keys().next()),
] {
if let Some(path) = path {
panic!("FsBuilder::{call} for undeclared path {}", path.display());
}
}
for (path, node) in &b.tree {
if matches!(node, VNode::Dir(_))
&& let Some(VNode::Dir(parent)) =
path.parent().and_then(|parent| b.tree.get(parent))
{
parent.links.fetch_add(1, Ordering::Relaxed);
}
}
VirtualFs {
tree: Mutex::new(b.tree),
next_inode: AtomicU64::new(b.next_inode | 1),
cwd: Mutex::default(),
volume,
open: Mutex::default(),
dirs: Mutex::default(),
owned_prefixes: b.owned_prefixes,
passthrough: b.passthrough,
deny: b.deny,
devnull,
device: device(),
clock: OnceLock::new(),
declared: Mutex::new(declared),
mapped: Mutex::default(),
}
}
pub(crate) fn attach(&self, clock: Option<Arc<Clock>>, shared: &crate::scope::SimShared) {
if self.clock.set(clock).is_err() {
return;
}
let now = self.now();
let user = Credentials::of(shared);
for (inode, times, named) in std::mem::take(&mut *self.declared.lock().unwrap()) {
*inode.times.lock().unwrap() = times.resolve(now);
if named.is_none() {
let mut owner = inode.owner.lock().unwrap();
(owner.uid, owner.gid) = (user.uid, user.gid);
}
}
}
fn lock_target(&self, fd: c_int) -> Option<(Arc<VInode>, Target)> {
let open = self.open.lock().unwrap();
let file = open.get(&fd)?;
let target = Target {
readable: file.flags & libc::O_ACCMODE != libc::O_WRONLY,
writable: file.writable,
cursor: file.cursor as u64,
len: file.inode.data.lock().unwrap().len() as u64,
description: file.description,
};
Some((file.inode.clone(), target))
}
fn stream_lock(&self, fd: c_int) -> Option<FsResult> {
self.dirs
.lock()
.unwrap()
.contains_key(&fd)
.then_some(FsResult::Err(libc::EINVAL))
}
fn now(&self) -> Stamp {
clock_now(self.clock.get().and_then(Option::as_deref))
}
fn read_access(&self, inode: &VInode, requested: usize, read: usize) {
if requested > 0 && (read > 0 || cfg!(target_os = "linux")) {
let now = self.now();
inode.touch(|times| times.access(now));
}
}
fn settle(&self, inode: &VInode) {
if inode.data.lock().unwrap().settle() {
let now = self.now();
inode.touch(|times| times.modify(now));
}
}
fn settle_fd(&self, fd: c_int) {
let inode = self
.open
.lock()
.unwrap()
.get(&fd)
.map(|file| file.inode.clone());
if let Some(inode) = inode {
self.settle(&inode);
}
}
fn list_access(&self, stream: &DirStream) {
if let Some(inode) = &stream.inode {
let now = self.now();
inode.touch(|times| times.access(now));
}
}
fn reserve_fd(&self) -> std::io::Result<c_int> {
if self.devnull < 0 {
return Err(std::io::Error::from_raw_os_error(libc::EMFILE));
}
let fd = unsafe { libc::dup(self.devnull) };
if fd < 0 {
return Err(std::io::Error::last_os_error());
}
Ok(fd)
}
fn owned(&self, path: &Path) -> bool {
self.owned_prefixes
.iter()
.any(|prefix| path.starts_with(prefix))
}
fn fd_inode(&self, fd: c_int) -> Option<Arc<VInode>> {
let open = self.open.lock().unwrap();
if let Some(file) = open.get(&fd) {
return Some(file.inode.clone());
}
let dirs = self.dirs.lock().unwrap();
let stream = dirs.get(&fd)?.lock().unwrap();
stream.inode.clone()
}
fn fd_meta(&self, fd: c_int) -> Option<(Attributes, Times)> {
let inode = self.fd_inode(fd)?;
Some((inode.attributes(0), inode.times()))
}
fn matches(patterns: &[Pattern], path: &Path) -> bool {
patterns.iter().any(|p| p.matches_path(path))
}
fn claims(&self, tree: &BTreeMap<PathBuf, VNode>, path: &Path) -> bool {
self.owned(path)
|| tree.contains_key(path)
|| path
.parent()
.is_some_and(|parent| tree.contains_key(parent))
}
fn parent(tree: &BTreeMap<PathBuf, VNode>, path: &Path) -> Result<Arc<VInode>, c_int> {
if path
.ancestors()
.skip(1)
.any(|ancestor| matches!(tree.get(ancestor), Some(VNode::File(_))))
{
return Err(libc::ENOTDIR);
}
match path.parent().and_then(|parent| tree.get(parent)) {
Some(VNode::Dir(inode)) => Ok(inode.clone()),
_ => Err(libc::ENOENT),
}
}
#[cfg(target_os = "linux")]
fn fd_flags(&self, fd: c_int) -> c_int {
self.open
.lock()
.unwrap()
.get(&fd)
.map_or(0, |file| file.flags)
}
fn may_remove(
&self,
tree: &BTreeMap<PathBuf, VNode>,
path: &Path,
victim: &VInode,
) -> Result<(), c_int> {
match path.parent().and_then(|parent| tree.get(parent)) {
Some(VNode::Dir(parent)) => {
Credentials::current().may_remove(&parent.owner(), &victim.owner())
}
_ => Ok(()),
}
}
fn may_rename(
&self,
tree: &BTreeMap<PathBuf, VNode>,
from: &Path,
to: &Path,
source: &VNode,
target: Option<&VNode>,
) -> Result<(), c_int> {
self.may_remove(tree, from, source.inode())?;
let caller = Credentials::current();
match target {
Some(target) => {
self.may_remove(tree, to, target.inode())?;
if cfg!(target_os = "macos") && matches!(target, VNode::Dir(_)) {
caller.check(&target.inode().owner(), Kind::Directory, fs_perm::WRITE)?;
}
}
None => {
if let Some(VNode::Dir(parent)) = to.parent().and_then(|parent| tree.get(parent)) {
caller.check(
&parent.owner(),
Kind::Directory,
fs_perm::WRITE | fs_perm::EXEC,
)?;
}
}
}
if matches!(source, VNode::Dir(_)) && from.parent() != to.parent() {
caller.check(&source.inode().owner(), Kind::Directory, fs_perm::WRITE)?;
}
Ok(())
}
unsafe fn retained_directory(
&self,
fd: c_int,
raw: *const c_char,
) -> Option<Result<Arc<VInode>, c_int>> {
if raw.is_null() {
return None;
}
let bytes = unsafe { CStr::from_ptr(raw) }.to_bytes();
if bytes.is_empty()
|| bytes.starts_with(b"/")
|| !bytes
.split(|byte| *byte == b'/')
.all(|component| component.is_empty() || component == b".")
{
return None;
}
let inode = if fd == libc::AT_FDCWD {
self.cwd.lock().unwrap().clone()
} else {
self.open
.lock()
.unwrap()
.get(&fd)
.filter(|file| file.is_dir)
.map(|file| file.inode.clone())
}?;
Some(
Credentials::current()
.search(&inode.owner())
.map(|()| inode),
)
}
fn open_directory_inode(&self, inode: Arc<VInode>, flags: c_int) -> Option<FsResult> {
if writable(flags) {
return err(libc::EISDIR);
}
if flags & (libc::O_CREAT | libc::O_EXCL) == (libc::O_CREAT | libc::O_EXCL) {
return err(libc::EEXIST);
}
if cfg!(target_os = "linux") && flags & libc::O_CREAT != 0 {
return err(libc::EISDIR);
}
if let Err(error) =
Credentials::current().check(&inode.owner(), Kind::Directory, open_access(flags))
{
return err(error);
}
let fd = match self.reserve_fd() {
Ok(fd) => fd,
Err(error) => return err(error.raw_os_error().unwrap_or(libc::EMFILE)),
};
let entries = {
let tree = self.tree.lock().unwrap();
tree.iter()
.find(|(_, node)| Arc::ptr_eq(node.inode(), &inode))
.map_or_else(Vec::new, |(path, _)| snapshot(&tree, path))
};
let mut stream = DirStream::new(entries);
stream.inode = Some(inode.clone());
self.open.lock().unwrap().insert(
fd,
OpenFile {
inode,
cursor: 0,
writable: false,
append: flags & libc::O_APPEND != 0,
flags: status_flags(flags),
is_dir: true,
directory: Some(Arc::new(Mutex::new(stream))),
description: fs_locks::description(),
},
);
set_cloexec(fd, flags);
ok(fd as i64)
}
unsafe fn absolute(&self, raw: *const c_char) -> Result<Option<CString>, c_int> {
if raw.is_null() {
return Ok(None);
}
let path = unsafe { CStr::from_ptr(raw) };
if path.to_bytes().starts_with(b"/") {
return Ok(Some(path.to_owned()));
}
if path.to_bytes().is_empty() {
return Err(libc::ENOENT);
}
let cwd = match self.cwd_bytes() {
Some(result) => result?,
None => std::env::current_dir()
.map_err(|error| error.raw_os_error().unwrap_or(libc::ENOENT))?
.into_os_string()
.into_encoded_bytes(),
};
let mut absolute = cwd;
absolute.push(b'/');
absolute.extend_from_slice(path.to_bytes());
Ok(Some(CString::new(absolute).unwrap()))
}
fn cwd_bytes(&self) -> Option<Result<Vec<u8>, c_int>> {
let inode = self.cwd.lock().unwrap().clone()?;
let tree = self.tree.lock().unwrap();
Some(
tree.iter()
.find(|(_, node)| Arc::ptr_eq(node.inode(), &inode))
.map(|(path, _)| path.as_os_str().as_bytes().to_vec())
.ok_or(libc::ENOENT),
)
}
unsafe fn traversal(
&self,
tree: &BTreeMap<PathBuf, VNode>,
raw: *const c_char,
path: &Path,
strict_dots: bool,
follow_final: bool,
) -> Result<PathBuf, c_int> {
let raw = unsafe { CStr::from_ptr(raw) }.to_bytes();
let raw_path = Path::new(OsStr::from_bytes(raw));
if !self.claims(tree, path)
&& !tree.iter().any(|(entry, node)| {
matches!(node, VNode::Symlink(..)) && raw_path.starts_with(entry)
})
{
return Ok(path.to_path_buf());
}
let mut pending: VecDeque<Vec<u8>> = raw
.split(|byte| *byte == b'/')
.filter(|component| !component.is_empty())
.map(<[u8]>::to_vec)
.collect();
if raw.ends_with(b"/") && (follow_final || cfg!(target_os = "macos")) {
pending.push_back(Vec::new());
}
let caller = Credentials::current();
let mut current = PathBuf::from("/");
let mut links = 0;
while let Some(component) = pending.pop_front() {
match tree.get(¤t) {
Some(VNode::File(_) | VNode::Symlink(..))
if strict_dots || !matches!(component.as_slice(), b"" | b"." | b"..") =>
{
return Err(libc::ENOTDIR);
}
None if current != Path::new("/") && self.claims(tree, ¤t) => {
return Err(libc::ENOENT);
}
Some(VNode::Dir(inode)) if !component.is_empty() => {
caller.search(&inode.owner())?;
}
_ => {}
}
match component.as_slice() {
b"" | b"." => continue,
b".." => {
current.pop();
continue;
}
component => current.push(OsStr::from_bytes(component)),
}
if let Some(VNode::Symlink(_, target)) = tree.get(¤t)
&& (follow_final || !pending.is_empty())
{
if target.is_empty() {
return Err(libc::ENOENT);
}
links += 1;
if links > if cfg!(target_os = "macos") { 32 } else { 40 } {
return Err(libc::ELOOP);
}
current.pop();
if target.starts_with(b"/") {
current = PathBuf::from("/");
}
let mut components: VecDeque<Vec<u8>> = target
.split(|byte| *byte == b'/')
.filter(|component| !component.is_empty())
.map(<[u8]>::to_vec)
.collect();
if target.ends_with(b"/") && pending.is_empty() {
components.push_back(Vec::new());
}
components.append(&mut pending);
pending = components;
}
}
if links > 0
&& (Self::matches(&self.passthrough, ¤t)
|| (!self.owned(¤t) && !tree.contains_key(¤t)))
{
return Err(libc::EXDEV);
}
if links > 0 && Self::matches(&self.deny, ¤t) {
return Err(libc::EACCES);
}
Ok(current)
}
unsafe fn stat_path(
&self,
path: *const c_char,
buf: *mut u8,
follow: bool,
) -> Option<FsResult> {
if let Some(inode) = unsafe { self.retained_directory(libc::AT_FDCWD, path) } {
let inode = match inode {
Ok(inode) => inode,
Err(error) => return err(error),
};
return with_times(
buf,
fill_stat(buf, self.device, &inode.attributes(0)),
&inode.times(),
false,
);
}
let path_absolute = match unsafe { self.absolute(path) } {
Ok(path) => path?,
Err(error) => return err(error),
};
let path = path_absolute.as_ptr();
let raw = path;
let trailing_slash =
!path.is_null() && unsafe { CStr::from_ptr(path) }.to_bytes().ends_with(b"/");
let path = unsafe { path_of(path) }?;
if Self::matches(&self.deny, &path) {
return err(libc::EACCES);
}
if Self::matches(&self.passthrough, &path) {
return None;
}
let tree = self.tree.lock().unwrap();
let path =
match unsafe { self.traversal(&tree, raw, &path, true, follow || trailing_slash) } {
Ok(path) => path,
Err(error) => return err(error),
};
match tree.get(&path) {
Some(node) => {
if trailing_slash && matches!(node, VNode::File(_)) {
return err(libc::ENOTDIR);
}
self.settle(node.inode());
let blocks = node.inode().data.lock().unwrap().pages.len() as u64 * 8;
let result = allocated_blocks(
buf,
fill_stat(buf, self.device, &node.attributes()),
blocks,
false,
);
with_times(buf, result, &node.inode().times(), false)
}
None if self.owned(&path) => err(libc::ENOENT),
None => None,
}
}
unsafe fn lookup(
&self,
dirfd: c_int,
path: *const c_char,
follow: bool,
) -> Option<Result<Arc<VInode>, c_int>> {
if path.is_null() {
return self.fd_inode(dirfd).map(Ok);
}
if let Some(inode) = unsafe { self.retained_directory(dirfd, path) } {
return Some(inode);
}
let resolved = match unsafe { self.resolve_at(dirfd, path) } {
Ok(path) => path?,
Err(error) => return Some(Err(error)),
};
let absolute = match unsafe { self.absolute(resolved.as_ptr()) } {
Ok(path) => path?,
Err(error) => return Some(Err(error)),
};
let raw = absolute.as_ptr();
let trailing_slash = absolute.to_bytes().ends_with(b"/");
let path = unsafe { path_of(raw) }?;
if Self::matches(&self.deny, &path) {
return Some(Err(libc::EACCES));
}
if Self::matches(&self.passthrough, &path) {
return None;
}
let tree = self.tree.lock().unwrap();
let path =
match unsafe { self.traversal(&tree, raw, &path, true, follow || trailing_slash) } {
Ok(path) => path,
Err(error) => return Some(Err(error)),
};
match tree.get(&path) {
Some(VNode::File(_)) if trailing_slash => Some(Err(libc::ENOTDIR)),
Some(node) => Some(Ok(node.inode().clone())),
None if self.owned(&path) => Some(Err(libc::ENOENT)),
None => None,
}
}
unsafe fn resolve_at(&self, fd: c_int, raw: *const c_char) -> Result<Option<CString>, c_int> {
if raw.is_null() {
return if self.owns(fd) {
Err(libc::EFAULT)
} else {
Ok(None)
};
}
let path = unsafe { CStr::from_ptr(raw) };
if path.to_bytes().starts_with(b"/") || fd == libc::AT_FDCWD {
return Ok(Some(path.to_owned()));
}
let inode = {
let open = self.open.lock().unwrap();
let Some(file) = open.get(&fd) else {
return Ok(None);
};
if !file.is_dir {
return Err(libc::ENOTDIR);
}
file.inode.clone()
};
if path.to_bytes().is_empty() {
return Err(libc::ENOENT);
}
let tree = self.tree.lock().unwrap();
let Some((directory, _)) = tree
.iter()
.find(|(_, node)| Arc::ptr_eq(node.inode(), &inode))
else {
return Err(libc::ENOENT);
};
let mut bytes = directory.as_os_str().as_encoded_bytes().to_vec();
bytes.push(b'/');
bytes.extend_from_slice(path.to_bytes());
Ok(Some(CString::new(bytes).unwrap()))
}
}
impl Drop for VirtualFs {
fn drop(&mut self) {
if self.devnull >= 0 {
snare_interpose::real(|| unsafe { libc::close(self.devnull) });
}
}
}
pub(crate) fn ok(n: i64) -> Option<FsResult> {
Some(FsResult::Ok(n))
}
pub(crate) fn err(errno: c_int) -> Option<FsResult> {
Some(FsResult::Err(errno))
}
pub(crate) fn normalize(raw: impl AsRef<Path>) -> PathBuf {
let mut out = PathBuf::from("/");
for component in raw.as_ref().components() {
match component {
Component::ParentDir => {
out.pop();
}
Component::Normal(c) => out.push(c),
_ => {}
}
}
out
}
pub(crate) unsafe fn path_of(path: *const c_char) -> Option<PathBuf> {
if path.is_null() {
return None;
}
let bytes = unsafe { CStr::from_ptr(path) }.to_bytes();
if bytes.first() != Some(&b'/') {
return None; }
Some(normalize(Path::new(OsStr::from_bytes(bytes))))
}
impl Fs for VirtualFs {
fn owns(&self, fd: c_int) -> bool {
self.open.lock().unwrap().contains_key(&fd) || self.dirs.lock().unwrap().contains_key(&fd)
}
fn cwd_path(&self) -> Option<Result<Vec<u8>, c_int>> {
self.cwd_bytes()
}
fn cwd_changed(&self) {
*self.cwd.lock().unwrap() = None;
}
unsafe fn chdir(&self, path: *const c_char) -> Option<FsResult> {
if let Some(inode) = unsafe { self.retained_directory(libc::AT_FDCWD, path) } {
return match inode {
Ok(_) => ok(0),
Err(error) => err(error),
};
}
let absolute = match unsafe { self.absolute(path) } {
Ok(path) => path?,
Err(error) => return err(error),
};
let path = unsafe { path_of(absolute.as_ptr()) }?;
if Self::matches(&self.passthrough, &path) {
return None;
}
if Self::matches(&self.deny, &path) {
return err(libc::EACCES);
}
let tree = self.tree.lock().unwrap();
if !self.claims(&tree, &path) {
return None;
}
let path = match unsafe { self.traversal(&tree, absolute.as_ptr(), &path, true, true) } {
Ok(path) => path,
Err(error) => return err(error),
};
let inode = match tree.get(&path) {
Some(VNode::Dir(inode)) => inode.clone(),
Some(_) => return err(libc::ENOTDIR),
None => return err(libc::ENOENT),
};
drop(tree);
if let Err(error) = Credentials::current().search(&inode.owner()) {
return err(error);
}
*self.cwd.lock().unwrap() = Some(inode);
ok(0)
}
unsafe fn fchdir(&self, fd: c_int) -> Option<FsResult> {
let inode = {
let open = self.open.lock().unwrap();
let file = open.get(&fd)?;
if !file.is_dir {
return err(libc::ENOTDIR);
}
file.inode.clone()
};
if let Err(error) = Credentials::current().search(&inode.owner()) {
return err(error);
}
*self.cwd.lock().unwrap() = Some(inode);
ok(0)
}
unsafe fn getcwd(&self, buf: *mut c_char, len: usize) -> Option<FsResult> {
let bytes = match self.cwd_bytes()? {
Ok(bytes) => bytes,
Err(error) => return err(error),
};
if !buf.is_null() && len == 0 {
return err(libc::EINVAL);
}
let len = if cfg!(target_os = "macos") && buf.is_null() {
0
} else {
len
};
if len != 0 && len <= bytes.len() {
return err(libc::ERANGE);
}
let out = if buf.is_null() {
let out = unsafe { libc::malloc(if len == 0 { bytes.len() + 1 } else { len }) }
.cast::<c_char>();
if out.is_null() {
return err(libc::ENOMEM);
}
out
} else {
buf
};
unsafe {
std::ptr::copy_nonoverlapping(bytes.as_ptr().cast::<c_char>(), out, bytes.len());
*out.add(bytes.len()) = 0;
}
ok(out as i64)
}
unsafe fn symlink(&self, target: *const c_char, link: *const c_char) -> Option<FsResult> {
let absolute = match unsafe { self.absolute(link) } {
Ok(path) => path?,
Err(error) => return err(error),
};
let path = unsafe { path_of(absolute.as_ptr()) }?;
if Self::matches(&self.passthrough, &path) {
return None;
}
if Self::matches(&self.deny, &path) {
return err(libc::EACCES);
}
let mut tree = self.tree.lock().unwrap();
if !self.claims(&tree, &path) {
return None;
}
if target.is_null() {
return err(libc::EFAULT);
}
let target = unsafe { CStr::from_ptr(target) }.to_bytes();
if target.is_empty() {
return err(libc::ENOENT);
}
let path = match unsafe { self.traversal(&tree, absolute.as_ptr(), &path, true, false) } {
Ok(path) => path,
Err(error) => return err(error),
};
if tree.contains_key(&path) {
return err(libc::EEXIST);
}
let parent = match Self::parent(&tree, &path) {
Ok(parent) => parent,
Err(error) => return err(error),
};
if absolute.to_bytes().ends_with(b"/") {
return err(libc::ENOENT);
}
let caller = Credentials::current();
let parent_owner = parent.owner();
if let Err(error) = caller.check(&parent_owner, Kind::Directory, fs_perm::WRITE) {
return err(error);
}
let now = self.now();
let inode = VInode::new(
self.next_inode.fetch_add(2, Ordering::Relaxed),
Vec::new(),
Kind::Symlink,
now,
caller.create(&parent_owner, 0o777, Kind::Symlink),
);
inode.data.lock().unwrap().attach(&self.volume);
tree.insert(path, VNode::Symlink(inode, Arc::new(target.to_vec())));
parent.touch(|times| times.modify(now));
ok(0)
}
unsafe fn symlinkat(
&self,
target: *const c_char,
fd: c_int,
link: *const c_char,
) -> Option<FsResult> {
let link = match unsafe { self.resolve_at(fd, link) } {
Ok(path) => path?,
Err(error) => return err(error),
};
unsafe { self.symlink(target, link.as_ptr()) }
}
unsafe fn access(&self, path: *const c_char, mode: c_int) -> Option<FsResult> {
unsafe { self.faccessat(libc::AT_FDCWD, path, mode, 0) }
}
unsafe fn faccessat(
&self,
dirfd: c_int,
path: *const c_char,
mode: c_int,
flags: c_int,
) -> Option<FsResult> {
let path = match unsafe { self.resolve_at(dirfd, path) } {
Ok(path) => path?,
Err(error) => return err(error),
};
let mut metadata: libc::stat = unsafe { std::mem::zeroed() };
let result = unsafe {
self.stat_path(
path.as_ptr(),
std::ptr::from_mut(&mut metadata).cast(),
flags & libc::AT_SYMLINK_NOFOLLOW == 0,
)
}?;
if mode & !(libc::R_OK | libc::W_OK | libc::X_OK) != 0
|| flags & !(libc::AT_EACCESS | libc::AT_SYMLINK_NOFOLLOW) != 0
{
return err(libc::EINVAL);
}
if matches!(result, FsResult::Ok(_)) {
#[allow(clippy::unnecessary_cast)]
let kind = match metadata.st_mode as u32 & libc::S_IFMT as u32 {
directory if directory == libc::S_IFDIR as u32 => Kind::Directory,
link if link == libc::S_IFLNK as u32 => Kind::Symlink,
_ => Kind::File,
};
let owner = Owner {
mode: metadata.st_mode as u32 & 0o7777,
uid: metadata.st_uid,
gid: metadata.st_gid,
};
if let Err(error) = Credentials::current().check(&owner, kind, mode as u32) {
return err(error);
}
}
Some(result)
}
unsafe fn readlink(&self, raw: *const c_char, buf: *mut u8, len: usize) -> Option<FsResult> {
let absolute = match unsafe { self.absolute(raw) } {
Ok(path) => path?,
Err(error) => return err(error),
};
let path = unsafe { path_of(absolute.as_ptr()) }?;
if Self::matches(&self.passthrough, &path) {
return None;
}
if Self::matches(&self.deny, &path) {
return err(libc::EACCES);
}
let tree = self.tree.lock().unwrap();
if !self.claims(&tree, &path) {
return None;
}
let path = match unsafe {
self.traversal(
&tree,
absolute.as_ptr(),
&path,
true,
absolute.to_bytes().ends_with(b"/"),
)
} {
Ok(path) => path,
Err(error) => return err(error),
};
let target = match tree.get(&path) {
Some(VNode::Symlink(_, target)) => target,
Some(_) => return err(libc::EINVAL),
None => return err(libc::ENOENT),
};
if len == 0 {
return err(libc::EINVAL);
}
if buf.is_null() {
return err(libc::EFAULT);
}
let count = len.min(target.len());
unsafe {
std::ptr::copy_nonoverlapping(target.as_ptr(), buf, count);
}
ok(count as i64)
}
unsafe fn readlinkat(
&self,
fd: c_int,
path: *const c_char,
buf: *mut u8,
len: usize,
) -> Option<FsResult> {
let path = match unsafe { self.resolve_at(fd, path) } {
Ok(path) => path?,
Err(error) => return err(error),
};
unsafe { self.readlink(path.as_ptr(), buf, len) }
}
unsafe fn open(&self, path: *const c_char, flags: c_int, mode: u32) -> Option<FsResult> {
if let Some(inode) = unsafe { self.retained_directory(libc::AT_FDCWD, path) } {
return match inode {
Ok(inode) => self.open_directory_inode(inode, flags),
Err(error) => err(error),
};
}
let path_absolute = match unsafe { self.absolute(path) } {
Ok(path) => path?,
Err(error) => return err(error),
};
let path = path_absolute.as_ptr();
let raw = path;
let trailing_slash =
!path.is_null() && unsafe { CStr::from_ptr(path) }.to_bytes().ends_with(b"/");
let path = unsafe { path_of(path) }?;
if Self::matches(&self.deny, &path) {
return err(libc::EACCES);
}
if Self::matches(&self.passthrough, &path) {
return None; }
let mut tree = self.tree.lock().unwrap();
#[cfg(target_os = "linux")]
if trailing_slash && flags & libc::O_CREAT != 0 && self.claims(&tree, &path) {
return err(libc::EISDIR);
}
let path = match unsafe {
self.traversal(
&tree,
raw,
&path,
true,
trailing_slash
|| (flags & libc::O_NOFOLLOW == 0
&& flags & (libc::O_CREAT | libc::O_EXCL)
!= (libc::O_CREAT | libc::O_EXCL)),
)
} {
Ok(path) => path,
Err(error) => return err(error),
};
let node = tree.get(&path).cloned();
#[cfg(target_os = "macos")]
if flags & libc::O_SYMLINK != 0 && self.claims(&tree, &path) {
return err(libc::EOPNOTSUPP);
}
#[cfg(target_os = "linux")]
if flags & (libc::O_PATH | libc::O_NOFOLLOW) == (libc::O_PATH | libc::O_NOFOLLOW)
&& matches!(node, Some(VNode::Symlink(..)))
{
return err(libc::EOPNOTSUPP);
}
if trailing_slash && matches!(node, Some(VNode::File(_))) {
return err(libc::ENOTDIR);
}
if trailing_slash && node.is_none() {
return if self.owned(&path) {
err(libc::ENOENT)
} else {
None
};
}
if node.is_some()
&& flags & (libc::O_CREAT | libc::O_EXCL) == (libc::O_CREAT | libc::O_EXCL)
{
return err(libc::EEXIST);
}
if matches!(node, Some(VNode::Dir(_))) && writable(flags) {
return err(libc::EISDIR);
}
if matches!(node, Some(VNode::File(_))) && flags & libc::O_DIRECTORY != 0 {
return err(libc::ENOTDIR);
}
#[cfg(target_os = "linux")]
if matches!(node, Some(VNode::Dir(_))) && flags & libc::O_CREAT != 0 {
return err(libc::EISDIR);
}
let creating = node.is_none();
let caller = Credentials::current();
let (data, is_dir, writable) = match node {
Some(VNode::Symlink(..)) => return err(libc::ELOOP),
Some(VNode::File(inode) | VNode::Dir(inode)) => {
if let Err(error) = caller.check(&inode.owner(), inode.kind, open_access(flags)) {
return err(error);
}
let is_dir = inode.kind == Kind::Directory;
(inode, is_dir, !is_dir && writable(flags))
}
None => {
if flags & libc::O_CREAT != 0
&& (self.owned(&path) || path.parent().is_some_and(|p| tree.contains_key(p)))
{
let parent = match Self::parent(&tree, &path) {
Ok(parent) => parent.owner(),
Err(error) => return err(error),
};
if let Err(error) = caller.check(&parent, Kind::Directory, fs_perm::WRITE) {
return err(error);
}
(
VInode::new(
self.next_inode.fetch_add(2, Ordering::Relaxed),
Vec::new(),
Kind::File,
self.now(),
caller.create(&parent, mode, Kind::File),
),
false,
writable(flags),
)
} else if self.owned(&path) {
return err(libc::ENOENT); } else {
return None; }
}
};
let fd = match self.reserve_fd() {
Ok(fd) => fd,
Err(e) => return err(e.raw_os_error().unwrap_or(libc::EMFILE)),
};
if creating {
data.data.lock().unwrap().attach(&self.volume);
tree.insert(path.clone(), VNode::File(data.clone()));
if let Ok(parent) = Self::parent(&tree, &path) {
parent.touch(|times| times.modify(data.times().born));
}
}
if !is_dir && flags & libc::O_TRUNC != 0 {
if let Err(error) = data.data.lock().unwrap().clear() {
unsafe { libc::close(fd) };
return err(error);
}
if !creating {
let now = self.now();
data.touch(|times| times.modify(now));
data.wrote(true);
}
}
let directory = is_dir.then(|| {
let mut stream = DirStream::new(snapshot(&tree, &path));
stream.inode = Some(data.clone());
Arc::new(Mutex::new(stream))
});
drop(tree);
self.open.lock().unwrap().insert(
fd,
OpenFile {
inode: data,
cursor: 0,
writable,
append: flags & libc::O_APPEND != 0,
flags: status_flags(flags),
is_dir,
directory,
description: fs_locks::description(),
},
);
set_cloexec(fd, flags);
ok(fd as i64)
}
unsafe fn openat(
&self,
dirfd: c_int,
path: *const c_char,
flags: c_int,
mode: u32,
) -> Option<FsResult> {
if let Some(inode) = unsafe { self.retained_directory(dirfd, path) } {
return match inode {
Ok(inode) => self.open_directory_inode(inode, flags),
Err(error) => err(error),
};
}
let path = match unsafe { self.resolve_at(dirfd, path) } {
Ok(path) => path?,
Err(error) => return err(error),
};
unsafe { self.open(path.as_ptr(), flags, mode) }
}
unsafe fn unlinkat(&self, fd: c_int, raw: *const c_char, flags: c_int) -> Option<FsResult> {
let path = match unsafe { self.resolve_at(fd, raw) } {
Ok(path) => path?,
Err(error) => return err(error),
};
if flags & !libc::AT_REMOVEDIR != 0 {
return err(libc::EINVAL);
}
if flags & libc::AT_REMOVEDIR != 0 {
unsafe { self.rmdir(path.as_ptr()) }
} else {
unsafe { self.unlink(path.as_ptr()) }
}
}
unsafe fn mkdirat(&self, fd: c_int, raw: *const c_char, mode: u32) -> Option<FsResult> {
let path = match unsafe { self.resolve_at(fd, raw) } {
Ok(path) => path?,
Err(error) => return err(error),
};
unsafe { self.mkdir(path.as_ptr(), mode) }
}
unsafe fn renameat(
&self,
fromfd: c_int,
from: *const c_char,
tofd: c_int,
to: *const c_char,
) -> Option<FsResult> {
let from = match unsafe { self.resolve_at(fromfd, from) } {
Ok(path) => path?,
Err(error) => return err(error),
};
let to = match unsafe { self.resolve_at(tofd, to) } {
Ok(path) => path?,
Err(error) => return err(error),
};
unsafe { self.rename(from.as_ptr(), to.as_ptr()) }
}
unsafe fn rmdir(&self, raw: *const c_char) -> Option<FsResult> {
let raw_absolute = match unsafe { self.absolute(raw) } {
Ok(path) => path?,
Err(error) => return err(error),
};
let raw = raw_absolute.as_ptr();
let path = unsafe { path_of(raw) }?;
if Self::matches(&self.deny, &path) {
return err(libc::EACCES);
}
if Self::matches(&self.passthrough, &path) {
return None;
}
let mut tree = self.tree.lock().unwrap();
if !self.claims(&tree, &path) {
return None;
}
let path = match unsafe { self.traversal(&tree, raw, &path, true, false) } {
Ok(path) => path,
Err(error) => return err(error),
};
let final_component = unsafe { CStr::from_ptr(raw) }
.to_bytes()
.rsplit(|b| *b == b'/')
.find(|c| !c.is_empty());
if final_component == Some(b".") {
return err(libc::EINVAL);
}
if path == Path::new("/") {
return err(libc::EBUSY);
}
let Some(node) = tree.get(&path) else {
return err(libc::ENOENT);
};
if let Err(error) = self.may_remove(&tree, &path, node.inode()) {
return err(error);
}
let inode = match node {
VNode::File(_) | VNode::Symlink(..) => return err(libc::ENOTDIR),
VNode::Dir(inode) => inode.clone(),
};
if tree
.keys()
.any(|entry| entry != &path && entry.starts_with(&path))
{
return err(libc::ENOTEMPTY);
}
tree.remove(&path);
if cfg!(target_os = "linux") {
inode.links.store(0, Ordering::Release);
}
if let Some(VNode::Dir(parent)) = path.parent().and_then(|parent| tree.get(parent)) {
parent.links.fetch_sub(1, Ordering::Release);
let now = self.now();
parent.touch(|times| times.modify(now));
}
ok(0)
}
unsafe fn unlink(&self, raw: *const c_char) -> Option<FsResult> {
let raw_absolute = match unsafe { self.absolute(raw) } {
Ok(path) => path?,
Err(error) => return err(error),
};
let raw = raw_absolute.as_ptr();
let trailing_slash =
!raw.is_null() && unsafe { CStr::from_ptr(raw) }.to_bytes().ends_with(b"/");
let path = unsafe { path_of(raw) }?;
if Self::matches(&self.deny, &path) {
return err(libc::EACCES);
}
if Self::matches(&self.passthrough, &path) {
return None;
}
let mut tree = self.tree.lock().unwrap();
if !self.claims(&tree, &path) {
return None;
}
let path = match unsafe { self.traversal(&tree, raw, &path, true, false) } {
Ok(path) => path,
Err(error) => return err(error),
};
match tree.get(&path) {
Some(VNode::Dir(_)) if cfg!(target_os = "macos") => return err(libc::EPERM),
Some(VNode::File(_) | VNode::Symlink(..)) if trailing_slash => {
return err(libc::ENOTDIR);
}
None => {
return err(Self::parent(&tree, &path).err().unwrap_or(libc::ENOENT));
}
Some(node) => {
if let Err(error) = self.may_remove(&tree, &path, node.inode()) {
return err(error);
}
if matches!(node, VNode::Dir(_)) {
return err(libc::EISDIR);
}
}
}
let inode = tree.remove(&path).unwrap();
inode.inode().links.fetch_sub(1, Ordering::AcqRel);
let now = self.now();
inode.inode().touch(|times| times.changed = now);
if let Ok(parent) = Self::parent(&tree, &path) {
parent.touch(|times| times.modify(now));
}
ok(0)
}
unsafe fn mkdir(&self, raw: *const c_char, mode: u32) -> Option<FsResult> {
let raw_absolute = match unsafe { self.absolute(raw) } {
Ok(path) => path?,
Err(error) => return err(error),
};
let raw = raw_absolute.as_ptr();
let trailing_slash =
!raw.is_null() && unsafe { CStr::from_ptr(raw) }.to_bytes().ends_with(b"/");
let path = unsafe { path_of(raw) }?;
if Self::matches(&self.deny, &path) {
return err(libc::EACCES);
}
if Self::matches(&self.passthrough, &path) {
return None;
}
let mut tree = self.tree.lock().unwrap();
if !self.claims(&tree, &path) {
return None;
}
let bytes = unsafe { CStr::from_ptr(raw) }.to_bytes();
let end = bytes
.iter()
.rposition(|byte| *byte != b'/')
.map_or(1, |last| last + 1);
let walk = CString::new(&bytes[..end]).unwrap();
let path = match unsafe {
self.traversal(
&tree,
walk.as_ptr(),
&path,
true,
cfg!(target_os = "macos") && trailing_slash,
)
} {
Ok(path) => path,
Err(error) => return err(error),
};
#[cfg(target_os = "macos")]
if trailing_slash && matches!(tree.get(&path), Some(VNode::File(_))) {
return err(libc::ENOTDIR);
}
if tree.contains_key(&path) {
return err(libc::EEXIST);
}
let parent = match Self::parent(&tree, &path) {
Ok(parent) => parent,
Err(error) => return err(error),
};
let caller = Credentials::current();
let parent_owner = parent.owner();
if let Err(error) = caller.check(&parent_owner, Kind::Directory, fs_perm::WRITE) {
return err(error);
}
let now = self.now();
let inode = VInode::new(
self.next_inode.fetch_add(2, Ordering::Relaxed),
Vec::new(),
Kind::Directory,
now,
caller.create(&parent_owner, mode, Kind::Directory),
);
inode.data.lock().unwrap().attach(&self.volume);
tree.insert(path, VNode::Dir(inode));
parent.links.fetch_add(1, Ordering::Release);
parent.touch(|times| times.modify(now));
ok(0)
}
unsafe fn rename(&self, from_raw: *const c_char, to_raw: *const c_char) -> Option<FsResult> {
let from_raw_absolute = match unsafe { self.absolute(from_raw) } {
Ok(path) => path?,
Err(error) => return err(error),
};
let from_raw = from_raw_absolute.as_ptr();
let to_raw_absolute = match unsafe { self.absolute(to_raw) } {
Ok(path) => path?,
Err(error) => return err(error),
};
let to_raw = to_raw_absolute.as_ptr();
let from_slash = !from_raw.is_null()
&& unsafe { CStr::from_ptr(from_raw) }
.to_bytes()
.ends_with(b"/");
let to_slash =
!to_raw.is_null() && unsafe { CStr::from_ptr(to_raw) }.to_bytes().ends_with(b"/");
let from = unsafe { path_of(from_raw) }?;
let to = unsafe { path_of(to_raw) }?;
if Self::matches(&self.deny, &from) || Self::matches(&self.deny, &to) {
return err(libc::EACCES);
}
if Self::matches(&self.passthrough, &from) || Self::matches(&self.passthrough, &to) {
return None;
}
let mut tree = self.tree.lock().unwrap();
let source_claimed = self.claims(&tree, &from);
let target_claimed = self.claims(&tree, &to);
if !source_claimed && !target_claimed {
return None;
}
if !source_claimed {
return err(libc::EXDEV);
}
let from = match unsafe { self.traversal(&tree, from_raw, &from, true, false) } {
Ok(path) => path,
Err(error) => return err(error),
};
let to = match unsafe { self.traversal(&tree, to_raw, &to, true, false) } {
Ok(path) => path,
Err(error) => return err(error),
};
if [from_raw, to_raw].into_iter().any(|raw| {
matches!(
unsafe { CStr::from_ptr(raw) }
.to_bytes()
.rsplit(|byte| *byte == b'/')
.find(|component| !component.is_empty()),
Some(b"." | b"..")
)
}) {
return err(if cfg!(target_os = "macos") {
libc::EINVAL
} else {
libc::EBUSY
});
}
let source = match tree.get(&from).cloned() {
Some(source) => source,
None => return err(Self::parent(&tree, &from).err().unwrap_or(libc::ENOENT)),
};
let directory = matches!(source, VNode::Dir(_));
if from_slash && !directory {
return err(libc::ENOTDIR);
}
if from == to {
return if to_slash && !directory {
err(libc::ENOTDIR)
} else {
ok(0)
};
}
if !target_claimed {
return err(libc::EXDEV);
}
let target_parent = match Self::parent(&tree, &to) {
Ok(parent) => parent,
Err(error) => return err(error),
};
let target = tree.get(&to).cloned();
if to_slash && !directory {
#[cfg(target_os = "linux")]
return err(libc::ENOTDIR);
#[cfg(target_os = "macos")]
match &target {
Some(VNode::File(_) | VNode::Symlink(..)) => return err(libc::ENOTDIR),
None => return err(libc::ENOENT),
Some(VNode::Dir(_)) => {}
}
}
if target
.as_ref()
.is_some_and(|target| Arc::ptr_eq(source.inode(), target.inode()))
{
return ok(0);
}
if directory && to.starts_with(&from) {
return err(libc::EINVAL);
}
if let Err(error) = self.may_rename(&tree, &from, &to, &source, target.as_ref()) {
return err(error);
}
match (&source, &target) {
(VNode::File(_) | VNode::Symlink(..), Some(VNode::Dir(_))) => return err(libc::EISDIR),
(VNode::Dir(_), Some(VNode::File(_) | VNode::Symlink(..))) => {
return err(libc::ENOTDIR);
}
(VNode::Dir(_), Some(VNode::Dir(_)))
if tree.keys().any(|path| path != &to && path.starts_with(&to)) =>
{
return err(libc::ENOTEMPTY);
}
_ => {}
}
let now = self.now();
if let Ok(parent) = Self::parent(&tree, &from) {
parent.touch(|times| times.modify(now));
}
target_parent.touch(|times| times.modify(now));
source.inode().touch(|times| times.changed = now);
if let Some(target) = tree.remove(&to) {
target.inode().touch(|times| times.changed = now);
if matches!(target, VNode::Dir(_)) {
#[cfg(target_os = "linux")]
target.inode().links.store(0, Ordering::Release);
} else {
target.inode().links.fetch_sub(1, Ordering::AcqRel);
}
}
let moving: Vec<_> = tree
.keys()
.filter(|path| *path == &from || (directory && path.starts_with(&from)))
.cloned()
.collect();
let moved: Vec<_> = moving
.into_iter()
.map(|path| {
let destination = if path == from {
to.clone()
} else {
to.join(path.strip_prefix(&from).unwrap())
};
(destination, tree.remove(&path).unwrap())
})
.collect();
tree.extend(moved);
if directory {
if let Some(VNode::Dir(parent)) = from.parent().and_then(|parent| tree.get(parent)) {
parent.links.fetch_sub(1, Ordering::Release);
}
if !matches!(target, Some(VNode::Dir(_))) {
target_parent.links.fetch_add(1, Ordering::Release);
}
}
ok(0)
}
unsafe fn link(&self, from: *const c_char, to: *const c_char) -> Option<FsResult> {
let from_absolute = match unsafe { self.absolute(from) } {
Ok(path) => path?,
Err(error) => return err(error),
};
let from = from_absolute.as_ptr();
let to_absolute = match unsafe { self.absolute(to) } {
Ok(path) => path?,
Err(error) => return err(error),
};
let to = to_absolute.as_ptr();
let source = unsafe { path_of(from) }?;
let target = unsafe { path_of(to) }?;
if Self::matches(&self.deny, &source) || Self::matches(&self.deny, &target) {
return err(libc::EACCES);
}
let mut tree = self.tree.lock().unwrap();
let source_claimed =
self.claims(&tree, &source) && !Self::matches(&self.passthrough, &source);
let target_claimed =
self.claims(&tree, &target) && !Self::matches(&self.passthrough, &target);
if !source_claimed && !target_claimed {
return None;
}
if !source_claimed {
return err(libc::EXDEV);
}
let source = match unsafe { self.traversal(&tree, from, &source, true, false) } {
Ok(path) => path,
Err(error) => return err(error),
};
let target = match unsafe { self.traversal(&tree, to, &target, true, false) } {
Ok(path) => path,
Err(error) => return err(error),
};
let node = match tree.get(&source) {
Some(node @ (VNode::File(_) | VNode::Symlink(..))) => node.clone(),
Some(VNode::Dir(_)) => return err(libc::EPERM),
None => return err(libc::ENOENT),
};
if unsafe { CStr::from_ptr(from) }.to_bytes().ends_with(b"/") {
return err(libc::ENOTDIR);
}
if !target_claimed {
return err(libc::EXDEV);
}
if unsafe { CStr::from_ptr(to) }.to_bytes().ends_with(b"/") {
return err(match tree.get(&target) {
#[cfg(target_os = "macos")]
Some(VNode::File(_) | VNode::Symlink(..)) => libc::ENOTDIR,
#[cfg(target_os = "linux")]
Some(VNode::File(_) | VNode::Symlink(..)) => libc::EEXIST,
Some(VNode::Dir(_)) => libc::EEXIST,
None => libc::ENOENT,
});
}
if tree.contains_key(&target) {
return err(libc::EEXIST);
}
let parent = match Self::parent(&tree, &target) {
Ok(parent) => parent,
Err(error) => return err(error),
};
if let Err(error) =
Credentials::current().check(&parent.owner(), Kind::Directory, fs_perm::WRITE)
{
return err(error);
}
node.inode().links.fetch_add(1, Ordering::AcqRel);
let now = self.now();
node.inode().touch(|times| times.changed = now);
parent.touch(|times| times.modify(now));
tree.insert(target, node);
ok(0)
}
unsafe fn linkat(
&self,
fromfd: c_int,
from: *const c_char,
tofd: c_int,
to: *const c_char,
flags: c_int,
) -> Option<FsResult> {
let from = match unsafe { self.resolve_at(fromfd, from) } {
Ok(path) => path?,
Err(error) => return err(error),
};
let to = match unsafe { self.resolve_at(tofd, to) } {
Ok(path) => path?,
Err(error) => return err(error),
};
if flags & !libc::AT_SYMLINK_FOLLOW != 0 {
return err(libc::EINVAL);
}
let from = if flags & libc::AT_SYMLINK_FOLLOW != 0 {
let absolute = match unsafe { self.absolute(from.as_ptr()) } {
Ok(path) => path?,
Err(error) => return err(error),
};
let path = unsafe { path_of(absolute.as_ptr()) }?;
let tree = self.tree.lock().unwrap();
let path = match unsafe { self.traversal(&tree, absolute.as_ptr(), &path, true, true) }
{
Ok(path) => path,
Err(error) => return err(error),
};
CString::new(path.as_os_str().as_bytes()).unwrap()
} else {
from
};
unsafe { self.link(from.as_ptr(), to.as_ptr()) }
}
unsafe fn stat(&self, path: *const c_char, buf: *mut u8) -> Option<FsResult> {
unsafe { self.stat_path(path, buf, true) }
}
unsafe fn lstat(&self, path: *const c_char, buf: *mut u8) -> Option<FsResult> {
unsafe { self.stat_path(path, buf, false) }
}
unsafe fn realpath(&self, path: *const c_char, resolved: *mut c_char) -> Option<FsResult> {
let path_absolute = match unsafe { self.absolute(path) } {
Ok(path) => path?,
Err(error) => return err(error),
};
let path = path_absolute.as_ptr();
let raw = path;
let path = unsafe { path_of(path) }?;
if Self::matches(&self.passthrough, &path) {
return None;
}
let tree = self.tree.lock().unwrap();
let path =
match unsafe { self.traversal(&tree, raw, &path, !cfg!(target_os = "macos"), true) } {
Ok(path) => path,
Err(error) => return err(error),
};
if !tree.contains_key(&path) {
return if self.owned(&path) {
err(libc::ENOENT)
} else {
None
};
}
drop(tree);
let bytes = path.as_os_str().as_encoded_bytes();
let out = if resolved.is_null() {
let buf = unsafe { libc::malloc(bytes.len() + 1) } as *mut c_char;
if buf.is_null() {
return err(libc::ENOMEM);
}
buf
} else {
resolved
};
unsafe {
std::ptr::copy_nonoverlapping(bytes.as_ptr().cast::<c_char>(), out, bytes.len());
*out.add(bytes.len()) = 0;
}
ok(out as i64)
}
unsafe fn fstatat(
&self,
dirfd: c_int,
path: *const c_char,
buf: *mut u8,
flags: c_int,
) -> Option<FsResult> {
#[cfg(target_os = "macos")]
let allowed = libc::AT_SYMLINK_NOFOLLOW;
#[cfg(target_os = "linux")]
let allowed = libc::AT_SYMLINK_NOFOLLOW | libc::AT_EMPTY_PATH | libc::AT_NO_AUTOMOUNT;
if flags & !allowed != 0 {
let claimed = self.owns(dirfd)
|| unsafe { path_of(path) }.is_some_and(|path| {
self.claims(&self.tree.lock().unwrap(), &path)
&& !Self::matches(&self.passthrough, &path)
});
return if claimed { err(libc::EINVAL) } else { None };
}
#[cfg(target_os = "linux")]
if flags & libc::AT_EMPTY_PATH != 0
&& (path.is_null() || unsafe { CStr::from_ptr(path) }.to_bytes().is_empty())
{
return unsafe { self.fstat(dirfd, buf) };
}
if let Some(inode) = unsafe { self.retained_directory(dirfd, path) } {
let inode = match inode {
Ok(inode) => inode,
Err(error) => return err(error),
};
return with_times(
buf,
fill_stat(buf, self.device, &inode.attributes(0)),
&inode.times(),
false,
);
}
let path = match unsafe { self.resolve_at(dirfd, path) } {
Ok(path) => path?,
Err(error) => return err(error),
};
unsafe { self.stat_path(path.as_ptr(), buf, flags & libc::AT_SYMLINK_NOFOLLOW == 0) }
}
unsafe fn fstat(&self, fd: c_int, buf: *mut u8) -> Option<FsResult> {
self.settle_fd(fd);
let (attributes, times) = self.fd_meta(fd)?;
let result = with_times(buf, fill_stat(buf, self.device, &attributes), ×, false);
if matches!(result, Some(FsResult::Ok(_))) {
let blocks = self.open.lock().unwrap().get(&fd).map_or(0, |file| {
file.inode.data.lock().unwrap().pages.len() as u64 * 8
});
unsafe { (*buf.cast::<libc::stat>()).st_blocks = blocks as _ };
}
result
}
unsafe fn statfs(&self, path: *const c_char, buf: *mut u8) -> Option<FsResult> {
let mut metadata: libc::stat = unsafe { std::mem::zeroed() };
match unsafe { self.stat(path, std::ptr::from_mut(&mut metadata).cast()) }? {
FsResult::Ok(_) => self.volume_stats(buf),
error => Some(error),
}
}
unsafe fn fstatfs(&self, fd: c_int, buf: *mut u8) -> Option<FsResult> {
if !self.owns(fd) {
return None;
}
self.volume_stats(buf)
}
#[cfg(target_os = "linux")]
unsafe fn statx(
&self,
dirfd: c_int,
path: *const c_char,
flags: c_int,
_mask: u32,
buf: *mut u8,
) -> Option<FsResult> {
let empty = path.is_null() || unsafe { path.read() } == 0;
if empty {
if flags & libc::AT_EMPTY_PATH != 0 {
self.settle_fd(dirfd);
}
if flags & libc::AT_EMPTY_PATH != 0
&& let Some((attributes, times)) = self.fd_meta(dirfd)
{
let blocks = self.open.lock().unwrap().get(&dirfd).map_or(0, |file| {
file.inode.data.lock().unwrap().pages.len() as u64 * 8
});
let result =
allocated_blocks(buf, fill_statx(buf, self.device, &attributes), blocks, true);
return with_times(buf, result, ×, true);
}
return None;
}
if let Some(inode) = unsafe { self.retained_directory(dirfd, path) } {
let inode = match inode {
Ok(inode) => inode,
Err(error) => return err(error),
};
return with_times(
buf,
fill_statx(buf, self.device, &inode.attributes(0)),
&inode.times(),
true,
);
}
let resolved = match unsafe { self.resolve_at(dirfd, path) } {
Ok(path) => path?,
Err(error) => return err(error),
};
let absolute = match unsafe { self.absolute(resolved.as_ptr()) } {
Ok(path) => path?,
Err(error) => return err(error),
};
let path = absolute.as_ptr();
let trailing_slash = unsafe { CStr::from_ptr(path) }.to_bytes().ends_with(b"/");
let raw = path;
let path = unsafe { path_of(path) }?;
if Self::matches(&self.deny, &path) {
return err(libc::EACCES);
}
if Self::matches(&self.passthrough, &path) {
return None;
}
let tree = self.tree.lock().unwrap();
let path = match unsafe {
self.traversal(
&tree,
raw,
&path,
true,
flags & libc::AT_SYMLINK_NOFOLLOW == 0 || trailing_slash,
)
} {
Ok(path) => path,
Err(error) => return err(error),
};
match tree.get(&path) {
Some(node) => {
if trailing_slash && matches!(node, VNode::File(_)) {
return err(libc::ENOTDIR);
}
self.settle(node.inode());
let blocks = node.inode().data.lock().unwrap().pages.len() as u64 * 8;
let result = allocated_blocks(
buf,
fill_statx(buf, self.device, &node.attributes()),
blocks,
true,
);
with_times(buf, result, &node.inode().times(), true)
}
None if self.owned(&path) => err(libc::ENOENT),
None => None,
}
}
unsafe fn set_times(
&self,
dirfd: c_int,
path: *const c_char,
times: &SetTimes,
flags: c_int,
) -> Option<FsResult> {
use snare_interpose::TimeSet;
let inode =
match unsafe { self.lookup(dirfd, path, flags & libc::AT_SYMLINK_NOFOLLOW == 0) }? {
Ok(inode) => inode,
Err(error) => return err(error),
};
if flags & !libc::AT_SYMLINK_NOFOLLOW != 0 {
return err(libc::EINVAL);
}
let set = [times.accessed, times.modified, times.created];
let explicit = set.iter().any(|time| matches!(time, TimeSet::At(_)));
let current = set.contains(&TimeSet::Now);
if let Err(error) =
Credentials::current().set_times(&inode.owner(), inode.kind, explicit, current)
{
return err(error);
}
let now = self.now();
inode.touch(|current| current.set(times, now));
ok(0)
}
unsafe fn chmod(
&self,
dirfd: c_int,
path: *const c_char,
mode: u32,
flags: c_int,
) -> Option<FsResult> {
let inode =
match unsafe { self.lookup(dirfd, path, flags & libc::AT_SYMLINK_NOFOLLOW == 0) }? {
Ok(inode) => inode,
Err(error) => return err(error),
};
if flags & !libc::AT_SYMLINK_NOFOLLOW != 0 {
return err(libc::EINVAL);
}
#[cfg(target_os = "linux")]
if path.is_null() && self.fd_flags(dirfd) & libc::O_PATH != 0 {
return err(libc::EBADF);
}
if cfg!(target_os = "linux") && inode.kind == Kind::Symlink {
return err(libc::EOPNOTSUPP);
}
let changed = {
let mut owner = inode.owner.lock().unwrap();
Credentials::current().chmod(&mut owner, mode)
};
if let Err(error) = changed {
return err(error);
}
let now = self.now();
inode.touch(|times| times.changed = now);
ok(0)
}
unsafe fn chown(
&self,
dirfd: c_int,
path: *const c_char,
uid: u32,
gid: u32,
flags: c_int,
) -> Option<FsResult> {
let inode =
match unsafe { self.lookup(dirfd, path, flags & libc::AT_SYMLINK_NOFOLLOW == 0) }? {
Ok(inode) => inode,
Err(error) => return err(error),
};
#[cfg(target_os = "linux")]
let allowed = libc::AT_SYMLINK_NOFOLLOW | libc::AT_EMPTY_PATH;
#[cfg(not(target_os = "linux"))]
let allowed = libc::AT_SYMLINK_NOFOLLOW;
if flags & !allowed != 0 {
return err(libc::EINVAL);
}
#[cfg(target_os = "linux")]
if path.is_null() && flags == 0 && self.fd_flags(dirfd) & libc::O_PATH != 0 {
return err(libc::EBADF);
}
let changed = {
let mut owner = inode.owner.lock().unwrap();
Credentials::current().chown(&mut owner, inode.kind, uid, gid)
};
match changed {
Ok(true) => {
let now = self.now();
inode.touch(|times| times.changed = now);
ok(0)
}
Ok(false) => ok(0),
Err(error) => err(error),
}
}
unsafe fn opendir(&self, path: *const c_char) -> Option<FsResult> {
let fd = match unsafe {
self.open(
path,
libc::O_RDONLY | libc::O_DIRECTORY | libc::O_CLOEXEC,
0,
)
}? {
FsResult::Ok(fd) => fd as c_int,
error => return Some(error),
};
unsafe { self.fdopendir(fd) }
}
unsafe fn fdopendir(&self, fd: c_int) -> Option<FsResult> {
let open = self.open.lock().unwrap();
let file = open.get(&fd)?;
let Some(stream) = &file.directory else {
return err(libc::ENOTDIR);
};
self.dirs.lock().unwrap().insert(fd, stream.clone());
set_cloexec(fd, libc::O_CLOEXEC);
ok(fd as i64)
}
unsafe fn readdir(&self, fd: c_int) -> Option<FsResult> {
let mut dirs = self.dirs.lock().unwrap();
let mut d = dirs.get_mut(&fd)?.lock().unwrap();
let Some(entry) = d.entries.get(d.cursor) else {
return ok(0); };
let (name, ino, dtype) = (entry.name.clone(), entry.ino, entry.dtype);
d.cursor += 1;
fill_dirent(&mut d.scratch, &name, ino, dtype);
self.list_access(&d);
let ptr = (&*d.scratch as *const Dirent) as i64;
ok(ptr)
}
unsafe fn readdir_r(
&self,
fd: c_int,
entry: *mut u8,
result: *mut *mut u8,
) -> Option<FsResult> {
let mut dirs = self.dirs.lock().unwrap();
let mut d = dirs.get_mut(&fd)?.lock().unwrap();
match d.entries.get(d.cursor) {
Some(e) => {
let (name, ino, dtype) = (e.name.clone(), e.ino, e.dtype);
d.cursor += 1;
self.list_access(&d);
fill_dirent(unsafe { &mut *entry.cast::<Dirent>() }, &name, ino, dtype);
unsafe { *result = entry };
}
None => {
unsafe { *result = std::ptr::null_mut() };
}
}
ok(0)
}
unsafe fn closedir(&self, fd: c_int) -> Option<FsResult> {
if !self.dirs.lock().unwrap().contains_key(&fd) {
return None;
}
unsafe { self.close(fd) }
}
unsafe fn getdents64(&self, fd: c_int, buf: *mut u8, len: usize) -> Option<FsResult> {
let open = self.open.lock().unwrap();
let file = open.get(&fd)?;
let Some(stream) = &file.directory else {
return err(libc::ENOTDIR);
};
let mut stream = stream.lock().unwrap();
self.list_access(&stream);
pack_directory(&mut stream, buf, len)
}
unsafe fn read(&self, fd: c_int, buf: *mut u8, len: usize) -> Option<FsResult> {
let mut open = self.open.lock().unwrap();
let file = open.get_mut(&fd)?;
if file.flags & libc::O_ACCMODE == libc::O_WRONLY {
return err(libc::EBADF);
}
if file.is_dir {
return err(libc::EISDIR);
}
let data = file.inode.data.lock().unwrap();
let n = match unsafe { data.read(file.cursor, buf, len) } {
Ok(n) => n,
Err(error) => return err(error),
};
file.cursor += n;
drop(data);
self.read_access(&file.inode, len, n);
ok(n as i64)
}
unsafe fn write(&self, fd: c_int, buf: *const u8, len: usize) -> Option<FsResult> {
let mut open = self.open.lock().unwrap();
let file = open.get_mut(&fd)?;
if !file.writable {
return err(libc::EBADF);
}
if len == 0 {
return ok(0);
}
let bytes = unsafe { std::slice::from_raw_parts(buf, len) };
let mut data = file.inode.data.lock().unwrap();
if file.append {
file.cursor = data.len();
}
match data.write(file.cursor, bytes) {
Ok(written) => {
file.cursor += written;
drop(data);
let now = self.now();
file.inode.touch(|times| times.modify(now));
file.inode.wrote(false);
ok(written as i64)
}
Err(error) => err(error),
}
}
unsafe fn pread(&self, fd: c_int, buf: *mut u8, len: usize, offset: i64) -> Option<FsResult> {
let open = self.open.lock().unwrap();
let file = open.get(&fd)?;
#[cfg(target_os = "macos")]
if file.flags & libc::O_ACCMODE == libc::O_WRONLY {
return err(libc::EBADF);
}
if offset < 0 {
return err(libc::EINVAL);
}
if file.flags & libc::O_ACCMODE == libc::O_WRONLY {
return err(libc::EBADF);
}
if file.is_dir {
return err(libc::EISDIR);
}
let data = file.inode.data.lock().unwrap();
let n = match unsafe { data.read(offset as usize, buf, len) } {
Ok(n) => n,
Err(error) => return err(error),
};
drop(data);
self.read_access(&file.inode, len, n);
ok(n as i64)
}
unsafe fn pwrite(
&self,
fd: c_int,
buf: *const u8,
len: usize,
offset: i64,
) -> Option<FsResult> {
let mut open = self.open.lock().unwrap();
let file = open.get_mut(&fd)?;
if offset < 0 {
return err(libc::EINVAL);
}
if !file.writable {
return err(libc::EBADF);
}
if len == 0 {
return ok(0);
}
let mut data = file.inode.data.lock().unwrap();
let start = if cfg!(target_os = "linux") && file.append {
data.len()
} else {
offset as usize
};
let bytes = unsafe { std::slice::from_raw_parts(buf, len) };
match data.write(start, bytes) {
Ok(written) => {
drop(data);
let now = self.now();
file.inode.touch(|times| times.modify(now));
file.inode.wrote(false);
ok(written as i64)
}
Err(error) => err(error),
}
}
unsafe fn ftruncate(&self, fd: c_int, len: i64) -> Option<FsResult> {
let mut open = self.open.lock().unwrap();
let file = open.get_mut(&fd)?;
if !file.writable {
return err(libc::EINVAL);
}
if len < 0 {
return err(libc::EINVAL);
}
let len = len as usize;
if let Err(error) = file.inode.data.lock().unwrap().resize(len) {
return err(error);
}
let now = self.now();
file.inode.touch(|times| times.modify(now));
file.inode.wrote(true);
ok(0)
}
unsafe fn fsync(&self, fd: c_int) -> Option<FsResult> {
self.open.lock().unwrap().get(&fd)?;
ok(0)
}
unsafe fn flock(&self, fd: c_int, operation: c_int) -> Option<FsResult> {
let Some((inode, target)) = self.lock_target(fd) else {
return self.stream_lock(fd);
};
inode.locks.flock(target.description, operation)
}
unsafe fn lseek(&self, fd: c_int, offset: i64, whence: c_int) -> Option<FsResult> {
let mut open = self.open.lock().unwrap();
let file = open.get_mut(&fd)?;
if let Some(stream) = &file.directory {
let mut stream = stream.lock().unwrap();
let base = match whence {
libc::SEEK_SET => 0,
libc::SEEK_CUR => stream.cursor as i64,
_ => return err(libc::EINVAL),
};
let Some(target) = base.checked_add(offset).filter(|target| *target >= 0) else {
return err(libc::EINVAL);
};
stream.cursor = target as usize;
return ok(target);
}
let base = match whence {
libc::SEEK_SET => 0,
libc::SEEK_CUR => file.cursor as i64,
libc::SEEK_END => file.inode.data.lock().unwrap().len() as i64,
_ => return err(libc::EINVAL),
};
let Some(target) = base.checked_add(offset) else {
return err(libc::EINVAL);
};
if target < 0 {
return err(libc::EINVAL);
}
file.cursor = target as usize;
ok(target)
}
unsafe fn fd_replaced(&self, fd: c_int) -> Option<FsResult> {
let mut open = self.open.lock().unwrap();
let inode = open.get(&fd).map(|file| file.inode.clone());
let file = open.remove(&fd);
let stream = self.dirs.lock().unwrap().remove(&fd);
drop(open);
if file.is_none() && stream.is_none() {
return None;
}
drop(file);
if let Some(inode) = inode {
inode.locks.release_process();
}
ok(0)
}
unsafe fn close(&self, fd: c_int) -> Option<FsResult> {
unsafe { self.fd_replaced(fd) }?;
let ret = unsafe { libc::close(fd) };
ok(ret as i64)
}
unsafe fn dup(&self, oldfd: c_int, newfd: c_int) -> Option<FsResult> {
let mut open = self.open.lock().unwrap();
let mut dirs = self.dirs.lock().unwrap();
let replaced = (oldfd != newfd)
.then(|| open.get(&newfd).map(|file| file.inode.clone()))
.flatten();
let released = if open.contains_key(&oldfd) {
dirs.remove(&newfd);
open.alias(oldfd, newfd)?
} else {
if !dirs.contains_key(&oldfd) {
return None;
}
let released = open.remove(&newfd).flatten();
dirs.alias(oldfd, newfd)?;
released
};
drop(dirs);
drop(open);
drop(released);
if let Some(inode) = replaced {
inode.locks.release_process();
}
ok(newfd as i64)
}
unsafe fn dup_to(&self, oldfd: c_int, newfd: c_int, flags: Option<c_int>) -> Option<FsResult> {
let mut open = self.open.lock().unwrap();
let mut dirs = self.dirs.lock().unwrap();
let is_file = open.contains_key(&oldfd);
if !is_file && !dirs.contains_key(&oldfd) {
return None;
}
let result = crate::fabric::duplicate_to(oldfd, newfd, flags);
if result < 0 {
return err(std::io::Error::last_os_error()
.raw_os_error()
.unwrap_or(libc::EBADF));
}
let replaced = (oldfd != newfd)
.then(|| open.get(&newfd).map(|file| file.inode.clone()))
.flatten();
let released = if is_file {
dirs.remove(&newfd);
open.alias(oldfd, newfd).flatten()
} else {
let released = open.remove(&newfd).flatten();
dirs.alias(oldfd, newfd);
released
};
drop(dirs);
drop(open);
drop(released);
if let Some(inode) = replaced {
inode.locks.release_process();
}
ok(result as i64)
}
unsafe fn fcntl(&self, fd: c_int, cmd: c_int, arg: i64) -> Option<FsResult> {
if fs_locks::is_record_lock(cmd) {
let Some((inode, target)) = self.lock_target(fd) else {
return self.stream_lock(fd);
};
return unsafe { inode.locks.fcntl(cmd, arg, &target) };
}
let mut open = self.open.lock().unwrap();
let mut dirs = self.dirs.lock().unwrap();
let is_file = open.contains_key(&fd);
if !is_file && !dirs.contains_key(&fd) {
return None;
}
match cmd {
#[cfg(target_os = "macos")]
libc::F_FULLFSYNC | libc::F_BARRIERFSYNC if is_file => ok(0),
libc::F_DUPFD | libc::F_DUPFD_CLOEXEC => {
let result = descriptor_fcntl(fd, cmd, arg)?;
if let FsResult::Ok(newfd) = result {
if is_file {
open.alias(fd, newfd as c_int);
} else {
dirs.alias(fd, newfd as c_int);
}
}
Some(result)
}
libc::F_GETFD | libc::F_SETFD => descriptor_fcntl(fd, cmd, arg),
libc::F_GETFL => ok(open.get(&fd).map_or(libc::O_RDONLY, |file| file.flags) as i64),
libc::F_SETFL => {
if let Some(file) = open.get_mut(&fd) {
let mask = libc::O_APPEND | libc::O_NONBLOCK;
file.flags = (file.flags & !mask) | (arg as c_int & mask);
file.append = file.flags & libc::O_APPEND != 0;
}
ok(0)
}
_ => err(libc::EINVAL),
}
}
unsafe fn mmap(
&self,
addr: *mut u8,
len: usize,
prot: c_int,
flags: c_int,
fd: c_int,
offset: i64,
) -> Option<FsResult> {
let map = |real: c_int| {
let address = unsafe { libc::mmap(addr.cast(), len, prot, flags, real, offset as _) };
if address == libc::MAP_FAILED {
err(last_error())
} else {
ok(address as i64)
}
};
let file = self
.open
.lock()
.unwrap()
.get(&fd)
.map(|file| (file.inode.clone(), file.flags, file.is_dir));
let Some((inode, mode, is_dir)) = file else {
return if self.dirs.lock().unwrap().contains_key(&fd) {
map(fs_mmap::directory())
} else {
None
};
};
if is_dir {
return map(fs_mmap::directory());
}
let (real, first) = {
let mut data = inode.data.lock().unwrap();
let first = data.backing.is_none();
match data.share() {
Ok(backing) => (backing.fd(mode), first),
Err(error) => return err(error),
}
};
if first {
self.mapped.lock().unwrap().push(Arc::downgrade(&inode));
}
let result = map(real);
if let Some(FsResult::Ok(_)) = result {
if flags & libc::MAP_SHARED != 0 && mode & libc::O_ACCMODE == libc::O_RDWR {
let mut data = inode.data.lock().unwrap();
if let Some(backing) = &mut data.backing {
backing.expose(offset as u64, len);
}
}
let now = self.now();
inode.touch(|times| times.access(now));
}
result
}
}
fn open_access(flags: c_int) -> u32 {
#[cfg(target_os = "linux")]
if flags & libc::O_PATH != 0 {
return 0;
}
let mut want = match flags & libc::O_ACCMODE {
libc::O_RDONLY => fs_perm::READ,
libc::O_WRONLY => fs_perm::WRITE,
_ => fs_perm::READ | fs_perm::WRITE,
};
if flags & libc::O_TRUNC != 0 {
want |= fs_perm::WRITE;
}
want
}
fn writable(flags: c_int) -> bool {
let mode = flags & libc::O_ACCMODE;
mode == libc::O_WRONLY || mode == libc::O_RDWR
}
pub(crate) fn pack_directory(stream: &mut DirStream, buf: *mut u8, len: usize) -> Option<FsResult> {
let mut written = 0;
while let Some(entry) = stream.entries.get(stream.cursor) {
let record: Dirent = unsafe { std::mem::zeroed() };
if entry.name.len() >= record.d_name.len() {
return if written == 0 {
err(libc::ENAMETOOLONG)
} else {
ok(written as i64)
};
}
let header = std::mem::offset_of!(Dirent, d_name);
#[cfg(target_os = "linux")]
let alignment = 8;
#[cfg(target_os = "macos")]
let alignment = 4;
let record_len = (header + entry.name.len() + 1).next_multiple_of(alignment);
if record_len > len.saturating_sub(written) {
return if written == 0 {
err(libc::EINVAL)
} else {
ok(written as i64)
};
}
if buf.is_null() {
return if written == 0 {
err(libc::EFAULT)
} else {
ok(written as i64)
};
}
unsafe {
let out = buf.add(written).cast::<Dirent>();
out.cast::<u8>().write_bytes(0, record_len);
std::ptr::addr_of_mut!((*out).d_ino).write_unaligned(entry.ino as _);
std::ptr::addr_of_mut!((*out).d_reclen).write_unaligned(record_len as _);
std::ptr::addr_of_mut!((*out).d_type).write_unaligned(entry.dtype);
#[cfg(target_os = "linux")]
std::ptr::addr_of_mut!((*out).d_off).write_unaligned((stream.cursor + 1) as _);
#[cfg(target_os = "macos")]
{
std::ptr::addr_of_mut!((*out).d_seekoff).write_unaligned((stream.cursor + 1) as _);
std::ptr::addr_of_mut!((*out).d_namlen).write_unaligned(entry.name.len() as _);
}
std::ptr::copy_nonoverlapping(
entry.name.as_ptr(),
buf.add(written + header),
entry.name.len(),
);
}
stream.cursor += 1;
written += record_len;
}
ok(written as i64)
}
fn allocated_blocks(
buf: *mut u8,
result: Option<FsResult>,
blocks: u64,
statx: bool,
) -> Option<FsResult> {
if matches!(result, Some(FsResult::Ok(_))) {
unsafe {
if statx {
buf.add(48).cast::<u64>().write_unaligned(blocks);
} else {
(*buf.cast::<libc::stat>()).st_blocks = blocks as _;
}
}
}
result
}
pub(crate) fn with_times(
buf: *mut u8,
result: Option<FsResult>,
times: &Times,
statx: bool,
) -> Option<FsResult> {
if matches!(result, Some(FsResult::Ok(_))) {
if statx {
#[cfg(target_os = "linux")]
unsafe {
statx_times(buf, times)
};
} else {
unsafe { stat_times(&mut *buf.cast::<libc::stat>(), times) };
}
}
result
}
fn split(stamp: Stamp) -> (i64, i64) {
(
stamp.div_euclid(1_000_000_000) as i64,
stamp.rem_euclid(1_000_000_000) as i64,
)
}
fn join(secs: i64, nanos: i64) -> Stamp {
Stamp::from(secs) * 1_000_000_000 + Stamp::from(nanos)
}
fn stat_times(st: &mut libc::stat, times: &Times) {
(st.st_atime, st.st_atime_nsec) = split(times.accessed);
(st.st_mtime, st.st_mtime_nsec) = split(times.modified);
(st.st_ctime, st.st_ctime_nsec) = split(times.changed);
#[cfg(target_os = "macos")]
{
(st.st_birthtime, st.st_birthtime_nsec) = split(times.born);
}
}
fn stat_times_of(st: &libc::stat) -> Times {
Times {
accessed: join(st.st_atime, st.st_atime_nsec),
modified: join(st.st_mtime, st.st_mtime_nsec),
changed: join(st.st_ctime, st.st_ctime_nsec),
#[cfg(target_os = "macos")]
born: join(st.st_birthtime, st.st_birthtime_nsec),
#[cfg(not(target_os = "macos"))]
born: 0,
}
}
#[cfg(target_os = "linux")]
const STATX_TIMES: [(usize, u32); 4] = [
(64, libc::STATX_ATIME),
(112, libc::STATX_MTIME),
(96, libc::STATX_CTIME),
(80, libc::STATX_BTIME),
];
#[cfg(target_os = "linux")]
unsafe fn statx_times(buf: *mut u8, times: &Times) {
let values = [times.accessed, times.modified, times.changed, times.born];
unsafe {
let mut mask = buf.cast::<u32>().read_unaligned();
for ((offset, bit), value) in STATX_TIMES.into_iter().zip(values) {
let (secs, nanos) = split(value);
buf.add(offset).cast::<i64>().write_unaligned(secs);
buf.add(offset + 8)
.cast::<u32>()
.write_unaligned(nanos as u32);
mask |= bit;
}
buf.cast::<u32>().write_unaligned(mask);
}
}
#[cfg(target_os = "linux")]
unsafe fn statx_times_of(buf: *const u8) -> [Option<Stamp>; 4] {
unsafe {
let mask = buf.cast::<u32>().read_unaligned();
STATX_TIMES.map(|(offset, bit)| {
(mask & bit != 0).then(|| {
join(
buf.add(offset).cast::<i64>().read_unaligned(),
i64::from(buf.add(offset + 8).cast::<u32>().read_unaligned()),
)
})
})
}
}
pub(crate) fn fill_stat(buf: *mut u8, device: u64, node: &Attributes) -> Option<FsResult> {
if buf.is_null() {
return err(libc::EFAULT);
}
let mut st: libc::stat = unsafe { core::mem::zeroed() };
st.st_dev = device as _;
st.st_mode = node.mode as _;
st.st_size = node.size as _;
st.st_ino = node.ino as _;
st.st_nlink = node.nlink as _;
st.st_uid = node.uid;
st.st_gid = node.gid;
st.st_blksize = 4096 as _;
st.st_blocks = node.size.div_ceil(512) as _;
unsafe { buf.cast::<libc::stat>().write(st) };
ok(0)
}
fn device() -> u64 {
static NEXT: std::sync::atomic::AtomicU32 = std::sync::atomic::AtomicU32::new(0x8000);
libc::makedev(0, NEXT.fetch_add(1, Ordering::Relaxed) as _) as u64
}
pub(crate) fn ino_of(path: &Path) -> u64 {
use std::hash::{Hash, Hasher};
let mut hasher = std::collections::hash_map::DefaultHasher::new();
path.hash(&mut hasher);
hasher.finish() | 1
}
fn snapshot(tree: &BTreeMap<PathBuf, VNode>, dir: &Path) -> Vec<DirEntry> {
let mut entries = vec![
DirEntry {
name: b".".to_vec(),
ino: tree
.get(dir)
.map_or_else(|| ino_of(dir), |node| node.inode().number),
dtype: libc::DT_DIR,
},
DirEntry {
name: b"..".to_vec(),
ino: dir
.parent()
.and_then(|parent| tree.get(parent))
.or_else(|| tree.get(dir))
.map_or_else(|| ino_of(dir), |node| node.inode().number),
dtype: libc::DT_DIR,
},
];
for (path, node) in tree.iter() {
if path.parent() == Some(dir)
&& let Some(name) = path.file_name()
{
let dtype = match node {
VNode::Dir(_) => libc::DT_DIR,
VNode::File(_) => libc::DT_REG,
VNode::Symlink(..) => libc::DT_LNK,
};
entries.push(DirEntry {
name: name.as_encoded_bytes().to_vec(),
ino: node.inode().number,
dtype,
});
}
}
entries
}
pub(crate) fn fill_dirent(dst: &mut Dirent, name: &[u8], ino: u64, dtype: u8) {
dst.d_ino = ino as _;
dst.d_reclen = core::mem::size_of::<Dirent>() as u16;
dst.d_type = dtype;
let n = name.len().min(dst.d_name.len() - 1);
for slot in dst.d_name.iter_mut() {
*slot = 0;
}
for (i, &b) in name.iter().take(n).enumerate() {
dst.d_name[i] = b as _;
}
#[cfg(target_os = "linux")]
{
dst.d_off = 0;
}
#[cfg(not(target_os = "linux"))]
{
dst.d_seekoff = 0;
dst.d_namlen = n as u16;
}
}
#[cfg(target_os = "linux")]
pub(crate) fn fill_statx(buf: *mut u8, device: u64, node: &Attributes) -> Option<FsResult> {
let &Attributes {
mode,
size,
ino,
nlink,
uid,
gid,
} = node;
if buf.is_null() {
return err(libc::EFAULT);
}
unsafe {
std::ptr::write_bytes(buf, 0, 256);
let put_u16 = |off: usize, v: u16| buf.add(off).cast::<u16>().write_unaligned(v);
let put_u32 = |off: usize, v: u32| buf.add(off).cast::<u32>().write_unaligned(v);
let put_u64 = |off: usize, v: u64| buf.add(off).cast::<u64>().write_unaligned(v);
put_u32(
0,
libc::STATX_TYPE
| libc::STATX_MODE
| libc::STATX_SIZE
| libc::STATX_INO
| libc::STATX_NLINK
| libc::STATX_UID
| libc::STATX_GID
| libc::STATX_BLOCKS,
);
put_u32(4, 4096); put_u32(16, nlink as u32); put_u32(20, uid); put_u32(24, gid); put_u16(28, mode as u16); put_u64(32, ino); put_u64(40, size); put_u64(48, size.div_ceil(512)); put_u32(136, libc::major(device as libc::dev_t)); put_u32(140, libc::minor(device as libc::dev_t)); }
ok(0)
}
pub(crate) struct FsChain(pub(crate) Vec<Arc<dyn Fs>>);
macro_rules! first {
($self:ident, |$fs:ident| $call:expr) => {
$self.0.iter().find_map(|$fs| unsafe { $call })
};
}
macro_rules! owner {
($self:ident, $fd:expr, |$fs:ident| $call:expr) => {
$self
.0
.iter()
.filter(|$fs| $fs.owns($fd))
.find_map(|$fs| unsafe { $call })
};
}
impl Fs for FsChain {
fn owns(&self, fd: c_int) -> bool {
self.0.iter().any(|fs| fs.owns(fd))
}
fn cwd_path(&self) -> Option<Result<Vec<u8>, c_int>> {
self.0.iter().find_map(|fs| fs.cwd_path())
}
fn cwd_changed(&self) {
for fs in &self.0 {
fs.cwd_changed();
}
}
unsafe fn open(&self, path: *const c_char, flags: c_int, mode: u32) -> Option<FsResult> {
first!(self, |fs| fs.open(path, flags, mode))
}
unsafe fn openat(
&self,
dirfd: c_int,
path: *const c_char,
flags: c_int,
mode: u32,
) -> Option<FsResult> {
first!(self, |fs| fs.openat(dirfd, path, flags, mode))
}
unsafe fn stat(&self, path: *const c_char, buf: *mut u8) -> Option<FsResult> {
first!(self, |fs| fs.stat(path, buf))
}
unsafe fn lstat(&self, path: *const c_char, buf: *mut u8) -> Option<FsResult> {
first!(self, |fs| fs.lstat(path, buf))
}
unsafe fn fstatat(
&self,
dirfd: c_int,
path: *const c_char,
buf: *mut u8,
flags: c_int,
) -> Option<FsResult> {
first!(self, |fs| fs.fstatat(dirfd, path, buf, flags))
}
unsafe fn statx(
&self,
dirfd: c_int,
path: *const c_char,
flags: c_int,
mask: u32,
buf: *mut u8,
) -> Option<FsResult> {
first!(self, |fs| fs.statx(dirfd, path, flags, mask, buf))
}
unsafe fn access(&self, path: *const c_char, mode: c_int) -> Option<FsResult> {
first!(self, |fs| fs.access(path, mode))
}
unsafe fn faccessat(
&self,
dirfd: c_int,
path: *const c_char,
mode: c_int,
flags: c_int,
) -> Option<FsResult> {
first!(self, |fs| fs.faccessat(dirfd, path, mode, flags))
}
unsafe fn readlink(&self, path: *const c_char, buf: *mut u8, len: usize) -> Option<FsResult> {
first!(self, |fs| fs.readlink(path, buf, len))
}
unsafe fn readlinkat(
&self,
dirfd: c_int,
path: *const c_char,
buf: *mut u8,
len: usize,
) -> Option<FsResult> {
first!(self, |fs| fs.readlinkat(dirfd, path, buf, len))
}
unsafe fn symlink(&self, target: *const c_char, link: *const c_char) -> Option<FsResult> {
first!(self, |fs| fs.symlink(target, link))
}
unsafe fn symlinkat(
&self,
target: *const c_char,
dirfd: c_int,
link: *const c_char,
) -> Option<FsResult> {
first!(self, |fs| fs.symlinkat(target, dirfd, link))
}
unsafe fn chdir(&self, path: *const c_char) -> Option<FsResult> {
for (index, fs) in self.0.iter().enumerate() {
if let Some(result) = unsafe { fs.chdir(path) } {
if matches!(result, FsResult::Ok(_)) {
for other in self
.0
.iter()
.enumerate()
.filter(|(other, _)| *other != index)
{
other.1.cwd_changed();
}
}
return Some(result);
}
}
None
}
unsafe fn fchdir(&self, fd: c_int) -> Option<FsResult> {
for (index, fs) in self.0.iter().enumerate() {
if let Some(result) = unsafe { fs.fchdir(fd) } {
if matches!(result, FsResult::Ok(_)) {
for other in self
.0
.iter()
.enumerate()
.filter(|(other, _)| *other != index)
{
other.1.cwd_changed();
}
}
return Some(result);
}
}
None
}
unsafe fn getcwd(&self, buf: *mut c_char, len: usize) -> Option<FsResult> {
first!(self, |fs| fs.getcwd(buf, len))
}
unsafe fn unlink(&self, path: *const c_char) -> Option<FsResult> {
first!(self, |fs| fs.unlink(path))
}
unsafe fn unlinkat(&self, fd: c_int, path: *const c_char, flags: c_int) -> Option<FsResult> {
first!(self, |fs| fs.unlinkat(fd, path, flags))
}
unsafe fn rmdir(&self, path: *const c_char) -> Option<FsResult> {
first!(self, |fs| fs.rmdir(path))
}
unsafe fn mkdir(&self, path: *const c_char, mode: u32) -> Option<FsResult> {
first!(self, |fs| fs.mkdir(path, mode))
}
unsafe fn mkdirat(&self, fd: c_int, path: *const c_char, mode: u32) -> Option<FsResult> {
first!(self, |fs| fs.mkdirat(fd, path, mode))
}
unsafe fn rename(&self, from: *const c_char, to: *const c_char) -> Option<FsResult> {
first!(self, |fs| fs.rename(from, to))
}
unsafe fn renameat(
&self,
fromfd: c_int,
from: *const c_char,
tofd: c_int,
to: *const c_char,
) -> Option<FsResult> {
first!(self, |fs| fs.renameat(fromfd, from, tofd, to))
}
unsafe fn realpath(&self, path: *const c_char, resolved: *mut c_char) -> Option<FsResult> {
first!(self, |fs| fs.realpath(path, resolved))
}
unsafe fn link(&self, from: *const c_char, to: *const c_char) -> Option<FsResult> {
first!(self, |fs| fs.link(from, to))
}
unsafe fn linkat(
&self,
fromfd: c_int,
from: *const c_char,
tofd: c_int,
to: *const c_char,
flags: c_int,
) -> Option<FsResult> {
first!(self, |fs| fs.linkat(fromfd, from, tofd, to, flags))
}
unsafe fn statfs(&self, path: *const c_char, buf: *mut u8) -> Option<FsResult> {
first!(self, |fs| fs.statfs(path, buf))
}
unsafe fn fstatfs(&self, fd: c_int, buf: *mut u8) -> Option<FsResult> {
owner!(self, fd, |fs| fs.fstatfs(fd, buf))
}
unsafe fn fstat(&self, fd: c_int, buf: *mut u8) -> Option<FsResult> {
owner!(self, fd, |fs| fs.fstat(fd, buf))
}
unsafe fn lseek(&self, fd: c_int, offset: i64, whence: c_int) -> Option<FsResult> {
owner!(self, fd, |fs| fs.lseek(fd, offset, whence))
}
unsafe fn pread(&self, fd: c_int, buf: *mut u8, len: usize, offset: i64) -> Option<FsResult> {
owner!(self, fd, |fs| fs.pread(fd, buf, len, offset))
}
unsafe fn pwrite(
&self,
fd: c_int,
buf: *const u8,
len: usize,
offset: i64,
) -> Option<FsResult> {
owner!(self, fd, |fs| fs.pwrite(fd, buf, len, offset))
}
unsafe fn ftruncate(&self, fd: c_int, len: i64) -> Option<FsResult> {
owner!(self, fd, |fs| fs.ftruncate(fd, len))
}
unsafe fn fsync(&self, fd: c_int) -> Option<FsResult> {
owner!(self, fd, |fs| fs.fsync(fd))
}
unsafe fn set_times(
&self,
dirfd: c_int,
path: *const c_char,
times: &SetTimes,
flags: c_int,
) -> Option<FsResult> {
if path.is_null() {
owner!(self, dirfd, |fs| fs.set_times(dirfd, path, times, flags))
} else {
first!(self, |fs| fs.set_times(dirfd, path, times, flags))
}
}
unsafe fn chmod(
&self,
dirfd: c_int,
path: *const c_char,
mode: u32,
flags: c_int,
) -> Option<FsResult> {
if path.is_null() {
owner!(self, dirfd, |fs| fs.chmod(dirfd, path, mode, flags))
} else {
first!(self, |fs| fs.chmod(dirfd, path, mode, flags))
}
}
unsafe fn chown(
&self,
dirfd: c_int,
path: *const c_char,
uid: u32,
gid: u32,
flags: c_int,
) -> Option<FsResult> {
if path.is_null() {
owner!(self, dirfd, |fs| fs.chown(dirfd, path, uid, gid, flags))
} else {
first!(self, |fs| fs.chown(dirfd, path, uid, gid, flags))
}
}
unsafe fn host_status_changed(&self, dirfd: c_int, path: *const c_char, flags: c_int) {
for fs in &self.0 {
unsafe { fs.host_status_changed(dirfd, path, flags) };
}
}
unsafe fn host_times_set(
&self,
dirfd: c_int,
path: *const c_char,
times: &SetTimes,
flags: c_int,
) {
for fs in &self.0 {
unsafe { fs.host_times_set(dirfd, path, times, flags) };
}
}
unsafe fn host_metadata(&self, buf: *mut u8, statx: bool) {
for fs in &self.0 {
unsafe { fs.host_metadata(buf, statx) };
}
}
unsafe fn getdents64(&self, fd: c_int, buf: *mut u8, len: usize) -> Option<FsResult> {
owner!(self, fd, |fs| fs.getdents64(fd, buf, len))
}
unsafe fn opendir(&self, path: *const c_char) -> Option<FsResult> {
first!(self, |fs| fs.opendir(path))
}
unsafe fn fdopendir(&self, fd: c_int) -> Option<FsResult> {
owner!(self, fd, |fs| fs.fdopendir(fd))
}
unsafe fn readdir(&self, fd: c_int) -> Option<FsResult> {
owner!(self, fd, |fs| fs.readdir(fd))
}
unsafe fn readdir_r(
&self,
fd: c_int,
entry: *mut u8,
result: *mut *mut u8,
) -> Option<FsResult> {
owner!(self, fd, |fs| fs.readdir_r(fd, entry, result))
}
unsafe fn closedir(&self, fd: c_int) -> Option<FsResult> {
owner!(self, fd, |fs| fs.closedir(fd))
}
unsafe fn read(&self, fd: c_int, buf: *mut u8, len: usize) -> Option<FsResult> {
owner!(self, fd, |fs| fs.read(fd, buf, len))
}
unsafe fn write(&self, fd: c_int, buf: *const u8, len: usize) -> Option<FsResult> {
owner!(self, fd, |fs| fs.write(fd, buf, len))
}
unsafe fn close(&self, fd: c_int) -> Option<FsResult> {
owner!(self, fd, |fs| fs.close(fd))
}
unsafe fn fcntl(&self, fd: c_int, cmd: c_int, arg: i64) -> Option<FsResult> {
owner!(self, fd, |fs| fs.fcntl(fd, cmd, arg))
}
unsafe fn flock(&self, fd: c_int, operation: c_int) -> Option<FsResult> {
owner!(self, fd, |fs| fs.flock(fd, operation))
}
unsafe fn ioctl(&self, fd: c_int, request: u64, arg: i64) -> Option<FsResult> {
owner!(self, fd, |fs| fs.ioctl(fd, request, arg))
}
unsafe fn mmap(
&self,
addr: *mut u8,
len: usize,
prot: c_int,
flags: c_int,
fd: c_int,
offset: i64,
) -> Option<FsResult> {
owner!(self, fd, |fs| fs.mmap(addr, len, prot, flags, fd, offset))
}
unsafe fn fd_replaced(&self, fd: c_int) -> Option<FsResult> {
owner!(self, fd, |fs| fs.fd_replaced(fd))
}
unsafe fn dup(&self, oldfd: c_int, newfd: c_int) -> Option<FsResult> {
owner!(self, oldfd, |fs| fs.dup(oldfd, newfd))
}
unsafe fn dup_to(&self, oldfd: c_int, newfd: c_int, flags: Option<c_int>) -> Option<FsResult> {
let source = self.0.iter().position(|fs| fs.owns(oldfd))?;
let target = self.0.iter().position(|fs| fs.owns(newfd));
let result = unsafe { self.0[source].dup_to(oldfd, newfd, flags) }?;
if matches!(result, FsResult::Ok(_))
&& oldfd != newfd
&& let Some(target) = target.filter(|target| *target != source)
{
unsafe { self.0[target].fd_replaced(newfd) };
}
Some(result)
}
}
pub(crate) struct HostTimes {
clock: Arc<Clock>,
offset: Stamp,
files: Mutex<HashMap<(u64, u64), HostFile>>,
writers: Mutex<HashMap<c_int, (u64, u64)>>,
any_writers: AtomicU64,
}
#[derive(Default)]
struct HostFile {
sim: [Option<Stamp>; 4],
seen: Option<Times>,
pending: bool,
}
const ACCESSED: usize = 0;
const MODIFIED: usize = 1;
const CHANGED: usize = 2;
const BORN: usize = 3;
impl HostTimes {
pub(crate) fn new(clock: Arc<Clock>) -> Self {
let offset = stamp_of(snare_interpose::real(SystemTime::now)) - clock_now(Some(&clock));
Self {
clock,
offset,
files: Mutex::default(),
writers: Mutex::default(),
any_writers: AtomicU64::new(0),
}
}
fn now(&self) -> Stamp {
clock_now(Some(&self.clock))
}
unsafe fn host_stat(dirfd: c_int, path: *const c_char, follow: bool) -> Option<libc::stat> {
let mut st: libc::stat = unsafe { std::mem::zeroed() };
let result = unsafe {
if path.is_null() {
libc::fstat(dirfd, &mut st)
} else {
let flags = if follow { 0 } else { libc::AT_SYMLINK_NOFOLLOW };
libc::fstatat(dirfd, path, &mut st, flags)
}
};
(result == 0).then_some(st)
}
#[allow(clippy::unnecessary_cast)]
fn key(st: &libc::stat) -> (u64, u64) {
(st.st_dev as u64, st.st_ino)
}
fn record(&self, st: &libc::stat, change: impl FnOnce(&mut HostFile, Stamp)) {
let now = self.now();
let mut files = self.files.lock().unwrap();
let file = files.entry(Self::key(st)).or_default();
change(file, now);
file.seen = Some(stat_times_of(st));
file.pending = false;
}
unsafe fn created(&self, dirfd: c_int, path: *const c_char) {
if let Some(st) = unsafe { Self::host_stat(dirfd, path, false) } {
self.record(&st, |file, now| file.sim = [Some(now); 4]);
}
unsafe { self.entries_changed(dirfd, path) };
}
unsafe fn entries_changed(&self, dirfd: c_int, path: *const c_char) {
let bytes = unsafe { CStr::from_ptr(path) }.to_bytes();
let end = bytes
.iter()
.rposition(|byte| *byte != b'/')
.map_or(0, |last| last + 1);
let parent = match bytes[..end].iter().rposition(|byte| *byte == b'/') {
Some(0) => b"/".to_vec(),
Some(slash) => bytes[..slash].to_vec(),
None => b".".to_vec(),
};
let Ok(parent) = CString::new(parent) else {
return;
};
if let Some(st) = unsafe { Self::host_stat(dirfd, parent.as_ptr(), true) } {
self.record(&st, |file, now| {
file.sim[MODIFIED] = Some(now);
file.sim[CHANGED] = Some(now);
});
}
}
fn inode_changed(&self, before: &libc::stat, links_left: bool) {
let key = Self::key(before);
if !links_left {
self.files.lock().unwrap().remove(&key);
return;
}
let now = self.now();
let mut files = self.files.lock().unwrap();
let file = files.entry(key).or_default();
file.sim[CHANGED] = Some(now);
file.pending = true;
}
fn written(&self, fd: c_int) {
if self.any_writers.load(Ordering::Acquire) == 0 {
return;
}
let Some(key) = self.writers.lock().unwrap().get(&fd).copied() else {
return;
};
let now = self.now();
let mut files = self.files.lock().unwrap();
let file = files.entry(key).or_default();
file.sim[MODIFIED] = Some(now);
file.sim[CHANGED] = Some(now);
file.pending = true;
}
fn watch(&self, fd: c_int, key: (u64, u64)) {
if self.writers.lock().unwrap().insert(fd, key).is_none() {
self.any_writers.fetch_add(1, Ordering::AcqRel);
}
}
fn unwatch(&self, fd: c_int) {
if self.any_writers.load(Ordering::Acquire) != 0
&& self.writers.lock().unwrap().remove(&fd).is_some()
{
self.any_writers.fetch_sub(1, Ordering::AcqRel);
}
}
fn alias(&self, fd: c_int, newfd: c_int) {
let key = self.writers.lock().unwrap().get(&fd).copied();
if let Some(key) = key {
self.watch(newfd, key);
}
}
unsafe fn open_real(
&self,
dirfd: c_int,
path: *const c_char,
flags: c_int,
mode: u32,
) -> Option<FsResult> {
let writes = writable(flags);
if path.is_null()
|| flags & libc::O_DIRECTORY != 0
|| (!writes && flags & (libc::O_CREAT | libc::O_TRUNC) == 0)
{
return None;
}
#[cfg(target_os = "linux")]
if flags & libc::O_PATH != 0 {
return None;
}
let before = unsafe { Self::host_stat(dirfd, path, flags & libc::O_NOFOLLOW == 0) };
match &before {
Some(st) if st.st_mode & libc::S_IFMT != libc::S_IFREG => return None,
None if flags & libc::O_CREAT == 0 => return None,
_ => {}
}
let fd = unsafe { libc::openat(dirfd, path, flags, mode as libc::c_uint) };
if fd < 0 {
return err(last_error());
}
let Some(after) = (unsafe { Self::host_stat(fd, std::ptr::null(), true) }) else {
return ok(fd as i64);
};
if after.st_mode & libc::S_IFMT != libc::S_IFREG {
return ok(fd as i64);
}
if before.is_none() {
self.record(&after, |file, now| file.sim = [Some(now); 4]);
unsafe { self.entries_changed(dirfd, path) };
} else if flags & libc::O_TRUNC != 0 {
self.record(&after, |file, now| {
file.sim[MODIFIED] = Some(now);
file.sim[CHANGED] = Some(now);
});
}
if writes {
self.watch(fd, Self::key(&after));
}
ok(fd as i64)
}
unsafe fn unlinked(
&self,
dirfd: c_int,
path: *const c_char,
call: impl FnOnce() -> c_int,
) -> Option<FsResult> {
if path.is_null() {
return None;
}
let before = unsafe { Self::host_stat(dirfd, path, false) };
let result = call();
if result < 0 {
return err(last_error());
}
if let Some(before) = before {
let directory = before.st_mode & libc::S_IFMT == libc::S_IFDIR;
self.inode_changed(&before, !directory && before.st_nlink > 1);
}
unsafe { self.entries_changed(dirfd, path) };
ok(result as i64)
}
unsafe fn made(
&self,
dirfd: c_int,
path: *const c_char,
call: impl FnOnce() -> c_int,
) -> Option<FsResult> {
if path.is_null() {
return None;
}
let result = call();
if result < 0 {
return err(last_error());
}
unsafe { self.created(dirfd, path) };
ok(result as i64)
}
unsafe fn relinked(
&self,
fromfd: c_int,
from: *const c_char,
tofd: c_int,
to: *const c_char,
rename: bool,
call: impl FnOnce() -> c_int,
) -> Option<FsResult> {
if from.is_null() || to.is_null() {
return None;
}
let result = call();
if result < 0 {
return err(last_error());
}
if let Some(st) = unsafe { Self::host_stat(tofd, to, false) } {
self.record(&st, |file, now| file.sim[CHANGED] = Some(now));
}
if rename {
unsafe { self.entries_changed(fromfd, from) };
}
unsafe { self.entries_changed(tofd, to) };
ok(result as i64)
}
}
pub(crate) fn last_error() -> c_int {
std::io::Error::last_os_error()
.raw_os_error()
.unwrap_or(libc::EIO)
}
impl Drop for HostTimes {
fn drop(&mut self) {
for fd in self.writers.get_mut().unwrap().keys() {
snare_interpose::release_file(*fd);
}
}
}
impl Fs for HostTimes {
fn owns(&self, fd: c_int) -> bool {
self.any_writers.load(Ordering::Acquire) != 0
&& self.writers.lock().unwrap().contains_key(&fd)
}
unsafe fn open(&self, path: *const c_char, flags: c_int, mode: u32) -> Option<FsResult> {
unsafe { self.open_real(libc::AT_FDCWD, path, flags, mode) }
}
unsafe fn openat(
&self,
dirfd: c_int,
path: *const c_char,
flags: c_int,
mode: u32,
) -> Option<FsResult> {
unsafe { self.open_real(dirfd, path, flags, mode) }
}
unsafe fn mkdir(&self, path: *const c_char, mode: u32) -> Option<FsResult> {
unsafe {
self.made(libc::AT_FDCWD, path, || {
libc::mkdir(path, mode as libc::mode_t)
})
}
}
unsafe fn mkdirat(&self, dirfd: c_int, path: *const c_char, mode: u32) -> Option<FsResult> {
unsafe {
self.made(dirfd, path, || {
libc::mkdirat(dirfd, path, mode as libc::mode_t)
})
}
}
unsafe fn symlink(&self, target: *const c_char, link: *const c_char) -> Option<FsResult> {
unsafe { self.made(libc::AT_FDCWD, link, || libc::symlink(target, link)) }
}
unsafe fn symlinkat(
&self,
target: *const c_char,
dirfd: c_int,
link: *const c_char,
) -> Option<FsResult> {
unsafe { self.made(dirfd, link, || libc::symlinkat(target, dirfd, link)) }
}
unsafe fn unlink(&self, path: *const c_char) -> Option<FsResult> {
unsafe { self.unlinked(libc::AT_FDCWD, path, || libc::unlink(path)) }
}
unsafe fn unlinkat(&self, dirfd: c_int, path: *const c_char, flags: c_int) -> Option<FsResult> {
unsafe { self.unlinked(dirfd, path, || libc::unlinkat(dirfd, path, flags)) }
}
unsafe fn rmdir(&self, path: *const c_char) -> Option<FsResult> {
unsafe { self.unlinked(libc::AT_FDCWD, path, || libc::rmdir(path)) }
}
unsafe fn rename(&self, from: *const c_char, to: *const c_char) -> Option<FsResult> {
unsafe {
self.relinked(libc::AT_FDCWD, from, libc::AT_FDCWD, to, true, || {
libc::rename(from, to)
})
}
}
unsafe fn renameat(
&self,
fromfd: c_int,
from: *const c_char,
tofd: c_int,
to: *const c_char,
) -> Option<FsResult> {
unsafe {
self.relinked(fromfd, from, tofd, to, true, || {
libc::renameat(fromfd, from, tofd, to)
})
}
}
unsafe fn link(&self, from: *const c_char, to: *const c_char) -> Option<FsResult> {
unsafe {
self.relinked(libc::AT_FDCWD, from, libc::AT_FDCWD, to, false, || {
libc::link(from, to)
})
}
}
unsafe fn linkat(
&self,
fromfd: c_int,
from: *const c_char,
tofd: c_int,
to: *const c_char,
flags: c_int,
) -> Option<FsResult> {
unsafe {
self.relinked(fromfd, from, tofd, to, false, || {
libc::linkat(fromfd, from, tofd, to, flags)
})
}
}
unsafe fn write(&self, fd: c_int, _buf: *const u8, len: usize) -> Option<FsResult> {
if len > 0 {
self.written(fd);
}
None
}
unsafe fn pwrite(
&self,
fd: c_int,
_buf: *const u8,
len: usize,
_offset: i64,
) -> Option<FsResult> {
if len > 0 {
self.written(fd);
}
None
}
unsafe fn ftruncate(&self, fd: c_int, _len: i64) -> Option<FsResult> {
self.written(fd);
None
}
unsafe fn close(&self, fd: c_int) -> Option<FsResult> {
self.unwatch(fd);
None
}
unsafe fn fd_replaced(&self, fd: c_int) -> Option<FsResult> {
self.unwatch(fd);
None
}
unsafe fn dup(&self, oldfd: c_int, newfd: c_int) -> Option<FsResult> {
if !self.owns(oldfd) {
return None;
}
self.alias(oldfd, newfd);
ok(newfd as i64)
}
unsafe fn dup_to(&self, oldfd: c_int, newfd: c_int, flags: Option<c_int>) -> Option<FsResult> {
if !self.owns(oldfd) {
return None;
}
let result = crate::fabric::duplicate_to(oldfd, newfd, flags);
if result < 0 {
return err(last_error());
}
if oldfd != newfd {
self.unwatch(newfd);
self.alias(oldfd, newfd);
}
ok(result as i64)
}
unsafe fn fcntl(&self, fd: c_int, cmd: c_int, arg: i64) -> Option<FsResult> {
if cmd != libc::F_DUPFD && cmd != libc::F_DUPFD_CLOEXEC || !self.owns(fd) {
return None;
}
let result = descriptor_fcntl(fd, cmd, arg)?;
if let FsResult::Ok(newfd) = result {
self.alias(fd, newfd as c_int);
}
Some(result)
}
unsafe fn host_times_set(
&self,
dirfd: c_int,
path: *const c_char,
times: &SetTimes,
flags: c_int,
) {
let follow = flags & libc::AT_SYMLINK_NOFOLLOW == 0;
let Some(st) = (unsafe { Self::host_stat(dirfd, path, follow) }) else {
return;
};
let host = stat_times_of(&st);
let host = [host.accessed, host.modified, host.changed, host.born];
let offset = self.offset;
self.record(&st, |file, now| {
let was: [Stamp; 4] =
std::array::from_fn(|index| file.sim[index].unwrap_or(host[index] - offset));
let mut current = Times {
accessed: was[ACCESSED],
modified: was[MODIFIED],
changed: was[CHANGED],
born: was[BORN],
};
current.set(times, now);
let after = [
current.accessed,
current.modified,
current.changed,
current.born,
];
for (index, time) in after.into_iter().enumerate() {
if time != was[index] {
file.sim[index] = Some(time);
}
}
});
}
unsafe fn host_status_changed(&self, dirfd: c_int, path: *const c_char, flags: c_int) {
let follow = flags & libc::AT_SYMLINK_NOFOLLOW == 0;
if let Some(st) = unsafe { Self::host_stat(dirfd, path, follow) } {
self.record(&st, |file, now| file.sim[CHANGED] = Some(now));
}
}
unsafe fn host_metadata(&self, buf: *mut u8, statx: bool) {
#[cfg(target_os = "linux")]
let (key, host) = if statx {
let dev = unsafe {
libc::makedev(
buf.add(136).cast::<u32>().read_unaligned(),
buf.add(140).cast::<u32>().read_unaligned(),
)
};
let ino = unsafe { buf.add(32).cast::<u64>().read_unaligned() };
((dev as u64, ino), unsafe { statx_times_of(buf) })
} else {
let st = unsafe { &*buf.cast::<libc::stat>() };
let times = stat_times_of(st);
(
Self::key(st),
[
Some(times.accessed),
Some(times.modified),
Some(times.changed),
None,
],
)
};
#[cfg(not(target_os = "linux"))]
let (key, host) = {
let _ = statx;
let st = unsafe { &*buf.cast::<libc::stat>() };
let times = stat_times_of(st);
(
Self::key(st),
[
Some(times.accessed),
Some(times.modified),
Some(times.changed),
Some(times.born),
],
)
};
let mut shown =
host.map(|time| time.map(|time| if time == 0 { 0 } else { time - self.offset }));
if let Some(sim) = self.observe(key, host) {
for (shown, sim) in shown.iter_mut().zip(sim) {
if let (Some(shown), Some(sim)) = (shown.as_mut(), sim) {
*shown = sim;
}
}
}
if statx {
#[cfg(target_os = "linux")]
unsafe {
for ((offset, _), time) in STATX_TIMES.into_iter().zip(shown) {
if let Some(time) = time {
let (secs, nanos) = split(time);
buf.add(offset).cast::<i64>().write_unaligned(secs);
buf.add(offset + 8)
.cast::<u32>()
.write_unaligned(nanos as u32);
}
}
}
} else {
let st = unsafe { &mut *buf.cast::<libc::stat>() };
let born = stat_times_of(st).born;
stat_times(
st,
&Times {
accessed: shown[ACCESSED].unwrap_or_default(),
modified: shown[MODIFIED].unwrap_or_default(),
changed: shown[CHANGED].unwrap_or_default(),
born: shown[BORN].unwrap_or(born),
},
);
}
}
}
impl HostTimes {
fn observe(&self, key: (u64, u64), host: [Option<Stamp>; 4]) -> Option<[Option<Stamp>; 4]> {
let mut files = self.files.lock().unwrap();
let file = files.get_mut(&key)?;
let Some(seen) = file.seen else {
file.seen = Some(Times {
accessed: host[ACCESSED].unwrap_or_default(),
modified: host[MODIFIED].unwrap_or_default(),
changed: host[CHANGED].unwrap_or_default(),
born: host[BORN].unwrap_or_default(),
});
file.pending = false;
return Some(file.sim);
};
if let Some(born) = host[BORN]
&& born != 0
&& seen.born != 0
&& born != seen.born
{
files.remove(&key);
return None;
}
let was = [seen.accessed, seen.modified, seen.changed, seen.born];
let moved: [bool; 3] =
std::array::from_fn(|index| host[index].is_some_and(|time| time != was[index]));
if moved.contains(&true) {
if !file.pending {
let now = self.now();
for (index, moved) in moved.into_iter().enumerate() {
if moved {
file.sim[index] = Some(now);
}
}
}
file.pending = false;
}
let mut seen = seen;
let fields = [
&mut seen.accessed,
&mut seen.modified,
&mut seen.changed,
&mut seen.born,
];
for (field, host) in fields.into_iter().zip(host) {
if let Some(host) = host.filter(|host| *host != 0) {
*field = host;
}
}
file.seen = Some(seen);
Some(file.sim)
}
}