Skip to content

Commit

Permalink
dbgln
Browse files Browse the repository at this point in the history
  • Loading branch information
fasterthanlime committed Dec 18, 2024
1 parent 2353489 commit 01179db
Showing 1 changed file with 29 additions and 3 deletions.
32 changes: 29 additions & 3 deletions rc-zip-monoio/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,55 @@ use rc_zip::{
parse::Archive,
};

macro_rules! dbgln {
($($tt:tt)*) => {
eprintln!($($tt)*);
}
}

pub async fn read_zip_from_file(file: &File) -> Result<Archive, Error> {
dbgln!("Starting to read zip from file...");
let meta = file.metadata().await?;
let size = meta.len();
let mut buf = vec![0u8; 128 * 1024];
dbgln!("File size: {size}");
let mut buf = vec![0u8; 256 * 1024].into_boxed_slice();

let mut fsm = ArchiveFsm::new(size);
loop {
dbgln!("Entering loop...");
if let Some(offset) = fsm.wants_read() {
dbgln!("FSM wants read at offset: {offset}");
let dst = fsm.space();
let max_read = dst.len().min(buf.len());
dbgln!(
"Calculated max_read: {max_read}, dst.len: {}, buf.len: {}",
dst.len(),
buf.len()
);
let slice = IoBufMut::slice_mut(buf, 0..max_read);

let (res, slice) = file.read_at(slice, offset).await;
dbgln!("Read result: {:?}", res);
let n = res?;
dbgln!("Number of bytes read: {n}");
(dst[..n]).copy_from_slice(&slice[..n]);

fsm.fill(n);
dbgln!("FSM filled with {n} bytes");
buf = slice.into_inner();
} else {
dbgln!("FSM does not want to read more data.");
}

fsm = match fsm.process()? {
FsmResult::Done(archive) => break Ok(archive),
FsmResult::Continue(fsm) => fsm,
FsmResult::Done(archive) => {
dbgln!("FSM processing done.");
break Ok(archive);
}
FsmResult::Continue(fsm) => {
dbgln!("FSM wants to continue.");
fsm
}
}
}
}

0 comments on commit 01179db

Please sign in to comment.