diff --git a/src/ddt/ddt_v2.c b/src/ddt/ddt_v2.c index 76364cc..a16b450 100644 --- a/src/ddt/ddt_v2.c +++ b/src/ddt/ddt_v2.c @@ -903,10 +903,16 @@ void set_ddt_multi_level_v2(aaruformatContext *ctx, uint64_t sectorAddress, bool // Step 3: Write the currently in-memory cached secondary level table to the end of the file if(ctx->cachedDdtOffset != 0) { + // Close the current data block first + if(ctx->writingBuffer != NULL) aaruf_close_current_block(ctx); + // Get current position and seek to end of file currentPos = ftell(ctx->imageStream); fseek(ctx->imageStream, 0, SEEK_END); endOfFile = ftell(ctx->imageStream); + endOfFile = endOfFile / (1 << ctx->userDataDdtHeader.blockAlignmentShift) * + (1 << ctx->userDataDdtHeader.blockAlignmentShift); + fseek(ctx->imageStream, endOfFile, SEEK_SET); // Prepare DDT header for the cached table memset(&ddtHeader, 0, sizeof(DdtHeader2)); diff --git a/src/write.c b/src/write.c index 1bcb2d6..4d7cb44 100644 --- a/src/write.c +++ b/src/write.c @@ -85,6 +85,8 @@ int32_t aaruf_write_sector(void *context, uint64_t sectorAddress, uint8_t *data, } } + set_ddt_entry_v2(ctx, sectorAddress, ctx->currentBlockOffset, ctx->nextBlockPosition, sectorStatus); + // No block set if(ctx->writingBufferPosition == 0) { @@ -120,7 +122,6 @@ int32_t aaruf_write_sector(void *context, uint64_t sectorAddress, uint8_t *data, pos / (1 << ctx->userDataDdtHeader.blockAlignmentShift) * (1 << ctx->userDataDdtHeader.blockAlignmentShift); } - set_ddt_entry_v2(ctx, sectorAddress, ctx->currentBlockOffset, ctx->nextBlockPosition, sectorStatus); TRACE("Copying data to writing buffer at position %zu", ctx->writingBufferPosition); memcpy(ctx->writingBuffer, data, length); TRACE("Advancing writing buffer position to %zu", ctx->writingBufferPosition + length);