1#!/bin/sh
2#
3#	$OpenBSD: updatedb.sh,v 1.17 2019/09/10 18:20:07 millert Exp $
4#
5# Copyright (c) September 1995 Wolfram Schneider <wosch@FreeBSD.org>. Berlin.
6# All rights reserved.
7#
8# Redistribution and use in source and binary forms, with or without
9# modification, are permitted provided that the following conditions
10# are met:
11# 1. Redistributions of source code must retain the above copyright
12#    notice, this list of conditions and the following disclaimer.
13# 2. Redistributions in binary form must reproduce the above copyright
14#    notice, this list of conditions and the following disclaimer in the
15#    documentation and/or other materials provided with the distribution.
16#
17# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27# SUCH DAMAGE.
28#
29# updatedb - update locate database for local mounted filesystems
30#
31
32LOCATE_CONFIG="/etc/locate.rc"
33if [ -f "$LOCATE_CONFIG" -a -r "$LOCATE_CONFIG" ]; then
34       . $LOCATE_CONFIG
35fi
36
37# The directory containing locate subprograms
38: ${LIBEXECDIR=/usr/libexec}; export LIBEXECDIR
39TMPDIR=${TMPDIR:-/tmp}; export TMPDIR;
40
41PATH=$LIBEXECDIR:/bin:/usr/bin:$PATH; export PATH
42
43USAGE="usage: $0 [--fcodes=dbfile] [--filesystems='type ...'] [--prunepaths='dir ...'] [--searchpaths='dir ...'] [--tmpdir=dir]"
44
45: ${mklocatedb=locate.mklocatedb}	 # make locate database program
46: ${FCODES=/var/db/locate.database}	 # the database
47: ${SEARCHPATHS="/"}			 # directories to be put in the database
48: ${PRUNEPATHS="/tmp /var/tmp"}		 # unwanted directories
49: ${FILESYSTEMS="ffs ufs"}		 # allowed filesystems 
50: ${find=find}
51
52# Command line args override rc file and defaults
53while test $# != 0; do
54	option=`echo $1 | sed 's/^\([^=]*\).*$/\1/'`
55	optarg=`echo $1 | sed 's/^[^=]*=\(.*$\)/\1/'`
56
57	# All options take an argument
58	if [ "$option" = "$optarg" ]; then
59		echo "$USAGE"
60		exit 1
61	fi
62
63	case "$option" in
64		--tmpdir) TMPDIR="$optarg";;
65		--fcodes) FCODES="$optarg";;
66		--searchpaths) SEARCHPATHS="$optarg";;
67		--prunepaths) PRUNEPATHS="$optarg";;
68		--filesystems) FILESYSTEMS="$optarg";;
69	esac
70	shift
71done
72
73if [ "${FCODES}" != "-" ]; then
74	FCODESDIR=$( dirname "${FCODES}" )
75	if [ ! -w "${FCODESDIR}" ]; then
76		echo "$0: no permission to create $FCODES"
77		exit 1
78	fi
79fi
80
81case X"$SEARCHPATHS" in 
82	X) echo "$0: empty variable SEARCHPATHS"; exit 1;; esac
83case X"$FILESYSTEMS" in 
84	X) echo "$0: empty variable FILESYSTEMS"; exit 1;; esac
85
86# Make a list a paths to exclude in the locate run
87excludes="! (" or=""
88for fstype in $FILESYSTEMS
89do
90       excludes="$excludes $or -fstype $fstype"
91       or="-or"
92done
93excludes="$excludes ) -prune"
94
95case X"$PRUNEPATHS" in
96	X) ;;
97	*) for path in $PRUNEPATHS
98           do 
99		excludes="$excludes -or -path $path -prune"
100	   done;;
101esac
102
103tmp=`mktemp ${TMPDIR=/tmp}/_updatedb.XXXXXXXXXX` || exit 1
104trap 'rm -rf $tmp' 0 1 2 3 5 10 15
105
106# search locally
107# echo $find $SEARCHPATHS $excludes -or -print && exit
108if $find $SEARCHPATHS $excludes -or -print 2>/dev/null | 
109        $mklocatedb > $tmp
110then
111	case X"`$find $tmp -size -257c -print`" in
112		X) if [ "$FCODES" = "-" ]; then
113			cat $tmp
114		   else
115			cat $tmp > $FCODES
116		   fi;;
117		*) echo "updatedb: locate database $tmp is empty"
118		   exit 1
119	esac
120fi
121