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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
use std::io::{Read, Seek};
use crate::attribute::FileAttributeFlags;
use crate::err::Result;
use byteorder::{LittleEndian, ReadBytesExt};
use jiff::Timestamp;
use log::trace;
use serde::Serialize;
use std::io::{Cursor, ErrorKind};
/// A `$STANDARD_INFORMATION` attribute: the file's timestamps and flags and, since NTFS 3.0, its
/// quota owner, quota charge, security id and update sequence number. The older, 48-byte layout
/// lacks those four, which [`StandardInfoAttr::from_slice`] reads as 0. Files can keep that layout
/// on a newer volume; [`StandardInfoAttr::V3_SIZE`] tells the layouts apart.
#[derive(Serialize, Debug, Clone)]
pub struct StandardInfoAttr {
#[serde(serialize_with = "crate::utils::serialize_timestamp")]
pub created: Timestamp,
#[serde(serialize_with = "crate::utils::serialize_timestamp")]
pub modified: Timestamp,
#[serde(serialize_with = "crate::utils::serialize_timestamp")]
pub mft_modified: Timestamp,
#[serde(serialize_with = "crate::utils::serialize_timestamp")]
pub accessed: Timestamp,
/// The file's `FILE_ATTRIBUTE_*` flags.
pub file_flags: FileAttributeFlags,
pub max_version: u32,
pub version: u32,
pub class_id: u32,
/// The key of the file's owner in `$Extend\$Quota`. 0 if quotas have never been enabled on
/// the volume, or in the older layout.
pub owner_id: u32,
/// The key of the file's security descriptor in `$Secure`. 0 for a file with no descriptor
/// there (for example one with its own
/// [`$SECURITY_DESCRIPTOR`](crate::attribute::x50::SecurityDescriptorAttr)), or in the older
/// layout.
pub security_id: u32,
/// The bytes charged to the owner's quota. 0 if quotas have never been enabled on the volume,
/// or in the older layout.
pub quota: u64,
/// The update sequence number of the file's last change journal (`$Extend\$UsnJrnl`) record,
/// which may no longer be in the journal. 0 if the file has had no record since the journal
/// was created, or in the older layout.
pub usn: u64,
}
impl StandardInfoAttr {
/// The size of the NTFS 3.0 layout. An attribute whose
/// [`value_size`](crate::attribute::header::MftAttributeHeader::value_size) is at least this
/// has `owner_id`, `security_id`, `quota` and `usn`.
pub const V3_SIZE: usize = 72;
/// Parse a Standard Information attribute buffer whose length is the attribute value size.
///
/// NTFS uses (at least) two layouts:
/// - **48 bytes** (NTFS < 3.0): ends at `class_id`; the four later fields are set to 0
/// - **72 bytes or more** (NTFS >= 3.0): includes `owner_id`, `security_id`, `quota`, and `usn`
pub fn from_slice(value: &[u8]) -> Result<StandardInfoAttr> {
if value.len() < 48 {
return Err(std::io::Error::from(ErrorKind::UnexpectedEof).into());
}
if value.len() != 48 && value.len() < Self::V3_SIZE {
return Err(std::io::Error::new(
ErrorKind::InvalidData,
format!(
"$STANDARD_INFORMATION has unsupported size {} (expected 48 or >=72)",
value.len()
),
)
.into());
}
let mut reader = Cursor::new(value);
// Timestamps are stored as Windows FILETIME (100ns since 1601-01-01 UTC).
let created =
crate::utils::windows_filetime_to_timestamp(reader.read_u64::<LittleEndian>()?)?;
let modified =
crate::utils::windows_filetime_to_timestamp(reader.read_u64::<LittleEndian>()?)?;
let mft_modified =
crate::utils::windows_filetime_to_timestamp(reader.read_u64::<LittleEndian>()?)?;
let accessed =
crate::utils::windows_filetime_to_timestamp(reader.read_u64::<LittleEndian>()?)?;
let file_flags = FileAttributeFlags::from_bits_retain(reader.read_u32::<LittleEndian>()?);
let max_version = reader.read_u32::<LittleEndian>()?;
let version = reader.read_u32::<LittleEndian>()?;
let class_id = reader.read_u32::<LittleEndian>()?;
let (owner_id, security_id, quota, usn) = if value.len() >= Self::V3_SIZE {
(
reader.read_u32::<LittleEndian>()?,
reader.read_u32::<LittleEndian>()?,
reader.read_u64::<LittleEndian>()?,
reader.read_u64::<LittleEndian>()?,
)
} else {
(0, 0, 0, 0)
};
Ok(StandardInfoAttr {
created,
modified,
mft_modified,
accessed,
file_flags,
max_version,
version,
class_id,
owner_id,
security_id,
quota,
usn,
})
}
/// Parse a Standard Information attribute buffer in the 72-byte layout. Use
/// [`StandardInfoAttr::from_slice`] for a value that may be the 48-byte one.
///
/// # Example
///
/// Parse a raw buffer.
///
/// ```
/// use mft::attribute::x10::StandardInfoAttr;
/// use mft::attribute::FileAttributeFlags;
/// # use std::io::Cursor;
/// let attribute_buffer: &[u8] = &[
/// 0x2F,0x6D,0xB6,0x6F,0x0C,0x97,0xCE,0x01,0x56,0xCD,0x1A,0x75,0x73,0xB5,0xCE,0x01,
/// 0x56,0xCD,0x1A,0x75,0x73,0xB5,0xCE,0x01,0x56,0xCD,0x1A,0x75,0x73,0xB5,0xCE,0x01,
/// 0x20,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
/// 0x00,0x00,0x00,0x00,0xB0,0x05,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
/// 0x68,0x58,0xA0,0x0A,0x02,0x00,0x00,0x00
/// ];
///
/// let attribute = StandardInfoAttr::from_reader(&mut Cursor::new(attribute_buffer)).unwrap();
///
/// assert_eq!(attribute.created.as_second(), 1376278290);
/// assert_eq!(attribute.modified.as_second(), 1379621073);
/// assert_eq!(attribute.mft_modified.as_second(), 1379621073);
/// assert_eq!(attribute.accessed.as_second(), 1379621073);
/// assert_eq!(attribute.file_flags.bits(), 32);
/// assert_eq!(attribute.max_version, 0);
/// assert_eq!(attribute.version, 0);
/// assert_eq!(attribute.class_id, 0);
/// assert_eq!(attribute.security_id, 1456);
/// assert_eq!(attribute.quota, 0);
/// assert_eq!(attribute.usn, 8768215144);
/// ```
pub fn from_reader<S: Read + Seek>(reader: &mut S) -> Result<StandardInfoAttr> {
trace!("Offset {}: StandardInfoAttr", reader.stream_position()?);
// Timestamps are stored as Windows FILETIME (100ns since 1601-01-01 UTC).
let created =
crate::utils::windows_filetime_to_timestamp(reader.read_u64::<LittleEndian>()?)?;
let modified =
crate::utils::windows_filetime_to_timestamp(reader.read_u64::<LittleEndian>()?)?;
let mft_modified =
crate::utils::windows_filetime_to_timestamp(reader.read_u64::<LittleEndian>()?)?;
let accessed =
crate::utils::windows_filetime_to_timestamp(reader.read_u64::<LittleEndian>()?)?;
Ok(StandardInfoAttr {
created,
modified,
mft_modified,
accessed,
file_flags: FileAttributeFlags::from_bits_retain(reader.read_u32::<LittleEndian>()?),
max_version: reader.read_u32::<LittleEndian>()?,
version: reader.read_u32::<LittleEndian>()?,
class_id: reader.read_u32::<LittleEndian>()?,
owner_id: reader.read_u32::<LittleEndian>()?,
security_id: reader.read_u32::<LittleEndian>()?,
quota: reader.read_u64::<LittleEndian>()?,
usn: reader.read_u64::<LittleEndian>()?,
})
}
}
#[cfg(test)]
mod tests {
use super::StandardInfoAttr;
use crate::attribute::FileAttributeFlags;
use crate::err::Error;
// Test vectors are based on the doc-test buffer above.
const STDINFO_72: &[u8] = &[
0x2F, 0x6D, 0xB6, 0x6F, 0x0C, 0x97, 0xCE, 0x01, 0x56, 0xCD, 0x1A, 0x75, 0x73, 0xB5, 0xCE,
0x01, 0x56, 0xCD, 0x1A, 0x75, 0x73, 0xB5, 0xCE, 0x01, 0x56, 0xCD, 0x1A, 0x75, 0x73, 0xB5,
0xCE, 0x01, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xB0, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x68, 0x58, 0xA0, 0x0A, 0x02, 0x00, 0x00, 0x00,
];
#[test]
fn file_flags_keep_unknown_bits() {
// 0x80000000 is set on `$Extend\$RmMetadata\$Txf`, and has no documented name.
let mut buf = STDINFO_72.to_vec();
buf[32..36].copy_from_slice(&0x8000_0020u32.to_le_bytes());
let attr = StandardInfoAttr::from_slice(&buf).unwrap();
assert_eq!(attr.file_flags.bits(), 0x8000_0020);
}
#[test]
fn from_slice_len_48_treats_extended_fields_as_absent() {
// Spec note: the "base" STANDARD_INFORMATION value layout is 48 bytes; the extended
// layout is 72 bytes. When a value is 48 bytes, the additional fields are not present
// and must not be read from subsequent bytes in the file record.
//
// See: external/refs/specs/NTFS-standard-information.md
let buf48 = &STDINFO_72[..48];
let attr = StandardInfoAttr::from_slice(buf48).unwrap();
assert_eq!(attr.created.as_second(), 1376278290);
assert_eq!(attr.modified.as_second(), 1379621073);
assert_eq!(attr.mft_modified.as_second(), 1379621073);
assert_eq!(attr.accessed.as_second(), 1379621073);
assert_eq!(attr.file_flags, FileAttributeFlags::FILE_ATTRIBUTE_ARCHIVE);
assert_eq!(attr.max_version, 0);
assert_eq!(attr.version, 0);
assert_eq!(attr.class_id, 0);
// Extended fields absent => zero.
assert_eq!(attr.owner_id, 0);
assert_eq!(attr.security_id, 0);
assert_eq!(attr.quota, 0);
assert_eq!(attr.usn, 0);
}
#[test]
fn from_slice_len_72_parses_extended_fields() {
let attr = StandardInfoAttr::from_slice(STDINFO_72).unwrap();
assert_eq!(attr.created.as_second(), 1376278290);
assert_eq!(attr.modified.as_second(), 1379621073);
assert_eq!(attr.mft_modified.as_second(), 1379621073);
assert_eq!(attr.accessed.as_second(), 1379621073);
assert_eq!(attr.file_flags.bits(), 32);
assert_eq!(attr.max_version, 0);
assert_eq!(attr.version, 0);
assert_eq!(attr.class_id, 0);
assert_eq!(attr.security_id, 1456);
assert_eq!(attr.quota, 0);
assert_eq!(attr.usn, 8768215144);
}
#[test]
fn from_slice_rejects_intermediate_sizes() {
let buf60 = [0u8; 60];
let err = StandardInfoAttr::from_slice(&buf60).unwrap_err();
match err {
Error::IoError { source } => {
assert_eq!(source.kind(), std::io::ErrorKind::InvalidData);
assert!(
source.to_string().contains("unsupported size"),
"unexpected io error message for len=60: {source}"
);
}
other => panic!("unexpected error kind for len=60: {other:?}"),
}
}
}