Define the m4 macro MKINSTALLDIRS ourselves, because po/Makefile.in.in needs it but...
[findutils.git] / locate / updatedb.sh
blob4392c92326d6f98891e2f5b2958efdb913a5a29d
1 #! /bin/sh
2 # updatedb -- build a locate pathname database
3 # Copyright (C) 1994, 1996, 1997, 2000, 2001, 2003, 2004, 2005, 2006
4 # Free Software Foundation, Inc.
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2, or (at your option)
9 # any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
19 # USA.
21 # csh original by James Woods; sh conversion by David MacKenzie.
23 #exec 2> /tmp/updatedb-trace.txt
24 #set -x
26 usage="\
27 Usage: $0 [--findoptions='-option1 -option2...']
28 [--localpaths='dir1 dir2...'] [--netpaths='dir1 dir2...']
29 [--prunepaths='dir1 dir2...'] [--prunefs='fs1 fs2...']
30 [--output=dbfile] [--netuser=user] [--localuser=user]
31 [--old-format] [--version] [--help]
33 Report bugs to <bug-findutils@gnu.org>."
34 changeto=/
35 old=no
36 for arg
38 # If we are unable to fork, the back-tick operator will
39 # fail (and the shell will emit an error message). When
40 # this happens, we exit with error value 71 (EX_OSERR).
41 # Alternative candidate - 75, EX_TEMPFAIL.
42 opt=`echo $arg|sed 's/^\([^=]*\).*/\1/'` || exit 71
43 val=`echo $arg|sed 's/^[^=]*=\(.*\)/\1/'` || exit 71
44 case "$opt" in
45 --findoptions) FINDOPTIONS="$val" ;;
46 --localpaths) SEARCHPATHS="$val" ;;
47 --netpaths) NETPATHS="$val" ;;
48 --prunepaths) PRUNEPATHS="$val" ;;
49 --prunefs) PRUNEFS="$val" ;;
50 --output) LOCATE_DB="$val" ;;
51 --netuser) NETUSER="$val" ;;
52 --localuser) LOCALUSER="$val" ;;
53 --old-format) old=yes ;;
54 --changecwd) changeto="$val" ;;
55 --version) echo "GNU updatedb version @VERSION@"; exit 0 ;;
56 --help) echo "$usage"; exit 0 ;;
57 *) echo "updatedb: invalid option $opt
58 $usage" >&2
59 exit 1 ;;
60 esac
61 done
63 if test "$old" = yes; then
64 echo "Warning: future versions of findutils will shortly discontinue support for the old locate database format." >&2
66 sort="@SORT@"
67 print_option="-print"
68 frcode_options=""
69 else
70 if @SORT_SUPPORTS_Z@
71 then
72 sort="@SORT@ -z"
73 print_option="-print0"
74 frcode_options="-0"
75 else
76 sort="@SORT@"
77 print_option="-print"
78 frcode_options=""
82 getuid() {
83 # format of "id" output is ...
84 # uid=1(daemon) gid=1(other)
85 # for `id's that don't understand -u
86 id | cut -d'(' -f 1 | cut -d'=' -f2
89 # figure out if su supports the -s option
90 select_shell() {
91 if su "$1" -s $SHELL -c false < /dev/null ; then
92 # No.
93 echo ""
94 else
95 if su "$1" -s $SHELL -c true < /dev/null ; then
96 # Yes.
97 echo "-s $SHELL"
98 else
99 # su is unconditionally failing. We won't be able to
100 # figure out what is wrong, so be conservative.
101 echo ""
107 # You can set these in the environment, or use command-line options,
108 # to override their defaults:
110 # Any global options for find?
111 : ${FINDOPTIONS=}
113 # What shell shoud we use? We should use a POSIX-ish sh.
114 : ${SHELL="/bin/sh"}
116 # Non-network directories to put in the database.
117 : ${SEARCHPATHS="/"}
119 # Network (NFS, AFS, RFS, etc.) directories to put in the database.
120 : ${NETPATHS=}
122 # Directories to not put in the database, which would otherwise be.
123 : ${PRUNEPATHS="/tmp /usr/tmp /var/tmp /afs /amd /sfs"}
125 # Trailing slashes result in regex items that are never matched, which
126 # is not what the user will expect. Therefore we now reject such
127 # constructs.
128 for p in $PRUNEPATHS; do
129 case "$p" in
130 /*/) echo "$0: $p: pruned paths should not contain trailing slashes" >&2
131 exit 1
132 esac
133 done
135 # The same, in the form of a regex that find can use.
136 test -z "$PRUNEREGEX" &&
137 PRUNEREGEX=`echo $PRUNEPATHS|sed -e 's,^,\\\(^,' -e 's, ,$\\\)\\\|\\\(^,g' -e 's,$,$\\\),'`
139 # The database file to build.
140 : ${LOCATE_DB=@LOCATE_DB@}
142 # Directory to hold intermediate files.
143 if test -d /var/tmp; then
144 : ${TMPDIR=/var/tmp}
145 elif test -d /usr/tmp; then
146 : ${TMPDIR=/usr/tmp}
147 else
148 : ${TMPDIR=/tmp}
150 export TMPDIR
152 # The user to search network directories as.
153 : ${NETUSER=daemon}
155 # The directory containing the subprograms.
156 if test -n "$LIBEXECDIR" ; then
157 : LIBEXECDIR already set, do nothing
158 else
159 : ${LIBEXECDIR=@libexecdir@}
162 # The directory containing find.
163 if test -n "$BINDIR" ; then
164 : BINDIR already set, do nothing
165 else
166 : ${BINDIR=@bindir@}
169 # The names of the utilities to run to build the database.
170 : ${find:=${BINDIR}/@find@}
171 : ${frcode:=${LIBEXECDIR}/@frcode@}
172 : ${bigram:=${LIBEXECDIR}/@bigram@}
173 : ${code:=${LIBEXECDIR}/@code@}
176 checkbinary () {
177 if test -x "$1" ; then
178 : ok
179 else
180 eval echo "updatedb needs to be able to execute $1, but cannot." >&2
181 exit 1
185 for binary in $find $frcode $bigram $code
187 checkbinary $binary
188 done
191 PATH=/bin:/usr/bin:${BINDIR}; export PATH
193 : ${PRUNEFS="nfs NFS proc afs proc smbfs autofs iso9660 ncpfs coda devpts ftpfs devfs mfs sysfs shfs"}
195 if test -n "$PRUNEFS"; then
196 prunefs_exp=`echo $PRUNEFS |sed -e 's/\([^ ][^ ]*\)/-o -fstype \1/g' \
197 -e 's/-o //' -e 's/$/ -o/'`
198 else
199 prunefs_exp=''
202 # Make and code the file list.
203 # Sort case insensitively for users' convenience.
205 rm -f $LOCATE_DB.n
206 trap 'rm -f $LOCATE_DB.n; exit' HUP TERM
208 if test $old = no; then
210 # FIXME figure out how to sort null-terminated strings, and use -print0.
211 if {
212 cd "$changeto"
213 if test -n "$SEARCHPATHS"; then
214 if [ "$LOCALUSER" != "" ]; then
215 # : A1
216 su $LOCALUSER `select_shell $LOCALUSER` -c \
217 "$find $SEARCHPATHS $FINDOPTIONS \
218 \\( $prunefs_exp \
219 -type d -regex '$PRUNEREGEX' \\) -prune -o $print_option"
220 else
221 # : A2
222 $find $SEARCHPATHS $FINDOPTIONS \
223 \( $prunefs_exp \
224 -type d -regex "$PRUNEREGEX" \) -prune -o $print_option
228 if test -n "$NETPATHS"; then
229 myuid=`getuid`
230 if [ "$myuid" = 0 ]; then
231 # : A3
232 su $NETUSER `select_shell $NETUSER` -c \
233 "$find $NETPATHS $FINDOPTIONS \\( -type d -regex '$PRUNEREGEX' -prune \\) -o $print_option" ||
234 exit $?
235 else
236 # : A4
237 $find $NETPATHS $FINDOPTIONS \( -type d -regex "$PRUNEREGEX" -prune \) -o $print_option ||
238 exit $?
241 } | $sort -f | $frcode $frcode_options > $LOCATE_DB.n
242 then
243 : OK so far
244 true
245 else
246 rv=$?
247 echo "Failed to generate $LOCATE_DB.n" >&2
248 rm -f $LOCATE_DB.n
249 exit $rv
252 # To avoid breaking locate while this script is running, put the
253 # results in a temp file, then rename it atomically.
254 if test -s $LOCATE_DB.n; then
255 rm -f $LOCATE_DB
256 mv $LOCATE_DB.n $LOCATE_DB
257 chmod 644 $LOCATE_DB
258 else
259 echo "updatedb: new database would be empty" >&2
260 rm -f $LOCATE_DB.n
263 else # old
265 if ! bigrams=`mktemp -t updatedbXXXXXXXXX`; then
266 echo tempfile failed
267 exit 1
270 if ! filelist=`mktemp -t updatedbXXXXXXXXX`; then
271 echo tempfile failed
272 exit 1
275 rm -f $LOCATE_DB.n
276 trap 'rm -f $bigrams $filelist $LOCATE_DB.n; exit' HUP TERM
278 # Alphabetize subdirectories before file entries using tr. James Woods says:
279 # "to get everything in monotonic collating sequence, to avoid some
280 # breakage i'll have to think about."
282 cd "$changeto"
283 if test -n "$SEARCHPATHS"; then
284 if [ "$LOCALUSER" != "" ]; then
285 # : A5
286 su $LOCALUSER `select_shell $LOCALUSER` -c \
287 "$find $SEARCHPATHS $FINDOPTIONS \
288 \( $prunefs_exp \
289 -type d -regex '$PRUNEREGEX' \) -prune -o $print_option" || exit $?
290 else
291 # : A6
292 $find $SEARCHPATHS $FINDOPTIONS \
293 \( $prunefs_exp \
294 -type d -regex "$PRUNEREGEX" \) -prune -o $print_option || exit $?
298 if test -n "$NETPATHS"; then
299 myuid=`getuid`
300 if [ "$myuid" = 0 ]; then
301 # : A7
302 su $NETUSER `select_shell $NETUSER` -c \
303 "$find $NETPATHS $FINDOPTIONS \\( -type d -regex '$PRUNEREGEX' -prune \\) -o $print_option" ||
304 exit $?
305 else
306 # : A8
307 $find $NETPATHS $FINDOPTIONS \( -type d -regex "$PRUNEREGEX" -prune \) -o $print_option ||
308 exit $?
311 } | tr / '\001' | $sort -f | tr '\001' / > $filelist
313 # Compute the (at most 128) most common bigrams in the file list.
314 $bigram $bigram_opts < $filelist | sort | uniq -c | sort -nr |
315 awk '{ if (NR <= 128) print $2 }' | tr -d '\012' > $bigrams
317 # Code the file list.
318 $code $bigrams < $filelist > $LOCATE_DB.n
320 rm -f $bigrams $filelist
322 # To reduce the chances of breaking locate while this script is running,
323 # put the results in a temp file, then rename it atomically.
324 if test -s $LOCATE_DB.n; then
325 rm -f $LOCATE_DB
326 mv $LOCATE_DB.n $LOCATE_DB
327 chmod 644 $LOCATE_DB
328 else
329 echo "updatedb: new database would be empty" >&2
330 rm -f $LOCATE_DB.n
335 exit 0