From 705f06b881e3ea69f2d5f2235f910250164979de Mon Sep 17 00:00:00 2001 From: ph0llux Date: Sat, 7 Oct 2023 22:30:06 +0200 Subject: [PATCH] fixed chunk offset calculation --- src/lib/io/zffwriter.rs | 17 ++++++++++++----- src/lib/segment.rs | 4 +++- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/src/lib/io/zffwriter.rs b/src/lib/io/zffwriter.rs index 93f8923..a9a8ae1 100644 --- a/src/lib/io/zffwriter.rs +++ b/src/lib/io/zffwriter.rs @@ -23,7 +23,6 @@ use crate::{ FILE_EXTENSION_INITIALIZER, ERROR_MISMATCH_ZFF_VERSION, ERROR_MISSING_SEGMENT_MAIN_FOOTER, - ERROR_INVALID_OPTION_ZFFEXTEND, ERROR_INVALID_OPTION_ZFFCREATE, }; use crate::{ @@ -565,9 +564,15 @@ impl ZffWriter { output.seek(SeekFrom::End(-8))?; let footer_offset = u64::decode_directly(output)?; output.seek(SeekFrom::Start(footer_offset))?; - SegmentFooter::decode_directly(output)? + let segment_footer = SegmentFooter::decode_directly(output)?; + //move the seek position to the footer start, to overwrite the old footer. + output.seek(SeekFrom::Start(footer_offset))?; + segment_footer + } else { - SegmentFooter::new_empty(DEFAULT_FOOTER_VERSION_SEGMENT_FOOTER) + let mut segment_footer = SegmentFooter::new_empty(DEFAULT_FOOTER_VERSION_SEGMENT_FOOTER); + segment_footer.first_chunk_number = self.current_object_encoder.current_chunk_number(); + segment_footer }; // prepare output @@ -754,9 +759,11 @@ impl ZffWriter { current_offset = match self.write_next_segment(&mut output_file, seek_value, &mut chunk_map, extend) { Ok(written_bytes) => { + //adds the seek value to the written bytes extend = false; - seek_value = 0; - written_bytes + current_offset = seek_value + written_bytes; + seek_value = current_offset; + current_offset }, Err(e) => match e.get_kind() { ZffErrorKind::ReadEOF => { diff --git a/src/lib/segment.rs b/src/lib/segment.rs index bcb104a..aaa2bec 100644 --- a/src/lib/segment.rs +++ b/src/lib/segment.rs @@ -93,6 +93,8 @@ impl Segment { DEFAULT_LENGTH_HEADER_IDENTIFIER as u64 + //skip the chunk header identifier DEFAULT_LENGTH_VALUE_HEADER_LENGTH as u64 + //skip the header length value 1 + // skip the ChunkMap header version + 8 + // skip the length of the map + 8 + // skip the chunk number itself ((chunk_number - first_chunk_number_of_map) * 2 * 8); //skip the other chunk entries //go to the appropriate chunk map. @@ -283,7 +285,7 @@ fn get_first_chunknumber(map: &BTreeMap, chunk_number: u64, first_segm let mut range = map.range(..chunk_number).rev(); match range.next() { Some((&k, _)) => Ok(k + 1), - None => if chunk_number > first_segment_chunk_number { + None => if chunk_number >= first_segment_chunk_number { Ok(first_segment_chunk_number) } else { Err(ZffError::new(ZffErrorKind::ValueNotInMap, chunk_number.to_string()))