imported from standard location
[findutils.git] / locate / updatedb.in
blob5653ffa39bafd93ebc0bb1c6625278d65c8457a6
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 : ${prefix=@prefix@}
50 : ${exec_prefix=@exec_prefix@}
52 # You can set these in the environment, or use command-line options,
53 # to override their defaults:
55 # Non-network directories to put in the database.
56 : ${SEARCHPATHS="/"}
58 # Network (NFS, AFS, RFS, etc.) directories to put in the database.
59 : ${NETPATHS=}
61 # Directories to not put in the database, which would otherwise be.
62 : ${PRUNEPATHS="/tmp /usr/tmp /var/tmp /afs"}
64 # The same, in the form of a regex that find can use.
65 test -z "$PRUNEREGEX" &&
66 PRUNEREGEX=`echo $PRUNEPATHS|sed -e 's,^,\\\(^,' -e 's, ,$\\\)\\\|\\\(^,g' -e 's,$,$\\\),'`
68 # The database file to build.
69 : ${LOCATE_DB=@LOCATE_DB@}
71 # Directory to hold intermediate files.
72 if test -d /var/tmp; then
73 : ${TMPDIR=/var/tmp}
74 elif test -d /usr/tmp; then
75 : ${TMPDIR=/usr/tmp}
76 else
77 : ${TMPDIR=/tmp}
80 # The user to search network directories as.
81 : ${NETUSER=daemon}
83 # The directory containing the subprograms.
84 : ${LIBEXECDIR=@libexecdir@}
86 # The directory containing find.
87 : ${BINDIR=@bindir@}
89 # The names of the utilities to run to build the database.
90 : ${find=@bindir@/find}
91 : ${frcode=@libexecdir@/frcode}
92 : ${bigram=@libexecdir@/bigram}
93 : ${code=@libexecdir@/code}
95 PATH=$LIBEXECDIR:/bin:/usr/bin:$PATH export PATH
97 : ${PRUNEFS=nfs NFS proc}
99 if test -n "$PRUNEFS"; then
100 prunefs_exp=`echo $PRUNEFS |sed -e 's/\([^ ]\+\)/-o -fstype \1/g' \
101 -e 's/-o //' -e 's/$/ -o/'`
102 else
103 prunefs_exp=''
106 # Make and code the file list.
107 # Sort case insensitively for users' convenience.
109 rm -f $LOCATE_DB.n
110 trap 'rm -f $LOCATE_DB.n; exit' 1 15
112 if test $old = no; then
114 # FIXME figure out how to sort null-terminated strings, and use -print0.
116 if test -n "$SEARCHPATHS"; then
117 if [ "$LOCALUSER" != "" ]; then
118 su $LOCALUSER -c \
119 "$find $SEARCHPATHS \
120 \\( $prunefs_exp \
121 -type d -regex '$PRUNEREGEX' \\) -prune -o -print"
122 else
123 $find $SEARCHPATHS \
124 \( $prunefs_exp \
125 -type d -regex "$PRUNEREGEX" \) -prune -o -print
129 if test -n "$NETPATHS"; then
130 if [ "`whoami`" = root ]; then
131 su $NETUSER -c \
132 "$find $NETPATHS \\( -type d -regex '$PRUNEREGEX' -prune \\) -o -print"
133 else
134 $find $NETPATHS \( -type d -regex "$PRUNEREGEX" -prune \) -o -print
137 } | sort -f | $frcode > $LOCATE_DB.n
139 # To avoid breaking locate while this script is running, put the
140 # results in a temp file, then rename it atomically.
141 if test -s $LOCATE_DB.n; then
142 rm -f $LOCATE_DB
143 mv $LOCATE_DB.n $LOCATE_DB
144 chmod 644 $LOCATE_DB
145 else
146 echo "updatedb: new database would be empty" >&2
147 rm -f $LOCATE_DB.n
150 else # old
152 if ! bigrams=`tempfile -p updatedb`; then
153 echo tempfile failed
154 exit 1
157 if ! filelist=`tempfile -p updatedb`; then
158 echo tempfile failed
159 exit 1
162 rm -f $LOCATE_DB.n
163 trap 'rm -f $bigrams $filelist $LOCATE_DB.n; exit' 1 15
165 # Alphabetize subdirectories before file entries using tr. James says:
166 # "to get everything in monotonic collating sequence, to avoid some
167 # breakage i'll have to think about."
169 if test -n "$SEARCHPATHS"; then
170 if [ "$LOCALUSER" != "" ]; then
171 su $LOCALUSER -c \
172 "$find $SEARCHPATHS \
173 \( $prunefs_exp \
174 -type d -regex '$PRUNEREGEX' \) -prune -o -print"
175 else
176 $find $SEARCHPATHS \
177 \( $prunefs_exp \
178 -type d -regex "$PRUNEREGEX" \) -prune -o -print
182 if test -n "$NETPATHS"; then
183 if [ "`whoami`" = root ]; then
184 su $NETUSER -c \
185 "$find $NETPATHS \\( -type d -regex '$PRUNEREGEX' -prune \\) -o -print"
186 else
187 $find $NETPATHS \( -type d -regex "$PRUNEREGEX" -prune \) -o -print
190 } | tr / '\001' | sort -f | tr '\001' / > $filelist
192 # Compute the (at most 128) most common bigrams in the file list.
193 $bigram < $filelist | sort | uniq -c | sort -nr |
194 awk '{ if (NR <= 128) print $2 }' | tr -d '\012' > $bigrams
196 # Code the file list.
197 $code $bigrams < $filelist > $LOCATE_DB.n
199 rm -f $bigrams $filelist
201 # To reduce the chances of breaking locate while this script is running,
202 # put the results in a temp file, then rename it atomically.
203 if test -s $LOCATE_DB.n; then
204 rm -f $LOCATE_DB
205 mv $LOCATE_DB.n $LOCATE_DB
206 chmod 644 $LOCATE_DB
207 else
208 echo "updatedb: new database would be empty" >&2
209 rm -f $LOCATE_DB.n
214 exit 0