11. Link

The "link" operation will link a page to a filepath.

<<argparse_entries>>=
{"link", 4, p_link, NULL},
<<static_funcdefs>>=
static int p_link(int argc, char *argv[]);
<<functions>>=
static int p_link(int argc, char *argv[])
{
    weewiki_d ww;
    sqlite3 *db;
    sqlite3_stmt *stmt;
    int rc;
    struct stat st;
    int force;

    force = 0;
    if (argc < 3) {
        fprintf(stderr,
                "Usage: %s key file [db]\n",
                argv[0]);
        return 1;
    }

    if (argc > 1  && !strcmp(argv[1], "-f")) {
        force = 1;
        argv++;
        argc--;
    }

    if (!force) {
        if (access(argv[2], F_OK) != -1) {
            fprintf(stderr, "%s: file exists.\n", argv[2]);
            return 1;
        }
    }

    weewiki_init(&ww);

    if (argc >= 4) {
        weewiki_open(&ww, argv[3]);
    } else {
        weewiki_open(&ww, weewiki_dbname_get());
    }

    if (force) {
        rc = weewiki_push(&ww, argv[2], argv[1]);
    } else {
        rc = weewiki_pull(&ww, argv[1], argv[2]);
    }

    if (rc) goto cleanup;

    db = ww.db;

    sqlite3_prepare_v2(db,
                       "INSERT OR REPLACE INTO wikilinks"
                       "(key, filename, mtime)\n"
                       "VALUES(?1,?2,?3);",
                       -1,
                       &stmt,
                       NULL);

    sqlite3_bind_text(stmt, 1, argv[1], -1, NULL);
    sqlite3_bind_text(stmt, 2, argv[2], -1, NULL);
    stat(argv[2], &st);
    sqlite3_bind_int(stmt, 3, st.st_mtime);

    sqlite3_step(stmt);

    sqlite3_finalize(stmt);

    cleanup:
    weewiki_close(&ww);
    weewiki_clean(&ww);
    return rc;
}



prev | home | next