Skip to main content

fast_ntfs/
file_reference.rs

1// Copyright 2021-2026 Colin Finck <colin@reactos.org>
2// SPDX-License-Identifier: MIT OR Apache-2.0
3
4use zerocopy::{FromBytes, Immutable, KnownLayout, Unaligned};
5
6use crate::error::Result;
7use crate::file::NtfsFile;
8use crate::io::{Read, Seek};
9use crate::ntfs::Ntfs;
10
11/// Absolute reference to a File Record on the filesystem, composed out of a File Record Number and a Sequence Number.
12///
13/// Reference: <https://flatcap.github.io/linux-ntfs/ntfs/concepts/file_reference.html>
14#[derive(Clone, Copy, Debug, FromBytes, Immutable, KnownLayout, Unaligned)]
15#[repr(transparent)]
16pub struct NtfsFileReference([u8; 8]);
17
18impl NtfsFileReference {
19    /// Returns the 48-bit File Record Number.
20    ///
21    /// This can be fed into [`Ntfs::file`] to create an [`NtfsFile`] object for the corresponding File Record
22    /// (if you cannot use [`Self::to_file`] for some reason).
23    pub fn file_record_number(&self) -> u64 {
24        u64::from_le_bytes(self.0) & 0xffff_ffff_ffff
25    }
26
27    /// Returns the 16-bit sequence number of the File Record.
28    ///
29    /// In a consistent file system, this number matches what [`NtfsFile::sequence_number`] returns.
30    pub fn sequence_number(&self) -> u16 {
31        (u64::from_le_bytes(self.0) >> 48) as u16
32    }
33
34    /// Returns an [`NtfsFile`] for the file referenced by this object.
35    pub fn to_file<'n, T>(&self, ntfs: &'n Ntfs, fs: &mut T) -> Result<NtfsFile<'n>>
36    where
37        T: Read + Seek,
38    {
39        ntfs.file(fs, self.file_record_number())
40    }
41}