grub-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH v4 2/2] mkconfig: use distro sorts when available


From: Robbie Harwood
Subject: [PATCH v4 2/2] mkconfig: use distro sorts when available
Date: Fri, 21 Jan 2022 16:01:55 -0500

For Debian-likes and Fedora-likes, use the distribution's sorting tools
to determine the latest package before falling back to sort(1).  While
Fedora's rpmdev-vercmp(1) is likely unavailable, Debian's is built into
dpkg itself, and hence likely present.

Refactor to remove unused code and make it easy to add other package
managers as needed.

Signed-off-by: Robbie Harwood <rharwood@redhat.com>
---
 util/grub-mkconfig_lib.in | 94 ++++++++++++++++++++++-----------------
 1 file changed, 53 insertions(+), 41 deletions(-)

diff --git a/util/grub-mkconfig_lib.in b/util/grub-mkconfig_lib.in
index 23d41475f..885baa424 100644
--- a/util/grub-mkconfig_lib.in
+++ b/util/grub-mkconfig_lib.in
@@ -200,62 +200,74 @@ grub_file_is_not_garbage ()
   return 0
 }
 
-version_sort ()
+# A $SORT function returns 0 if $1 is newer than $2, and 1 otherwise.  Other
+# package managers can be plugged in here as needed with their own functions.
+sort_dpkg ()
 {
-  case $version_sort_sort_has_v in
-    yes)
-      LC_ALL=C sort -V;;
-    no)
-      LC_ALL=C sort -n;;
-    *)
-      if sort -V </dev/null > /dev/null 2>&1; then
-        version_sort_sort_has_v=yes
-       LC_ALL=C sort -V
-      else
-        version_sort_sort_has_v=no
-        LC_ALL=C sort -n
-      fi;;
-   esac
+  left="`echo "$1" | sed -e "s/^[^0-9]*//"`"
+  right="`echo "$2" | sed -e "s/^[^0-9]*//"`"
+  dpkg --compare-versions "$left" gt "$right"
 }
 
-version_test_numeric ()
+sort_rpm ()
 {
-  version_test_numeric_a="$1"
-  version_test_numeric_cmp="$2"
-  version_test_numeric_b="$3"
-  if [ "$version_test_numeric_a" = "$version_test_numeric_b" ] ; then
-    case "$version_test_numeric_cmp" in
-      ge|eq|le) return 0 ;;
-      gt|lt) return 1 ;;
-    esac
-  fi
-  if [ "$version_test_numeric_cmp" = "lt" ] ; then
-    version_test_numeric_c="$version_test_numeric_a"
-    version_test_numeric_a="$version_test_numeric_b"
-    version_test_numeric_b="$version_test_numeric_c"
-  fi
-  if (echo "$version_test_numeric_a" ; echo "$version_test_numeric_b") | 
version_sort | head -n 1 | grep -qx "$version_test_numeric_b" ; then
-    return 0
-  else
-    return 1
+  left="`echo "$1" | sed -e "s/^[^0-9]*//"`"
+  right="`echo "$2" | sed -e "s/^[^0-9]*//"`"
+  rpmdev-vercmp "$left" "$right" >/dev/null
+  if [ $? -eq 12 ]; then
+    return 0;
   fi
+  return 1;
+}
+
+sort_V ()
+{
+  left="`echo "$1" | sed -e "s/[^-]*-//" -e "s/_/-/g"`"
+  right="`echo "$2" | sed -e "s/[^-]*-//" -e "s/_/-/g"`"
+  printf "$left\n$right\n" | LC_ALL=C sort -V | head -n1 | grep -qx "$right"
+}
+
+sort_n ()
+{
+  left="`echo "$1" | sed -e "s/[^-]*-//" -e "s/_/-/g"`"
+  right="`echo "$2" | sed -e "s/[^-]*-//" -e "s/_/-/g"`"
+  printf "$left\n$right\n" | LC_ALL=C sort -n | head -n1 | grep -qx "$right"
 }
 
 version_test_gt ()
 {
-  version_test_gt_a="`echo "$1" | sed -e "s/[^-]*-//" -e "s/_/-/g"`"
-  version_test_gt_b="`echo "$2" | sed -e "s/[^-]*-//" -e "s/_/-/g"`"
-  version_test_gt_cmp=gt
+  version_test_gt_a="$1"
+  version_test_gt_b="$2"
+
   if [ "x$version_test_gt_b" = "x" ] ; then
     return 0
   fi
   case "$version_test_gt_a:$version_test_gt_b" in
     *.old:*.old) ;;
-    *.old:*) version_test_gt_a="`echo "$version_test_gt_a" | sed -e 
's/\.old$//'`" ; version_test_gt_cmp=gt ;;
-    *:*.old) version_test_gt_b="`echo "$version_test_gt_b" | sed -e 
's/\.old$//'`" ; version_test_gt_cmp=ge ;;
+    *.old:*) version_test_gt_a="`echo "$version_test_gt_a" | sed -e 
's/\.old$//'`" ;;
+    *:*.old) version_test_gt_b="`echo "$version_test_gt_b" | sed -e 
's/\.old$//'`" ;;
   esac
-  version_test_numeric "$version_test_gt_a" "$version_test_gt_cmp" 
"$version_test_gt_b"
-  return "$?"
+
+  if [ "$version_test_gt_a" = "$version_test_gt_b" ]; then
+    return 1;
+  fi
+
+  if [ x"$SORT" = x ]; then
+    if [ -f /etc/debian_version ] && command -v dpkg >/dev/null; then
+      SORT=sort_dpkg
+    elif [ -f /etc/redhat-release ] && command -v rpmdev-vercmp >/dev/null; 
then
+      SORT=sort_rpm
+    elif command -v rpmdev-vercmp >/dev/null; then
+      SORT=sort_rpm
+    elif command -v dpkg >/dev/null; then
+      SORT=sort_dpkg
+    elif sort -V </dev/null > /dev/null 2>&1; then
+      SORT=sort_V
+    else
+      SORT=sort_n
+    fi
+  fi
+  $SORT "$version_test_gt_a" "$version_test_gt_b"
 }
 
 version_find_latest ()
-- 
2.34.1




reply via email to

[Prev in Thread] Current Thread [Next in Thread]