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., 675 Mass Ave, Cambridge, MA 02139, USA.
19 # csh original by James Woods; sh conversion by David MacKenzie.
22 Usage: updatedb [--localpaths='dir1 dir2...'] [--netpaths='dir1 dir2...']
23 [--prunepaths='dir1 dir2...'] [--prunefs='fs1 fs2...']
24 [--output=dbfile] [--netuser=user] [--localuser=user]
25 [--old-format] [--version] [--help]"
30 opt
=`echo $arg|sed 's/^\([^=]*\).*/\1/'`
31 val
=`echo $arg|sed 's/^[^=]*=\(.*\)/\1/'`
33 --localpaths) SEARCHPATHS
="$val" ;;
34 --netpaths) NETPATHS
="$val" ;;
35 --prunepaths) PRUNEPATHS
="$val" ;;
36 --prunefs) PRUNEFS
="$val" ;;
37 --output) LOCATE_DB
="$val" ;;
38 --netuser) NETUSER
="$val" ;;
39 --localuser) LOCALUSER
="$val" ;;
40 --old-format) old
=yes ;;
41 --version) echo "GNU updatedb version @VERSION@"; exit 0 ;;
42 --help) echo "$usage"; exit 0 ;;
43 *) echo "updatedb: invalid option $opt
49 # You can set these in the environment, or use command-line options,
50 # to override their defaults:
52 # Non-network directories to put in the database.
55 # Network (NFS, AFS, RFS, etc.) directories to put in the database.
58 # Directories to not put in the database, which would otherwise be.
59 : ${PRUNEPATHS="/tmp /usr/tmp /var/tmp /afs"}
61 # The same, in the form of a regex that find can use.
62 test -z "$PRUNEREGEX" &&
63 PRUNEREGEX
=`echo $PRUNEPATHS|sed -e 's,^,\\\(^,' -e 's, ,$\\\)\\\|\\\(^,g' -e 's,$,$\\\),'`
65 # The database file to build.
66 : ${LOCATE_DB=@LOCATE_DB@}
68 # Directory to hold intermediate files.
69 if test -d /var
/tmp
; then
71 elif test -d /usr
/tmp
; then
78 # The user to search network directories as.
81 # The directory containing the subprograms.
82 : ${LIBEXECDIR=@libexecdir@}
84 # The directory containing find.
87 # The names of the utilities to run to build the database.
88 : ${find=${BINDIR}/@find@}
89 : ${frcode=${LIBEXECDIR}/@frcode@}
90 : ${bigram=${LIBEXECDIR}/@bigram@}
91 : ${code=${LIBEXECDIR}/@code@}
93 PATH
=/bin
:/usr
/bin
:${BINDIR}; export PATH
95 : ${PRUNEFS="nfs NFS proc"}
97 if test -n "$PRUNEFS"; then
98 prunefs_exp
=`echo $PRUNEFS |sed -e 's/\([^ ][^ ]*\)/-o -fstype \1/g' \
99 -e 's/-o //' -e 's/$/ -o/'`
104 # Make and code the file list.
105 # Sort case insensitively for users' convenience.
108 trap 'rm -f $LOCATE_DB.n; exit' 1 15
110 if test $old = no
; then
112 # FIXME figure out how to sort null-terminated strings, and use -print0.
114 if test -n "$SEARCHPATHS"; then
115 if [ "$LOCALUSER" != "" ]; then
117 "$find $SEARCHPATHS \
119 -type d -regex '$PRUNEREGEX' \\) -prune -o -print"
123 -type d
-regex "$PRUNEREGEX" \
) -prune -o -print
127 if test -n "$NETPATHS"; then
128 if [ "`whoami`" = root
]; then
130 "$find $NETPATHS \\( -type d -regex '$PRUNEREGEX' -prune \\) -o -print"
132 $find $NETPATHS \
( -type d
-regex "$PRUNEREGEX" -prune \
) -o -print
135 } |
sort -f |
$frcode > $LOCATE_DB.n
137 # To avoid breaking locate while this script is running, put the
138 # results in a temp file, then rename it atomically.
139 if test -s $LOCATE_DB.n
; then
141 mv $LOCATE_DB.n
$LOCATE_DB
144 echo "updatedb: new database would be empty" >&2
150 if ! bigrams
=`tempfile -p updatedb`; then
155 if ! filelist
=`tempfile -p updatedb`; then
161 trap 'rm -f $bigrams $filelist $LOCATE_DB.n; exit' 1 15
163 # Alphabetize subdirectories before file entries using tr. James says:
164 # "to get everything in monotonic collating sequence, to avoid some
165 # breakage i'll have to think about."
167 if test -n "$SEARCHPATHS"; then
168 if [ "$LOCALUSER" != "" ]; then
170 "$find $SEARCHPATHS \
172 -type d -regex '$PRUNEREGEX' \) -prune -o -print"
176 -type d
-regex "$PRUNEREGEX" \
) -prune -o -print
180 if test -n "$NETPATHS"; then
181 if [ "`whoami`" = root
]; then
183 "$find $NETPATHS \\( -type d -regex '$PRUNEREGEX' -prune \\) -o -print"
185 $find $NETPATHS \
( -type d
-regex "$PRUNEREGEX" -prune \
) -o -print
188 } |
tr / '\001' |
sort -f |
tr '\001' / > $filelist
190 # Compute the (at most 128) most common bigrams in the file list.
191 $bigram < $filelist |
sort |
uniq -c |
sort -nr |
192 awk '{ if (NR <= 128) print $2 }' |
tr -d '\012' > $bigrams
194 # Code the file list.
195 $code $bigrams < $filelist > $LOCATE_DB.n
197 rm -f $bigrams $filelist
199 # To reduce the chances of breaking locate while this script is running,
200 # put the results in a temp file, then rename it atomically.
201 if test -s $LOCATE_DB.n
; then
203 mv $LOCATE_DB.n
$LOCATE_DB
206 echo "updatedb: new database would be empty" >&2