Don't leave junk files lying around the place
[findutils.git] / locate / updatedb.sh
blob5786bdab446cee33e1a182908a05a51681b49f4f
1 #! /bin/sh
2 # updatedb -- build a locate pathname database
3 # Copyright (C) 1994 Free Software Foundation, Inc.
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2, or (at your option)
8 # any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
18 # USA.
20 # csh original by James Woods; sh conversion by David MacKenzie.
22 #exec 2> /tmp/updatedb-trace.txt
23 #set -x
25 usage="\
26 Usage: $0 [--findoptions='-option1 -option2...']
27 [--localpaths='dir1 dir2...'] [--netpaths='dir1 dir2...']
28 [--prunepaths='dir1 dir2...'] [--prunefs='fs1 fs2...']
29 [--output=dbfile] [--netuser=user] [--localuser=user]
30 [--old-format] [--version] [--help]
32 Report bugs to <bug-findutils@gnu.org>."
33 changeto=/
34 old=no
35 for arg
37 # If we are unable to fork, the back-tick operator will
38 # fail (and the shell will emit an error message). When
39 # this happens, we exit with error value 71 (EX_OSERR).
40 # Alternative candidate - 75, EX_TEMPFAIL.
41 opt=`echo $arg|sed 's/^\([^=]*\).*/\1/'` || exit 71
42 val=`echo $arg|sed 's/^[^=]*=\(.*\)/\1/'` || exit 71
43 case "$opt" in
44 --findoptions) FINDOPTIONS="$val" ;;
45 --localpaths) SEARCHPATHS="$val" ;;
46 --netpaths) NETPATHS="$val" ;;
47 --prunepaths) PRUNEPATHS="$val" ;;
48 --prunefs) PRUNEFS="$val" ;;
49 --output) LOCATE_DB="$val" ;;
50 --netuser) NETUSER="$val" ;;
51 --localuser) LOCALUSER="$val" ;;
52 --old-format) old=yes ;;
53 --changecwd) changeto="$val" ;;
54 --version) echo "GNU updatedb version @VERSION@"; exit 0 ;;
55 --help) echo "$usage"; exit 0 ;;
56 *) echo "updatedb: invalid option $opt
57 $usage" >&2
58 exit 1 ;;
59 esac
60 done
62 if test "$old" = yes; then
63 echo "Warning: future versions of findutils will shortly discontinue support for the old locate database format." >&2
65 sort="@SORT@"
66 print_option="-print"
67 frcode_options=""
68 else
69 if @SORT_SUPPORTS_Z@
70 then
71 sort="@SORT@ -z"
72 print_option="-print0"
73 frcode_options="-0"
74 else
75 sort="@SORT@"
76 print_option="-print"
77 frcode_options=""
81 getuid() {
82 # format of "id" output is ...
83 # uid=1(daemon) gid=1(other)
84 # for `id's that don't understand -u
85 id | cut -d'(' -f 1 | cut -d'=' -f2
88 # figure out if su supports the -s option
89 select_shell() {
90 if su "$1" -s $SHELL false < /dev/null ; then
91 # No.
92 echo ""
93 else
94 if su "$1" -s $SHELL true < /dev/null ; then
95 # Yes.
96 echo "-s $SHELL"
97 else
98 # su is unconditionally failing. We won't be able to
99 # figure out what is wrong, so be conservative.
100 echo ""
106 # You can set these in the environment, or use command-line options,
107 # to override their defaults:
109 # Any global options for find?
110 : ${FINDOPTIONS=}
112 # What shell shoud we use? We should use a POSIX-ish sh.
113 : ${SHELL="/bin/sh"}
115 # Non-network directories to put in the database.
116 : ${SEARCHPATHS="/"}
118 # Network (NFS, AFS, RFS, etc.) directories to put in the database.
119 : ${NETPATHS=}
121 # Directories to not put in the database, which would otherwise be.
122 : ${PRUNEPATHS="/tmp /usr/tmp /var/tmp /afs /amd /sfs"}
124 # Trailing slashes result in regex items that are never matched, which
125 # is not what the user will expect. Therefore we now reject such
126 # constructs.
127 for p in $PRUNEPATHS; do
128 case "$p" in
129 /*/) echo "$0: $p: pruned paths should not contain trailing slashes" >&2
130 exit 1
131 esac
132 done
134 # The same, in the form of a regex that find can use.
135 test -z "$PRUNEREGEX" &&
136 PRUNEREGEX=`echo $PRUNEPATHS|sed -e 's,^,\\\(^,' -e 's, ,$\\\)\\\|\\\(^,g' -e 's,$,$\\\),'`
138 # The database file to build.
139 : ${LOCATE_DB=@LOCATE_DB@}
141 # Directory to hold intermediate files.
142 if test -d /var/tmp; then
143 : ${TMPDIR=/var/tmp}
144 elif test -d /usr/tmp; then
145 : ${TMPDIR=/usr/tmp}
146 else
147 : ${TMPDIR=/tmp}
149 export TMPDIR
151 # The user to search network directories as.
152 : ${NETUSER=daemon}
154 # The directory containing the subprograms.
155 if test -n "$LIBEXECDIR" ; then
156 : LIBEXECDIR already set, do nothing
157 else
158 : ${LIBEXECDIR=@libexecdir@}
161 # The directory containing find.
162 if test -n "$BINDIR" ; then
163 : BINDIR already set, do nothing
164 else
165 : ${BINDIR=@bindir@}
168 # The names of the utilities to run to build the database.
169 : ${find:=${BINDIR}/@find@}
170 : ${frcode:=${LIBEXECDIR}/@frcode@}
171 : ${bigram:=${LIBEXECDIR}/@bigram@}
172 : ${code:=${LIBEXECDIR}/@code@}
175 checkbinary () {
176 if test -x "$1" ; then
177 : ok
178 else
179 eval echo "updatedb needs to be able to execute $1, but cannot." >&2
180 exit 1
184 for binary in $find $frcode $bigram $code
186 checkbinary $binary
187 done
190 PATH=/bin:/usr/bin:${BINDIR}; export PATH
192 : ${PRUNEFS="nfs NFS proc afs proc smbfs autofs iso9660 ncpfs coda devpts ftpfs devfs mfs sysfs shfs"}
194 if test -n "$PRUNEFS"; then
195 prunefs_exp=`echo $PRUNEFS |sed -e 's/\([^ ][^ ]*\)/-o -fstype \1/g' \
196 -e 's/-o //' -e 's/$/ -o/'`
197 else
198 prunefs_exp=''
201 # Make and code the file list.
202 # Sort case insensitively for users' convenience.
204 rm -f $LOCATE_DB.n
205 trap 'rm -f $LOCATE_DB.n; exit' HUP TERM
207 if test $old = no; then
209 # FIXME figure out how to sort null-terminated strings, and use -print0.
210 if {
211 cd "$changeto"
212 if test -n "$SEARCHPATHS"; then
213 if [ "$LOCALUSER" != "" ]; then
214 # : A1
215 su $LOCALUSER `select_shell $LOCALUSER` -c \
216 "$find $SEARCHPATHS $FINDOPTIONS \
217 \\( $prunefs_exp \
218 -type d -regex '$PRUNEREGEX' \\) -prune -o $print_option"
219 else
220 # : A2
221 $find $SEARCHPATHS $FINDOPTIONS \
222 \( $prunefs_exp \
223 -type d -regex "$PRUNEREGEX" \) -prune -o $print_option
227 if test -n "$NETPATHS"; then
228 myuid=`getuid`
229 if [ "$myuid" = 0 ]; then
230 # : A3
231 su $NETUSER `select_shell $NETUSER` -c \
232 "$find $NETPATHS $FINDOPTIONS \\( -type d -regex '$PRUNEREGEX' -prune \\) -o $print_option" ||
233 exit $?
234 else
235 # : A4
236 $find $NETPATHS $FINDOPTIONS \( -type d -regex "$PRUNEREGEX" -prune \) -o $print_option ||
237 exit $?
240 } | $sort -f | $frcode $frcode_options > $LOCATE_DB.n
241 then
242 : OK so far
243 true
244 else
245 rv=$?
246 echo "Failed to generate $LOCATE_DB.n" >&2
247 rm -f $LOCATE_DB.n
248 exit $rv
251 # To avoid breaking locate while this script is running, put the
252 # results in a temp file, then rename it atomically.
253 if test -s $LOCATE_DB.n; then
254 rm -f $LOCATE_DB
255 mv $LOCATE_DB.n $LOCATE_DB
256 chmod 644 $LOCATE_DB
257 else
258 echo "updatedb: new database would be empty" >&2
259 rm -f $LOCATE_DB.n
262 else # old
264 if ! bigrams=`mktemp -t updatedbXXXXXXXXX`; then
265 echo tempfile failed
266 exit 1
269 if ! filelist=`mktemp -t updatedbXXXXXXXXX`; then
270 echo tempfile failed
271 exit 1
274 rm -f $LOCATE_DB.n
275 trap 'rm -f $bigrams $filelist $LOCATE_DB.n; exit' HUP TERM
277 # Alphabetize subdirectories before file entries using tr. James Woods says:
278 # "to get everything in monotonic collating sequence, to avoid some
279 # breakage i'll have to think about."
281 cd "$changeto"
282 if test -n "$SEARCHPATHS"; then
283 if [ "$LOCALUSER" != "" ]; then
284 # : A5
285 su $LOCALUSER `select_shell $LOCALUSER` -c \
286 "$find $SEARCHPATHS $FINDOPTIONS \
287 \( $prunefs_exp \
288 -type d -regex '$PRUNEREGEX' \) -prune -o $print_option" || exit $?
289 else
290 # : A6
291 $find $SEARCHPATHS $FINDOPTIONS \
292 \( $prunefs_exp \
293 -type d -regex "$PRUNEREGEX" \) -prune -o $print_option || exit $?
297 if test -n "$NETPATHS"; then
298 myuid=`getuid`
299 if [ "$myuid" = 0 ]; then
300 # : A7
301 su $NETUSER `select_shell $NETUSER` -c \
302 "$find $NETPATHS $FINDOPTIONS \\( -type d -regex '$PRUNEREGEX' -prune \\) -o $print_option" ||
303 exit $?
304 else
305 # : A8
306 $find $NETPATHS $FINDOPTIONS \( -type d -regex "$PRUNEREGEX" -prune \) -o $print_option ||
307 exit $?
310 } | tr / '\001' | $sort -f | tr '\001' / > $filelist
312 # Compute the (at most 128) most common bigrams in the file list.
313 $bigram $bigram_opts < $filelist | sort | uniq -c | sort -nr |
314 awk '{ if (NR <= 128) print $2 }' | tr -d '\012' > $bigrams
316 # Code the file list.
317 $code $bigrams < $filelist > $LOCATE_DB.n
319 rm -f $bigrams $filelist
321 # To reduce the chances of breaking locate while this script is running,
322 # put the results in a temp file, then rename it atomically.
323 if test -s $LOCATE_DB.n; then
324 rm -f $LOCATE_DB
325 mv $LOCATE_DB.n $LOCATE_DB
326 chmod 644 $LOCATE_DB
327 else
328 echo "updatedb: new database would be empty" >&2
329 rm -f $LOCATE_DB.n
334 exit 0