Skip to content

Commit

Permalink
Pull request FauxFaux#9: Добавлена поддержка работы Ext4 с произвольн…
Browse files Browse the repository at this point in the history
…ым потоком данных

Merge in CRATES/ext4-rs from cases/PKM-1590 to without_state_verification

* commit '3401da1a1a41648c569a5673590223cb8372b9a8':
  [PKM-1590] Tests compilation fixed
  [PKM-1590] Using our own ReadAt trait to be able to work with Read+Seek objects
  • Loading branch information
dmitry-zakablukov committed Mar 23, 2023
2 parents a56e25b + 3401da1 commit 43c6cb6
Show file tree
Hide file tree
Showing 7 changed files with 185 additions and 75 deletions.
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ anyhow = { version = "1.0.58", features = ["backtrace"] }
bitflags = "1"
byteorder = "1"
crc = "1"
positioned-io = "0.2"
thiserror = "1"

[dev-dependencies]
Expand Down
2 changes: 1 addition & 1 deletion examples/walk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ fn main() {
let r = fs::File::open(env::args().nth(1).expect("one argument")).expect("openable file");
let mut options = ext4::Options::default();
options.checksums = ext4::Checksums::Enabled;
let vol = ext4::SuperBlock::new_with_options(r, &options).expect("ext4 volume");
let mut vol = ext4::SuperBlock::new_with_options(r, &options).expect("ext4 volume");
let root = vol.root().expect("root");
vol.walk(&root, "/", &mut |_, path, _, _| {
println!("{}", path);
Expand Down
15 changes: 8 additions & 7 deletions src/extents.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ use std::io;

use anyhow::ensure;
use anyhow::Error;
use positioned_io::ReadAt;

use crate::{
assumption_failed, map_lib_error_to_io, read_le16, read_le32, Crypto, InnerReader,
MetadataCrypto,
MetadataCrypto, ReadAt,
};

#[derive(Debug)]
Expand All @@ -20,7 +19,7 @@ struct Extent {
}

pub struct TreeReader<'a, R: ReadAt, C: Crypto, M: MetadataCrypto> {
inner: &'a InnerReader<R, M>,
inner: &'a mut InnerReader<R, M>,
pos: u64,
len: u64,
block_size: u32,
Expand All @@ -31,7 +30,7 @@ pub struct TreeReader<'a, R: ReadAt, C: Crypto, M: MetadataCrypto> {

impl<'a, R: ReadAt, C: Crypto, M: MetadataCrypto> TreeReader<'a, R, C, M> {
pub fn new(
inner: &'a InnerReader<R, M>,
inner: &'a mut InnerReader<R, M>,
block_size: u32,
size: u64,
core: [u8; crate::INODE_CORE_SIZE],
Expand All @@ -55,7 +54,7 @@ impl<'a, R: ReadAt, C: Crypto, M: MetadataCrypto> TreeReader<'a, R, C, M> {
}

fn create(
inner: &'a InnerReader<R, M>,
inner: &'a mut InnerReader<R, M>,
block_size: u32,
size: u64,
extents: Vec<Extent>,
Expand Down Expand Up @@ -320,9 +319,11 @@ mod tests {
let size = 4 + 4 * 2;
let crypto = NoneCrypto {};
let metadata_crypto = NoneCrypto {};
let data = InnerReader::new((0..255u8).collect::<Vec<u8>>(), metadata_crypto);

let cursor = std::io::Cursor::new((0..255u8).collect::<Vec<u8>>());
let mut data = InnerReader::new(cursor, metadata_crypto);
let mut reader = TreeReader::create(
&data,
&mut data,
4,
u64::try_from(size).expect("infallible u64 conversion"),
vec![
Expand Down
25 changes: 13 additions & 12 deletions src/inner_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ use std::io;
use std::io::ErrorKind;

use anyhow::Error;
use positioned_io::ReadAt;

use crate::ReadAt;

pub trait MetadataCrypto {
fn decrypt(&self, page: &mut [u8], page_addr: u64) -> Result<(), Error>;
Expand All @@ -22,15 +23,15 @@ impl<R: ReadAt, M: MetadataCrypto> InnerReader<R, M> {
}
}

pub fn read_at_without_decrypt(&self, pos: u64, buf: &mut [u8]) -> io::Result<usize> {
pub fn read_at_without_decrypt(&mut self, pos: u64, buf: &mut [u8]) -> io::Result<usize> {
self.inner.read_at(pos, buf)
}

fn decrypt<F: Fn(u64, &mut [u8]) -> io::Result<usize>>(
&self,
fn decrypt<F: FnMut(&mut InnerReader<R, M>, u64, &mut [u8]) -> io::Result<usize>>(
&mut self,
pos: u64,
buf: &mut [u8],
read_fn: F,
mut read_fn: F,
) -> io::Result<usize> {
let mut read_offset = 0;
const CHUNK_SIZE: usize = 0x1000;
Expand All @@ -51,7 +52,7 @@ impl<R: ReadAt, M: MetadataCrypto> InnerReader<R, M> {

for block_buffer in buffer.chunks_mut(CHUNK_SIZE) {
let address = aligned_address + read_offset as u64;
read_fn(address, block_buffer)?;
read_fn(self, address, block_buffer)?;

self.metadata_crypto
.decrypt(block_buffer, address)
Expand All @@ -67,15 +68,15 @@ impl<R: ReadAt, M: MetadataCrypto> InnerReader<R, M> {
}

impl<R: ReadAt, M: MetadataCrypto> ReadAt for InnerReader<R, M> {
fn read_at(&self, pos: u64, buf: &mut [u8]) -> io::Result<usize> {
self.decrypt(pos, buf, |offset, buffer| {
self.read_at_without_decrypt(offset, buffer)
fn read_at(&mut self, pos: u64, buf: &mut [u8]) -> io::Result<usize> {
self.decrypt(pos, buf, |reader, offset, buffer| {
reader.read_at_without_decrypt(offset, buffer)
})
}

fn read_exact_at(&self, pos: u64, buf: &mut [u8]) -> io::Result<()> {
self.decrypt(pos, buf, |offset, buffer| {
self.inner.read_exact_at(offset, buffer)?;
fn read_exact_at(&mut self, pos: u64, buf: &mut [u8]) -> io::Result<()> {
self.decrypt(pos, buf, |reader, offset, buffer| {
reader.inner.read_exact_at(offset, buffer)?;
Ok(0)
})?;

Expand Down
Loading

0 comments on commit 43c6cb6

Please sign in to comment.