Add sorting function for DumpExtent arrays and implement sorting in dump processing

This commit is contained in:
2025-10-05 16:31:21 +01:00
parent 0381041dab
commit cfce456a5c
4 changed files with 40 additions and 2 deletions

View File

@@ -55,5 +55,6 @@ bool set_ddt_multi_level_v2(aaruformatContext *ctx, uint64_t sector_address
aaru_options parse_options(const char *options); aaru_options parse_options(const char *options);
uint64_t get_filetime_uint64(); uint64_t get_filetime_uint64();
int32_t aaruf_close_current_block(aaruformatContext *ctx); int32_t aaruf_close_current_block(aaruformatContext *ctx);
int compare_extents(const void *a, const void *b);
#endif // LIBAARUFORMAT_INTERNAL_H #endif // LIBAARUFORMAT_INTERNAL_H

View File

@@ -22,6 +22,7 @@
#include <stdlib.h> #include <stdlib.h>
#include "aaruformat.h" #include "aaruformat.h"
#include "internal.h"
#include "log.h" #include "log.h"
/** /**
@@ -329,7 +330,13 @@ void process_dumphw_block(aaruformatContext *ctx, const IndexEntry *entry)
continue; continue;
} }
// TODO: qsort() // Sort extents by start sector for efficient lookup and validation
if(ctx->dumpHardwareEntriesWithData[e].entry.extents > 0 && ctx->dumpHardwareEntriesWithData[e].extents != NULL)
{
qsort(ctx->dumpHardwareEntriesWithData[e].extents, ctx->dumpHardwareEntriesWithData[e].entry.extents,
sizeof(DumpExtent), compare_extents);
TRACE("Sorted %u extents for entry %u", ctx->dumpHardwareEntriesWithData[e].entry.extents, e);
}
} }
TRACE("Exiting process_dumphw_block()"); TRACE("Exiting process_dumphw_block()");
} }

View File

@@ -17,8 +17,10 @@
*/ */
#include <stdint.h> #include <stdint.h>
#include <stdlib.h>
#include "aaruformat.h" #include "aaruformat.h"
#include "internal.h"
#include "log.h" #include "log.h"
/** /**
@@ -675,7 +677,12 @@ int32_t aaruf_set_dumphw(void *context, uint8_t *data, size_t length)
memcpy(copy[e].extents, data + pos, sizeof(DumpExtent) * ctx->dumpHardwareEntriesWithData->entry.extents); memcpy(copy[e].extents, data + pos, sizeof(DumpExtent) * ctx->dumpHardwareEntriesWithData->entry.extents);
pos += sizeof(DumpExtent) * ctx->dumpHardwareEntriesWithData->entry.extents; pos += sizeof(DumpExtent) * ctx->dumpHardwareEntriesWithData->entry.extents;
// TODO: qsort() // Sort extents by start sector for efficient lookup and validation
if(copy[e].entry.extents > 0 && copy[e].extents != NULL)
{
qsort(copy[e].extents, copy[e].entry.extents, sizeof(DumpExtent), compare_extents);
TRACE("Sorted %u extents for entry %d", copy[e].entry.extents, e);
}
} }
// Free old data // Free old data

View File

@@ -436,3 +436,26 @@ int32_t aaruf_get_xml_mediatype(const int32_t type)
return BlockMedia; return BlockMedia;
} }
} }
/**
* @brief Comparison function for sorting DumpExtent arrays by start sector.
*
* This function is used by qsort() to order dump extents in ascending order based on their
* start sector values. Extents with lower start sectors will appear first in the sorted array.
* This ordering is important for efficient extent lookup and validation during image operations.
*
* @param a Pointer to the first DumpExtent to compare.
* @param b Pointer to the second DumpExtent to compare.
* @return Negative value if a->start < b->start, zero if equal, positive if a->start > b->start.
*/
int compare_extents(const void *a, const void *b)
{
const DumpExtent *extent_a = a;
const DumpExtent *extent_b = b;
if(extent_a->start < extent_b->start)
return -1;
if(extent_a->start > extent_b->start)
return 1;
return 0;
}