ntfs/structured_values/
volume_name.rs

1// Copyright 2021-2023 Colin Finck <colin@reactos.org>
2// SPDX-License-Identifier: MIT OR Apache-2.0
3
4use core::mem;
5
6use arrayvec::ArrayVec;
7use binrw::io::{Cursor, Read, Seek};
8use nt_string::u16strle::U16StrLe;
9
10use crate::attribute::NtfsAttributeType;
11use crate::attribute_value::{NtfsAttributeValue, NtfsResidentAttributeValue};
12use crate::error::{NtfsError, Result};
13use crate::structured_values::{
14    NtfsStructuredValue, NtfsStructuredValueFromResidentAttributeValue,
15};
16use crate::types::NtfsPosition;
17
18/// The largest VolumeName attribute has a name containing 128 UTF-16 code points (256 bytes).
19const VOLUME_NAME_MAX_SIZE: usize = 128 * mem::size_of::<u16>();
20
21/// Structure of a $VOLUME_NAME attribute.
22///
23/// This attribute is only used by the top-level $Volume file and contains the user-defined name of this filesystem.
24/// You can easily access it via [`Ntfs::volume_name`].
25///
26/// A $VOLUME_NAME attribute is always resident.
27///
28/// Reference: <https://flatcap.github.io/linux-ntfs/ntfs/attributes/volume_name.html>
29///
30/// [`Ntfs::volume_name`]: crate::Ntfs::volume_name
31#[derive(Clone, Debug)]
32pub struct NtfsVolumeName {
33    name: ArrayVec<u8, VOLUME_NAME_MAX_SIZE>,
34}
35
36impl NtfsVolumeName {
37    fn new<T>(r: &mut T, position: NtfsPosition, value_length: u64) -> Result<Self>
38    where
39        T: Read + Seek,
40    {
41        if value_length > VOLUME_NAME_MAX_SIZE as u64 {
42            return Err(NtfsError::InvalidStructuredValueSize {
43                position,
44                ty: NtfsAttributeType::VolumeName,
45                expected: VOLUME_NAME_MAX_SIZE as u64,
46                actual: value_length,
47            });
48        }
49
50        let value_length = value_length as usize;
51
52        let mut name = ArrayVec::from([0u8; VOLUME_NAME_MAX_SIZE]);
53        r.read_exact(&mut name[..value_length])?;
54        name.truncate(value_length);
55
56        Ok(Self { name })
57    }
58
59    /// Gets the volume name and returns it wrapped in a [`U16StrLe`].
60    pub fn name(&self) -> U16StrLe {
61        U16StrLe(&self.name)
62    }
63
64    /// Returns the volume name length, in bytes.
65    ///
66    /// A volume name has a maximum length of 128 UTF-16 code points (256 bytes).
67    pub fn name_length(&self) -> usize {
68        self.name.len()
69    }
70}
71
72impl<'n, 'f> NtfsStructuredValue<'n, 'f> for NtfsVolumeName {
73    const TY: NtfsAttributeType = NtfsAttributeType::VolumeName;
74
75    fn from_attribute_value<T>(fs: &mut T, value: NtfsAttributeValue<'n, 'f>) -> Result<Self>
76    where
77        T: Read + Seek,
78    {
79        let position = value.data_position();
80        let value_length = value.len();
81
82        let mut value_attached = value.attach(fs);
83        Self::new(&mut value_attached, position, value_length)
84    }
85}
86
87impl<'n, 'f> NtfsStructuredValueFromResidentAttributeValue<'n, 'f> for NtfsVolumeName {
88    fn from_resident_attribute_value(value: NtfsResidentAttributeValue<'f>) -> Result<Self> {
89        let position = value.data_position();
90        let value_length = value.len();
91
92        let mut cursor = Cursor::new(value.data());
93        Self::new(&mut cursor, position, value_length)
94    }
95}