-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwhatprovides
executable file
·245 lines (213 loc) · 6.28 KB
/
whatprovides
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#!/bin/bash
# vim: set ts=4 sw=4 expandtab :
# Copyright (c) 2017 Timothy Savannah - All Rights Reserved
# This code is licensed under the terms of the APACHE license version 2.0
#
#
# Whatprovides - Query pacman database to see what package provides a given file
# on the current system.
#
# Note, if a directory is queried, multiple packages may be printed (one-per-line)
#
# Returns 0 if at least one provider is found
echoerr() {
echo "$@" >&2
}
usageSummary() {
echoerr "Usage: whatprovides (Options) [file]"
echoerr " Prints the package which provides the given file on the current system,"
echoerr " otherwise returns non-zero."
echoerr;
echoerr " Use '*' as a wildcard. Example: '*/libz.so*'"
echoerr " When in wildcard mode, both the package name will be printed, followed"
echoerr " by a tab, and then the matched file."
echoerr;
}
usageOptions() {
echoerr " Options:"
echoerr;
echoerr " -n --no-cache DO NOT Use a cache of the package ownership list. "
echoerr " This option is should never be necessary, "
echoerr " as the cache will be regenerated when any "
echoerr " package is installed/removed/updated. "
}
usageShort() {
usageSummary;
echoerr "Use --help to see available options"
echoerr;
}
usageFull() {
usageSummary;
usageOptions;
echoerr;
}
if ( echo " $@ " | grep -qE ' \-\-help ' );
then
usageFull;
exit 0;
fi
if [[ $# -eq 0 ]];
then
usageFull;
exit 2;
fi
if [[ $# -gt 2 ]];
then
echoerr -e "Too many arguments.\n"
usageShort;
exit 2;
fi
USE_CACHED="true"
QUERY_ARG="unset"
for arg in "$@";
do
if [[ "${arg}" = "-n" ]] || [[ "${arg}" = "--no-cache" ]];
then
USE_CACHED="false"
elif [[ "${QUERY_ARG}" = "unset" ]];
then
QUERY_ARG="${arg}"
else
echoerr -e "Unknown argument: '${arg}'\n"
usageShort;
exit 2;
fi
done
if [[ "${QUERY_ARG}" = "unset" ]];
then
echoerr -e "Missing query file.\n\n"
usageShort;
fi
get_mtime() {
stat --printf="%Y" "${1}"
return $?
}
#######
## check_db_needs_regen - Check if we need to regen.
##
## Args:
##
## arg1 - database name
##
## Return:
## 0 - database DOES need regen
## 1 - database does NOT need regen
##
check_db_needs_regen() {
TRY_DB="${1}"
[[ ! -e "${TRY_DB}" ]] && return 0;
[[ $(get_mtime '/var/log/pacman.log' ) -le $(get_mtime "${TRY_DB}") ]] && return 1
return 0
}
check_can_use_dbfile() {
TRY_DB="${1}"
if [[ ! -e "${TRY_DB}" ]];
then
TMP_FILE="$(mktemp /tmp/.doXXXXXXXX)"
# Create with an old time (Just do +1 year on epoch, so don't have to worry about timezone going
# BACK TO THE FUTURE!!!
printf "#!/bin/bash\ntouch -d 'Jan 1 1971 00:00:00' '%s' 2>/dev/null && rm -f '%s' 2>/dev/null\nexit \$?\n'" "${TRY_DB}" "${TRY_DB}" > "${TMP_FILE}"
chmod +x "${TMP_FILE}"
flock "${TRY_DB}" -c "${TMP_FILE}" >/dev/null 2>/dev/null
rm -f "${TMP_FILE}"
if [ -e "${TRY_DB}" -a -w "${TRY_DB}" ];
then
# The database exists and we can write to it, so it's usable.
return 0;
else
# Does not already exist and failed to create
return 1;
fi
fi
# At this point DB does exist.
if [[ -w "${TRY_DB}" ]];
then
# If we can write, we are good to go and use it
return 0;
elif [[ -r "${TRY_DB}" ]];
then
# Otherwise, if we can read it, check if it needs update.
# If it does, since we can't update (no write), can't use it.
if ( check_db_needs_regen "${TRY_DB}" );
then
return 1;
fi
# We need to update, so fallback.
return 0;
else
# Can't read, can't write, can't do diddly-squat!
return 1;
fi
}
# PHEW! That was a LOT of argument parsing! Onto the good stuff...
if [[ "${USE_CACHED}" = "true" ]];
then
FOUND_USABLE_DB="false"
for tryDB in "/var/cache/pacman/whatprovides.db" "$HOME/.whatprovides.db";
do
if ( check_can_use_dbfile "${tryDB}" );
then
FOUND_USABLE_DB="true"
USE_CACHED="${tryDB}"
break
fi
done
if [[ "${FOUND_USABLE_DB}" = "true" ]];
then
if ( check_db_needs_regen "${USE_CACHED}" );
then
TMP_FILE="$(mktemp /tmp/.doXXXXXXXX)"
printf "#!/bin/bash\npacman -Ql > '%s'\n" "${USE_CACHED}" > "${TMP_FILE}"
chmod +x "${TMP_FILE}"
flock "${USE_CACHED}" -c "${TMP_FILE}"
rm -f "${TMP_FILE}"
fi
else
echoerr "Warning: Can't access /var/cache/pacman/whatprovides.db (or needs regenerated), and cannot create/update file: '$HOME/.whatprovides'"
echoerr "Warning: Skipping cached DB."
USE_CACHED="false"
fi
fi
if ( echo "${QUERY_ARG}" | grep -q '[*?]' );
then
HAS_WILDCARD="true"
if ( ! echo "${QUERY_ARG}" | grep -q '^[/\*]' );
then
# If no star or slash at start, add star for them.
# (otherwise, will never match)
QUERY_ARG='*'"${QUERY_ARG}"
fi
SEARCH="$(printf "%s\n" "${QUERY_ARG}" | sed -e "s|\.|[\\\\.]|g" -e 's|*|.*|g' -e 's|[?]|.|g')"
else
HAS_WILDCARD="false"
if [[ ! -e "${QUERY_ARG}" ]] && ! ( echo "${QUERY_ARG}" | grep -q "/" );
then
SEARCH="$(/usr/bin/which "${QUERY_ARG}" 2>/dev/null)"
if [ $? -ne 0 -o -z "${SEARCH}" ];
then
echoerr "Warning: '${QUERY_ARG}' does not contain a slash and does not point to a file/directory, and cannot find in PATH. Failing.";
exit 1;
fi
else
SEARCH="$(/usr/bin/realpath -s "${QUERY_ARG}")"
fi
fi
if [[ -d "${SEARCH}" ]] && ( ! echo "${SEARCH}" | grep -qE '/$' );
then
SEARCH="${SEARCH}/"
fi
if [[ "${USE_CACHED}" = "false" ]];
then
DB_QUERY="pacman -Ql"
else
DB_QUERY="cat ${USE_CACHED}"
fi
if [ "${HAS_WILDCARD}" = "false" ];
then
${DB_QUERY} | grep " ${SEARCH}"'$' | sed 's/ .*//g' | sort
[ $? -ne 0 ] && exit 1;
else
${DB_QUERY} | grep -E " ${SEARCH}"'$' | sed "s| |\t|" | sort
[ $? -ne 0 ] && exit 1;
fi
# vim: set ts=4 sw=4 expandtab :