commit:     138797c63271bb63f9769964a42a9a4307c8bb17
Author:     Michał Górny <mgorny <AT> gentoo <DOT> org>
AuthorDate: Thu Mar 11 13:19:44 2021 +0000
Commit:     Michał Górny <mgorny <AT> gentoo <DOT> org>
CommitDate: Thu Mar 11 14:08:32 2021 +0000
URL:        https://gitweb.gentoo.org/repo/gentoo.git/commit/?id=138797c6

kernel-install.eclass: Refactor /usr/src/linux check to function

Refactor the check whether /usr/src/linux should be updated to
a separate function, to improve readability.

Signed-off-by: Michał Górny <mgorny <AT> gentoo.org>

 eclass/kernel-install.eclass | 69 +++++++++++++++++++++++++++-----------------
 1 file changed, 43 insertions(+), 26 deletions(-)

diff --git a/eclass/kernel-install.eclass b/eclass/kernel-install.eclass
index e3357cda0fd..107a526c328 100644
--- a/eclass/kernel-install.eclass
+++ b/eclass/kernel-install.eclass
@@ -76,6 +76,45 @@ BDEPEND="
                x86? ( app-emulation/qemu[qemu_softmmu_targets_i386] )
        )"
 
+# @FUNCTION: kernel-install_can_update_symlink
+# @USAGE:
+# @DESCRIPTION:
+# Determine whether the symlink at <target> (full path) should be
+# updated.  Returns 0 if it should, 1 to leave as-is.
+kernel-install_can_update_symlink() {
+       debug-print-function ${FUNCNAME} "${@}"
+
+       [[ ${#} -eq 1 ]] || die "${FUNCNAME}: invalid arguments"
+       local target=${1}
+
+       # if the symlink does not exist or is broken, update
+       [[ ! -e ${target} ]] && return 0
+
+       local symlink_target=$(readlink "${target}")
+       # the symlink target should start with the same basename as target
+       # (e.g. "linux-*")
+       [[ ${symlink_target} != ${target##*/}-* ]] && return 1
+
+       # try to establish the kernel version from symlink target
+       local symlink_ver=${symlink_target#${target##*/}-}
+       # strip KV_LOCALVERSION, we want to update the old kernels not using
+       # KV_LOCALVERSION suffix and the new kernels using it
+       symlink_ver=${symlink_ver%${KV_LOCALVERSION}}
+
+       # if ${symlink_ver} contains anything but numbers (e.g. an extra
+       # suffix), it's not our kernel, so leave it alone
+       [[ -n ${symlink_ver//[0-9.]/} ]] && return 1
+
+       local symlink_pkg=${CATEGORY}/${PN}-${symlink_ver}
+       # if the current target is either being replaced, or still
+       # installed (probably depclean candidate), update the symlink
+       has "${symlink_ver}" ${REPLACING_VERSIONS} && return 0
+       has_version -r "~${symlink_pkg}" && return 0
+
+       # otherwise it could be another kernel package, so leave it alone
+       return 1
+}
+
 # @FUNCTION: kernel-install_update_symlink
 # @USAGE: <target> <version>
 # @DESCRIPTION:
@@ -89,35 +128,13 @@ kernel-install_update_symlink() {
        local target=${1}
        local version=${2}
 
-       if [[ ! -e ${target} ]]; then
-               ebegin "Creating ${target} symlink"
+       if kernel-install_can_update_symlink "${target}"; then
+               ebegin "Updating ${target} symlink"
                ln -f -n -s "${target##*/}-${version}" "${target}"
                eend ${?}
        else
-               local symlink_target=$(readlink "${target}")
-               local symlink_ver=${symlink_target#${target##*/}-}
-               local updated=
-               symlink_ver=${symlink_ver%${KV_LOCALVERSION}}
-               if [[ ${symlink_target} == ${target##*/}-* && \
-                               -z ${symlink_ver//[0-9.]/} ]]
-               then
-                       local symlink_pkg=${CATEGORY}/${PN}-${symlink_ver}
-                       # if the current target is either being replaced, or 
still
-                       # installed (probably depclean candidate), update the 
symlink
-                       if has "${symlink_ver}" ${REPLACING_VERSIONS} ||
-                                       has_version -r "~${symlink_pkg}"
-                       then
-                               ebegin "Updating ${target} symlink"
-                               ln -f -n -s "${target##*/}-${version}" 
"${target}"
-                               eend ${?}
-                               updated=1
-                       fi
-               fi
-
-               if [[ ! ${updated} ]]; then
-                       elog "${target} points at another kernel, leaving it 
as-is."
-                       elog "Please use 'eselect kernel' to update it when 
desired."
-               fi
+               elog "${target} points at another kernel, leaving it as-is."
+               elog "Please use 'eselect kernel' to update it when desired."
        fi
 }
 

Reply via email to