artifactize-foundation 0.27.2

Native file, path and executable primitives for Artifactize.
Documentation
//! Shared native fixture setup for the Foundation tests.

use std::path::Path;

// Unix symlinks can always be created without privileges.
#[cfg(unix)]
pub(super) fn directory_link(target: &Path, path: &Path) {
    std::os::unix::fs::symlink(target, path).unwrap();
}

#[cfg(unix)]
pub(super) fn shared_directory(path: &Path) {
    use std::os::unix::fs::PermissionsExt;
    // Grant all permission bits deliberately, to model an untrusted ancestor.
    const SHARED: u32 = 0o777;
    std::fs::create_dir(path).unwrap();
    std::fs::set_permissions(path, std::fs::Permissions::from_mode(SHARED)).unwrap();
}

#[cfg(windows)]
pub(super) fn grant_inheritable_read(path: &Path) {
    grant_access(path, "*S-1-1-0:(OI)(CI)(R)");
}

#[cfg(windows)]
pub(super) fn grant_access(path: &Path, ace: &str) {
    let root = std::env::var_os("SystemRoot").expect("Windows system directory");
    let tool = std::path::PathBuf::from(root)
        .join("System32")
        .join("icacls.exe");
    let output = std::process::Command::new(tool)
        .arg(path)
        .args(["/grant", ace])
        .output()
        .unwrap();
    assert!(output.status.success(), "{output:?}");
}

#[cfg(windows)]
pub(super) fn verbatim_entries(path: &Path) -> std::path::PathBuf {
    let root = std::fs::canonicalize(path).unwrap();
    std::fs::write(root.join("literal."), b"verbatim").unwrap();
    std::fs::write(root.join("literal"), b"ordinary").unwrap();
    root
}