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 # The same, in the form of a regex that find can use.
122 test -z "$PRUNEREGEX" &&
123 PRUNEREGEX
=`echo $PRUNEPATHS|sed -e 's,^,\\\(^,' -e 's, ,$\\\)\\\|\\\(^,g' -e 's,$,$\\\),'`
125 # The database file to build.
126 : ${LOCATE_DB=@LOCATE_DB@}
128 # Directory to hold intermediate files.
129 if test -d /var
/tmp
; then
131 elif test -d /usr
/tmp
; then
138 # The user to search network directories as.
141 # The directory containing the subprograms.
142 if test -n "$LIBEXECDIR" ; then
143 : LIBEXECDIR already
set, do nothing
145 : ${LIBEXECDIR=@libexecdir@}
148 # The directory containing find.
149 if test -n "$BINDIR" ; then
150 : BINDIR already
set, do nothing
155 # The names of the utilities to run to build the database.
156 : ${find:=${BINDIR}/@find@}
157 : ${frcode:=${LIBEXECDIR}/@frcode@}
158 : ${bigram:=${LIBEXECDIR}/@bigram@}
159 : ${code:=${LIBEXECDIR}/@code@}
162 PATH
=/bin
:/usr
/bin
:${BINDIR}; export PATH
164 : ${PRUNEFS="nfs NFS proc afs proc smbfs autofs iso9660 ncpfs coda devpts ftpfs devfs mfs sysfs shfs"}
166 if test -n "$PRUNEFS"; then
167 prunefs_exp
=`echo $PRUNEFS |sed -e 's/\([^ ][^ ]*\)/-o -fstype \1/g' \
168 -e 's/-o //' -e 's/$/ -o/'`
173 # Make and code the file list.
174 # Sort case insensitively for users' convenience.
177 trap 'rm -f $LOCATE_DB.n; exit' HUP TERM
179 if test $old = no
; then
181 # FIXME figure out how to sort null-terminated strings, and use -print0.
184 if test -n "$SEARCHPATHS"; then
185 if [ "$LOCALUSER" != "" ]; then
187 su
$LOCALUSER `select_shell $LOCALUSER` -c \
188 "$find $SEARCHPATHS $FINDOPTIONS \
190 -type d -regex '$PRUNEREGEX' \\) -prune -o $print_option"
193 $find $SEARCHPATHS $FINDOPTIONS \
195 -type d
-regex "$PRUNEREGEX" \
) -prune -o $print_option
199 if test -n "$NETPATHS"; then
201 if [ "$myuid" = 0 ]; then
203 su
$NETUSER `select_shell $NETUSER` -c \
204 "$find $NETPATHS $FINDOPTIONS \\( -type d -regex '$PRUNEREGEX' -prune \\) -o $print_option" ||
208 $find $NETPATHS $FINDOPTIONS \
( -type d
-regex "$PRUNEREGEX" -prune \
) -o $print_option ||
212 } |
$sort -f |
$frcode $frcode_options > $LOCATE_DB.n
218 echo "Failed to generate $LOCATE_DB.n" >&2
223 # To avoid breaking locate while this script is running, put the
224 # results in a temp file, then rename it atomically.
225 if test -s $LOCATE_DB.n
; then
227 mv $LOCATE_DB.n
$LOCATE_DB
230 echo "updatedb: new database would be empty" >&2
236 if ! bigrams
=`mktemp -t updatedbXXXXXXXXX`; then
241 if ! filelist
=`mktemp -t updatedbXXXXXXXXX`; then
247 trap 'rm -f $bigrams $filelist $LOCATE_DB.n; exit' HUP TERM
249 # Alphabetize subdirectories before file entries using tr. James Woods says:
250 # "to get everything in monotonic collating sequence, to avoid some
251 # breakage i'll have to think about."
254 if test -n "$SEARCHPATHS"; then
255 if [ "$LOCALUSER" != "" ]; then
257 su
$LOCALUSER `select_shell $LOCALUSER` -c \
258 "$find $SEARCHPATHS $FINDOPTIONS \
260 -type d -regex '$PRUNEREGEX' \) -prune -o $print_option" ||
exit $?
263 $find $SEARCHPATHS $FINDOPTIONS \
265 -type d
-regex "$PRUNEREGEX" \
) -prune -o $print_option ||
exit $?
269 if test -n "$NETPATHS"; then
271 if [ "$myuid" = 0 ]; then
273 su
$NETUSER `select_shell $NETUSER` -c \
274 "$find $NETPATHS $FINDOPTIONS \\( -type d -regex '$PRUNEREGEX' -prune \\) -o $print_option" ||
278 $find $NETPATHS $FINDOPTIONS \
( -type d
-regex "$PRUNEREGEX" -prune \
) -o $print_option ||
282 } |
tr / '\001' |
$sort -f |
tr '\001' / > $filelist
284 # Compute the (at most 128) most common bigrams in the file list.
285 $bigram $bigram_opts < $filelist |
sort |
uniq -c |
sort -nr |
286 awk '{ if (NR <= 128) print $2 }' |
tr -d '\012' > $bigrams
288 # Code the file list.
289 $code $bigrams < $filelist > $LOCATE_DB.n
291 rm -f $bigrams $filelist
293 # To reduce the chances of breaking locate while this script is running,
294 # put the results in a temp file, then rename it atomically.
295 if test -s $LOCATE_DB.n
; then
297 mv $LOCATE_DB.n
$LOCATE_DB
300 echo "updatedb: new database would be empty" >&2