Remove dependency on positioned-io

the stdlib already provides all required traits for a seekable reader
This commit is contained in:
Moritz Bitsch 2021-09-01 11:55:10 +02:00
parent b4edacd81e
commit 21b9a027b7
2 changed files with 18 additions and 11 deletions

View file

@ -2,7 +2,7 @@
name = "e2size" name = "e2size"
description = "A simple library for ext2 (also ext3 and ext4) size calculation based on the superblock" description = "A simple library for ext2 (also ext3 and ext4) size calculation based on the superblock"
repository = "https://github.com/madmo/e2size" repository = "https://github.com/madmo/e2size"
version = "0.1.2" version = "1.0.0"
authors = ["Moritz Bitsch <moritz@h6t.eu>"] authors = ["Moritz Bitsch <moritz@h6t.eu>"]
readme = "README.md" readme = "README.md"
license = "ISC" license = "ISC"
@ -10,5 +10,4 @@ edition = "2018"
[dependencies] [dependencies]
byteorder = "1.4.3" byteorder = "1.4.3"
positioned-io = "0.2.2"
thiserror = "1.0.25" thiserror = "1.0.25"

View file

@ -1,5 +1,5 @@
use byteorder::{ByteOrder, LittleEndian}; use byteorder::{ByteOrder, LittleEndian};
use positioned_io::ReadAt; use std::io::{Read, Seek};
use thiserror::Error; use thiserror::Error;
#[derive(Error, Debug)] #[derive(Error, Debug)]
@ -25,12 +25,14 @@ pub enum E2SizeError {
/// Retrieves the size of a ext2/3/4 filesystem in bytes /// Retrieves the size of a ext2/3/4 filesystem in bytes
/// ///
/// Reads the superblock (1024 bytes at 1024 byte offset) from reader /// Reads the superblock (1024 bytes at 1024 byte offset) from reader
pub fn e2size<T>(reader: T) -> std::result::Result<u64, E2SizeError> pub fn e2size<T>(mut reader: T) -> std::result::Result<u64, E2SizeError>
where where
T: ReadAt, T: Read,
T: Seek,
{ {
let mut superblock = [0u8; 1024]; 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..]); let magic = LittleEndian::read_u16(&superblock[0x38..]);
if magic != 0xEF53 { if magic != 0xEF53 {
@ -61,7 +63,9 @@ mod tests {
LittleEndian::write_u32(&mut superblock[1024 + 0x4..], 256); LittleEndian::write_u32(&mut superblock[1024 + 0x4..], 256);
LittleEndian::write_u32(&mut superblock[1024 + 0x18..], 2); 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(()) Ok(())
} }
@ -74,7 +78,8 @@ mod tests {
LittleEndian::write_u32(&mut superblock[1024 + 0x4..], 1 << 31); LittleEndian::write_u32(&mut superblock[1024 + 0x4..], 1 << 31);
LittleEndian::write_u32(&mut superblock[1024 + 0x18..], 22); 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(()) Ok(())
} }
@ -87,7 +92,8 @@ mod tests {
LittleEndian::write_u32(&mut superblock[1024 + 0x4..], 1); LittleEndian::write_u32(&mut superblock[1024 + 0x4..], 1);
LittleEndian::write_u32(&mut superblock[1024 + 0x18..], 0); 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), Err(E2SizeError::BadMagic(e)) => assert_eq!(e, 0xEF52),
_ => panic!("not reached!"), _ => panic!("not reached!"),
}; };
@ -103,7 +109,8 @@ mod tests {
LittleEndian::write_u32(&mut superblock[1024 + 0x4..], 1); LittleEndian::write_u32(&mut superblock[1024 + 0x4..], 1);
LittleEndian::write_u32(&mut superblock[1024 + 0x18..], 54); 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(E2SizeError::InvalidBlockSize(_)) => {}
Err(e) => panic!("not expected error: {}", e), Err(e) => panic!("not expected error: {}", e),
_ => panic!("not reached!"), _ => panic!("not reached!"),
@ -120,7 +127,8 @@ mod tests {
LittleEndian::write_u32(&mut superblock[1024 + 0x4..], 1 << 31); LittleEndian::write_u32(&mut superblock[1024 + 0x4..], 1 << 31);
LittleEndian::write_u32(&mut superblock[1024 + 0x18..], 23); 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(E2SizeError::FilesystemTooBig) => {}
Err(e) => panic!("not expected error: {}", e), Err(e) => panic!("not expected error: {}", e),
_ => panic!("not reached!"), _ => panic!("not reached!"),