Switch to a new scheme for saving directory entry data which is extensible to allow...
[findutils.git] / locate / updatedb.sh
blobdc7672c3ec522294eef0f1cf5ebaa4c4abdd26ed
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 # The same, in the form of a regex that find can use.
122 test -z "$PRUNEREGEX" &&
123 PRUNEREGEX=`echo $PRUNEPATHS|sed -e 's,^,\\\(^,' -e 's, ,$\\\)\\\|\\\(^,g' -e 's,$,$\\\),'`
125 # The database file to build.
126 : ${LOCATE_DB=@LOCATE_DB@}
128 # Directory to hold intermediate files.
129 if test -d /var/tmp; then
130 : ${TMPDIR=/var/tmp}
131 elif test -d /usr/tmp; then
132 : ${TMPDIR=/usr/tmp}
133 else
134 : ${TMPDIR=/tmp}
136 export TMPDIR
138 # The user to search network directories as.
139 : ${NETUSER=daemon}
141 # The directory containing the subprograms.
142 if test -n "$LIBEXECDIR" ; then
143 : LIBEXECDIR already set, do nothing
144 else
145 : ${LIBEXECDIR=@libexecdir@}
148 # The directory containing find.
149 if test -n "$BINDIR" ; then
150 : BINDIR already set, do nothing
151 else
152 : ${BINDIR=@bindir@}
155 # The names of the utilities to run to build the database.
156 : ${find:=${BINDIR}/@find@}
157 : ${frcode:=${LIBEXECDIR}/@frcode@}
158 : ${bigram:=${LIBEXECDIR}/@bigram@}
159 : ${code:=${LIBEXECDIR}/@code@}
162 PATH=/bin:/usr/bin:${BINDIR}; export PATH
164 : ${PRUNEFS="nfs NFS proc afs proc smbfs autofs iso9660 ncpfs coda devpts ftpfs devfs mfs sysfs shfs"}
166 if test -n "$PRUNEFS"; then
167 prunefs_exp=`echo $PRUNEFS |sed -e 's/\([^ ][^ ]*\)/-o -fstype \1/g' \
168 -e 's/-o //' -e 's/$/ -o/'`
169 else
170 prunefs_exp=''
173 # Make and code the file list.
174 # Sort case insensitively for users' convenience.
176 rm -f $LOCATE_DB.n
177 trap 'rm -f $LOCATE_DB.n; exit' HUP TERM
179 if test $old = no; then
181 # FIXME figure out how to sort null-terminated strings, and use -print0.
182 if {
183 cd "$changeto"
184 if test -n "$SEARCHPATHS"; then
185 if [ "$LOCALUSER" != "" ]; then
186 # : A1
187 su $LOCALUSER `select_shell $LOCALUSER` -c \
188 "$find $SEARCHPATHS $FINDOPTIONS \
189 \\( $prunefs_exp \
190 -type d -regex '$PRUNEREGEX' \\) -prune -o $print_option"
191 else
192 # : A2
193 $find $SEARCHPATHS $FINDOPTIONS \
194 \( $prunefs_exp \
195 -type d -regex "$PRUNEREGEX" \) -prune -o $print_option
199 if test -n "$NETPATHS"; then
200 myuid=`getuid`
201 if [ "$myuid" = 0 ]; then
202 # : A3
203 su $NETUSER `select_shell $NETUSER` -c \
204 "$find $NETPATHS $FINDOPTIONS \\( -type d -regex '$PRUNEREGEX' -prune \\) -o $print_option" ||
205 exit $?
206 else
207 # : A4
208 $find $NETPATHS $FINDOPTIONS \( -type d -regex "$PRUNEREGEX" -prune \) -o $print_option ||
209 exit $?
212 } | $sort -f | $frcode $frcode_options > $LOCATE_DB.n
213 then
214 # OK so far
215 true
216 else
217 rv=$?
218 echo "Failed to generate $LOCATE_DB.n" >&2
219 rm -f $LOCATE_DB.n
220 exit $rv
223 # To avoid breaking locate while this script is running, put the
224 # results in a temp file, then rename it atomically.
225 if test -s $LOCATE_DB.n; then
226 rm -f $LOCATE_DB
227 mv $LOCATE_DB.n $LOCATE_DB
228 chmod 644 $LOCATE_DB
229 else
230 echo "updatedb: new database would be empty" >&2
231 rm -f $LOCATE_DB.n
234 else # old
236 if ! bigrams=`mktemp -t updatedbXXXXXXXXX`; then
237 echo tempfile failed
238 exit 1
241 if ! filelist=`mktemp -t updatedbXXXXXXXXX`; then
242 echo tempfile failed
243 exit 1
246 rm -f $LOCATE_DB.n
247 trap 'rm -f $bigrams $filelist $LOCATE_DB.n; exit' HUP TERM
249 # Alphabetize subdirectories before file entries using tr. James Woods says:
250 # "to get everything in monotonic collating sequence, to avoid some
251 # breakage i'll have to think about."
253 cd "$changeto"
254 if test -n "$SEARCHPATHS"; then
255 if [ "$LOCALUSER" != "" ]; then
256 # : A5
257 su $LOCALUSER `select_shell $LOCALUSER` -c \
258 "$find $SEARCHPATHS $FINDOPTIONS \
259 \( $prunefs_exp \
260 -type d -regex '$PRUNEREGEX' \) -prune -o $print_option" || exit $?
261 else
262 # : A6
263 $find $SEARCHPATHS $FINDOPTIONS \
264 \( $prunefs_exp \
265 -type d -regex "$PRUNEREGEX" \) -prune -o $print_option || exit $?
269 if test -n "$NETPATHS"; then
270 myuid=`getuid`
271 if [ "$myuid" = 0 ]; then
272 # : A7
273 su $NETUSER `select_shell $NETUSER` -c \
274 "$find $NETPATHS $FINDOPTIONS \\( -type d -regex '$PRUNEREGEX' -prune \\) -o $print_option" ||
275 exit $?
276 else
277 # : A8
278 $find $NETPATHS $FINDOPTIONS \( -type d -regex "$PRUNEREGEX" -prune \) -o $print_option ||
279 exit $?
282 } | tr / '\001' | $sort -f | tr '\001' / > $filelist
284 # Compute the (at most 128) most common bigrams in the file list.
285 $bigram $bigram_opts < $filelist | sort | uniq -c | sort -nr |
286 awk '{ if (NR <= 128) print $2 }' | tr -d '\012' > $bigrams
288 # Code the file list.
289 $code $bigrams < $filelist > $LOCATE_DB.n
291 rm -f $bigrams $filelist
293 # To reduce the chances of breaking locate while this script is running,
294 # put the results in a temp file, then rename it atomically.
295 if test -s $LOCATE_DB.n; then
296 rm -f $LOCATE_DB
297 mv $LOCATE_DB.n $LOCATE_DB
298 chmod 644 $LOCATE_DB
299 else
300 echo "updatedb: new database would be empty" >&2
301 rm -f $LOCATE_DB.n
306 exit 0