artifactize-foundation 0.27.2

Native file, path and executable primitives for Artifactize.
Documentation
use super::{File, OsStr, PinnedFile, check_file, io};
use crate::platform::private::restrict_file;
use rustix::fs::{AtFlags, Mode, OFlags, mkdirat, openat, renameat, unlinkat};

// New directories/files start with no public grants. The mode is restored on the opened
// object after create so a restrictive umask cannot keep the owner out of a private file.
const PRIVATE_DIR: Mode = Mode::RWXU;
const PRIVATE_FILE: Mode = Mode::RUSR.union(Mode::WUSR);

pub(super) fn child(parent: &File, name: &OsStr, create: bool) -> io::Result<File> {
    if create {
        match mkdirat(parent, name, PRIVATE_DIR) {
            Ok(()) => {
                if let Err(error) = restore_directory_access(parent, name) {
                    let _ = unlinkat(parent, name, AtFlags::REMOVEDIR);
                    return Err(error);
                }
            }
            Err(rustix::io::Errno::EXIST) => {}
            Err(error) => return Err(error.into()),
        }
    }
    Ok(openat(
        parent,
        name,
        OFlags::RDONLY | OFlags::DIRECTORY | OFlags::NOFOLLOW | OFlags::CLOEXEC,
        Mode::empty(),
    )?
    .into())
}

// The new entry can have mode 000 after mkdirat. Use a handle which does not
// require read access to the entry, then restore permissions on that exact inode.
#[cfg(target_os = "linux")]
fn restore_directory_access(parent: &File, name: &OsStr) -> io::Result<()> {
    let directory = super::super::unix::open_directory_for_mode(parent, name.as_ref())?;
    super::super::unix::restrict_directory_handle(&directory)
}

#[cfg(target_os = "macos")]
fn restore_directory_access(parent: &File, name: &OsStr) -> io::Result<()> {
    let events = OFlags::from_bits_retain(libc::O_EVTONLY as _);
    let pinned: File = openat(
        parent,
        name,
        events | OFlags::DIRECTORY | OFlags::NOFOLLOW | OFlags::CLOEXEC,
        Mode::empty(),
    )?
    .into();
    super::super::unix::restrict_directory_handle(&pinned)
}

pub(super) fn file(parent: &File, name: &OsStr, mode: PinnedFile) -> io::Result<File> {
    let access = match mode {
        PinnedFile::Read => OFlags::RDONLY,
        PinnedFile::Write | PinnedFile::OpenOrCreate | PinnedFile::Create | PinnedFile::Lease => {
            OFlags::RDWR
        }
    } | OFlags::CLOEXEC
        | OFlags::NOFOLLOW
        | OFlags::NONBLOCK;
    let file = match mode {
        PinnedFile::OpenOrCreate | PinnedFile::Create | PinnedFile::Lease => {
            match openat(
                parent,
                name,
                access | OFlags::CREATE | OFlags::EXCL,
                PRIVATE_FILE,
            ) {
                Ok(fd) => {
                    let file: File = fd.into();
                    restrict_file(&file)?;
                    return Ok(file);
                }
                Err(rustix::io::Errno::EXIST)
                    if matches!(mode, PinnedFile::OpenOrCreate | PinnedFile::Lease) => {}
                Err(error) => return Err(error.into()),
            }
            openat(parent, name, access, Mode::empty())?
        }
        _ => openat(parent, name, access, Mode::empty())?,
    };
    Ok(file.into())
}

pub(super) fn workspace_file(parent: &File, name: &OsStr) -> io::Result<File> {
    // Apply the ordinary workspace umask, not the storage's owner-only policy.
    let flags =
        OFlags::RDWR | OFlags::CREATE | OFlags::NOFOLLOW | OFlags::CLOEXEC | OFlags::NONBLOCK;
    let mode = Mode::RUSR | Mode::WUSR | Mode::RGRP | Mode::WGRP | Mode::ROTH | Mode::WOTH;
    Ok(openat(parent, name, flags, mode)?.into())
}

pub(super) fn single_link(file: &File) -> io::Result<bool> {
    use std::os::unix::fs::MetadataExt;
    Ok(file.metadata()?.nlink() == 1)
}

pub(super) fn replace(parent: &File, _: &File, source: &OsStr, target: &OsStr) -> io::Result<()> {
    renameat(parent, source, parent, target)?;
    Ok(())
}

pub(super) fn remove(parent: &File, name: &OsStr) -> io::Result<()> {
    check_file(&file(parent, name, PinnedFile::Read)?)?;
    unlinkat(parent, name, AtFlags::empty())?;
    Ok(())
}

pub(super) fn sync(parent: &File) -> io::Result<()> {
    parent.sync_all()
}