CommitGraphWriter: Assert written bytes

The final size of the commit-graph is known before-hand. As a
safety-net, assert the written size matches the expected value.

Change-Id: Ib0828a7cce5bacb33f6325ee3910f4eebd95eb8c
This commit is contained in:
Ivan Frade 2023-09-05 10:17:52 -07:00
parent 13c8dacae5
commit f90f0717a0
1 changed files with 12 additions and 0 deletions

View File

@ -122,6 +122,7 @@ public Stats write(@NonNull ProgressMonitor monitor,
}
List<ChunkHeader> chunks = createChunks(stats);
long expectedSize = calculateExpectedSize(chunks);
long writeCount = 256 + 2 * graphCommits.size()
+ graphCommits.getExtraEdgeCnt();
monitor.beginTask(
@ -135,6 +136,11 @@ public Stats write(@NonNull ProgressMonitor monitor,
writeChunkLookup(out, chunks);
writeChunks(monitor, out, chunks);
writeCheckSum(out);
if (expectedSize != out.length()) {
throw new IllegalStateException(String.format(
"Commit-graph: expected %d bytes but out has %d bytes", //$NON-NLS-1$
expectedSize, out.length()));
}
} catch (InterruptedIOException e) {
throw new IOException(JGitText.get().commitGraphWritingCancelled,
e);
@ -168,6 +174,12 @@ private List<ChunkHeader> createChunks(Stats stats)
return chunks;
}
private static long calculateExpectedSize(List<ChunkHeader> chunks) {
int chunkLookup = (chunks.size() + 1) * CHUNK_LOOKUP_WIDTH;
long chunkContent = chunks.stream().mapToLong(c -> c.size).sum();
return /* header */ 8 + chunkLookup + chunkContent + /* CRC */ 20;
}
private void writeHeader(CancellableDigestOutputStream out, int numChunks)
throws IOException {
byte[] headerBuffer = new byte[8];