From d62e3119c202aec28193fe6be0cdc0f09bdc5b2c Mon Sep 17 00:00:00 2001 From: Natalia Portillo Date: Wed, 13 Aug 2025 20:16:42 +0100 Subject: [PATCH] Add slog logging support and update error handling in various modules --- .gitmodules | 3 ++ 3rdparty/slog | 1 + CMakeLists.txt | 17 +++++++++++- include/aaruformat/context.h | 1 + include/log.h | 43 ++++++++++++++++++++++++++++ src/blocks/checksum.c | 52 +++++++++++++++++----------------- src/blocks/data.c | 21 +++++++------- src/blocks/dump.c | 54 ++++++++++++++++++------------------ src/blocks/metadata.c | 41 +++++++++++++-------------- src/blocks/optical.c | 21 +++++++------- src/ddt/ddt_v1.c | 43 ++++++++++++++-------------- src/ddt/ddt_v2.c | 30 ++++++++++---------- src/open.c | 47 ++++++++++++++++++------------- src/write.c | 16 ++++++----- 14 files changed, 228 insertions(+), 162 deletions(-) create mode 160000 3rdparty/slog create mode 100644 include/log.h diff --git a/.gitmodules b/.gitmodules index 6a132a5..6b05097 100644 --- a/.gitmodules +++ b/.gitmodules @@ -7,3 +7,6 @@ [submodule "3rdparty/uthash"] path = 3rdparty/uthash url = https://github.com/troydhanson/uthash +[submodule "3rdparty/slog"] + path = 3rdparty/slog + url = https://github.com/kala13x/slog diff --git a/3rdparty/slog b/3rdparty/slog new file mode 160000 index 0000000..bc857db --- /dev/null +++ b/3rdparty/slog @@ -0,0 +1 @@ +Subproject commit bc857dbcd924d9d2af7adb3b4c1464a0de84757a diff --git a/CMakeLists.txt b/CMakeLists.txt index ea01c92..028c64b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -122,7 +122,8 @@ add_library(aaruformat SHARED include/aaruformat/consts.h include/aaruformat/enu src/options.c src/create.c src/time.c - src/write.c) + src/write.c + include/log.h) include_directories(include include/aaruformat) @@ -176,8 +177,19 @@ elseif(OpenSSL_FOUND) TARGET_LINK_LIBRARIES_WHOLE_ARCHIVE(aaruformat ${OPENSSL_CRYPTO_LIBRARY}) endif() +set(CMAKE_POSITION_INDEPENDENT_CODE ON) + +# Add slog submodule +add_subdirectory(3rdparty/slog) + include_directories(include 3rdparty/uthash/src) +# Include slog headers +include_directories(aaruformat 3rdparty/slog/src) + +# Enable TRACE and slog output +add_compile_definitions(aaruformat ENABLE_TRACE USE_SLOG) + include(CheckLibraryExists) check_library_exists(m log "" HAVE_LIB_M) @@ -185,5 +197,8 @@ if(HAVE_LIB_M) TARGET_LINK_LIBRARIES_WHOLE_ARCHIVE(aaruformat m) endif() +# Link slog +target_link_libraries(aaruformat slog) + add_subdirectory(tests) add_subdirectory(tool) \ No newline at end of file diff --git a/include/aaruformat/context.h b/include/aaruformat/context.h index 3d0c2bc..6613ad4 100644 --- a/include/aaruformat/context.h +++ b/include/aaruformat/context.h @@ -122,6 +122,7 @@ typedef struct aaruformatContext int currentBlockOffset; crc64_ctx *crc64Context; int writingBufferPosition; + long nextBlockPosition; } aaruformatContext; typedef struct DumpHardwareEntriesWithData diff --git a/include/log.h b/include/log.h new file mode 100644 index 0000000..be02428 --- /dev/null +++ b/include/log.h @@ -0,0 +1,43 @@ +// +// Created by claunia on 13/8/25. +// + +#ifndef LIBAARUFORMAT_LOG_H +#define LIBAARUFORMAT_LOG_H + +#include +#include + +// Uncomment to enable tracing +// #define ENABLE_TRACE +// Uncomment to use slog instead of stderr +// #define USE_SLOG + +#ifdef ENABLE_TRACE +#ifdef USE_SLOG +#include "slog.h" + +#define TRACE(fmt, ...) slog_trace(fmt, ##__VA_ARGS__) +#else +#define TRACE(fmt, ...) fprintf(stderr, "[TRACE] %s:%d:%s(): " fmt "\n", __FILE__, __LINE__, __func__, ##__VA_ARGS__) +#endif +#else +#define TRACE(fmt, ...) +#endif + +#include +#include + +#ifdef ENABLE_FATAL +#ifdef USE_SLOG +#include "slog.h" + +#define FATAL(fmt, ...) slog_fatal(fmt, ##__VA_ARGS__) +#else +#define FATAL(fmt, ...) fprintf(stderr, "[FATAL] %s:%d:%s(): " fmt "\n", __FILE__, __LINE__, __func__, ##__VA_ARGS__) +#endif +#else +#define FATAL(fmt, ...) +#endif + +#endif // LIBAARUFORMAT_LOG_H diff --git a/src/blocks/checksum.c b/src/blocks/checksum.c index 0c0c3ed..8c9f48d 100644 --- a/src/blocks/checksum.c +++ b/src/blocks/checksum.c @@ -1,20 +1,20 @@ /* -* 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 . -*/ + * 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 . + */ #include #include @@ -22,15 +22,16 @@ #include #include "aaruformat.h" +#include "log.h" void process_checksum_block(aaruformatContext *ctx, const IndexEntry *entry) { - int pos = 0; - size_t readBytes = 0; + int pos = 0; + size_t readBytes = 0; ChecksumHeader checksum_header; ChecksumEntry const *checksum_entry = NULL; - uint8_t *data = NULL; - int j = 0; + uint8_t *data = NULL; + int j = 0; // Check if the context and image stream are valid if(ctx == NULL || ctx->imageStream == NULL) @@ -43,7 +44,7 @@ void process_checksum_block(aaruformatContext *ctx, const IndexEntry *entry) pos = fseek(ctx->imageStream, entry->offset, SEEK_SET); if(pos < 0 || ftell(ctx->imageStream) != entry->offset) { - fprintf(stderr, "libaaruformat: Could not seek to %" PRIu64 " as indicated by index entry...\n", entry->offset); + FATAL("Could not seek to %" PRIu64 " as indicated by index entry...\n", entry->offset); return; } @@ -54,15 +55,14 @@ void process_checksum_block(aaruformatContext *ctx, const IndexEntry *entry) if(readBytes != sizeof(ChecksumHeader)) { memset(&checksum_header, 0, sizeof(ChecksumHeader)); - fprintf(stderr, "libaaruformat: Could not read checksums block header, continuing...\n"); + FATAL("Could not read checksums block header, continuing...\n"); return; } if(checksum_header.identifier != ChecksumBlock) { memset(&checksum_header, 0, sizeof(ChecksumHeader)); - fprintf(stderr, "libaaruformat: Incorrect identifier for checksum block at position %" PRIu64 "\n", - entry->offset); + FATAL("Incorrect identifier for checksum block at position %" PRIu64 "\n", entry->offset); } data = (uint8_t *)malloc(checksum_header.length); @@ -70,7 +70,7 @@ void process_checksum_block(aaruformatContext *ctx, const IndexEntry *entry) if(data == NULL) { memset(&checksum_header, 0, sizeof(ChecksumHeader)); - fprintf(stderr, "libaaruformat: Could not allocate memory for checksum block, continuing...\n"); + FATAL("Could not allocate memory for checksum block, continuing...\n"); return; } @@ -80,7 +80,7 @@ void process_checksum_block(aaruformatContext *ctx, const IndexEntry *entry) { memset(&checksum_header, 0, sizeof(ChecksumHeader)); free(data); - fprintf(stderr, "libaaruformat: Could not read checksums block, continuing...\n"); + FATAL("Could not read checksums block, continuing...\n"); return; } diff --git a/src/blocks/data.c b/src/blocks/data.c index 1bc9c3e..032f092 100644 --- a/src/blocks/data.c +++ b/src/blocks/data.c @@ -22,6 +22,7 @@ #include #include "aaruformat.h" +#include "log.h" #include "uthash.h" // Process data blocks found while opening an AaruFormat file @@ -51,7 +52,7 @@ int32_t process_data_block(aaruformatContext *ctx, IndexEntry *entry) pos = fseek(ctx->imageStream, entry->offset, SEEK_SET); if(pos < 0 || ftell(ctx->imageStream) != entry->offset) { - fprintf(stderr, "libaaruformat: Could not seek to %" PRIu64 " as indicated by index entry...\n", entry->offset); + FATAL("Could not seek to %" PRIu64 " as indicated by index entry...\n", entry->offset); return AARUF_ERROR_CANNOT_READ_BLOCK; } @@ -65,7 +66,7 @@ int32_t process_data_block(aaruformatContext *ctx, IndexEntry *entry) if(readBytes != sizeof(BlockHeader)) { - fprintf(stderr, "libaaruformat: Could not read block header at %" PRIu64 "\n", entry->offset); + FATAL("Could not read block header at %" PRIu64 "\n", entry->offset); return AARUF_STATUS_OK; } @@ -82,22 +83,21 @@ int32_t process_data_block(aaruformatContext *ctx, IndexEntry *entry) if(blockHeader.identifier != entry->blockType) { - fprintf(stderr, "libaaruformat: Incorrect identifier for data block at position %" PRIu64 "\n", entry->offset); + TRACE("Incorrect identifier for data block at position %" PRIu64 "\n", entry->offset); return AARUF_STATUS_OK; } if(blockHeader.type != entry->dataType) { - fprintf(stderr, - "libaaruformat: Expected block with data type %4.4s at position %" PRIu64 + TRACE("Expected block with data type %4.4s at position %" PRIu64 " but found data type %4.4s\n", (char *)&entry->blockType, entry->offset, (char *)&blockHeader.type); return AARUF_STATUS_OK; } - fprintf(stderr, "libaaruformat: Found data block with type %4.4s at position %" PRIu64 "\n", + TRACE("Found data block with type %4.4s at position %" PRIu64 "\n", (char *)&entry->blockType, entry->offset); if(blockHeader.compression == Lzma || blockHeader.compression == LzmaClauniaSubchannelTransform) @@ -209,7 +209,7 @@ int32_t process_data_block(aaruformatContext *ctx, IndexEntry *entry) } else { - fprintf(stderr, "libaaruformat: Found unknown compression type %d, continuing...\n", blockHeader.compression); + TRACE("Found unknown compression type %d, continuing...\n", blockHeader.compression); return AARUF_STATUS_OK; } @@ -223,8 +223,7 @@ int32_t process_data_block(aaruformatContext *ctx, IndexEntry *entry) if(crc64 != blockHeader.crc64) { - fprintf(stderr, - "libaaruformat: Incorrect CRC found: 0x%" PRIx64 " found, expected 0x%" PRIx64 ", continuing...\n", + TRACE("Incorrect CRC found: 0x%" PRIx64 " found, expected 0x%" PRIx64 ", continuing...\n", crc64, blockHeader.crc64); return AARUF_STATUS_OK; @@ -275,7 +274,7 @@ int32_t process_data_block(aaruformatContext *ctx, IndexEntry *entry) if(mediaTag == NULL) { - fprintf(stderr, "libaaruformat: Cannot allocate memory for media tag entry.\n"); + TRACE("Cannot allocate memory for media tag entry.\n"); break; } memset(mediaTag, 0, sizeof(mediaTagEntry)); @@ -288,7 +287,7 @@ int32_t process_data_block(aaruformatContext *ctx, IndexEntry *entry) if(oldMediaTag != NULL) { - fprintf(stderr, "libaaruformat: Replaced media tag with type %d\n", oldMediaTag->type); + TRACE("Replaced media tag with type %d\n", oldMediaTag->type); free(oldMediaTag->data); free(oldMediaTag); oldMediaTag = NULL; diff --git a/src/blocks/dump.c b/src/blocks/dump.c index d489cb7..67be4bc 100644 --- a/src/blocks/dump.c +++ b/src/blocks/dump.c @@ -22,6 +22,7 @@ #include #include "aaruformat.h" +#include "log.h" void process_dumphw_block(aaruformatContext *ctx, const IndexEntry *entry) { @@ -34,7 +35,7 @@ void process_dumphw_block(aaruformatContext *ctx, const IndexEntry *entry) // Check if the context and image stream are valid if(ctx == NULL || ctx->imageStream == NULL) { - fprintf(stderr, "Invalid context or image stream.\n"); + FATAL("Invalid context or image stream.\n"); return; } @@ -42,7 +43,7 @@ void process_dumphw_block(aaruformatContext *ctx, const IndexEntry *entry) pos = fseek(ctx->imageStream, entry->offset, SEEK_SET); if(pos < 0 || ftell(ctx->imageStream) != entry->offset) { - fprintf(stderr, "libaaruformat: Could not seek to %" PRIu64 " as indicated by index entry...\n", entry->offset); + FATAL("Could not seek to %" PRIu64 " as indicated by index entry...\n", entry->offset); return; } @@ -53,14 +54,14 @@ void process_dumphw_block(aaruformatContext *ctx, const IndexEntry *entry) if(readBytes != sizeof(DumpHardwareHeader)) { memset(&ctx->dumpHardwareHeader, 0, sizeof(DumpHardwareHeader)); - fprintf(stderr, "libaaruformat: Could not read dump hardware block header, continuing...\n"); + TRACE("Could not read dump hardware block header, continuing...\n"); return; } if(ctx->dumpHardwareHeader.identifier != DumpHardwareBlock) { memset(&ctx->dumpHardwareHeader, 0, sizeof(DumpHardwareHeader)); - fprintf(stderr, "libaaruformat: Incorrect identifier for data block at position %" PRIu64 "\n", entry->offset); + TRACE("Incorrect identifier for data block at position %" PRIu64 "\n", entry->offset); } data = (uint8_t *)malloc(ctx->dumpHardwareHeader.length); @@ -68,7 +69,7 @@ void process_dumphw_block(aaruformatContext *ctx, const IndexEntry *entry) if(data == NULL) { memset(&ctx->dumpHardwareHeader, 0, sizeof(DumpHardwareHeader)); - fprintf(stderr, "libaaruformat: Could not allocate memory for dump hardware block, continuing...\n"); + TRACE("Could not allocate memory for dump hardware block, continuing...\n"); return; } @@ -84,9 +85,8 @@ void process_dumphw_block(aaruformatContext *ctx, const IndexEntry *entry) if(crc64 != ctx->dumpHardwareHeader.crc64) { free(data); - fprintf(stderr, - "libaaruformat: Incorrect CRC found: 0x%" PRIx64 " found, expected 0x%" PRIx64 ", continuing...\n", - crc64, ctx->dumpHardwareHeader.crc64); + TRACE("Incorrect CRC found: 0x%" PRIx64 " found, expected 0x%" PRIx64 ", continuing...\n", crc64, + ctx->dumpHardwareHeader.crc64); return; } } @@ -100,7 +100,7 @@ void process_dumphw_block(aaruformatContext *ctx, const IndexEntry *entry) if(ctx->dumpHardwareEntriesWithData == NULL) { memset(&ctx->dumpHardwareHeader, 0, sizeof(DumpHardwareHeader)); - fprintf(stderr, "libaaruformat: Could not allocate memory for dump hardware block, continuing...\n"); + TRACE("Could not allocate memory for dump hardware block, continuing...\n"); return; } @@ -113,7 +113,7 @@ void process_dumphw_block(aaruformatContext *ctx, const IndexEntry *entry) if(readBytes != sizeof(DumpHardwareEntry)) { ctx->dumpHardwareHeader.entries = e; - fprintf(stderr, "libaaruformat: Could not read dump hardware block entry, continuing...\n"); + TRACE("Could not read dump hardware block entry, continuing...\n"); break; } @@ -133,8 +133,8 @@ void process_dumphw_block(aaruformatContext *ctx, const IndexEntry *entry) { free(ctx->dumpHardwareEntriesWithData[e].manufacturer); ctx->dumpHardwareEntriesWithData[e].entry.manufacturerLength = 0; - fprintf(stderr, "libaaruformat: Could not read dump hardware block entry manufacturer, " - "continuing...\n"); + TRACE("Could not read dump hardware block entry manufacturer, " + "continuing...\n"); } } } @@ -154,7 +154,7 @@ void process_dumphw_block(aaruformatContext *ctx, const IndexEntry *entry) { free(ctx->dumpHardwareEntriesWithData[e].model); ctx->dumpHardwareEntriesWithData[e].entry.modelLength = 0; - fprintf(stderr, "libaaruformat: Could not read dump hardware block entry model, continuing...\n"); + TRACE("Could not read dump hardware block entry model, continuing...\n"); } } } @@ -175,8 +175,8 @@ void process_dumphw_block(aaruformatContext *ctx, const IndexEntry *entry) { free(ctx->dumpHardwareEntriesWithData[e].revision); ctx->dumpHardwareEntriesWithData[e].entry.revisionLength = 0; - fprintf(stderr, "libaaruformat: Could not read dump hardware block entry revision, " - "continuing...\n"); + TRACE("Could not read dump hardware block entry revision, " + "continuing...\n"); } } } @@ -197,8 +197,8 @@ void process_dumphw_block(aaruformatContext *ctx, const IndexEntry *entry) { free(ctx->dumpHardwareEntriesWithData[e].firmware); ctx->dumpHardwareEntriesWithData[e].entry.firmwareLength = 0; - fprintf(stderr, "libaaruformat: Could not read dump hardware block entry firmware, " - "continuing...\n"); + TRACE("Could not read dump hardware block entry firmware, " + "continuing...\n"); } } } @@ -218,7 +218,7 @@ void process_dumphw_block(aaruformatContext *ctx, const IndexEntry *entry) { free(ctx->dumpHardwareEntriesWithData[e].serial); ctx->dumpHardwareEntriesWithData[e].entry.serialLength = 0; - fprintf(stderr, "libaaruformat: Could not read dump hardware block entry serial, continuing...\n"); + TRACE("Could not read dump hardware block entry serial, continuing...\n"); } } } @@ -239,8 +239,8 @@ void process_dumphw_block(aaruformatContext *ctx, const IndexEntry *entry) { free(ctx->dumpHardwareEntriesWithData[e].softwareName); ctx->dumpHardwareEntriesWithData[e].entry.softwareNameLength = 0; - fprintf(stderr, "libaaruformat: Could not read dump hardware block entry software name, " - "continuing...\n"); + TRACE("Could not read dump hardware block entry software name, " + "continuing...\n"); } } } @@ -261,8 +261,8 @@ void process_dumphw_block(aaruformatContext *ctx, const IndexEntry *entry) { free(ctx->dumpHardwareEntriesWithData[e].softwareVersion); ctx->dumpHardwareEntriesWithData[e].entry.softwareVersionLength = 0; - fprintf(stderr, "libaaruformat: Could not read dump hardware block entry software version, " - "continuing...\n"); + TRACE("Could not read dump hardware block entry software version, " + "continuing...\n"); } } } @@ -285,8 +285,8 @@ void process_dumphw_block(aaruformatContext *ctx, const IndexEntry *entry) { free(ctx->dumpHardwareEntriesWithData[e].softwareOperatingSystem); ctx->dumpHardwareEntriesWithData[e].entry.softwareOperatingSystemLength = 0; - fprintf(stderr, "libaaruformat: Could not read dump hardware block entry manufacturer, " - "continuing...\n"); + TRACE("Could not read dump hardware block entry manufacturer, " + "continuing...\n"); } } } @@ -296,8 +296,8 @@ void process_dumphw_block(aaruformatContext *ctx, const IndexEntry *entry) if(ctx->dumpHardwareEntriesWithData[e].extents == NULL) { - fprintf(stderr, "libaaruformat: Could not allocate memory for dump hardware block extents, " - "continuing...\n"); + TRACE("Could not allocate memory for dump hardware block extents, " + "continuing...\n"); continue; } @@ -307,7 +307,7 @@ void process_dumphw_block(aaruformatContext *ctx, const IndexEntry *entry) if(readBytes != ctx->dumpHardwareEntriesWithData->entry.extents) { free(ctx->dumpHardwareEntriesWithData[e].extents); - fprintf(stderr, "libaaruformat: Could not read dump hardware block extents, continuing...\n"); + TRACE("Could not read dump hardware block extents, continuing...\n"); continue; } diff --git a/src/blocks/metadata.c b/src/blocks/metadata.c index 14c5e4a..151ee7a 100644 --- a/src/blocks/metadata.c +++ b/src/blocks/metadata.c @@ -22,6 +22,7 @@ #include #include "aaruformat.h" +#include "log.h" // Process the metadata block found while opening an AaruFormat file void process_metadata_block(aaruformatContext *ctx, const IndexEntry *entry) @@ -40,7 +41,7 @@ void process_metadata_block(aaruformatContext *ctx, const IndexEntry *entry) pos = fseek(ctx->imageStream, entry->offset, SEEK_SET); if(pos < 0 || ftell(ctx->imageStream) != entry->offset) { - fprintf(stderr, "libaaruformat: Could not seek to %" PRIu64 " as indicated by index entry...\n", entry->offset); + FATAL("Could not seek to %" PRIu64 " as indicated by index entry...\n", entry->offset); return; } @@ -52,14 +53,14 @@ void process_metadata_block(aaruformatContext *ctx, const IndexEntry *entry) if(readBytes != sizeof(MetadataBlockHeader)) { memset(&ctx->metadataBlockHeader, 0, sizeof(MetadataBlockHeader)); - fprintf(stderr, "libaaruformat: Could not read metadata block header, continuing...\n"); + FATAL("Could not read metadata block header, continuing...\n"); return; } if(ctx->metadataBlockHeader.identifier != entry->blockType) { memset(&ctx->metadataBlockHeader, 0, sizeof(MetadataBlockHeader)); - fprintf(stderr, "libaaruformat: Incorrect identifier for data block at position %" PRIu64 "\n", entry->offset); + TRACE("Incorrect identifier for data block at position %" PRIu64 "\n", entry->offset); return; } @@ -70,7 +71,7 @@ void process_metadata_block(aaruformatContext *ctx, const IndexEntry *entry) if(ctx->metadataBlock == NULL) { memset(&ctx->metadataBlockHeader, 0, sizeof(MetadataBlockHeader)); - fprintf(stderr, "libaaruformat: Could not allocate memory for metadata block, continuing...\n"); + FATAL("Could not allocate memory for metadata block, continuing...\n"); return; } @@ -80,15 +81,14 @@ void process_metadata_block(aaruformatContext *ctx, const IndexEntry *entry) { memset(&ctx->metadataBlockHeader, 0, sizeof(MetadataBlockHeader)); free(ctx->metadataBlock); - fprintf(stderr, "libaaruformat: Could not read metadata block, continuing...\n"); + FATAL("Could not read metadata block, continuing...\n"); } if(ctx->metadataBlockHeader.mediaSequence > 0 && ctx->metadataBlockHeader.lastMediaSequence > 0) { ctx->imageInfo.MediaSequence = ctx->metadataBlockHeader.mediaSequence; ctx->imageInfo.LastMediaSequence = ctx->metadataBlockHeader.lastMediaSequence; - fprintf(stderr, "libaaruformat: Setting media sequence as %d of %d\n", ctx->imageInfo.MediaSequence, - ctx->imageInfo.LastMediaSequence); + TRACE("Setting media sequence as %d of %d\n", ctx->imageInfo.MediaSequence, ctx->imageInfo.LastMediaSequence); } if(ctx->metadataBlockHeader.creatorLength > 0 && @@ -249,14 +249,14 @@ void process_geometry_block(aaruformatContext *ctx, const IndexEntry *entry) // Check if the context and image stream are valid if(ctx == NULL || ctx->imageStream == NULL) { - fprintf(stderr, "Invalid context or image stream.\n"); + FATAL("Invalid context or image stream.\n"); return; } // Seek to block if(fseek(ctx->imageStream, entry->offset, SEEK_SET) != 0) { - fprintf(stderr, "libaaruformat: Could not seek to %" PRIu64 " as indicated by index entry...\n", entry->offset); + FATAL("Could not seek to %" PRIu64 " as indicated by index entry...\n", entry->offset); return; } @@ -265,22 +265,21 @@ void process_geometry_block(aaruformatContext *ctx, const IndexEntry *entry) if(readBytes != sizeof(GeometryBlockHeader)) { memset(&ctx->geometryBlock, 0, sizeof(GeometryBlockHeader)); - fprintf(stderr, "libaaruformat: Could not read geometry block header, continuing...\n"); + TRACE("Could not read geometry block header, continuing...\n"); return; } if(ctx->geometryBlock.identifier != GeometryBlock) { memset(&ctx->geometryBlock, 0, sizeof(GeometryBlockHeader)); - fprintf(stderr, "libaaruformat: Incorrect identifier for geometry block at position %" PRIu64 "\n", - entry->offset); + TRACE("Incorrect identifier for geometry block at position %" PRIu64 "\n", entry->offset); return; } ctx->imageInfo.ImageSize += sizeof(GeometryBlockHeader); - fprintf(stderr, "libaaruformat: Geometry set to %d cylinders %d heads %d sectors per track\n", - ctx->geometryBlock.cylinders, ctx->geometryBlock.heads, ctx->geometryBlock.sectorsPerTrack); + TRACE("Geometry set to %d cylinders %d heads %d sectors per track\n", ctx->geometryBlock.cylinders, + ctx->geometryBlock.heads, ctx->geometryBlock.sectorsPerTrack); ctx->imageInfo.Cylinders = ctx->geometryBlock.cylinders; ctx->imageInfo.Heads = ctx->geometryBlock.heads; @@ -296,7 +295,7 @@ void process_cicm_block(aaruformatContext *ctx, const IndexEntry *entry) // Check if the context and image stream are valid if(ctx == NULL || ctx->imageStream == NULL) { - fprintf(stderr, "Invalid context or image stream.\n"); + FATAL("Invalid context or image stream.\n"); return; } @@ -304,7 +303,7 @@ void process_cicm_block(aaruformatContext *ctx, const IndexEntry *entry) pos = fseek(ctx->imageStream, entry->offset, SEEK_SET); if(pos < 0 || ftell(ctx->imageStream) != entry->offset) { - fprintf(stderr, "libaaruformat: Could not seek to %" PRIu64 " as indicated by index entry...\n", entry->offset); + FATAL("Could not seek to %" PRIu64 " as indicated by index entry...\n", entry->offset); return; } @@ -316,14 +315,14 @@ void process_cicm_block(aaruformatContext *ctx, const IndexEntry *entry) if(readBytes != sizeof(CicmMetadataBlock)) { memset(&ctx->cicmBlockHeader, 0, sizeof(CicmMetadataBlock)); - fprintf(stderr, "libaaruformat: Could not read CICM XML metadata header, continuing...\n"); + TRACE("Could not read CICM XML metadata header, continuing...\n"); return; } if(ctx->cicmBlockHeader.identifier != CicmBlock) { memset(&ctx->cicmBlockHeader, 0, sizeof(CicmMetadataBlock)); - fprintf(stderr, "libaaruformat: Incorrect identifier for data block at position %" PRIu64 "\n", entry->offset); + TRACE("Incorrect identifier for data block at position %" PRIu64 "\n", entry->offset); } ctx->imageInfo.ImageSize += ctx->cicmBlockHeader.length; @@ -333,7 +332,7 @@ void process_cicm_block(aaruformatContext *ctx, const IndexEntry *entry) if(ctx->cicmBlock == NULL) { memset(&ctx->cicmBlockHeader, 0, sizeof(CicmMetadataBlock)); - fprintf(stderr, "libaaruformat: Could not allocate memory for CICM XML metadata block, continuing...\n"); + TRACE("Could not allocate memory for CICM XML metadata block, continuing...\n"); return; } @@ -343,8 +342,8 @@ void process_cicm_block(aaruformatContext *ctx, const IndexEntry *entry) { memset(&ctx->cicmBlockHeader, 0, sizeof(CicmMetadataBlock)); free(ctx->cicmBlock); - fprintf(stderr, "libaaruformat: Could not read CICM XML metadata block, continuing...\n"); + TRACE("Could not read CICM XML metadata block, continuing...\n"); } - fprintf(stderr, "libaaruformat: Found CICM XML metadata block %" PRIu64 ".\n", entry->offset); + TRACE("Found CICM XML metadata block %" PRIu64 ".\n", entry->offset); } \ No newline at end of file diff --git a/src/blocks/optical.c b/src/blocks/optical.c index a979fd5..e348fe3 100644 --- a/src/blocks/optical.c +++ b/src/blocks/optical.c @@ -22,6 +22,7 @@ #include #include "aaruformat.h" +#include "log.h" void process_tracks_block(aaruformatContext *ctx, const IndexEntry *entry) { @@ -33,7 +34,7 @@ void process_tracks_block(aaruformatContext *ctx, const IndexEntry *entry) // Check if the context and image stream are valid if(ctx == NULL || ctx->imageStream == NULL) { - fprintf(stderr, "Invalid context or image stream.\n"); + FATAL("Invalid context or image stream.\n"); return; } @@ -41,7 +42,7 @@ void process_tracks_block(aaruformatContext *ctx, const IndexEntry *entry) pos = fseek(ctx->imageStream, entry->offset, SEEK_SET); if(pos < 0 || ftell(ctx->imageStream) != entry->offset) { - fprintf(stderr, "libaaruformat: Could not seek to %" PRIu64 " as indicated by index entry...\n", entry->offset); + FATAL("Could not seek to %" PRIu64 " as indicated by index entry...\n", entry->offset); return; } @@ -52,14 +53,14 @@ void process_tracks_block(aaruformatContext *ctx, const IndexEntry *entry) if(readBytes != sizeof(TracksHeader)) { memset(&ctx->tracksHeader, 0, sizeof(TracksHeader)); - fprintf(stderr, "libaaruformat: Could not read tracks header, continuing...\n"); + TRACE("Could not read tracks header, continuing...\n"); return; } if(ctx->tracksHeader.identifier != TracksBlock) { memset(&ctx->tracksHeader, 0, sizeof(TracksHeader)); - fprintf(stderr, "libaaruformat: Incorrect identifier for data block at position %" PRIu64 "\n", entry->offset); + TRACE("Incorrect identifier for data block at position %" PRIu64 "\n", entry->offset); } ctx->imageInfo.ImageSize += sizeof(TrackEntry) * ctx->tracksHeader.entries; @@ -69,7 +70,7 @@ void process_tracks_block(aaruformatContext *ctx, const IndexEntry *entry) if(ctx->trackEntries == NULL) { memset(&ctx->tracksHeader, 0, sizeof(TracksHeader)); - fprintf(stderr, "libaaruformat: Could not allocate memory for metadata block, continuing...\n"); + FATAL("Could not allocate memory for metadata block, continuing...\n"); return; } @@ -79,7 +80,7 @@ void process_tracks_block(aaruformatContext *ctx, const IndexEntry *entry) { memset(&ctx->tracksHeader, 0, sizeof(TracksHeader)); free(ctx->trackEntries); - fprintf(stderr, "libaaruformat: Could not read metadata block, continuing...\n"); + FATAL("Could not read metadata block, continuing...\n"); return; } @@ -91,14 +92,12 @@ void process_tracks_block(aaruformatContext *ctx, const IndexEntry *entry) if(crc64 != ctx->tracksHeader.crc64) { - fprintf(stderr, - "libaaruformat: Incorrect CRC found: 0x%" PRIx64 " found, expected 0x%" PRIx64 ", continuing...\n", - crc64, ctx->tracksHeader.crc64); + TRACE("Incorrect CRC found: 0x%" PRIx64 " found, expected 0x%" PRIx64 ", continuing...\n", crc64, + ctx->tracksHeader.crc64); return; } - fprintf(stderr, "libaaruformat: Found %d tracks at position %" PRIu64 ".\n", ctx->tracksHeader.entries, - entry->offset); + TRACE("Found %d tracks at position %" PRIu64 ".\n", ctx->tracksHeader.entries, entry->offset); ctx->imageInfo.HasPartitions = true; ctx->imageInfo.HasSessions = true; diff --git a/src/ddt/ddt_v1.c b/src/ddt/ddt_v1.c index 36dfcef..df5c524 100644 --- a/src/ddt/ddt_v1.c +++ b/src/ddt/ddt_v1.c @@ -26,6 +26,7 @@ #endif #include "aaruformat.h" +#include "log.h" int32_t process_ddt_v1(aaruformatContext *ctx, IndexEntry *entry, bool *foundUserDataDdt) { @@ -49,7 +50,7 @@ int32_t process_ddt_v1(aaruformatContext *ctx, IndexEntry *entry, bool *foundUse pos = fseek(ctx->imageStream, entry->offset, SEEK_SET); if(pos < 0 || ftell(ctx->imageStream) != entry->offset) { - fprintf(stderr, "libaaruformat: Could not seek to %" PRIu64 " as indicated by index entry...\n", entry->offset); + FATAL("Could not seek to %" PRIu64 " as indicated by index entry...\n", entry->offset); return AARUF_ERROR_CANNOT_READ_BLOCK; } @@ -60,7 +61,7 @@ int32_t process_ddt_v1(aaruformatContext *ctx, IndexEntry *entry, bool *foundUse if(readBytes != sizeof(DdtHeader)) { - fprintf(stderr, "libaaruformat: Could not read block header at %" PRIu64 "\n", entry->offset); + FATAL("Could not read block header at %" PRIu64 "\n", entry->offset); return AARUF_ERROR_CANNOT_READ_BLOCK; } @@ -85,14 +86,14 @@ int32_t process_ddt_v1(aaruformatContext *ctx, IndexEntry *entry, bool *foundUse cmpData = (uint8_t *)malloc(lzmaSize); if(cmpData == NULL) { - fprintf(stderr, "Cannot allocate memory for DDT, continuing...\n"); + TRACE("Cannot allocate memory for DDT, continuing...\n"); break; } ctx->userDataDdt = (uint64_t *)malloc(ddtHeader.length); if(ctx->userDataDdt == NULL) { - fprintf(stderr, "Cannot allocate memory for DDT, continuing...\n"); + TRACE("Cannot allocate memory for DDT, continuing...\n"); free(cmpData); break; } @@ -100,7 +101,7 @@ int32_t process_ddt_v1(aaruformatContext *ctx, IndexEntry *entry, bool *foundUse readBytes = fread(lzmaProperties, 1, LZMA_PROPERTIES_LENGTH, ctx->imageStream); if(readBytes != LZMA_PROPERTIES_LENGTH) { - fprintf(stderr, "Could not read LZMA properties, continuing...\n"); + TRACE("Could not read LZMA properties, continuing...\n"); free(cmpData); free(ctx->userDataDdt); ctx->userDataDdt = NULL; @@ -110,7 +111,7 @@ int32_t process_ddt_v1(aaruformatContext *ctx, IndexEntry *entry, bool *foundUse readBytes = fread(cmpData, 1, lzmaSize, ctx->imageStream); if(readBytes != lzmaSize) { - fprintf(stderr, "Could not read compressed block, continuing...\n"); + TRACE("Could not read compressed block, continuing...\n"); free(cmpData); free(ctx->userDataDdt); ctx->userDataDdt = NULL; @@ -123,7 +124,7 @@ int32_t process_ddt_v1(aaruformatContext *ctx, IndexEntry *entry, bool *foundUse if(errorNo != 0) { - fprintf(stderr, "Got error %d from LZMA, stopping...\n", errorNo); + FATAL("Got error %d from LZMA, stopping...\n", errorNo); free(cmpData); free(ctx->userDataDdt); ctx->userDataDdt = NULL; @@ -132,7 +133,7 @@ int32_t process_ddt_v1(aaruformatContext *ctx, IndexEntry *entry, bool *foundUse if(readBytes != ddtHeader.length) { - fprintf(stderr, "Error decompressing block, should be {0} bytes but got {1} bytes., stopping...\n"); + FATAL("Error decompressing block, should be {0} bytes but got {1} bytes., stopping...\n"); free(cmpData); free(ctx->userDataDdt); ctx->userDataDdt = NULL; @@ -153,20 +154,19 @@ int32_t process_ddt_v1(aaruformatContext *ctx, IndexEntry *entry, bool *foundUse if(ctx->userDataDdt == MAP_FAILED) { *foundUserDataDdt = false; - fprintf(stderr, "libaaruformat: Could not read map deduplication table.\n"); + FATAL("Could not read map deduplication table.\n"); break; } ctx->inMemoryDdt = false; break; #else // TODO: Implement - fprintf(stderr, "libaaruformat: Uncompressed DDT not yet implemented...\n"); + TRACE("Uncompressed DDT not yet implemented...\n"); *foundUserDataDdt = false; break; #endif default: - fprintf(stderr, "libaaruformat: Found unknown compression type %d, continuing...\n", - ddtHeader.compression); + TRACE("Found unknown compression type %d, continuing...\n", ddtHeader.compression); *foundUserDataDdt = false; break; } @@ -182,14 +182,14 @@ int32_t process_ddt_v1(aaruformatContext *ctx, IndexEntry *entry, bool *foundUse cmpData = (uint8_t *)malloc(lzmaSize); if(cmpData == NULL) { - fprintf(stderr, "Cannot allocate memory for DDT, continuing...\n"); + TRACE("Cannot allocate memory for DDT, continuing...\n"); break; } cdDdt = (uint32_t *)malloc(ddtHeader.length); if(cdDdt == NULL) { - fprintf(stderr, "Cannot allocate memory for DDT, continuing...\n"); + TRACE("Cannot allocate memory for DDT, continuing...\n"); free(cmpData); break; } @@ -197,7 +197,7 @@ int32_t process_ddt_v1(aaruformatContext *ctx, IndexEntry *entry, bool *foundUse readBytes = fread(lzmaProperties, 1, LZMA_PROPERTIES_LENGTH, ctx->imageStream); if(readBytes != LZMA_PROPERTIES_LENGTH) { - fprintf(stderr, "Could not read LZMA properties, continuing...\n"); + TRACE("Could not read LZMA properties, continuing...\n"); free(cmpData); free(cdDdt); break; @@ -206,7 +206,7 @@ int32_t process_ddt_v1(aaruformatContext *ctx, IndexEntry *entry, bool *foundUse readBytes = fread(cmpData, 1, lzmaSize, ctx->imageStream); if(readBytes != lzmaSize) { - fprintf(stderr, "Could not read compressed block, continuing...\n"); + TRACE("Could not read compressed block, continuing...\n"); free(cmpData); free(cdDdt); break; @@ -218,7 +218,7 @@ int32_t process_ddt_v1(aaruformatContext *ctx, IndexEntry *entry, bool *foundUse if(errorNo != 0) { - fprintf(stderr, "Got error %d from LZMA, stopping...\n", errorNo); + FATAL("Got error %d from LZMA, stopping...\n", errorNo); free(cmpData); free(cdDdt); return AARUF_ERROR_CANNOT_DECOMPRESS_BLOCK; @@ -226,7 +226,7 @@ int32_t process_ddt_v1(aaruformatContext *ctx, IndexEntry *entry, bool *foundUse if(readBytes != ddtHeader.length) { - fprintf(stderr, "Error decompressing block, should be {0} bytes but got {1} bytes., stopping...\n"); + FATAL("Error decompressing block, should be {0} bytes but got {1} bytes., stopping...\n"); free(cmpData); free(cdDdt); return AARUF_ERROR_CANNOT_DECOMPRESS_BLOCK; @@ -247,7 +247,7 @@ int32_t process_ddt_v1(aaruformatContext *ctx, IndexEntry *entry, bool *foundUse if(cdDdt == NULL) { - fprintf(stderr, "libaaruformat: Cannot allocate memory for deduplication table.\n"); + TRACE("Cannot allocate memory for deduplication table.\n"); break; } @@ -256,7 +256,7 @@ int32_t process_ddt_v1(aaruformatContext *ctx, IndexEntry *entry, bool *foundUse if(readBytes != ddtHeader.entries * sizeof(uint32_t)) { free(cdDdt); - fprintf(stderr, "libaaruformat: Could not read deduplication table, continuing...\n"); + TRACE("Could not read deduplication table, continuing...\n"); break; } @@ -269,8 +269,7 @@ int32_t process_ddt_v1(aaruformatContext *ctx, IndexEntry *entry, bool *foundUse break; default: - fprintf(stderr, "libaaruformat: Found unknown compression type %d, continuing...\n", - ddtHeader.compression); + TRACE("Found unknown compression type %d, continuing...\n", ddtHeader.compression); break; } } diff --git a/src/ddt/ddt_v2.c b/src/ddt/ddt_v2.c index 0826d16..20946f4 100644 --- a/src/ddt/ddt_v2.c +++ b/src/ddt/ddt_v2.c @@ -23,6 +23,7 @@ #include "aaruformat.h" #include "internal.h" +#include "log.h" int32_t process_ddt_v2(aaruformatContext *ctx, IndexEntry *entry, bool *foundUserDataDdt) { @@ -48,7 +49,7 @@ int32_t process_ddt_v2(aaruformatContext *ctx, IndexEntry *entry, bool *foundUse pos = fseek(ctx->imageStream, entry->offset, SEEK_SET); if(pos < 0 || ftell(ctx->imageStream) != entry->offset) { - fprintf(stderr, "libaaruformat: Could not seek to %" PRIu64 " as indicated by index entry...\n", entry->offset); + FATAL("Could not seek to %" PRIu64 " as indicated by index entry...\n", entry->offset); return AARUF_ERROR_CANNOT_READ_BLOCK; } @@ -59,7 +60,7 @@ int32_t process_ddt_v2(aaruformatContext *ctx, IndexEntry *entry, bool *foundUse if(readBytes != sizeof(DdtHeader2)) { - fprintf(stderr, "libaaruformat: Could not read block header at %" PRIu64 "\n", entry->offset); + FATAL("Could not read block header at %" PRIu64 "\n", entry->offset); return AARUF_ERROR_CANNOT_READ_BLOCK; } @@ -179,7 +180,7 @@ int32_t process_ddt_v2(aaruformatContext *ctx, IndexEntry *entry, bool *foundUse if(readBytes != ddtHeader.length) { free(buffer); - fprintf(stderr, "libaaruformat: Could not read deduplication table, continuing...\n"); + FATAL("Could not read deduplication table, continuing...\n"); break; } @@ -212,8 +213,7 @@ int32_t process_ddt_v2(aaruformatContext *ctx, IndexEntry *entry, bool *foundUse break; default: - fprintf(stderr, "libaaruformat: Found unknown compression type %d, continuing...\n", - ddtHeader.compression); + TRACE("Found unknown compression type %d, continuing...\n", ddtHeader.compression); *foundUserDataDdt = false; break; } @@ -321,7 +321,7 @@ int32_t process_ddt_v2(aaruformatContext *ctx, IndexEntry *entry, bool *foundUse if(buffer == NULL) { - fprintf(stderr, "libaaruformat: Cannot allocate memory for deduplication table.\n"); + TRACE("Cannot allocate memory for deduplication table.\n"); break; } @@ -330,7 +330,7 @@ int32_t process_ddt_v2(aaruformatContext *ctx, IndexEntry *entry, bool *foundUse if(readBytes != ddtHeader.length) { free(buffer); - fprintf(stderr, "libaaruformat: Could not read deduplication table, continuing...\n"); + FATAL("Could not read deduplication table, continuing...\n"); break; } @@ -372,8 +372,7 @@ int32_t process_ddt_v2(aaruformatContext *ctx, IndexEntry *entry, bool *foundUse break; default: - fprintf(stderr, "libaaruformat: Found unknown compression type %d, continuing...\n", - ddtHeader.compression); + TRACE("Found unknown compression type %d, continuing...\n", ddtHeader.compression); break; } } @@ -421,7 +420,7 @@ int32_t decode_ddt_single_level_v2(aaruformatContext *ctx, uint64_t sectorAddres ddtEntry = ctx->userDataDdtBig[sectorAddress]; else { - fprintf(stderr, "libaaruformat: Unknown DDT size type %d.\n", ctx->userDataDdtHeader.sizeType); + TRACE("Unknown DDT size type %d.\n", ctx->userDataDdtHeader.sizeType); return AARUF_ERROR_CANNOT_READ_BLOCK; } @@ -488,7 +487,7 @@ int32_t decode_ddt_multi_level_v2(aaruformatContext *ctx, uint64_t sectorAddress secondaryDdtOffset = ctx->userDataDdtBig[ddtPosition]; else { - fprintf(stderr, "libaaruformat: Unknown DDT size type %d.\n", ctx->userDataDdtHeader.sizeType); + TRACE("Unknown DDT size type %d.\n", ctx->userDataDdtHeader.sizeType); return AARUF_ERROR_CANNOT_READ_BLOCK; } @@ -504,14 +503,14 @@ int32_t decode_ddt_multi_level_v2(aaruformatContext *ctx, uint64_t sectorAddress if(readBytes != sizeof(DdtHeader2)) { - fprintf(stderr, "libaaruformat: Could not read block header at %" PRIu64 "\n", secondaryDdtOffset); + FATAL("Could not read block header at %" PRIu64 "\n", secondaryDdtOffset); return AARUF_ERROR_CANNOT_READ_BLOCK; } if(ddtHeader.identifier != DeDuplicationTable2 || ddtHeader.type != UserData) { - fprintf(stderr, "libaaruformat: Invalid block header at %" PRIu64 "\n", secondaryDdtOffset); + TRACE("Invalid block header at %" PRIu64 "\n", secondaryDdtOffset); return AARUF_ERROR_CANNOT_READ_BLOCK; } @@ -618,7 +617,7 @@ int32_t decode_ddt_multi_level_v2(aaruformatContext *ctx, uint64_t sectorAddress if(readBytes != ddtHeader.length) { free(buffer); - fprintf(stderr, "libaaruformat: Could not read deduplication table, stopping...\n"); + FATAL("Could not read deduplication table, stopping...\n"); return AARUF_ERROR_CANNOT_READ_BLOCK; } @@ -650,8 +649,7 @@ int32_t decode_ddt_multi_level_v2(aaruformatContext *ctx, uint64_t sectorAddress break; default: - fprintf(stderr, "libaaruformat: Found unknown compression type %d, stopping...\n", - ddtHeader.compression); + TRACE("Found unknown compression type %d, stopping...\n", ddtHeader.compression); return AARUF_ERROR_CANNOT_READ_BLOCK; } } diff --git a/src/open.c b/src/open.c index f951c6b..e52a170 100644 --- a/src/open.c +++ b/src/open.c @@ -25,6 +25,7 @@ #include #include "internal.h" +#include "log.h" #include "utarray.h" void *aaruf_open(const char *filepath) @@ -35,7 +36,13 @@ void *aaruf_open(const char *filepath) long pos = 0; int i = 0; uint32_t signature = 0; - UT_array * index_entries = NULL; + UT_array *index_entries = NULL; + +#ifdef USE_SLOG +#include "slog.h" + + slog_init("aaruformat.log", SLOG_FLAGS_ALL, 0); +#endif ctx = (aaruformatContext *)malloc(sizeof(aaruformatContext)); memset(ctx, 0, sizeof(aaruformatContext)); @@ -99,8 +106,7 @@ void *aaruf_open(const char *filepath) return NULL; } - fprintf(stderr, "libaaruformat: Opening image version %d.%d\n", ctx->header.imageMajorVersion, - ctx->header.imageMinorVersion); + TRACE("Opening image version %d.%d\n", ctx->header.imageMajorVersion, ctx->header.imageMinorVersion); ctx->readableSectorTags = (bool *)malloc(sizeof(bool) * MaxSectorTag); @@ -151,8 +157,8 @@ void *aaruf_open(const char *filepath) readBytes = fread(&signature, 1, sizeof(uint32_t), ctx->imageStream); - if(readBytes != sizeof(uint32_t) || (signature != IndexBlock && signature != IndexBlock2 && signature != - IndexBlock3)) + if(readBytes != sizeof(uint32_t) || + (signature != IndexBlock && signature != IndexBlock2 && signature != IndexBlock3)) { free(ctx); errno = AARUF_ERROR_CANNOT_READ_INDEX; @@ -160,9 +166,12 @@ void *aaruf_open(const char *filepath) return NULL; } - if(signature == IndexBlock) index_entries = process_index_v1(ctx); - else if(signature == IndexBlock2) index_entries = process_index_v2(ctx); - else if(signature == IndexBlock3) index_entries = process_index_v3(ctx); + if(signature == IndexBlock) + index_entries = process_index_v1(ctx); + else if(signature == IndexBlock2) + index_entries = process_index_v2(ctx); + else if(signature == IndexBlock3) + index_entries = process_index_v3(ctx); if(index_entries == NULL) { @@ -174,14 +183,13 @@ void *aaruf_open(const char *filepath) return NULL; } - fprintf(stderr, "libaaruformat: Index at %" PRIu64 " contains %d entries\n", ctx->header.indexOffset, - utarray_len(index_entries)); + TRACE("Index at %" PRIu64 " contains %d entries\n", ctx->header.indexOffset, utarray_len(index_entries)); for(i = 0; i < utarray_len(index_entries); i++) { IndexEntry *entry = (IndexEntry *)utarray_eltptr(index_entries, i); - fprintf(stderr, "libaaruformat: Block type %4.4s with data type %d is indexed to be at %" PRIu64 "\n", - (char *)&entry->blockType, entry->dataType, entry->offset); + TRACE("Block type %4.4s with data type %d is indexed to be at %" PRIu64 "\n", (char *)&entry->blockType, + entry->dataType, entry->offset); } bool foundUserDataDdt = false; @@ -193,9 +201,9 @@ void *aaruf_open(const char *filepath) if(pos < 0 || ftell(ctx->imageStream) != entry->offset) { - fprintf( - stderr, "libaaruformat: Could not seek to %" PRIu64 " as indicated by index entry %d, continuing...\n", - entry->offset, i); + fprintf(stderr, + "libaaruformat: Could not seek to %" PRIu64 " as indicated by index entry %d, continuing...\n", + entry->offset, i); continue; } @@ -268,10 +276,9 @@ void *aaruf_open(const char *filepath) break; default: - fprintf( - stderr, - "libaaruformat: Unhandled block type %4.4s with data type %d is indexed to be at %" PRIu64 "\n", - (char *)&entry->blockType, entry->dataType, entry->offset); + fprintf(stderr, + "libaaruformat: Unhandled block type %4.4s with data type %d is indexed to be at %" PRIu64 "\n", + (char *)&entry->blockType, entry->dataType, entry->offset); break; } } @@ -280,7 +287,7 @@ void *aaruf_open(const char *filepath) if(!foundUserDataDdt) { - fprintf(stderr, "libaaruformat: Could not find user data deduplication table, aborting...\n"); + FATAL("Could not find user data deduplication table, aborting...\n"); aaruf_close(ctx); return NULL; } diff --git a/src/write.c b/src/write.c index 1861d74..33aa228 100644 --- a/src/write.c +++ b/src/write.c @@ -71,6 +71,13 @@ int32_t aaruf_write_sector(void *context, uint64_t sectorAddress, uint8_t *data, if(ctx->writingBuffer == NULL) return AARUF_ERROR_NOT_ENOUGH_MEMORY; ctx->crc64Context = aaruf_crc64_init(); + + // Get current file position + long pos = ftell(ctx->imageStream); + + // Calculate and save next block aligned position + ctx->nextBlockPosition = + pos / (1 << ctx->userDataDdtHeader.blockAlignmentShift) * (1 << ctx->userDataDdtHeader.blockAlignmentShift); } // TODO: DDT entry @@ -105,13 +112,8 @@ int32_t aaruf_close_current_block(aaruformatContext *ctx) // Write block header to file - // Get file position - long pos = ftell(ctx->imageStream); - - // Fill file with zeroes until next aligned position according to DDT's block alignment shift - long next_alignment = - pos / (1 << ctx->userDataDdtHeader.blockAlignmentShift) * (1 << ctx->userDataDdtHeader.blockAlignmentShift); - fwrite("\0", 1, next_alignment - pos, ctx->imageStream); + // Move to expected block position + fseek(ctx->imageStream, ctx->nextBlockPosition, SEEK_SET); // Write block header if(fwrite(&ctx->currentBlockHeader, sizeof(BlockHeader), 1, ctx->imageStream) != 1)