Skip to content
pi-c Artifact [f3130fbff1]
Login

Artifact [f3130fbff1]

Artifact f3130fbff140ccef5c700488333c0a133153c1ab596cb5bfd41679d61c88b828:


/* The workspace's verify command (verify.h). */
#ifndef _DEFAULT_SOURCE
#define _DEFAULT_SOURCE 1
#endif
#include <errno.h>
#include <limits.h>
#include <stdlib.h>
#include <string.h>

#include "input.h"
#include "sqlite3.h"
#include "userdb.h"
#include "verify.h"

#define VERIFY_FILE_MAX (64u * 1024)

char *pic_verify_load(const char *workspace, FILE *warn) {
    char real[PATH_MAX], path[PATH_MAX + 32], *raw, *command = NULL;
    size_t len = 0;
    sqlite3 *db = NULL;
    sqlite3_stmt *q = NULL;
    if (!realpath(workspace, real) || pic_trust_get(real) != 1 ||
        snprintf(path, sizeof path, "%s/.pic/verify.json", real) >= (int)sizeof path) {
        return NULL;
    }
    if (!(raw = pic_input_read(path, VERIFY_FILE_MAX, 0, &len, NULL))) {
        if (errno != ENOENT && warn) {
            fprintf(warn, "pic: cannot read %s: %s\n", path, strerror(errno));
        }
        return NULL;
    }
    if (sqlite3_open(":memory:", &db) == SQLITE_OK &&
        sqlite3_prepare_v2(db,
                           "SELECT json_extract(?1, '$.command') WHERE json_valid(?1) AND json_type(?1, '$.command') = 'text'"
                           " AND length(json_extract(?1, '$.command')) > 0",
                           -1, &q, NULL) == SQLITE_OK) {
        sqlite3_bind_text(q, 1, raw, (int)len, SQLITE_STATIC);
        if (sqlite3_step(q) == SQLITE_ROW) {
            command = strdup((const char *)sqlite3_column_text(q, 0));
        }
    }
    if (!command && warn) {
        fprintf(warn, "pic: %s has no \"command\" string; no verify command\n", path);
    }
    sqlite3_finalize(q);
    sqlite3_close(db);
    free(raw);
    return command;
}