2 # updatedb -- build a locate pathname database
3 # Copyright (C) 1994, 1996, 1997, 2000, 2001, 2003, 2004, 2005, 2006
4 # Free Software Foundation, Inc.
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2, or (at your option)
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
21 # csh original by James Woods; sh conversion by David MacKenzie.
23 #exec 2> /tmp/updatedb-trace.txt
27 Usage: $0 [--findoptions='-option1 -option2...']
28 [--localpaths='dir1 dir2...'] [--netpaths='dir1 dir2...']
29 [--prunepaths='dir1 dir2...'] [--prunefs='fs1 fs2...']
30 [--output=dbfile] [--netuser=user] [--localuser=user]
31 [--old-format] [--version] [--help]
33 Report bugs to <bug-findutils@gnu.org>."
38 # If we are unable to fork, the back-tick operator will
39 # fail (and the shell will emit an error message). When
40 # this happens, we exit with error value 71 (EX_OSERR).
41 # Alternative candidate - 75, EX_TEMPFAIL.
42 opt
=`echo $arg|sed 's/^\([^=]*\).*/\1/'` ||
exit 71
43 val
=`echo $arg|sed 's/^[^=]*=\(.*\)/\1/'` ||
exit 71
45 --findoptions) FINDOPTIONS
="$val" ;;
46 --localpaths) SEARCHPATHS
="$val" ;;
47 --netpaths) NETPATHS
="$val" ;;
48 --prunepaths) PRUNEPATHS
="$val" ;;
49 --prunefs) PRUNEFS
="$val" ;;
50 --output) LOCATE_DB
="$val" ;;
51 --netuser) NETUSER
="$val" ;;
52 --localuser) LOCALUSER
="$val" ;;
53 --old-format) old
=yes ;;
54 --changecwd) changeto
="$val" ;;
55 --version) echo "GNU updatedb version @VERSION@"; exit 0 ;;
56 --help) echo "$usage"; exit 0 ;;
57 *) echo "updatedb: invalid option $opt
63 if test "$old" = yes; then
64 echo "Warning: future versions of findutils will shortly discontinue support for the old locate database format." >&2
73 print_option
="-print0"
83 # format of "id" output is ...
84 # uid=1(daemon) gid=1(other)
85 # for `id's that don't understand -u
86 id | cut
-d'(' -f 1 | cut
-d'=' -f2
89 # figure out if su supports the -s option
91 if su
"$1" -s $SHELL -c false
< /dev
/null
; then
95 if su
"$1" -s $SHELL -c true
< /dev
/null
; then
99 # su is unconditionally failing. We won't be able to
100 # figure out what is wrong, so be conservative.
107 # You can set these in the environment, or use command-line options,
108 # to override their defaults:
110 # Any global options for find?
113 # What shell shoud we use? We should use a POSIX-ish sh.
116 # Non-network directories to put in the database.
119 # Network (NFS, AFS, RFS, etc.) directories to put in the database.
122 # Directories to not put in the database, which would otherwise be.
123 : ${PRUNEPATHS="/tmp /usr/tmp /var/tmp /afs /amd /sfs"}
125 # Trailing slashes result in regex items that are never matched, which
126 # is not what the user will expect. Therefore we now reject such
128 for p
in $PRUNEPATHS; do
130 /*/) echo "$0: $p: pruned paths should not contain trailing slashes" >&2
135 # The same, in the form of a regex that find can use.
136 test -z "$PRUNEREGEX" &&
137 PRUNEREGEX
=`echo $PRUNEPATHS|sed -e 's,^,\\\(^,' -e 's, ,$\\\)\\\|\\\(^,g' -e 's,$,$\\\),'`
139 # The database file to build.
140 : ${LOCATE_DB=@LOCATE_DB@}
142 # Directory to hold intermediate files.
143 if test -d /var
/tmp
; then
145 elif test -d /usr
/tmp
; then
152 # The user to search network directories as.
155 # The directory containing the subprograms.
156 if test -n "$LIBEXECDIR" ; then
157 : LIBEXECDIR already
set, do nothing
159 : ${LIBEXECDIR=@libexecdir@}
162 # The directory containing find.
163 if test -n "$BINDIR" ; then
164 : BINDIR already
set, do nothing
169 # The names of the utilities to run to build the database.
170 : ${find:=${BINDIR}/@find@}
171 : ${frcode:=${LIBEXECDIR}/@frcode@}
172 : ${bigram:=${LIBEXECDIR}/@bigram@}
173 : ${code:=${LIBEXECDIR}/@code@}
177 if test -x "$1" ; then
180 eval echo "updatedb needs to be able to execute $1, but cannot." >&2
185 for binary
in $find $frcode $bigram $code
191 PATH
=/bin
:/usr
/bin
:${BINDIR}; export PATH
193 : ${PRUNEFS="nfs NFS proc afs proc smbfs autofs iso9660 ncpfs coda devpts ftpfs devfs mfs sysfs shfs"}
195 if test -n "$PRUNEFS"; then
196 prunefs_exp
=`echo $PRUNEFS |sed -e 's/\([^ ][^ ]*\)/-o -fstype \1/g' \
197 -e 's/-o //' -e 's/$/ -o/'`
202 # Make and code the file list.
203 # Sort case insensitively for users' convenience.
206 trap 'rm -f $LOCATE_DB.n; exit' HUP TERM
208 if test $old = no
; then
210 # FIXME figure out how to sort null-terminated strings, and use -print0.
213 if test -n "$SEARCHPATHS"; then
214 if [ "$LOCALUSER" != "" ]; then
216 su
$LOCALUSER `select_shell $LOCALUSER` -c \
217 "$find $SEARCHPATHS $FINDOPTIONS \
219 -type d -regex '$PRUNEREGEX' \\) -prune -o $print_option"
222 $find $SEARCHPATHS $FINDOPTIONS \
224 -type d
-regex "$PRUNEREGEX" \
) -prune -o $print_option
228 if test -n "$NETPATHS"; then
230 if [ "$myuid" = 0 ]; then
232 su
$NETUSER `select_shell $NETUSER` -c \
233 "$find $NETPATHS $FINDOPTIONS \\( -type d -regex '$PRUNEREGEX' -prune \\) -o $print_option" ||
237 $find $NETPATHS $FINDOPTIONS \
( -type d
-regex "$PRUNEREGEX" -prune \
) -o $print_option ||
241 } |
$sort -f |
$frcode $frcode_options > $LOCATE_DB.n
247 echo "Failed to generate $LOCATE_DB.n" >&2
252 # To avoid breaking locate while this script is running, put the
253 # results in a temp file, then rename it atomically.
254 if test -s $LOCATE_DB.n
; then
256 mv $LOCATE_DB.n
$LOCATE_DB
259 echo "updatedb: new database would be empty" >&2
265 if ! bigrams
=`mktemp -t updatedbXXXXXXXXX`; then
270 if ! filelist
=`mktemp -t updatedbXXXXXXXXX`; then
276 trap 'rm -f $bigrams $filelist $LOCATE_DB.n; exit' HUP TERM
278 # Alphabetize subdirectories before file entries using tr. James Woods says:
279 # "to get everything in monotonic collating sequence, to avoid some
280 # breakage i'll have to think about."
283 if test -n "$SEARCHPATHS"; then
284 if [ "$LOCALUSER" != "" ]; then
286 su
$LOCALUSER `select_shell $LOCALUSER` -c \
287 "$find $SEARCHPATHS $FINDOPTIONS \
289 -type d -regex '$PRUNEREGEX' \) -prune -o $print_option" ||
exit $?
292 $find $SEARCHPATHS $FINDOPTIONS \
294 -type d
-regex "$PRUNEREGEX" \
) -prune -o $print_option ||
exit $?
298 if test -n "$NETPATHS"; then
300 if [ "$myuid" = 0 ]; then
302 su
$NETUSER `select_shell $NETUSER` -c \
303 "$find $NETPATHS $FINDOPTIONS \\( -type d -regex '$PRUNEREGEX' -prune \\) -o $print_option" ||
307 $find $NETPATHS $FINDOPTIONS \
( -type d
-regex "$PRUNEREGEX" -prune \
) -o $print_option ||
311 } |
tr / '\001' |
$sort -f |
tr '\001' / > $filelist
313 # Compute the (at most 128) most common bigrams in the file list.
314 $bigram $bigram_opts < $filelist |
sort |
uniq -c |
sort -nr |
315 awk '{ if (NR <= 128) print $2 }' |
tr -d '\012' > $bigrams
317 # Code the file list.
318 $code $bigrams < $filelist > $LOCATE_DB.n
320 rm -f $bigrams $filelist
322 # To reduce the chances of breaking locate while this script is running,
323 # put the results in a temp file, then rename it atomically.
324 if test -s $LOCATE_DB.n
; then
326 mv $LOCATE_DB.n
$LOCATE_DB
329 echo "updatedb: new database would be empty" >&2