summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorIngo Schwarze <schwarze@openbsd.org>2017-05-17 22:27:12 +0000
committerIngo Schwarze <schwarze@openbsd.org>2017-05-17 22:27:12 +0000
commit9545095c2bf847cb8689d285805819c1e4c00502 (patch)
tree8109dbe5c1c1213c9f8ade302abf7202fc888d00
parentfbb27951be5b224a8e3f34ecf1a4750a3eaf2b3f (diff)
downloadmandoc-9545095c2bf847cb8689d285805819c1e4c00502.tar.gz
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--makewhatis.83
-rw-r--r--mandocdb.c22
2 files changed, 25 insertions, 0 deletions
diff --git a/makewhatis.8 b/makewhatis.8
index 080a9b79..cf675e89 100644
--- a/makewhatis.8
+++ b/makewhatis.8
@@ -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/mandocdb.c b/mandocdb.c
index 6990bbed..b74c31bd 100644
--- a/mandocdb.c
+++ b/mandocdb.c
@@ -2122,6 +2122,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;
@@ -2131,6 +2148,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;