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.
23 Usage: $0 [--findoptions='-option1 -option2...']
24 [--localpaths='dir1 dir2...'] [--netpaths='dir1 dir2...']
25 [--prunepaths='dir1 dir2...'] [--prunefs='fs1 fs2...']
26 [--output=dbfile] [--netuser=user] [--localuser=user]
27 [--old-format] [--version] [--help]
29 Report bugs to <bug-findutils@gnu.org>."
34 # If we are unable to fork, the back-tick operator will
35 # fail (and the shell will emit an error message). When
36 # this happens, we exit with error value 71 (EX_OSERR).
37 # Alternative candidate - 75, EX_TEMPFAIL.
38 opt
=`echo $arg|sed 's/^\([^=]*\).*/\1/'` ||
exit 71
39 val
=`echo $arg|sed 's/^[^=]*=\(.*\)/\1/'` ||
exit 71
41 --findoptions) FINDOPTIONS
="$val" ;;
42 --localpaths) SEARCHPATHS
="$val" ;;
43 --netpaths) NETPATHS
="$val" ;;
44 --prunepaths) PRUNEPATHS
="$val" ;;
45 --prunefs) PRUNEFS
="$val" ;;
46 --output) LOCATE_DB
="$val" ;;
47 --netuser) NETUSER
="$val" ;;
48 --localuser) LOCALUSER
="$val" ;;
49 --old-format) old
=yes ;;
50 --changecwd) changeto
="$val" ;;
51 --version) echo "GNU updatedb version @VERSION@"; exit 0 ;;
52 --help) echo "$usage"; exit 0 ;;
53 *) echo "updatedb: invalid option $opt
59 if test "$old" = yes; then
60 echo "Warning: future versions of findutils will shortly discontinue support for the old locate database format." >&2
69 print_option
="-print0"
79 # format of "id" output is ...
80 # uid=1(daemon) gid=1(other)
81 # for `id's that don't understand -u
82 id | cut
-d'(' -f 1 | cut
-d'=' -f2
85 # figure out if su supports the -s option
87 if su
"$1" -s $SHELL false
< /dev
/null
; then
91 if su
"$1" -s $SHELL true
< /dev
/null
; then
95 # su is unconditionally failing. We won't be able to
96 # figure out what is wrong, so be conservative.
103 # You can set these in the environment, or use command-line options,
104 # to override their defaults:
106 # Any global options for find?
109 # What shell shoud we use? We should use a POSIX-ish sh.
112 # Non-network directories to put in the database.
115 # Network (NFS, AFS, RFS, etc.) directories to put in the database.
118 # Directories to not put in the database, which would otherwise be.
119 : ${PRUNEPATHS="/tmp /usr/tmp /var/tmp /afs /amd /sfs"}
121 # Trailing slashes result in regex items that are never matched, which
122 # is not what the user will expect. Therefore we now reject such
124 for p
in $PRUNEPATHS; do
126 /*/) echo "$0: $p: pruned paths should not contain trailing slashes" >&2
131 # The same, in the form of a regex that find can use.
132 test -z "$PRUNEREGEX" &&
133 PRUNEREGEX
=`echo $PRUNEPATHS|sed -e 's,^,\\\(^,' -e 's, ,$\\\)\\\|\\\(^,g' -e 's,$,$\\\),'`
135 # The database file to build.
136 : ${LOCATE_DB=@LOCATE_DB@}
138 # Directory to hold intermediate files.
139 if test -d /var
/tmp
; then
141 elif test -d /usr
/tmp
; then
148 # The user to search network directories as.
151 # The directory containing the subprograms.
152 if test -n "$LIBEXECDIR" ; then
153 : LIBEXECDIR already
set, do nothing
155 : ${LIBEXECDIR=@libexecdir@}
158 # The directory containing find.
159 if test -n "$BINDIR" ; then
160 : BINDIR already
set, do nothing
165 # The names of the utilities to run to build the database.
166 : ${find:=${BINDIR}/@find@}
167 : ${frcode:=${LIBEXECDIR}/@frcode@}
168 : ${bigram:=${LIBEXECDIR}/@bigram@}
169 : ${code:=${LIBEXECDIR}/@code@}
172 PATH
=/bin
:/usr
/bin
:${BINDIR}; export PATH
174 : ${PRUNEFS="nfs NFS proc afs proc smbfs autofs iso9660 ncpfs coda devpts ftpfs devfs mfs sysfs shfs"}
176 if test -n "$PRUNEFS"; then
177 prunefs_exp
=`echo $PRUNEFS |sed -e 's/\([^ ][^ ]*\)/-o -fstype \1/g' \
178 -e 's/-o //' -e 's/$/ -o/'`
183 # Make and code the file list.
184 # Sort case insensitively for users' convenience.
187 trap 'rm -f $LOCATE_DB.n; exit' HUP TERM
189 if test $old = no
; then
191 # FIXME figure out how to sort null-terminated strings, and use -print0.
194 if test -n "$SEARCHPATHS"; then
195 if [ "$LOCALUSER" != "" ]; then
197 su
$LOCALUSER `select_shell $LOCALUSER` -c \
198 "$find $SEARCHPATHS $FINDOPTIONS \
200 -type d -regex '$PRUNEREGEX' \\) -prune -o $print_option"
203 $find $SEARCHPATHS $FINDOPTIONS \
205 -type d
-regex "$PRUNEREGEX" \
) -prune -o $print_option
209 if test -n "$NETPATHS"; then
211 if [ "$myuid" = 0 ]; then
213 su
$NETUSER `select_shell $NETUSER` -c \
214 "$find $NETPATHS $FINDOPTIONS \\( -type d -regex '$PRUNEREGEX' -prune \\) -o $print_option" ||
218 $find $NETPATHS $FINDOPTIONS \
( -type d
-regex "$PRUNEREGEX" -prune \
) -o $print_option ||
222 } |
$sort -f |
$frcode $frcode_options > $LOCATE_DB.n
228 echo "Failed to generate $LOCATE_DB.n" >&2
233 # To avoid breaking locate while this script is running, put the
234 # results in a temp file, then rename it atomically.
235 if test -s $LOCATE_DB.n
; then
237 mv $LOCATE_DB.n
$LOCATE_DB
240 echo "updatedb: new database would be empty" >&2
246 if ! bigrams
=`mktemp -t updatedbXXXXXXXXX`; then
251 if ! filelist
=`mktemp -t updatedbXXXXXXXXX`; then
257 trap 'rm -f $bigrams $filelist $LOCATE_DB.n; exit' HUP TERM
259 # Alphabetize subdirectories before file entries using tr. James Woods says:
260 # "to get everything in monotonic collating sequence, to avoid some
261 # breakage i'll have to think about."
264 if test -n "$SEARCHPATHS"; then
265 if [ "$LOCALUSER" != "" ]; then
267 su
$LOCALUSER `select_shell $LOCALUSER` -c \
268 "$find $SEARCHPATHS $FINDOPTIONS \
270 -type d -regex '$PRUNEREGEX' \) -prune -o $print_option" ||
exit $?
273 $find $SEARCHPATHS $FINDOPTIONS \
275 -type d
-regex "$PRUNEREGEX" \
) -prune -o $print_option ||
exit $?
279 if test -n "$NETPATHS"; then
281 if [ "$myuid" = 0 ]; then
283 su
$NETUSER `select_shell $NETUSER` -c \
284 "$find $NETPATHS $FINDOPTIONS \\( -type d -regex '$PRUNEREGEX' -prune \\) -o $print_option" ||
288 $find $NETPATHS $FINDOPTIONS \
( -type d
-regex "$PRUNEREGEX" -prune \
) -o $print_option ||
292 } |
tr / '\001' |
$sort -f |
tr '\001' / > $filelist
294 # Compute the (at most 128) most common bigrams in the file list.
295 $bigram $bigram_opts < $filelist |
sort |
uniq -c |
sort -nr |
296 awk '{ if (NR <= 128) print $2 }' |
tr -d '\012' > $bigrams
298 # Code the file list.
299 $code $bigrams < $filelist > $LOCATE_DB.n
301 rm -f $bigrams $filelist
303 # To reduce the chances of breaking locate while this script is running,
304 # put the results in a temp file, then rename it atomically.
305 if test -s $LOCATE_DB.n
; then
307 mv $LOCATE_DB.n
$LOCATE_DB
310 echo "updatedb: new database would be empty" >&2