From 36a845d24a47e121c9390ba1daa0cdc6180f000a Mon Sep 17 00:00:00 2001 From: "Alexis (Poliorcetics) Bourget" Date: Mon, 12 Jun 2023 11:40:30 +0200 Subject: [PATCH 1/2] deps: Update bitflags and memoffset --- Cargo.toml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 0cc0304..8b54f98 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,11 +17,11 @@ categories = ["filesystem", "no-std", "os::windows-apis", "parser-implementation arrayvec = { version = "0.7.2", default-features = false } binread = { version = "2.2.0", features = ["const_generics"], default-features = false } byteorder = { version = "1.4.3", default-features = false } -bitflags = "1.3.2" +bitflags = "2.3.1" derive_more = "0.99.17" displaydoc = { version = "0.2.3", default-features = false } enumn = "0.1.3" -memoffset = "0.8.0" +memoffset = "0.9.0" strum_macros = "0.24.0" time = { version = "0.3.9", features = ["large-dates", "macros"], default-features = false, optional = true } From 578c53153ded8bf69e3726726de66e3df256e931 Mon Sep 17 00:00:00 2001 From: Colin Finck Date: Tue, 13 Jun 2023 07:00:00 +0200 Subject: [PATCH 2/2] Implement suggested traits and use `Display` over `Debug` in ntfs-shell. bitflags 2.x does not implement `Debug` automatically anymore. Docs suggest using `derive`: https://github.com/bitflags/bitflags/blob/395736882672dcfaaf764817af70a9db209f8003/README.md#example However, the implementation we get via `derive` now prepends the struct name. This is not the case for the suggested `Display` implementation, which is why my ntfs-shell example now uses `Display` for the file attribute flags. --- examples/ntfs-shell/main.rs | 2 +- src/attribute.rs | 9 ++++++++- src/file.rs | 8 ++++++++ src/index_entry.rs | 9 ++++++++- src/structured_values/mod.rs | 9 +++++++++ src/structured_values/volume_information.rs | 9 +++++++++ 6 files changed, 43 insertions(+), 3 deletions(-) diff --git a/examples/ntfs-shell/main.rs b/examples/ntfs-shell/main.rs index e848e5f..2a2ba13 100644 --- a/examples/ntfs-shell/main.rs +++ b/examples/ntfs-shell/main.rs @@ -365,7 +365,7 @@ fn fileinfo_std(attribute: NtfsAttribute) -> Result<()> { let std_info = attribute.resident_structured_value::()?; - println!("{:34}{:?}", "Attributes:", std_info.file_attributes()); + println!("{:34}{}", "Attributes:", std_info.file_attributes()); let atime = OffsetDateTime::from(std_info.access_time()) .format(TIME_FORMAT) diff --git a/src/attribute.rs b/src/attribute.rs index 9b4d177..32c88ed 100644 --- a/src/attribute.rs +++ b/src/attribute.rs @@ -2,8 +2,8 @@ // SPDX-License-Identifier: MIT OR Apache-2.0 use core::iter::FusedIterator; -use core::mem; use core::ops::Range; +use core::{fmt, mem}; use binread::io::{Read, Seek}; use bitflags::bitflags; @@ -49,6 +49,7 @@ struct NtfsAttributeHeader { bitflags! { /// Flags returned by [`NtfsAttribute::flags`]. + #[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)] pub struct NtfsAttributeFlags: u16 { /// The attribute value is compressed. const COMPRESSED = 0x0001; @@ -59,6 +60,12 @@ bitflags! { } } +impl fmt::Display for NtfsAttributeFlags { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + fmt::Display::fmt(&self.0, f) + } +} + /// On-disk structure of the extra header of an NTFS Attribute that has a resident value. #[repr(C, packed)] struct NtfsResidentAttributeHeader { diff --git a/src/file.rs b/src/file.rs index c8ef0f3..fdaa0d8 100644 --- a/src/file.rs +++ b/src/file.rs @@ -2,6 +2,7 @@ // SPDX-License-Identifier: MIT OR Apache-2.0 use core::cmp::Ordering; +use core::fmt; use core::num::NonZeroU64; use alloc::vec; @@ -95,6 +96,7 @@ struct FileRecordHeader { bitflags! { /// Flags returned by [`NtfsFile::flags`]. + #[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)] pub struct NtfsFileFlags: u16 { /// Record is in use. const IN_USE = 0x0001; @@ -103,6 +105,12 @@ bitflags! { } } +impl fmt::Display for NtfsFileFlags { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + fmt::Display::fmt(&self.0, f) + } +} + /// A single NTFS File Record. /// /// These records are denoted via a `FILE` signature on the filesystem. diff --git a/src/index_entry.rs b/src/index_entry.rs index 779f1ec..f37773f 100644 --- a/src/index_entry.rs +++ b/src/index_entry.rs @@ -3,8 +3,8 @@ use core::iter::FusedIterator; use core::marker::PhantomData; -use core::mem; use core::ops::Range; +use core::{fmt, mem}; use alloc::vec::Vec; use binread::io::{Read, Seek}; @@ -43,6 +43,7 @@ struct IndexEntryHeader { bitflags! { /// Flags returned by [`NtfsIndexEntry::flags`]. + #[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)] pub struct NtfsIndexEntryFlags: u8 { /// This Index Entry points to a sub-node. const HAS_SUBNODE = 0x01; @@ -51,6 +52,12 @@ bitflags! { } } +impl fmt::Display for NtfsIndexEntryFlags { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + fmt::Display::fmt(&self.0, f) + } +} + #[derive(Clone, Debug)] pub(crate) struct IndexEntryRange where diff --git a/src/structured_values/mod.rs b/src/structured_values/mod.rs index 7f05194..ee51277 100644 --- a/src/structured_values/mod.rs +++ b/src/structured_values/mod.rs @@ -12,6 +12,8 @@ mod standard_information; mod volume_information; mod volume_name; +use core::fmt; + pub use attribute_list::*; pub use file_name::*; pub use index_allocation::*; @@ -36,6 +38,7 @@ bitflags! { /// Returned by [`NtfsStandardInformation::file_attributes`] and [`NtfsFileName::file_attributes`]. /// /// [`NtfsAttribute`]: crate::attribute::NtfsAttribute + #[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)] pub struct NtfsFileAttributeFlags: u32 { /// File is marked read-only. const READ_ONLY = 0x0001; @@ -71,6 +74,12 @@ bitflags! { } } +impl fmt::Display for NtfsFileAttributeFlags { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + fmt::Display::fmt(&self.0, f) + } +} + /// Trait implemented by every NTFS attribute structured value. pub trait NtfsStructuredValue<'n, 'f>: Sized { const TY: NtfsAttributeType; diff --git a/src/structured_values/volume_information.rs b/src/structured_values/volume_information.rs index 6ee5a9d..b51580d 100644 --- a/src/structured_values/volume_information.rs +++ b/src/structured_values/volume_information.rs @@ -1,6 +1,8 @@ // Copyright 2021-2022 Colin Finck // SPDX-License-Identifier: MIT OR Apache-2.0 +use core::fmt; + use binread::io::{Cursor, Read, Seek}; use binread::{BinRead, BinReaderExt}; use bitflags::bitflags; @@ -26,6 +28,7 @@ struct VolumeInformationData { bitflags! { /// Flags returned by [`NtfsVolumeInformation::flags`]. + #[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)] pub struct NtfsVolumeFlags: u16 { /// The volume needs to be checked by `chkdsk`. const IS_DIRTY = 0x0001; @@ -39,6 +42,12 @@ bitflags! { } } +impl fmt::Display for NtfsVolumeFlags { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + fmt::Display::fmt(&self.0, f) + } +} + /// Structure of a $VOLUME_INFORMATION attribute. /// /// This attribute is only used by the top-level $Volume file and contains general information about the filesystem.