From 21b9a027b7c36553ba5446fc000e77b3365eb895 Mon Sep 17 00:00:00 2001 From: Moritz Bitsch Date: Wed, 1 Sep 2021 11:55:10 +0200 Subject: [PATCH] Remove dependency on positioned-io the stdlib already provides all required traits for a seekable reader --- Cargo.toml | 3 +-- src/lib.rs | 26 +++++++++++++++++--------- 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 4997dd0..b4e4cd1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,7 +2,7 @@ name = "e2size" description = "A simple library for ext2 (also ext3 and ext4) size calculation based on the superblock" repository = "https://github.com/madmo/e2size" -version = "0.1.2" +version = "1.0.0" authors = ["Moritz Bitsch "] readme = "README.md" license = "ISC" @@ -10,5 +10,4 @@ edition = "2018" [dependencies] byteorder = "1.4.3" -positioned-io = "0.2.2" thiserror = "1.0.25" diff --git a/src/lib.rs b/src/lib.rs index e5f8905..8ec4279 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,5 @@ use byteorder::{ByteOrder, LittleEndian}; -use positioned_io::ReadAt; +use std::io::{Read, Seek}; use thiserror::Error; #[derive(Error, Debug)] @@ -25,12 +25,14 @@ pub enum E2SizeError { /// Retrieves the size of a ext2/3/4 filesystem in bytes /// /// Reads the superblock (1024 bytes at 1024 byte offset) from reader -pub fn e2size(reader: T) -> std::result::Result +pub fn e2size(mut reader: T) -> std::result::Result where - T: ReadAt, + T: Read, + T: Seek, { let mut superblock = [0u8; 1024]; - reader.read_exact_at(1024, &mut superblock)?; + reader.seek(std::io::SeekFrom::Start(1024))?; + reader.read_exact(&mut superblock)?; let magic = LittleEndian::read_u16(&superblock[0x38..]); if magic != 0xEF53 { @@ -61,7 +63,9 @@ mod tests { LittleEndian::write_u32(&mut superblock[1024 + 0x4..], 256); LittleEndian::write_u32(&mut superblock[1024 + 0x18..], 2); - assert_eq!(e2size(&superblock[..])?, 1048576); + let mut superblock = std::io::Cursor::new(superblock); + assert_eq!(e2size(&mut superblock)?, 1048576); + assert_eq!(e2size(superblock)?, 1048576); Ok(()) } @@ -74,7 +78,8 @@ mod tests { LittleEndian::write_u32(&mut superblock[1024 + 0x4..], 1 << 31); LittleEndian::write_u32(&mut superblock[1024 + 0x18..], 22); - assert_eq!(e2size(&superblock[..])?, 9223372036854775808); + let mut superblock = std::io::Cursor::new(superblock); + assert_eq!(e2size(&mut superblock)?, 9223372036854775808); Ok(()) } @@ -87,7 +92,8 @@ mod tests { LittleEndian::write_u32(&mut superblock[1024 + 0x4..], 1); LittleEndian::write_u32(&mut superblock[1024 + 0x18..], 0); - match e2size(&superblock[..]) { + let mut superblock = std::io::Cursor::new(superblock); + match e2size(&mut superblock) { Err(E2SizeError::BadMagic(e)) => assert_eq!(e, 0xEF52), _ => panic!("not reached!"), }; @@ -103,7 +109,8 @@ mod tests { LittleEndian::write_u32(&mut superblock[1024 + 0x4..], 1); LittleEndian::write_u32(&mut superblock[1024 + 0x18..], 54); - match e2size(&superblock[..]) { + let mut superblock = std::io::Cursor::new(superblock); + match e2size(&mut superblock) { Err(E2SizeError::InvalidBlockSize(_)) => {} Err(e) => panic!("not expected error: {}", e), _ => panic!("not reached!"), @@ -120,7 +127,8 @@ mod tests { LittleEndian::write_u32(&mut superblock[1024 + 0x4..], 1 << 31); LittleEndian::write_u32(&mut superblock[1024 + 0x18..], 23); - match e2size(&superblock[..]) { + let mut superblock = std::io::Cursor::new(superblock); + match e2size(&mut superblock) { Err(E2SizeError::FilesystemTooBig) => {} Err(e) => panic!("not expected error: {}", e), _ => panic!("not reached!"),