*** empty log message ***
[findutils.git] / locate / updatedb.sh
blob3569b5ff8d99d6f4501ff06ae226a513baddb9bf
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., 675 Mass Ave, Cambridge, MA 02139, USA.
19 # csh original by James Woods; sh conversion by David MacKenzie.
21 usage="\
22 Usage: updatedb [--localpaths='dir1 dir2...'] [--netpaths='dir1 dir2...']
23 [--prunepaths='dir1 dir2...'] [--prunefs='fs1 fs2...']
24 [--output=dbfile] [--netuser=user] [--localuser=user]
25 [--old-format] [--version] [--help]"
27 old=no
28 for arg
30 opt=`echo $arg|sed 's/^\([^=]*\).*/\1/'`
31 val=`echo $arg|sed 's/^[^=]*=\(.*\)/\1/'`
32 case "$opt" in
33 --localpaths) SEARCHPATHS="$val" ;;
34 --netpaths) NETPATHS="$val" ;;
35 --prunepaths) PRUNEPATHS="$val" ;;
36 --prunefs) PRUNEFS="$val" ;;
37 --output) LOCATE_DB="$val" ;;
38 --netuser) NETUSER="$val" ;;
39 --localuser) LOCALUSER="$val" ;;
40 --old-format) old=yes ;;
41 --version) echo "GNU updatedb version @version@"; exit 0 ;;
42 --help) echo "$usage"; exit 0 ;;
43 *) echo "updatedb: invalid option $opt
44 $usage" >&2
45 exit 1 ;;
46 esac
47 done
49 # You can set these in the environment, or use command-line options,
50 # to override their defaults:
52 # Non-network directories to put in the database.
53 : ${SEARCHPATHS="/"}
55 # Network (NFS, AFS, RFS, etc.) directories to put in the database.
56 : ${NETPATHS=}
58 # Directories to not put in the database, which would otherwise be.
59 : ${PRUNEPATHS="/tmp /usr/tmp /var/tmp /afs"}
61 # The same, in the form of a regex that find can use.
62 test -z "$PRUNEREGEX" &&
63 PRUNEREGEX=`echo $PRUNEPATHS|sed -e 's,^,\\\(^,' -e 's, ,$\\\)\\\|\\\(^,g' -e 's,$,$\\\),'`
65 # The database file to build.
66 : ${LOCATE_DB=@LOCATE_DB@}
68 # Directory to hold intermediate files.
69 if test -d /var/tmp; then
70 : ${TMPDIR=/var/tmp}
71 elif test -d /usr/tmp; then
72 : ${TMPDIR=/usr/tmp}
73 else
74 : ${TMPDIR=/tmp}
77 # The user to search network directories as.
78 : ${NETUSER=daemon}
80 # The directory containing the subprograms.
81 : ${LIBEXECDIR=@libexecdir@}
83 # The directory containing find.
84 : ${BINDIR=@bindir@}
86 # The names of the utilities to run to build the database.
87 : ${find=@find@}
88 : ${frcode=@frcode@}
89 : ${bigram=@bigram@}
90 : ${code=@code@}
92 PATH=$LIBEXECDIR:/bin:/usr/bin:$PATH export PATH
94 : ${PRUNEFS=nfs NFS proc}
96 if test -n "$PRUNEFS"; then
97 prunefs_exp=`echo $PRUNEFS |sed -e 's/\([^ ]\+\)/-o -fstype \1/g' \
98 -e 's/-o //' -e 's/$/ -o/'`
99 else
100 prunefs_exp=''
103 # Make and code the file list.
104 # Sort case insensitively for users' convenience.
106 rm -f $LOCATE_DB.n
107 trap 'rm -f $LOCATE_DB.n; exit' 1 15
109 if test $old = no; then
111 # FIXME figure out how to sort null-terminated strings, and use -print0.
113 if test -n "$SEARCHPATHS"; then
114 if [ "$LOCALUSER" != "" ]; then
115 su $LOCALUSER -c \
116 "$find $SEARCHPATHS \
117 \\( $prunefs_exp \
118 -type d -regex '$PRUNEREGEX' \\) -prune -o -print"
119 else
120 $find $SEARCHPATHS \
121 \( $prunefs_exp \
122 -type d -regex "$PRUNEREGEX" \) -prune -o -print
126 if test -n "$NETPATHS"; then
127 if [ "`whoami`" = root ]; then
128 su $NETUSER -c \
129 "$find $NETPATHS \\( -type d -regex '$PRUNEREGEX' -prune \\) -o -print"
130 else
131 $find $NETPATHS \( -type d -regex "$PRUNEREGEX" -prune \) -o -print
134 } | sort -f | $frcode > $LOCATE_DB.n
136 # To avoid breaking locate while this script is running, put the
137 # results in a temp file, then rename it atomically.
138 if test -s $LOCATE_DB.n; then
139 rm -f $LOCATE_DB
140 mv $LOCATE_DB.n $LOCATE_DB
141 chmod 644 $LOCATE_DB
142 else
143 echo "updatedb: new database would be empty" >&2
144 rm -f $LOCATE_DB.n
147 else # old
149 if ! bigrams=`tempfile -p updatedb`; then
150 echo tempfile failed
151 exit 1
154 if ! filelist=`tempfile -p updatedb`; then
155 echo tempfile failed
156 exit 1
159 rm -f $LOCATE_DB.n
160 trap 'rm -f $bigrams $filelist $LOCATE_DB.n; exit' 1 15
162 # Alphabetize subdirectories before file entries using tr. James says:
163 # "to get everything in monotonic collating sequence, to avoid some
164 # breakage i'll have to think about."
166 if test -n "$SEARCHPATHS"; then
167 if [ "$LOCALUSER" != "" ]; then
168 su $LOCALUSER -c \
169 "$find $SEARCHPATHS \
170 \( $prunefs_exp \
171 -type d -regex '$PRUNEREGEX' \) -prune -o -print"
172 else
173 $find $SEARCHPATHS \
174 \( $prunefs_exp \
175 -type d -regex "$PRUNEREGEX" \) -prune -o -print
179 if test -n "$NETPATHS"; then
180 if [ "`whoami`" = root ]; then
181 su $NETUSER -c \
182 "$find $NETPATHS \\( -type d -regex '$PRUNEREGEX' -prune \\) -o -print"
183 else
184 $find $NETPATHS \( -type d -regex "$PRUNEREGEX" -prune \) -o -print
187 } | tr / '\001' | sort -f | tr '\001' / > $filelist
189 # Compute the (at most 128) most common bigrams in the file list.
190 $bigram < $filelist | sort | uniq -c | sort -nr |
191 awk '{ if (NR <= 128) print $2 }' | tr -d '\012' > $bigrams
193 # Code the file list.
194 $code $bigrams < $filelist > $LOCATE_DB.n
196 rm -f $bigrams $filelist
198 # To reduce the chances of breaking locate while this script is running,
199 # put the results in a temp file, then rename it atomically.
200 if test -s $LOCATE_DB.n; then
201 rm -f $LOCATE_DB
202 mv $LOCATE_DB.n $LOCATE_DB
203 chmod 644 $LOCATE_DB
204 else
205 echo "updatedb: new database would be empty" >&2
206 rm -f $LOCATE_DB.n
211 exit 0