xref: /netbsd/usr.bin/locate/locate/updatedb.sh (revision 6550d01e)
1#!/bin/sh
2#
3#	$NetBSD: updatedb.sh,v 1.11 2006/04/23 03:04:08 christos Exp $
4#
5# Copyright (c) 1989, 1993
6#	The Regents of the University of California.  All rights reserved.
7#
8# This code is derived from software contributed to Berkeley by
9# James A. Woods.
10#
11# Redistribution and use in source and binary forms, with or without
12# modification, are permitted provided that the following conditions
13# are met:
14# 1. Redistributions of source code must retain the above copyright
15#    notice, this list of conditions and the following disclaimer.
16# 2. Redistributions in binary form must reproduce the above copyright
17#    notice, this list of conditions and the following disclaimer in the
18#    documentation and/or other materials provided with the distribution.
19# 3. All advertising materials mentioning features or use of this software
20#    must display the following acknowledgement:
21#	This product includes software developed by the University of
22#	California, Berkeley and its contributors.
23# 4. Neither the name of the University nor the names of its contributors
24#    may be used to endorse or promote products derived from this software
25#    without specific prior written permission.
26#
27# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30# ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37# SUCH DAMAGE.
38#
39#	@(#)updatedb.csh	8.4 (Berkeley) 10/27/94
40#
41
42LIBDIR="/usr/libexec"			# for subprograms
43					# for temp files
44TMPDIR=/tmp
45FCODES="/var/db/locate.database"	# the database
46CONF=/etc/locate.conf			# configuration file
47
48PATH="/bin:/usr/bin"
49
50ignorefs='! -fstype local -o -fstype cd9660 -o -fstype fdesc -o -fstype kernfs -o -fstype procfs'
51ignore=
52SRCHPATHS=
53
54# read configuration file
55if [ -f "$CONF" ]; then
56	exec 5<&0 < "$CONF"
57	while read com args; do
58		case "$com/$args" in /) continue;; esac	# skip blank lines
59		case "$com" in
60		'#'*)	;;			# lines start with # is comment
61		searchpath)
62			SRCHPATHS="$SRCHPATHS $args";;
63		ignorefs)
64			for i in $args; do
65				case "$i" in
66				none)	ignorefs=;;
67				*)	fs=`echo "$i" | sed -e 's/^!/! -fstype /' -e t -e 's/^/-fstype /'`
68					ignorefs="${ignorefs:+${ignorefs} -o }${fs}"
69				esac
70			done;;
71		ignore)
72			set -f
73			for i in $args; do
74				ignore="${ignore:+${ignore} -o }-path ${i}"
75			done
76			set +f;;
77		ignorecontents)
78			set -f
79			for i in $args; do
80				ignore="${ignore:+${ignore} -o }-path ${i} -print"
81			done
82			set +f;;
83		workdir)
84			if [ -d "$args" ]; then
85				TMPDIR="$args"
86			else
87				echo "$CONF: workdir: $args nonexistent" >&2
88			fi;;
89		*)
90			echo "$CONF: $com: unknown config command"	>&2
91			exit 1;;
92		esac
93	done
94	exec <&5 5>&-
95fi
96
97: ${SRCHPATHS:=/}			# directories to be put in the database
98export TMPDIR
99
100case "$ignorefs/$ignore" in
101/)	lp= ;   rp= ;;
102*)	lp='('; rp=') -prune -o' ;;
103esac
104
105# insert '-o' if neither $ignorefs or $ignore are empty
106case "$ignorefs $ignore" in
107' '*|*' ')	;;
108*)		ignore="-o $ignore";;
109esac
110
111FILELIST=$(mktemp -t locate.list) || exit 1
112trap "rm -f '$FILELIST'" EXIT
113trap "rm -f '$FILELIST'; exit 1" INT QUIT TERM
114
115# Make a file list and compute common bigrams.
116# Entries of each directory shall be sorted (find -s).
117
118set -f
119(find -s ${SRCHPATHS} $lp $ignorefs $ignore $rp -print; true) | cat  >> "$FILELIST"
120if [ $? != 0 ]
121then
122	exit 1
123fi
124
125BIGRAMS=$($LIBDIR/locate.bigram <"$FILELIST")
126
127# code the file list
128if [ -z "$BIGRAMS" ]; then
129	echo 'locate: updatedb failed' >&2
130else
131	$LIBDIR/locate.code "$BIGRAMS" <"$FILELIST" >"$FCODES"
132	chmod 644 "$FCODES"
133fi
134