Skip to content

Commit

Permalink
All tests pass
Browse files Browse the repository at this point in the history
  • Loading branch information
fasterthanlime committed Feb 1, 2024
1 parent 1413e7a commit 635dcd8
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 5 deletions.
10 changes: 8 additions & 2 deletions rc-zip-sync/src/read_zip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,14 @@ where
type File = F;

fn read_zip_with_size(&self, size: u64) -> Result<SyncArchive<'_, F>, Error> {
tracing::trace!(%size, "read_zip_with_size");
let mut ar = ArchiveReader::new(size);
loop {
if let Some(offset) = ar.wants_read() {
tracing::trace!(%offset, "read_zip_with_size: wants_read, space len = {}", ar.space().len());
match self.cursor_at(offset).read(ar.space()) {
Ok(read_bytes) => {
tracing::trace!(%read_bytes, "read_zip_with_size: read");
if read_bytes == 0 {
return Err(Error::IO(std::io::ErrorKind::UnexpectedEof.into()));
}
Expand All @@ -56,12 +59,15 @@ where

match ar.process()? {
ArchiveReaderResult::Done(archive) => {
tracing::trace!("read_zip_with_size: done");
return Ok(SyncArchive {
file: self,
archive,
})
});
}
ArchiveReaderResult::Continue => {
tracing::trace!("read_zip_with_size: continue");
}
ArchiveReaderResult::Continue => {}
}
}
}
Expand Down
36 changes: 33 additions & 3 deletions rc-zip/src/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,15 @@ impl ArchiveReader {
/// write to
#[inline]
pub fn space(&mut self) -> &mut [u8] {
self.state.expect_buffer_mut().space()
let buf = self.state.expect_buffer_mut();
trace!(
available_space = buf.available_space(),
"space() | available_space"
);
if buf.available_space() == 0 {
buf.shift();
}
buf.space()
}

/// after having written data to the buffer, use this function
Expand Down Expand Up @@ -190,6 +198,11 @@ impl ArchiveReader {
haystack_size,
} => {
if buffer.read_bytes() < haystack_size {
trace!(
read_bytes = buffer.read_bytes(),
haystack_size,
"ReadEocd | need more data"
);
return Ok(R::Continue);
}

Expand Down Expand Up @@ -440,6 +453,8 @@ impl ArchiveReader {
}
}

/// FIXME: get rid of this wrapper entirely, we can just use `.available_data` from oval::Buffer ?
/// A wrapper around [oval::Buffer] that keeps track of how many bytes we've read since
/// initialization or the last reset.
pub(crate) struct Buffer {
Expand Down Expand Up @@ -482,13 +497,27 @@ impl Buffer {
self.buffer.available_data()
}

/// returns how much free space is available to write to
#[inline]
pub fn available_space(&self) -> usize {
self.buffer.available_space()
}

/// returns a mutable slice with all the available space to
/// write to
#[inline]
pub(crate) fn space(&mut self) -> &mut [u8] {
self.buffer.space()
}

/// moves the data at the beginning of the buffer
///
/// if the position was more than 0, it is now 0
#[inline]
pub fn shift(&mut self) {
self.buffer.shift()
}

/// after having written data to the buffer, use this function
/// to indicate how many bytes were written
///
Expand All @@ -497,7 +526,9 @@ impl Buffer {
/// buffer
#[inline]
pub(crate) fn fill(&mut self, count: usize) -> usize {
self.buffer.fill(count)
let n = self.buffer.fill(count);
self.read_bytes += n as u64;
n
}

/// advances the position tracker
Expand All @@ -508,7 +539,6 @@ impl Buffer {
#[inline]
pub(crate) fn consume(&mut self, size: usize) {
self.buffer.consume(size);
self.read_bytes += size as u64;
}

/// computes an absolute offset, given an offset relative
Expand Down

0 comments on commit 635dcd8

Please sign in to comment.