From 8fdd508949e754e8ea758e8c1b3c063e41cce3f2 Mon Sep 17 00:00:00 2001 From: Lucy Phipps Date: Sun, 22 Nov 2020 06:23:05 +0000 Subject: [PATCH] only bother with leaf nodes, smaller but slower? (#12) also error message when package not found also correctly prepend $PREFIX on reverse mode --- update-whatprovides-db.sh | 10 ++++++++-- whatprovides | 15 ++++++++++++--- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/update-whatprovides-db.sh b/update-whatprovides-db.sh index f52a0b4..115a9f7 100755 --- a/update-whatprovides-db.sh +++ b/update-whatprovides-db.sh @@ -9,9 +9,15 @@ set -e -u : "${TERMUX_PREFIX:="/data/data/com.termux/files/usr"}" list_files() { - dpkg-deb --fsys-tarfile "${1}" | tar -t | cut -b2- \ + FILES=$(dpkg-deb --fsys-tarfile "${1}" | tar -t | cut -b2- \ | xargs -rd\\n realpath -sm --relative-base="$TERMUX_PREFIX" -- \ - | grep -vEx '[./]|/data(/data(/com\.termux(/files)?)?)?' + | grep -vEx '[./]|/data(/data(/com\.termux(/files)?)?)?') + SORTKEYS='-k1,1' + for ((x=2; x<=$(wc -L <<< "${FILES//[^\/$'\n']/}")+1; x++)); do + SORTKEYS="$SORTKEYS -k$x,$x" + done + sort -t/ $SORTKEYS <<< "$FILES" \ + | awk 'NR == 1 { p=$0; next } substr($0, 1, length(p) +1 ) != p"/" { print p } { p=$0 } END { print p }' } write_sql_script() { diff --git a/whatprovides b/whatprovides index 6ce116b..acacd48 100755 --- a/whatprovides +++ b/whatprovides @@ -116,9 +116,17 @@ if ${REVERSE_MODE}; then exit 1 fi - sqlite3 "${DB_PATH}" \ + if ! sqlite3 "${DB_PATH}" \ "SELECT owned_file FROM 'whatprovides' WHERE package_name == '${1}' ORDER BY owned_file" \ - | awk "NR == 1 { print \"${1}: /.\\n${1}: /data\\n${1}: /data/data\\n${1}: /data/data/com.termux\\n${1}: /data/data/com.termux/files\\n${1}: ${PREFIX}\" } { print \"${1}: ${PREFIX}/\"\$0 }" + | awk "{ if (substr(\$0, 1, 1) == \"/\") print \"${1}: \"\$0; else print \"${1}: ${PREFIX}/\"\$0 } END { if (NR == 0) exit 1 }" + then + { + echo + echo "Error: package '$1' is not found." + echo + } >&2 + exit 1 + fi else NEWPREFIX= FILE="$(realpath -sm "$1")" @@ -141,8 +149,9 @@ else NEWPREFIX="${PREFIX}/" fi # else FILE is entirely separate from PREFIX (unlikely to be in DB) + FILE_ESC="${FILE//\'/\'\'}" if ! sqlite3 "${DB_PATH}" \ - "SELECT package_name FROM 'whatprovides' WHERE owned_file == '${FILE//\'/\'\'}' ORDER BY package_name" \ + "SELECT DISTINCT package_name FROM 'whatprovides' WHERE owned_file == '${FILE_ESC}' OR substr(owned_file, 1, ${#FILE} +1) == '${FILE_ESC}/' ORDER BY package_name" \ | awk "{ print \$0\": ${NEWPREFIX}${FILE}\" } END { if (NR == 0) exit 1 }" then {