git: 1a556fa3ec8d - main - sysutils/plocate: Fast & privacy-respecting locate utility

From: Robert Clausecker <fuz_at_FreeBSD.org>
Date: Thu, 18 May 2023 08:20:37 UTC
The branch main has been updated by fuz:

URL: https://cgit.FreeBSD.org/ports/commit/?id=1a556fa3ec8dbea28a7b34b2e9e2f036b0276ad6

commit 1a556fa3ec8dbea28a7b34b2e9e2f036b0276ad6
Author:     Stefan `Sec` Zehl <sec@42.org>
AuthorDate: 2023-05-14 08:26:17 +0000
Commit:     Robert Clausecker <fuz@FreeBSD.org>
CommitDate: 2023-05-18 08:19:45 +0000

    sysutils/plocate: Fast & privacy-respecting locate utility
    
    plocate is a locate(1) based on posting lists, completely replacing mlocate
    with a much faster (and smaller) index. It is suitable as a default locate on
    your system.  Like mlocate and slocate, the returned file set is
    user-dependent, ie. a user will only see a file if find(1) would list it (all
    directories from the root have +rx permissions).
    
    WWW: https://plocate.sesse.net/
    
    PR:             270657
---
 GIDs                                             |   2 +-
 sysutils/Makefile                                |   1 +
 sysutils/plocate/Makefile                        |  34 +++++
 sysutils/plocate/distinfo                        |   3 +
 sysutils/plocate/files/315.plocate.in            |  32 ++++
 sysutils/plocate/files/mntent.h                  |  62 ++++++++
 sysutils/plocate/files/mntent_compat.c++         | 177 +++++++++++++++++++++++
 sysutils/plocate/files/patch-conf.cpp            |  11 ++
 sysutils/plocate/files/patch-io__uring__engine.h |  10 ++
 sysutils/plocate/files/patch-meson.build         |  56 +++++++
 sysutils/plocate/files/patch-updatedb.cpp        |  11 ++
 sysutils/plocate/files/updatedb.conf.sample      |   1 +
 sysutils/plocate/pkg-descr                       |   5 +
 sysutils/plocate/pkg-plist                       |   9 ++
 14 files changed, 413 insertions(+), 1 deletion(-)

diff --git a/GIDs b/GIDs
index d6880befb380..141ef8ef9ffd 100644
--- a/GIDs
+++ b/GIDs
@@ -801,7 +801,7 @@ opensearch:*:855:
 # free: 857
 # free: 858
 # free: 859
-# free: 860
+plocate:*:860:
 # free: 861
 # free: 862
 # free: 863
diff --git a/sysutils/Makefile b/sysutils/Makefile
index da2b7f28b9b5..77f732fd8cf3 100644
--- a/sysutils/Makefile
+++ b/sysutils/Makefile
@@ -1032,6 +1032,7 @@
     SUBDIR += plasma5-powerdevil
     SUBDIR += plasma5-systemsettings
     SUBDIR += plconfig
+    SUBDIR += plocate
     SUBDIR += pmt
     SUBDIR += pnscan
     SUBDIR += podman
diff --git a/sysutils/plocate/Makefile b/sysutils/plocate/Makefile
new file mode 100644
index 000000000000..034a86cd6954
--- /dev/null
+++ b/sysutils/plocate/Makefile
@@ -0,0 +1,34 @@
+PORTNAME=	plocate
+DISTVERSION=	1.1.18
+CATEGORIES=	sysutils
+MASTER_SITES=	https://plocate.sesse.net/download/
+
+MAINTAINER=	sec@42.org
+COMMENT=	Fast & privacy-respecting locate utility
+WWW=		https://plocate.sesse.net/
+
+LICENSE=	GPLv2+
+LICENSE_FILE=	${WRKSRC}/COPYING
+
+BUILD_DEPENDS=	meson:devel/meson
+LIB_DEPENDS=	libzstd.so:archivers/zstd
+
+USES=		meson pkgconfig
+MESON_ARGS=	--sharedstatedir=/var/db
+
+SUB_FILES=	315.plocate
+
+GROUPS=		plocate
+
+post-patch:
+	${CP} ${FILESDIR}/mntent_compat.c++ ${FILESDIR}/mntent.h ${WRKSRC}
+
+post-install:
+	@${MKDIR} ${STAGEDIR}${PREFIX}/etc/periodic/daily
+	${INSTALL_SCRIPT} ${WRKDIR}/315.plocate \
+		${STAGEDIR}${PREFIX}/etc/periodic/daily
+	${INSTALL_DATA} ${FILESDIR}/updatedb.conf.sample \
+		${STAGEDIR}${PREFIX}/etc
+	${MKDIR} ${STAGEDIR}/var/db/plocate
+
+.include <bsd.port.mk>
diff --git a/sysutils/plocate/distinfo b/sysutils/plocate/distinfo
new file mode 100644
index 000000000000..9e8a1adee1e9
--- /dev/null
+++ b/sysutils/plocate/distinfo
@@ -0,0 +1,3 @@
+TIMESTAMP = 1680026625
+SHA256 (plocate-1.1.18.tar.gz) = 939657050b70719b01ce04fc1c8b64270062a0d53da2c72eafbe036a1964e12c
+SIZE (plocate-1.1.18.tar.gz) = 73906
diff --git a/sysutils/plocate/files/315.plocate.in b/sysutils/plocate/files/315.plocate.in
new file mode 100644
index 000000000000..d02f915ba684
--- /dev/null
+++ b/sysutils/plocate/files/315.plocate.in
@@ -0,0 +1,32 @@
+#!/bin/sh -
+#
+# $FreeBSD$
+#
+
+# If there is a global system configuration file, suck it in.
+#
+if [ -r /etc/defaults/periodic.conf ]
+then
+    . /etc/defaults/periodic.conf
+    source_periodic_confs
+fi
+
+case "$daily_plocate_enable" in
+    [Yy][Ee][Ss])
+	echo ""
+	echo "Rebuilding plocate database:"
+
+	SBINDIR=%%PREFIX%%/sbin
+	LOCATEGROUP=plocate
+	DBFILE=/var/db/plocate/plocate.db
+
+	touch $DBFILE && rc=0 || rc=3
+
+	cd /
+	nice -n 5 $SBINDIR/updatedb
+    ;;
+
+    *)  rc=0;;
+esac
+
+exit $rc
diff --git a/sysutils/plocate/files/mntent.h b/sysutils/plocate/files/mntent.h
new file mode 100644
index 000000000000..e61cddd89c82
--- /dev/null
+++ b/sysutils/plocate/files/mntent.h
@@ -0,0 +1,62 @@
+/*
+ *  mntent
+ *  mntent.h - compatability header for FreeBSD
+ *
+ *  Copyright (c) 2001 David Rufino <daverufino@btinternet.com>
+ *  All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#if defined(HAVE_MNTENT_H)
+#include <mntent.h>
+#else
+#ifndef _MNTENT_H
+#define _MNTENT_H
+#include <stdio.h>
+
+#define MOUNTED "dummy"
+
+#define MNTTYPE_NFS "nfs"
+
+struct mntent {
+	char *mnt_fsname;
+	char *mnt_dir;
+	char *mnt_type;
+	char *mnt_opts;
+	int mnt_freq;
+	int mnt_passno;
+};
+
+#define setmntent(x,y) ((FILE *)0x1)
+#ifdef __cplusplus
+extern "C" {
+#endif
+struct mntent *getmntent __P ((FILE *fp));
+char *hasmntopt __P ((const struct mntent *mnt, const char *option));
+#ifdef __cplusplus
+}; // extern "C"
+#endif
+#define endmntent(x) ((int)1)
+
+#endif /* _MNTENT_H */
+#endif /* HAVE_MNTENT_H */
diff --git a/sysutils/plocate/files/mntent_compat.c++ b/sysutils/plocate/files/mntent_compat.c++
new file mode 100644
index 000000000000..4ca1a55c8ffe
--- /dev/null
+++ b/sysutils/plocate/files/mntent_compat.c++
@@ -0,0 +1,177 @@
+/*
+ * Copyright (c) 1980, 1989, 1993, 1994
+ *      The Regents of the University of California.  All rights reserved.
+ * Copyright (c) 2001
+ *      David Rufino <daverufino@btinternet.com>
+ * Copyright (c) 2006
+ *      Stanislav Sedov <ssedov@mbsd.msk.ru>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *      This product includes software developed by the University of
+ *      California, Berkeley and its contributors.
+ * 4. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+/* most of this was ripped from the mount(3) source */
+
+//#include "config.h"
+#include "mntent.h"
+#include <stdlib.h>
+#include <string.h>
+#include <sys/param.h>
+#include <sys/ucred.h>
+#include <sys/mount.h>
+
+static int pos = -1;
+static int mntsize = -1;
+static struct mntent _mntent;
+
+struct {
+	int		m_flag;
+	const char	*m_option;
+} mntoptions[] = {
+	{ MNT_ASYNC,		"async" },
+	{ MNT_NOATIME,		"noatime"},
+	{ MNT_NOEXEC,		"noexec"},
+	{ MNT_NOSUID,		"nosuid"},
+	{ MNT_NOSYMFOLLOW,	"nosymfollow"},
+	{ MNT_SYNCHRONOUS,	"sync"},
+	{ MNT_UNION,		"union"},
+	{ MNT_NOCLUSTERR,	"noclusterr"},
+	{ static_cast<int>(MNT_NOCLUSTERW),	"noclusterw"},
+	{ MNT_SUIDDIR,		"suiddir"},
+#ifdef MNT_SNAPSHOT
+	{ MNT_SNAPSHOT,		"snapshot"},
+#endif
+#ifdef MNT_MULTILABEL
+	{ MNT_MULTILABEL,	"multilabel"},
+#endif
+#ifdef MNT_ACLS
+	{ MNT_ACLS,		"acls"},
+#endif
+#ifdef MNT_NODEV
+	{ MNT_NODEV,		"nodev"},
+#endif
+};
+
+#define N_OPTS (sizeof(mntoptions) / sizeof(*mntoptions))
+
+
+extern "C" {
+
+char *
+hasmntopt (const struct mntent *mnt, const char *option)
+{
+	char *opt, *optbuf;
+
+	optbuf = strdup(mnt->mnt_opts);
+	for (opt = optbuf; (opt = strtok(opt, " ")) != NULL; opt = NULL) {
+		if (!strcasecmp(opt, option)) {
+			opt = opt - optbuf + mnt->mnt_opts;
+			free (optbuf);
+			return (opt);
+		}
+	}
+	free (optbuf);
+	return (NULL);
+}
+
+static char *
+catopt (char *s0, const char *s1)
+{
+	size_t newlen;
+	char *cp;
+
+	if (s1 == NULL || *s1 == '\0')
+		return s0;
+
+	if (s0 != NULL) {
+		newlen = strlen(s0) + strlen(s1) + 1 + 1;
+		if ((cp = (char *)realloc(s0, newlen)) == NULL)
+			return (NULL);
+
+		(void)strcat(cp, " ");
+		(void)strcat(cp, s1);
+	} else
+		cp = strdup(s1);
+
+	return (cp);
+}
+
+
+static char *
+flags2opts (int flags)
+{
+	char *res = NULL;
+	int i;
+
+	res = catopt(res, (flags & MNT_RDONLY) ? "ro" : "rw");
+
+	for (i = 0; i < N_OPTS; i++)
+		if (flags & mntoptions[i].m_flag)
+			res = catopt(res, mntoptions[i].m_option);
+	return res;
+}
+
+static struct mntent *
+statfs_to_mntent (struct statfs *mntbuf)
+{
+	static char opts_buf[40], *tmp;
+	
+	_mntent.mnt_fsname = mntbuf->f_mntfromname;
+	_mntent.mnt_dir = mntbuf->f_mntonname;
+	_mntent.mnt_type = mntbuf->f_fstypename;
+	tmp = flags2opts (mntbuf->f_flags);
+	if (tmp) {
+		opts_buf[sizeof(opts_buf) - 1] = '\0';
+		strncpy (opts_buf, tmp, sizeof(opts_buf)-1);
+		free (tmp);
+	} else {
+		*opts_buf = '\0';
+	}
+	_mntent.mnt_opts = opts_buf;	
+	_mntent.mnt_freq = _mntent.mnt_passno = 0;
+	return (&_mntent);
+}
+
+struct mntent *
+getmntent (FILE *fp)
+{
+	static struct statfs *mntbuf;
+
+	if (pos == -1 || mntsize == -1)
+		mntsize = getmntinfo (&mntbuf, MNT_NOWAIT);
+
+	++pos;
+	if (pos == mntsize) {
+		pos = mntsize = -1;
+		return (NULL);
+	}
+
+	return (statfs_to_mntent (&mntbuf[pos]));
+}
+
+}; // extern "C"
diff --git a/sysutils/plocate/files/patch-conf.cpp b/sysutils/plocate/files/patch-conf.cpp
new file mode 100644
index 000000000000..019bf4b1ab96
--- /dev/null
+++ b/sysutils/plocate/files/patch-conf.cpp
@@ -0,0 +1,11 @@
+--- conf.cpp.orig	2023-05-04 07:34:30 UTC
++++ conf.cpp
+@@ -35,6 +35,8 @@ any later version.
+ #include <string.h>
+ #include <unistd.h>
+ 
++#define program_invocation_name getprogname()
++
+ using namespace std;
+ 
+ /* true if locate(1) should check whether files are visible before reporting
diff --git a/sysutils/plocate/files/patch-io__uring__engine.h b/sysutils/plocate/files/patch-io__uring__engine.h
new file mode 100644
index 000000000000..6ae751285cf6
--- /dev/null
+++ b/sysutils/plocate/files/patch-io__uring__engine.h
@@ -0,0 +1,10 @@
+--- io_uring_engine.h.orig	2023-05-04 07:33:59 UTC
++++ io_uring_engine.h
+@@ -7,7 +7,6 @@
+ #include <string_view>
+ #include <sys/socket.h>
+ #include <sys/types.h>
+-#include <linux/stat.h>
+ 
+ struct io_uring_sqe;
+ #ifndef WITHOUT_URING
diff --git a/sysutils/plocate/files/patch-meson.build b/sysutils/plocate/files/patch-meson.build
new file mode 100644
index 000000000000..091386cdcfa8
--- /dev/null
+++ b/sysutils/plocate/files/patch-meson.build
@@ -0,0 +1,56 @@
+--- meson.build.orig	2023-05-04 07:34:20 UTC
++++ meson.build
+@@ -1,7 +1,7 @@
+ project('plocate', 'cpp', default_options: ['buildtype=debugoptimized','cpp_std=c++17'], version: '1.1.18')
+ 
+ add_project_arguments('-DGROUPNAME="' + get_option('locategroup') + '"', language: 'cpp')
+-add_project_arguments('-DUPDATEDB_CONF="/etc/updatedb.conf"', language: 'cpp')
++add_project_arguments('-DUPDATEDB_CONF= "' + get_option('prefix') + '/etc/updatedb.conf"', language: 'cpp')
+ dbfile = join_paths(get_option('sharedstatedir'), get_option('dbpath'))
+ add_project_arguments('-DDBFILE="' + dbfile + '"', language: 'cpp')
+ add_project_arguments('-DPACKAGE_NAME="plocate"', language: 'cpp')
+@@ -30,16 +30,12 @@ if cxx.compiles(code, name: 'function multiversioning'
+ 	add_project_arguments('-DHAS_FUNCTION_MULTIVERSIONING', language: 'cpp')
+ endif
+ 
+-executable('plocate', ['plocate.cpp', 'io_uring_engine.cpp', 'turbopfor.cpp', 'parse_trigrams.cpp', 'serializer.cpp', 'access_rx_cache.cpp', 'needle.cpp', 'complete_pread.cpp'],
++executable('plocate', ['plocate.cpp', 'io_uring_engine.cpp', 'turbopfor.cpp', 'parse_trigrams.cpp', 'serializer.cpp', 'access_rx_cache.cpp', 'needle.cpp', 'complete_pread.cpp', 'mntent_compat.c++'],
+ 	dependencies: [uringdep, zstddep, threaddep, atomicdep],
+ 	install: true,
+ 	install_mode: ['rwxr-sr-x', 'root', get_option('locategroup')])
+-executable('plocate-build', ['plocate-build.cpp', 'database-builder.cpp'],
+-	dependencies: [zstddep],
+-	install: true,
+-	install_dir: get_option('sbindir'))
+ updatedb_progname = get_option('updatedb_progname')
+-executable(updatedb_progname, ['updatedb.cpp', 'database-builder.cpp', 'conf.cpp', 'lib.cpp', 'bind-mount.cpp', 'complete_pread.cpp'],
++executable(updatedb_progname, ['updatedb.cpp', 'database-builder.cpp', 'conf.cpp', 'lib.cpp', 'bind-mount.cpp', 'complete_pread.cpp', 'mntent_compat.c++'],
+ 	dependencies: [zstddep, threaddep],
+ 	install: true,
+ 	install_dir: get_option('sbindir'))
+@@ -49,7 +45,7 @@ conf_data.set('PROCESSED_BY_MESON', '1')
+ conf_data.set('sbindir', join_paths(get_option('prefix'), get_option('sbindir')))
+ conf_data.set('groupname', get_option('locategroup'))
+ conf_data.set('dbfile', dbfile)
+-conf_data.set('updatedb_conf', '/etc/updatedb.conf')
++conf_data.set('updatedb_conf',get_option('prefix')+'/etc/updatedb.conf')
+ conf_data.set('updatedb_progname', updatedb_progname)
+ update_script = configure_file(input: 'update-plocate.sh',
+                output: 'update-plocate.sh',
+@@ -64,7 +60,6 @@ if get_option('install_cron')
+ 		rename: 'plocate')
+ endif
+ install_man('plocate.1')
+-install_man('plocate-build.8')
+ 
+ updatedb_man = configure_file(input: 'updatedb.8.in',
+                output: updatedb_progname + '.8',
+@@ -102,7 +97,7 @@ if run_command('[', '-r', pfordir + '/libic.a', ']', c
+ 	turbopfordep = declare_dependency(
+ 		include_directories: include_directories('TurboPFor-Integer-Compression'),
+ 		dependencies: meson.get_compiler('cpp').find_library('ic', dirs: pfordir))
+-	executable('bench', ['bench.cpp', 'io_uring_engine.cpp', 'turbopfor.cpp', 'complete_pread.cpp'],
++	executable('bench', ['bench.cpp', 'io_uring_engine.cpp', 'turbopfor.cpp', 'complete_pread.cpp', 'mntent_compat.c++'],
+ 		dependencies: [uringdep, turbopfordep],
+ 		build_by_default: false,
+ 		install: false)
diff --git a/sysutils/plocate/files/patch-updatedb.cpp b/sysutils/plocate/files/patch-updatedb.cpp
new file mode 100644
index 000000000000..65f5b4614d72
--- /dev/null
+++ b/sysutils/plocate/files/patch-updatedb.cpp
@@ -0,0 +1,11 @@
+--- updatedb.cpp.orig	2023-05-04 07:34:10 UTC
++++ updatedb.cpp
+@@ -44,7 +44,7 @@ any later version.
+ #include <memory>
+ #include <mntent.h>
+ #include <random>
+-#include <stdint.h>
++#include <cinttypes>
+ #include <stdio.h>
+ #include <stdlib.h>
+ #include <string.h>
diff --git a/sysutils/plocate/files/updatedb.conf.sample b/sysutils/plocate/files/updatedb.conf.sample
new file mode 100644
index 000000000000..9bbf44669cd5
--- /dev/null
+++ b/sysutils/plocate/files/updatedb.conf.sample
@@ -0,0 +1 @@
+PRUNEFS = "devfs fdescfs nullfs procfs linsysfs"
diff --git a/sysutils/plocate/pkg-descr b/sysutils/plocate/pkg-descr
new file mode 100644
index 000000000000..04e074d87025
--- /dev/null
+++ b/sysutils/plocate/pkg-descr
@@ -0,0 +1,5 @@
+plocate is a locate(1) based on posting lists, completely replacing mlocate
+with a much faster (and smaller) index. It is suitable as a default locate on
+your system.  Like mlocate and slocate, the returned file set is
+user-dependent, ie. a user will only see a file if find(1) would list it (all
+directories from the root have +rx permissions).
diff --git a/sysutils/plocate/pkg-plist b/sysutils/plocate/pkg-plist
new file mode 100644
index 000000000000..f6908d72dba0
--- /dev/null
+++ b/sysutils/plocate/pkg-plist
@@ -0,0 +1,9 @@
+@(,plocate,2755) bin/plocate
+sbin/updatedb
+man/man1/plocate.1.gz
+man/man8/updatedb.8.gz
+man/man5/updatedb.conf.5.gz
+etc/periodic/daily/315.plocate
+@sample etc/updatedb.conf.sample
+@dir /var/db/plocate
+/var/db/plocate/CACHEDIR.TAG