=================================================================== RCS file: /cvs/mandoc/mandocdb.c,v retrieving revision 1.248 retrieving revision 1.250 diff -u -p -r1.248 -r1.250 --- mandoc/mandocdb.c 2017/05/05 13:17:55 1.248 +++ mandoc/mandocdb.c 2017/05/17 22:27:12 1.250 @@ -1,4 +1,4 @@ -/* $Id: mandocdb.c,v 1.248 2017/05/05 13:17:55 schwarze Exp $ */ +/* $Id: mandocdb.c,v 1.250 2017/05/17 22:27:12 schwarze Exp $ */ /* * Copyright (c) 2011, 2012 Kristaps Dzonsons * Copyright (c) 2011-2017 Ingo Schwarze @@ -302,7 +302,6 @@ static const struct mdoc_handler __mdocs[MDOC_MAX - MD { NULL, 0, 0 }, /* En */ { NULL, TYPE_Dx, NODE_NOSRC }, /* Dx */ { NULL, 0, 0 }, /* %Q */ - { NULL, 0, 0 }, /* sp */ { NULL, 0, 0 }, /* %U */ { NULL, 0, 0 }, /* Ta */ }; @@ -2123,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 +2147,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) {