Blame SOURCES/0241-blscfg-sort-everything-with-rpm-package-comparison.patch

8631a2
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
8631a2
From: Peter Jones <pjones@redhat.com>
8631a2
Date: Mon, 15 Oct 2018 15:08:33 -0400
8631a2
Subject: [PATCH] blscfg: sort everything with rpm *package* comparison
8631a2
8631a2
This makes comparisons use the n-v-r tuple, and compare name with name,
8631a2
version with version, and release with release.
8631a2
8631a2
Related: rhbz#1638103
8631a2
8631a2
Signed-off-by: Peter Jones <pjones@redhat.com>
8631a2
---
8631a2
 grub-core/commands/blscfg.c | 118 ++++++++++++++++++++++++++++++++++++++++----
8631a2
 1 file changed, 108 insertions(+), 10 deletions(-)
8631a2
8631a2
diff --git a/grub-core/commands/blscfg.c b/grub-core/commands/blscfg.c
8631a2
index 3847572dabd..347128c9ddd 100644
8631a2
--- a/grub-core/commands/blscfg.c
8631a2
+++ b/grub-core/commands/blscfg.c
8631a2
@@ -206,7 +206,7 @@ static int vercmp(const char * a, const char * b)
8631a2
     int isnum;
8631a2
     int ret = 0;
8631a2
 
8631a2
-  grub_dprintf("blscfg", "%s got here\n", __func__);
8631a2
+    grub_dprintf("blscfg", "%s got here\n", __func__);
8631a2
     if (!grub_strcmp(a, b))
8631a2
 	    return 0;
8631a2
 
8631a2
@@ -315,6 +315,81 @@ finish:
8631a2
     return ret;
8631a2
 }
8631a2
 
8631a2
+/* returns name/version/release */
8631a2
+/* NULL string pointer returned if nothing found */
8631a2
+static void
8631a2
+split_package_string (char *package_string, char **name,
8631a2
+                     char **version, char **release)
8631a2
+{
8631a2
+  char *package_version, *package_release;
8631a2
+
8631a2
+  /* Release */
8631a2
+  package_release = grub_strrchr (package_string, '-');
8631a2
+
8631a2
+  if (package_release != NULL)
8631a2
+      *package_release++ = '\0';
8631a2
+
8631a2
+  *release = package_release;
8631a2
+
8631a2
+  if (name == NULL)
8631a2
+    {
8631a2
+      *version = package_string;
8631a2
+    }
8631a2
+  else
8631a2
+    {
8631a2
+      /* Version */
8631a2
+      package_version = grub_strrchr(package_string, '-');
8631a2
+
8631a2
+      if (package_version != NULL)
8631a2
+	*package_version++ = '\0';
8631a2
+
8631a2
+      *version = package_version;
8631a2
+      /* Name */
8631a2
+      *name = package_string;
8631a2
+    }
8631a2
+
8631a2
+  /* Bubble up non-null values from release to name */
8631a2
+  if (name != NULL && *name == NULL)
8631a2
+    {
8631a2
+      *name = (*version == NULL ? *release : *version);
8631a2
+      *version = *release;
8631a2
+      *release = NULL;
8631a2
+    }
8631a2
+  if (*version == NULL)
8631a2
+    {
8631a2
+      *version = *release;
8631a2
+      *release = NULL;
8631a2
+    }
8631a2
+}
8631a2
+
8631a2
+static int
8631a2
+split_cmp(char *nvr0, char *nvr1, int has_name)
8631a2
+{
8631a2
+  int ret = 0;
8631a2
+  char *name0, *version0, *release0;
8631a2
+  char *name1, *version1, *release1;
8631a2
+
8631a2
+  split_package_string(nvr0, has_name ? &name0 : NULL, &version0, &release0);
8631a2
+  split_package_string(nvr1, has_name ? &name1 : NULL, &version1, &release1);
8631a2
+
8631a2
+  if (has_name)
8631a2
+    {
8631a2
+      ret = vercmp(name0 == NULL ? "" : name0,
8631a2
+		   name1 == NULL ? "" : name1);
8631a2
+      if (ret != 0)
8631a2
+	return ret;
8631a2
+    }
8631a2
+
8631a2
+  ret = vercmp(version0 == NULL ? "" : version0,
8631a2
+	       version1 == NULL ? "" : version1);
8631a2
+  if (ret != 0)
8631a2
+    return ret;
8631a2
+
8631a2
+  ret = vercmp(release0 == NULL ? "" : release0,
8631a2
+	       release1 == NULL ? "" : release1);
8631a2
+  return ret;
8631a2
+}
8631a2
+
8631a2
 /* return 1: p0 is newer than p1 */
8631a2
 /*        0: p0 and p1 are the same version */
8631a2
 /*       -1: p1 is newer than p0 */
8631a2
@@ -323,18 +398,41 @@ static int bls_cmp(const void *p0, const void *p1, void *state)
8631a2
   struct bls_entry * e0 = *(struct bls_entry **)p0;
8631a2
   struct bls_entry * e1 = *(struct bls_entry **)p1;
8631a2
   bool use_version = *(bool *)state;
8631a2
-  const char *v0, *v1;
8631a2
-  int r;
8631a2
+  char *v0, *v1;
8631a2
+  char *id0, *id1;
8631a2
+  int l, r;
8631a2
 
8631a2
-  if (use_version) {
8631a2
-    v0 = bls_get_val(e0, "version", NULL);
8631a2
-    v1 = bls_get_val(e1, "version", NULL);
8631a2
+  if (use_version)
8631a2
+    {
8631a2
+      v0 = grub_strdup(bls_get_val(e0, "version", NULL));
8631a2
+      v1 = grub_strdup(bls_get_val(e1, "version", NULL));
8631a2
 
8631a2
-    if ((r = vercmp(v0, v1)) != 0)
8631a2
-      return r;
8631a2
-  }
8631a2
+      r = split_cmp(v0, v1, 0);
8631a2
 
8631a2
-  return vercmp(e0->filename, e1->filename);
8631a2
+      grub_free(v0);
8631a2
+      grub_free(v1);
8631a2
+
8631a2
+      if (r != 0)
8631a2
+	return r;
8631a2
+    }
8631a2
+
8631a2
+  id0 = grub_strdup(e0->filename);
8631a2
+  id1 = grub_strdup(e1->filename);
8631a2
+
8631a2
+  l = grub_strlen(id0);
8631a2
+  if (l > 5 && grub_strcmp(id0 + l - 5, ".conf"))
8631a2
+    id0[l-5] = '\0';
8631a2
+
8631a2
+  l = grub_strlen(id1);
8631a2
+  if (l > 5 && grub_strcmp(id1 + l - 5, ".conf"))
8631a2
+    id1[l-5] = '\0';
8631a2
+
8631a2
+  r = split_cmp(id0, id1, 1);
8631a2
+
8631a2
+  grub_free(id0);
8631a2
+  grub_free(id1);
8631a2
+
8631a2
+  return r;
8631a2
 }
8631a2
 
8631a2
 struct read_entry_info {