source@mandoc.bsd.lv
 help / color / mirror / Atom feed
* mdocml: Fix mandocdb(8) to pass over the type when pruning the database.
@ 2011-12-01 21:05 kristaps
  0 siblings, 0 replies; only message in thread
From: kristaps @ 2011-12-01 21:05 UTC (permalink / raw)
  To: source

Log Message:
-----------
Fix mandocdb(8) to pass over the type when pruning the database.  This
fixed `-d' perpetually adding the same files.  While here, clean up the
code and document it.  Remove -vv (complain if you want it back in).
Document the error messages in a DIAGNOSTICS section of mandocdb(8).

Modified Files:
--------------
    mdocml:
        mandocdb.8
        mandocdb.c

Revision Data
-------------
Index: mandocdb.8
===================================================================
RCS file: /usr/vhosts/mdocml.bsd.lv/cvs/mdocml/mandocdb.8,v
retrieving revision 1.9
retrieving revision 1.10
diff -Lmandocdb.8 -Lmandocdb.8 -u -p -r1.9 -r1.10
--- mandocdb.8
+++ mandocdb.8
@@ -86,9 +86,7 @@ from the database in
 .Ar dir
 without truncating it.
 .It Fl v
-Verbose operation.
-Use once to display all files added or removed and twice for keywords as
-well.
+Display all files added or removed to the index.
 .El
 .Pp
 If fatal parse errors are encountered while parsing, the offending file
@@ -229,6 +227,26 @@ Such errors cause
 .Nm
 to exit at once, possibly in the middle of parsing or formatting a file.
 The output databases are corrupt and should be removed .
+.El
+.Sh DIAGNOSTICS
+If the following errors occur, the
+.Nm
+databases should be rebuilt.
+.Bl -diag
+.It "%s: Corrupt database"
+The keyword database file indicated by
+.Pa %s
+is unreadable.
+.It "%s: Corrupt index"
+The index database file indicated by
+.Pa %s
+is unreadable.
+.It "%s: Path too long"
+The file
+.Pa %s
+is too long.
+This usually indicates database corruption or invalid command-line
+arguments.
 .El
 .Sh SEE ALSO
 .Xr man 1 ,
Index: mandocdb.c
===================================================================
RCS file: /usr/vhosts/mdocml.bsd.lv/cvs/mdocml/mandocdb.c,v
retrieving revision 1.17
retrieving revision 1.18
diff -Lmandocdb.c -Lmandocdb.c -u -p -r1.17 -r1.18
--- mandocdb.c
+++ mandocdb.c
@@ -635,9 +635,6 @@ index_merge(const struct of *of, struct 
 			val.size = sizeof(struct db_val);
 			val.data = &vbuf;
 
-			if (verb > 1)
-				printf("%s: Added keyword: %s\n", 
-						fn, (char *)key.data);
 			dbt_put(db, dbf, &key, &val);
 		}
 		if (ch < 0) {
@@ -661,6 +658,7 @@ index_merge(const struct of *of, struct 
 
 		if (verb)
 			printf("%s: Added index\n", fn);
+
 		dbt_put(idx, idxf, &key, &val);
 	}
 }
@@ -677,7 +675,7 @@ index_prune(const struct of *ofile, DB *
 		recno_t *maxrec, recno_t **recs, size_t *recsz)
 {
 	const struct of	*of;
-	const char	*fn;
+	const char	*fn, *cp;
 	struct db_val	*vbuf;
 	unsigned	 seq, sseq;
 	DBT		 key, val;
@@ -689,18 +687,32 @@ index_prune(const struct of *ofile, DB *
 	while (0 == (ch = (*idx->seq)(idx, &key, &val, seq))) {
 		seq = R_NEXT;
 		*maxrec = *(recno_t *)key.data;
-		if (0 == val.size) {
-			if (reccur >= *recsz) {
-				*recsz += MANDOC_SLOP;
-				*recs = mandoc_realloc(*recs, 
-					*recsz * sizeof(recno_t));
-			}
-			(*recs)[(int)reccur] = *maxrec;
-			reccur++;
-			continue;
-		}
+		cp = val.data;
+
+		/* Deleted records are zero-sized.  Skip them. */
+
+		if (0 == val.size)
+			goto cont;
+
+		/*
+		 * Make sure we're sane.
+		 * Read past our mdoc/man/cat type to the next string,
+		 * then make sure it's bounded by a NUL.
+		 * Failing any of these, we go into our error handler.
+		 */
+
+		if (NULL == (fn = memchr(cp, '\0', val.size)))
+			break;
+		if (++fn - cp >= (int)val.size)
+			break;
+		if (NULL == memchr(fn, '\0', val.size - (fn - cp)))
+			break;
+
+		/* 
+		 * Search for the file in those we care about.
+		 * XXX: build this into a tree.  Too slow.
+		 */
 
-		fn = (char *)val.data;
 		for (of = ofile; of; of = of->next)
 			if (0 == strcmp(fn, of->fname))
 				break;
@@ -708,23 +720,31 @@ index_prune(const struct of *ofile, DB *
 		if (NULL == of)
 			continue;
 
+		/*
+		 * Search through the keyword database, throwing out all
+		 * references to our file.
+		 */
+
 		sseq = R_FIRST;
 		while (0 == (ch = (*db->seq)(db, &key, &val, sseq))) {
 			sseq = R_NEXT;
-			assert(sizeof(struct db_val) == val.size);
+			if (sizeof(struct db_val) != val.size)
+				break;
+
 			vbuf = val.data;
 			if (*maxrec != vbuf->rec)
 				continue;
-			if (verb)
-				printf("%s: Deleted keyword: %s\n", 
-						fn, (char *)key.data);
-			ch = (*db->del)(db, &key, R_CURSOR);
-			if (ch < 0)
+
+			if ((ch = (*db->del)(db, &key, R_CURSOR)) < 0)
 				break;
 		}
+
 		if (ch < 0) {
 			perror(dbf);
 			exit((int)MANDOCLEVEL_SYSERR);
+		} else if (1 != ch) {
+			fprintf(stderr, "%s: Corrupt database\n", dbf);
+			exit((int)MANDOCLEVEL_SYSERR);
 		}
 
 		if (verb)
@@ -732,11 +752,10 @@ index_prune(const struct of *ofile, DB *
 
 		val.size = 0;
 		ch = (*idx->put)(idx, &key, &val, R_CURSOR);
-		if (ch < 0) {
-			perror(idxf);
-			exit((int)MANDOCLEVEL_SYSERR);
-		}
 
+		if (ch < 0)
+			break;
+cont:
 		if (reccur >= *recsz) {
 			*recsz += MANDOC_SLOP;
 			*recs = mandoc_realloc
@@ -746,6 +765,15 @@ index_prune(const struct of *ofile, DB *
 		(*recs)[(int)reccur] = *maxrec;
 		reccur++;
 	}
+
+	if (ch < 0) {
+		perror(idxf);
+		exit((int)MANDOCLEVEL_SYSERR);
+	} else if (1 != ch) {
+		fprintf(stderr, "%s: Corrupt index\n", idxf);
+		exit((int)MANDOCLEVEL_SYSERR);
+	}
+
 	(*maxrec)++;
 }
 
--
 To unsubscribe send an email to source+unsubscribe@mdocml.bsd.lv

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2011-12-01 21:05 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-12-01 21:05 mdocml: Fix mandocdb(8) to pass over the type when pruning the database kristaps

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).