From 8d9fcf658aadb4848038fbcbeb1561f82d21e7c7 Mon Sep 17 00:00:00 2001 From: Thomas Schmitt Date: Sun, 1 Sep 2024 18:41:10 +0200 Subject: [PATCH] =?UTF-8?q?Made=20filelist=20weight=20sorting=20determinis?= =?UTF-8?q?tic=20with=20different=20qsort=20implementations.=20By=20Henrik?= =?UTF-8?q?=20Lindstr=C3=B6m.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libisofs/ecma119.c | 4 ++-- libisofs/filesrc.c | 13 ++++++++++++- libisofs/filesrc.h | 11 +++++++---- 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/libisofs/ecma119.c b/libisofs/ecma119.c index 5c076a9..1db605e 100644 --- a/libisofs/ecma119.c +++ b/libisofs/ecma119.c @@ -4633,8 +4633,8 @@ void ecma119_filesrc_array(Ecma119Node *dir, } else { if (!child->info.file->taken) { filelist[*size] = child->info.file; - child->info.file->taken = 1; (*size)++; + child->info.file->taken = *size; } } } @@ -4658,8 +4658,8 @@ void hidden_filesrc_array(Ecma119Image *t, } else { if (!item->src->taken) { filelist[*size] = item->src; - item->src->taken = 1; (*size)++; + item->src->taken = *size; } } } diff --git a/libisofs/filesrc.c b/libisofs/filesrc.c index 7213f29..be7c8c6 100644 --- a/libisofs/filesrc.c +++ b/libisofs/filesrc.c @@ -218,8 +218,19 @@ static int cmp_by_weight(const void *f1, const void *f2) { IsoFileSrc *f = *((IsoFileSrc**)f1); IsoFileSrc *g = *((IsoFileSrc**)f2); + int cmp; + /* higher weighted first */ - return g->sort_weight - f->sort_weight; + cmp = g->sort_weight - f->sort_weight; + if (cmp) + return cmp; + /* Make the result of qsort(3) stable by avoiding return value 0 as good + as possible. If not f == g, then their ->taken values are supposed + to differ. + */ + if(f->taken == g->taken) + return 0; + return f->taken < g->taken ? -1 : 1; } static diff --git a/libisofs/filesrc.h b/libisofs/filesrc.h index 5dbb742..a89d497 100644 --- a/libisofs/filesrc.h +++ b/libisofs/filesrc.h @@ -32,12 +32,15 @@ struct Iso_File_Src */ unsigned int no_write :1; - /* Is 1 if the object was already put into the filelist array. - */ - unsigned int taken :1; - unsigned int checksum_index :31; + /* Is > 0 if the object was already put into the filelist array. + In this case, the value is the order in which it was inserted, + which can be used for stable sorting of the filelist array in + case of identical file weights. + */ + size_t taken; + /** File Sections of the file in the image */ /* Special sections[0].block values while they are relative before filesrc_writer_compute_data_blocks().