This repository has been archived by the owner on Jul 11, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
update-whatprovides-db.sh
executable file
·83 lines (67 loc) · 2.67 KB
/
update-whatprovides-db.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/env bash
##
## Script that generates or updates a Whatprovides
## database for Termux.
##
set -e -u
list_files() {
FILES=$(dpkg-deb --fsys-tarfile "${1}" | tar -t | cut -b2- \
| xargs -rd\\n realpath -sm --relative-base="/data/data/com.termux/files/usr" -- \
| grep -vEx '[./]|/data(/data(/com\.termux(/files)?)?)?')
SORTKEYS='-k1,1'
MAXKEY=$(awk -F/ 'BEGIN { x=0 } { if (NF > x) x = NF } END { print x }' <<< "${FILES}")
for ((x=2; x<=$MAXKEY; 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() {
local debfile_dir=$1
local debfile_path
# SQL statements should be executed within a single
# transaction, otherwise execution performance would
# be very slow.
echo "BEGIN TRANSACTION;" >> "$SQLSCRIPT_TMPFILE"
while read -r debfile_path; do
# .deb files are named package-name_version-revision_cpuarch.deb
local package_name
package_name=$(basename "$debfile_path" | cut -d_ -f1)
echo "[*] Processing '${debfile_path}'..." >&2
# Delete content of processed package to ensure there will not be any
# duplicates and stale entries.
echo "DELETE FROM whatprovides WHERE package_name == \"${package_name}\";" >> "$SQLSCRIPT_TMPFILE"
# Add row with matching package-name and file-name.
while read -r file_path; do
echo "${package_name}: adding '${file_path}'" >&2
echo "INSERT INTO whatprovides (package_name, owned_file) VALUES (\"${package_name}\", \"${file_path}\");" >> "$SQLSCRIPT_TMPFILE"
done < <(list_files "$debfile_path")
# Note: searching only for ARCH "all" (platform-independent) and "aarch64" which
# has 100% support by Termux packages.
done < <(find "$debfile_dir" -type f \( -name "*_all.deb" -o -name "*_aarch64.deb" \) -print)
echo "COMMIT;" >> "$SQLSCRIPT_TMPFILE"
# Databases which were not generated from scratch needs
# to be compacted.
echo "VACUUM;" >> "$SQLSCRIPT_TMPFILE"
echo "REINDEX;" >> "$SQLSCRIPT_TMPFILE"
}
if [ $# -ge 2 ]; then
SQLSCRIPT_TMPFILE=$(mktemp -t "whatprovides.XXXXXX.sql")
if [ ! -e "${1}" ]; then
echo "[*] Creating new database." >&2
sqlite3 "$1" "CREATE TABLE whatprovides ( id INTEGER PRIMARY KEY, package_name TEXT NOT NULL, owned_file TEXT NOT NULL);"
fi
echo "[*] Generating SQL script..." >&2
write_sql_script "$2" "$SQLSCRIPT_TMPFILE"
echo "[*] Rebuilding database..." >&2
sqlite3 "$1" < "$SQLSCRIPT_TMPFILE"
rm -f "${SQLSCRIPT_TMPFILE}"
echo "[*] Done." >&2
else
echo
echo "Usage: update-whatprovides-db.sh [db path] [directory with .deb files]"
echo
echo "Regenerate a database for Termux Whatprovides utility."
echo
exit 1
fi