112155Snate#!/bin/sh
212155Snate#
317849Swosch# Copyright (c) September 1995 Wolfram Schneider <wosch@FreeBSD.org>. Berlin.
417849Swosch# All rights reserved.
512155Snate#
617849Swosch# Redistribution and use in source and binary forms, with or without
717849Swosch# modification, are permitted provided that the following conditions
817849Swosch# are met:
917849Swosch# 1. Redistributions of source code must retain the above copyright
1017849Swosch#    notice, this list of conditions and the following disclaimer.
1117849Swosch# 2. Redistributions in binary form must reproduce the above copyright
1217849Swosch#    notice, this list of conditions and the following disclaimer in the
1317849Swosch#    documentation and/or other materials provided with the distribution.
1417849Swosch#
1517849Swosch# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1617849Swosch# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1717849Swosch# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1817849Swosch# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1917849Swosch# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2017849Swosch# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2117849Swosch# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2217849Swosch# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2317849Swosch# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2417849Swosch# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2517849Swosch# SUCH DAMAGE.
2617849Swosch#
2717592Swosch# updatedb - update locate database for local mounted filesystems
2812155Snate#
2950477Speter# $FreeBSD$
3012155Snate
3198446Seivindif [ "$(id -u)" = "0" ]; then
3298446Seivind	echo ">>> WARNING" 1>&2
3398446Seivind	echo ">>> Executing updatedb as root.  This WILL reveal all filenames" 1>&2
3498446Seivind	echo ">>> on your machine to all login users, which is a security risk." 1>&2
3598446Seivindfi
3647174Swosch: ${LOCATE_CONFIG="/etc/locate.rc"}
3717592Swoschif [ -f "$LOCATE_CONFIG" -a -r "$LOCATE_CONFIG" ]; then
3817592Swosch       . $LOCATE_CONFIG
3912155Snatefi
4012155Snate
4117592Swosch# The directory containing locate subprograms
4234312Swosch: ${LIBEXECDIR:=/usr/libexec}; export LIBEXECDIR
4341413Swosch: ${TMPDIR:=/tmp}; export TMPDIR
4455835Skrisif ! TMPDIR=`mktemp -d $TMPDIR/locateXXXXXXXXXX`; then
4537049Swosch	exit 1
4637049Swoschfi
4712155Snate
4817592SwoschPATH=$LIBEXECDIR:/bin:/usr/bin:$PATH; export PATH
4912155Snate
5012155Snate
5134312Swosch: ${mklocatedb:=locate.mklocatedb}	 # make locate database program
5234312Swosch: ${FCODES:=/var/db/locate.database}	 # the database
53214615Swollman: ${SEARCHPATHS="/"}		# directories to be put in the database
54294779Sdes: ${PRUNEPATHS="/tmp /usr/tmp /var/tmp /var/db/portsnap /var/db/freebsd-update"} # unwanted directories
55214613Swollman: ${PRUNEDIRS=".zfs"}	# unwanted directories, in any parent
56214615Swollman: ${FILESYSTEMS="$(lsvfs | tail -n +3 | \
57172718Sse	egrep -vw "loopback|network|synthetic|read-only|0" | \
58172718Sse	cut -d " " -f1)"}		# allowed filesystems
5934312Swosch: ${find:=find}
6012155Snate
61214615Swollmanif [ -z "$SEARCHPATHS" ]; then
62214615Swollman	echo "$0: empty variable SEARCHPATHS" >&2; exit 1
63214615Swollmanfi
64214615Swollmanif [ -z "$FILESYSTEMS" ]; then
65214615Swollman	echo "$0: empty variable FILESYSTEMS" >&2; exit 1
66214615Swollmanfi
6717592Swosch
6817592Swosch# Make a list a paths to exclude in the locate run
6917592Swoschexcludes="! (" or=""
7017592Swoschfor fstype in $FILESYSTEMS
7117592Swoschdo
7217592Swosch       excludes="$excludes $or -fstype $fstype"
7317592Swosch       or="-or"
7417592Swoschdone
7517592Swoschexcludes="$excludes ) -prune"
7617592Swosch
77214615Swollmanif [ -n "$PRUNEPATHS" ]; then
78214615Swollman	for path in $PRUNEPATHS; do 
7917592Swosch		excludes="$excludes -or -path $path -prune"
80214615Swollman	done
81214615Swollmanfi
8217592Swosch
83214615Swollmanif [ -n "$PRUNEDIRS" ]; then
84214615Swollman	for dir in $PRUNEDIRS; do
85214583Swollman		excludes="$excludes -or -name $dir -type d -prune"
86214615Swollman	done
87214615Swollmanfi
88214583Swollman
8918895Swoschtmp=$TMPDIR/_updatedb$$
9041413Swoschtrap 'rm -f $tmp; rmdir $TMPDIR' 0 1 2 3 5 10 15
9117592Swosch		
9217592Swosch# search locally
9341413Swoschif $find -s $SEARCHPATHS $excludes -or -print 2>/dev/null |
9441413Swosch        $mklocatedb -presort > $tmp
9517592Swoschthen
96214615Swollman	if [ -n "$($find $tmp -size -257c -print)" ]; then
97214615Swollman		echo "updatedb: locate database $tmp is empty" >&2
98214615Swollman		exit 1
99214615Swollman	else
100214615Swollman		cat $tmp > $FCODES		# should be cp?
101214615Swollman	fi
10212155Snatefi
103