artifactize-foundation 0.27.1

Native file, path and executable primitives for Artifactize.
Documentation
use super::*;

#[test]
fn native_directory_iteration_continues_after_a_full_record_page() {
    // Six hundred real names exceed the native 64 KiB query buffer, so collecting
    // all of them without a duplicate proves continuation instead of a restart.
    const ENTRIES: usize = 600;
    let directory = tempfile::tempdir().unwrap();
    let expected: std::collections::BTreeSet<_> = (0..ENTRIES)
        .map(|index| format!("entry-{index:04}.txt"))
        .collect();
    for name in &expected {
        std::fs::write(directory.path().join(name), "").unwrap();
    }
    let handle = open_directory(directory.path()).unwrap();
    let mut entries = read_dir(&handle).unwrap();
    let first = entries
        .next()
        .unwrap()
        .unwrap()
        .file_name()
        .into_string()
        .unwrap();
    let parallel: std::collections::BTreeSet<_> = read_dir(&handle)
        .unwrap()
        .map(|entry| entry.unwrap().file_name().into_string().unwrap())
        .collect();
    assert_eq!(parallel, expected);
    let observed: Vec<_> = std::iter::once(first)
        .chain(
            entries
                .by_ref()
                .map(|entry| entry.unwrap().file_name().into_string().unwrap()),
        )
        .collect();
    assert_eq!(observed.len(), ENTRIES);
    assert_eq!(
        observed
            .into_iter()
            .collect::<std::collections::BTreeSet<_>>(),
        expected
    );
    assert!(entries.next().is_none());
    assert!(entries.next().is_none());
}

// A Unix trailing slash can cause the kernel to follow the last symlink before O_NOFOLLOW.
#[cfg(unix)]
#[test]
fn trailing_directory_syntax_does_not_follow_the_final_link() {
    let root = tempfile::tempdir().unwrap();
    let real = root.path().join("real");
    let link = root.path().join("link");
    std::fs::create_dir(&real).unwrap();
    crate::platform::tests::directory_link(&real, &link);
    for suffix in ["link", "link/", "link/./"] {
        let path = root.path().join(suffix);
        assert!(open_no_follow(std::fs::OpenOptions::new().read(true), &path).is_err());
        assert!(super::super::executable::open_verified_directory(&path).is_err());
    }
    for suffix in ["real", "real/", "real/./"] {
        let path = root.path().join(suffix);
        assert!(
            open_no_follow(std::fs::OpenOptions::new().read(true), &path)
                .unwrap()
                .metadata()
                .unwrap()
                .is_dir()
        );
        assert!(super::super::executable::open_verified_directory(&path).is_ok());
    }
}