From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <gentoo-commits+bounces-1659487-garchives=archives.gentoo.org@lists.gentoo.org>
Received: from lists.gentoo.org (pigeon.gentoo.org [208.92.234.80])
	(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
	 key-exchange X25519 server-signature RSA-PSS (2048 bits))
	(No client certificate requested)
	by finch.gentoo.org (Postfix) with ESMTPS id 4A49C159C83
	for <garchives@archives.gentoo.org>; Wed,  7 Aug 2024 08:59:03 +0000 (UTC)
Received: from pigeon.gentoo.org (localhost [127.0.0.1])
	by pigeon.gentoo.org (Postfix) with SMTP id 6E8722BC017;
	Wed,  7 Aug 2024 08:59:02 +0000 (UTC)
Received: from smtp.gentoo.org (woodpecker.gentoo.org [140.211.166.183])
	(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
	 key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256)
	(No client certificate requested)
	by pigeon.gentoo.org (Postfix) with ESMTPS id 513502BC017
	for <gentoo-commits@lists.gentoo.org>; Wed,  7 Aug 2024 08:59:02 +0000 (UTC)
Received: from oystercatcher.gentoo.org (oystercatcher.gentoo.org [148.251.78.52])
	(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
	 key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256)
	(No client certificate requested)
	by smtp.gentoo.org (Postfix) with ESMTPS id 3B4E234307A
	for <gentoo-commits@lists.gentoo.org>; Wed,  7 Aug 2024 08:59:01 +0000 (UTC)
Received: from localhost.localdomain (localhost [IPv6:::1])
	by oystercatcher.gentoo.org (Postfix) with ESMTP id 6E3161DCA
	for <gentoo-commits@lists.gentoo.org>; Wed,  7 Aug 2024 08:58:59 +0000 (UTC)
From: "Andrew Ammerlaan" <andrewammerlaan@gentoo.org>
To: gentoo-commits@lists.gentoo.org
Content-Transfer-Encoding: 8bit
Content-type: text/plain; charset=UTF-8
Reply-To: gentoo-dev@lists.gentoo.org, "Andrew Ammerlaan" <andrewammerlaan@gentoo.org>
Message-ID: <1723021115.facd2865e403fd295c955fff8d609bfc717aed24.andrewammerlaan@gentoo>
Subject: [gentoo-commits] repo/gentoo:master commit in: eclass/
X-VCS-Repository: repo/gentoo
X-VCS-Files: eclass/dist-kernel-utils.eclass
X-VCS-Directories: eclass/
X-VCS-Committer: andrewammerlaan
X-VCS-Committer-Name: Andrew Ammerlaan
X-VCS-Revision: facd2865e403fd295c955fff8d609bfc717aed24
X-VCS-Branch: master
Date: Wed,  7 Aug 2024 08:58:59 +0000 (UTC)
Precedence: bulk
List-Post: <mailto:gentoo-commits@lists.gentoo.org>
List-Help: <mailto:gentoo-commits+help@lists.gentoo.org>
List-Unsubscribe: <mailto:gentoo-commits+unsubscribe@lists.gentoo.org>
List-Subscribe: <mailto:gentoo-commits+subscribe@lists.gentoo.org>
List-Id: Gentoo Linux mail <gentoo-commits.gentoo.org>
X-BeenThere: gentoo-commits@lists.gentoo.org
X-Auto-Response-Suppress: DR, RN, NRN, OOF, AutoReply
X-Archives-Salt: 22675565-761f-47d9-ac1f-da8b6a2315fd
X-Archives-Hash: 239d48b9add91a3ed4accd2b36c8176d

commit:     facd2865e403fd295c955fff8d609bfc717aed24
Author:     Andrew Ammerlaan <andrewammerlaan <AT> gentoo <DOT> org>
AuthorDate: Mon Aug  5 19:13:06 2024 +0000
Commit:     Andrew Ammerlaan <andrewammerlaan <AT> gentoo <DOT> org>
CommitDate: Wed Aug  7 08:58:35 2024 +0000
URL:        https://gitweb.gentoo.org/repo/gentoo.git/commit/?id=facd2865

dist-kernel-utils.eclass: fix module cleanup when using binpkgs

When installing a binpkg -nt is not a good check because the modules in the
binpkg we are installing may actually be older then what we have in root.

Instead introduce a new function dist-kernel_get_module_suffix to find the
desired compression based on USE=modules-sign and the kernel config. Then,
remove only those files that do not match our desired compression.

Signed-off-by: Andrew Ammerlaan <andrewammerlaan <AT> gentoo.org>

 eclass/dist-kernel-utils.eclass | 59 ++++++++++++++++++++++++++++++++++-------
 1 file changed, 49 insertions(+), 10 deletions(-)

diff --git a/eclass/dist-kernel-utils.eclass b/eclass/dist-kernel-utils.eclass
index 4bc3fab44aae..0b0eb0ec8818 100644
--- a/eclass/dist-kernel-utils.eclass
+++ b/eclass/dist-kernel-utils.eclass
@@ -1,4 +1,4 @@
-# Copyright 2020-2023 Gentoo Authors
+# Copyright 2020-2024 Gentoo Authors
 # Distributed under the terms of the GNU General Public License v2
 
 # @ECLASS: dist-kernel-utils.eclass
@@ -159,6 +159,35 @@ dist-kernel_PV_to_KV() {
 	echo "${kv}"
 }
 
+# @FUNCTION: dist-kernel_get_module_suffix
+# @USAGE: <kernel_dir>
+# @DESCRIPTION:
+# Returns the suffix for kernel modules based on the CONFIG_MODULES_COMPESS_*
+# setting in the kernel config and USE=modules-compress.
+dist-kernel_get_module_suffix() {
+	debug-print-function ${FUNCNAME} "${@}"
+
+	[[ ${#} -eq 1 ]] || die "${FUNCNAME}: invalid arguments"
+
+	local config=${1}/.config
+
+	if ! in_iuse modules-compress || ! use modules-compress; then
+		echo .ko
+	elif [[ ! -r ${config} ]]; then
+		die "Cannot find kernel config ${config}"
+	elif grep -q "CONFIG_MODULE_COMPRESS_NONE=y" "${config}"; then
+		echo .ko
+	elif grep -q "CONFIG_MODULE_COMPRESS_GZIP=y" "${config}"; then
+		echo .ko.gz
+	elif grep -q "CONFIG_MODULE_COMPRESS_XZ=y" "${config}"; then
+		echo .ko.xz
+	elif grep -q "CONFIG_MODULE_COMPRESS_ZSTD=y" "${config}"; then
+		echo .ko.zst
+	else
+		die "Module compression is enabled, but compressor not known"
+	fi
+}
+
 # @FUNCTION: dist-kernel_compressed_module_cleanup
 # @USAGE: <path>
 # @DESCRIPTION:
@@ -169,20 +198,29 @@ dist-kernel_compressed_module_cleanup() {
 
 	[[ ${#} -ne 1 ]] && die "${FUNCNAME}: invalid arguments"
 	local path=${1}
-	local basename f
+	local preferred=$(dist-kernel_get_module_suffix "${path}/source")
+	local basename suffix
 
 	while read -r basename; do
 		local prev=
-		for f in "${path}/${basename}"{,.gz,.xz,.zst}; do
-			if [[ ! -e ${f} ]]; then
-				continue
+		for suffix in .ko .ko.gz .ko.xz .ko.zst; do
+			[[ ${suffix} == ${preferred} ]] && continue
+			local current=${path}/${basename}${suffix}
+			[[ -f ${current} ]] || continue
+
+			if [[ -f ${path}/${basename}${preferred} ]]; then
+				# If the module with the desired compression exists, remove
+				# all other variations.
+				rm -v "${current}" || die
 			elif [[ -z ${prev} ]]; then
-				prev=${f}
-			elif [[ ${f} -nt ${prev} ]]; then
+				# If not, then keep whichever of the duplicate modules is the
+				# newest. Normally you should not end up here.
+				prev=${current}
+			elif [[ ${current} -nt ${prev} ]]; then
 				rm -v "${prev}" || die
-				prev=${f}
+				prev=${current}
 			else
-				rm -v "${f}" || die
+				rm -v "${current}" || die
 			fi
 		done
 	done < <(
@@ -192,7 +230,8 @@ dist-kernel_compressed_module_cleanup() {
 			-o -name '*.ko.gz' \
 			-o -name '*.ko.xz' \
 			-o -name '*.ko.zst' \
-			\) | sed -e 's:[.]\(gz\|xz\|zst\)$::' | sort | uniq -d || die
+			\) | sed -e 's:[.]ko\(\|[.]gz\|[.]xz\|[.]zst\)$::' |
+				sort | uniq -d || die
 	)
 }