FileChunks: save offset instead of chunk index

This commit is contained in:
Jörn-Michael Miehe 2025-06-02 01:34:25 +00:00
parent b999c35965
commit 256f3ce8ba

View file

@ -8,7 +8,7 @@ use log::error;
pub struct FileChunks<'t> { pub struct FileChunks<'t> {
file_path: &'t Path, file_path: &'t Path,
chunk_index: u64, offset: u64,
chunk_size: usize, chunk_size: usize,
} }
@ -16,15 +16,15 @@ impl<'t> FileChunks<'t> {
pub(super) fn new(path: &'t Path, chunk_size: usize) -> Self { pub(super) fn new(path: &'t Path, chunk_size: usize) -> Self {
Self { Self {
file_path: path, file_path: path,
chunk_index: 0, offset: 0,
chunk_size, chunk_size,
} }
} }
pub fn seek(self, chunk_index: u64) -> Self { pub fn seek(self, offset: u64) -> Self {
Self { Self {
file_path: self.file_path, file_path: self.file_path,
chunk_index, offset,
chunk_size: self.chunk_size, chunk_size: self.chunk_size,
} }
} }
@ -34,15 +34,10 @@ impl Iterator for FileChunks<'_> {
type Item = Chunk; type Item = Chunk;
fn next(&mut self) -> Option<Self::Item> { fn next(&mut self) -> Option<Self::Item> {
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) let mut f = File::open(self.file_path)
.inspect_err(|e| error!("Error opening file: {e}")) .inspect_err(|e| error!("Error opening file: {e}"))
.ok()?; .ok()?;
f.seek(SeekFrom::Start(offset)).ok()?; f.seek(SeekFrom::Start(self.offset)).ok()?;
let mut bytes = vec![0; self.chunk_size]; let mut bytes = vec![0; self.chunk_size];
let read_len = (f.read(&mut bytes)) let read_len = (f.read(&mut bytes))
@ -50,9 +45,17 @@ impl Iterator for FileChunks<'_> {
.ok()?; .ok()?;
bytes.truncate(read_len); 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())
} }
} }