diff options
author | Ingo Schwarze <schwarze@openbsd.org> | 2013-09-17 23:07:22 +0000 |
---|---|---|
committer | Ingo Schwarze <schwarze@openbsd.org> | 2013-09-17 23:07:22 +0000 |
commit | c05a768e438fa796c548e24cc6f92cc3077084b0 (patch) | |
tree | 606bbd21ee5453a60be33f9ee54f9c18bdd57360 /mandocdb.c | |
parent | 1afa3fd996caf2d462ebca9abb86703784c01b40 (diff) | |
download | mandoc-c05a768e438fa796c548e24cc6f92cc3077084b0.tar.gz |
Merge OpenBSD rev. 1.34.
Do not truncate the production database when starting to build a new one.
Suggested by deraadt@.
Diffstat (limited to 'mandocdb.c')
-rw-r--r-- | mandocdb.c | 65 |
1 files changed, 52 insertions, 13 deletions
@@ -495,19 +495,38 @@ main(int argc, char *argv[]) exit((int)MANDOCLEVEL_SYSERR); } - strlcpy(mdb.dbn, MANDOC_DB, PATH_MAX); - strlcpy(mdb.idxn, MANDOC_IDX, PATH_MAX); - - flags = O_CREAT | O_TRUNC | O_RDWR; - mdb.db = dbopen(mdb.dbn, flags, 0644, DB_BTREE, &info); - mdb.idx = dbopen(mdb.idxn, flags, 0644, DB_RECNO, NULL); - - if (NULL == mdb.db) { - perror(mdb.dbn); - exit((int)MANDOCLEVEL_SYSERR); - } else if (NULL == mdb.idx) { - perror(mdb.idxn); - exit((int)MANDOCLEVEL_SYSERR); + /* Create a new database in two temporary files. */ + + flags = O_CREAT | O_EXCL | O_RDWR; + while (NULL == mdb.db) { + strlcpy(mdb.dbn, MANDOC_DB, PATH_MAX); + strlcat(mdb.dbn, ".XXXXXXXXXX", PATH_MAX); + if (NULL == mktemp(mdb.dbn)) { + perror(mdb.dbn); + exit((int)MANDOCLEVEL_SYSERR); + } + mdb.db = dbopen(mdb.dbn, flags, 0644, + DB_BTREE, &info); + if (NULL == mdb.db && EEXIST != errno) { + perror(mdb.dbn); + exit((int)MANDOCLEVEL_SYSERR); + } + } + while (NULL == mdb.idx) { + strlcpy(mdb.idxn, MANDOC_IDX, PATH_MAX); + strlcat(mdb.idxn, ".XXXXXXXXXX", PATH_MAX); + if (NULL == mktemp(mdb.idxn)) { + perror(mdb.idxn); + unlink(mdb.dbn); + exit((int)MANDOCLEVEL_SYSERR); + } + mdb.idx = dbopen(mdb.idxn, flags, 0644, + DB_RECNO, NULL); + if (NULL == mdb.idx && EEXIST != errno) { + perror(mdb.idxn); + unlink(mdb.dbn); + exit((int)MANDOCLEVEL_SYSERR); + } } /* @@ -527,6 +546,26 @@ main(int argc, char *argv[]) (*mdb.idx->close)(mdb.idx); mdb.db = NULL; mdb.idx = NULL; + + /* + * Replace the old database with the new one. + * This is not perfectly atomic, + * but i cannot think of a better way. + */ + + if (-1 == rename(mdb.dbn, MANDOC_DB)) { + perror(MANDOC_DB); + unlink(mdb.dbn); + unlink(mdb.idxn); + exit((int)MANDOCLEVEL_SYSERR); + } + if (-1 == rename(mdb.idxn, MANDOC_IDX)) { + perror(MANDOC_IDX); + unlink(MANDOC_DB); + unlink(MANDOC_IDX); + unlink(mdb.idxn); + exit((int)MANDOCLEVEL_SYSERR); + } } out: |