Blame SOURCES/0161-Use-BLS-fragment-filename-as-menu-entry-id-and-for-c.patch

d9d99f
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
d9d99f
From: Javier Martinez Canillas <javierm@redhat.com>
d9d99f
Date: Tue, 26 Jun 2018 14:01:26 +0200
d9d99f
Subject: [PATCH] Use BLS fragment filename as menu entry id and for criteria
d9d99f
 to sort
d9d99f
d9d99f
The BLS config filenames are guaranteed to be unique, so they can be
d9d99f
used as GRUB2 entry id and can also be used to sort the menu entries.
d9d99f
d9d99f
Signed-off-by: Javier Martinez Canillas <javierm@redhat.com>
d9d99f
---
d9d99f
 grub-core/commands/blscfg.c | 62 +++++++++------------------------------------
d9d99f
 1 file changed, 12 insertions(+), 50 deletions(-)
d9d99f
d9d99f
diff --git a/grub-core/commands/blscfg.c b/grub-core/commands/blscfg.c
b71686
index 70939a818..cd8659384 100644
d9d99f
--- a/grub-core/commands/blscfg.c
d9d99f
+++ b/grub-core/commands/blscfg.c
d9d99f
@@ -70,6 +70,7 @@ struct bls_entry
d9d99f
 {
d9d99f
   struct keyval **keyvals;
d9d99f
   int nkeyvals;
d9d99f
+  char *filename;
d9d99f
 };
d9d99f
 
d9d99f
 static struct bls_entry **entries;
d9d99f
@@ -166,6 +167,7 @@ static void bls_free_entry(struct bls_entry *entry)
d9d99f
 
d9d99f
   grub_free (entry->keyvals);
d9d99f
   grub_memset (entry, 0, sizeof (*entry));
d9d99f
+  grub_free (entry->filename);
d9d99f
   grub_free (entry);
d9d99f
 }
d9d99f
 
d9d99f
@@ -327,58 +329,12 @@ finish:
d9d99f
 
d9d99f
 typedef int (*void_cmp_t)(void *, void *);
d9d99f
 
d9d99f
-static int nulcmp(char *s0, char *s1, void_cmp_t cmp)
d9d99f
-{
d9d99f
-  grub_dprintf("blscfg", "%s got here\n", __func__);
d9d99f
-  if (s1 && !s0)
d9d99f
-    return 1;
d9d99f
-  if (s0 && !s1)
d9d99f
-    return -1;
d9d99f
-  if (!s0 && !s1)
d9d99f
-    return 0;
d9d99f
-  if (cmp)
d9d99f
-    return cmp(s0, s1);
d9d99f
-  return grub_strcmp(s0, s1);
d9d99f
-}
d9d99f
-
d9d99f
-static int
d9d99f
-bls_keyval_cmp(struct bls_entry *e0, struct bls_entry *e1, const char *keyname)
d9d99f
-{
d9d99f
-  char *val0, *val1;
d9d99f
-
d9d99f
-  val0 = bls_get_val (e0, keyname, NULL);
d9d99f
-  val1 = bls_get_val (e1, keyname, NULL);
d9d99f
-
d9d99f
-  if (val1 && !val0)
d9d99f
-    return 1;
d9d99f
-
d9d99f
-  if (val0 && !val1)
d9d99f
-    return -1;
d9d99f
-
d9d99f
-  if (!val0 && !val1)
d9d99f
-    return 0;
d9d99f
-
d9d99f
-  return nulcmp(val0, val1, (void_cmp_t)vercmp);
d9d99f
-}
d9d99f
-
d9d99f
 static int bls_cmp(const void *p0, const void *p1, void *state UNUSED)
d9d99f
 {
d9d99f
   struct bls_entry * e0 = *(struct bls_entry **)p0;
d9d99f
   struct bls_entry * e1 = *(struct bls_entry **)p1;
d9d99f
-  int rc = 0;
d9d99f
 
d9d99f
-  rc = bls_keyval_cmp (e0, e1, "id");
d9d99f
-
d9d99f
-  if (rc == 0)
d9d99f
-    rc = bls_keyval_cmp (e0, e1, "version");
d9d99f
-
d9d99f
-  if (rc == 0)
d9d99f
-    rc = bls_keyval_cmp (e0, e1, "title");
d9d99f
-
d9d99f
-  if (rc == 0)
d9d99f
-    rc = bls_keyval_cmp (e0, e1, "linux");
d9d99f
-
d9d99f
-  return rc;
d9d99f
+  return vercmp(e0->filename, e1->filename);
d9d99f
 }
d9d99f
 
d9d99f
 struct read_entry_info {
d9d99f
@@ -424,6 +380,12 @@ static int read_entry (
d9d99f
   if (!entry)
d9d99f
     goto finish;
d9d99f
 
d9d99f
+  entry->filename = grub_strndup(filename, n - 5);
d9d99f
+  if (!entry->filename)
d9d99f
+    goto finish;
d9d99f
+
d9d99f
+  entry->filename[n - 5] = '\0';
d9d99f
+
d9d99f
   for (;;)
d9d99f
     {
d9d99f
       char *buf;
d9d99f
@@ -548,7 +510,7 @@ static void create_entry (struct bls_entry *entry, const char *cfgfile)
d9d99f
   char *options = NULL;
d9d99f
   char **initrds = NULL;
d9d99f
   char *initrd = NULL;
d9d99f
-  char *id = NULL;
d9d99f
+  char *id = entry->filename;
d9d99f
   char *hotkey = NULL;
d9d99f
 
d9d99f
   char *users = NULL;
d9d99f
@@ -570,7 +532,6 @@ static void create_entry (struct bls_entry *entry, const char *cfgfile)
d9d99f
   title = bls_get_val (entry, "title", NULL);
d9d99f
   options = bls_get_val (entry, "options", NULL);
d9d99f
   initrds = bls_make_list (entry, "initrd", NULL);
d9d99f
-  id = bls_get_val (entry, "id", NULL);
d9d99f
 
d9d99f
   hotkey = bls_get_val (entry, "grub_hotkey", NULL);
d9d99f
   users = bls_get_val (entry, "grub_users", NULL);
d9d99f
@@ -584,7 +545,8 @@ static void create_entry (struct bls_entry *entry, const char *cfgfile)
d9d99f
     argv[i] = args[i-1];
d9d99f
   argv[argc] = NULL;
d9d99f
 
d9d99f
-  grub_dprintf ("blscfg", "adding menu entry for \"%s\"\n", title);
d9d99f
+  grub_dprintf ("blscfg", "adding menu entry for \"%s\" with id \"%s\"\n",
d9d99f
+		title, id);
d9d99f
   if (initrds)
d9d99f
     {
d9d99f
       int initrd_size = sizeof (GRUB_INITRD_CMD);