Welcome to mirror list, hosted at ThFree Co, Russian Federation.

index_record.rs « src - github.com/windirstat/ntfs.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a313982878ae3e9a815fb6a03aab4b5bbe6f3447 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// Copyright 2021 Colin Finck <colin@reactos.org>
// SPDX-License-Identifier: GPL-2.0-or-later

use crate::attribute_value::NtfsAttributeValue;
use crate::error::{NtfsError, Result};
use crate::index_entry::NtfsIndexNodeEntries;
use crate::ntfs::Ntfs;
use crate::record::RecordHeader;
use crate::structured_values::NewNtfsStructuredValue;
use crate::traits::NtfsReadSeek;
use crate::types::Vcn;
use binread::io::{Read, Seek, SeekFrom};
use binread::{BinRead, BinReaderExt};

/// Size of all [`IndexRecordHeader`] fields.
const INDEX_RECORD_HEADER_SIZE: u32 = 24;

#[allow(unused)]
#[derive(BinRead, Clone, Debug)]
struct IndexRecordHeader {
    record_header: RecordHeader,
    vcn: Vcn,
}

/// Size of all [`IndexNodeHeader`] fields plus some reserved bytes.
pub(crate) const INDEX_NODE_HEADER_SIZE: u64 = 16;

#[derive(BinRead, Clone, Debug)]
pub(crate) struct IndexNodeHeader {
    pub(crate) entries_offset: u32,
    pub(crate) index_size: u32,
    pub(crate) allocated_size: u32,
    pub(crate) flags: u8,
}

#[derive(Clone, Debug)]
pub struct NtfsIndexRecord<'n> {
    ntfs: &'n Ntfs,
    value: NtfsAttributeValue<'n>,
    index_record_header: IndexRecordHeader,
    index_node_header: IndexNodeHeader,
}

const HAS_SUBNODES_FLAG: u8 = 0x01;

impl<'n> NtfsIndexRecord<'n> {
    pub(crate) fn new<T>(
        ntfs: &'n Ntfs,
        fs: &mut T,
        value: NtfsAttributeValue<'n>,
        index_record_size: u32,
    ) -> Result<Self>
    where
        T: Read + Seek,
    {
        let mut value_attached = value.clone().attach(fs);
        let index_record_header = value_attached.read_le::<IndexRecordHeader>()?;
        let index_node_header = value_attached.read_le::<IndexNodeHeader>()?;

        let index_record = Self {
            ntfs,
            value,
            index_record_header,
            index_node_header,
        };
        index_record.validate_signature()?;
        index_record.validate_sizes(index_record_size)?;

        Ok(index_record)
    }

    pub fn entries<K, T>(&self, fs: &mut T) -> Result<NtfsIndexNodeEntries<'n, K>>
    where
        K: NewNtfsStructuredValue<'n>,
        T: Read + Seek,
    {
        let offset = self.value.stream_position() + INDEX_RECORD_HEADER_SIZE as u64;
        let start = offset + self.index_node_header.entries_offset as u64;
        let end = offset + self.index_used_size() as u64;

        let mut value = self.value.clone();
        value.seek(fs, SeekFrom::Start(start))?;

        Ok(NtfsIndexNodeEntries::new(self.ntfs, value, end))
    }

    /// Returns whether this index node has sub-nodes.
    /// Otherwise, this index node is a leaf node.
    pub fn has_subnodes(&self) -> bool {
        (self.index_node_header.flags & HAS_SUBNODES_FLAG) != 0
    }

    pub fn index_allocated_size(&self) -> u32 {
        self.index_node_header.allocated_size
    }

    pub fn index_used_size(&self) -> u32 {
        self.index_node_header.index_size
    }

    fn validate_signature(&self) -> Result<()> {
        let signature = &self.index_record_header.record_header.signature;
        let expected = b"INDX";

        if signature == expected {
            Ok(())
        } else {
            Err(NtfsError::InvalidNtfsIndexSignature {
                position: self.value.data_position().unwrap(),
                expected,
                actual: *signature,
            })
        }
    }

    fn validate_sizes(&self, index_record_size: u32) -> Result<()> {
        // The total size allocated for this index record must not be larger than
        // the size defined for all index records of this index.
        let total_allocated_size = INDEX_RECORD_HEADER_SIZE + self.index_allocated_size();
        if total_allocated_size > index_record_size {
            return Err(NtfsError::InvalidNtfsIndexSize {
                position: self.value.data_position().unwrap(),
                expected: index_record_size,
                actual: total_allocated_size,
            });
        }

        // Furthermore, the total used size for this index record must not be
        // larger than the total allocated size.
        let total_used_size = INDEX_RECORD_HEADER_SIZE
            + self
                .index_record_header
                .record_header
                .update_sequence_array_size()
            + self.index_used_size();
        if total_used_size > total_allocated_size {
            return Err(NtfsError::InvalidNtfsIndexSize {
                position: self.value.data_position().unwrap(),
                expected: total_allocated_size,
                actual: total_used_size,
            });
        }

        Ok(())
    }

    pub fn vcn(&self) -> Vcn {
        self.index_record_header.vcn
    }
}