From af233d3b6f04597402d8a6edb2e9fbc74d6b4b15 Mon Sep 17 00:00:00 2001 From: Natalia Portillo Date: Fri, 10 Oct 2025 18:32:17 +0100 Subject: [PATCH] Add support for long sector read/write in convert command --- tool/aaruformattool.h | 2 +- tool/commands.c | 5 +++-- tool/convert.c | 44 ++++++++++++++++++++++++++++++++++++++----- tool/usage.c | 4 +++- 4 files changed, 46 insertions(+), 9 deletions(-) diff --git a/tool/aaruformattool.h b/tool/aaruformattool.h index 12426a7..fdeccf5 100644 --- a/tool/aaruformattool.h +++ b/tool/aaruformattool.h @@ -37,6 +37,6 @@ bool check_cd_sector_channel(CdEccContext *context, const uint8_t *sector bool *ecc_q_correct); int compare(const char *path1, const char *path2); int cli_compare(const char *path1, const char *path2); -int convert(const char *input_path, const char *output_path); +int convert(const char *input_path, const char *output_path, bool use_long); #endif // LIBAARUFORMAT_TOOL_AARUFORMATTOOL_H_ diff --git a/tool/commands.c b/tool/commands.c index 3a5ab19..da2b4f3 100644 --- a/tool/commands.c +++ b/tool/commands.c @@ -155,10 +155,11 @@ int cmd_verify_sectors(int argc, char *argv[]) { return cmd_verify_common(argc, int cmd_convert(int argc, char *argv[]) { + struct arg_lit *use_long = arg_lit0("l", NULL, "Use long sector read/write (includes tags and metadata)"); struct arg_str *input_filename = arg_str1(NULL, NULL, "", "Input image file"); struct arg_str *output_filename = arg_str1(NULL, NULL, "", "Output image file"); struct arg_end *end = arg_end(10); - void *argtable[] = {input_filename, output_filename, end}; + void *argtable[] = {use_long, input_filename, output_filename, end}; if(arg_parse(argc, argv, argtable) > 0) { @@ -168,7 +169,7 @@ int cmd_convert(int argc, char *argv[]) return -1; } - const int result = convert(input_filename->sval[0], output_filename->sval[0]); + const int result = convert(input_filename->sval[0], output_filename->sval[0], use_long->count > 0); arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); return result; } diff --git a/tool/convert.c b/tool/convert.c index a9dab69..56db13b 100644 --- a/tool/convert.c +++ b/tool/convert.c @@ -28,7 +28,7 @@ #include "aaruformattool.h" -int convert(const char *input_path, const char *output_path) +int convert(const char *input_path, const char *output_path, bool use_long) { aaruformat_context *input_ctx = NULL; aaruformat_context *output_ctx = NULL; @@ -37,7 +37,7 @@ int convert(const char *input_path, const char *output_path) uint64_t total_sectors = 0; uint8_t *sector_data = NULL; - printf("Converting image from %s to %s...\n", input_path, output_path); + printf("Converting image from %s to %s%s...\n", input_path, output_path, use_long ? " (long mode)" : ""); // Open input image input_ctx = aaruf_open(input_path); @@ -170,7 +170,7 @@ int convert(const char *input_path, const char *output_path) // Copy sectors from input to output for(uint64_t sector = 0; sector < total_sectors; sector++) { - uint32_t read_length = sector_size; + uint32_t read_length = 0; // Show progress every 1000 sectors if(sector % 1000 == 0 || sector == total_sectors - 1) @@ -180,8 +180,38 @@ int convert(const char *input_path, const char *output_path) fflush(stdout); } + // Check sector size + if(use_long) + res = aaruf_read_sector_long(input_ctx, sector, false, sector_data, &read_length); + else + res = aaruf_read_sector(input_ctx, sector, false, sector_data, &read_length); + + if(res != AARUF_ERROR_BUFFER_TOO_SMALL) + { + printf("\nError %d when reading sector %llu from input image.\n", res, (unsigned long long)sector); + break; + } + + if(sector_size < read_length) + { + free(sector_data); + sector_size = read_length; + sector_data = malloc(sector_size); + if(sector_data == NULL) + { + printf("Error allocating memory for sector buffer.\n"); + aaruf_close(input_ctx); + aaruf_close(output_ctx); + return AARUF_ERROR_NOT_ENOUGH_MEMORY; + } + } + // Read sector from input - res = aaruf_read_sector(input_ctx, sector, false, sector_data, &read_length); + if(use_long) + res = aaruf_read_sector_long(input_ctx, sector, false, sector_data, &read_length); + else + res = aaruf_read_sector(input_ctx, sector, false, sector_data, &read_length); + if(res != AARUF_STATUS_OK) { printf("\nError %d when reading sector %llu from input image.\n", res, (unsigned long long)sector); @@ -189,7 +219,11 @@ int convert(const char *input_path, const char *output_path) } // Write sector to output - res = aaruf_write_sector(output_ctx, sector, false, sector_data, SectorStatusDumped, read_length); + if(use_long) + res = aaruf_write_sector_long(output_ctx, sector, false, sector_data, SectorStatusDumped, read_length); + else + res = aaruf_write_sector(output_ctx, sector, false, sector_data, SectorStatusDumped, read_length); + if(res != AARUF_STATUS_OK) { printf("\nError %d when writing sector %llu to output image.\n", res, (unsigned long long)sector); diff --git a/tool/usage.c b/tool/usage.c index 237ed89..7c94e6b 100644 --- a/tool/usage.c +++ b/tool/usage.c @@ -127,9 +127,11 @@ void usage_cli_compare() void usage_convert() { printf("\nUsage:\n"); - printf(" aaruformattool convert \n\n"); + printf(" aaruformattool convert [-l] \n\n"); printf("Converts an AaruFormat image by reading all sectors from input and writing them to output.\n"); printf("Arguments:\n"); printf(" Path to input image file.\n"); printf(" Path to output image file.\n"); + printf("Options:\n"); + printf(" -l Use long sector read/write (includes tags and metadata).\n"); }