Skip to main content

ntfs/
file_reference.rs

1// Copyright 2021-2023 Colin Finck <colin@reactos.org>
2// SPDX-License-Identifier: MIT OR Apache-2.0
3
4use binrw::io::{Read, Seek};
5use binrw::BinRead;
6
7use crate::error::Result;
8use crate::file::NtfsFile;
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(BinRead, Clone, Copy, Debug)]
15pub struct NtfsFileReference([u8; 8]);
16
17impl NtfsFileReference {
18    pub(crate) const fn new(file_reference_bytes: [u8; 8]) -> Self {
19        Self(file_reference_bytes)
20    }
21
22    /// Returns the 48-bit File Record Number.
23    ///
24    /// This can be fed into [`Ntfs::file`] to create an [`NtfsFile`] object for the corresponding File Record
25    /// (if you cannot use [`Self::to_file`] for some reason).
26    pub fn file_record_number(&self) -> u64 {
27        u64::from_le_bytes(self.0) & 0xffff_ffff_ffff
28    }
29
30    /// Returns the 16-bit sequence number of the File Record.
31    ///
32    /// In a consistent file system, this number matches what [`NtfsFile::sequence_number`] returns.
33    pub fn sequence_number(&self) -> u16 {
34        (u64::from_le_bytes(self.0) >> 48) as u16
35    }
36
37    /// Returns an [`NtfsFile`] for the file referenced by this object.
38    pub fn to_file<'n, T>(&self, ntfs: &'n Ntfs, fs: &mut T) -> Result<NtfsFile<'n>>
39    where
40        T: Read + Seek,
41    {
42        ntfs.file(fs, self.file_record_number())
43    }
44}