fast_ntfs 1.0.1

Forked a low-level NTFS filesystem library
Documentation
// Copyright 2021-2026 Colin Finck <colin@reactos.org>
// SPDX-License-Identifier: MIT OR Apache-2.0

use crate::error::Result;
use crate::index::NtfsIndexFinder;
use crate::index_entry::NtfsIndexEntry;
use crate::indexes::{NtfsIndexEntryHasFileReference, NtfsIndexEntryType};
use crate::io::{Read, Seek};
use crate::ntfs::Ntfs;
use crate::structured_values::NtfsFileName;
use crate::upcase_table::uppercase_cmp_u16str;

/// Defines the [`NtfsIndexEntryType`] for filename indexes (commonly known as "directories").
#[derive(Clone, Copy, Debug)]
pub struct NtfsFileNameIndex;

impl NtfsFileNameIndex {
    /// Finds a file in a filename index by name and returns the [`NtfsIndexEntry`] (if any).
    /// The name is compared case-insensitively based on the filesystem's $UpCase table.
    ///
    /// # Panics
    ///
    /// Panics if [`read_upcase_table`][Ntfs::read_upcase_table] had not been called on the passed [`Ntfs`] object.
    pub fn find<'a, T>(
        index_finder: &'a mut NtfsIndexFinder<Self>,
        ntfs: &Ntfs,
        fs: &mut T,
        name: &str,
    ) -> Option<Result<NtfsIndexEntry<'a, Self>>>
    where
        T: Read + Seek,
    {
        let mut uppercase_name = ArrayVec::<u16, { u8::MAX as usize }>::new();
        for code_unit in name.encode_utf16() {
            if uppercase_name
                .try_push(ntfs.upcase_table().u16_to_uppercase(code_unit))
                .is_err()
            {
                return None;
            }
        }

        // TODO: This always performs a case-insensitive comparison.
        // There are some corner cases where NTFS uses case-sensitive filenames. These need to be considered!
        index_finder.find_by(fs, |slice, position| {
            let file_name = NtfsFileName::name_from_slice(slice, position)?;
            Ok(uppercase_cmp_u16str(&uppercase_name, &file_name, ntfs))
        })
    }
}

impl NtfsIndexEntryType for NtfsFileNameIndex {
    type KeyType = NtfsFileName;
}

impl NtfsIndexEntryHasFileReference for NtfsFileNameIndex {}
use arrayvec::ArrayVec;