From bbe333090d84745f99a7ee117baab3fa7d115b16 Mon Sep 17 00:00:00 2001 From: Colin Finck Date: Fri, 14 Jan 2022 07:23:58 +0100 Subject: Apply suggestions from clippy. --- examples/ntfs-shell/main.rs | 2 +- src/attribute_value/attribute_list_non_resident.rs | 5 ++ src/attribute_value/mod.rs | 11 ++++ src/attribute_value/non_resident.rs | 61 ++++++++++++---------- src/attribute_value/resident.rs | 5 ++ src/boot_sector.rs | 5 +- src/index_entry.rs | 2 +- src/index_record.rs | 2 +- src/ntfs.rs | 4 +- src/structured_values/attribute_list.rs | 2 +- src/structured_values/file_name.rs | 4 +- src/structured_values/volume_name.rs | 2 +- 12 files changed, 68 insertions(+), 37 deletions(-) diff --git a/examples/ntfs-shell/main.rs b/examples/ntfs-shell/main.rs index e362250..99e6e04 100644 --- a/examples/ntfs-shell/main.rs +++ b/examples/ntfs-shell/main.rs @@ -181,7 +181,7 @@ fn attr_print_attribute<'n>( let data_run = data_run?; let instance = format!("{}{}", data_run_prefix, i); let start = data_run.data_position().unwrap_or(0); - let length = data_run.len(); + let length = data_run.allocated_size(); println!( "{:<10} | {:<20} | {:<8} | {:>13} | {:#18x} | {:>13} |", diff --git a/src/attribute_value/attribute_list_non_resident.rs b/src/attribute_value/attribute_list_non_resident.rs index cc4db33..afcc327 100644 --- a/src/attribute_value/attribute_list_non_resident.rs +++ b/src/attribute_value/attribute_list_non_resident.rs @@ -65,6 +65,11 @@ impl<'n, 'f> NtfsAttributeListNonResidentAttributeValue<'n, 'f> { self.stream_state.data_position() } + /// Returns `true` if the non-resident attribute value contains no data. + pub fn is_empty(&self) -> bool { + self.len() == 0 + } + /// Returns the total length of the non-resident attribute value data, in bytes. pub fn len(&self) -> u64 { self.data_size diff --git a/src/attribute_value/mod.rs b/src/attribute_value/mod.rs index 95c56ba..652ccdf 100644 --- a/src/attribute_value/mod.rs +++ b/src/attribute_value/mod.rs @@ -20,6 +20,7 @@ use crate::traits::NtfsReadSeek; /// Reader that abstracts over all attribute value types, returned by [`NtfsAttribute::value`]. /// /// [`NtfsAttribute::value`]: crate::NtfsAttribute::value +#[allow(clippy::large_enum_variant)] #[derive(Clone, Debug)] pub enum NtfsAttributeValue<'n, 'f> { /// A resident attribute value (which is entirely contained in the NTFS File Record). @@ -52,6 +53,11 @@ impl<'n, 'f> NtfsAttributeValue<'n, 'f> { } } + /// Returns `true` if the attribute value contains no data. + pub fn is_empty(&self) -> bool { + self.len() == 0 + } + /// Returns the total length of the attribute value data, in bytes. pub fn len(&self) -> u64 { match self { @@ -123,6 +129,11 @@ where self.value } + /// Returns `true` if the attribute value contains no data. + pub fn is_empty(&self) -> bool { + self.len() == 0 + } + /// Returns the total length of the attribute value, in bytes. pub fn len(&self) -> u64 { self.value.len() diff --git a/src/attribute_value/non_resident.rs b/src/attribute_value/non_resident.rs index f1b79e8..9749a7f 100644 --- a/src/attribute_value/non_resident.rs +++ b/src/attribute_value/non_resident.rs @@ -85,6 +85,11 @@ impl<'n, 'f> NtfsNonResidentAttributeValue<'n, 'f> { NtfsDataRuns::new(self.ntfs, self.data, self.position) } + /// Returns `true` if the non-resident attribute value contains no data. + pub fn is_empty(&self) -> bool { + self.len() == 0 + } + /// Returns the total length of the non-resident attribute value data, in bytes. pub fn len(&self) -> u64 { self.stream_state.data_size() @@ -222,7 +227,12 @@ where self.value } - /// Returns the total length of the attribute value, in bytes. + /// Returns `true` if the non-resident attribute value contains no data. + pub fn is_empty(&self) -> bool { + self.len() == 0 + } + + /// Returns the total length of the non-resident attribute value, in bytes. pub fn len(&self) -> u64 { self.value.len() } @@ -436,25 +446,25 @@ impl NtfsDataRun { }) } + /// Returns the allocated size of the Data Run, in bytes. + pub fn allocated_size(&self) -> u64 { + self.allocated_size + } + /// Returns the absolute current data seek position within the filesystem, in bytes. /// This may be `None` if: /// * The current seek position is outside the valid range, or /// * The Data Run is a "sparse" Data Run pub fn data_position(&self) -> Option { - if self.position > 0 && self.stream_position < self.len() { + if self.position > 0 && self.stream_position < self.allocated_size() { Some(self.position + self.stream_position) } else { None } } - /// Returns the allocated size of the Data Run, in bytes. - pub fn len(&self) -> u64 { - self.allocated_size - } - pub(crate) fn remaining_len(&self) -> u64 { - self.len().saturating_sub(self.stream_position) + self.allocated_size().saturating_sub(self.stream_position) } } @@ -470,25 +480,26 @@ impl NtfsReadSeek for NtfsDataRun { let bytes_to_read = usize::min(buf.len(), self.remaining_len() as usize); let work_slice = &mut buf[..bytes_to_read]; - if self.position == 0 { + let bytes_read = if self.position == 0 { // This is a sparse Data Run. work_slice.fill(0); + work_slice.len() } else { // This Data Run contains "real" data. // We have already performed all necessary sanity checks above, so we can just unwrap here. fs.seek(SeekFrom::Start(self.data_position().unwrap()))?; - fs.read(work_slice)?; - } + fs.read(work_slice)? + }; - self.stream_position += bytes_to_read as u64; - Ok(bytes_to_read) + self.stream_position += bytes_read as u64; + Ok(bytes_read) } fn seek(&mut self, _fs: &mut T, pos: SeekFrom) -> Result where T: Read + Seek, { - let length = self.len(); + let length = self.allocated_size(); seek_contiguous(&mut self.stream_position, length, pos) } @@ -561,11 +572,9 @@ impl StreamState { // Seek data_size + n bytes from the very beginning. return Ok(SeekFrom::Start(bytes_to_seek)); } - } else { - if let Some(bytes_to_seek) = data_size.checked_sub(n.wrapping_neg() as u64) { - // Seek data_size + n bytes (with n being negative) from the very beginning. - return Ok(SeekFrom::Start(bytes_to_seek)); - } + } else if let Some(bytes_to_seek) = data_size.checked_sub(n.wrapping_neg() as u64) { + // Seek data_size + n bytes (with n being negative) from the very beginning. + return Ok(SeekFrom::Start(bytes_to_seek)); } } SeekFrom::Current(n) => { @@ -576,13 +585,11 @@ impl StreamState { // data runs from the very beginning. return Ok(SeekFrom::Current(n)); } - } else { - if let Some(bytes_to_seek) = - self.stream_position().checked_sub(n.wrapping_neg() as u64) - { - // Seek stream_position + n bytes (with n being negative) from the very beginning. - return Ok(SeekFrom::Start(bytes_to_seek)); - } + } else if let Some(bytes_to_seek) = + self.stream_position().checked_sub(n.wrapping_neg() as u64) + { + // Seek stream_position + n bytes (with n being negative) from the very beginning. + return Ok(SeekFrom::Start(bytes_to_seek)); } } } @@ -610,7 +617,7 @@ impl StreamState { }; // Have we already seeked past the size of the Data Run? - if data_run.stream_position() >= data_run.len() { + if data_run.stream_position() >= data_run.allocated_size() { return Ok(false); } diff --git a/src/attribute_value/resident.rs b/src/attribute_value/resident.rs index 85c4a69..65d8878 100644 --- a/src/attribute_value/resident.rs +++ b/src/attribute_value/resident.rs @@ -50,6 +50,11 @@ impl<'f> NtfsResidentAttributeValue<'f> { } } + /// Returns `true` if the resident attribute value contains no data. + pub fn is_empty(&self) -> bool { + self.len() == 0 + } + /// Returns the total length of the resident attribute value data, in bytes. pub fn len(&self) -> u64 { self.data.len() as u64 diff --git a/src/boot_sector.rs b/src/boot_sector.rs index 2fc3140..ad60a66 100644 --- a/src/boot_sector.rs +++ b/src/boot_sector.rs @@ -4,6 +4,7 @@ use crate::error::{NtfsError, Result}; use crate::types::Lcn; use binread::BinRead; +use core::ops::Range; use memoffset::offset_of; // Sources: @@ -76,6 +77,8 @@ impl BiosParameterBlock { /// Exponents > 31 (2^31 = 2 GiB) would make no sense, exceed a u32, and must be outright denied. const MAXIMUM_SIZE_INFO_EXPONENT: u32 = 31; + const SIZE_INFO_RANGE: Range = MINIMUM_SIZE_INFO_EXPONENT..MAXIMUM_SIZE_INFO_EXPONENT; + let cluster_size = self.cluster_size()?; if size_info > 0 { @@ -90,7 +93,7 @@ impl BiosParameterBlock { // The size field denotes a binary exponent after negation. let exponent = (-size_info) as u32; - if exponent < MINIMUM_SIZE_INFO_EXPONENT || exponent >= MAXIMUM_SIZE_INFO_EXPONENT { + if !SIZE_INFO_RANGE.contains(&exponent) { return Err(NtfsError::InvalidRecordSizeInfo { size_info, cluster_size, diff --git a/src/index_entry.rs b/src/index_entry.rs index 4fbcd1f..d88087d 100644 --- a/src/index_entry.rs +++ b/src/index_entry.rs @@ -314,7 +314,7 @@ where } } - pub(crate) fn data<'d>(&'d self) -> &'d [u8] { + pub(crate) fn data(&self) -> &[u8] { &self.data } } diff --git a/src/index_record.rs b/src/index_record.rs index 4f7bc42..46a0172 100644 --- a/src/index_record.rs +++ b/src/index_record.rs @@ -83,7 +83,7 @@ impl<'n> NtfsIndexRecord<'n> { /// Returns an iterator over all entries of this Index Record (cf. [`NtfsIndexEntry`]). /// /// [`NtfsIndexEntry`]: crate::NtfsIndexEntry - pub fn entries<'r, E>(&'r self) -> Result> + pub fn entries(&self) -> Result> where E: NtfsIndexEntryType, { diff --git a/src/ntfs.rs b/src/ntfs.rs index ea56716..973bd2b 100644 --- a/src/ntfs.rs +++ b/src/ntfs.rs @@ -87,7 +87,7 @@ impl Ntfs { // The MFT may be split into multiple data runs, referenced by its $DATA attribute. // We therefore read it just like any other non-resident attribute value. // However, this code assumes that the MFT does not have an Attribute List! - let mft = NtfsFile::new(&self, fs, self.mft_position, 0)?; + let mft = NtfsFile::new(self, fs, self.mft_position, 0)?; let mft_data_attribute = mft .attributes_raw() .find(|attribute| { @@ -107,7 +107,7 @@ impl Ntfs { .data_position() .ok_or(NtfsError::InvalidFileRecordNumber { file_record_number })?; - NtfsFile::new(&self, fs, position, file_record_number) + NtfsFile::new(self, fs, position, file_record_number) } /// Returns the size of a File Record of this NTFS filesystem, in bytes. diff --git a/src/structured_values/attribute_list.rs b/src/structured_values/attribute_list.rs index 636eb98..4a8c661 100644 --- a/src/structured_values/attribute_list.rs +++ b/src/structured_values/attribute_list.rs @@ -228,7 +228,7 @@ impl NtfsAttributeListEntry { } /// Gets the attribute name and returns it wrapped in an [`NtfsString`]. - pub fn name<'s>(&'s self) -> NtfsString<'s> { + pub fn name(&self) -> NtfsString { NtfsString(&self.name) } diff --git a/src/structured_values/file_name.rs b/src/structured_values/file_name.rs index 31d8c71..09467eb 100644 --- a/src/structured_values/file_name.rs +++ b/src/structured_values/file_name.rs @@ -204,7 +204,7 @@ impl NtfsFileName { } /// Gets the file name and returns it wrapped in an [`NtfsString`]. - pub fn name<'s>(&'s self) -> NtfsString<'s> { + pub fn name(&self) -> NtfsString { NtfsString(&self.name) } @@ -243,7 +243,7 @@ impl NtfsFileName { if total_size > data_size { return Err(NtfsError::InvalidStructuredValueSize { - position: position, + position, ty: NtfsAttributeType::FileName, expected: data_size, actual: total_size, diff --git a/src/structured_values/volume_name.rs b/src/structured_values/volume_name.rs index 55dbb8c..1e4d2a9 100644 --- a/src/structured_values/volume_name.rs +++ b/src/structured_values/volume_name.rs @@ -64,7 +64,7 @@ impl NtfsVolumeName { } /// Gets the volume name and returns it wrapped in an [`NtfsString`]. - pub fn name<'s>(&'s self) -> NtfsString<'s> { + pub fn name(&self) -> NtfsString { NtfsString(&self.name) } -- cgit v1.2.3