=================================================================== RCS file: /cvs/mandoc/mandocdb.c,v retrieving revision 1.49.2.2 retrieving revision 1.49.2.11 diff -u -p -r1.49.2.2 -r1.49.2.11 --- mandoc/mandocdb.c 2013/09/17 22:48:53 1.49.2.2 +++ mandoc/mandocdb.c 2014/01/05 21:30:57 1.49.2.11 @@ -1,4 +1,4 @@ -/* $Id: mandocdb.c,v 1.49.2.2 2013/09/17 22:48:53 schwarze Exp $ */ +/* $Id: mandocdb.c,v 1.49.2.11 2014/01/05 21:30:57 schwarze Exp $ */ /* * Copyright (c) 2011, 2012 Kristaps Dzonsons * Copyright (c) 2011, 2012 Ingo Schwarze @@ -34,12 +34,19 @@ #include #include -#if defined(__linux__) +#if defined(__APPLE__) +# include +#elif defined(__linux__) # include +#elif defined(__sun) +# include +# include +#else +# include +#endif + +#if defined(__linux__) || defined(__sun) # include -#elif defined(__APPLE__) -# include -# include #else # include #endif @@ -281,6 +288,7 @@ static const struct mdoc_handler mdocs[MDOC_MAX] = { }; static const char *progname; +static int quick; /* abort the parse early */ static int use_all; /* Use all directories and files. */ static int verb; /* Output verbosity level. */ static int warnings; /* Potential problems in manuals. */ @@ -299,7 +307,7 @@ main(int argc, char *argv[]) int ch, i, flags; DB *hash; /* temporary keyword hashtable */ BTREEINFO info; /* btree configuration */ - size_t sz1, sz2; + size_t sz1, sz2, ipath; struct buf buf, /* keyword buffer */ dbuf; /* description buffer */ struct of *of; /* list of files for processing */ @@ -322,7 +330,7 @@ main(int argc, char *argv[]) op = OP_DEFAULT; dir = NULL; - while (-1 != (ch = getopt(argc, argv, "aC:d:tu:vW"))) + while (-1 != (ch = getopt(argc, argv, "aC:d:Qtu:vW"))) switch (ch) { case ('a'): use_all = 1; @@ -345,6 +353,9 @@ main(int argc, char *argv[]) dir = optarg; op = OP_UPDATE; break; + case ('Q'): + quick = 1; + break; case ('t'): dup2(STDOUT_FILENO, STDERR_FILENO); if (op) { @@ -387,7 +398,8 @@ main(int argc, char *argv[]) info.lorder = 4321; info.flags = R_DUP; - mp = mparse_alloc(MPARSE_AUTO, MANDOCLEVEL_FATAL, NULL, NULL, NULL); + mp = mparse_alloc(MPARSE_AUTO, + MANDOCLEVEL_FATAL, NULL, NULL, quick); memset(&buf, 0, sizeof(struct buf)); memset(&dbuf, 0, sizeof(struct buf)); @@ -482,7 +494,7 @@ main(int argc, char *argv[]) } else manpath_parse(&dirs, dir, NULL, NULL); - for (i = 0; i < dirs.sz; i++) { + for (ipath = 0; ipath < dirs.sz; ipath++) { /* * Go to the root of the respective manual tree. @@ -490,25 +502,44 @@ main(int argc, char *argv[]) * They are indexed relative to the root. */ - if (-1 == chdir(dirs.paths[i])) { - perror(dirs.paths[i]); + if (-1 == chdir(dirs.paths[ipath])) { + perror(dirs.paths[ipath]); exit((int)MANDOCLEVEL_SYSERR); } - strlcpy(mdb.dbn, MANDOC_DB, PATH_MAX); - strlcpy(mdb.idxn, MANDOC_IDX, PATH_MAX); + /* Create a new database in two temporary files. */ - 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); + 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); + } + } /* * Search for manuals and fill the new database. @@ -527,6 +558,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: @@ -549,7 +600,7 @@ out: usage: fprintf(stderr, - "usage: %s [-avvv] [-C file] | dir ... | -t file ...\n" + "usage: %s [-aQvvv] [-C file] | dir ... | -t file ...\n" " -d dir [file ...] | " "-u dir [file ...]\n", progname); @@ -576,6 +627,8 @@ index_merge(const struct of *of, struct mparse *mp, uint64_t vbuf[2]; char type; + static char emptystring[] = ""; + if (warnings) { files = NULL; hash_reset(&files); @@ -688,13 +741,13 @@ index_merge(const struct of *of, struct mparse *mp, } buf_appendb(buf, ")", 2); for (p = buf->cp; '\0' != *p; p++) - *p = tolower(*p); + *p = tolower((unsigned char)*p); key.data = buf->cp; key.size = buf->len; val.data = NULL; val.size = 0; if (0 == skip) - val.data = ""; + val.data = emptystring; else { ch = (*files->get)(files, &key, &val, 0); if (ch < 0) { @@ -1708,7 +1761,7 @@ ofile_argbuild(int argc, char *argv[], struct of **of, * Recursively build up a list of files to parse. * We use this instead of ftw() and so on because I don't want global * variables hanging around. - * This ignores the mandocdb.db and mandocdb.index files, but assumes that + * This ignores the mandoc.db and mandoc.index files, but assumes that * everything else is a manual. * Pass in a pointer to a NULL structure for the first invocation. */ @@ -1717,6 +1770,9 @@ ofile_dirbuild(const char *dir, const char* psec, cons int p_src_form, struct of **of) { char buf[PATH_MAX]; +#if defined(__sun) + struct stat sb; +#endif size_t sz; DIR *d; const char *fn, *sec, *arch; @@ -1739,7 +1795,12 @@ ofile_dirbuild(const char *dir, const char* psec, cons src_form = p_src_form; +#if defined(__sun) + stat(dp->d_name, &sb); + if (S_IFDIR & sb.st_mode) { +#else if (DT_DIR == dp->d_type) { +#endif sec = psec; arch = parch; @@ -1796,7 +1857,11 @@ ofile_dirbuild(const char *dir, const char* psec, cons continue; } +#if defined(__sun) + if (0 == S_IFREG & sb.st_mode) { +#else if (DT_REG != dp->d_type) { +#endif if (warnings) fprintf(stderr, "%s/%s: not a regular file\n",