Blame SOURCES/0055-Add-usr-libexec-rpm-sort.patch

24fce8
From 4e0e028b21070f956b144bd530230d01b9015d73 Mon Sep 17 00:00:00 2001
24fce8
From: Peter Jones <pjones@redhat.com>
24fce8
Date: Fri, 12 Oct 2018 16:39:37 -0400
24fce8
Subject: [PATCH 55/55] Add /usr/libexec/rpm-sort
24fce8
24fce8
Signed-off-by: Peter Jones <pjones@redhat.com>
24fce8
---
24fce8
 rpm-sort.c | 355 +++++++++++++++++++++++++++++++++++++++++++++++++++++
24fce8
 Makefile   |   9 +-
24fce8
 .gitignore |   1 +
24fce8
 3 files changed, 363 insertions(+), 2 deletions(-)
24fce8
 create mode 100644 rpm-sort.c
24fce8
24fce8
diff --git a/rpm-sort.c b/rpm-sort.c
24fce8
new file mode 100644
24fce8
index 00000000000..f19635645ba
24fce8
--- /dev/null
24fce8
+++ b/rpm-sort.c
24fce8
@@ -0,0 +1,355 @@
24fce8
+#define _GNU_SOURCE
24fce8
+
24fce8
+#include <stdio.h>
24fce8
+#include <stdlib.h>
24fce8
+#include <unistd.h>
24fce8
+#include <errno.h>
24fce8
+#include <assert.h>
24fce8
+#include <argp.h>
24fce8
+#include <rpm/rpmlib.h>
24fce8
+#include <err.h>
24fce8
+
24fce8
+typedef enum {
24fce8
+        RPMNVRCMP,
24fce8
+        VERSNVRCMP,
24fce8
+        RPMVERCMP,
24fce8
+        STRVERSCMP,
24fce8
+} comparitors;
24fce8
+
24fce8
+static comparitors comparitor = RPMNVRCMP;
24fce8
+
24fce8
+static inline void *xmalloc(size_t sz)
24fce8
+{
24fce8
+        void *ret = malloc(sz);
24fce8
+
24fce8
+        assert(sz == 0 || ret != NULL);
24fce8
+        return ret;
24fce8
+}
24fce8
+
24fce8
+static inline void *xrealloc(void *p, size_t sz)
24fce8
+{
24fce8
+        void *ret = realloc(p, sz);
24fce8
+
24fce8
+        assert(sz == 0 || ret != NULL);
24fce8
+        return ret;
24fce8
+}
24fce8
+
24fce8
+static inline char *xstrdup(const char * const s)
24fce8
+{
24fce8
+        void *ret = strdup(s);
24fce8
+
24fce8
+        assert(s == NULL || ret != NULL);
24fce8
+        return ret;
24fce8
+}
24fce8
+
24fce8
+static size_t
24fce8
+read_file (const char *input, char **ret)
24fce8
+{
24fce8
+  FILE *in;
24fce8
+  size_t s;
24fce8
+  size_t sz = 2048;
24fce8
+  size_t offset = 0;
24fce8
+  char *text;
24fce8
+
24fce8
+  if (!strcmp(input, "-"))
24fce8
+    in = stdin;
24fce8
+  else
24fce8
+    in = fopen(input, "r");
24fce8
+
24fce8
+  text = xmalloc (sz);
24fce8
+
24fce8
+  if (!in)
24fce8
+    err(1, "cannot open `%s'", input);
24fce8
+
24fce8
+  while ((s = fread (text + offset, 1, sz - offset, in)) != 0)
24fce8
+    {
24fce8
+      offset += s;
24fce8
+      if (sz - offset == 0)
24fce8
+	{
24fce8
+	  sz += 2048;
24fce8
+	  text = xrealloc (text, sz);
24fce8
+	}
24fce8
+    }
24fce8
+
24fce8
+  text[offset] = '\0';
24fce8
+  *ret = text;
24fce8
+
24fce8
+  if (in != stdin)
24fce8
+    fclose(in);
24fce8
+
24fce8
+  return offset + 1;
24fce8
+}
24fce8
+
24fce8
+/* returns name/version/release */
24fce8
+/* NULL string pointer returned if nothing found */
24fce8
+static void
24fce8
+split_package_string (char *package_string, char **name,
24fce8
+                     char **version, char **release)
24fce8
+{
24fce8
+  char *package_version, *package_release;
24fce8
+
24fce8
+  /* Release */
24fce8
+  package_release = strrchr (package_string, '-');
24fce8
+
24fce8
+  if (package_release != NULL)
24fce8
+      *package_release++ = '\0';
24fce8
+
24fce8
+  *release = package_release;
24fce8
+
24fce8
+  /* Version */
24fce8
+  package_version = strrchr(package_string, '-');
24fce8
+
24fce8
+  if (package_version != NULL)
24fce8
+      *package_version++ = '\0';
24fce8
+
24fce8
+  *version = package_version;
24fce8
+  /* Name */
24fce8
+  *name = package_string;
24fce8
+
24fce8
+  /* Bubble up non-null values from release to name */
24fce8
+  if (*name == NULL)
24fce8
+    {
24fce8
+      *name = (*version == NULL ? *release : *version);
24fce8
+      *version = *release;
24fce8
+      *release = NULL;
24fce8
+    }
24fce8
+  if (*version == NULL)
24fce8
+    {
24fce8
+      *version = *release;
24fce8
+      *release = NULL;
24fce8
+    }
24fce8
+}
24fce8
+
24fce8
+static int
24fce8
+cmprpmversp(const void *p1, const void *p2)
24fce8
+{
24fce8
+        return rpmvercmp(*(char * const *)p1, *(char * const *)p2);
24fce8
+}
24fce8
+
24fce8
+static int
24fce8
+cmpstrversp(const void *p1, const void *p2)
24fce8
+{
24fce8
+        return strverscmp(*(char * const *)p1, *(char * const *)p2);
24fce8
+}
24fce8
+
24fce8
+/*
24fce8
+ * package name-version-release comparator for qsort
24fce8
+ * expects p, q which are pointers to character strings (char *)
24fce8
+ * which will not be altered in this function
24fce8
+ */
24fce8
+static int
24fce8
+package_version_compare (const void *p, const void *q)
24fce8
+{
24fce8
+  char *local_p, *local_q;
24fce8
+  char *lhs_name, *lhs_version, *lhs_release;
24fce8
+  char *rhs_name, *rhs_version, *rhs_release;
24fce8
+  int vercmpflag = 0;
24fce8
+  int (*cmp)(const char *s1, const char *s2);
24fce8
+
24fce8
+  switch(comparitor)
24fce8
+    {
24fce8
+    default: /* just to shut up -Werror=maybe-uninitialized */
24fce8
+    case RPMNVRCMP:
24fce8
+      cmp = rpmvercmp;
24fce8
+      break;
24fce8
+    case VERSNVRCMP:
24fce8
+      cmp = strverscmp;
24fce8
+      break;
24fce8
+    case RPMVERCMP:
24fce8
+      return cmprpmversp(p, q);
24fce8
+      break;
24fce8
+    case STRVERSCMP:
24fce8
+      return cmpstrversp(p, q);
24fce8
+      break;
24fce8
+    }
24fce8
+
24fce8
+  local_p = alloca (strlen (*(char * const *)p) + 1);
24fce8
+  local_q = alloca (strlen (*(char * const *)q) + 1);
24fce8
+
24fce8
+  /* make sure these allocated */
24fce8
+  assert (local_p);
24fce8
+  assert (local_q);
24fce8
+
24fce8
+  strcpy (local_p, *(char * const *)p);
24fce8
+  strcpy (local_q, *(char * const *)q);
24fce8
+
24fce8
+  split_package_string (local_p, &lhs_name, &lhs_version, &lhs_release);
24fce8
+  split_package_string (local_q, &rhs_name, &rhs_version, &rhs_release);
24fce8
+
24fce8
+  /* Check Name and return if unequal */
24fce8
+  vercmpflag = cmp ((lhs_name == NULL ? "" : lhs_name),
24fce8
+                    (rhs_name == NULL ? "" : rhs_name));
24fce8
+  if (vercmpflag != 0)
24fce8
+    return vercmpflag;
24fce8
+
24fce8
+  /* Check version and return if unequal */
24fce8
+  vercmpflag = cmp ((lhs_version == NULL ? "" : lhs_version),
24fce8
+                    (rhs_version == NULL ? "" : rhs_version));
24fce8
+  if (vercmpflag != 0)
24fce8
+    return vercmpflag;
24fce8
+
24fce8
+  /* Check release and return the version compare value */
24fce8
+  vercmpflag = cmp ((lhs_release == NULL ? "" : lhs_release),
24fce8
+                    (rhs_release == NULL ? "" : rhs_release));
24fce8
+
24fce8
+  return vercmpflag;
24fce8
+}
24fce8
+
24fce8
+static void
24fce8
+add_input (const char *filename, char ***package_names, size_t *n_package_names)
24fce8
+{
24fce8
+  char *orig_input_buffer = NULL;
24fce8
+  char *input_buffer;
24fce8
+  char *position_of_newline;
24fce8
+  char **names = *package_names;
24fce8
+  char **new_names = NULL;
24fce8
+  size_t n_names = *n_package_names;
24fce8
+
24fce8
+  if (!*package_names)
24fce8
+    new_names = names = xmalloc (sizeof (char *) * 2);
24fce8
+
24fce8
+  if (read_file (filename, &orig_input_buffer) < 2)
24fce8
+    {
24fce8
+      if (new_names)
24fce8
+	free (new_names);
24fce8
+      if (orig_input_buffer)
24fce8
+	free (orig_input_buffer);
24fce8
+      return;
24fce8
+    }
24fce8
+
24fce8
+  input_buffer = orig_input_buffer;
24fce8
+  while (input_buffer && *input_buffer &&
24fce8
+	 (position_of_newline = strchrnul (input_buffer, '\n')))
24fce8
+    {
24fce8
+      size_t sz = position_of_newline - input_buffer;
24fce8
+      char *new;
24fce8
+
24fce8
+      if (sz == 0)
24fce8
+	{
24fce8
+	  input_buffer = position_of_newline + 1;
24fce8
+	  continue;
24fce8
+	}
24fce8
+
24fce8
+      new = xmalloc (sz+1);
24fce8
+      strncpy (new, input_buffer, sz);
24fce8
+      new[sz] = '\0';
24fce8
+
24fce8
+      names = xrealloc (names, sizeof (char *) * (n_names + 1));
24fce8
+      names[n_names] = new;
24fce8
+      n_names++;
24fce8
+
24fce8
+      /* move buffer ahead to next line */
24fce8
+      input_buffer = position_of_newline + 1;
24fce8
+      if (*position_of_newline == '\0')
24fce8
+	input_buffer = NULL;
24fce8
+    }
24fce8
+
24fce8
+  free (orig_input_buffer);
24fce8
+
24fce8
+  *package_names = names;
24fce8
+  *n_package_names = n_names;
24fce8
+}
24fce8
+
24fce8
+static char *
24fce8
+help_filter (int key, const char *text, void *input __attribute__ ((unused)))
24fce8
+{
24fce8
+  return (char *)text;
24fce8
+}
24fce8
+
24fce8
+static struct argp_option options[] = {
24fce8
+  { "comparitor", 'c', "COMPARITOR", 0, "[rpm-nvr-cmp|vers-nvr-cmp|rpmvercmp|strverscmp]", 0},
24fce8
+  { 0, }
24fce8
+};
24fce8
+
24fce8
+struct arguments
24fce8
+{
24fce8
+  size_t ninputs;
24fce8
+  size_t input_max;
24fce8
+  char **inputs;
24fce8
+};
24fce8
+
24fce8
+static error_t
24fce8
+argp_parser (int key, char *arg, struct argp_state *state)
24fce8
+{
24fce8
+  struct arguments *arguments = state->input;
24fce8
+  switch (key)
24fce8
+    {
24fce8
+    case 'c':
24fce8
+      if (!strcmp(arg, "rpm-nvr-cmp") || !strcmp(arg, "rpmnvrcmp"))
24fce8
+        comparitor = RPMNVRCMP;
24fce8
+      else if (!strcmp(arg, "vers-nvr-cmp") || !strcmp(arg, "versnvrcmp"))
24fce8
+        comparitor = VERSNVRCMP;
24fce8
+      else if (!strcmp(arg, "rpmvercmp"))
24fce8
+        comparitor = RPMVERCMP;
24fce8
+      else if (!strcmp(arg, "strverscmp"))
24fce8
+        comparitor = STRVERSCMP;
24fce8
+      else
24fce8
+        err(1, "Invalid comparitor \"%s\"", arg);
24fce8
+      break;
24fce8
+    case ARGP_KEY_ARG:
24fce8
+      assert (arguments->ninputs < arguments->input_max);
24fce8
+      arguments->inputs[arguments->ninputs++] = xstrdup (arg);
24fce8
+      break;
24fce8
+    default:
24fce8
+      return ARGP_ERR_UNKNOWN;
24fce8
+    }
24fce8
+  return 0;
24fce8
+}
24fce8
+
24fce8
+static struct argp argp = {
24fce8
+  options, argp_parser, "[INPUT_FILES]",
24fce8
+  "Sort a list of strings in RPM version sort order.",
24fce8
+  NULL, help_filter, NULL
24fce8
+};
24fce8
+
24fce8
+int
24fce8
+main (int argc, char *argv[])
24fce8
+{
24fce8
+  struct arguments arguments;
24fce8
+  char **package_names = NULL;
24fce8
+  size_t n_package_names = 0;
24fce8
+  int i;
24fce8
+
24fce8
+  memset (&arguments, 0, sizeof (struct arguments));
24fce8
+  arguments.input_max = argc+1;
24fce8
+  arguments.inputs = xmalloc ((arguments.input_max + 1)
24fce8
+			      * sizeof (arguments.inputs[0]));
24fce8
+  memset (arguments.inputs, 0, (arguments.input_max + 1)
24fce8
+	  * sizeof (arguments.inputs[0]));
24fce8
+
24fce8
+  /* Parse our arguments */
24fce8
+  if (argp_parse (&argp, argc, argv, 0, 0, &arguments) != 0)
24fce8
+    errx(1, "%s", "Error in parsing command line arguments\n");
24fce8
+
24fce8
+  /* If there's no inputs in argv, add one for stdin */
24fce8
+  if (!arguments.ninputs)
24fce8
+    {
24fce8
+      arguments.ninputs = 1;
24fce8
+      arguments.inputs[0] = xmalloc (2);
24fce8
+      strcpy(arguments.inputs[0], "-");
24fce8
+    }
24fce8
+
24fce8
+  for (i = 0; i < arguments.ninputs; i++)
24fce8
+    add_input(arguments.inputs[i], &package_names, &n_package_names);
24fce8
+
24fce8
+  if (package_names == NULL || n_package_names < 1)
24fce8
+    errx(1, "Invalid input");
24fce8
+
24fce8
+  qsort (package_names, n_package_names, sizeof (char *),
24fce8
+	 package_version_compare);
24fce8
+
24fce8
+  /* send sorted list to stdout */
24fce8
+  for (i = 0; i < n_package_names; i++)
24fce8
+    {
24fce8
+      fprintf (stdout, "%s\n", package_names[i]);
24fce8
+      free (package_names[i]);
24fce8
+    }
24fce8
+
24fce8
+  free (package_names);
24fce8
+  for (i = 0; i < arguments.ninputs; i++)
24fce8
+    free (arguments.inputs[i]);
24fce8
+
24fce8
+  free (arguments.inputs);
24fce8
+
24fce8
+  return 0;
24fce8
+}
24fce8
diff --git a/Makefile b/Makefile
24fce8
index 44fc12f0c1b..8eeec80da21 100644
24fce8
--- a/Makefile
24fce8
+++ b/Makefile
24fce8
@@ -34,7 +34,7 @@ endif
24fce8
 
24fce8
 grubby_LIBS = -lblkid -lpopt
24fce8
 
24fce8
-all: grubby
24fce8
+all: grubby rpm-sort
24fce8
 
24fce8
 debug : clean
24fce8
 	$(MAKE) CFLAGS="${CFLAGS} -DDEBUG=1" all
24fce8
@@ -57,12 +57,17 @@ install: all
24fce8
 		install -m 755 grubby $(DESTDIR)$(PREFIX)$(sbindir) ; \
24fce8
 		install -m 644 grubby.8 $(DESTDIR)/$(mandir)/man8 ; \
24fce8
 	fi
24fce8
+	install -m 755 -d $(DESTDIR)$(PREFIX)$(libexecdir)/grubby/
24fce8
+	install -m 755 rpm-sort $(DESTDIR)$(PREFIX)$(libexecdir)/grubby/rpm-sort
24fce8
 
24fce8
 grubby:: $(OBJECTS)
24fce8
 	$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^ $(grubby_LIBS)
24fce8
 
24fce8
+rpm-sort::rpm-sort.o
24fce8
+	$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^ -lrpm
24fce8
+
24fce8
 clean:
24fce8
-	rm -f *.o grubby *~
24fce8
+	rm -f *.o grubby rpm-sort *~
24fce8
 
24fce8
 GITTAG = $(VERSION)-1
24fce8
 
24fce8
diff --git a/.gitignore b/.gitignore
24fce8
index 1c00ff7c5ed..bcaf1494f61 100644
24fce8
--- a/.gitignore
24fce8
+++ b/.gitignore
24fce8
@@ -1,4 +1,5 @@
24fce8
 grubby
24fce8
+rpm-sort
24fce8
 version.h
24fce8
 *.o
24fce8
 core.*
24fce8
-- 
24fce8
2.17.1
24fce8