use std::fs;
use std::path::{Path, PathBuf};
use crate::{Error, Result};
#[cfg(unix)]
pub(crate) use unix::Beneath;
#[cfg(unix)]
mod unix {
use super::*;
use rustix::fs::{AtFlags, FileType, Mode, OFlags};
use rustix::io::Errno;
use std::os::fd::{AsFd, BorrowedFd, OwnedFd};
fn split(rel: &str) -> (&str, &str) {
rel.rsplit_once('/').unwrap_or(("", rel))
}
#[allow(clippy::unnecessary_cast, clippy::cast_possible_truncation)]
fn mode_of(bits: u32) -> Mode {
Mode::from_raw_mode((bits & 0o7777) as rustix::fs::RawMode)
}
pub(crate) struct Beneath {
root: OwnedFd,
path: PathBuf,
}
impl Beneath {
pub(crate) fn new(root: &Path) -> Result<Beneath> {
let fd = rustix::fs::open(root, OFlags::RDONLY | OFlags::DIRECTORY | OFlags::CLOEXEC, Mode::empty())
.map_err(|e| Error::Io(root.to_path_buf(), e.into()))?;
Ok(Beneath { root: fd, path: root.to_path_buf() })
}
fn fail(&self, base: BorrowedFd<'_>, comp: &str, prefix: &str, e: Errno) -> Error {
let link = matches!(e, Errno::LOOP | Errno::NOTDIR | Errno::MLINK)
&& rustix::fs::statat(base, comp, AtFlags::SYMLINK_NOFOLLOW)
.is_ok_and(|st| FileType::from_raw_mode(st.st_mode) == FileType::Symlink);
if link { Error::ThroughLink(prefix.to_owned()) } else { Error::Io(self.path.join(prefix), e.into()) }
}
fn dir(&self, rel: &str, create: bool) -> Result<OwnedFd> {
let flags = OFlags::RDONLY | OFlags::DIRECTORY | OFlags::NOFOLLOW | OFlags::CLOEXEC;
let mut cur = self.root.try_clone().map_err(|e| Error::Io(self.path.clone(), e))?;
if rel.is_empty() {
return Ok(cur);
}
let mut end = 0;
for comp in rel.split('/') {
end += comp.len();
let prefix = &rel[..end];
end += 1;
let base = cur.as_fd();
let next = match rustix::fs::openat(base, comp, flags, Mode::empty()) {
Err(Errno::NOENT) if create => {
match rustix::fs::mkdirat(base, comp, Mode::from_raw_mode(0o755)) {
Ok(()) | Err(Errno::EXIST) => {}
Err(e) => return Err(self.fail(base, comp, prefix, e)),
}
rustix::fs::openat(base, comp, flags, Mode::empty())
}
r => r,
};
cur = next.map_err(|e| self.fail(base, comp, prefix, e))?;
}
Ok(cur)
}
fn open_at(&self, rel: &str, flags: OFlags, mode: u32, create_parents: bool) -> Result<OwnedFd> {
let (parent, name) = split(rel);
let dir = self.dir(parent, create_parents)?;
rustix::fs::openat(&dir, name, flags | OFlags::NOFOLLOW | OFlags::CLOEXEC, mode_of(mode))
.map_err(|e| self.fail(dir.as_fd(), name, rel, e))
}
pub(crate) fn create(&self, rel: &str) -> Result<fs::File> {
Ok(self.open_at(rel, OFlags::RDWR | OFlags::CREATE | OFlags::EXCL, 0o666, true)?.into())
}
pub(crate) fn open(&self, rel: &str) -> Result<fs::File> {
Ok(self.open_at(rel, OFlags::RDWR, 0, false)?.into())
}
pub(crate) fn mkdir_all(&self, rel: &str) -> Result<()> {
self.dir(rel, true).map(drop)
}
pub(crate) fn symlink(&self, rel: &str, target: &str) -> Result<()> {
let (parent, name) = split(rel);
let dir = self.dir(parent, true)?;
rustix::fs::symlinkat(target, &dir, name).map_err(|e| self.fail(dir.as_fd(), name, rel, e))
}
pub(crate) fn chmod(&self, rel: &str, mode: u32, is_dir: bool) -> Result<()> {
let flags = if is_dir { OFlags::RDONLY | OFlags::DIRECTORY } else { OFlags::RDONLY };
let fd = self.open_at(rel, flags, 0, false)?;
rustix::fs::fchmod(&fd, mode_of(mode)).map_err(|e| Error::Io(self.path.join(rel), e.into()))
}
pub(crate) fn free_bytes(&self) -> Option<u64> {
let st = rustix::fs::fstatvfs(&self.root).ok()?;
Some(st.f_bavail.saturating_mul(st.f_frsize))
}
pub(crate) fn path(&self) -> &Path {
&self.path
}
}
}
#[cfg(not(unix))]
pub(crate) use other::Beneath;
#[cfg(not(unix))]
mod other {
use super::*;
pub(crate) struct Beneath {
path: PathBuf,
}
impl Beneath {
pub(crate) fn new(root: &Path) -> Result<Beneath> {
if !root.is_dir() {
return Err(Error::Io(root.to_path_buf(), std::io::ErrorKind::NotADirectory.into()));
}
Ok(Beneath { path: root.to_path_buf() })
}
pub(crate) fn create(&self, rel: &str) -> Result<fs::File> {
let p = self.path.join(rel);
if let Some(dir) = p.parent() {
fs::create_dir_all(dir).map_err(|e| Error::Io(dir.to_path_buf(), e))?;
}
fs::File::create_new(&p).map_err(|e| Error::Io(p, e))
}
pub(crate) fn open(&self, rel: &str) -> Result<fs::File> {
let p = self.path.join(rel);
fs::OpenOptions::new().read(true).write(true).open(&p).map_err(|e| Error::Io(p, e))
}
pub(crate) fn free_bytes(&self) -> Option<u64> {
None
}
pub(crate) fn path(&self) -> &Path {
&self.path
}
}
}