Skip to content

Commit

Permalink
feat: new file::Index::highest_offset() method (#279)
Browse files Browse the repository at this point in the history
With it it's simpler to figure out from where to read trailing
checksums.
  • Loading branch information
Byron committed Dec 20, 2021
1 parent 7a9e628 commit d0fab1e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
6 changes: 6 additions & 0 deletions git-chunk/src/file/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ mod error {
ChunkSizeOutOfBounds { offset: crate::file::Offset, file_length: u64 } {
display("The chunk offset {} went past the file of length {} - was it truncated?", offset, file_length)
}
NonIncrementalChunkOffsets {
display("All chunk offsets must be incrementing.")
}
DuplicateChunk(kind: crate::Kind) {
display("The chunk of kind {:#016x} was encountered more than once", kind)
}
Expand Down Expand Up @@ -78,6 +81,9 @@ impl file::Index {
file_length: data_len,
});
}
if next_offset <= offset {
return Err(Error::NonIncrementalChunkOffsets);
}
chunks.push(index::Entry {
kind,
offset: Range {
Expand Down
6 changes: 6 additions & 0 deletions git-chunk/src/file/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,10 @@ impl Index {
let offset = self.offset_by_kind(kind, name)?;
Ok(&data[crate::into_usize_range(offset).ok_or(data_by_kind::Error::FileTooLarge)?])
}

/// Return the end offset lf the last chunk, which is the highest offset as well.
/// It's definitely available as we have one or more chunks.
pub fn highest_offset(&self) -> crate::file::Offset {
self.chunks.last().expect("at least one chunk").offset.end
}
}

0 comments on commit d0fab1e

Please sign in to comment.