summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIngo Schwarze <schwarze@cvs.openbsd.org>2017-05-17 22:26:53 +0000
committerIngo Schwarze <schwarze@cvs.openbsd.org>2017-05-17 22:26:53 +0000
commita16514a9ed5573973999fae16e374e5124f1ab2d (patch)
treebffdae326974afb1781736c5e379c349974f550b
parentffb688096dbe142fb1488b6511539759f224aa08 (diff)
Never create empty databases.
When pkg_add(1)ing packages installing manual pages into some directory, the database in that directory automatically gets created or updated, no change so far. This patch causes the database file to be automatically unlinked when pkg_delete(1)ing the last package having manual pages in that directory, to leave less cruft behind. Suggested by ajacoutot@.
-rw-r--r--usr.bin/mandoc/makewhatis.87
-rw-r--r--usr.bin/mandoc/mandocdb.c24
2 files changed, 28 insertions, 3 deletions
diff --git a/usr.bin/mandoc/makewhatis.8 b/usr.bin/mandoc/makewhatis.8
index abc44b6dea7..c42bcc490fd 100644
--- a/usr.bin/mandoc/makewhatis.8
+++ b/usr.bin/mandoc/makewhatis.8
@@ -1,4 +1,4 @@
-.\" $OpenBSD: makewhatis.8,v 1.13 2017/03/18 19:50:58 schwarze Exp $
+.\" $OpenBSD: makewhatis.8,v 1.14 2017/05/17 22:26:52 schwarze Exp $
.\"
.\" Copyright (c) 2011, 2012 Kristaps Dzonsons <kristaps@bsd.lv>
.\" Copyright (c) 2011, 2012, 2014, 2017 Ingo Schwarze <schwarze@openbsd.org>
@@ -15,7 +15,7 @@
.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
.\"
-.Dd $Mdocdate: March 18 2017 $
+.Dd $Mdocdate: May 17 2017 $
.Dt MAKEWHATIS 8
.Os
.Sh NAME
@@ -74,6 +74,8 @@ and
.Sm on
in that directory.
Existing databases are replaced.
+If a directory contains no manual pages, no database is created in that
+directory.
If
.Ar dir
is not provided,
@@ -130,6 +132,7 @@ Remove
.Ar
from the database in
.Ar dir .
+If that causes the database to become empty, also delete the database file.
.El
.Pp
If fatal parse errors are encountered while parsing, the offending file
diff --git a/usr.bin/mandoc/mandocdb.c b/usr.bin/mandoc/mandocdb.c
index 6763f1f200a..8e44629b2ad 100644
--- a/usr.bin/mandoc/mandocdb.c
+++ b/usr.bin/mandoc/mandocdb.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: mandocdb.c,v 1.198 2017/05/05 15:16:25 schwarze Exp $ */
+/* $OpenBSD: mandocdb.c,v 1.199 2017/05/17 22:26:52 schwarze Exp $ */
/*
* Copyright (c) 2011, 2012 Kristaps Dzonsons <kristaps@bsd.lv>
* Copyright (c) 2011-2017 Ingo Schwarze <schwarze@openbsd.org>
@@ -2082,6 +2082,23 @@ dbwrite(struct dba *dba)
int status;
pid_t child;
+ /*
+ * Do not write empty databases, and delete existing ones
+ * when makewhatis -u causes them to become empty.
+ */
+
+ dba_array_start(dba->pages);
+ if (dba_array_next(dba->pages) == NULL) {
+ if (unlink(MANDOC_DB) == -1)
+ say(MANDOC_DB, "&unlink");
+ return;
+ }
+
+ /*
+ * Build the database in a temporary file,
+ * then atomically move it into place.
+ */
+
if (dba_write(MANDOC_DB "~", dba) != -1) {
if (rename(MANDOC_DB "~", MANDOC_DB) == -1) {
exitcode = (int)MANDOCLEVEL_SYSERR;
@@ -2091,6 +2108,11 @@ dbwrite(struct dba *dba)
return;
}
+ /*
+ * We lack write permission and cannot replace the database
+ * file, but let's at least check whether the data changed.
+ */
+
(void)strlcpy(tfn, "/tmp/mandocdb.XXXXXXXX", sizeof(tfn));
if (mkdtemp(tfn) == NULL) {
exitcode = (int)MANDOCLEVEL_SYSERR;