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

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

use crate::attribute::NtfsAttributeType;
use crate::attribute_value::{NtfsAttributeValue, NtfsResidentAttributeValue};
use crate::error::{NtfsError, Result};
use crate::guid::{NtfsGuid, GUID_SIZE};
use crate::structured_values::{
    NtfsStructuredValue, NtfsStructuredValueFromResidentAttributeValue,
};
use binread::io::{Cursor, Read, Seek};
use binread::BinReaderExt;

/// Structure of an $OBJECT_ID attribute.
///
/// This optional attribute contains a globally unique identifier of the file.
///
/// An $OBJECT_ID attribute is always resident.
///
/// Reference: <https://flatcap.github.io/linux-ntfs/ntfs/attributes/object_id.html>
#[derive(Clone, Debug)]
pub struct NtfsObjectId {
    object_id: NtfsGuid,
    birth_volume_id: Option<NtfsGuid>,
    birth_object_id: Option<NtfsGuid>,
    domain_id: Option<NtfsGuid>,
}

impl NtfsObjectId {
    fn new<T>(r: &mut T, position: u64, value_length: u64) -> Result<Self>
    where
        T: Read + Seek,
    {
        if value_length < GUID_SIZE as u64 {
            return Err(NtfsError::InvalidStructuredValueSize {
                position,
                ty: NtfsAttributeType::ObjectId,
                expected: GUID_SIZE as u64,
                actual: value_length,
            });
        }

        let object_id = r.read_le::<NtfsGuid>()?;

        let mut birth_volume_id = None;
        if value_length >= 2 * GUID_SIZE as u64 {
            birth_volume_id = Some(r.read_le::<NtfsGuid>()?);
        }

        let mut birth_object_id = None;
        if value_length >= 3 * GUID_SIZE as u64 {
            birth_object_id = Some(r.read_le::<NtfsGuid>()?);
        }

        let mut domain_id = None;
        if value_length >= 4 * GUID_SIZE as u64 {
            domain_id = Some(r.read_le::<NtfsGuid>()?);
        }

        Ok(Self {
            object_id,
            birth_volume_id,
            birth_object_id,
            domain_id,
        })
    }

    /// Returns the (optional) first Object ID that has ever been assigned to this file.
    pub fn birth_object_id(&self) -> Option<&NtfsGuid> {
        self.birth_object_id.as_ref()
    }

    /// Returns the (optional) Object ID of the $Volume file of the partition where this file was created.
    pub fn birth_volume_id(&self) -> Option<&NtfsGuid> {
        self.birth_volume_id.as_ref()
    }

    /// Returns the (optional) Domain ID of this file.
    pub fn domain_id(&self) -> Option<&NtfsGuid> {
        self.domain_id.as_ref()
    }

    /// Returns the Object ID, a globally unique identifier of the file.
    pub fn object_id(&self) -> &NtfsGuid {
        &self.object_id
    }
}

impl<'n, 'f> NtfsStructuredValue<'n, 'f> for NtfsObjectId {
    const TY: NtfsAttributeType = NtfsAttributeType::ObjectId;

    fn from_attribute_value<T>(fs: &mut T, value: NtfsAttributeValue<'n, 'f>) -> Result<Self>
    where
        T: Read + Seek,
    {
        let position = value.data_position().unwrap();
        let value_length = value.len();

        let mut value_attached = value.attach(fs);
        Self::new(&mut value_attached, position, value_length)
    }
}

impl<'n, 'f> NtfsStructuredValueFromResidentAttributeValue<'n, 'f> for NtfsObjectId {
    fn from_resident_attribute_value(value: NtfsResidentAttributeValue<'f>) -> Result<Self> {
        let position = value.data_position().unwrap();
        let value_length = value.len();

        let mut cursor = Cursor::new(value.data());
        Self::new(&mut cursor, position, value_length)
    }
}