diff --git a/src/ddt/ddt_v2.c b/src/ddt/ddt_v2.c index aed55dd..f6cc3d6 100644 --- a/src/ddt/ddt_v2.c +++ b/src/ddt/ddt_v2.c @@ -1039,6 +1039,11 @@ void set_ddt_multi_level_v2(aaruformatContext *ctx, uint64_t sectorAddress, bool return; } + // Update nextBlockPosition to ensure future blocks don't overwrite the DDT + uint64_t ddtTotalSize = sizeof(DdtHeader2) + ddtHeader.length; + ctx->nextBlockPosition = (endOfFile + ddtTotalSize + alignmentMask) & ~alignmentMask; + TRACE("Updated nextBlockPosition after DDT write to %" PRIu64, ctx->nextBlockPosition); + fseek(ctx->imageStream, savedPos, SEEK_SET); // Free the cached table diff --git a/src/write.c b/src/write.c index a286228..42da07f 100644 --- a/src/write.c +++ b/src/write.c @@ -112,14 +112,6 @@ int32_t aaruf_write_sector(void *context, uint64_t sectorAddress, uint8_t *data, TRACE("Initializing CRC64 context"); ctx->crc64Context = aaruf_crc64_init(); - - // Get current file position - long pos = ftell(ctx->imageStream); - TRACE("Saving current file position as next block position: %ld", pos); - - // Calculate and save next block aligned position - ctx->nextBlockPosition = - pos / (1 << ctx->userDataDdtHeader.blockAlignmentShift) * (1 << ctx->userDataDdtHeader.blockAlignmentShift); } TRACE("Copying data to writing buffer at position %zu", ctx->writingBufferPosition); @@ -176,6 +168,12 @@ int32_t aaruf_close_current_block(aaruformatContext *ctx) if(fwrite(ctx->writingBuffer, ctx->currentBlockHeader.length, 1, ctx->imageStream) != 1) return AARUF_ERROR_CANNOT_WRITE_BLOCK_DATA; + // Update nextBlockPosition to point to the next available aligned position + uint64_t blockTotalSize = sizeof(BlockHeader) + ctx->currentBlockHeader.cmpLength; + uint64_t alignmentMask = (1ULL << ctx->userDataDdtHeader.blockAlignmentShift) - 1; + ctx->nextBlockPosition = (ctx->nextBlockPosition + blockTotalSize + alignmentMask) & ~alignmentMask; + TRACE("Updated nextBlockPosition to %" PRIu64, ctx->nextBlockPosition); + // Clear values free(ctx->writingBuffer); ctx->writingBuffer = NULL;