]> git.cameronkatri.com Git - mandoc.git/blobdiff - mandocdb.c
Some simple set_basedir() cleanup; more to come.
[mandoc.git] / mandocdb.c
index 0fc16443601df6d018e74442852ab9a04df14112..152fe1c645681b007231b553a32591da708335c4 100644 (file)
@@ -1,4 +1,4 @@
-/*     $Id: mandocdb.c,v 1.141 2014/04/20 16:46:04 schwarze Exp $ */
+/*     $Id: mandocdb.c,v 1.150 2014/06/19 00:45:37 schwarze Exp $ */
 /*
  * Copyright (c) 2011, 2012 Kristaps Dzonsons <kristaps@bsd.lv>
  * Copyright (c) 2011, 2012, 2013, 2014 Ingo Schwarze <schwarze@openbsd.org>
@@ -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)) {
@@ -471,8 +471,8 @@ main(int argc, char *argv[])
                 * manpath_parse() wants to do it.
                 */
                if (argc > 0) {
-                       dirs.paths = mandoc_calloc(argc,
-                           sizeof(char *));
+                       dirs.paths = mandoc_reallocarray(NULL,
+                           argc, sizeof(char *));
                        dirs.sz = (size_t)argc;
                        for (i = 0; i < argc; i++)
                                dirs.paths[i] = mandoc_strdup(argv[i]);
@@ -512,7 +512,7 @@ main(int argc, char *argv[])
                                goto out;
 
                        mpages_merge(mc, mp);
-                       if (warnings &&
+                       if (warnings && !nodb &&
                            ! (MPARSE_QUICK & mparse_options))
                                names_check();
                        dbclose(0);
@@ -525,7 +525,6 @@ main(int argc, char *argv[])
                }
        }
 out:
-       set_basedir(NULL);
        manpath_free(&dirs);
        mchars_free(mc);
        mparse_free(mp);
@@ -663,7 +662,13 @@ treescan(void)
                                fsec[-1] = '\0';
 
                        mlink = mandoc_calloc(1, sizeof(struct mlink));
-                       strlcpy(mlink->file, path, sizeof(mlink->file));
+                       if (strlcpy(mlink->file, path,
+                           sizeof(mlink->file)) >=
+                           sizeof(mlink->file)) {
+                               say(path, "Filename too long");
+                               free(mlink);
+                               continue;
+                       }
                        mlink->dform = dform;
                        mlink->dsec = dsec;
                        mlink->arch = arch;
@@ -794,10 +799,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);
@@ -819,13 +824,21 @@ filescan(const char *file)
                        say(file, "&stat");
                        return;
                }
-               strlcpy(buf, file, sizeof(buf));
-               start = strstr(buf, basedir) == buf ?
-                   buf + strlen(basedir) + 1 : buf;
+               if (strlcpy(buf, file, sizeof(buf)) >= sizeof(buf)) {
+                       say(file, "Filename too long");
+                       return;
+               }
+               start = buf;
+               if (OP_TEST != op && strstr(buf, basedir) == buf)
+                       start += strlen(basedir);
        }
 
        mlink = mandoc_calloc(1, sizeof(struct mlink));
-       strlcpy(mlink->file, start, sizeof(mlink->file));
+       if (strlcpy(mlink->file, start, sizeof(mlink->file)) >=
+           sizeof(mlink->file)) {
+               say(start, "Filename too long");
+               return;
+       }
 
        /*
         * First try to guess our directory structure.
@@ -973,17 +986,13 @@ mlinks_undupe(struct mpage *mpage)
                        mpage->form = FORM_NONE;
                        goto nextlink;
                }
-               if (strlcpy(buf, mlink->file, PATH_MAX) >= PATH_MAX) {
-                       if (warnings)
-                               say(mlink->file, "Filename too long");
-                       goto nextlink;
-               }
+               (void)strlcpy(buf, mlink->file, sizeof(buf));
                bufp = strstr(buf, "cat");
                assert(NULL != bufp);
                memcpy(bufp, "man", 3);
                if (NULL != (bufp = strrchr(buf, '.')))
                        *++bufp = '\0';
-               strlcat(buf, mlink->dsec, PATH_MAX);
+               (void)strlcat(buf, mlink->dsec, sizeof(buf));
                if (NULL == ohash_find(&mlinks,
                    ohash_qlookup(&mlinks, buf)))
                        goto nextlink;
@@ -1287,7 +1296,8 @@ names_check(void)
              "AND mlinks.name == names.name"
            ")"
          ") JOIN ("
-           "SELECT * FROM mlinks GROUP BY pageid"
+           "SELECT sec, arch, name, pageid FROM mlinks "
+           "GROUP BY pageid"
          ") USING (pageid);",
          -1, &stmt, NULL);
 
@@ -1774,7 +1784,7 @@ putkeys(const struct mpage *mpage,
                s->mask |= v;
                return;
        } else if (NULL == s) {
-               s = mandoc_calloc(sizeof(struct str) + sz + 1, 1);
+               s = mandoc_calloc(1, sizeof(struct str) + sz + 1);
                memcpy(s->key, cp, sz);
                ohash_insert(htab, slot, s);
        }
@@ -1986,6 +1996,20 @@ 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) {
                        fputs(mlink->name, stdout);
                        if (NULL == mlink->next ||
@@ -2012,13 +2036,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;
@@ -2197,7 +2230,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;
@@ -2211,32 +2245,23 @@ 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);
        }
 
-       if (strlcpy(tempfilename, "/tmp/mandocdb.XXXXXX",
-           sizeof(tempfilename)) >= sizeof(tempfilename)) {
-               exitcode = (int)MANDOCLEVEL_SYSERR;
-               say("", "/tmp/mandocdb.XXXXXX: Filename too long");
-               return(0);
-       }
+       (void)strlcpy(tempfilename, "/tmp/mandocdb.XXXXXX",
+           sizeof(tempfilename));
        if (NULL == mkdtemp(tempfilename)) {
                exitcode = (int)MANDOCLEVEL_SYSERR;
                say("", "&%s", tempfilename);
                return(0);
        }
-       if (strlcat(tempfilename, "/" MANDOC_DB,
-           sizeof(tempfilename)) >= sizeof(tempfilename)) {
-               exitcode = (int)MANDOCLEVEL_SYSERR;
-               say("", "%s/" MANDOC_DB ": Filename too long",
-                   tempfilename);
-               return(0);
-       }
+       (void)strlcat(tempfilename, "/" MANDOC_DB,
+           sizeof(tempfilename));
        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);
        }
 
@@ -2274,11 +2299,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=?)";
@@ -2302,8 +2336,14 @@ 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);
@@ -2313,7 +2353,7 @@ static void *
 hash_halloc(size_t sz, void *arg)
 {
 
-       return(mandoc_calloc(sz, 1));
+       return(mandoc_calloc(1, sz));
 }
 
 static void *
@@ -2335,6 +2375,7 @@ set_basedir(const char *targetdir)
 {
        static char      startdir[PATH_MAX];
        static int       fd;
+       char            *cp;
 
        /*
         * Remember where we started by keeping a fd open to the origin
@@ -2345,8 +2386,7 @@ set_basedir(const char *targetdir)
        if ('\0' == *startdir) {
                if (NULL == getcwd(startdir, PATH_MAX)) {
                        exitcode = (int)MANDOCLEVEL_SYSERR;
-                       if (NULL != targetdir)
-                               say("", "&getcwd");
+                       say("", "&getcwd");
                        return(0);
                }
                if (-1 == (fd = open(startdir, O_RDONLY, 0))) {
@@ -2354,8 +2394,6 @@ set_basedir(const char *targetdir)
                        say("", "&open %s", startdir);
                        return(0);
                }
-               if (NULL == targetdir)
-                       targetdir = startdir;
        } else {
                if (-1 == fd)
                        return(0);
@@ -2366,10 +2404,6 @@ set_basedir(const char *targetdir)
                        say("", "&chdir %s", startdir);
                        return(0);
                }
-               if (NULL == targetdir) {
-                       close(fd);
-                       return(1);
-               }
        }
        if (NULL == realpath(targetdir, basedir)) {
                basedir[0] = '\0';
@@ -2381,6 +2415,16 @@ set_basedir(const char *targetdir)
                say("", "&chdir");
                return(0);
        }
+       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);
 }