Ejemplo n.º 1
0
static int
watchman_read_and_handle_errors(struct watchman_connection *conn,
                                struct watchman_error *error)
{
    json_t *obj = watchman_read(conn, error);
    if (!obj) {
        return 1;
    }
    if (!json_is_object(obj)) {
        char *bogus_json_text = json_dumps(obj, 0);
        watchman_err(error, "Got non-object result from watchman : %s",
                     bogus_json_text);
        free(bogus_json_text);
        json_decref(obj);
        return 1;
    }
    json_t *error_json = json_object_get(obj, "error");
    if (error_json) {
        watchman_err(error, "Got error result from watchman : %s",
                     json_string_value(error_json));
        json_decref(obj);
        return 1;
    }

    json_decref(obj);
    return 0;
}
Ejemplo n.º 2
0
char *
watchman_clock(struct watchman_connection *conn,
               const char *path,
               unsigned int sync_timeout,
               struct watchman_error *error)
{
    char *result = NULL;
    json_t *query = json_array();
    json_array_append_new(query, json_string("clock"));
    json_array_append_new(query, json_string(path));
    if (sync_timeout) {
        json_t *options = json_object();
        json_object_set_new(options, "sync_timeout", json_integer(sync_timeout));
        json_array_append_new(query, options);
    }

    int ret = watchman_send(conn, query, error);
    json_decref(query);
    if (ret) {
        return NULL;
    }

    proto_t obj = watchman_read(conn, error);
    if (proto_is_null(obj)) {
        return NULL;
    }
    PROTO_ASSERT(proto_is_object, obj, "Got bogus value from clock %s");
    proto_t clock = proto_object_get(obj, "clock");
    PROTO_ASSERT(proto_is_string, clock, "Bad clock %s");
    result = proto_strdup(clock);

done:
    proto_free(obj);
    return result;
}
Ejemplo n.º 3
0
static int
watchman_read_and_handle_errors(struct watchman_connection *conn,
                                struct watchman_error *error)
{
    proto_t obj = watchman_read(conn, error);
    if (proto_is_null(obj)) {
        return 1;
    }
    if (!proto_is_object(obj)) {
        char *bogus_text = proto_dumps(obj, 0);
        watchman_err(error, WATCHMAN_ERR_WATCHMAN_BROKEN,
                     "Got non-object result from watchman : %s",
                     bogus_text);
        free(bogus_text);
        proto_free(obj);
        return 1;
    }
    proto_t error_node = proto_object_get(obj, "error");
    if (!proto_is_null(error_node)) {
        watchman_err(error, WATCHMAN_ERR_OTHER,
                     "Got error result from watchman : %s",
                     proto_strdup(error_node));
        proto_free(obj);
        return 1;
    }

    proto_free(obj);
    return 0;
}
Ejemplo n.º 4
0
int
watchman_version(struct watchman_connection *conn,
                 struct watchman_error *error,
                 struct watchman_version* version)
{
    const char *result = NULL;
    json_t *cmd = json_array();
    json_array_append_new(cmd, json_string("version"));

    int ret = watchman_send(conn, cmd, error);
    json_decref(cmd);
    if (ret) {
        return -1;
    }

    proto_t obj = watchman_read(conn, error);
    if (proto_is_null(obj)) {
        return -1;
    }
    PROTO_ASSERT(proto_is_object, obj, "Got bogus value from version %s");
    proto_t version_field = proto_object_get(obj, "version");
    PROTO_ASSERT(proto_is_string, version_field, "Bad version %s");
    result = proto_strdup(version_field);

    int count = sscanf(result, "%d.%d.%d", &version->major,
                       &version->minor, &version->micro);
    proto_free(obj);
    return count == 3 ? 0 : -1;

done:
    proto_free(obj);
    return -1;
}
Ejemplo n.º 5
0
struct watchman_watch_list *
watchman_watch_list(struct watchman_connection *conn,
                    struct watchman_error *error)
{
    struct watchman_watch_list *res = NULL;
    struct watchman_watch_list *result = NULL;
    if (watchman_send_simple_command(conn, error, "watch-list", NULL)) {
        return NULL;
    }

    json_t *obj = watchman_read(conn, error);
    if (!obj) {
        return NULL;
    }
    JSON_ASSERT(json_is_object, obj, "Got bogus value from watch-list %s");
    json_t *roots = json_object_get(obj, "roots");
    JSON_ASSERT(json_is_array, roots, "Got bogus value from watch-list %s");

    res = malloc(sizeof(*res));
    int nr = json_array_size(roots);
    res->nr = 0;
    res->roots = calloc(nr, sizeof(*res->roots));
    int i;
    for (i = 0; i < nr; ++i) {
        json_t *root = json_array_get(roots, i);
        JSON_ASSERT(json_is_string, root,
                    "Got non-string root from watch-list %s");
        res->nr++;
        res->roots[i] = strdup(json_string_value(root));
    }
    result = res;
    res = NULL;
done:
    if (res) {
        watchman_free_watch_list(res);
    }
    json_decref(obj);
    return result;
}
Ejemplo n.º 6
0
struct watchman_watch_list *
watchman_watch_list(struct watchman_connection *conn,
                    struct watchman_error *error)
{
    struct watchman_watch_list *res = NULL;
    struct watchman_watch_list *result = NULL;
    if (watchman_send_simple_command(conn, error, "watch-list", NULL)) {
        return NULL;
    }

    proto_t obj = watchman_read(conn, error);
    if (proto_is_null(obj)) {
        return NULL;
    }
    PROTO_ASSERT(proto_is_object, obj, "Got bogus value from watch-list %s");
    proto_t roots = proto_object_get(obj, "roots");
    PROTO_ASSERT(proto_is_array, roots, "Got bogus value from watch-list %s");

    res = malloc(sizeof(*res));
    int nr = proto_array_size(roots);
    res->nr = 0;
    res->roots = calloc(nr, sizeof(*res->roots));
    int i;
    for (i = 0; i < nr; ++i) {
        proto_t root = proto_array_get(roots, i);
        PROTO_ASSERT(proto_is_string, root,
                    "Got non-string root from watch-list %s");
        res->nr++;
        res->roots[i] = proto_strdup(root);
    }
    result = res;
    res = NULL;
done:
    if (res) {
        watchman_free_watch_list(res);
    }
    proto_free(obj);
    return result;
}
Ejemplo n.º 7
0
int
watchman_shutdown_server(struct watchman_connection *conn,
                         struct watchman_error *error)
{
    json_t *cmd = json_array();
    json_array_append_new(cmd, json_string("shutdown-server"));

    int ret = watchman_send(conn, cmd, error);
    json_decref(cmd);

    if (ret) {
        return -1;
    }

    proto_t obj = watchman_read(conn, error);
    if (proto_is_null(obj)) {
        return -1;
    }

    proto_free(obj);
    return 0;
}
Ejemplo n.º 8
0
static struct watchman_query_result *
watchman_query_json(struct watchman_connection *conn, json_t *query,
                    struct watchman_error *error)
{
    struct watchman_query_result *result = NULL;
    struct watchman_query_result *res = NULL;

    if (watchman_send(conn, query, error)) {
        return NULL;
    }
    /* parse the result */
    json_t *obj = watchman_read(conn, error);
    if (!obj) {
        return NULL;
    }
    JSON_ASSERT(json_is_object, obj, "Failed to send watchman query %s");

    json_t *jerror = json_object_get(obj, "error");
    if (jerror) {
        watchman_err(error, "Error result from watchman: %s",
                     json_string_value(jerror));
        goto done;
    }

    res = calloc(1, sizeof(*res));

    json_t *files = json_object_get(obj, "files");
    JSON_ASSERT(json_is_array, files, "Bad files %s");

    int nr = json_array_size(files);
    res->stats = calloc(nr, sizeof(*res->stats));

    int i;
    for (i = 0; i < nr; ++i) {
        struct watchman_stat *stat = res->stats + i;
        json_t *statobj = json_array_get(files, i);
        if (json_is_string(statobj)) {
            /* then hopefully we only requested names */
            stat->name = strdup(json_string_value(statobj));
            res->nr++;
            continue;
        }

        JSON_ASSERT(json_is_object, statobj, "must be object: %s");

        json_t *name = json_object_get(statobj, "name");
        JSON_ASSERT(json_is_string, name, "name must be string: %s");
        stat->name = strdup(json_string_value(name));

        WRITE_BOOL_STAT(stat, statobj, exists);
        WRITE_INT_STAT(stat, statobj, ctime);
        WRITE_INT_STAT(stat, statobj, ctime_ms);
        WRITE_INT_STAT(stat, statobj, ctime_us);
        WRITE_INT_STAT(stat, statobj, ctime_ns);
        WRITE_INT_STAT(stat, statobj, dev);
        WRITE_INT_STAT(stat, statobj, gid);
        WRITE_INT_STAT(stat, statobj, ino);
        WRITE_INT_STAT(stat, statobj, mode);
        WRITE_INT_STAT(stat, statobj, mtime);
        WRITE_INT_STAT(stat, statobj, mtime_ms);
        WRITE_INT_STAT(stat, statobj, mtime_us);
        WRITE_INT_STAT(stat, statobj, mtime_ns);
        WRITE_INT_STAT(stat, statobj, nlink);
        WRITE_INT_STAT(stat, statobj, size);
        WRITE_INT_STAT(stat, statobj, uid);

        WRITE_STR_STAT(stat, statobj, cclock);
        WRITE_STR_STAT(stat, statobj, oclock);

        WRITE_FLOAT_STAT(stat, statobj, ctime_f);
        WRITE_FLOAT_STAT(stat, statobj, mtime_f);

        /* the one we have to do manually because we don't
         * want to use the name "new" */
        json_t *newer = json_object_get(statobj, "new");
        if (newer) {
            stat->newer = json_is_true(newer);
        }
        res->nr++;
    }

    json_t *version = json_object_get(obj, "version");
    JSON_ASSERT(json_is_string, version, "Bad version %s");
    res->version = strdup(json_string_value(version));

    json_t *clock = json_object_get(obj, "clock");
    JSON_ASSERT(json_is_string, clock, "Bad clock %s");
    res->clock = strdup(json_string_value(clock));

    json_t *fresh = json_object_get(obj, "is_fresh_instance");
    JSON_ASSERT(json_is_boolean, fresh, "Bad is_fresh_instance %s");
    res->is_fresh_instance = json_is_true(fresh);

    result = res;
    res = NULL;
done:
    if (res) {
        watchman_free_query_result(res);
    }
    json_decref(obj);
    return result;
}