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., 9 Temple Place - Suite 330, Boston, MA 02111-1307,
20 # csh original by James Woods; sh conversion by David MacKenzie.
23 Usage: updatedb [--localpaths='dir1 dir2...'] [--netpaths='dir1 dir2...']
24 [--prunepaths='dir1 dir2...'] [--prunefs='fs1 fs2...']
25 [--output=dbfile] [--netuser=user] [--localuser=user]
26 [--old-format] [--version] [--help]
28 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 --localpaths) SEARCHPATHS
="$val" ;;
42 --netpaths) NETPATHS
="$val" ;;
43 --prunepaths) PRUNEPATHS
="$val" ;;
44 --prunefs) PRUNEFS
="$val" ;;
45 --output) LOCATE_DB
="$val" ;;
46 --netuser) NETUSER
="$val" ;;
47 --localuser) LOCALUSER
="$val" ;;
48 --old-format) old
=yes ;;
49 --changecwd) changeto
="$val" ;;
50 --version) echo "GNU updatedb version @VERSION@"; exit 0 ;;
51 --help) echo "$usage"; exit 0 ;;
52 *) echo "updatedb: invalid option $opt
59 # format of "id" output is ...
60 # uid=1(daemon) gid=1(other)
61 # for `id's that don't understand -u
62 id | cut
-d'(' -f 1 | cut
-d'=' -f2
65 # You can set these in the environment, or use command-line options,
66 # to override their defaults:
68 # Non-network directories to put in the database.
71 # Network (NFS, AFS, RFS, etc.) directories to put in the database.
74 # Directories to not put in the database, which would otherwise be.
75 : ${PRUNEPATHS="/tmp /usr/tmp /var/tmp /afs"}
77 # The same, in the form of a regex that find can use.
78 test -z "$PRUNEREGEX" &&
79 PRUNEREGEX
=`echo $PRUNEPATHS|sed -e 's,^,\\\(^,' -e 's, ,$\\\)\\\|\\\(^,g' -e 's,$,$\\\),'`
81 # The database file to build.
82 : ${LOCATE_DB=@LOCATE_DB@}
84 # Directory to hold intermediate files.
85 if test -d /var
/tmp
; then
87 elif test -d /usr
/tmp
; then
94 # The user to search network directories as.
97 # The directory containing the subprograms.
98 if test -n "$LIBEXECDIR" ; then
99 : LIBEXECDIR already
set, do nothing
101 : ${LIBEXECDIR=@libexecdir@}
104 # The directory containing find.
105 if test -n "$BINDIR" ; then
106 : BINDIR already
set, do nothing
111 # The names of the utilities to run to build the database.
112 : ${find:=${BINDIR}/@find@}
113 : ${frcode:=${LIBEXECDIR}/@frcode@}
114 : ${bigram:=${LIBEXECDIR}/@bigram@}
115 : ${code:=${LIBEXECDIR}/@code@}
117 PATH
=/bin
:/usr
/bin
:${BINDIR}; export PATH
119 : ${PRUNEFS="nfs NFS proc"}
121 if test -n "$PRUNEFS"; then
122 prunefs_exp
=`echo $PRUNEFS |sed -e 's/\([^ ][^ ]*\)/-o -fstype \1/g' \
123 -e 's/-o //' -e 's/$/ -o/'`
128 # Make and code the file list.
129 # Sort case insensitively for users' convenience.
132 trap 'rm -f $LOCATE_DB.n; exit' 1 15
134 if test $old = no
; then
136 # FIXME figure out how to sort null-terminated strings, and use -print0.
139 if test -n "$SEARCHPATHS"; then
140 if [ "$LOCALUSER" != "" ]; then
143 "$find $SEARCHPATHS \
145 -type d -regex '$PRUNEREGEX' \\) -prune -o -print"
150 -type d
-regex "$PRUNEREGEX" \
) -prune -o -print
154 if test -n "$NETPATHS"; then
156 if [ "$myuid" = 0 ]; then
159 "$find $NETPATHS \\( -type d -regex '$PRUNEREGEX' -prune \\) -o -print" ||
163 $find $NETPATHS \
( -type d
-regex "$PRUNEREGEX" -prune \
) -o -print ||
167 } |
sort -f |
$frcode > $LOCATE_DB.n
173 echo "Failed to generate $LOCATE_DB.n" >&2
178 # To avoid breaking locate while this script is running, put the
179 # results in a temp file, then rename it atomically.
180 if test -s $LOCATE_DB.n
; then
182 mv $LOCATE_DB.n
$LOCATE_DB
185 echo "updatedb: new database would be empty" >&2
191 if ! bigrams
=`tempfile -p updatedb`; then
196 if ! filelist
=`tempfile -p updatedb`; then
202 trap 'rm -f $bigrams $filelist $LOCATE_DB.n; exit' 1 15
204 # Alphabetize subdirectories before file entries using tr. James says:
205 # "to get everything in monotonic collating sequence, to avoid some
206 # breakage i'll have to think about."
209 if test -n "$SEARCHPATHS"; then
210 if [ "$LOCALUSER" != "" ]; then
213 "$find $SEARCHPATHS \
215 -type d -regex '$PRUNEREGEX' \) -prune -o -print" ||
exit $?
220 -type d
-regex "$PRUNEREGEX" \
) -prune -o -print ||
exit $?
224 if test -n "$NETPATHS"; then
226 if [ "$myuid" = 0 ]; then
229 "$find $NETPATHS \\( -type d -regex '$PRUNEREGEX' -prune \\) -o -print" ||
233 $find $NETPATHS \
( -type d
-regex "$PRUNEREGEX" -prune \
) -o -print ||
237 } |
tr / '\001' |
sort -f |
tr '\001' / > $filelist
239 # Compute the (at most 128) most common bigrams in the file list.
240 $bigram < $filelist |
sort |
uniq -c |
sort -nr |
241 awk '{ if (NR <= 128) print $2 }' |
tr -d '\012' > $bigrams
243 # Code the file list.
244 $code $bigrams < $filelist > $LOCATE_DB.n
246 rm -f $bigrams $filelist
248 # To reduce the chances of breaking locate while this script is running,
249 # put the results in a temp file, then rename it atomically.
250 if test -s $LOCATE_DB.n
; then
252 mv $LOCATE_DB.n
$LOCATE_DB
255 echo "updatedb: new database would be empty" >&2