From 7edadd7ca116a1ba8665a2dc0ee8f7d856a5f364 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn-Michael=20Miehe?= <40151420+ldericher@users.noreply.github.com> Date: Tue, 3 Jun 2025 00:07:44 +0000 Subject: [PATCH] bug: wrong chunk offset --- src/main.rs | 2 +- src/sharry/file/chunks.rs | 20 ++++++++++++++------ 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/src/main.rs b/src/main.rs index cc51e17..a4b1347 100644 --- a/src/main.rs +++ b/src/main.rs @@ -35,7 +35,7 @@ fn main() { info!("file: {file:?}"); for chunk in file.chunked(args.chunk_size * 1024 * 1024).seek(0) { - info!("chunk len: {}", chunk.bytes.len()); + info!("chunk: {chunk:?}"); file.upload_chunk(&agent, &alias, &chunk) .unwrap_or_else(|e| { diff --git a/src/sharry/file/chunks.rs b/src/sharry/file/chunks.rs index 1f42fb2..96b800a 100644 --- a/src/sharry/file/chunks.rs +++ b/src/sharry/file/chunks.rs @@ -1,4 +1,5 @@ use std::{ + fmt::Debug, fs::File, io::{Read, Seek, SeekFrom}, path::Path, @@ -34,10 +35,12 @@ impl Iterator for FileChunks<'_> { type Item = Chunk; fn next(&mut self) -> Option { + let offset = self.offset; + let mut f = File::open(self.file_path) .inspect_err(|e| error!("Error opening file: {e}")) .ok()?; - f.seek(SeekFrom::Start(self.offset)).ok()?; + f.seek(SeekFrom::Start(offset)).ok()?; let mut bytes = vec![0; self.chunk_size]; let read_len = (f.read(&mut bytes)) @@ -51,11 +54,7 @@ impl Iterator for FileChunks<'_> { .ok()?; self.offset += read_len; - Some(Self::Item { - offset: self.offset, - bytes, - }) - .filter(|c| !c.bytes.is_empty()) + Some(Self::Item { offset, bytes }).filter(|c| !c.bytes.is_empty()) } } @@ -64,6 +63,15 @@ pub struct Chunk { pub bytes: Vec, } +impl Debug for Chunk { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("Chunk") + .field("offset", &self.offset) + .field("len", &self.bytes.len()) + .finish() + } +} + impl Chunk { pub fn after(&self) -> u64 { let len: u64 = self.bytes.len().try_into().unwrap();