From 9e2d3c25a6f98b2697968950a60a73a08e6e25a7 Mon Sep 17 00:00:00 2001 From: Marcin Czenko Date: Fri, 4 Jul 2025 19:29:56 +0200 Subject: [PATCH] changes the size function in StoreStream to accommodate block alignement required by encryption --- codex/streams/storestream.nim | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/codex/streams/storestream.nim b/codex/streams/storestream.nim index 2e06d39d..094ca434 100644 --- a/codex/streams/storestream.nim +++ b/codex/streams/storestream.nim @@ -55,10 +55,20 @@ proc new*( result.initStream() method `size`*(self: StoreStream): int = - ## The size of a StoreStream is the size of the original dataset, without - ## padding or parity blocks. + ## The size of a StoreStream is the size of the original dataset + ## padded to the boundary of the block - this is because we have + ## encrypted data, and when encrypting we always encrypt the whole block let m = self.manifest - (if m.protected: m.originalDatasetSize else: m.datasetSize).int + let size = if m.protected: m.originalDatasetSize else: m.datasetSize + + ( + if size mod m.blockSize == 0.NBytes: + size + else: + # Pad to the next block boundary + size + (m.blockSize - (size mod m.blockSize)) + ).int # (size div m.blockSize.int + 1) * m.blockSize.int + # (if m.protected: m.originalDatasetSize else: m.datasetSize).int proc `size=`*(self: StoreStream, size: int) {.error: "Setting the size is forbidden".} = discard