From 5972c0137f4d33caca608022c17583f5d7a0567f Mon Sep 17 00:00:00 2001 From: Daniele Rondina Date: Fri, 9 Aug 2019 19:45:05 +0200 Subject: [PATCH 1/8] sabayon-brokenlinks: WIP --- sabayon-brokenlinks | 332 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 332 insertions(+) create mode 100755 sabayon-brokenlinks diff --git a/sabayon-brokenlinks b/sabayon-brokenlinks new file mode 100755 index 0000000..421ac5b --- /dev/null +++ b/sabayon-brokenlinks @@ -0,0 +1,332 @@ +#!/bin/bash +# Authors: Sabayon Team 2019 + +set -e + +# Accepted values: x86_64, armv7l +export SAB_ARCH="${SAB_ARCH:-}" +export DRY_RUN=${DRY_RUN:-0} +export DEBUG="${DEBUG:-}" + +declare -a MANUALLY_REMOVED_LINKS +declare -a BROKEN_LINKS2REMOVE + +log() { + local with_dashes=$1 + shift || true + + [ "$with_dashes" != 1 ] || \ + echo "===============================================================" + echo "$@" + [ "$with_dashes" != 1 ] || \ + echo "===============================================================" +} + +summary() { + + local msg="SAB_ARCH = ${SAB_ARCH} +EXAMINED_LIBDIRS = ${EXAMINED_LIBDIRS} +DRY_RUN = ${DRY_RUN}" + + log 1 "$msg" +} + +check_arch() { + if [ -z "${SAB_ARCH}" ] ; then + SAB_ARCH=$(uname -m) + fi + + [[ "${SAB_ARCH}" != "x86_64" && "${SAB_ARCH}" != "armv7l" ]] && { + log 1 "Unsupported arch ${SAB_ARCH}." + exit 1 + } + + export SAB_ARCH +} + +search_pkg() { + local basename=$1 + # passed by reference + local -n p="$2" + local -n libdir="$3" + local lib="$4" + + # Try to search for package + p=$(equery -N -q -C belongs $lib/$basename --early-out || true) + + if [ -z "$p" ] ; then + if [ "$lib" = "/usr/lib64" ] ; then + # Try to search for file under /lib64 + p=$(equery -N -q -C belongs /lib64/$basename --early-out || true) + libdir="/lib64" + fi + if [ "$lib" = "/usr/lib32" ] ; then + # Try to search for file under /lib32 + p=$(equery -N -q -C belongs /lib32/$basename --early-out || true) + libdir="/lib32" + fi + if [ "$lib" = "/usr/lib" ] ; then + # Try to search for file under /lib + p=$(equery -N -q -C belongs /lib/$basename --early-out || true) + libdir="/lib" + fi + + fi + + if [ -z "$p" ] ; then + libdir="" + fi +} + +detect_libs_dir() { + local libs="" + if [ "${SAB_ARCH}" = "x86_64" ] ; then + if [ -d "/usr/lib32" ] ; then + libs="/usr/lib32" + fi + if [ -d "/usr/lib64" ] ; then + libs="$libs /usr/lib64" + fi + else + if [ -d "/usr/lib" ] ; then + libs="/usr/lib" + fi + fi + if [ -z "$libs" ] ; then + log 0 "Error on detect libraries directories." + exit 1 + fi + + EXAMINED_LIBDIRS=$libs + export EXAMINED_LIBDIRS +} + +examine_libs() { + local lib="" + local file="" + local broken_files="" + + for lib in ${EXAMINED_LIBDIRS} ; do + broken_files="" + + log 1 "Checking directory $lib..." + + broken_files=$(find $lib -type l -xtype l -name *.so* 2>/dev/null) + + [ -z "$DEBUG" ] || find $lib -type l -xtype l -name *.so* + + for file in ${broken_files} ; do + examine_file "${file}" "${lib}" + done + done +} + +examine_file() { + local f=$1 + local res="" + local libdir=$2 + local is_present_pkg=1 + local pkg="" + + [ -z "$DEBUG" ] || log 1 "Analyze file $f" + + local base=$(basename $1) + + local is_present_equery=1 + local is_present_qlist=1 + which equery 2>&1 >/dev/null || { + is_present_equery=0 + } + which qlist 2>&1 >/dev/null || { + is_present_qlist=0 + } + local targetlink=$(readlink $f) + local link2remove=false + local link2fix=false + local newlibdir="" + local tmp="" + local current_link="" + + log 1 "Broken link $f => $targetlink" + + # Check if file is without revision (es. libudev.so) + res=$(perl -e "if ('$base' =~ /(.so$)/) { print '1' }") + if [ -n "$res" ] ; then + # + #echo "BASE = $base" + + if [ $is_present_equery = 1 ] ; then + + search_pkg "$base" pkg newlibdir "$lib" + if [ -z "$pkg" ] ; then + MANUALLY_REMOVED_LINKS+=( "$f" ) + return + else + + if [ -n "$newlibdir" ] ; then + log 0 "Link $base available under $newlibdir. I remove it." + link2remove=true + fi + fi + + if [ "$is_present_qlist" = 1 ] ; then + # Check new + res=$(qlist -IC $pkg 2>/dev/null | grep $f | wc -l) + if [ "$res" = 1 ] ; then + link2fix=true + fi + fi + + if [ $link2remove = true ] ; then + BROKEN_LINKS2REMOVE+=( "$f" ) + fi + + log 0 "Link $f of the package $pkg" + fi + + else + + # Check if file has only one revision number (es. libudev.so.0) + res=$(perl -e "if ('$base' =~ /(.so[.][\d]+$)/) { print '1' }") + if [ -n "$res" ] ; then + + if [ $is_present_equery = 1 ] ; then + search_pkg "$base" pkg newlibdir "$lib" + if [ -z "$pkg" ] ; then + # Try to search package with .so + base=$(echo "$base" | awk 'match($0, /.so/) { print substr($0, 0, RSTART+2) }') + search_pkg "$base" pkg newlibdir "$lib" + if [ -z "$pkg" ] ; then + log 0 "No package found for link $f." + MANUALLY_REMOVED_LINKS+=( "$f" ) + return + fi + + if [ -n "$newlibdir" ] ; then + log 0 "Link $base available under $newlibdir. I remove it." + link2remove=true + else + # POST: we have a broken link or old link + + current_link=$(readlink $lib/$base) + log 0 "Link $f related to package $pkg (with an updated version $current_link). I remove it." + link2remove=true + + fi + + else + + log 0 "Link $f of the package $pkg" + + if [ -n "$newlibdir" ] ; then + log 0 "Link $base available under $newlibdir. I remove it." + link2remove=true + else + + if [ "$is_present_qlist" = 1 ] ; then + # Check new + res=$(qlist -IC $pkg 2>/dev/null | grep $f | wc -l) + if [ "$res" = 1 ] ; then + link2fix=true + fi + fi + + fi + + fi + fi + + if [ $link2remove = true ] ; then + BROKEN_LINKS2REMOVE+=( "$f" ) + fi + + else + # Check if file has only one revision number (es. libudev.so.0.63) + res=$(perl -e "if ('$base' =~ /(.so[.][\d]+[.][\d]+$)/) { print '1' }") + if [ -n "$res" ] ; then + + if [ $is_present_equery = 1 ] ; then + search_pkg "$base" pkg newlibdir "$lib" + if [ -z "$pkg" ] ; then + + base=$(echo "$base" | awk 'match($0, /.so[.][0-9]+/) { print substr($0, 0, RSTART+RLENGTH-1) }') + search_pkg "$base" pkg newlibdir "$lib" + if [ -z "$pkg" ] ; then + log 0 "No package found for link $f." + MANUALLY_REMOVED_LINKS+=( "$f" ) + return + fi + + if [ -n "$newlibdir" ] ; then + log 0 "Link $base available under $newlibdir. I remove it." + link2remove=true + else + + current_link=$(readlink $lib/$base) + log 0 "Link $f related to package $pkg (with an updated version $current_link). I remove it." + link2remove=true + + fi + + else + + log 0 "Link $f of the package $pkg" + + if [ -n "$newlibdir" ] ; then + log 0 "Link $base available under $newlibdir. I remove it." + link2remove=true + else + + if [ "$is_present_qlist" = 1 ] ; then + # Check new + res=$(qlist -IC $pkg 2>/dev/null | grep $f | wc -l) + if [ "$res" = 1 ] ; then + link2fix=true + fi + fi + + fi + fi + + fi # $is_present_equery + + else + echo "Link $f not supported". + MANUALLY_REMOVED_LINKS+=( "$f" ) + + fi + + fi + + fi +} + +print_links2remove() { + + echo $MANUALLY_REMOVED_LINKS + +} + +main() { + + log 1 "sabayon-brokenlinks: Examine system broken links" + check_arch + detect_libs_dir + summary + + if [[ "$(id -u)" != "0" && "${DRY_RUN}" != "1" ]]; then + echo "This script must be run as root" 1>&2 + exit 1 + fi + + examine_libs + + echo "=======+++++++++++++++++=======" + echo $BROKEN_LINKS2REMOVE + echo "=======+++++++++++++++++=======" + + print_links2remove + +} + +main $@ From 11977d3afd3a15c16fd577158eba615a6be7ed63 Mon Sep 17 00:00:00 2001 From: Daniele Rondina Date: Mon, 12 Aug 2019 17:09:30 +0200 Subject: [PATCH 2/8] sabayon-brokenlinks: Integrate automatic remove of broken links --- sabayon-brokenlinks | 225 +++++++++++++++++++++++--------------------- 1 file changed, 117 insertions(+), 108 deletions(-) diff --git a/sabayon-brokenlinks b/sabayon-brokenlinks index 421ac5b..b7c0e5a 100755 --- a/sabayon-brokenlinks +++ b/sabayon-brokenlinks @@ -51,6 +51,8 @@ search_pkg() { local -n libdir="$3" local lib="$4" + # TODO: check if there is a more fast way instead of equery. + # Try to search for package p=$(equery -N -q -C belongs $lib/$basename --early-out || true) @@ -82,14 +84,14 @@ detect_libs_dir() { local libs="" if [ "${SAB_ARCH}" = "x86_64" ] ; then if [ -d "/usr/lib32" ] ; then - libs="/usr/lib32" + libs="/usr/lib32 /lib32" fi if [ -d "/usr/lib64" ] ; then - libs="$libs /usr/lib64" + libs="$libs /usr/lib64 /lib64" fi else if [ -d "/usr/lib" ] ; then - libs="/usr/lib" + libs="/usr/lib /lib" fi fi if [ -z "$libs" ] ; then @@ -106,6 +108,8 @@ examine_libs() { local file="" local broken_files="" + log 1 "The exam could be long sorry for the wait." + for lib in ${EXAMINED_LIBDIRS} ; do broken_files="" @@ -132,14 +136,6 @@ examine_file() { local base=$(basename $1) - local is_present_equery=1 - local is_present_qlist=1 - which equery 2>&1 >/dev/null || { - is_present_equery=0 - } - which qlist 2>&1 >/dev/null || { - is_present_qlist=0 - } local targetlink=$(readlink $f) local link2remove=false local link2fix=false @@ -155,46 +151,93 @@ examine_file() { # #echo "BASE = $base" - if [ $is_present_equery = 1 ] ; then + search_pkg "$base" pkg newlibdir "$lib" + if [ -z "$pkg" ] ; then + MANUALLY_REMOVED_LINKS+=( "$f" ) + return + else - search_pkg "$base" pkg newlibdir "$lib" - if [ -z "$pkg" ] ; then - MANUALLY_REMOVED_LINKS+=( "$f" ) - return - else - - if [ -n "$newlibdir" ] ; then - log 0 "Link $base available under $newlibdir. I remove it." - link2remove=true - fi + if [ -n "$newlibdir" ] ; then + log 0 "Link $base available under $newlibdir. I remove it." + link2remove=true fi - - if [ "$is_present_qlist" = 1 ] ; then - # Check new - res=$(qlist -IC $pkg 2>/dev/null | grep $f | wc -l) - if [ "$res" = 1 ] ; then - link2fix=true - fi - fi - - if [ $link2remove = true ] ; then - BROKEN_LINKS2REMOVE+=( "$f" ) - fi - - log 0 "Link $f of the package $pkg" fi + if [ "$is_present_qlist" = 1 ] ; then + # Check new + res=$(qlist -IC $pkg 2>/dev/null | grep $f | wc -l) + if [ "$res" = 1 ] ; then + link2fix=true + fi + fi + + if [ $link2remove = true ] ; then + BROKEN_LINKS2REMOVE+=( "$f" ) + fi + + log 0 "Link $f of the package $pkg" + else # Check if file has only one revision number (es. libudev.so.0) res=$(perl -e "if ('$base' =~ /(.so[.][\d]+$)/) { print '1' }") if [ -n "$res" ] ; then - if [ $is_present_equery = 1 ] ; then + search_pkg "$base" pkg newlibdir "$lib" + if [ -z "$pkg" ] ; then + # Try to search package with .so + base=$(echo "$base" | awk 'match($0, /.so/) { print substr($0, 0, RSTART+2) }') search_pkg "$base" pkg newlibdir "$lib" if [ -z "$pkg" ] ; then - # Try to search package with .so - base=$(echo "$base" | awk 'match($0, /.so/) { print substr($0, 0, RSTART+2) }') + log 0 "No package found for link $f." + MANUALLY_REMOVED_LINKS+=( "$f" ) + return + fi + + if [ -n "$newlibdir" ] ; then + log 0 "Link $base available under $newlibdir. I remove it." + link2remove=true + else + # POST: we have a broken link or old link + + current_link=$(readlink $lib/$base) + log 0 "Link $f related to package $pkg (with an updated version $current_link). I remove it." + link2remove=true + + fi + + else + + log 0 "Link $f of the package $pkg" + + if [ -n "$newlibdir" ] ; then + log 0 "Link $base available under $newlibdir. I remove it." + link2remove=true + else + + # Check new + res=$(qlist -IC $pkg 2>/dev/null | grep $f | wc -l) + if [ "$res" = 1 ] ; then + link2fix=true + fi + + fi + + fi + + if [ $link2remove = true ] ; then + BROKEN_LINKS2REMOVE+=( "$f" ) + fi + + else + # Check if file has only one revision number (es. libudev.so.0.63) + res=$(perl -e "if ('$base' =~ /(.so[.][\d]+[.][\d]+$)/) { print '1' }") + if [ -n "$res" ] ; then + + search_pkg "$base" pkg newlibdir "$lib" + if [ -z "$pkg" ] ; then + + base=$(echo "$base" | awk 'match($0, /.so[.][0-9]+/) { print substr($0, 0, RSTART+RLENGTH-1) }') search_pkg "$base" pkg newlibdir "$lib" if [ -z "$pkg" ] ; then log 0 "No package found for link $f." @@ -206,7 +249,6 @@ examine_file() { log 0 "Link $base available under $newlibdir. I remove it." link2remove=true else - # POST: we have a broken link or old link current_link=$(readlink $lib/$base) log 0 "Link $f related to package $pkg (with an updated version $current_link). I remove it." @@ -223,72 +265,15 @@ examine_file() { link2remove=true else - if [ "$is_present_qlist" = 1 ] ; then - # Check new - res=$(qlist -IC $pkg 2>/dev/null | grep $f | wc -l) - if [ "$res" = 1 ] ; then - link2fix=true - fi + # Check new + res=$(qlist -IC $pkg 2>/dev/null | grep $f | wc -l) + if [ "$res" = 1 ] ; then + link2fix=true fi fi fi - fi - - if [ $link2remove = true ] ; then - BROKEN_LINKS2REMOVE+=( "$f" ) - fi - - else - # Check if file has only one revision number (es. libudev.so.0.63) - res=$(perl -e "if ('$base' =~ /(.so[.][\d]+[.][\d]+$)/) { print '1' }") - if [ -n "$res" ] ; then - - if [ $is_present_equery = 1 ] ; then - search_pkg "$base" pkg newlibdir "$lib" - if [ -z "$pkg" ] ; then - - base=$(echo "$base" | awk 'match($0, /.so[.][0-9]+/) { print substr($0, 0, RSTART+RLENGTH-1) }') - search_pkg "$base" pkg newlibdir "$lib" - if [ -z "$pkg" ] ; then - log 0 "No package found for link $f." - MANUALLY_REMOVED_LINKS+=( "$f" ) - return - fi - - if [ -n "$newlibdir" ] ; then - log 0 "Link $base available under $newlibdir. I remove it." - link2remove=true - else - - current_link=$(readlink $lib/$base) - log 0 "Link $f related to package $pkg (with an updated version $current_link). I remove it." - link2remove=true - - fi - - else - - log 0 "Link $f of the package $pkg" - - if [ -n "$newlibdir" ] ; then - log 0 "Link $base available under $newlibdir. I remove it." - link2remove=true - else - - if [ "$is_present_qlist" = 1 ] ; then - # Check new - res=$(qlist -IC $pkg 2>/dev/null | grep $f | wc -l) - if [ "$res" = 1 ] ; then - link2fix=true - fi - fi - - fi - fi - - fi # $is_present_equery else echo "Link $f not supported". @@ -301,10 +286,20 @@ examine_file() { fi } +rm_broken_links() { + for l in ${BROKEN_LINKS2REMOVE[@]} ; do + if [ "$DRY_RUN" = 0 ]; then + rm -vf ${l} + else + echo "rm -vf ${l}" + fi + done +} + print_links2remove() { - - echo $MANUALLY_REMOVED_LINKS - + for l in ${MANUALLY_REMOVED_LINKS[@]} ; do + echo "rm -vf ${l}" + done } main() { @@ -319,13 +314,27 @@ main() { exit 1 fi + which equery 2>&1 >/dev/null || { + echo "equery tool is needed. Please install app-portage/gentoolkit." + exit 1 + } + + which qlist 2>&1 >/dev/null || { + echo "qlist tool is needed. Please install app-portage/portage-utils." + exit 1 + } + examine_libs - echo "=======+++++++++++++++++=======" - echo $BROKEN_LINKS2REMOVE - echo "=======+++++++++++++++++=======" + log 1 "Remove Broken Links!" + rm_broken_links - print_links2remove + if [ ${#MANUALLY_REMOVED_LINKS[@]} -gt 0 ] ; then + log 1 "Hereinafter, links to manually remove:" + print_links2remove + fi + + echo "All Done." } From 4839c818de5c64ca92de378a8dc612592ff819c1 Mon Sep 17 00:00:00 2001 From: Daniele Rondina Date: Tue, 13 Aug 2019 15:33:11 +0200 Subject: [PATCH 3/8] sabayon-brokenlinks: Review with cli arguments support --- sabayon-brokenlinks | 152 +++++++++++++++++++++++++++++++++++--------- 1 file changed, 121 insertions(+), 31 deletions(-) diff --git a/sabayon-brokenlinks b/sabayon-brokenlinks index b7c0e5a..d96f8b1 100755 --- a/sabayon-brokenlinks +++ b/sabayon-brokenlinks @@ -7,6 +7,7 @@ set -e export SAB_ARCH="${SAB_ARCH:-}" export DRY_RUN=${DRY_RUN:-0} export DEBUG="${DEBUG:-}" +export FORCE_MANUAL="${FORCE_MANUAL:-0}" declare -a MANUALLY_REMOVED_LINKS declare -a BROKEN_LINKS2REMOVE @@ -26,7 +27,8 @@ summary() { local msg="SAB_ARCH = ${SAB_ARCH} EXAMINED_LIBDIRS = ${EXAMINED_LIBDIRS} -DRY_RUN = ${DRY_RUN}" +DRY_RUN = ${DRY_RUN} +FORCE_MANUAL= ${FORCE_MANUAL}" log 1 "$msg" } @@ -82,21 +84,26 @@ search_pkg() { detect_libs_dir() { local libs="" - if [ "${SAB_ARCH}" = "x86_64" ] ; then - if [ -d "/usr/lib32" ] ; then - libs="/usr/lib32 /lib32" - fi - if [ -d "/usr/lib64" ] ; then - libs="$libs /usr/lib64 /lib64" - fi + + if [ -n "$SCANDIR" ] ; then + libs=$SCANDIR else - if [ -d "/usr/lib" ] ; then - libs="/usr/lib /lib" + if [ "${SAB_ARCH}" = "x86_64" ] ; then + if [ -d "/usr/lib32" ] ; then + libs="/usr/lib32 /lib32" + fi + if [ -d "/usr/lib64" ] ; then + libs="$libs /usr/lib64 /lib64" + fi + else + if [ -d "/usr/lib" ] ; then + libs="/usr/lib /lib" + fi + fi + if [ -z "$libs" ] ; then + log 0 "Error on detect libraries directories." + exit 1 fi - fi - if [ -z "$libs" ] ; then - log 0 "Error on detect libraries directories." - exit 1 fi EXAMINED_LIBDIRS=$libs @@ -163,16 +170,12 @@ examine_file() { fi fi - if [ "$is_present_qlist" = 1 ] ; then - # Check new - res=$(qlist -IC $pkg 2>/dev/null | grep $f | wc -l) - if [ "$res" = 1 ] ; then - link2fix=true - fi - fi - - if [ $link2remove = true ] ; then - BROKEN_LINKS2REMOVE+=( "$f" ) + res=$(qlist -IC $pkg 2>/dev/null | grep $f | wc -l) + if [ "$res" = 1 ] ; then + link2fix=true + echo "Link $f related to existing package. Try to reinstall $pkg." + else + MANUALLY_REMOVED_LINKS+=( "$f" ) fi log 0 "Link $f of the package $pkg" @@ -219,16 +222,15 @@ examine_file() { res=$(qlist -IC $pkg 2>/dev/null | grep $f | wc -l) if [ "$res" = 1 ] ; then link2fix=true + echo "Link $f related to existing package. Try to reinstall $pkg." + else + MANUALLY_REMOVED_LINKS+=( "$f" ) fi fi fi - if [ $link2remove = true ] ; then - BROKEN_LINKS2REMOVE+=( "$f" ) - fi - else # Check if file has only one revision number (es. libudev.so.0.63) res=$(perl -e "if ('$base' =~ /(.so[.][\d]+[.][\d]+$)/) { print '1' }") @@ -269,6 +271,9 @@ examine_file() { res=$(qlist -IC $pkg 2>/dev/null | grep $f | wc -l) if [ "$res" = 1 ] ; then link2fix=true + echo "Link $f related to existing package. Try to reinstall $pkg." + else + MANUALLY_REMOVED_LINKS+=( "$f" ) fi fi @@ -284,6 +289,11 @@ examine_file() { fi fi + + if [ $link2remove = true ] ; then + BROKEN_LINKS2REMOVE+=( "$f" ) + fi + } rm_broken_links() { @@ -298,12 +308,88 @@ rm_broken_links() { print_links2remove() { for l in ${MANUALLY_REMOVED_LINKS[@]} ; do + if [ "$FORCE_MANUAL" = 1 ] ; then + if [ "$DRY_RUN" = 0 ] ; then + rm -vf ${l} + else + echo "force: rm -vf ${l}" + fi + else echo "rm -vf ${l}" + fi done } +parse_args() { + + help_message() { + + echo "Sabayon Broken Links analyzer. +Sabayon Team (2019) + +$0 [opts] + +Available options: + +-h|--help This message. +--dry-run Dry run execution. +--force-manual Force remove of links to remove manually. +--scandir Define a specific directory to analyze +-d|--debug Enable debug stuff. + +Environment variables: +DRY_RUN To use instead of --dry-run option. Values: 0 or 1. +DEBUG Enable debug output. + +" + + } + + local short_opts="hd" + local long_opts="help dry-run force-manual debug scandir:" + + $(set -- $(getopt -u -q -a -o "$short_opts" -l "$long_opts" -- "$@")) + + while [ $# -gt 0 ] ; do + case "$1" in + + -h|--help) + help_message + exit 1 + ;; + -d|--debug) + DEBUG=1 + ;; + --force-manual) + FORCE_MANUAL=1 + ;; + --dry-run) + DRY_RUN=1 + ;; + --scandir) + SCANDIR="$2" + shift + ;; + --) + ;; + *) + echo "Invalid parameter $1." + exit 1 + ;; + + esac + + shift + + done + + unset -f help_message +} + main() { + parse_args "$@" + log 1 "sabayon-brokenlinks: Examine system broken links" check_arch detect_libs_dir @@ -324,10 +410,15 @@ main() { exit 1 } + parse_args + unset -f parse_args + examine_libs - log 1 "Remove Broken Links!" - rm_broken_links + if [ ${#BROKEN_LINKS2REMOVE[@]} -gt 0 ] ; then + log 1 "Remove Broken Links!" + rm_broken_links + fi if [ ${#MANUALLY_REMOVED_LINKS[@]} -gt 0 ] ; then log 1 "Hereinafter, links to manually remove:" @@ -335,7 +426,6 @@ main() { fi echo "All Done." - } main $@ From 631072d2a5c469463adcc1744a64f3a7a1b906c2 Mon Sep 17 00:00:00 2001 From: Daniele Rondina Date: Wed, 21 Aug 2019 18:41:32 +0200 Subject: [PATCH 4/8] sabayon-brokenlinks: Use equo by default as tool --- sabayon-brokenlinks | 83 ++++++++++++++++++++++++++++----------------- 1 file changed, 52 insertions(+), 31 deletions(-) diff --git a/sabayon-brokenlinks b/sabayon-brokenlinks index d96f8b1..094820e 100755 --- a/sabayon-brokenlinks +++ b/sabayon-brokenlinks @@ -8,6 +8,7 @@ export SAB_ARCH="${SAB_ARCH:-}" export DRY_RUN=${DRY_RUN:-0} export DEBUG="${DEBUG:-}" export FORCE_MANUAL="${FORCE_MANUAL:-0}" +export GENTOO_TOOL="${GENTOO_TOOL:-0}" declare -a MANUALLY_REMOVED_LINKS declare -a BROKEN_LINKS2REMOVE @@ -28,6 +29,7 @@ summary() { local msg="SAB_ARCH = ${SAB_ARCH} EXAMINED_LIBDIRS = ${EXAMINED_LIBDIRS} DRY_RUN = ${DRY_RUN} +GENTOO_TOOL = ${GENTOO_TOOL} FORCE_MANUAL= ${FORCE_MANUAL}" log 1 "$msg" @@ -52,27 +54,31 @@ search_pkg() { local -n p="$2" local -n libdir="$3" local lib="$4" - - # TODO: check if there is a more fast way instead of equery. + local l="" + local ll="" # Try to search for package - p=$(equery -N -q -C belongs $lib/$basename --early-out || true) + if [ "${GENTOO_TOOL}" = "1" ] ; then + p=$(equery -N -q -C belongs ${lib}/$basename --early-out || true) + else + p=$(equo q belongs ${lib}/${basename} -q || true) + fi if [ -z "$p" ] ; then - if [ "$lib" = "/usr/lib64" ] ; then - # Try to search for file under /lib64 - p=$(equery -N -q -C belongs /lib64/$basename --early-out || true) - libdir="/lib64" - fi - if [ "$lib" = "/usr/lib32" ] ; then - # Try to search for file under /lib32 - p=$(equery -N -q -C belongs /lib32/$basename --early-out || true) - libdir="/lib32" - fi - if [ "$lib" = "/usr/lib" ] ; then - # Try to search for file under /lib - p=$(equery -N -q -C belongs /lib/$basename --early-out || true) - libdir="/lib" + + for l in "/usr/lib64" "/usr/lib32" "/usr/lib" ; do + if [ "$lib" = "$l" ] ; then + ll="${l/\/usr\///}" + break + fi + done + + if [ -n "${ll}" ] ; then + if [ "${GENTOO_TOOL}" = "1" ] ; then + p=$(equery -N -q -C belongs ${ll}/$basename --early-out || true) + else + p=$(equo q belongs ${ll}/${basename} -q || true) + fi fi fi @@ -170,7 +176,11 @@ examine_file() { fi fi - res=$(qlist -IC $pkg 2>/dev/null | grep $f | wc -l) + if [ "${GENTOO_TOOL}" = "1" ] ; then + res=$(qlist -IC $pkg 2>/dev/null | grep $f | wc -l) + else + res=$(equo q files $pkg -q | grep $f | wc -l) + fi if [ "$res" = 1 ] ; then link2fix=true echo "Link $f related to existing package. Try to reinstall $pkg." @@ -219,7 +229,11 @@ examine_file() { else # Check new - res=$(qlist -IC $pkg 2>/dev/null | grep $f | wc -l) + if [ "${GENTOO_TOOL}" = "1" ] ; then + res=$(qlist -IC $pkg 2>/dev/null | grep $f | wc -l) + else + res=$(equo q files $pkg -q | grep $f | wc -l) + fi if [ "$res" = 1 ] ; then link2fix=true echo "Link $f related to existing package. Try to reinstall $pkg." @@ -268,7 +282,11 @@ examine_file() { else # Check new - res=$(qlist -IC $pkg 2>/dev/null | grep $f | wc -l) + if [ "${GENTOO_TOOL}" = "1" ] ; then + res=$(qlist -IC $pkg 2>/dev/null | grep $f | wc -l) + else + res=$(equo q files $pkg -q | grep $f | wc -l) + fi if [ "$res" = 1 ] ; then link2fix=true echo "Link $f related to existing package. Try to reinstall $pkg." @@ -335,6 +353,7 @@ Available options: --dry-run Dry run execution. --force-manual Force remove of links to remove manually. --scandir Define a specific directory to analyze +--gentoo Use gentoo tool instead of equo. -d|--debug Enable debug stuff. Environment variables: @@ -346,7 +365,7 @@ DEBUG Enable debug output. } local short_opts="hd" - local long_opts="help dry-run force-manual debug scandir:" + local long_opts="help dry-run force-manual debug scandir: gentoo" $(set -- $(getopt -u -q -a -o "$short_opts" -l "$long_opts" -- "$@")) @@ -400,19 +419,21 @@ main() { exit 1 fi - which equery 2>&1 >/dev/null || { - echo "equery tool is needed. Please install app-portage/gentoolkit." - exit 1 - } - - which qlist 2>&1 >/dev/null || { - echo "qlist tool is needed. Please install app-portage/portage-utils." - exit 1 - } - parse_args unset -f parse_args + if [ "${GENTOO_TOOL}" = 1 ] ; then + which equery 2>&1 >/dev/null || { + echo "equery tool is needed. Please install app-portage/gentoolkit." + exit 1 + } + + which qlist 2>&1 >/dev/null || { + echo "qlist tool is needed. Please install app-portage/portage-utils." + exit 1 + } + fi + examine_libs if [ ${#BROKEN_LINKS2REMOVE[@]} -gt 0 ] ; then From 280a66570752c6ba3fcf9242d9a9bc6912cfcd23 Mon Sep 17 00:00:00 2001 From: Daniele Rondina Date: Fri, 23 Aug 2019 14:59:19 +0200 Subject: [PATCH 5/8] sabayon-brokenlinks: Add support for libs *.a --- sabayon-brokenlinks | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/sabayon-brokenlinks b/sabayon-brokenlinks index 094820e..f09f1a6 100755 --- a/sabayon-brokenlinks +++ b/sabayon-brokenlinks @@ -127,10 +127,11 @@ examine_libs() { broken_files="" log 1 "Checking directory $lib..." + broken_files=$(find $lib -type l -and -xtype l -and -name *.so* -or -name *.a -and -type l -and -xtype l 2>/dev/null || true ) - broken_files=$(find $lib -type l -xtype l -name *.so* 2>/dev/null) - - [ -z "$DEBUG" ] || find $lib -type l -xtype l -name *.so* + [ -z "$DEBUG" ] || { + find $lib -type l -and -xtype l -and -name *.so* -or -name *.a -and -type l -and -xtype l || true + } for file in ${broken_files} ; do examine_file "${file}" "${lib}" @@ -159,7 +160,7 @@ examine_file() { log 1 "Broken link $f => $targetlink" # Check if file is without revision (es. libudev.so) - res=$(perl -e "if ('$base' =~ /(.so$)/) { print '1' }") + res=$(perl -e "if ('$base' =~ /(.so|.a$)/) { print '1' }") if [ -n "$res" ] ; then # #echo "BASE = $base" From d72e8c220b0ddee9298f1c16f9b1dcc8d03f056e Mon Sep 17 00:00:00 2001 From: Daniele Rondina Date: Fri, 23 Aug 2019 15:02:25 +0200 Subject: [PATCH 6/8] sabayon-brokenlinks: Minor fix on message --- sabayon-brokenlinks | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/sabayon-brokenlinks b/sabayon-brokenlinks index f09f1a6..505c514 100755 --- a/sabayon-brokenlinks +++ b/sabayon-brokenlinks @@ -443,7 +443,11 @@ main() { fi if [ ${#MANUALLY_REMOVED_LINKS[@]} -gt 0 ] ; then - log 1 "Hereinafter, links to manually remove:" + if [ "${FORCE_MANUAL}" = 1 ] ; then + log 1 "Hereinafter, links removed with force:" + else + log 1 "Hereinafter, links to manually remove:" + fi print_links2remove fi From e7aade22a03da5c41cfa355784a50c717d57b565 Mon Sep 17 00:00:00 2001 From: Daniele Rondina Date: Fri, 23 Aug 2019 23:12:32 +0200 Subject: [PATCH 7/8] sabayon-brokenlinks: Use USE_EQUO instead of GENTOO_TOOL env --- sabayon-brokenlinks | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/sabayon-brokenlinks b/sabayon-brokenlinks index 505c514..529bfc8 100755 --- a/sabayon-brokenlinks +++ b/sabayon-brokenlinks @@ -8,7 +8,7 @@ export SAB_ARCH="${SAB_ARCH:-}" export DRY_RUN=${DRY_RUN:-0} export DEBUG="${DEBUG:-}" export FORCE_MANUAL="${FORCE_MANUAL:-0}" -export GENTOO_TOOL="${GENTOO_TOOL:-0}" +export USE_EQUO="${USE_EQUO:-1}" declare -a MANUALLY_REMOVED_LINKS declare -a BROKEN_LINKS2REMOVE @@ -29,7 +29,7 @@ summary() { local msg="SAB_ARCH = ${SAB_ARCH} EXAMINED_LIBDIRS = ${EXAMINED_LIBDIRS} DRY_RUN = ${DRY_RUN} -GENTOO_TOOL = ${GENTOO_TOOL} +USE_EQUO = ${USE_EQUO} FORCE_MANUAL= ${FORCE_MANUAL}" log 1 "$msg" @@ -58,7 +58,7 @@ search_pkg() { local ll="" # Try to search for package - if [ "${GENTOO_TOOL}" = "1" ] ; then + if [ "${USE_EQUO}" = "0" ] ; then p=$(equery -N -q -C belongs ${lib}/$basename --early-out || true) else p=$(equo q belongs ${lib}/${basename} -q || true) @@ -74,7 +74,7 @@ search_pkg() { done if [ -n "${ll}" ] ; then - if [ "${GENTOO_TOOL}" = "1" ] ; then + if [ "${USE_EQUO}" = "0" ] ; then p=$(equery -N -q -C belongs ${ll}/$basename --early-out || true) else p=$(equo q belongs ${ll}/${basename} -q || true) @@ -177,7 +177,7 @@ examine_file() { fi fi - if [ "${GENTOO_TOOL}" = "1" ] ; then + if [ "${USE_EQUO}" = "0" ] ; then res=$(qlist -IC $pkg 2>/dev/null | grep $f | wc -l) else res=$(equo q files $pkg -q | grep $f | wc -l) @@ -230,7 +230,7 @@ examine_file() { else # Check new - if [ "${GENTOO_TOOL}" = "1" ] ; then + if [ "${USE_EQUO}" = "0" ] ; then res=$(qlist -IC $pkg 2>/dev/null | grep $f | wc -l) else res=$(equo q files $pkg -q | grep $f | wc -l) @@ -283,7 +283,7 @@ examine_file() { else # Check new - if [ "${GENTOO_TOOL}" = "1" ] ; then + if [ "${USE_EQUO}" = "0" ] ; then res=$(qlist -IC $pkg 2>/dev/null | grep $f | wc -l) else res=$(equo q files $pkg -q | grep $f | wc -l) @@ -360,6 +360,8 @@ Available options: Environment variables: DRY_RUN To use instead of --dry-run option. Values: 0 or 1. DEBUG Enable debug output. +USE_EQUO To use instead of --gentoo option. +FORCE_MANUAL To use instead of --force-manual. " @@ -390,6 +392,9 @@ DEBUG Enable debug output. SCANDIR="$2" shift ;; + --gentoo) + USE_EQUO=0 + ;; --) ;; *) @@ -423,7 +428,7 @@ main() { parse_args unset -f parse_args - if [ "${GENTOO_TOOL}" = 1 ] ; then + if [ "${USE_EQUO}" = "0" ] ; then which equery 2>&1 >/dev/null || { echo "equery tool is needed. Please install app-portage/gentoolkit." exit 1 From 460101a9857db0a8c0197804032596a0eaf8e74b Mon Sep 17 00:00:00 2001 From: Daniele Rondina Date: Wed, 11 Sep 2019 11:45:37 +0200 Subject: [PATCH 8/8] sabayon-brokenlinks: Use FORCE/--force instead of --force-manual --- sabayon-brokenlinks | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/sabayon-brokenlinks b/sabayon-brokenlinks index 529bfc8..2524b18 100755 --- a/sabayon-brokenlinks +++ b/sabayon-brokenlinks @@ -7,7 +7,7 @@ set -e export SAB_ARCH="${SAB_ARCH:-}" export DRY_RUN=${DRY_RUN:-0} export DEBUG="${DEBUG:-}" -export FORCE_MANUAL="${FORCE_MANUAL:-0}" +export FORCE="${FORCE:-0}" export USE_EQUO="${USE_EQUO:-1}" declare -a MANUALLY_REMOVED_LINKS @@ -30,7 +30,7 @@ summary() { EXAMINED_LIBDIRS = ${EXAMINED_LIBDIRS} DRY_RUN = ${DRY_RUN} USE_EQUO = ${USE_EQUO} -FORCE_MANUAL= ${FORCE_MANUAL}" +FORCE = ${FORCE}" log 1 "$msg" } @@ -327,7 +327,7 @@ rm_broken_links() { print_links2remove() { for l in ${MANUALLY_REMOVED_LINKS[@]} ; do - if [ "$FORCE_MANUAL" = 1 ] ; then + if [ "$FORCE" = 1 ] ; then if [ "$DRY_RUN" = 0 ] ; then rm -vf ${l} else @@ -352,7 +352,7 @@ Available options: -h|--help This message. --dry-run Dry run execution. ---force-manual Force remove of links to remove manually. +--force Force remove of the links to remove manually. --scandir Define a specific directory to analyze --gentoo Use gentoo tool instead of equo. -d|--debug Enable debug stuff. @@ -361,14 +361,14 @@ Environment variables: DRY_RUN To use instead of --dry-run option. Values: 0 or 1. DEBUG Enable debug output. USE_EQUO To use instead of --gentoo option. -FORCE_MANUAL To use instead of --force-manual. +FORCE To use instead of --force. " } local short_opts="hd" - local long_opts="help dry-run force-manual debug scandir: gentoo" + local long_opts="help dry-run force debug scandir: gentoo" $(set -- $(getopt -u -q -a -o "$short_opts" -l "$long_opts" -- "$@")) @@ -382,8 +382,8 @@ FORCE_MANUAL To use instead of --force-manual. -d|--debug) DEBUG=1 ;; - --force-manual) - FORCE_MANUAL=1 + --force) + FORCE=1 ;; --dry-run) DRY_RUN=1 @@ -448,7 +448,7 @@ main() { fi if [ ${#MANUALLY_REMOVED_LINKS[@]} -gt 0 ] ; then - if [ "${FORCE_MANUAL}" = 1 ] ; then + if [ "${FORCE}" = 1 ] ; then log 1 "Hereinafter, links removed with force:" else log 1 "Hereinafter, links to manually remove:"