diff options
Diffstat (limited to 'pacman-ps.sh')
-rwxr-xr-x | pacman-ps.sh | 89 |
1 files changed, 60 insertions, 29 deletions
diff --git a/pacman-ps.sh b/pacman-ps.sh index 681580ab3ac3..bead513089fd 100755 --- a/pacman-ps.sh +++ b/pacman-ps.sh @@ -1,52 +1,83 @@ -#!/bin/sh +#!/bin/env bash # Copyright (C) 2016 Samantha McVey <samantham@posteo.net> -# This file and project are licensed under the GPLv2 or greater at your choosing +# This file and project are licensed under the GPLv2 or greater at your choosing. +# For more information view the license included or visit: +# https://www.gnu.org/licenses/gpl-2.0.html # # This script processes the output from ps-lsof which is a two column list, where -# the 1st and 2nd columns must be delimited by a space. The first column of the +# the 1st and 2nd columns must be delimited by spaces. The first column of the # input is the process name. The second column is the file. It then compares -# the filename with the file at $PACKAGELIST which is a two column file delimited +# the filename with the file at $DB_FILE which is a two column file delimited # by spaces, the first column is the name of the package and the second is the # filename which belongs to the package. +cleanuptemp() { + if [ "${DB_TEMP_FILE}" = "1" ]; then + rm "${LIST}" + fi +} -PACKAGELIST="/var/cache/pacman-ps/files.db" -TEMPLIST="/tmp/pacman-ps/files.db" -TEMPDIR="/tmp/pacman-ps" +DB_FILE="/var/cache/pacman-ps/files.db" +# Get the resolved filename of the currently running script RUN=$(readlink -f "$0") +# Get the directory name that file is in RUNDIR=$(dirname "${RUN}") +# If a file called ps-lsof is found in the same directory as pacman-ps use that if [ -f "${RUNDIR}/ps-lsof" ]; then - PS_LSOF="${RUNDIR}/ps-lsof" + PS_LSOF_CMD="${RUNDIR}/ps-lsof -q" +# Otherwise, if we find a ps-lsof.sh file, use that to call ps-lsof elif [ -f "${RUNDIR}/ps-lsof.sh" ]; then - PS_LSOF="${RUNDIR}/ps-lsof.sh" + PS_LSOF_CMD="${RUNDIR}/ps-lsof.sh -q" else echo "Could not find ps-lsof or ps-lsof.sh" echo "Make sure it is in the same folder as pacman-ps or pacman-ps.sh" exit 1 fi -if [ -f "${PACKAGELIST}" ]; then - # echo "Using ${PACKAGELIST} for the package and file list" - LIST=${PACKAGELIST} -else - echo "Could not find ${PACKAGELIST}" - echo "Asking pacman for file listing using pacman -Ql" - mkdir -p ${TEMPDIR} - pacman -Ql > ${TEMPLIST} - LIST=${TEMPLIST} +# -s option will only print out packages and files but not processes + + +# Get the output from ps-lsof and sort it so that 'join' will be happy +PS_LSOF_OUTPUT=$($PS_LSOF_CMD | sort -k 2,2) +# If ps-lsof doesn't output anything, our work here is done +if [ "${PS_LSOF_OUTPUT}" = "" ]; then + exit 0 fi -#PACMANCHK="${RUNDIR}/ps-lsof.sh | cut -d ' ' -f 2-99 | xargs -I '{}' grep '{}' ${LIST} | sort | column -t" -if [ "${1}" = "-s" ]; then - $PS_LSOF | cut -d ' ' -f 2-99 | xargs -I '{}' grep '{}' ${LIST} | sort | uniq | (echo "PROCESS FILENAME"; cat) | column -t +# If we find the database file at $DB_FILE then use that +if [ -f "${DB_FILE}" ]; then + LIST=${DB_FILE} else - mkdir -p ${TEMPDIR} - $PS_LSOF | sort -k 2 > "${TEMPDIR}/process.txt" - cut -d ' ' -f 2-99 < ${TEMPDIR}/process.txt | xargs -I '{}' grep '{}' ${LIST} | sort | uniq | column -t | sort -k 2 > "${TEMPDIR}/package.txt" - join -j 2 "${TEMPDIR}/process.txt" "${TEMPDIR}/package.txt" | column -t | sort -k 2 | uniq > "${TEMPDIR}/all.txt" - echo "FILE PROCESS PACKAGE" > ${TEMPDIR}/head.txt - cat ${TEMPDIR}/head.txt ${TEMPDIR}/all.txt | column -t + # If we can't find it, print out that we are falling back to asking pacman + # Unless we invoke -q (quiet) + if [ ! "${1}" = "-q" ]; then + echo "Could not find ${DB_FILE}" + echo "Asking pacman for file listing using pacman -Ql" + fi + # Get a temp filename + LIST=$(mktemp) + trap "cleanuptemp" + pacman -Ql | sort -u -k 2,2 > "${LIST}" + DB_TEMP_FILE="1" +fi +if [ "${1}" = "-s" ]; then + $PS_LSOF_CMD | cut -d ' ' -f 2-99 | xargs -I '{}' grep '{}' "${LIST}" | sort -u | (echo "PROCESS FILENAME"; cat) | column -t + exit 0 +#elif [ "${1}" = "-h" ]; then +# printf "-q (quiet) -s (show only packages and files) -h (shows this help)\n" +# exit 0 +fi + +OUTPUT=$(join -j 2 <(printf "%s" "${PS_LSOF_OUTPUT}") <(cut -d ' ' -f 2-99 <(printf "%s" "${PS_LSOF_OUTPUT}") | xargs -P 8 -I '{}' grep '{}' "${LIST}" | sort -u -k 2,2) ) +# If there's no output just cleanup a temp file if there is one and exit +if [ "$OUTPUT" = "" ]; then + cleanuptemp + exit 0 fi -if [ -d ${TEMPDIR} ]; then - rm -rf ${TEMPDIR} +if [ "${1}" = "-q" ]; then + printf "%s\n" "$OUTPUT" | column -t +else + printf "FILENAME PROCESS PACKAGE\n%s\n" "$OUTPUT" | column -t fi +cleanuptemp +exit 0 |