Enable the 'Warning: filesystem XXX has recently been mounted' check on Solaris,...
[findutils.git] / locate / updatedb.sh
blob7af9d8808364ff8a80c316b4830f5d294e466951
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
18 # USA.
20 # csh original by James Woods; sh conversion by David MacKenzie.
22 usage="\
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>.
31 changeto=/
32 old=no
33 for arg
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
41 case "$opt" in
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
55 $usage" >&2
56 exit 1 ;;
57 esac
58 done
60 if test "$old" = yes; then
61 echo "Warning: future versions of findutils will shortly discontinue support for the old locate database format." >&2
63 sort="@SORT@"
64 print_option="-print"
65 frcode_options=""
66 else
67 if @SORT_SUPPORTS_Z@
68 then
69 sort="@SORT@ -z"
70 print_option="-print0"
71 frcode_options="-0"
72 else
73 sort="@SORT@"
74 print_option="-print"
75 frcode_options=""
79 getuid() {
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?
90 : ${FINDOPTIONS=}
92 # What shell shoud we use? We should use a POSIX-ish sh.
93 : ${SHELL="/bin/sh"}
95 # Non-network directories to put in the database.
96 : ${SEARCHPATHS="/"}
98 # Network (NFS, AFS, RFS, etc.) directories to put in the database.
99 : ${NETPATHS=}
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
113 : ${TMPDIR=/var/tmp}
114 elif test -d /usr/tmp; then
115 : ${TMPDIR=/usr/tmp}
116 else
117 : ${TMPDIR=/tmp}
119 export TMPDIR
121 # The user to search network directories as.
122 : ${NETUSER=daemon}
124 # The directory containing the subprograms.
125 if test -n "$LIBEXECDIR" ; then
126 : LIBEXECDIR already set, do nothing
127 else
128 : ${LIBEXECDIR=@libexecdir@}
131 # The directory containing find.
132 if test -n "$BINDIR" ; then
133 : BINDIR already set, do nothing
134 else
135 : ${BINDIR=@bindir@}
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/'`
152 else
153 prunefs_exp=''
156 # Make and code the file list.
157 # Sort case insensitively for users' convenience.
159 rm -f $LOCATE_DB.n
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.
165 if {
166 cd "$changeto"
167 if test -n "$SEARCHPATHS"; then
168 if [ "$LOCALUSER" != "" ]; then
169 # : A1
170 su $LOCALUSER -s $SHELL -c \
171 "$find $SEARCHPATHS $FINDOPTIONS \
172 \\( $prunefs_exp \
173 -type d -regex '$PRUNEREGEX' \\) -prune -o $print_option"
174 else
175 # : A2
176 $find $SEARCHPATHS $FINDOPTIONS \
177 \( $prunefs_exp \
178 -type d -regex "$PRUNEREGEX" \) -prune -o $print_option
182 if test -n "$NETPATHS"; then
183 myuid=`getuid`
184 if [ "$myuid" = 0 ]; then
185 # : A3
186 su $NETUSER -s $SHELL -c \
187 "$find $NETPATHS $FINDOPTIONS \\( -type d -regex '$PRUNEREGEX' -prune \\) -o $print_option" ||
188 exit $?
189 else
190 # : A4
191 $find $NETPATHS $FINDOPTIONS \( -type d -regex "$PRUNEREGEX" -prune \) -o $print_option ||
192 exit $?
195 } | $sort -f | $frcode $frcode_options > $LOCATE_DB.n
196 then
197 # OK so far
198 true
199 else
200 rv=$?
201 echo "Failed to generate $LOCATE_DB.n" >&2
202 rm -f $LOCATE_DB.n
203 exit $rv
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
209 rm -f $LOCATE_DB
210 mv $LOCATE_DB.n $LOCATE_DB
211 chmod 644 $LOCATE_DB
212 else
213 echo "updatedb: new database would be empty" >&2
214 rm -f $LOCATE_DB.n
217 else # old
219 if ! bigrams=`tempfile -p updatedb`; then
220 echo tempfile failed
221 exit 1
224 if ! filelist=`tempfile -p updatedb`; then
225 echo tempfile failed
226 exit 1
229 rm -f $LOCATE_DB.n
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."
236 cd "$changeto"
237 if test -n "$SEARCHPATHS"; then
238 if [ "$LOCALUSER" != "" ]; then
239 # : A5
240 su $LOCALUSER -s $SHELL -c \
241 "$find $SEARCHPATHS $FINDOPTIONS \
242 \( $prunefs_exp \
243 -type d -regex '$PRUNEREGEX' \) -prune -o $print_option" || exit $?
244 else
245 # : A6
246 $find $SEARCHPATHS $FINDOPTIONS \
247 \( $prunefs_exp \
248 -type d -regex "$PRUNEREGEX" \) -prune -o $print_option || exit $?
252 if test -n "$NETPATHS"; then
253 myuid=`getuid`
254 if [ "$myuid" = 0 ]; then
255 # : A7
256 su $NETUSER -s $SHELL -c \
257 "$find $NETPATHS $FINDOPTIONS \\( -type d -regex '$PRUNEREGEX' -prune \\) -o $print_option" ||
258 exit $?
259 else
260 # : A8
261 $find $NETPATHS $FINDOPTIONS \( -type d -regex "$PRUNEREGEX" -prune \) -o $print_option ||
262 exit $?
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
279 rm -f $LOCATE_DB
280 mv $LOCATE_DB.n $LOCATE_DB
281 chmod 644 $LOCATE_DB
282 else
283 echo "updatedb: new database would be empty" >&2
284 rm -f $LOCATE_DB.n
289 exit 0