From c5b693e7aa0fee2fe4f980334c98da56ff7c4108 Mon Sep 17 00:00:00 2001 From: joshieDo <93316087+joshieDo@users.noreply.github.com> Date: Wed, 12 Jun 2024 11:42:19 +0200 Subject: [PATCH] ensure offset file creation is valid --- crates/storage/nippy-jar/src/writer.rs | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/crates/storage/nippy-jar/src/writer.rs b/crates/storage/nippy-jar/src/writer.rs index da6c13cc6c2c..695fd6642e54 100644 --- a/crates/storage/nippy-jar/src/writer.rs +++ b/crates/storage/nippy-jar/src/writer.rs @@ -130,10 +130,19 @@ impl NippyJarWriter { } let mut offsets_file = OpenOptions::new().read(true).write(true).open(offsets)?; + if is_created { + let mut buf = Vec::with_capacity(1 + OFFSET_SIZE_BYTES as usize); - // First byte of the offset file is the size of one offset in bytes - offsets_file.write_all(&[OFFSET_SIZE_BYTES])?; - offsets_file.seek(SeekFrom::End(0))?; + // First byte of the offset file is the size of one offset in bytes + buf.write_all(&[OFFSET_SIZE_BYTES])?; + + // The last offset should always represent the data file len, which is 0 on + // creation. + buf.write_all(&[0; OFFSET_SIZE_BYTES as usize])?; + + offsets_file.write_all(&buf)?; + offsets_file.seek(SeekFrom::End(0))?; + } Ok((data_file, offsets_file, is_created)) }