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., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
20 # csh original by James Woods; sh conversion by David MacKenzie.
23 Usage: updatedb [--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>.
35 # If we are unable to fork, the back-tick operator will
36 # fail (and the shell will emit an error message). When
37 # this happens, we exit with error value 71 (EX_OSERR).
38 # Alternative candidate - 75, EX_TEMPFAIL.
39 opt
=`echo $arg|sed 's/^\([^=]*\).*/\1/'` ||
exit 71
40 val
=`echo $arg|sed 's/^[^=]*=\(.*\)/\1/'` ||
exit 71
42 --findoptions) FINDOPTIONS
="$val" ;;
43 --localpaths) SEARCHPATHS
="$val" ;;
44 --netpaths) NETPATHS
="$val" ;;
45 --prunepaths) PRUNEPATHS
="$val" ;;
46 --prunefs) PRUNEFS
="$val" ;;
47 --output) LOCATE_DB
="$val" ;;
48 --netuser) NETUSER
="$val" ;;
49 --localuser) LOCALUSER
="$val" ;;
50 --old-format) old
=yes ;;
51 --changecwd) changeto
="$val" ;;
52 --version) echo "GNU updatedb version @VERSION@"; exit 0 ;;
53 --help) echo "$usage"; exit 0 ;;
54 *) echo "updatedb: invalid option $opt
60 if test "$old" = yes; then
61 echo "Warning: future versions of findutils will shortly discontinue support for the old locate database format." >&2
70 print_option
="-print0"
80 # format of "id" output is ...
81 # uid=1(daemon) gid=1(other)
82 # for `id's that don't understand -u
83 id | cut
-d'(' -f 1 | cut
-d'=' -f2
86 # You can set these in the environment, or use command-line options,
87 # to override their defaults:
89 # Any global options for find?
92 # What shell shoud we use? We should use a POSIX-ish sh.
95 # Non-network directories to put in the database.
98 # Network (NFS, AFS, RFS, etc.) directories to put in the database.
101 # Directories to not put in the database, which would otherwise be.
102 : ${PRUNEPATHS="/tmp /usr/tmp /var/tmp /afs /amd /sfs"}
104 # The same, in the form of a regex that find can use.
105 test -z "$PRUNEREGEX" &&
106 PRUNEREGEX
=`echo $PRUNEPATHS|sed -e 's,^,\\\(^,' -e 's, ,$\\\)\\\|\\\(^,g' -e 's,$,$\\\),'`
108 # The database file to build.
109 : ${LOCATE_DB=@LOCATE_DB@}
111 # Directory to hold intermediate files.
112 if test -d /var
/tmp
; then
114 elif test -d /usr
/tmp
; then
121 # The user to search network directories as.
124 # The directory containing the subprograms.
125 if test -n "$LIBEXECDIR" ; then
126 : LIBEXECDIR already
set, do nothing
128 : ${LIBEXECDIR=@libexecdir@}
131 # The directory containing find.
132 if test -n "$BINDIR" ; then
133 : BINDIR already
set, do nothing
138 # The names of the utilities to run to build the database.
139 : ${find:=${BINDIR}/@find@}
140 : ${frcode:=${LIBEXECDIR}/@frcode@}
141 : ${bigram:=${LIBEXECDIR}/@bigram@}
142 : ${code:=${LIBEXECDIR}/@code@}
145 PATH
=/bin
:/usr
/bin
:${BINDIR}; export PATH
147 : ${PRUNEFS="nfs NFS proc afs proc smbfs autofs iso9660 ncpfs coda devpts ftpfs devfs mfs sysfs shfs"}
149 if test -n "$PRUNEFS"; then
150 prunefs_exp
=`echo $PRUNEFS |sed -e 's/\([^ ][^ ]*\)/-o -fstype \1/g' \
151 -e 's/-o //' -e 's/$/ -o/'`
156 # Make and code the file list.
157 # Sort case insensitively for users' convenience.
160 trap 'rm -f $LOCATE_DB.n; exit' HUP TERM
162 if test $old = no
; then
164 # FIXME figure out how to sort null-terminated strings, and use -print0.
167 if test -n "$SEARCHPATHS"; then
168 if [ "$LOCALUSER" != "" ]; then
170 su
$LOCALUSER -s $SHELL -c \
171 "$find $SEARCHPATHS $FINDOPTIONS \
173 -type d -regex '$PRUNEREGEX' \\) -prune -o $print_option"
176 $find $SEARCHPATHS $FINDOPTIONS \
178 -type d
-regex "$PRUNEREGEX" \
) -prune -o $print_option
182 if test -n "$NETPATHS"; then
184 if [ "$myuid" = 0 ]; then
186 su
$NETUSER -s $SHELL -c \
187 "$find $NETPATHS $FINDOPTIONS \\( -type d -regex '$PRUNEREGEX' -prune \\) -o $print_option" ||
191 $find $NETPATHS $FINDOPTIONS \
( -type d
-regex "$PRUNEREGEX" -prune \
) -o $print_option ||
195 } |
$sort -f |
$frcode $frcode_options > $LOCATE_DB.n
201 echo "Failed to generate $LOCATE_DB.n" >&2
206 # To avoid breaking locate while this script is running, put the
207 # results in a temp file, then rename it atomically.
208 if test -s $LOCATE_DB.n
; then
210 mv $LOCATE_DB.n
$LOCATE_DB
213 echo "updatedb: new database would be empty" >&2
219 if ! bigrams
=`tempfile -p updatedb`; then
224 if ! filelist
=`tempfile -p updatedb`; then
230 trap 'rm -f $bigrams $filelist $LOCATE_DB.n; exit' HUP TERM
232 # Alphabetize subdirectories before file entries using tr. James Woods says:
233 # "to get everything in monotonic collating sequence, to avoid some
234 # breakage i'll have to think about."
237 if test -n "$SEARCHPATHS"; then
238 if [ "$LOCALUSER" != "" ]; then
240 su
$LOCALUSER -s $SHELL -c \
241 "$find $SEARCHPATHS $FINDOPTIONS \
243 -type d -regex '$PRUNEREGEX' \) -prune -o $print_option" ||
exit $?
246 $find $SEARCHPATHS $FINDOPTIONS \
248 -type d
-regex "$PRUNEREGEX" \
) -prune -o $print_option ||
exit $?
252 if test -n "$NETPATHS"; then
254 if [ "$myuid" = 0 ]; then
256 su
$NETUSER -s $SHELL -c \
257 "$find $NETPATHS $FINDOPTIONS \\( -type d -regex '$PRUNEREGEX' -prune \\) -o $print_option" ||
261 $find $NETPATHS $FINDOPTIONS \
( -type d
-regex "$PRUNEREGEX" -prune \
) -o $print_option ||
265 } |
tr / '\001' |
$sort -f |
tr '\001' / > $filelist
267 # Compute the (at most 128) most common bigrams in the file list.
268 $bigram $bigram_opts < $filelist |
sort |
uniq -c |
sort -nr |
269 awk '{ if (NR <= 128) print $2 }' |
tr -d '\012' > $bigrams
271 # Code the file list.
272 $code $bigrams < $filelist > $LOCATE_DB.n
274 rm -f $bigrams $filelist
276 # To reduce the chances of breaking locate while this script is running,
277 # put the results in a temp file, then rename it atomically.
278 if test -s $LOCATE_DB.n
; then
280 mv $LOCATE_DB.n
$LOCATE_DB
283 echo "updatedb: new database would be empty" >&2