Applied (my own version of) Savannah patch 1601.
[findutils.git] / locate / updatedb.sh
blobe8368eac7b7a100a24637db7266d95b84b4a6e13
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>.
31 old=no
32 for arg
34 opt=`echo $arg|sed 's/^\([^=]*\).*/\1/'`
35 val=`echo $arg|sed 's/^[^=]*=\(.*\)/\1/'`
36 case "$opt" in
37 --localpaths) SEARCHPATHS="$val" ;;
38 --netpaths) NETPATHS="$val" ;;
39 --prunepaths) PRUNEPATHS="$val" ;;
40 --prunefs) PRUNEFS="$val" ;;
41 --output) LOCATE_DB="$val" ;;
42 --netuser) NETUSER="$val" ;;
43 --localuser) LOCALUSER="$val" ;;
44 --old-format) old=yes ;;
45 --version) echo "GNU updatedb version @VERSION@"; exit 0 ;;
46 --help) echo "$usage"; exit 0 ;;
47 *) echo "updatedb: invalid option $opt
48 $usage" >&2
49 exit 1 ;;
50 esac
51 done
53 getuid() {
54 # format of "id" output is ...
55 # uid=1(daemon) gid=1(other)
56 # for `id's that don't understand -u
57 id | cut -d'(' -f 1 | cut -d'=' -f2
60 # You can set these in the environment, or use command-line options,
61 # to override their defaults:
63 # Non-network directories to put in the database.
64 : ${SEARCHPATHS="/"}
66 # Network (NFS, AFS, RFS, etc.) directories to put in the database.
67 : ${NETPATHS=}
69 # Directories to not put in the database, which would otherwise be.
70 : ${PRUNEPATHS="/tmp /usr/tmp /var/tmp /afs"}
72 # The same, in the form of a regex that find can use.
73 test -z "$PRUNEREGEX" &&
74 PRUNEREGEX=`echo $PRUNEPATHS|sed -e 's,^,\\\(^,' -e 's, ,$\\\)\\\|\\\(^,g' -e 's,$,$\\\),'`
76 # The database file to build.
77 : ${LOCATE_DB=@LOCATE_DB@}
79 # Directory to hold intermediate files.
80 if test -d /var/tmp; then
81 : ${TMPDIR=/var/tmp}
82 elif test -d /usr/tmp; then
83 : ${TMPDIR=/usr/tmp}
84 else
85 : ${TMPDIR=/tmp}
87 export TMPDIR
89 # The user to search network directories as.
90 : ${NETUSER=daemon}
92 # The directory containing the subprograms.
93 : ${LIBEXECDIR=@libexecdir@}
95 # The directory containing find.
96 : ${BINDIR=@bindir@}
98 # The names of the utilities to run to build the database.
99 : ${find=${BINDIR}/@find@}
100 : ${frcode=${LIBEXECDIR}/@frcode@}
101 : ${bigram=${LIBEXECDIR}/@bigram@}
102 : ${code=${LIBEXECDIR}/@code@}
104 PATH=/bin:/usr/bin:${BINDIR}; export PATH
106 : ${PRUNEFS="nfs NFS proc"}
108 if test -n "$PRUNEFS"; then
109 prunefs_exp=`echo $PRUNEFS |sed -e 's/\([^ ][^ ]*\)/-o -fstype \1/g' \
110 -e 's/-o //' -e 's/$/ -o/'`
111 else
112 prunefs_exp=''
115 # Make and code the file list.
116 # Sort case insensitively for users' convenience.
118 rm -f $LOCATE_DB.n
119 trap 'rm -f $LOCATE_DB.n; exit' 1 15
121 if test $old = no; then
123 # FIXME figure out how to sort null-terminated strings, and use -print0.
125 if test -n "$SEARCHPATHS"; then
126 if [ "$LOCALUSER" != "" ]; then
127 su $LOCALUSER -c \
128 "$find $SEARCHPATHS \
129 \\( $prunefs_exp \
130 -type d -regex '$PRUNEREGEX' \\) -prune -o -print"
131 else
132 $find $SEARCHPATHS \
133 \( $prunefs_exp \
134 -type d -regex "$PRUNEREGEX" \) -prune -o -print
138 if test -n "$NETPATHS"; then
139 myuid=`getuid`
140 if [ "$myuid" = 0 ]; then
141 su $NETUSER -c \
142 "$find $NETPATHS \\( -type d -regex '$PRUNEREGEX' -prune \\) -o -print"
143 else
144 $find $NETPATHS \( -type d -regex "$PRUNEREGEX" -prune \) -o -print
147 } | sort -f | $frcode > $LOCATE_DB.n
149 # To avoid breaking locate while this script is running, put the
150 # results in a temp file, then rename it atomically.
151 if test -s $LOCATE_DB.n; then
152 rm -f $LOCATE_DB
153 mv $LOCATE_DB.n $LOCATE_DB
154 chmod 644 $LOCATE_DB
155 else
156 echo "updatedb: new database would be empty" >&2
157 rm -f $LOCATE_DB.n
160 else # old
162 if ! bigrams=`tempfile -p updatedb`; then
163 echo tempfile failed
164 exit 1
167 if ! filelist=`tempfile -p updatedb`; then
168 echo tempfile failed
169 exit 1
172 rm -f $LOCATE_DB.n
173 trap 'rm -f $bigrams $filelist $LOCATE_DB.n; exit' 1 15
175 # Alphabetize subdirectories before file entries using tr. James says:
176 # "to get everything in monotonic collating sequence, to avoid some
177 # breakage i'll have to think about."
179 if test -n "$SEARCHPATHS"; then
180 if [ "$LOCALUSER" != "" ]; then
181 su $LOCALUSER -c \
182 "$find $SEARCHPATHS \
183 \( $prunefs_exp \
184 -type d -regex '$PRUNEREGEX' \) -prune -o -print"
185 else
186 $find $SEARCHPATHS \
187 \( $prunefs_exp \
188 -type d -regex "$PRUNEREGEX" \) -prune -o -print
192 if test -n "$NETPATHS"; then
193 myuid=`getuid`
194 if [ "$myuid" = 0 ]; then
195 su $NETUSER -c \
196 "$find $NETPATHS \\( -type d -regex '$PRUNEREGEX' -prune \\) -o -print"
197 else
198 $find $NETPATHS \( -type d -regex "$PRUNEREGEX" -prune \) -o -print
201 } | tr / '\001' | sort -f | tr '\001' / > $filelist
203 # Compute the (at most 128) most common bigrams in the file list.
204 $bigram < $filelist | sort | uniq -c | sort -nr |
205 awk '{ if (NR <= 128) print $2 }' | tr -d '\012' > $bigrams
207 # Code the file list.
208 $code $bigrams < $filelist > $LOCATE_DB.n
210 rm -f $bigrams $filelist
212 # To reduce the chances of breaking locate while this script is running,
213 # put the results in a temp file, then rename it atomically.
214 if test -s $LOCATE_DB.n; then
215 rm -f $LOCATE_DB
216 mv $LOCATE_DB.n $LOCATE_DB
217 chmod 644 $LOCATE_DB
218 else
219 echo "updatedb: new database would be empty" >&2
220 rm -f $LOCATE_DB.n
225 exit 0