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 opt
=`echo $arg|sed 's/^\([^=]*\).*/\1/'`
35 val
=`echo $arg|sed 's/^[^=]*=\(.*\)/\1/'`
37 --localpaths) SEARCHPATHS
="$val" ;;
38 --netpaths) NETPATHS
="$val" ;;
39 --prunepaths) PRUNEPATHS
="$val" ;;
40 --prunefs) PRUNEFS
="$val" ;;
41 --output) LOCATE_DB
="$val" ;;
42 --netuser) NETUSER
="$val" ;;
43 --localuser) LOCALUSER
="$val" ;;
44 --old-format) old
=yes ;;
45 --version) echo "GNU updatedb version @VERSION@"; exit 0 ;;
46 --help) echo "$usage"; exit 0 ;;
47 *) echo "updatedb: invalid option $opt
54 # format of "id" output is ...
55 # uid=1(daemon) gid=1(other)
56 # for `id's that don't understand -u
57 id | cut
-d'(' -f 1 | cut
-d'=' -f2
60 # You can set these in the environment, or use command-line options,
61 # to override their defaults:
63 # Non-network directories to put in the database.
66 # Network (NFS, AFS, RFS, etc.) directories to put in the database.
69 # Directories to not put in the database, which would otherwise be.
70 : ${PRUNEPATHS="/tmp /usr/tmp /var/tmp /afs"}
72 # The same, in the form of a regex that find can use.
73 test -z "$PRUNEREGEX" &&
74 PRUNEREGEX
=`echo $PRUNEPATHS|sed -e 's,^,\\\(^,' -e 's, ,$\\\)\\\|\\\(^,g' -e 's,$,$\\\),'`
76 # The database file to build.
77 : ${LOCATE_DB=@LOCATE_DB@}
79 # Directory to hold intermediate files.
80 if test -d /var
/tmp
; then
82 elif test -d /usr
/tmp
; then
89 # The user to search network directories as.
92 # The directory containing the subprograms.
93 : ${LIBEXECDIR=@libexecdir@}
95 # The directory containing find.
98 # The names of the utilities to run to build the database.
99 : ${find=${BINDIR}/@find@}
100 : ${frcode=${LIBEXECDIR}/@frcode@}
101 : ${bigram=${LIBEXECDIR}/@bigram@}
102 : ${code=${LIBEXECDIR}/@code@}
104 PATH
=/bin
:/usr
/bin
:${BINDIR}; export PATH
106 : ${PRUNEFS="nfs NFS proc"}
108 if test -n "$PRUNEFS"; then
109 prunefs_exp
=`echo $PRUNEFS |sed -e 's/\([^ ][^ ]*\)/-o -fstype \1/g' \
110 -e 's/-o //' -e 's/$/ -o/'`
115 # Make and code the file list.
116 # Sort case insensitively for users' convenience.
119 trap 'rm -f $LOCATE_DB.n; exit' 1 15
121 if test $old = no
; then
123 # FIXME figure out how to sort null-terminated strings, and use -print0.
125 if test -n "$SEARCHPATHS"; then
126 if [ "$LOCALUSER" != "" ]; then
128 "$find $SEARCHPATHS \
130 -type d -regex '$PRUNEREGEX' \\) -prune -o -print"
134 -type d
-regex "$PRUNEREGEX" \
) -prune -o -print
138 if test -n "$NETPATHS"; then
140 if [ "$myuid" = 0 ]; then
142 "$find $NETPATHS \\( -type d -regex '$PRUNEREGEX' -prune \\) -o -print"
144 $find $NETPATHS \
( -type d
-regex "$PRUNEREGEX" -prune \
) -o -print
147 } |
sort -f |
$frcode > $LOCATE_DB.n
149 # To avoid breaking locate while this script is running, put the
150 # results in a temp file, then rename it atomically.
151 if test -s $LOCATE_DB.n
; then
153 mv $LOCATE_DB.n
$LOCATE_DB
156 echo "updatedb: new database would be empty" >&2
162 if ! bigrams
=`tempfile -p updatedb`; then
167 if ! filelist
=`tempfile -p updatedb`; then
173 trap 'rm -f $bigrams $filelist $LOCATE_DB.n; exit' 1 15
175 # Alphabetize subdirectories before file entries using tr. James says:
176 # "to get everything in monotonic collating sequence, to avoid some
177 # breakage i'll have to think about."
179 if test -n "$SEARCHPATHS"; then
180 if [ "$LOCALUSER" != "" ]; then
182 "$find $SEARCHPATHS \
184 -type d -regex '$PRUNEREGEX' \) -prune -o -print"
188 -type d
-regex "$PRUNEREGEX" \
) -prune -o -print
192 if test -n "$NETPATHS"; then
194 if [ "$myuid" = 0 ]; then
196 "$find $NETPATHS \\( -type d -regex '$PRUNEREGEX' -prune \\) -o -print"
198 $find $NETPATHS \
( -type d
-regex "$PRUNEREGEX" -prune \
) -o -print
201 } |
tr / '\001' |
sort -f |
tr '\001' / > $filelist
203 # Compute the (at most 128) most common bigrams in the file list.
204 $bigram < $filelist |
sort |
uniq -c |
sort -nr |
205 awk '{ if (NR <= 128) print $2 }' |
tr -d '\012' > $bigrams
207 # Code the file list.
208 $code $bigrams < $filelist > $LOCATE_DB.n
210 rm -f $bigrams $filelist
212 # To reduce the chances of breaking locate while this script is running,
213 # put the results in a temp file, then rename it atomically.
214 if test -s $LOCATE_DB.n
; then
216 mv $LOCATE_DB.n
$LOCATE_DB
219 echo "updatedb: new database would be empty" >&2