From 256f3ce8ba30462e3bc519b5a105b4d754754db9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn-Michael=20Miehe?= <40151420+ldericher@users.noreply.github.com> Date: Mon, 2 Jun 2025 01:34:25 +0000 Subject: [PATCH] FileChunks: save offset instead of chunk index --- src/sharry/file/chunks.rs | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/src/sharry/file/chunks.rs b/src/sharry/file/chunks.rs index 7f81246..1f42fb2 100644 --- a/src/sharry/file/chunks.rs +++ b/src/sharry/file/chunks.rs @@ -8,7 +8,7 @@ use log::error; pub struct FileChunks<'t> { file_path: &'t Path, - chunk_index: u64, + offset: u64, chunk_size: usize, } @@ -16,15 +16,15 @@ impl<'t> FileChunks<'t> { pub(super) fn new(path: &'t Path, chunk_size: usize) -> Self { Self { file_path: path, - chunk_index: 0, + offset: 0, chunk_size, } } - pub fn seek(self, chunk_index: u64) -> Self { + pub fn seek(self, offset: u64) -> Self { Self { file_path: self.file_path, - chunk_index, + offset, chunk_size: self.chunk_size, } } @@ -34,15 +34,10 @@ impl Iterator for FileChunks<'_> { type Item = Chunk; fn next(&mut self) -> Option { - let offset = self.chunk_index - * u64::try_from(self.chunk_size) - .inspect_err(|e| error!("Error converting to u64: {e}")) - .ok()?; - let mut f = File::open(self.file_path) .inspect_err(|e| error!("Error opening file: {e}")) .ok()?; - f.seek(SeekFrom::Start(offset)).ok()?; + f.seek(SeekFrom::Start(self.offset)).ok()?; let mut bytes = vec![0; self.chunk_size]; let read_len = (f.read(&mut bytes)) @@ -50,9 +45,17 @@ impl Iterator for FileChunks<'_> { .ok()?; bytes.truncate(read_len); - self.chunk_index += 1; + let read_len: u64 = read_len + .try_into() + .inspect_err(|e| error!("Error converting to u64: {e}")) + .ok()?; + self.offset += read_len; - Some(Self::Item { offset, bytes }).filter(|c| !c.bytes.is_empty()) + Some(Self::Item { + offset: self.offset, + bytes, + }) + .filter(|c| !c.bytes.is_empty()) } }