Add -Q flag:

Print only fatal error messages (i.e., when the database is left in
an inconsistent state and needs manual intervention).

From Abhinav Upadhyay <er.abhinav.upadhyay@gmail.com>.
This commit is contained in:
wiz 2012-08-29 20:33:01 +00:00
parent 2101b6c77f
commit e93044e5ab
2 changed files with 73 additions and 37 deletions

View File

@ -1,4 +1,4 @@
.\" $NetBSD: makemandb.8,v 1.4 2012/08/25 12:37:00 wiz Exp $
.\" $NetBSD: makemandb.8,v 1.5 2012/08/29 20:33:01 wiz Exp $
.\"
.\" Copyright (c) 2011 Abhinav Upadhyay <er.abhinav.upadhyay@gmail.com>
.\" All rights reserved.
@ -29,7 +29,7 @@
.\" OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
.Dd April 21, 2012
.Dd August 29, 2012
.Dt MAKEMANDB 8
.Os
.Sh NAME
@ -37,7 +37,7 @@
.Nd parse the manual pages and build a search index over them
.Sh SYNOPSIS
.Nm
.Op Fl floqv
.Op Fl floQqv
.Op Fl C Ar path
.Sh DESCRIPTION
The
@ -71,11 +71,15 @@ and also to substantially save disk space.
Use this option to optimize the index for speed and also
to significantly reduce disk space usage.
This is a somewhat expensive operation.
.It Fl Q
Print only fatal error messages (i.e., when the database is left in
an inconsistent state and needs manual intervention).
.It Fl q
Print only error messages and no status updates.
Print only warnings and error messages but no status updates.
.It Fl v
Enable verbose output.
This prints the name of every file being parsed.
This prints the name of every file being parsed
and a summary at the end of the index update.
.El
.Pp
As the database file is stored under

View File

@ -1,4 +1,4 @@
/* $NetBSD: makemandb.c,v 1.12 2012/08/25 12:37:12 wiz Exp $ */
/* $NetBSD: makemandb.c,v 1.13 2012/08/29 20:33:01 wiz Exp $ */
/*
* Copyright (c) 2011 Abhinav Upadhyay <er.abhinav.upadhyay@gmail.com>
* Copyright (c) 2011 Kristaps Dzonsons <kristaps@bsd.lv>
@ -17,7 +17,7 @@
*/
#include <sys/cdefs.h>
__RCSID("$NetBSD: makemandb.c,v 1.12 2012/08/25 12:37:12 wiz Exp $");
__RCSID("$NetBSD: makemandb.c,v 1.13 2012/08/29 20:33:01 wiz Exp $");
#include <sys/stat.h>
#include <sys/types.h>
@ -304,7 +304,7 @@ main(int argc, char *argv[])
size_t linesize;
struct mandb_rec rec;
while ((ch = getopt(argc, argv, "C:floqv")) != -1) {
while ((ch = getopt(argc, argv, "C:floQqv")) != -1) {
switch (ch) {
case 'C':
manconf = optarg;
@ -319,9 +319,12 @@ main(int argc, char *argv[])
case 'o':
mflags.optimize = 1;
break;
case 'q':
case 'Q':
mflags.verbosity = 0;
break;
case 'q':
mflags.verbosity = 1;
break;
case 'v':
mflags.verbosity = 2;
break;
@ -417,6 +420,8 @@ main(int argc, char *argv[])
err(EXIT_FAILURE, "pclose error");
}
if (mflags.verbosity)
printf("Performing index update\n");
update_db(db, mp, &rec);
mparse_free(mp);
free_secbuffs(&rec);
@ -451,6 +456,7 @@ traversedir(const char *parent, const char *file, sqlite3 *db,
char *buf;
if (stat(file, &sb) < 0) {
if (mflags.verbosity)
warn("stat failed: %s", file);
return;
}
@ -464,6 +470,7 @@ traversedir(const char *parent, const char *file, sqlite3 *db,
/* If it is a directory, traverse it recursively */
if (S_ISDIR(sb.st_mode)) {
if ((dp = opendir(file)) == NULL) {
if (mflags.verbosity)
warn("opendir error: %s", file);
return;
}
@ -503,6 +510,7 @@ build_file_cache(sqlite3 *db, const char *parent, const char *file,
" :mtime, :parent, :file)";
rc = sqlite3_prepare_v2(db, sqlstr, -1, &stmt, NULL);
if (rc != SQLITE_OK) {
if (mflags.verbosity)
warnx("%s", sqlite3_errmsg(db));
return;
}
@ -510,6 +518,7 @@ build_file_cache(sqlite3 *db, const char *parent, const char *file,
idx = sqlite3_bind_parameter_index(stmt, ":device");
rc = sqlite3_bind_int64(stmt, idx, device_cache);
if (rc != SQLITE_OK) {
if (mflags.verbosity)
warnx("%s", sqlite3_errmsg(db));
sqlite3_finalize(stmt);
return;
@ -518,6 +527,7 @@ build_file_cache(sqlite3 *db, const char *parent, const char *file,
idx = sqlite3_bind_parameter_index(stmt, ":inode");
rc = sqlite3_bind_int64(stmt, idx, inode_cache);
if (rc != SQLITE_OK) {
if (mflags.verbosity)
warnx("%s", sqlite3_errmsg(db));
sqlite3_finalize(stmt);
return;
@ -526,6 +536,7 @@ build_file_cache(sqlite3 *db, const char *parent, const char *file,
idx = sqlite3_bind_parameter_index(stmt, ":mtime");
rc = sqlite3_bind_int64(stmt, idx, mtime_cache);
if (rc != SQLITE_OK) {
if (mflags.verbosity)
warnx("%s", sqlite3_errmsg(db));
sqlite3_finalize(stmt);
return;
@ -534,6 +545,7 @@ build_file_cache(sqlite3 *db, const char *parent, const char *file,
idx = sqlite3_bind_parameter_index(stmt, ":parent");
rc = sqlite3_bind_text(stmt, idx, parent, -1, NULL);
if (rc != SQLITE_OK) {
if (mflags.verbosity)
warnx("%s", sqlite3_errmsg(db));
sqlite3_finalize(stmt);
return;
@ -542,6 +554,7 @@ build_file_cache(sqlite3 *db, const char *parent, const char *file,
idx = sqlite3_bind_parameter_index(stmt, ":file");
rc = sqlite3_bind_text(stmt, idx, file, -1, NULL);
if (rc != SQLITE_OK) {
if (mflags.verbosity)
warnx("%s", sqlite3_errmsg(db));
sqlite3_finalize(stmt);
return;
@ -567,6 +580,7 @@ update_existing_entry(sqlite3 *db, const char *file, const char *hash,
" :inode2 OR mtime <> :mtime2)";
rc = sqlite3_prepare_v2(db, inner_sqlstr, -1, &inner_stmt, NULL);
if (rc != SQLITE_OK) {
if (mflags.verbosity)
warnx("%s", sqlite3_errmsg(db));
return;
}
@ -591,7 +605,7 @@ update_existing_entry(sqlite3 *db, const char *file, const char *hash,
if (rc == SQLITE_DONE) {
/* Check if an update has been performed. */
if (update_count != sqlite3_total_changes(db)) {
if (mflags.verbosity)
if (mflags.verbosity == 2)
printf("Updated %s\n", file);
(*new_count)++;
} else {
@ -599,6 +613,7 @@ update_existing_entry(sqlite3 *db, const char *file, const char *hash,
(*link_count)++;
}
} else {
if (mflags.verbosity == 2)
warnx("Could not update the meta data for %s", file);
(*err_count)++;
}
@ -643,6 +658,7 @@ read_and_decompress(const char *file, void **buf, size_t *len)
if (off == *len) {
*len *= 2;
if (*len < off) {
if (mflags.verbosity)
warnx("File too large: %s", file);
free(*buf);
archive_read_close(a);
@ -691,6 +707,7 @@ update_db(sqlite3 *db, struct mparse *mp, mandb_rec *rec)
rc = sqlite3_prepare_v2(db, sqlstr, -1, &stmt, NULL);
if (rc != SQLITE_OK) {
if (mflags.verbosity)
warnx("%s", sqlite3_errmsg(db));
close_db(db);
errx(EXIT_FAILURE, "Could not query file cache");
@ -713,6 +730,7 @@ update_db(sqlite3 *db, struct mparse *mp, mandb_rec *rec)
md5_status = check_md5(file, db, "mandb_meta", &md5sum, buf, buflen);
assert(md5sum != NULL);
if (md5_status == -1) {
if (mflags.verbosity)
warnx("An error occurred in checking md5 value"
" for file %s", file);
err_count++;
@ -743,7 +761,7 @@ update_db(sqlite3 *db, struct mparse *mp, mandb_rec *rec)
* This means is either a new file or an updated file.
* We should go ahead with parsing.
*/
if (mflags.verbosity > 1)
if (mflags.verbosity == 2)
printf("Parsing: %s\n", file);
rec->md5_hash = md5sum;
rec->file_path = estrdup(file);
@ -751,6 +769,7 @@ update_db(sqlite3 *db, struct mparse *mp, mandb_rec *rec)
chdir(parent);
begin_parse(file, mp, rec, buf, buflen);
if (insert_into_db(db, rec) < 0) {
if (mflags.verbosity)
warnx("Error in indexing %s", file);
err_count++;
} else {
@ -762,7 +781,7 @@ update_db(sqlite3 *db, struct mparse *mp, mandb_rec *rec)
sqlite3_finalize(stmt);
if (mflags.verbosity) {
if (mflags.verbosity == 2) {
printf("Total Number of new or updated pages encountered = %d\n"
"Total number of (hard or symbolic) links found = %d\n"
"Total number of pages that were successfully"
@ -775,7 +794,7 @@ update_db(sqlite3 *db, struct mparse *mp, mandb_rec *rec)
if (mflags.recreate)
return;
if (mflags.verbosity)
if (mflags.verbosity == 2)
printf("Deleting stale index entries\n");
sqlstr = "DELETE FROM mandb_meta WHERE file NOT IN"
@ -810,12 +829,18 @@ begin_parse(const char *file, struct mparse *mp, mandb_rec *rec,
rec->xr_found = 0;
if (mparse_readmem(mp, buf, len, file) >= MANDOCLEVEL_FATAL) {
/* Printing this warning at verbosity level 2
* because some packages from pkgsrc might trigger several
* of such warnings.
*/
if (mflags.verbosity == 2)
warnx("%s: Parse failure", file);
return;
}
mparse_result(mp, &mdoc, &man);
if (mdoc == NULL && man == NULL) {
if (mflags.verbosity == 2)
warnx("Not a man(7) or mdoc(7) page");
return;
}
@ -1670,6 +1695,7 @@ insert_into_db(sqlite3 *db, mandb_rec *rec)
sqlite3_exec(db, sql, NULL, NULL, &errmsg);
sqlite3_free(sql);
if (errmsg != NULL) {
if (mflags.verbosity)
warnx("%s", errmsg);
free(errmsg);
}
@ -1678,6 +1704,7 @@ insert_into_db(sqlite3 *db, mandb_rec *rec)
" md5_hash = :md5 WHERE file = :file";
rc = sqlite3_prepare_v2(db, sqlstr, -1, &stmt, NULL);
if (rc != SQLITE_OK) {
if (mflags.verbosity)
warnx("Update failed with error: %s",
sqlite3_errmsg(db));
close_db(db);
@ -1702,6 +1729,7 @@ insert_into_db(sqlite3 *db, mandb_rec *rec)
sqlite3_finalize(stmt);
if (rc != SQLITE_DONE) {
if (mflags.verbosity)
warnx("%s", sqlite3_errmsg(db));
close_db(db);
cleanup(rec);
@ -1746,6 +1774,7 @@ insert_into_db(sqlite3 *db, mandb_rec *rec)
return 0;
Out:
if (mflags.verbosity)
warnx("%s", sqlite3_errmsg(db));
cleanup(rec);
return -1;
@ -1776,6 +1805,7 @@ check_md5(const char *file, sqlite3 *db, const char *table, char **md5sum,
assert(file != NULL);
*md5sum = MD5Data(buf, buflen, NULL);
if (*md5sum == NULL) {
if (mflags.verbosity)
warn("md5 failed: %s", file);
return -1;
}
@ -1793,6 +1823,7 @@ check_md5(const char *file, sqlite3 *db, const char *table, char **md5sum,
idx = sqlite3_bind_parameter_index(stmt, ":md5_hash");
rc = sqlite3_bind_text(stmt, idx, *md5sum, -1, NULL);
if (rc != SQLITE_OK) {
if (mflags.verbosity)
warnx("%s", sqlite3_errmsg(db));
sqlite3_finalize(stmt);
free(sqlstr);
@ -1819,12 +1850,13 @@ optimize(sqlite3 *db)
const char *sqlstr;
char *errmsg = NULL;
if (mflags.verbosity)
if (mflags.verbosity == 2)
printf("Optimizing the database index\n");
sqlstr = "INSERT INTO mandb(mandb) VALUES (\'optimize\');"
"VACUUM";
sqlite3_exec(db, sqlstr, NULL, NULL, &errmsg);
if (errmsg != NULL) {
if (mflags.verbosity)
warnx("%s", errmsg);
free(errmsg);
return;
@ -2036,6 +2068,6 @@ append(secbuff *sbuff, const char *src)
static void
usage(void)
{
fprintf(stderr, "Usage: %s [-floqv] [-C path]\n", getprogname());
fprintf(stderr, "Usage: %s [-floQqv] [-C path]\n", getprogname());
exit(1);
}