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

volume_information.rs « structured_values « src - github.com/windirstat/ntfs.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 4550bc12ac92980563f4d0f9660787a998d19aa2 (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
// Copyright 2021 Colin Finck <colin@reactos.org>
// SPDX-License-Identifier: GPL-2.0-or-later

use crate::attribute::NtfsAttributeType;
use crate::error::{NtfsError, Result};
use crate::structured_values::{NtfsStructuredValue, NtfsStructuredValueFromSlice};
use binread::io::Cursor;
use binread::{BinRead, BinReaderExt};
use bitflags::bitflags;

/// Size of all [`VolumeInformationData`] fields.
const VOLUME_INFORMATION_SIZE: usize = 12;

#[derive(BinRead, Clone, Debug)]
struct VolumeInformationData {
    reserved: u64,
    major_version: u8,
    minor_version: u8,
    flags: u16,
}

bitflags! {
    pub struct NtfsVolumeFlags: u16 {
        /// The volume needs to be checked by `chkdsk`.
        const IS_DIRTY = 0x0001;
        const RESIZE_LOG_FILE = 0x0002;
        const UPGRADE_ON_MOUNT = 0x0004;
        const MOUNTED_ON_NT4 = 0x0008;
        const DELETE_USN_UNDERWAY = 0x0010;
        const REPAIR_OBJECT_ID = 0x0020;
        const CHKDSK_UNDERWAY = 0x4000;
        const MODIFIED_BY_CHKDSK = 0x8000;
    }
}

#[derive(Clone, Debug)]
pub struct NtfsVolumeInformation {
    info: VolumeInformationData,
}

impl NtfsVolumeInformation {
    pub fn flags(&self) -> NtfsVolumeFlags {
        NtfsVolumeFlags::from_bits_truncate(self.info.flags)
    }

    pub fn major_version(&self) -> u8 {
        self.info.major_version
    }

    pub fn minor_version(&self) -> u8 {
        self.info.minor_version
    }
}

impl NtfsStructuredValue for NtfsVolumeInformation {
    const TY: NtfsAttributeType = NtfsAttributeType::VolumeInformation;
}

impl<'s> NtfsStructuredValueFromSlice<'s> for NtfsVolumeInformation {
    fn from_slice(slice: &'s [u8], position: u64) -> Result<Self> {
        if slice.len() < VOLUME_INFORMATION_SIZE {
            return Err(NtfsError::InvalidStructuredValueSize {
                position,
                ty: NtfsAttributeType::StandardInformation,
                expected: VOLUME_INFORMATION_SIZE,
                actual: slice.len(),
            });
        }

        let mut cursor = Cursor::new(slice);
        let info = cursor.read_le::<VolumeInformationData>()?;

        Ok(Self { info })
    }
}