static idinfo_t *ibs_get_list(const char *secret, const char *pid, const char *ptype, const char *ctype, int *n) { char url[1024], *rst; idinfo_t *ids = 0; int len, i; kson_t *kson; *n = 0; sprintf(url, "%s/%s/%s/%s/%s?access_token=%s", ibs_url, ibs_prefix, ptype, pid, ctype, secret); rst = ibs_read_all(url, &len); if ((kson = kson_parse(rst)) != 0) { const kson_node_t *p, *q; if ((p = kson_by_path(kson->root, 2, "Response", "Items")) != 0) { ids = malloc(p->n * sizeof(idinfo_t)); *n = p->n; for (i = 0; i < p->n; ++i) { ids[i].id = ids[i].name = 0; if ((q = kson_by_path(p, 2, i, "Id"))) ids[i].id = strdup(q->v.str); if ((q = kson_by_path(p, 2, i, "Name"))) ids[i].name = strdup(q->v.str); } } kson_destroy(kson); } free(rst); return ids; }
static int ibs_by_sample(const char *secret, const char *sid, const char *pid, const char *pname) { char url[1024], *rst; int len, i, n = 0; kson_t *kson; sprintf(url, "%s/%s/samples/%s/files?access_token=%s", ibs_url, ibs_prefix, sid, secret); rst = ibs_read_all(url, &len); if ((kson = kson_parse(rst)) != 0) { const kson_node_t *p, *q; char *id; int64_t size; if ((p = kson_by_path(kson->root, 2, "Response", "Items")) != 0) { for (i = 0; i < p->n; ++i) { if ((q = kson_by_path(p, 2, i, "Id")) == 0) continue; id = q->v.str; if ((q = kson_by_path(p, 2, i, "Size")) == 0) continue; size = atoll(q->v.str); if ((q = kson_by_path(p, 2, i, "Name")) == 0) continue; printf("%s\t%s\t%lld\t%s\t%s\t%s\n", id, q->v.str, (long long)size, sid, pid, pname); ++n; } } kson_destroy(kson); } free(rst); return n; }
kson_t *kson_parse(const char *json) { kson_t *kson; int error; kson = (kson_t*)calloc(1, sizeof(kson_t)); kson->root = kson_parse_core(json, &kson->n_nodes, &error, 0); if (error) { kson_destroy(kson); return 0; } return kson; }
char *ibs_get_userID(const char *secret) { char url[1024], *rst, *id = 0; int len; kson_t *kson; sprintf(url, "%s/%s/oauthv2/token/current?access_token=%s", ibs_url, ibs_prefix, secret); rst = ibs_read_all(url, &len); if ((kson = kson_parse(rst)) != 0) { const kson_node_t *p; if ((p = kson_by_path(kson->root, 3, "Response", "UserResourceOwner", "Id")) != 0) id = strdup(p->v.str); kson_destroy(kson); } free(rst); return id; }
int64_t ibs_download(const char *secret, const char *id, int is_stdout) { char url[1024], *rst, *fn = 0; uint8_t *buf = 0; kurl_t *ku = 0; FILE *fp = 0; int l; int64_t size = 0, start = 0, write_size = 0; kson_t *kson; // get file name and file size sprintf(url, "%s/%s/files/%s?access_token=%s", ibs_url, ibs_prefix, id, secret); rst = ibs_read_all(url, 0); if ((kson = kson_parse(rst)) != 0) { const kson_node_t *p; if ((p = kson_by_path(kson->root, 2, "Response", "Name")) != 0) fn = strdup(p->v.str); if ((p = kson_by_path(kson->root, 2, "Response", "Size")) != 0) size = atoll(p->v.str); kson_destroy(kson); } free(rst); if (fn == 0) { fprintf(stderr, "[E::%s] failed to find file '%s'.\n", __func__, id); return -1; } fprintf(stderr, "[M::%s] file name: %s; size: %lld\n", __func__, id, (long long)size); if (!is_stdout) { struct stat s; if (stat(fn, &s) == 0) { start = s.st_size; if (start == size) { fprintf(stderr, "[W::%s] file '%s' is complete. Download skipped.\n", __func__, fn); goto ret_getfile; } else fprintf(stderr, "[W::%s] file '%s' exists but incomplete. Start from offset %lld.\n", __func__, fn, (long long)start); } } else fp = stdout; sprintf(url, "%s/%s/files/%s/content?access_token=%s", ibs_url, ibs_prefix, id, secret); if ((ku = kurl_open(url, 0)) == 0) { fprintf(stderr, "[E::%s] failed to establish the connection.\n", __func__); goto ret_getfile; } if (start > 0 && kurl_seek(ku, start, SEEK_SET) < 0) { fprintf(stderr, "[E::%s] file '%s' present, but 'range' is not supported. Please manually delete the file first.\n", __func__, fn); goto ret_getfile; } if (!is_stdout) { if (fn && (fp = fopen(fn, "ab")) == 0) { fprintf(stderr, "[E::%s] failed to create file '%s' in the working directory.\n", __func__, fn); goto ret_getfile; } else fprintf(stderr, "[M::%s] writing/appending to file '%s'...\n", __func__, fn); } buf = malloc(IBS_BUFSIZE); while ((l = kurl_read(ku, buf, IBS_BUFSIZE)) > 0) write_size += fwrite(buf, 1, l, fp); free(buf); fprintf(stderr, "[M::%s] %lld bytes transferred.\n", __func__, (long long)write_size); ret_getfile: if (fp && fp != stdout) fclose(fp); if (ku) kurl_close(ku); if (fn) free(fn); return size; }