=================================================================== RCS file: /cvs/mandoc/mandocdb.c,v retrieving revision 1.145 retrieving revision 1.152 diff -u -p -r1.145 -r1.152 --- mandoc/mandocdb.c 2014/04/25 12:13:15 1.145 +++ mandoc/mandocdb.c 2014/06/20 02:24:40 1.152 @@ -1,4 +1,4 @@ -/* $Id: mandocdb.c,v 1.145 2014/04/25 12:13:15 schwarze Exp $ */ +/* $Id: mandocdb.c,v 1.152 2014/06/20 02:24:40 schwarze Exp $ */ /* * Copyright (c) 2011, 2012 Kristaps Dzonsons * Copyright (c) 2011, 2012, 2013, 2014 Ingo Schwarze @@ -145,8 +145,8 @@ static int dbopen(int); static void dbprune(void); static void filescan(const char *); static void *hash_alloc(size_t, void *); -static void hash_free(void *, size_t, void *); -static void *hash_halloc(size_t, void *); +static void hash_free(void *, void *); +static void *hash_calloc(size_t, size_t, void *); static void mlink_add(struct mlink *, const struct stat *); static void mlink_check(struct mpage *, struct mlink *); static void mlink_free(struct mlink *); @@ -336,8 +336,8 @@ main(int argc, char *argv[]) memset(&dirs, 0, sizeof(struct manpaths)); mpages_info.alloc = mlinks_info.alloc = hash_alloc; - mpages_info.halloc = mlinks_info.halloc = hash_halloc; - mpages_info.hfree = mlinks_info.hfree = hash_free; + mpages_info.calloc = mlinks_info.calloc = hash_calloc; + mpages_info.free = mlinks_info.free = hash_free; mpages_info.key_offset = offsetof(struct mpage, inodev); mlinks_info.key_offset = offsetof(struct mlink, file); @@ -433,10 +433,10 @@ main(int argc, char *argv[]) if (OP_UPDATE == op || OP_DELETE == op || OP_TEST == op) { /* - * All of these deal with a specific directory. + * Most of these deal with a specific directory. * Jump into that directory first. */ - if (0 == set_basedir(path_arg)) + if (OP_TEST != op && 0 == set_basedir(path_arg)) goto out; if (dbopen(1)) { @@ -506,13 +506,11 @@ main(int argc, char *argv[]) goto out; if (0 == treescan()) goto out; - if (0 == set_basedir(dirs.paths[j])) - goto out; if (0 == dbopen(0)) goto out; mpages_merge(mc, mp); - if (warnings && + if (warnings && !nodb && ! (MPARSE_QUICK & mparse_options)) names_check(); dbclose(0); @@ -525,7 +523,6 @@ main(int argc, char *argv[]) } } out: - set_basedir(NULL); manpath_free(&dirs); mchars_free(mc); mparse_free(mp); @@ -800,10 +797,10 @@ filescan(const char *file) return; } - if (strstr(buf, basedir) == buf) - start = buf + strlen(basedir) + 1; - else if (OP_TEST == op) + if (OP_TEST == op) start = buf; + else if (strstr(buf, basedir) == buf) + start = buf + strlen(basedir); else { exitcode = (int)MANDOCLEVEL_BADARG; say("", "%s: outside base directory", buf); @@ -829,8 +826,9 @@ filescan(const char *file) say(file, "Filename too long"); return; } - start = strstr(buf, basedir) == buf ? - buf + strlen(basedir) + 1 : buf; + start = buf; + if (OP_TEST != op && strstr(buf, basedir) == buf) + start += strlen(basedir); } mlink = mandoc_calloc(1, sizeof(struct mlink)); @@ -1090,8 +1088,8 @@ mpages_merge(struct mchars *mc, struct mparse *mp) enum mandoclevel lvl; str_info.alloc = hash_alloc; - str_info.halloc = hash_halloc; - str_info.hfree = hash_free; + str_info.calloc = hash_calloc; + str_info.free = hash_free; str_info.key_offset = offsetof(struct str, key); if (0 == nodb) @@ -1996,6 +1994,18 @@ dbadd(struct mpage *mpage, struct mchars *mc) mlink = mpage->mlinks; if (nodb) { + for (key = ohash_first(&names, &slot); NULL != key; + key = ohash_next(&names, &slot)) { + if (key->rendered != key->key) + free(key->rendered); + free(key); + } + for (key = ohash_first(&strings, &slot); NULL != key; + key = ohash_next(&strings, &slot)) { + if (key->rendered != key->key) + free(key->rendered); + free(key); + } if (0 == debug) return; while (NULL != mlink) { @@ -2024,13 +2034,22 @@ dbadd(struct mpage *mpage, struct mchars *mc) if (debug) say(mlink->file, "Adding to database"); + i = strlen(mpage->desc) + 1; + key = mandoc_calloc(1, sizeof(struct str) + i); + memcpy(key->key, mpage->desc, i); + render_key(mc, key); + i = 1; - SQL_BIND_TEXT(stmts[STMT_INSERT_PAGE], i, mpage->desc); + SQL_BIND_TEXT(stmts[STMT_INSERT_PAGE], i, key->rendered); SQL_BIND_INT(stmts[STMT_INSERT_PAGE], i, FORM_SRC == mpage->form); SQL_STEP(stmts[STMT_INSERT_PAGE]); mpage->pageid = sqlite3_last_insert_rowid(db); sqlite3_reset(stmts[STMT_INSERT_PAGE]); + if (key->rendered != key->key) + free(key->rendered); + free(key); + while (NULL != mlink) { dbadd_mlink(mlink); mlink = mlink->next; @@ -2209,7 +2228,8 @@ dbopen(int real) rc = sqlite3_open_v2(MANDOC_DB, &db, ofl, NULL); if (SQLITE_OK != rc) { exitcode = (int)MANDOCLEVEL_SYSERR; - say(MANDOC_DB, "%s", sqlite3_errmsg(db)); + if (SQLITE_CANTOPEN != rc) + say(MANDOC_DB, "%s", sqlite3_errstr(rc)); return(0); } goto prepare_statements; @@ -2223,7 +2243,7 @@ dbopen(int real) goto create_tables; if (MPARSE_QUICK & mparse_options) { exitcode = (int)MANDOCLEVEL_SYSERR; - say(MANDOC_DB "~", "%s", sqlite3_errmsg(db)); + say(MANDOC_DB "~", "%s", sqlite3_errstr(rc)); return(0); } @@ -2239,7 +2259,7 @@ dbopen(int real) rc = sqlite3_open_v2(tempfilename, &db, ofl, NULL); if (SQLITE_OK != rc) { exitcode = (int)MANDOCLEVEL_SYSERR; - say("", "%s: %s", tempfilename, sqlite3_errmsg(db)); + say("", "%s: %s", tempfilename, sqlite3_errstr(rc)); return(0); } @@ -2277,11 +2297,20 @@ create_tables: if (SQLITE_OK != sqlite3_exec(db, sql, NULL, NULL, NULL)) { exitcode = (int)MANDOCLEVEL_SYSERR; say(MANDOC_DB, "%s", sqlite3_errmsg(db)); + sqlite3_close(db); return(0); } prepare_statements: - SQL_EXEC("PRAGMA foreign_keys = ON"); + if (SQLITE_OK != sqlite3_exec(db, + "PRAGMA foreign_keys = ON", NULL, NULL, NULL)) { + exitcode = (int)MANDOCLEVEL_SYSERR; + say(MANDOC_DB, "PRAGMA foreign_keys: %s", + sqlite3_errmsg(db)); + sqlite3_close(db); + return(0); + } + sql = "DELETE FROM mpages WHERE pageid IN " "(SELECT pageid FROM mlinks WHERE " "sec=? AND arch=? AND name=?)"; @@ -2305,18 +2334,24 @@ prepare_statements: * synchronous mode for much better performance. */ - if (real) - SQL_EXEC("PRAGMA synchronous = OFF"); + if (real && SQLITE_OK != sqlite3_exec(db, + "PRAGMA synchronous = OFF", NULL, NULL, NULL)) { + exitcode = (int)MANDOCLEVEL_SYSERR; + say(MANDOC_DB, "PRAGMA synchronous: %s", + sqlite3_errmsg(db)); + sqlite3_close(db); + return(0); + } #endif return(1); } static void * -hash_halloc(size_t sz, void *arg) +hash_calloc(size_t n, size_t sz, void *arg) { - return(mandoc_calloc(1, sz)); + return(mandoc_calloc(n, sz)); } static void * @@ -2327,7 +2362,7 @@ hash_alloc(size_t sz, void *arg) } static void -hash_free(void *p, size_t sz, void *arg) +hash_free(void *p, void *arg) { free(p); @@ -2337,45 +2372,56 @@ static int set_basedir(const char *targetdir) { static char startdir[PATH_MAX]; - static int fd; + static int getcwd_status; /* 1 = ok, 2 = failure */ + static int chdir_status; /* 1 = changed directory */ + char *cp; /* - * Remember where we started by keeping a fd open to the origin - * path component: throughout this utility, we chdir() a lot to - * handle relative paths, and by doing this, we can return to - * the starting point. + * Remember the original working directory, if possible. + * This will be needed if the second or a later directory + * on the command line is given as a relative path. + * Do not error out if the current directory is not + * searchable: Maybe it won't be needed after all. */ - if ('\0' == *startdir) { - if (NULL == getcwd(startdir, PATH_MAX)) { + if (0 == getcwd_status) { + if (NULL == getcwd(startdir, sizeof(startdir))) { + getcwd_status = 2; + (void)strlcpy(startdir, strerror(errno), + sizeof(startdir)); + } else + getcwd_status = 1; + } + + /* + * We are leaving the old base directory. + * Do not use it any longer, not even for messages. + */ + *basedir = '\0'; + + /* + * If and only if the directory was changed earlier and + * the next directory to process is given as a relative path, + * first go back, or bail out if that is impossible. + */ + if (chdir_status && '/' != *targetdir) { + if (2 == getcwd_status) { exitcode = (int)MANDOCLEVEL_SYSERR; - if (NULL != targetdir) - say("", "&getcwd"); + say("", "getcwd: %s", startdir); return(0); } - if (-1 == (fd = open(startdir, O_RDONLY, 0))) { + if (-1 == chdir(startdir)) { exitcode = (int)MANDOCLEVEL_SYSERR; - say("", "&open %s", startdir); - return(0); - } - if (NULL == targetdir) - targetdir = startdir; - } else { - if (-1 == fd) - return(0); - if (-1 == fchdir(fd)) { - close(fd); - basedir[0] = '\0'; - exitcode = (int)MANDOCLEVEL_SYSERR; say("", "&chdir %s", startdir); return(0); } - if (NULL == targetdir) { - close(fd); - return(1); - } } + + /* + * Always resolve basedir to the canonicalized absolute + * pathname and append a trailing slash, such that + * we can reliably check whether files are inside. + */ if (NULL == realpath(targetdir, basedir)) { - basedir[0] = '\0'; exitcode = (int)MANDOCLEVEL_BADARG; say("", "&%s: realpath", targetdir); return(0); @@ -2384,6 +2430,17 @@ set_basedir(const char *targetdir) say("", "&chdir"); return(0); } + chdir_status = 1; + cp = strchr(basedir, '\0'); + if ('/' != cp[-1]) { + if (cp - basedir >= PATH_MAX - 1) { + exitcode = (int)MANDOCLEVEL_SYSERR; + say("", "Filename too long"); + return(0); + } + *cp++ = '/'; + *cp = '\0'; + } return(1); } @@ -2396,7 +2453,7 @@ say(const char *file, const char *format, ...) if ('\0' != *basedir) fprintf(stderr, "%s", basedir); if ('\0' != *basedir && '\0' != *file) - fputs("//", stderr); + fputc('/', stderr); if ('\0' != *file) fprintf(stderr, "%s", file);