mirror of
https://github.com/aaru-dps/libaaruformat.git
synced 2025-12-16 19:24:40 +00:00
Update nextBlockPosition calculation for aligned block writing
This commit is contained in:
@@ -1039,6 +1039,11 @@ void set_ddt_multi_level_v2(aaruformatContext *ctx, uint64_t sectorAddress, bool
|
|||||||
return;
|
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);
|
fseek(ctx->imageStream, savedPos, SEEK_SET);
|
||||||
|
|
||||||
// Free the cached table
|
// Free the cached table
|
||||||
|
|||||||
14
src/write.c
14
src/write.c
@@ -112,14 +112,6 @@ int32_t aaruf_write_sector(void *context, uint64_t sectorAddress, uint8_t *data,
|
|||||||
|
|
||||||
TRACE("Initializing CRC64 context");
|
TRACE("Initializing CRC64 context");
|
||||||
ctx->crc64Context = aaruf_crc64_init();
|
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);
|
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)
|
if(fwrite(ctx->writingBuffer, ctx->currentBlockHeader.length, 1, ctx->imageStream) != 1)
|
||||||
return AARUF_ERROR_CANNOT_WRITE_BLOCK_DATA;
|
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
|
// Clear values
|
||||||
free(ctx->writingBuffer);
|
free(ctx->writingBuffer);
|
||||||
ctx->writingBuffer = NULL;
|
ctx->writingBuffer = NULL;
|
||||||
|
|||||||
Reference in New Issue
Block a user