diff options
author | Omer Ben-Amram <omerbenamram@gmail.com> | 2019-05-13 15:49:56 +0300 |
---|---|---|
committer | Omer Ben-Amram <omerbenamram@gmail.com> | 2019-05-13 15:49:56 +0300 |
commit | 4433fe6b87263e9eba82615240de0cc8e7d9c380 (patch) | |
tree | eb088723eabcce0b1889d93ee6c125370fc68e61 | |
parent | 52e44e7ab39fae1b53eb437b43d21c9ebb5958e7 (diff) |
add more flags
-rw-r--r-- | src/attribute/header.rs | 3 | ||||
-rw-r--r-- | src/attribute/mod.rs | 12 | ||||
-rw-r--r-- | src/attribute/x30.rs | 30 | ||||
-rw-r--r-- | src/entry.rs | 27 | ||||
-rw-r--r-- | src/err.rs | 2 | ||||
-rw-r--r-- | src/lib.rs | 1 | ||||
-rw-r--r-- | src/macros.rs | 13 |
7 files changed, 51 insertions, 37 deletions
diff --git a/src/attribute/header.rs b/src/attribute/header.rs index aea32f3..4920e9a 100644 --- a/src/attribute/header.rs +++ b/src/attribute/header.rs @@ -1,4 +1,4 @@ -use crate::attribute::{serialize_attr_data_flags, AttributeDataFlags, AttributeType}; +use crate::attribute::{AttributeDataFlags, AttributeType}; use crate::err::{self, Result}; use crate::utils::read_utf16_string; use crate::ReadSeek; @@ -26,7 +26,6 @@ pub struct AttributeHeader { /// The offset of the attribute name from the start of the attribute record, in bytes. /// If the NameLength member is 0, this member is undefined. pub name_offset: Option<u16>, - #[serde(serialize_with = "serialize_attr_data_flags")] pub data_flags: AttributeDataFlags, /// The unique instance for this attribute in the file record. pub instance: u16, diff --git a/src/attribute/mod.rs b/src/attribute/mod.rs index 0aa97b3..dc1bd40 100644 --- a/src/attribute/mod.rs +++ b/src/attribute/mod.rs @@ -4,6 +4,8 @@ pub mod x10; pub mod x30; use crate::err::{self, Result}; +use crate::impl_serialize_for_bitflags; + use bitflags::bitflags; use num_traits::FromPrimitive; @@ -72,12 +74,4 @@ bitflags! { } } -pub fn serialize_attr_data_flags<S>( - item: &AttributeDataFlags, - serializer: S, -) -> ::std::result::Result<S::Ok, S::Error> -where - S: ser::Serializer, -{ - serializer.serialize_str(&format!("{:?}", item)) -} +impl_serialize_for_bitflags! {AttributeDataFlags} diff --git a/src/attribute/x30.rs b/src/attribute/x30.rs index 9ab6069..5ee0b96 100644 --- a/src/attribute/x30.rs +++ b/src/attribute/x30.rs @@ -1,6 +1,8 @@ use crate::err::{self, Result}; +use crate::impl_serialize_for_bitflags; use log::trace; +use bitflags::bitflags; use byteorder::{LittleEndian, ReadBytesExt}; use encoding::all::UTF_16LE; use encoding::{DecoderTrap, Encoding}; @@ -23,14 +25,33 @@ pub struct FileNameAttr { pub accessed: DateTime<Utc>, pub logical_size: u64, pub physical_size: u64, - pub flags: u32, + pub flags: FileAttributeFlags, pub reparse_value: u32, pub name_length: u8, pub namespace: u8, pub name: String, - pub fullname: Option<String>, } +bitflags! { + pub struct FileAttributeFlags: u32 { + const FILE_ATTRIBUTE_READONLY = 0x0000_0001; + const FILE_ATTRIBUTE_HIDDEN = 0x0000_0002; + const FILE_ATTRIBUTE_SYSTEM = 0x0000_0004; + const FILE_ATTRIBUTE_ARCHIVE = 0x0000_0020; + const FILE_ATTRIBUTE_DEVICE = 0x0000_0040; + const FILE_ATTRIBUTE_NORMAL = 0x0000_0080; + const FILE_ATTRIBUTE_TEMPORARY = 0x0000_0100; + const FILE_ATTRIBUTE_SPARSE_FILE = 0x0000_0200; + const FILE_ATTRIBUTE_REPARSE_POINT = 0x0000_0400; + const FILE_ATTRIBUTE_COMPRESSED = 0x0000_0800; + const FILE_ATTRIBUTE_OFFLINE = 0x0000_1000; + const FILE_ATTRIBUTE_NOT_CONTENT_INDEXED = 0x0000_2000; + const FILE_ATTRIBUTE_ENCRYPTED = 0x0000_4000; + } +} + +impl_serialize_for_bitflags! {FileAttributeFlags} + impl FileNameAttr { /// Parse a Filename attrbiute buffer. /// @@ -84,7 +105,7 @@ impl FileNameAttr { .to_datetime(); let logical_size = reader.read_u64::<LittleEndian>()?; let physical_size = reader.read_u64::<LittleEndian>()?; - let flags = reader.read_u32::<LittleEndian>()?; + let flags = FileAttributeFlags::from_bits_truncate(reader.read_u32::<LittleEndian>()?); let reparse_value = reader.read_u32::<LittleEndian>()?; let name_length = reader.read_u8()?; let namespace = reader.read_u8()?; @@ -97,8 +118,6 @@ impl FileNameAttr { Err(_e) => return err::InvalidFilename {}.fail(), }; - let fullname = None; - Ok(FileNameAttr { parent, created, @@ -112,7 +131,6 @@ impl FileNameAttr { name_length, namespace, name, - fullname, }) } } diff --git a/src/entry.rs b/src/entry.rs index 6d69274..9b32030 100644 --- a/src/entry.rs +++ b/src/entry.rs @@ -1,7 +1,8 @@ use crate::enumerator::PathMapping; use crate::err::{self, Result}; +use crate::impl_serialize_for_bitflags; -use crate::{attribute, ReadSeek}; +use crate::attribute; use log::{debug, trace}; use snafu::{ensure, OptionExt, ResultExt}; @@ -22,9 +23,8 @@ use crate::attribute::x30::FileNameAttr; use std::io::Read; use std::io::SeekFrom; use std::io::{Cursor, Seek}; -use std::mem; -const SEQUENCE_NUMBER_STRIDE: u16 = 512; +const SEQUENCE_NUMBER_STRIDE: usize = 512; #[derive(Debug)] pub struct MftEntry { @@ -69,7 +69,6 @@ pub struct EntryHeader { #[serde(skip_serializing)] /// The offset of the first attribute record, in bytes. pub first_attribute_record_offset: u16, - #[serde(serialize_with = "serialize_entry_flags")] pub flags: EntryFlags, #[serde(skip_serializing)] /// Contains the number of bytes of the MFT entry that are in use @@ -95,15 +94,7 @@ bitflags! { } } -pub fn serialize_entry_flags<S>( - item: &EntryFlags, - serializer: S, -) -> ::std::result::Result<S::Ok, S::Error> -where - S: ser::Serializer, -{ - serializer.serialize_str(&format!("{:?}", item)) -} +impl_serialize_for_bitflags! {EntryFlags} impl EntryHeader { pub fn from_reader<R: Read>(reader: &mut R) -> Result<EntryHeader> { @@ -208,13 +199,11 @@ impl MftEntry { // We need to compare each last two bytes each 512-bytes stride with the update_sequence, // And if they match, replace those bytes with the matching bytes from the fixup_sequence. - for (stride_number, fixup_bytes) in (0..number_of_fixups).zip(fixups) { - let sector_start_offset = (stride_number * u32::from(SEQUENCE_NUMBER_STRIDE)) as usize; + for (stride_number, fixup_bytes) in (0_usize..number_of_fixups as usize).zip(fixups) { + let sector_start_offset = stride_number * SEQUENCE_NUMBER_STRIDE; - let end_of_sector_bytes_start_offset = - (sector_start_offset + SEQUENCE_NUMBER_STRIDE as usize) - 2; - let end_of_sector_bytes_end_offset = - sector_start_offset + SEQUENCE_NUMBER_STRIDE as usize; + let end_of_sector_bytes_end_offset = sector_start_offset + SEQUENCE_NUMBER_STRIDE; + let end_of_sector_bytes_start_offset = end_of_sector_bytes_end_offset - 2; let end_of_sector_bytes = &mut buffer[end_of_sector_bytes_start_offset..end_of_sector_bytes_end_offset]; @@ -34,7 +34,7 @@ pub enum Error { fixup_bytes ))] FailedToApplyFixup { - stride_number: u32, + stride_number: usize, end_of_sector_bytes: Vec<u8>, fixup_bytes: Vec<u8>, }, @@ -9,6 +9,7 @@ pub mod enumerator; pub mod err; pub mod mft; +pub(crate) mod macros; pub(crate) mod utils; pub trait ReadSeek: Read + Seek { diff --git a/src/macros.rs b/src/macros.rs new file mode 100644 index 0000000..c643413 --- /dev/null +++ b/src/macros.rs @@ -0,0 +1,13 @@ +#[macro_export] +macro_rules! impl_serialize_for_bitflags { + ($flags: ident) => { + impl serde::ser::Serialize for $flags { + fn serialize<S>(&self, serializer: S) -> ::std::result::Result<S::Ok, S::Error> + where + S: serde::ser::Serializer, + { + serializer.serialize_str(&format!("{:?}", &self)) + } + } + }; +} |