* find/find.1: -D option (for Solaris door files) is documented
[findutils.git] / locate / updatedb.sh
blobe708715b7fbd9613e6556f4557a6cc128e83e121
1 #!/bin/sh
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)
8 # any later version.
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.
21 usage="\
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]
27 Report bugs to <bug-findutils@gnu.org>.
30 old=no
31 for arg
33 opt=`echo $arg|sed 's/^\([^=]*\).*/\1/'`
34 val=`echo $arg|sed 's/^[^=]*=\(.*\)/\1/'`
35 case "$opt" in
36 --localpaths) SEARCHPATHS="$val" ;;
37 --netpaths) NETPATHS="$val" ;;
38 --prunepaths) PRUNEPATHS="$val" ;;
39 --prunefs) PRUNEFS="$val" ;;
40 --output) LOCATE_DB="$val" ;;
41 --netuser) NETUSER="$val" ;;
42 --localuser) LOCALUSER="$val" ;;
43 --old-format) old=yes ;;
44 --version) echo "GNU updatedb version @VERSION@"; exit 0 ;;
45 --help) echo "$usage"; exit 0 ;;
46 *) echo "updatedb: invalid option $opt
47 $usage" >&2
48 exit 1 ;;
49 esac
50 done
52 # You can set these in the environment, or use command-line options,
53 # to override their defaults:
55 # Non-network directories to put in the database.
56 : ${SEARCHPATHS="/"}
58 # Network (NFS, AFS, RFS, etc.) directories to put in the database.
59 : ${NETPATHS=}
61 # Directories to not put in the database, which would otherwise be.
62 : ${PRUNEPATHS="/tmp /usr/tmp /var/tmp /afs"}
64 # The same, in the form of a regex that find can use.
65 test -z "$PRUNEREGEX" &&
66 PRUNEREGEX=`echo $PRUNEPATHS|sed -e 's,^,\\\(^,' -e 's, ,$\\\)\\\|\\\(^,g' -e 's,$,$\\\),'`
68 # The database file to build.
69 : ${LOCATE_DB=@LOCATE_DB@}
71 # Directory to hold intermediate files.
72 if test -d /var/tmp; then
73 : ${TMPDIR=/var/tmp}
74 elif test -d /usr/tmp; then
75 : ${TMPDIR=/usr/tmp}
76 else
77 : ${TMPDIR=/tmp}
79 export TMPDIR
81 # The user to search network directories as.
82 : ${NETUSER=daemon}
84 # The directory containing the subprograms.
85 : ${LIBEXECDIR=@libexecdir@}
87 # The directory containing find.
88 : ${BINDIR=@bindir@}
90 # The names of the utilities to run to build the database.
91 : ${find=${BINDIR}/@find@}
92 : ${frcode=${LIBEXECDIR}/@frcode@}
93 : ${bigram=${LIBEXECDIR}/@bigram@}
94 : ${code=${LIBEXECDIR}/@code@}
96 PATH=/bin:/usr/bin:${BINDIR}; export PATH
98 : ${PRUNEFS="nfs NFS proc"}
100 if test -n "$PRUNEFS"; then
101 prunefs_exp=`echo $PRUNEFS |sed -e 's/\([^ ][^ ]*\)/-o -fstype \1/g' \
102 -e 's/-o //' -e 's/$/ -o/'`
103 else
104 prunefs_exp=''
107 # Make and code the file list.
108 # Sort case insensitively for users' convenience.
110 rm -f $LOCATE_DB.n
111 trap 'rm -f $LOCATE_DB.n; exit' 1 15
113 if test $old = no; then
115 # FIXME figure out how to sort null-terminated strings, and use -print0.
117 if test -n "$SEARCHPATHS"; then
118 if [ "$LOCALUSER" != "" ]; then
119 su $LOCALUSER -c \
120 "$find $SEARCHPATHS \
121 \\( $prunefs_exp \
122 -type d -regex '$PRUNEREGEX' \\) -prune -o -print"
123 else
124 $find $SEARCHPATHS \
125 \( $prunefs_exp \
126 -type d -regex "$PRUNEREGEX" \) -prune -o -print
130 if test -n "$NETPATHS"; then
131 if [ "`whoami`" = root ]; then
132 su $NETUSER -c \
133 "$find $NETPATHS \\( -type d -regex '$PRUNEREGEX' -prune \\) -o -print"
134 else
135 $find $NETPATHS \( -type d -regex "$PRUNEREGEX" -prune \) -o -print
138 } | sort -f | $frcode > $LOCATE_DB.n
140 # To avoid breaking locate while this script is running, put the
141 # results in a temp file, then rename it atomically.
142 if test -s $LOCATE_DB.n; then
143 rm -f $LOCATE_DB
144 mv $LOCATE_DB.n $LOCATE_DB
145 chmod 644 $LOCATE_DB
146 else
147 echo "updatedb: new database would be empty" >&2
148 rm -f $LOCATE_DB.n
151 else # old
153 if ! bigrams=`tempfile -p updatedb`; then
154 echo tempfile failed
155 exit 1
158 if ! filelist=`tempfile -p updatedb`; then
159 echo tempfile failed
160 exit 1
163 rm -f $LOCATE_DB.n
164 trap 'rm -f $bigrams $filelist $LOCATE_DB.n; exit' 1 15
166 # Alphabetize subdirectories before file entries using tr. James says:
167 # "to get everything in monotonic collating sequence, to avoid some
168 # breakage i'll have to think about."
170 if test -n "$SEARCHPATHS"; then
171 if [ "$LOCALUSER" != "" ]; then
172 su $LOCALUSER -c \
173 "$find $SEARCHPATHS \
174 \( $prunefs_exp \
175 -type d -regex '$PRUNEREGEX' \) -prune -o -print"
176 else
177 $find $SEARCHPATHS \
178 \( $prunefs_exp \
179 -type d -regex "$PRUNEREGEX" \) -prune -o -print
183 if test -n "$NETPATHS"; then
184 if [ "`whoami`" = root ]; then
185 su $NETUSER -c \
186 "$find $NETPATHS \\( -type d -regex '$PRUNEREGEX' -prune \\) -o -print"
187 else
188 $find $NETPATHS \( -type d -regex "$PRUNEREGEX" -prune \) -o -print
191 } | tr / '\001' | sort -f | tr '\001' / > $filelist
193 # Compute the (at most 128) most common bigrams in the file list.
194 $bigram < $filelist | sort | uniq -c | sort -nr |
195 awk '{ if (NR <= 128) print $2 }' | tr -d '\012' > $bigrams
197 # Code the file list.
198 $code $bigrams < $filelist > $LOCATE_DB.n
200 rm -f $bigrams $filelist
202 # To reduce the chances of breaking locate while this script is running,
203 # put the results in a temp file, then rename it atomically.
204 if test -s $LOCATE_DB.n; then
205 rm -f $LOCATE_DB
206 mv $LOCATE_DB.n $LOCATE_DB
207 chmod 644 $LOCATE_DB
208 else
209 echo "updatedb: new database would be empty" >&2
210 rm -f $LOCATE_DB.n
215 exit 0