Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1nAgp8-0003Y2-V2 for mharc-grub-devel@gnu.org; Thu, 20 Jan 2022 18:26:36 -0500 Received: from eggs.gnu.org ([209.51.188.92]:60782) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nAgp6-0003VB-3F for grub-devel@gnu.org; Thu, 20 Jan 2022 18:26:32 -0500 Received: from [2607:f8b0:4864:20::536] (port=47048 helo=mail-pg1-x536.google.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1nAgp3-0007mg-Bh for grub-devel@gnu.org; Thu, 20 Jan 2022 18:26:31 -0500 Received: by mail-pg1-x536.google.com with SMTP id i8so6667979pgt.13 for ; Thu, 20 Jan 2022 15:26:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=efficientek-com.20210112.gappssmtp.com; s=20210112; h=date:from:to:cc:subject:message-id:in-reply-to:references:reply-to :mime-version:content-transfer-encoding; bh=NrSfeKd4jPrKLU5D/8wrQp06wd/c+fgPTlccwVW0hlA=; b=5SCm/jdRjHwkt5sJG6+31co47p0G7zw9x0aoMu07g0TYEsXA/0lB+yExPRIGYmeTF7 oyF7G/L+3v4wWqkHccbnFkxSGKd2+rJWFez264OdxP50YCnVhUkqrY+uUdVemgVUpz/R eMwpnCXyLZjirGgdG+FXqMy3A4z11stE891uaYLGqBd/3mhnSwiwZ0Hti1fEVcm5crPr 0ZjNxVioWvlWsXyw42XqcLRAjnr5zdfs30JNhfBNCr+9cyiN3lNmivQ13SPNKm3cnvcO b1J6ya9LKwx4cTS2yKsMclhZnAA0GYUZU8KmoalZFPHn8JaCLihxLnxSyRh0bsSEH2TF FY8A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:in-reply-to :references:reply-to:mime-version:content-transfer-encoding; bh=NrSfeKd4jPrKLU5D/8wrQp06wd/c+fgPTlccwVW0hlA=; b=lrUWhIrASJIa9CMDRK2BnvsPxsY2YZZ47zOkGmcyRTYYvp2+zEbZqwZ3KUIs1KDmgi ZLEXon3RpMGndgYja5EcHwtLgGJ4hyax5GImepUCWvEwHrb/b0uw1YpXKgnJJYkuROGG mssyk4KRRWXcleb+RRK+akIjk+WKgZVqu5krNAfOBAUGVU8Joa3DL7tJc0/GHJ9MGbjg 05weMWCE/Lpah9OT5I52QVjtTstiCASpzfWPjZxTpeZH6AzMPNlHBX1aUJdtW0BsEIJn VkN9X1j0wBmaokDDF4c5wmuZ21kruWCsB/ObxwQAkk93CivuEgRBy0RTbawiC1TuYuxi OAsA== X-Gm-Message-State: AOAM532XYSim1d18Rkpbpdy7m+q5T56s5hFmwi78Ck8pIOhellcuW3rI TCSRZI7GWCGXHDldlEc+CvXcKQ== X-Google-Smtp-Source: ABdhPJxpNNMjUG49vIgOYmJBbemmd2gmn5oBqIt40qZJP/QmX1iqtjtue1OgM1WmfXbth49PK3v8Hg== X-Received: by 2002:aa7:80cf:0:b0:4bc:ecc1:e7f5 with SMTP id a15-20020aa780cf000000b004bcecc1e7f5mr1002565pfn.11.1642721187501; Thu, 20 Jan 2022 15:26:27 -0800 (PST) Received: from crass-HP-ZBook-15-G2 ([37.218.244.251]) by smtp.gmail.com with ESMTPSA id y23sm4501301pfa.80.2022.01.20.15.26.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 20 Jan 2022 15:26:26 -0800 (PST) Date: Thu, 20 Jan 2022 17:24:01 -0600 From: Glenn Washburn To: Robbie Harwood Cc: grub-devel@gnu.org Subject: Re: [PATCH v3 2/2] mkconfig: use distro sorts when available Message-ID: <20220120172401.07af119e@crass-HP-ZBook-15-G2> In-Reply-To: <20220120193320.16249-3-rharwood@redhat.com> References: <20220120193320.16249-1-rharwood@redhat.com> <20220120193320.16249-3-rharwood@redhat.com> Reply-To: development@efficientek.com X-Mailer: Claws Mail 3.18.0 (GTK+ 2.24.33; x86_64-pc-linux-gnu) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Host-Lookup-Failed: Reverse DNS lookup failed for 2607:f8b0:4864:20::536 (failed) Received-SPF: pass client-ip=2607:f8b0:4864:20::536; envelope-from=development@efficientek.com; helo=mail-pg1-x536.google.com X-Spam_score_int: -10 X-Spam_score: -1.1 X-Spam_bar: - X-Spam_report: (-1.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, PDS_HP_HELO_NORDNS=0.001, RCVD_IN_DNSWL_NONE=-0.0001, RDNS_NONE=0.793, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=no 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 23:26:32 -0000 On Thu, 20 Jan 2022 14:33:20 -0500 Robbie Harwood wrote: > 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 I think without the space before ']' the test won't be evaluated correctly. > + SORT=sort_dpkg At first I was wondering why you left out the elif clause that tests for /etc/redhat-release. But on second thought it does seem to not be needed because if rpmdev-vercmp exists then we want to use it next regardless. Even though its not needed, I think the excluded elif clause makes explicit the intent. If next two elif clauses get swapped, then we will potentially preferring dpkg over rpmdev-vercmp on redhat based distros (not good). Perhaps that's an unlikely scenario. I think we should add a comment here saying something like, "No need to explicitly check for Redhat-based distros because we want to use rpmdev-vercmp next if it exists regardless of distro". My thinking is based on the assumption that the version compare that rpmdev-vercmp does is different that dpkg and better suited to Redhat-based distros. Is this a valid assumption? If they are the same algorithm, then none of this matters. There's also the assumption that if rpmdev-vercmp, its highly likely that this is on a primarily rpm based distro. An even more complete solution would check which package manager installed the kernel and use that to choose which sorting method to use. That's probably overkill for now since this is only for doing the right thing when on non-RPM based distros when both rpmdev-vercmp and dpkg are installed, and I suspect that's an extremely tiny minority. And either way we can make changes when/if someone complains. Glenn > + 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 ()