artifactize-foundation 0.27.2

Native file, path and executable primitives for Artifactize.
Documentation
//! Handle-pinned Windows directory queries and bounded native record decoding.
use super::{FileKind, WIDE_UNIT_BYTES, kind, open_relative};
use std::{
    ffi::OsString,
    fs::File,
    io, mem,
    os::windows::{ffi::OsStringExt, io::AsRawHandle},
};
use windows_sys::Win32::{
    Foundation::ERROR_NO_MORE_FILES,
    Storage::FileSystem::{
        FILE_ID_BOTH_DIR_INFO, FILE_LIST_DIRECTORY, FileIdBothDirectoryInfo,
        FileIdBothDirectoryRestartInfo, GetFileInformationByHandleEx, SYNCHRONIZE,
    },
};

/// Batch directory records in 64 KiB without unbounded allocation.
const DIRECTORY_PAGE_BYTES: usize = 64 * 1024;
/// FileIdBothDirectoryInfo records start on an eight-byte boundary.
const DIRECTORY_RECORD_ALIGNMENT: usize = 8;

/// The native query requires aligned storage; decoding only uses checked byte slices.
#[repr(align(8))]
struct Page([u8; DIRECTORY_PAGE_BYTES]);

/// The entries of a pinned directory, read from its handle rather than a path that could
/// have been replaced by a link.
pub fn read_dir(directory: &File) -> io::Result<impl Iterator<Item = io::Result<DirEntry>>> {
    // ReOpenFile rejects directory handles on Windows. An empty NT name relative to this
    // handle opens the same directory object with an independent enumeration cursor. It
    // never traverses a pathname, even if the directory has since been renamed.
    let scan = open_relative(directory, &[], FILE_LIST_DIRECTORY | SYNCHRONIZE)?;
    Ok(ReadDir::new(move |query, buffer: &mut Page| {
        query_directory(&scan, query, buffer)
    }))
}

/// The native-query seam: choose restart/continuation and fill one aligned record page.
fn query_directory(directory: &File, query: Query, buffer: &mut Page) -> io::Result<()> {
    let class = match query {
        Query::Restart => FileIdBothDirectoryRestartInfo,
        Query::Continue => FileIdBothDirectoryInfo,
    };
    // SAFETY: the buffer is writable for the length passed and u64-aligned.
    let ok = unsafe {
        GetFileInformationByHandleEx(
            directory.as_raw_handle(),
            class,
            buffer.0.as_mut_ptr().cast(),
            DIRECTORY_PAGE_BYTES as u32,
        )
    };
    if ok == 0 {
        Err(io::Error::last_os_error())
    } else {
        Ok(())
    }
}

pub struct DirEntry {
    name: OsString,
    kind: FileKind,
}

impl DirEntry {
    pub fn file_name(&self) -> OsString {
        self.name.clone()
    }

    pub fn file_type(&self) -> io::Result<FileKind> {
        Ok(self.kind)
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Query {
    Restart,
    Continue,
}

#[derive(Clone, Copy)]
enum Scan {
    Query(Query),
    /// Byte offset of the next record in the current page.
    Record(usize),
    Done,
}

struct ReadDir<Q> {
    query: Q,
    buffer: Box<Page>,
    scan: Scan,
}

impl<Q> Iterator for ReadDir<Q>
where
    Q: FnMut(Query, &mut Page) -> io::Result<()>,
{
    type Item = io::Result<DirEntry>;

    fn next(&mut self) -> Option<Self::Item> {
        loop {
            let query = match self.scan {
                Scan::Record(offset) => match self.record(offset) {
                    Ok(Some(entry)) => return Some(Ok(entry)),
                    Ok(None) => continue,
                    Err(error) => return Some(Err(error)),
                },
                Scan::Query(query) => query,
                Scan::Done => return None,
            };
            if let Err(error) = (self.query)(query, &mut self.buffer) {
                self.scan = Scan::Done;
                if error.raw_os_error() == Some(ERROR_NO_MORE_FILES as i32) {
                    return None;
                }
                return Some(Err(error));
            }
            self.scan = Scan::Record(0);
        }
    }
}

impl<Q> ReadDir<Q> {
    fn new(query: Q) -> Self {
        Self {
            query,
            buffer: Box::new(Page([0; DIRECTORY_PAGE_BYTES])),
            scan: Scan::Query(Query::Restart),
        }
    }

    /// Decode the record at `offset` and advance past it; `.` and `..` decode to `None`.
    fn record(&mut self, offset: usize) -> io::Result<Option<DirEntry>> {
        let Some((next, entry)) = decode_record(&self.buffer.0, offset) else {
            self.scan = Scan::Done;
            return Err(io::Error::other("malformed directory listing"));
        };
        self.scan = next.map_or(Scan::Query(Query::Continue), Scan::Record);
        Ok(entry)
    }
}

fn decode_record(page: &[u8], offset: usize) -> Option<(Option<usize>, Option<DirEntry>)> {
    if !offset.is_multiple_of(DIRECTORY_RECORD_ALIGNMENT) {
        return None;
    }
    let record = page.get(offset..)?;
    let field = |start: usize| {
        let bytes = record.get(start..start + mem::size_of::<u32>())?;
        Some(u32::from_le_bytes(bytes.try_into().ok()?))
    };
    let next = usize::try_from(field(mem::offset_of!(
        FILE_ID_BOTH_DIR_INFO,
        NextEntryOffset
    ))?)
    .ok()?;
    let attributes = field(mem::offset_of!(FILE_ID_BOTH_DIR_INFO, FileAttributes))?;
    // When FILE_ATTRIBUTE_REPARSE_POINT is set, EaSize carries the reparse tag.
    let tag = field(mem::offset_of!(FILE_ID_BOTH_DIR_INFO, EaSize))?;
    let length = usize::try_from(field(mem::offset_of!(
        FILE_ID_BOTH_DIR_INFO,
        FileNameLength
    ))?)
    .ok()?;
    if length == 0 || !length.is_multiple_of(WIDE_UNIT_BYTES) {
        return None;
    }
    let name_start = mem::offset_of!(FILE_ID_BOTH_DIR_INFO, FileName);
    let name_end = name_start.checked_add(length)?;
    let next = match next {
        0 => None,
        next if next >= name_end
            && next < record.len()
            && next.is_multiple_of(DIRECTORY_RECORD_ALIGNMENT) =>
        {
            Some(offset.checked_add(next)?)
        }
        _ => return None,
    };
    let name: Vec<_> = record
        .get(name_start..name_end)?
        .as_chunks::<WIDE_UNIT_BYTES>()
        .0
        .iter()
        .map(|bytes| u16::from_le_bytes(*bytes))
        .collect();
    let dot = u16::from(b'.');
    let entry = (name != [dot] && name != [dot, dot]).then(|| DirEntry {
        name: OsString::from_wide(&name),
        kind: kind(attributes, tag),
    });
    Some((next, entry))
}

// These tests exercise Windows-native directory query buffers, unavailable on other OSes.
#[cfg(test)]
mod tests;