2025-08-13 16:17:45 +01:00
|
|
|
/*
|
|
|
|
|
* This file is part of the Aaru Data Preservation Suite.
|
|
|
|
|
* Copyright (c) 2019-2025 Natalia Portillo.
|
|
|
|
|
*
|
|
|
|
|
* This library is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU Lesser General Public License as
|
|
|
|
|
* published by the Free Software Foundation; either version 2.1 of the
|
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This library is distributed in the hope that it will be useful, but
|
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
|
* License along with this library; if not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
#include <stdbool.h>
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
|
|
#include "aaruformat.h"
|
|
|
|
|
#include "internal.h"
|
2025-08-14 00:38:28 +01:00
|
|
|
#include "log.h"
|
2025-08-13 16:17:45 +01:00
|
|
|
|
|
|
|
|
int32_t aaruf_write_sector(void *context, uint64_t sectorAddress, uint8_t *data, uint8_t sectorStatus, uint32_t length)
|
|
|
|
|
{
|
2025-08-14 00:38:28 +01:00
|
|
|
TRACE("Entering aaruf_write_sector(%p, %" PRIu64 ", %p, %u, %u)", context, sectorAddress, data, sectorStatus,
|
|
|
|
|
length);
|
|
|
|
|
|
2025-08-13 16:17:45 +01:00
|
|
|
// Check context is correct AaruFormat context
|
2025-08-14 00:38:28 +01:00
|
|
|
if(context == NULL)
|
|
|
|
|
{
|
|
|
|
|
FATAL("Invalid context");
|
|
|
|
|
|
|
|
|
|
TRACE("Exiting aaruf_write_sector() = AARUF_ERROR_NOT_AARUFORMAT");
|
|
|
|
|
return AARUF_ERROR_NOT_AARUFORMAT;
|
|
|
|
|
}
|
2025-08-13 16:17:45 +01:00
|
|
|
|
|
|
|
|
aaruformatContext *ctx = context;
|
|
|
|
|
|
|
|
|
|
// Not a libaaruformat context
|
2025-08-14 00:38:28 +01:00
|
|
|
if(ctx->magic != AARU_MAGIC)
|
|
|
|
|
{
|
|
|
|
|
FATAL("Invalid context");
|
|
|
|
|
|
|
|
|
|
TRACE("Exiting aaruf_write_sector() = AARUF_ERROR_NOT_AARUFORMAT");
|
|
|
|
|
return AARUF_ERROR_NOT_AARUFORMAT;
|
|
|
|
|
}
|
2025-08-13 16:17:45 +01:00
|
|
|
|
|
|
|
|
// Check we are writing
|
2025-08-14 00:38:28 +01:00
|
|
|
if(!ctx->isWriting)
|
|
|
|
|
{
|
|
|
|
|
FATAL("Trying to write a read-only image");
|
|
|
|
|
|
|
|
|
|
TRACE("Exiting aaruf_write_sector() = AARUF_READ_ONLY");
|
|
|
|
|
return AARUF_READ_ONLY;
|
|
|
|
|
}
|
2025-08-13 16:17:45 +01:00
|
|
|
|
|
|
|
|
// TODO: Check not trying to write beyond media limits
|
|
|
|
|
|
|
|
|
|
// TODO: Check rewinded for disabling checksums
|
|
|
|
|
|
|
|
|
|
// TODO: If optical disc check track
|
|
|
|
|
|
|
|
|
|
// Close current block first
|
|
|
|
|
if(ctx->writingBuffer != NULL &&
|
|
|
|
|
// When sector size changes
|
|
|
|
|
(ctx->currentBlockHeader.sectorSize != length || ctx->currentBlockOffset == 1 << ctx->userDataDdtHeader.dataShift
|
|
|
|
|
// TODO: Implement compression
|
|
|
|
|
))
|
|
|
|
|
{
|
2025-08-14 00:38:28 +01:00
|
|
|
TRACE("Closing current block before writing new data");
|
2025-08-13 16:17:45 +01:00
|
|
|
int error = aaruf_close_current_block(ctx);
|
|
|
|
|
|
2025-08-14 00:38:28 +01:00
|
|
|
if(error != AARUF_STATUS_OK)
|
|
|
|
|
{
|
|
|
|
|
FATAL("Error closing current block: %d", error);
|
|
|
|
|
|
|
|
|
|
TRACE("Exiting aaruf_write_sector() = %d", error);
|
|
|
|
|
return error;
|
|
|
|
|
}
|
2025-08-13 16:17:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// No block set
|
|
|
|
|
if(ctx->writingBufferPosition == 0)
|
|
|
|
|
{
|
2025-08-14 00:38:28 +01:00
|
|
|
TRACE("Creating new writing block");
|
2025-08-13 16:17:45 +01:00
|
|
|
ctx->currentBlockHeader.identifier = DataBlock;
|
|
|
|
|
ctx->currentBlockHeader.type = UserData;
|
|
|
|
|
ctx->currentBlockHeader.compression = None; // TODO: Compression
|
|
|
|
|
ctx->currentBlockHeader.sectorSize = length;
|
|
|
|
|
|
|
|
|
|
// TODO: Optical discs
|
|
|
|
|
uint32_t maxBufferSize = (1 << ctx->userDataDdtHeader.dataShift) * ctx->currentBlockHeader.sectorSize;
|
2025-08-14 00:38:28 +01:00
|
|
|
TRACE("Setting max buffer size to %u bytes", maxBufferSize);
|
|
|
|
|
|
|
|
|
|
TRACE("Allocating memory for writing buffer");
|
|
|
|
|
ctx->writingBuffer = (uint8_t *)malloc(maxBufferSize);
|
|
|
|
|
if(ctx->writingBuffer == NULL)
|
|
|
|
|
{
|
|
|
|
|
FATAL("Could not allocate memory");
|
|
|
|
|
|
|
|
|
|
TRACE("Exiting aaruf_write_sector() = AARUF_ERROR_NOT_ENOUGH_MEMORY");
|
|
|
|
|
return AARUF_ERROR_NOT_ENOUGH_MEMORY;
|
|
|
|
|
}
|
2025-08-13 16:17:45 +01:00
|
|
|
|
2025-08-14 00:38:28 +01:00
|
|
|
TRACE("Initializing CRC64 context");
|
2025-08-13 16:17:45 +01:00
|
|
|
ctx->crc64Context = aaruf_crc64_init();
|
2025-08-13 20:16:42 +01:00
|
|
|
|
|
|
|
|
// Get current file position
|
|
|
|
|
long pos = ftell(ctx->imageStream);
|
2025-08-14 00:38:28 +01:00
|
|
|
TRACE("Saving current file position as next block position: %ld", pos);
|
2025-08-13 20:16:42 +01:00
|
|
|
|
|
|
|
|
// Calculate and save next block aligned position
|
|
|
|
|
ctx->nextBlockPosition =
|
|
|
|
|
pos / (1 << ctx->userDataDdtHeader.blockAlignmentShift) * (1 << ctx->userDataDdtHeader.blockAlignmentShift);
|
2025-08-13 16:17:45 +01:00
|
|
|
}
|
|
|
|
|
|
2025-09-28 15:27:11 +01:00
|
|
|
set_ddt_entry_v2(ctx, sectorAddress, ctx->currentBlockOffset, ctx->nextBlockPosition, sectorStatus);
|
2025-08-14 00:38:28 +01:00
|
|
|
TRACE("Copying data to writing buffer at position %zu", ctx->writingBufferPosition);
|
2025-08-13 16:17:45 +01:00
|
|
|
memcpy(ctx->writingBuffer, data, length);
|
2025-08-14 00:38:28 +01:00
|
|
|
TRACE("Advancing writing buffer position to %zu", ctx->writingBufferPosition + length);
|
2025-08-13 16:17:45 +01:00
|
|
|
ctx->writingBufferPosition += length;
|
2025-08-14 00:38:28 +01:00
|
|
|
TRACE("Updating CRC64");
|
2025-08-13 16:17:45 +01:00
|
|
|
aaruf_crc64_update(ctx->crc64Context, data, length);
|
2025-08-14 00:38:28 +01:00
|
|
|
TRACE("Advancing current block offset to %zu", ctx->currentBlockOffset + 1);
|
2025-08-13 16:17:45 +01:00
|
|
|
ctx->currentBlockOffset++;
|
|
|
|
|
|
2025-08-14 00:38:28 +01:00
|
|
|
TRACE("Exiting aaruf_write_sector() = AARUF_STATUS_OK");
|
2025-08-13 16:17:45 +01:00
|
|
|
return AARUF_STATUS_OK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int32_t aaruf_close_current_block(aaruformatContext *ctx)
|
|
|
|
|
{
|
|
|
|
|
// Not a libaaruformat context
|
|
|
|
|
if(ctx->magic != AARU_MAGIC) return AARUF_ERROR_NOT_AARUFORMAT;
|
|
|
|
|
|
|
|
|
|
// Check we are writing
|
|
|
|
|
if(!ctx->isWriting) return AARUF_READ_ONLY;
|
|
|
|
|
|
|
|
|
|
ctx->currentBlockHeader.length = ctx->currentBlockOffset * ctx->currentBlockHeader.sectorSize;
|
|
|
|
|
aaruf_crc64_final(ctx->crc64Context, &ctx->currentBlockHeader.crc64);
|
|
|
|
|
|
|
|
|
|
switch(ctx->currentBlockHeader.compression)
|
|
|
|
|
{
|
|
|
|
|
case None:
|
|
|
|
|
ctx->currentBlockHeader.cmpCrc64 = ctx->currentBlockHeader.crc64;
|
|
|
|
|
ctx->currentBlockHeader.cmpLength = ctx->currentBlockHeader.length;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TODO: Add to index
|
|
|
|
|
|
|
|
|
|
// Write block header to file
|
|
|
|
|
|
2025-08-13 20:16:42 +01:00
|
|
|
// Move to expected block position
|
|
|
|
|
fseek(ctx->imageStream, ctx->nextBlockPosition, SEEK_SET);
|
2025-08-13 16:17:45 +01:00
|
|
|
|
|
|
|
|
// Write block header
|
|
|
|
|
if(fwrite(&ctx->currentBlockHeader, sizeof(BlockHeader), 1, ctx->imageStream) != 1)
|
|
|
|
|
return AARUF_ERROR_CANNOT_WRITE_BLOCK_HEADER;
|
|
|
|
|
|
|
|
|
|
// Write block data
|
|
|
|
|
if(fwrite(ctx->writingBuffer, ctx->currentBlockHeader.length, 1, ctx->imageStream) != 1)
|
|
|
|
|
return AARUF_ERROR_CANNOT_WRITE_BLOCK_DATA;
|
|
|
|
|
|
|
|
|
|
// Clear values
|
|
|
|
|
free(ctx->writingBuffer);
|
|
|
|
|
ctx->writingBuffer = NULL;
|
|
|
|
|
ctx->currentBlockOffset = 0;
|
|
|
|
|
memset(&ctx->currentBlockHeader, 0, sizeof(BlockHeader));
|
|
|
|
|
aaruf_crc64_free(ctx->crc64Context);
|
|
|
|
|
ctx->writingBufferPosition = 0;
|
|
|
|
|
|
|
|
|
|
return AARUF_STATUS_OK;
|
|
|
|
|
}
|