Added new test case for SourceForge bug #14070
[findutils.git] / locate / updatedb.sh
blob2114841efb5ca5a95ec9e9d8dfbf6869a3b1c67e
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
18 # USA.
20 # csh original by James Woods; sh conversion by David MacKenzie.
22 usage="\
23 Usage: $0 [--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>."
30 changeto=/
31 old=no
32 for arg
34 # If we are unable to fork, the back-tick operator will
35 # fail (and the shell will emit an error message). When
36 # this happens, we exit with error value 71 (EX_OSERR).
37 # Alternative candidate - 75, EX_TEMPFAIL.
38 opt=`echo $arg|sed 's/^\([^=]*\).*/\1/'` || exit 71
39 val=`echo $arg|sed 's/^[^=]*=\(.*\)/\1/'` || exit 71
40 case "$opt" in
41 --findoptions) FINDOPTIONS="$val" ;;
42 --localpaths) SEARCHPATHS="$val" ;;
43 --netpaths) NETPATHS="$val" ;;
44 --prunepaths) PRUNEPATHS="$val" ;;
45 --prunefs) PRUNEFS="$val" ;;
46 --output) LOCATE_DB="$val" ;;
47 --netuser) NETUSER="$val" ;;
48 --localuser) LOCALUSER="$val" ;;
49 --old-format) old=yes ;;
50 --changecwd) changeto="$val" ;;
51 --version) echo "GNU updatedb version @VERSION@"; exit 0 ;;
52 --help) echo "$usage"; exit 0 ;;
53 *) echo "updatedb: invalid option $opt
54 $usage" >&2
55 exit 1 ;;
56 esac
57 done
59 if test "$old" = yes; then
60 echo "Warning: future versions of findutils will shortly discontinue support for the old locate database format." >&2
62 sort="@SORT@"
63 print_option="-print"
64 frcode_options=""
65 else
66 if @SORT_SUPPORTS_Z@
67 then
68 sort="@SORT@ -z"
69 print_option="-print0"
70 frcode_options="-0"
71 else
72 sort="@SORT@"
73 print_option="-print"
74 frcode_options=""
78 getuid() {
79 # format of "id" output is ...
80 # uid=1(daemon) gid=1(other)
81 # for `id's that don't understand -u
82 id | cut -d'(' -f 1 | cut -d'=' -f2
85 # figure out if su supports the -s option
86 select_shell() {
87 if su "$1" -s $SHELL false < /dev/null ; then
88 # No.
89 echo ""
90 else
91 if su "$1" -s $SHELL true < /dev/null ; then
92 # Yes.
93 echo "-s $SHELL"
94 else
95 # su is unconditionally failing. We won't be able to
96 # figure out what is wrong, so be conservative.
97 echo ""
103 # You can set these in the environment, or use command-line options,
104 # to override their defaults:
106 # Any global options for find?
107 : ${FINDOPTIONS=}
109 # What shell shoud we use? We should use a POSIX-ish sh.
110 : ${SHELL="/bin/sh"}
112 # Non-network directories to put in the database.
113 : ${SEARCHPATHS="/"}
115 # Network (NFS, AFS, RFS, etc.) directories to put in the database.
116 : ${NETPATHS=}
118 # Directories to not put in the database, which would otherwise be.
119 : ${PRUNEPATHS="/tmp /usr/tmp /var/tmp /afs /amd /sfs"}
121 # Trailing slashes result in regex items that are never matched, which
122 # is not what the user will expect. Therefore we now reject such
123 # constructs.
124 for p in $PRUNEPATHS; do
125 case "$p" in
126 /*/) echo "$0: $p: pruned paths should not contain trailing slashes" >&2
127 exit 1
128 esac
129 done
131 # The same, in the form of a regex that find can use.
132 test -z "$PRUNEREGEX" &&
133 PRUNEREGEX=`echo $PRUNEPATHS|sed -e 's,^,\\\(^,' -e 's, ,$\\\)\\\|\\\(^,g' -e 's,$,$\\\),'`
135 # The database file to build.
136 : ${LOCATE_DB=@LOCATE_DB@}
138 # Directory to hold intermediate files.
139 if test -d /var/tmp; then
140 : ${TMPDIR=/var/tmp}
141 elif test -d /usr/tmp; then
142 : ${TMPDIR=/usr/tmp}
143 else
144 : ${TMPDIR=/tmp}
146 export TMPDIR
148 # The user to search network directories as.
149 : ${NETUSER=daemon}
151 # The directory containing the subprograms.
152 if test -n "$LIBEXECDIR" ; then
153 : LIBEXECDIR already set, do nothing
154 else
155 : ${LIBEXECDIR=@libexecdir@}
158 # The directory containing find.
159 if test -n "$BINDIR" ; then
160 : BINDIR already set, do nothing
161 else
162 : ${BINDIR=@bindir@}
165 # The names of the utilities to run to build the database.
166 : ${find:=${BINDIR}/@find@}
167 : ${frcode:=${LIBEXECDIR}/@frcode@}
168 : ${bigram:=${LIBEXECDIR}/@bigram@}
169 : ${code:=${LIBEXECDIR}/@code@}
172 PATH=/bin:/usr/bin:${BINDIR}; export PATH
174 : ${PRUNEFS="nfs NFS proc afs proc smbfs autofs iso9660 ncpfs coda devpts ftpfs devfs mfs sysfs shfs"}
176 if test -n "$PRUNEFS"; then
177 prunefs_exp=`echo $PRUNEFS |sed -e 's/\([^ ][^ ]*\)/-o -fstype \1/g' \
178 -e 's/-o //' -e 's/$/ -o/'`
179 else
180 prunefs_exp=''
183 # Make and code the file list.
184 # Sort case insensitively for users' convenience.
186 rm -f $LOCATE_DB.n
187 trap 'rm -f $LOCATE_DB.n; exit' HUP TERM
189 if test $old = no; then
191 # FIXME figure out how to sort null-terminated strings, and use -print0.
192 if {
193 cd "$changeto"
194 if test -n "$SEARCHPATHS"; then
195 if [ "$LOCALUSER" != "" ]; then
196 # : A1
197 su $LOCALUSER `select_shell $LOCALUSER` -c \
198 "$find $SEARCHPATHS $FINDOPTIONS \
199 \\( $prunefs_exp \
200 -type d -regex '$PRUNEREGEX' \\) -prune -o $print_option"
201 else
202 # : A2
203 $find $SEARCHPATHS $FINDOPTIONS \
204 \( $prunefs_exp \
205 -type d -regex "$PRUNEREGEX" \) -prune -o $print_option
209 if test -n "$NETPATHS"; then
210 myuid=`getuid`
211 if [ "$myuid" = 0 ]; then
212 # : A3
213 su $NETUSER `select_shell $NETUSER` -c \
214 "$find $NETPATHS $FINDOPTIONS \\( -type d -regex '$PRUNEREGEX' -prune \\) -o $print_option" ||
215 exit $?
216 else
217 # : A4
218 $find $NETPATHS $FINDOPTIONS \( -type d -regex "$PRUNEREGEX" -prune \) -o $print_option ||
219 exit $?
222 } | $sort -f | $frcode $frcode_options > $LOCATE_DB.n
223 then
224 # OK so far
225 true
226 else
227 rv=$?
228 echo "Failed to generate $LOCATE_DB.n" >&2
229 rm -f $LOCATE_DB.n
230 exit $rv
233 # To avoid breaking locate while this script is running, put the
234 # results in a temp file, then rename it atomically.
235 if test -s $LOCATE_DB.n; then
236 rm -f $LOCATE_DB
237 mv $LOCATE_DB.n $LOCATE_DB
238 chmod 644 $LOCATE_DB
239 else
240 echo "updatedb: new database would be empty" >&2
241 rm -f $LOCATE_DB.n
244 else # old
246 if ! bigrams=`mktemp -t updatedbXXXXXXXXX`; then
247 echo tempfile failed
248 exit 1
251 if ! filelist=`mktemp -t updatedbXXXXXXXXX`; then
252 echo tempfile failed
253 exit 1
256 rm -f $LOCATE_DB.n
257 trap 'rm -f $bigrams $filelist $LOCATE_DB.n; exit' HUP TERM
259 # Alphabetize subdirectories before file entries using tr. James Woods says:
260 # "to get everything in monotonic collating sequence, to avoid some
261 # breakage i'll have to think about."
263 cd "$changeto"
264 if test -n "$SEARCHPATHS"; then
265 if [ "$LOCALUSER" != "" ]; then
266 # : A5
267 su $LOCALUSER `select_shell $LOCALUSER` -c \
268 "$find $SEARCHPATHS $FINDOPTIONS \
269 \( $prunefs_exp \
270 -type d -regex '$PRUNEREGEX' \) -prune -o $print_option" || exit $?
271 else
272 # : A6
273 $find $SEARCHPATHS $FINDOPTIONS \
274 \( $prunefs_exp \
275 -type d -regex "$PRUNEREGEX" \) -prune -o $print_option || exit $?
279 if test -n "$NETPATHS"; then
280 myuid=`getuid`
281 if [ "$myuid" = 0 ]; then
282 # : A7
283 su $NETUSER `select_shell $NETUSER` -c \
284 "$find $NETPATHS $FINDOPTIONS \\( -type d -regex '$PRUNEREGEX' -prune \\) -o $print_option" ||
285 exit $?
286 else
287 # : A8
288 $find $NETPATHS $FINDOPTIONS \( -type d -regex "$PRUNEREGEX" -prune \) -o $print_option ||
289 exit $?
292 } | tr / '\001' | $sort -f | tr '\001' / > $filelist
294 # Compute the (at most 128) most common bigrams in the file list.
295 $bigram $bigram_opts < $filelist | sort | uniq -c | sort -nr |
296 awk '{ if (NR <= 128) print $2 }' | tr -d '\012' > $bigrams
298 # Code the file list.
299 $code $bigrams < $filelist > $LOCATE_DB.n
301 rm -f $bigrams $filelist
303 # To reduce the chances of breaking locate while this script is running,
304 # put the results in a temp file, then rename it atomically.
305 if test -s $LOCATE_DB.n; then
306 rm -f $LOCATE_DB
307 mv $LOCATE_DB.n $LOCATE_DB
308 chmod 644 $LOCATE_DB
309 else
310 echo "updatedb: new database would be empty" >&2
311 rm -f $LOCATE_DB.n
316 exit 0