diff options
author | Colin Finck <colin@reactos.org> | 2021-05-01 18:39:14 +0300 |
---|---|---|
committer | Colin Finck <colin@reactos.org> | 2021-05-01 18:39:14 +0300 |
commit | 9ff4d41d5a57c15f7f299f48ed8c2c75dd7d5fcc (patch) | |
tree | e93f88c912e1aafcf3c3b72e744cb92135413e2b | |
parent | 08f063df590c457860831e10c9106f56e492e3c5 (diff) |
Implement `NtfsString::read_from_fs` to simplify several functions.
-rw-r--r-- | src/attribute.rs | 13 | ||||
-rw-r--r-- | src/string.rs | 24 | ||||
-rw-r--r-- | src/structured_values/file_name.rs | 15 | ||||
-rw-r--r-- | src/structured_values/volume_name.rs | 15 |
4 files changed, 29 insertions, 38 deletions
diff --git a/src/attribute.rs b/src/attribute.rs index 3af1d01..6536e39 100644 --- a/src/attribute.rs +++ b/src/attribute.rs @@ -214,19 +214,8 @@ impl NtfsAttribute { where T: Read + Seek, { - let name_length = self.name_length(); - if buf.len() < name_length { - return Err(NtfsError::BufferTooSmall { - expected: name_length, - actual: buf.len(), - }); - } - let name_position = self.position + self.header.name_offset as u64; - fs.seek(SeekFrom::Start(name_position))?; - fs.read_exact(&mut buf[..name_length])?; - - Ok(NtfsString(&buf[..name_length])) + NtfsString::read_from_fs(fs, name_position, self.name_length(), buf) } pub fn structured_value<T>(&self, fs: &mut T) -> Result<NtfsStructuredValue> diff --git a/src/string.rs b/src/string.rs index 18acc4d..1b226c8 100644 --- a/src/string.rs +++ b/src/string.rs @@ -1,7 +1,9 @@ // Copyright 2021 Colin Finck <colin@reactos.org> // SPDX-License-Identifier: GPL-2.0-or-later +use crate::error::{NtfsError, Result}; use alloc::string::String; +use binread::io::{Read, Seek, SeekFrom}; use core::char; use core::cmp::Ordering; use core::convert::TryInto; @@ -65,6 +67,28 @@ impl<'a> NtfsString<'a> { self.0.len() } + pub(crate) fn read_from_fs<T>( + fs: &mut T, + position: u64, + length: usize, + buf: &'a mut [u8], + ) -> Result<Self> + where + T: Read + Seek, + { + if buf.len() < length { + return Err(NtfsError::BufferTooSmall { + expected: length, + actual: buf.len(), + }); + } + + fs.seek(SeekFrom::Start(position))?; + fs.read_exact(&mut buf[..length])?; + + Ok(Self(&buf[..length])) + } + /// Attempts to convert `self` to an owned `String`. /// Returns `Some(String)` if all characters could be converted successfully or `None` if a decoding error occurred. pub fn to_string_checked(&self) -> Option<String> { diff --git a/src/structured_values/file_name.rs b/src/structured_values/file_name.rs index 7a2edf6..71b0fd8 100644 --- a/src/structured_values/file_name.rs +++ b/src/structured_values/file_name.rs @@ -7,7 +7,7 @@ use crate::error::{NtfsError, Result}; use crate::string::NtfsString; use crate::structured_values::NtfsFileAttributeFlags; use crate::time::NtfsTime; -use binread::io::{Read, Seek, SeekFrom}; +use binread::io::{Read, Seek}; use binread::{BinRead, BinReaderExt}; use core::mem; use enumn::N; @@ -126,18 +126,7 @@ impl NtfsFileName { where T: Read + Seek, { - let name_length = self.name_length(); - if buf.len() < name_length { - return Err(NtfsError::BufferTooSmall { - expected: name_length, - actual: buf.len(), - }); - } - - fs.seek(SeekFrom::Start(self.name_position))?; - fs.read_exact(&mut buf[..name_length])?; - - Ok(NtfsString(&buf[..name_length])) + NtfsString::read_from_fs(fs, self.name_position, self.name_length(), buf) } } diff --git a/src/structured_values/volume_name.rs b/src/structured_values/volume_name.rs index 72bd3bc..2f70ea7 100644 --- a/src/structured_values/volume_name.rs +++ b/src/structured_values/volume_name.rs @@ -5,7 +5,7 @@ use crate::attribute::NtfsAttributeType; use crate::attribute_value::NtfsAttributeValueAttached; use crate::error::{NtfsError, Result}; use crate::string::NtfsString; -use binread::io::{Read, Seek, SeekFrom}; +use binread::io::{Read, Seek}; use core::mem; /// The smallest VolumeName attribute has a name containing just a single character. @@ -67,17 +67,6 @@ impl NtfsVolumeName { where T: Read + Seek, { - let name_length = self.name_length(); - if buf.len() < name_length { - return Err(NtfsError::BufferTooSmall { - expected: name_length, - actual: buf.len(), - }); - } - - fs.seek(SeekFrom::Start(self.name_position))?; - fs.read_exact(&mut buf[..name_length])?; - - Ok(NtfsString(&buf[..name_length])) + NtfsString::read_from_fs(fs, self.name_position, self.name_length(), buf) } } |