Subject: Let "locate" search/index network volumes, too?
To: None <tech-userlevel@NetBSD.org>
From: Frederick Bruckman <fredb@immanent.net>
List: tech-userlevel
Date: 02/02/2004 08:52:26
Hi,

Below are my patches to make the locate database construction
configurable in "/etc/weekly.conf". I use this to index the files of
the nfs server on the faster workstation, where I'm normally logged
in. The settings look like this:

rebuild_locatedb=YES	locatedb_flags="-local +cd9660 +fdesc +kernfs +procfs"
			# locatedb_flags="-ffs -nfs"

the first being the normal default, the second being the point of all
this, though I've made the configuration mechanism general.

The logic goes, any file (or directory) that's *not* in a file system
listed with a "-" prefix is pruned (i.e not indexed), and any file or
directory that's in a file system given with a "+" prefix is pruned
as well. You can also limit the search to a non-root directory or
directories by giving arguments that don't start with "+" or "-".
(They should probably start with "/", anyway, so there's no loss of
generality, except that there's no support for paths with spaces.)

So, the script will index the intersection of the union of all the
specified paths, and the union of all the file systems specified with
"-", but excluding any files on file systems specified with "+". It
all sounds incredibly complicated, but I believe it simply does what
you'd expect.

I've taken care that there is no change to the default behaviour,
even if you forget to update "/etc/weekly".

The updated "updatedb.sh" runs under pdksh, in order to use its
nifty shell array feature. Should the name, then, be changed to
"updatedb.ksh"?

Would this be useful to anyone else but me? Comments, please.

Frederick


Index: usr.bin/locate/locate/updatedb.sh
===================================================================
RCS file: /cvsroot/src/usr.bin/locate/locate/updatedb.sh,v
retrieving revision 1.7
diff -u -r1.7 updatedb.sh
--- usr.bin/locate/locate/updatedb.sh	5 May 2002 07:27:35 -0000	1.7
+++ usr.bin/locate/locate/updatedb.sh	2 Feb 2004 04:16:17 -0000
@@ -1,4 +1,4 @@
-#!/bin/sh
+#!/bin/ksh
 #
 #	$NetBSD: updatedb.sh,v 1.7 2002/05/05 07:27:35 kim Exp $
 #
@@ -49,19 +49,42 @@
 trap "rm -f '$FILELIST'" EXIT
 trap "rm -f '$FILELIST'; exit 1" INT QUIT TERM

+# Sort arguments into directories and file systems, and process.
+
+set -A dir
+set -A fs_dash -- ! -inum 0		# always true
+set -A fs_plus
+while [ $# -ne 0 ]; do
+    if [ x"${1##-*}" = x ]; then
+	set -A fs_dash -- ${fs_dash[@]} -and ! -fstype ${1#-}
+    elif [ x"${1##+*}" = x ]; then
+	set -A fs_plus -- ${fs_plus[@]} -or -fstype ${1#+}
+    else
+	set -A dir -- ${dir[@]} ${1}
+    fi
+    shift
+done
+
+# Set defaults, if no arguments.
+
+if [ ${#dir[@]} -eq 0 ]; then
+    set -A dir -- $SRCHPATHS
+fi
+
+if [ ${#fs_dash[@]} -eq 3 -a ${#fs_plus[@]} -eq 0 ]; then
+    set -A fs_dash -- -and ! -fstype local
+    set -A fs_plus --		\
+	-or -fstype cd9660	\
+	-or -fstype fdesc	\
+	-or -fstype kernfs	\
+	-or -fstype procfs
+fi
+
 # Make a file list and compute common bigrams.
 # Entries of each directory shall be sorted (find -s).

-# search locally or everything
-# find -s $SRCHPATHS -print \
-find -s $SRCHPATHS \( \
-    ! -fstype local	\
-    -o -fstype cd9660	\
-    -o -fstype fdesc	\
-    -o -fstype kernfs	\
-    -o -fstype procfs	\
-    \) -a -prune -o -print \
-	>> "$FILELIST"
+find -s ${dir[@]} \( \( ${fs_dash[@]} \) ${fs_plus[@]} \) \
+    -and -prune -or -print > "$FILELIST"

 BIGRAMS=`$LIBDIR/locate.bigram <"$FILELIST"`

Index: etc/defaults/weekly.conf
===================================================================
RCS file: /cvsroot/src/etc/defaults/weekly.conf,v
retrieving revision 1.2
diff -u -r1.2 weekly.conf
--- etc/defaults/weekly.conf	8 Nov 2000 23:17:50 -0000	1.2
+++ etc/defaults/weekly.conf	2 Feb 2004 04:16:17 -0000
@@ -10,4 +10,5 @@
 #

 clean_uucp=YES
-rebuild_locatedb=YES
+rebuild_locatedb=YES	locatedb_flags="-local +cd9660 +fdesc +kernfs +procfs"
+			# locatedb_flags="-ffs -nfs"
Index: etc/weekly
===================================================================
RCS file: /cvsroot/src/etc/weekly,v
retrieving revision 1.17
diff -u -r1.17 weekly
--- etc/weekly	18 Jun 2001 10:54:02 -0000	1.17
+++ etc/weekly	2 Feb 2004 04:16:17 -0000
@@ -66,7 +66,8 @@
 		echo "Rebuilding locate database:"
 		chmod 644 /var/db/locate.database
 		chown nobody:nobody /var/db/locate.database
-		nice -5 su -m nobody -c /usr/libexec/locate.updatedb 2>/dev/null
+		nice -5 su -m nobody -c \
+		    "/usr/libexec/locate.updatedb $locatedb_flags" 2>/dev/null
 		chown root:wheel /var/db/locate.database
 	else
 		echo "Not rebuilding locate database; no /var/db/locate.database"