Skip to content

Commit

Permalink
fixed chunk offset calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
ph0llux committed Oct 7, 2023
1 parent eb737fb commit 705f06b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
17 changes: 12 additions & 5 deletions src/lib/io/zffwriter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -565,9 +564,15 @@ impl<R: Read> ZffWriter<R> {
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
Expand Down Expand Up @@ -754,9 +759,11 @@ impl<R: Read> ZffWriter<R> {

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 => {
Expand Down
4 changes: 3 additions & 1 deletion src/lib/segment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ impl<R: Read + Seek> Segment<R> {
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.
Expand Down Expand Up @@ -283,7 +285,7 @@ fn get_first_chunknumber(map: &BTreeMap<u64, u64>, 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()))
Expand Down

0 comments on commit 705f06b

Please sign in to comment.