Added new option --changecwd to updatedb so that the 'cd /' which it does can be...
[findutils.git] / locate / updatedb.sh
blob9db2ff9383e50f5548f7d9773af92a685c87163f
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., 9 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 [--localpaths='dir1 dir2...'] [--netpaths='dir1 dir2...']
24 [--prunepaths='dir1 dir2...'] [--prunefs='fs1 fs2...']
25 [--output=dbfile] [--netuser=user] [--localuser=user]
26 [--old-format] [--version] [--help]
28 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 --localpaths) SEARCHPATHS="$val" ;;
42 --netpaths) NETPATHS="$val" ;;
43 --prunepaths) PRUNEPATHS="$val" ;;
44 --prunefs) PRUNEFS="$val" ;;
45 --output) LOCATE_DB="$val" ;;
46 --netuser) NETUSER="$val" ;;
47 --localuser) LOCALUSER="$val" ;;
48 --old-format) old=yes ;;
49 --changecwd) changeto="$val" ;;
50 --version) echo "GNU updatedb version @VERSION@"; exit 0 ;;
51 --help) echo "$usage"; exit 0 ;;
52 *) echo "updatedb: invalid option $opt
53 $usage" >&2
54 exit 1 ;;
55 esac
56 done
58 getuid() {
59 # format of "id" output is ...
60 # uid=1(daemon) gid=1(other)
61 # for `id's that don't understand -u
62 id | cut -d'(' -f 1 | cut -d'=' -f2
65 # You can set these in the environment, or use command-line options,
66 # to override their defaults:
68 # Non-network directories to put in the database.
69 : ${SEARCHPATHS="/"}
71 # Network (NFS, AFS, RFS, etc.) directories to put in the database.
72 : ${NETPATHS=}
74 # Directories to not put in the database, which would otherwise be.
75 : ${PRUNEPATHS="/tmp /usr/tmp /var/tmp /afs"}
77 # The same, in the form of a regex that find can use.
78 test -z "$PRUNEREGEX" &&
79 PRUNEREGEX=`echo $PRUNEPATHS|sed -e 's,^,\\\(^,' -e 's, ,$\\\)\\\|\\\(^,g' -e 's,$,$\\\),'`
81 # The database file to build.
82 : ${LOCATE_DB=@LOCATE_DB@}
84 # Directory to hold intermediate files.
85 if test -d /var/tmp; then
86 : ${TMPDIR=/var/tmp}
87 elif test -d /usr/tmp; then
88 : ${TMPDIR=/usr/tmp}
89 else
90 : ${TMPDIR=/tmp}
92 export TMPDIR
94 # The user to search network directories as.
95 : ${NETUSER=daemon}
97 # The directory containing the subprograms.
98 if test -n "$LIBEXECDIR" ; then
99 : LIBEXECDIR already set, do nothing
100 else
101 : ${LIBEXECDIR=@libexecdir@}
104 # The directory containing find.
105 if test -n "$BINDIR" ; then
106 : BINDIR already set, do nothing
107 else
108 : ${BINDIR=@bindir@}
111 # The names of the utilities to run to build the database.
112 : ${find:=${BINDIR}/@find@}
113 : ${frcode:=${LIBEXECDIR}/@frcode@}
114 : ${bigram:=${LIBEXECDIR}/@bigram@}
115 : ${code:=${LIBEXECDIR}/@code@}
117 PATH=/bin:/usr/bin:${BINDIR}; export PATH
119 : ${PRUNEFS="nfs NFS proc"}
121 if test -n "$PRUNEFS"; then
122 prunefs_exp=`echo $PRUNEFS |sed -e 's/\([^ ][^ ]*\)/-o -fstype \1/g' \
123 -e 's/-o //' -e 's/$/ -o/'`
124 else
125 prunefs_exp=''
128 # Make and code the file list.
129 # Sort case insensitively for users' convenience.
131 rm -f $LOCATE_DB.n
132 trap 'rm -f $LOCATE_DB.n; exit' 1 15
134 if test $old = no; then
136 # FIXME figure out how to sort null-terminated strings, and use -print0.
137 if {
138 cd "$changeto"
139 if test -n "$SEARCHPATHS"; then
140 if [ "$LOCALUSER" != "" ]; then
141 # : A1
142 su $LOCALUSER -c \
143 "$find $SEARCHPATHS \
144 \\( $prunefs_exp \
145 -type d -regex '$PRUNEREGEX' \\) -prune -o -print"
146 else
147 # : A2
148 $find $SEARCHPATHS \
149 \( $prunefs_exp \
150 -type d -regex "$PRUNEREGEX" \) -prune -o -print
154 if test -n "$NETPATHS"; then
155 myuid=`getuid`
156 if [ "$myuid" = 0 ]; then
157 # : A3
158 su $NETUSER -c \
159 "$find $NETPATHS \\( -type d -regex '$PRUNEREGEX' -prune \\) -o -print" ||
160 exit $?
161 else
162 # : A4
163 $find $NETPATHS \( -type d -regex "$PRUNEREGEX" -prune \) -o -print ||
164 exit $?
167 } | sort -f | $frcode > $LOCATE_DB.n
168 then
169 # OK so far
170 true
171 else
172 rv=$?
173 echo "Failed to generate $LOCATE_DB.n" >&2
174 rm -f $LOCATE_DB.n
175 exit $rv
178 # To avoid breaking locate while this script is running, put the
179 # results in a temp file, then rename it atomically.
180 if test -s $LOCATE_DB.n; then
181 rm -f $LOCATE_DB
182 mv $LOCATE_DB.n $LOCATE_DB
183 chmod 644 $LOCATE_DB
184 else
185 echo "updatedb: new database would be empty" >&2
186 rm -f $LOCATE_DB.n
189 else # old
191 if ! bigrams=`tempfile -p updatedb`; then
192 echo tempfile failed
193 exit 1
196 if ! filelist=`tempfile -p updatedb`; then
197 echo tempfile failed
198 exit 1
201 rm -f $LOCATE_DB.n
202 trap 'rm -f $bigrams $filelist $LOCATE_DB.n; exit' 1 15
204 # Alphabetize subdirectories before file entries using tr. James says:
205 # "to get everything in monotonic collating sequence, to avoid some
206 # breakage i'll have to think about."
208 cd "$changeto"
209 if test -n "$SEARCHPATHS"; then
210 if [ "$LOCALUSER" != "" ]; then
211 # : A5
212 su $LOCALUSER -c \
213 "$find $SEARCHPATHS \
214 \( $prunefs_exp \
215 -type d -regex '$PRUNEREGEX' \) -prune -o -print" || exit $?
216 else
217 # : A6
218 $find $SEARCHPATHS \
219 \( $prunefs_exp \
220 -type d -regex "$PRUNEREGEX" \) -prune -o -print || exit $?
224 if test -n "$NETPATHS"; then
225 myuid=`getuid`
226 if [ "$myuid" = 0 ]; then
227 # : A7
228 su $NETUSER -c \
229 "$find $NETPATHS \\( -type d -regex '$PRUNEREGEX' -prune \\) -o -print" ||
230 exit $?
231 else
232 # : A8
233 $find $NETPATHS \( -type d -regex "$PRUNEREGEX" -prune \) -o -print ||
234 exit $?
237 } | tr / '\001' | sort -f | tr '\001' / > $filelist
239 # Compute the (at most 128) most common bigrams in the file list.
240 $bigram < $filelist | sort | uniq -c | sort -nr |
241 awk '{ if (NR <= 128) print $2 }' | tr -d '\012' > $bigrams
243 # Code the file list.
244 $code $bigrams < $filelist > $LOCATE_DB.n
246 rm -f $bigrams $filelist
248 # To reduce the chances of breaking locate while this script is running,
249 # put the results in a temp file, then rename it atomically.
250 if test -s $LOCATE_DB.n; then
251 rm -f $LOCATE_DB
252 mv $LOCATE_DB.n $LOCATE_DB
253 chmod 644 $LOCATE_DB
254 else
255 echo "updatedb: new database would be empty" >&2
256 rm -f $LOCATE_DB.n
261 exit 0