Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1nAdC0-0002yG-L0 for mharc-grub-devel@gnu.org; Thu, 20 Jan 2022 14:33:56 -0500 Received: from eggs.gnu.org ([209.51.188.92]:35252) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nAdBx-0002j7-FH for grub-devel@gnu.org; Thu, 20 Jan 2022 14:33:53 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:23102) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nAdBp-0000R3-D9 for grub-devel@gnu.org; Thu, 20 Jan 2022 14:33:47 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1642707219; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=8CuUi2xQJnsOntYnP4vEkqVy13oN4RihfsVdyrfqm/s=; b=A4YLHNdsn0mIlijLZPwHlfyrweGRxriEQP4F+rwx6hDcheRaQKt1A+cWf5QpjBAuVruqt0 XuBqgi+HkgEi+0YXT8WGKN3chvNxHbzW5hpNDJJqUJo2oRriyKcrd1d8YmYpeXiPRCT37X QHco6+qYtXnFdrAPVk1FUv0mb/CJHQw= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-588-fTXduiBbPqeunhHyLAyJiA-1; Thu, 20 Jan 2022 14:33:36 -0500 X-MC-Unique: fTXduiBbPqeunhHyLAyJiA-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id DEFF51932482; Thu, 20 Jan 2022 19:33:35 +0000 (UTC) Received: from eesha.redhat.com (unknown [10.22.48.2]) by smtp.corp.redhat.com (Postfix) with ESMTP id 54EEF196F2; Thu, 20 Jan 2022 19:33:35 +0000 (UTC) From: Robbie Harwood To: grub-devel@gnu.org Cc: development@efficientek.com, Robbie Harwood Subject: [PATCH v3 2/2] mkconfig: use distro sorts when available Date: Thu, 20 Jan 2022 14:33:20 -0500 Message-Id: <20220120193320.16249-3-rharwood@redhat.com> In-Reply-To: <20220120193320.16249-1-rharwood@redhat.com> References: <20220120193320.16249-1-rharwood@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=rharwood@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII" Received-SPF: pass client-ip=170.10.133.124; envelope-from=rharwood@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -34 X-Spam_score: -3.5 X-Spam_bar: --- X-Spam_report: (-3.5 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.698, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: The development of GNU GRUB List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 20 Jan 2022 19:33:53 -0000 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 --- util/grub-mkconfig_lib.in | 92 ++++++++++++++++++++++----------------- 1 file changed, 51 insertions(+), 41 deletions(-) diff --git a/util/grub-mkconfig_lib.in b/util/grub-mkconfig_lib.in index 23d41475f..b858cd1a0 100644 --- a/util/grub-mkconfig_lib.in +++ b/util/grub-mkconfig_lib.in @@ -200,62 +200,72 @@ 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 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 command -v rpmdev-vercmp; then + SORT=sort_rpm + elif command -v dpkg >/dev/null; then + SORT=sort_dpkg + elif sort -V /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