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)
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,
20 # csh original by James Woods; sh conversion by David MacKenzie.
22 #exec 2> /tmp/updatedb-trace.txt
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>."
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
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
62 if test "$old" = yes; then
63 echo "Warning: future versions of findutils will shortly discontinue support for the old locate database format." >&2
72 print_option
="-print0"
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
90 if su
"$1" -s $SHELL false
< /dev
/null
; then
94 if su
"$1" -s $SHELL true
< /dev
/null
; then
98 # su is unconditionally failing. We won't be able to
99 # figure out what is wrong, so be conservative.
106 # You can set these in the environment, or use command-line options,
107 # to override their defaults:
109 # Any global options for find?
112 # What shell shoud we use? We should use a POSIX-ish sh.
115 # Non-network directories to put in the database.
118 # Network (NFS, AFS, RFS, etc.) directories to put in the database.
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
127 for p
in $PRUNEPATHS; do
129 /*/) echo "$0: $p: pruned paths should not contain trailing slashes" >&2
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
144 elif test -d /usr
/tmp
; then
151 # The user to search network directories as.
154 # The directory containing the subprograms.
155 if test -n "$LIBEXECDIR" ; then
156 : LIBEXECDIR already
set, do nothing
158 : ${LIBEXECDIR=@libexecdir@}
161 # The directory containing find.
162 if test -n "$BINDIR" ; then
163 : BINDIR already
set, do nothing
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@}
176 if test -x "$1" ; then
179 eval echo "updatedb needs to be able to execute $1, but cannot." >&2
184 for binary
in $find $frcode $bigram $code
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/'`
201 # Make and code the file list.
202 # Sort case insensitively for users' convenience.
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.
212 if test -n "$SEARCHPATHS"; then
213 if [ "$LOCALUSER" != "" ]; then
215 su
$LOCALUSER `select_shell $LOCALUSER` -c \
216 "$find $SEARCHPATHS $FINDOPTIONS \
218 -type d -regex '$PRUNEREGEX' \\) -prune -o $print_option"
221 $find $SEARCHPATHS $FINDOPTIONS \
223 -type d
-regex "$PRUNEREGEX" \
) -prune -o $print_option
227 if test -n "$NETPATHS"; then
229 if [ "$myuid" = 0 ]; then
231 su
$NETUSER `select_shell $NETUSER` -c \
232 "$find $NETPATHS $FINDOPTIONS \\( -type d -regex '$PRUNEREGEX' -prune \\) -o $print_option" ||
236 $find $NETPATHS $FINDOPTIONS \
( -type d
-regex "$PRUNEREGEX" -prune \
) -o $print_option ||
240 } |
$sort -f |
$frcode $frcode_options > $LOCATE_DB.n
246 echo "Failed to generate $LOCATE_DB.n" >&2
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
255 mv $LOCATE_DB.n
$LOCATE_DB
258 echo "updatedb: new database would be empty" >&2
264 if ! bigrams
=`mktemp -t updatedbXXXXXXXXX`; then
269 if ! filelist
=`mktemp -t updatedbXXXXXXXXX`; then
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."
282 if test -n "$SEARCHPATHS"; then
283 if [ "$LOCALUSER" != "" ]; then
285 su
$LOCALUSER `select_shell $LOCALUSER` -c \
286 "$find $SEARCHPATHS $FINDOPTIONS \
288 -type d -regex '$PRUNEREGEX' \) -prune -o $print_option" ||
exit $?
291 $find $SEARCHPATHS $FINDOPTIONS \
293 -type d
-regex "$PRUNEREGEX" \
) -prune -o $print_option ||
exit $?
297 if test -n "$NETPATHS"; then
299 if [ "$myuid" = 0 ]; then
301 su
$NETUSER `select_shell $NETUSER` -c \
302 "$find $NETPATHS $FINDOPTIONS \\( -type d -regex '$PRUNEREGEX' -prune \\) -o $print_option" ||
306 $find $NETPATHS $FINDOPTIONS \
( -type d
-regex "$PRUNEREGEX" -prune \
) -o $print_option ||
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
325 mv $LOCATE_DB.n
$LOCATE_DB
328 echo "updatedb: new database would be empty" >&2