bootspec: implement sorting by tries left/done, to match what sd-boot does
authorLennart Poettering <lennart@poettering.net>
Fri, 5 Jul 2024 07:52:58 +0000 (09:52 +0200)
committerLuca Boccassi <bluca@debian.org>
Mon, 22 Jul 2024 09:31:02 +0000 (10:31 +0100)
(cherry picked from commit 35451a32043504013eed5725c8be46b36ccdf71a)

src/shared/bootspec.c

index 4bc3ae73ab1e61b0e83e015b8ec8a7d2eb3d1af8..4f00e483b511444f4fef6747308a984e999796c7 100644 (file)
@@ -505,6 +505,12 @@ static int boot_entry_compare(const BootEntry *a, const BootEntry *b) {
         assert(a);
         assert(b);
 
+        /* This mimics a function of the same name in src/boot/efi/sd-boot.c */
+
+        r = CMP(a->tries_left == 0, b->tries_left == 0);
+        if (r != 0)
+                return r;
+
         r = CMP(!a->sort_key, !b->sort_key);
         if (r != 0)
                 return r;
@@ -523,7 +529,18 @@ static int boot_entry_compare(const BootEntry *a, const BootEntry *b) {
                         return r;
         }
 
-        return -strverscmp_improved(a->id, b->id);
+        r = -strverscmp_improved(a->id, b->id);
+        if (r != 0)
+                return r;
+
+        if (a->tries_left != UINT_MAX || b->tries_left != UINT_MAX)
+                return 0;
+
+        r = -CMP(a->tries_left, b->tries_left);
+        if (r != 0)
+                return r;
+
+        return CMP(a->tries_done, b->tries_done);
 }
 
 static int config_check_inode_relevant_and_unseen(BootConfig *config, int fd, const char *fname) {