nalika / rpms / grub2

Forked from rpms/grub2 2 years ago
Clone

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

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