diff options
-rw-r--r-- | tools/dumpsolv.c | 476 |
1 files changed, 312 insertions, 164 deletions
diff --git a/tools/dumpsolv.c b/tools/dumpsolv.c index b0ac053..a97e5d5 100644 --- a/tools/dumpsolv.c +++ b/tools/dumpsolv.c @@ -10,53 +10,14 @@ #include <unistd.h> #include <string.h> -static int with_attr = 0; +static int with_attr; +static int dump_json; #include "pool.h" #include "repo_solv.h" -static int dump_repoattrs_cb(void *vcbdata, Solvable *s, Repodata *data, Repokey *key, KeyValue *kv); -static void -dump_repodata(Repo *repo) -{ - unsigned i; - Repodata *data; - if (repo->nrepodata == 0) - return; - printf("repo contains %d repodata sections:\n", repo->nrepodata - 1); - FOR_REPODATAS(repo, i, data) - { - unsigned int j; - printf("\nrepodata %d has %d keys, %d schemata\n", i, data->nkeys - 1, data->nschemata - 1); - for (j = 1; j < data->nkeys; j++) - printf(" %s (type %s size %d storage %d)\n", pool_id2str(repo->pool, data->keys[j].name), pool_id2str(repo->pool, data->keys[j].type), data->keys[j].size, data->keys[j].storage); - if (data->localpool) - printf(" localpool has %d strings, size is %d\n", data->spool.nstrings, data->spool.sstrings); - if (data->dirpool.ndirs) - printf(" localpool has %d directories\n", data->dirpool.ndirs); - printf("\n"); - repodata_search(data, SOLVID_META, 0, SEARCH_ARRAYSENTINEL|SEARCH_SUB, dump_repoattrs_cb, 0); - } - printf("\n"); -} - -#if 0 -static void -printids(Repo *repo, char *kind, Offset ido) -{ - Pool *pool = repo->pool; - Id id, *ids; - if (!ido) - return; - printf("%s:\n", kind); - ids = repo->idarraydata + ido; - while((id = *ids++) != 0) - printf(" %s\n", pool_dep2str(pool, id)); -} -#endif - -int +static int dump_attr(Repo *repo, Repodata *data, Repokey *key, KeyValue *kv) { const char *keyname; @@ -134,48 +95,255 @@ dump_attr(Repo *repo, Repodata *data, Repokey *key, KeyValue *kv) return 0; } -#if 1 +static const char * +jsonstring(Pool *pool, const char *s) +{ + int needed = 0; + const unsigned char *s1; + char *r, *rp; + + for (s1 = (const unsigned char *)s; *s1; s1++) + { + if (*s1 < 32) + needed += *s1 == '\n' ? 2 : 6; + else if (*s1 == '\\' || *s1 == '\"') + needed += 2; + else + needed++; + } + r = rp = pool_alloctmpspace(pool, needed + 3); + *rp++ = '\"'; + for (s1 = (const unsigned char *)s; *s1; s1++) + { + if (*s1 < 32) + { + int x; + if (*s1 == '\n') + { + *rp++ = '\\'; + *rp++ = 'n'; + continue; + } + *rp++ = '\\'; + *rp++ = 'u'; + *rp++ = '0'; + *rp++ = '0'; + x = *s1 / 16; + *rp++ = (x < 10 ? '0' : 'a' - 10) + x; + x = *s1 & 15; + *rp++ = (x < 10 ? '0' : 'a' - 10) + x; + } + else if (*s1 == '\\' || *s1 == '\"') + { + *rp++ = '\\'; + *rp++ = *s1; + } + else + *rp++ = *s1; + } + *rp++ = '\"'; + *rp = 0; + return r; +} + +struct cbdata { + unsigned char *first; + int nfirst; + int baseindent; +}; + static int -dump_repoattrs_cb(void *vcbdata, Solvable *s, Repodata *data, Repokey *key, KeyValue *kv) +dump_attr_json(Repo *repo, Repodata *data, Repokey *key, KeyValue *kv, struct cbdata *cbdata) +{ + Pool *pool = repo->pool; + const char *keyname; + KeyValue *kvp; + int indent = cbdata->baseindent; + int isarray = 0; + const char *str; + int depth = 0; + + keyname = pool_id2str(repo->pool, key->name); + for (kvp = kv; (kvp = kvp->parent) != 0; indent += 4) + depth++; + if (cbdata->nfirst < depth + 1) + { + cbdata->first = solv_realloc(cbdata->first, depth + 16); + memset(cbdata->first + cbdata->nfirst, 0, depth + 16 - cbdata->nfirst); + cbdata->nfirst = depth + 16; + } + switch(key->type) + { + case REPOKEY_TYPE_IDARRAY: + case REPOKEY_TYPE_DIRNUMNUMARRAY: + case REPOKEY_TYPE_DIRSTRARRAY: + isarray = 1; + break; + case REPOKEY_TYPE_FIXARRAY: + case REPOKEY_TYPE_FLEXARRAY: + isarray = 2; + break; + default: + break; + } + if (!isarray || !kv->entry) + { + if (cbdata->first[depth]) + printf(",\n"); + printf("%*s%s: ", indent, "", jsonstring(pool, keyname)); + cbdata->first[depth] = 1; + } + if (isarray == 1 && !kv->entry) + printf("[\n%*s", indent + 2, ""); + else if (isarray == 1 && kv->entry) + printf("%*s", indent + 2, ""); + switch(key->type) + { + case REPOKEY_TYPE_ID: + if (data && data->localpool) + str = stringpool_id2str(&data->spool, kv->id); + else + str = pool_dep2str(repo->pool, kv->id); + printf("%s", jsonstring(pool, str)); + break; + case REPOKEY_TYPE_CONSTANTID: + str = pool_dep2str(repo->pool, kv->id); + printf("%s", jsonstring(pool, str)); + break; + case REPOKEY_TYPE_IDARRAY: + if (data && data->localpool) + str = stringpool_id2str(&data->spool, kv->id); + else + str = pool_dep2str(repo->pool, kv->id); + printf("%s", jsonstring(pool, str)); + break; + case REPOKEY_TYPE_STR: + str = kv->str; + printf("%s", jsonstring(pool, str)); + break; + case REPOKEY_TYPE_MD5: + case REPOKEY_TYPE_SHA1: + case REPOKEY_TYPE_SHA256: + printf("{\n"); + printf("%*s \"value\": %s,\n", indent, "", jsonstring(pool, repodata_chk2str(data, key->type, (unsigned char *)kv->str))); + printf("%*s \"type\": %s\n", indent, "", jsonstring(pool, pool_id2str(repo->pool, key->type))); + printf("%*s}", indent, ""); + break; + case REPOKEY_TYPE_VOID: + printf("null"); + break; + case REPOKEY_TYPE_U32: + case REPOKEY_TYPE_CONSTANT: + printf("%u", kv->num); + break; + case REPOKEY_TYPE_NUM: + printf("%llu", SOLV_KV_NUM64(kv)); + break; + case REPOKEY_TYPE_BINARY: + printf("\"<binary>\""); + break; + case REPOKEY_TYPE_DIRNUMNUMARRAY: + printf("{\n"); + printf("%*s \"dir\": %s,\n", indent, "", jsonstring(pool, repodata_dir2str(data, kv->id, 0))); + printf("%*s \"num1\": %u,\n", indent, "", kv->num); + printf("%*s \"num2\": %u\n", indent, "", kv->num2); + printf("%*s }", indent, ""); + break; + case REPOKEY_TYPE_DIRSTRARRAY: + printf("%s", jsonstring(pool, repodata_dir2str(data, kv->id, kv->str))); + break; + case REPOKEY_TYPE_FIXARRAY: + case REPOKEY_TYPE_FLEXARRAY: + cbdata->first[depth + 1] = 0; + if (!kv->entry) + printf("[\n"); + else + { + if (kv->eof != 2) + printf("\n%*s },\n", indent, ""); + else + printf("\n%*s }\n", indent, ""); + } + if (kv->eof != 2) + printf("%*s {\n", indent, ""); + else + printf("%*s]", indent, ""); + break; + default: + printf("\"?\""); + break; + } + if (isarray == 1) + { + if (!kv->eof) + printf(",\n"); + else + printf("\n%*s]", indent, ""); + } + return 0; +} + +static int +dump_repodata_cb(void *vcbdata, Solvable *s, Repodata *data, Repokey *key, KeyValue *kv) { if (key->name == REPOSITORY_SOLVABLES) return SEARCH_NEXT_SOLVABLE; - return dump_attr(data->repo, data, key, kv); + if (!dump_json) + return dump_attr(data->repo, data, key, kv); + else + return dump_attr_json(data->repo, data, key, kv, vcbdata); +} + +static void +dump_repodata(Repo *repo, struct cbdata *cbdata) +{ + unsigned int i; + Repodata *data; + if (repo->nrepodata == 0) + return; + if (!dump_json) + printf("repo contains %d repodata sections:\n", repo->nrepodata - 1); + cbdata->baseindent = 6; + FOR_REPODATAS(repo, i, data) + { + unsigned int j; + if (!dump_json) + { + printf("\nrepodata %d has %d keys, %d schemata\n", i, data->nkeys - 1, data->nschemata - 1); + for (j = 1; j < data->nkeys; j++) + printf(" %s (type %s size %d storage %d)\n", pool_id2str(repo->pool, data->keys[j].name), pool_id2str(repo->pool, data->keys[j].type), data->keys[j].size, data->keys[j].storage); + if (data->localpool) + printf(" localpool has %d strings, size is %d\n", data->spool.nstrings, data->spool.sstrings); + if (data->dirpool.ndirs) + printf(" localpool has %d directories\n", data->dirpool.ndirs); + printf("\n"); + } + repodata_search(data, SOLVID_META, 0, SEARCH_ARRAYSENTINEL|SEARCH_SUB, dump_repodata_cb, cbdata); + } + if (!dump_json) + printf("\n"); } -#endif /* * dump all attributes for Id <p> */ void -dump_repoattrs(Repo *repo, Id p) +dump_solvable(Repo *repo, Id p, struct cbdata *cbdata) { -#if 0 - repo_search(repo, p, 0, 0, SEARCH_ARRAYSENTINEL|SEARCH_SUB, dump_repoattrs_cb, 0); -#else Dataiterator di; dataiterator_init(&di, repo->pool, repo, p, 0, 0, SEARCH_ARRAYSENTINEL|SEARCH_SUB); + if (cbdata->first) + cbdata->first[0] = 0; + cbdata->baseindent = 10; while (dataiterator_step(&di)) - dump_attr(repo, di.data, di.key, &di.kv); -#endif -} - -#if 0 -void -dump_some_attrs(Repo *repo, Solvable *s) -{ - const char *summary = 0; - unsigned int medianr = -1, downloadsize = -1; - unsigned int time = -1; - summary = repo_lookup_str(s, SOLVABLE_SUMMARY); - medianr = repo_lookup_num(s, SOLVABLE_MEDIANR); - downloadsize = repo_lookup_num (s, SOLVABLE_DOWNLOADSIZE); - time = repo_lookup_num(s, SOLVABLE_BUILDTIME); - printf (" XXX %d %d %u %s\n", medianr, downloadsize, time, summary); + { + if (!dump_json) + dump_attr(repo, di.data, di.key, &di.kv); + else + dump_attr_json(repo, di.data, di.key, &di.kv, cbdata); + } } -#endif - static int loadcallback(Pool *pool, Repodata *data, void *vdata) @@ -187,7 +355,7 @@ loadcallback(Pool *pool, Repodata *data, void *vdata) location = repodata_lookup_str(data, SOLVID_META, REPOSITORY_LOCATION); if (!location || !with_attr) return 0; - fprintf (stderr, "[Loading SOLV file %s]\n", location); + fprintf(stderr, "[Loading SOLV file %s]\n", location); fp = fopen (location, "r"); if (!fp) { @@ -204,56 +372,27 @@ static void usage(int status) { fprintf( stderr, "\nUsage:\n" - "dumpsolv [-a] [<solvfile>]\n" + "dumpsolv [-a] [-j] [<solvfile>]\n" " -a read attributes.\n" + " -j dump json format.\n" ); exit(status); } -#if 0 -static void -tryme (Repo *repo, Id p, Id keyname, const char *match, int flags) -{ - Dataiterator di; - dataiterator_init(&di, repo, p, keyname, match, flags); - while (dataiterator_step(&di)) - { - switch (di.key->type) - { - case REPOKEY_TYPE_ID: - case REPOKEY_TYPE_IDARRAY: - if (di.data && di.data->localpool) - di.kv.str = stringpool_id2str(&di.data->spool, di.kv.id); - else - di.kv.str = pool_id2str(repo->pool, di.kv.id); - break; - case REPOKEY_TYPE_STR: - case REPOKEY_TYPE_DIRSTRARRAY: - break; - default: - di.kv.str = 0; - } - fprintf (stdout, "found: %d:%s %d %s %d %d %d\n", - di.solvid, - pool_id2str(repo->pool, di.key->name), - di.kv.id, - di.kv.str, di.kv.num, di.kv.num2, di.kv.eof); - } -} -#endif - int main(int argc, char **argv) { Repo *repo; Pool *pool; int c, i, j, n; Solvable *s; + int openrepo = 0; + struct cbdata cbdata; + memset(&cbdata, 0, sizeof(cbdata)); pool = pool_create(); - pool_setdebuglevel(pool, 1); pool_setloadcallback(pool, loadcallback, 0); - while ((c = getopt(argc, argv, "ha")) >= 0) + while ((c = getopt(argc, argv, "haj")) >= 0) { switch(c) { @@ -263,11 +402,18 @@ int main(int argc, char **argv) case 'a': with_attr = 1; break; + case 'j': + dump_json = 1; + break; default: usage(1); break; } } + if (!dump_json) + pool_setdebuglevel(pool, 1); + if (dump_json) + pool->debugmask |= SOLV_DEBUG_TO_STDERR; for (; optind < argc; optind++) { if (freopen(argv[optind], "r", stdin) == 0) @@ -277,83 +423,85 @@ int main(int argc, char **argv) } repo = repo_create(pool, argv[optind]); if (repo_add_solv(repo, stdin, 0)) - printf("could not read repository: %s\n", pool_errstr(pool)); + { + fprintf(stderr, "could not read repository: %s\n", pool_errstr(pool)); + exit(1); + } } if (!pool->urepos) { repo = repo_create(pool, argc != 1 ? argv[1] : "<stdin>"); if (repo_add_solv(repo, stdin, 0)) - printf("could not read repository: %s\n", pool_errstr(pool)); + { + fprintf(stderr, "could not read repository: %s\n", pool_errstr(pool)); + exit(1); + } } - printf("pool contains %d strings, %d rels, string size is %d\n", pool->ss.nstrings, pool->nrels, pool->ss.sstrings); - -#if 0 -{ - Dataiterator di; - dataiterator_init(&di, repo, -1, 0, "oo", DI_SEARCHSUB|SEARCH_SUBSTRING); - while (dataiterator_step(&di)) - dump_attr(di.repo, di.data, di.key, &di.kv); - exit(0); -} -#endif + if (!dump_json) + printf("pool contains %d strings, %d rels, string size is %d\n", pool->ss.nstrings, pool->nrels, pool->ss.sstrings); + if (dump_json) + { + printf("{\n"); + printf(" \"repositories\": [\n"); + } n = 0; FOR_REPOS(j, repo) { - dump_repodata(repo); - - printf("repo %d contains %d solvables\n", j, repo->nsolvables); - printf("repo start: %d end: %d\n", repo->start, repo->end); + int open = 0; + if (dump_json) + { + if (openrepo) + printf("\n },"); + printf(" {\n"); + openrepo = 1; + if (cbdata.first) + cbdata.first[0] = 0; + } + dump_repodata(repo, &cbdata); + if (!dump_json) + { + printf("repo %d contains %d solvables\n", j, repo->nsolvables); + printf("repo start: %d end: %d\n", repo->start, repo->end); + } + else + { + if (cbdata.first[0]) + printf(",\n"); + printf(" \"solvables\": [\n"); + } FOR_REPO_SOLVABLES(repo, i, s) { n++; - printf("\n"); - printf("solvable %d (%d):\n", n, i); -#if 0 - if (s->name || s->evr || s->arch) - printf("name: %s %s %s\n", pool_id2str(pool, s->name), pool_id2str(pool, s->evr), pool_id2str(pool, s->arch)); - if (s->vendor) - printf("vendor: %s\n", pool_id2str(pool, s->vendor)); - printids(repo, "provides", s->provides); - printids(repo, "obsoletes", s->obsoletes); - printids(repo, "conflicts", s->conflicts); - printids(repo, "requires", s->requires); - printids(repo, "recommends", s->recommends); - printids(repo, "suggests", s->suggests); - printids(repo, "supplements", s->supplements); - printids(repo, "enhances", s->enhances); - if (repo->rpmdbid) - printf("rpmdbid: %u\n", repo->rpmdbid[i - repo->start]); -#endif - dump_repoattrs(repo, i); -#if 0 - dump_some_attrs(repo, s); -#endif + if (!dump_json) + { + printf("\n"); + printf("solvable %d (%d):\n", n, i); + } + else + { + if (open) + printf("\n },\n"); + printf(" {\n"); + open = 1; + } + dump_solvable(repo, i, &cbdata); + } + if (dump_json) + { + if (open) + printf("\n }\n"); + printf(" ]\n"); } -#if 0 - tryme(repo, 0, SOLVABLE_MEDIANR, 0, 0); - printf("\n"); - tryme(repo, 0, 0, 0, 0); - printf("\n"); - tryme(repo, 0, 0, "*y*e*", SEARCH_GLOB); -#endif } -#if 0 - printf ("\nSearchresults:\n"); - Dataiterator di; - dataiterator_init(&di, pool, 0, 0, 0, "3", SEARCH_SUB | SEARCH_SUBSTRING | SEARCH_FILES); - /* int count = 0; */ - while (dataiterator_step(&di)) + if (dump_json) { - printf("%d:", di.solvid); - dump_attr(repo, di.data, di.key, &di.kv); - /*if (di.solvid == 4 && count++ == 0) - dataiterator_jump_to_solvable(&di, pool->solvables + 3);*/ - /* dataiterator_skip_attribute(&di); */ - /* dataiterator_skip_solvable(&di); */ - /* dataiterator_skip_repo(&di); */ + if (openrepo) + printf(" }\n"); + printf(" ]\n"); + printf("}\n"); } -#endif pool_free(pool); + solv_free(cbdata.first); exit(0); } |