#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"
-#if 0
-#include "attr_store.h"
-#include "attr_store_p.h"
-static void
-dump_attrs_1 (Attrstore *s, unsigned int entry)
+
+static int
+dump_attr(Repo *repo, Repodata *data, Repokey *key, KeyValue *kv)
{
- attr_iterator ai;
- FOR_ATTRS (s, entry, &ai)
+ const char *keyname;
+ KeyValue *kvp;
+ int indent = 0;
+
+ keyname = pool_id2str(repo->pool, key->name);
+ for (kvp = kv; (kvp = kvp->parent) != 0; indent += 2)
+ printf(" ");
+ switch(key->type)
{
- fprintf (stdout, "%s:", id2str (s->pool, ai.name));
- switch (ai.type)
- {
- case TYPE_ATTR_INT:
- fprintf (stdout, "int %u\n", ai.as_int);
- break;
- case TYPE_ATTR_CHUNK:
- {
- const char *str = attr_retrieve_blob (s, ai.as_chunk[0], ai.as_chunk[1]);
- if (str)
- fprintf (stdout, "blob %s\n", str);
- else
- fprintf (stdout, "blob %u+%u\n", ai.as_chunk[0], ai.as_chunk[1]);
- }
- break;
- case TYPE_ATTR_STRING:
- fprintf (stdout, "str %s\n", ai.as_string);
- break;
- case TYPE_ATTR_INTLIST:
- {
- fprintf (stdout, "lint\n ");
- while (1)
- {
- int val;
- get_num (ai.as_numlist, val);
- fprintf (stdout, " %d", (val & 63) | ((val >> 1) & ~63));
- if (!(val & 64))
- break;
- }
- fprintf (stdout, "\n");
- break;
- }
- case TYPE_ATTR_LOCALIDS:
- {
- fprintf (stdout, "lids");
- while (1)
- {
- Id val;
- get_num (ai.as_numlist, val);
- if (!val)
- break;
- fprintf (stdout, "\n %s(%d)", localid2str (s, val), val);
- }
- fprintf (stdout, "\n");
- break;
- }
- default:
- fprintf (stdout, "\n");
- break;
- }
+ case REPOKEY_TYPE_ID:
+ if (data && data->localpool)
+ kv->str = stringpool_id2str(&data->spool, kv->id);
+ else
+ kv->str = pool_dep2str(repo->pool, kv->id);
+ printf("%s: %s\n", keyname, kv->str);
+ break;
+ case REPOKEY_TYPE_CONSTANTID:
+ printf("%s: %s\n", keyname, pool_dep2str(repo->pool, kv->id));
+ break;
+ case REPOKEY_TYPE_IDARRAY:
+ if (!kv->entry)
+ printf("%s:\n%*s", keyname, indent, "");
+ if (data && data->localpool)
+ printf(" %s\n", stringpool_id2str(&data->spool, kv->id));
+ else
+ printf(" %s\n", pool_dep2str(repo->pool, kv->id));
+ break;
+ case REPOKEY_TYPE_STR:
+ printf("%s: %s\n", keyname, kv->str);
+ break;
+ case REPOKEY_TYPE_MD5:
+ case REPOKEY_TYPE_SHA1:
+ case REPOKEY_TYPE_SHA256:
+ printf("%s: %s (%s)\n", keyname, repodata_chk2str(data, key->type, (unsigned char *)kv->str), pool_id2str(repo->pool, key->type));
+ break;
+ case REPOKEY_TYPE_VOID:
+ printf("%s: (void)\n", keyname);
+ break;
+ case REPOKEY_TYPE_U32:
+ case REPOKEY_TYPE_CONSTANT:
+ printf("%s: %u\n", keyname, kv->num);
+ break;
+ case REPOKEY_TYPE_NUM:
+ printf("%s: %llu\n", keyname, SOLV_KV_NUM64(kv));
+ break;
+ case REPOKEY_TYPE_BINARY:
+ if (kv->num)
+ printf("%s: %02x..%02x len %u\n", keyname, (unsigned char)kv->str[0], (unsigned char)kv->str[kv->num - 1], kv->num);
+ else
+ printf("%s: len 0\n", keyname);
+ break;
+ case REPOKEY_TYPE_DIRNUMNUMARRAY:
+ if (!kv->entry)
+ printf("%s:\n%*s", keyname, indent, "");
+ printf(" %s %u %u\n", repodata_dir2str(data, kv->id, 0), kv->num, kv->num2);
+ break;
+ case REPOKEY_TYPE_DIRSTRARRAY:
+ if (!kv->entry)
+ printf("%s:\n%*s", keyname, indent, "");
+ printf(" %s\n", repodata_dir2str(data, kv->id, kv->str));
+ break;
+ case REPOKEY_TYPE_FIXARRAY:
+ case REPOKEY_TYPE_FLEXARRAY:
+ if (!kv->entry)
+ printf("%s:\n", keyname);
+ else
+ printf("\n");
+ break;
+ default:
+ printf("%s: ?\n", keyname);
+ break;
}
+ return 0;
}
-static void
-dump_attrs (Repo *repo, unsigned int entry)
+static const char *
+jsonstring(Pool *pool, const char *s)
{
- unsigned i;
- for (i = 0; i < repo->nrepodata; i++)
+ int needed = 0;
+ const unsigned char *s1;
+ char *r, *rp;
+
+ for (s1 = (const unsigned char *)s; *s1; s1++)
{
- Attrstore *s = repo->repodata[i].s;
- if (s && entry < s->entries)
- dump_attrs_1 (s, entry);
+ if (*s1 < 32)
+ needed += *s1 == '\n' ? 2 : 6;
+ else if (*s1 == '\\' || *s1 == '\"')
+ needed += 2;
+ else
+ needed++;
}
-}
-#endif
-
-static void
-dump_repodata (Repo *repo)
-{
- unsigned i;
- Repodata *data;
- if (repo->nrepodata == 0)
- return;
- printf("repo refers to %d subfiles:\n", repo->nrepodata);
- for (i = 0, data = repo->repodata; i < repo->nrepodata; i++, data++)
+ r = rp = pool_alloctmpspace(pool, needed + 3);
+ *rp++ = '\"';
+ for (s1 = (const unsigned char *)s; *s1; s1++)
{
- unsigned int j;
- printf("%s has %d keys", data->location ? data->location : "**EMBED**", data->nkeys);
- for (j = 1; j < data->nkeys; j++)
- printf("\n %s", id2str(repo->pool, data->keys[j].name));
- printf("\n");
+ 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;
}
- printf("\n");
+ *rp++ = '\"';
+ *rp = 0;
+ return r;
}
-static void
-printids(Repo *repo, char *kind, Offset ido)
+struct cbdata {
+ unsigned char *first;
+ int nfirst;
+ int baseindent;
+};
+
+static int
+dump_attr_json(Repo *repo, Repodata *data, Repokey *key, KeyValue *kv, struct cbdata *cbdata)
{
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", dep2str(pool, id));
-}
+ const char *keyname;
+ KeyValue *kvp;
+ int indent = cbdata->baseindent;
+ int isarray = 0;
+ const char *str;
+ int depth = 0;
-static void
-printdir(Repodata *data, Id dir)
-{
- Id comp;
- Id parent = dirpool_parent(&data->dirpool, dir);
- if (parent)
+ keyname = pool_id2str(repo->pool, key->name);
+ for (kvp = kv; (kvp = kvp->parent) != 0; indent += 4)
+ depth++;
+ if (cbdata->nfirst < depth + 1)
{
- printdir(data, parent);
- putchar('/');
+ cbdata->first = solv_realloc(cbdata->first, depth + 16);
+ memset(cbdata->first + cbdata->nfirst, 0, depth + 16 - cbdata->nfirst);
+ cbdata->nfirst = depth + 16;
}
- comp = dirpool_compid(&data->dirpool, dir);
- if (data->localpool)
- printf("%s", stringpool_id2str(&data->spool, comp));
- else
- printf("%s", id2str(data->repo->pool, comp));
-}
-
-int
-dump_repoattrs_cb(void *vcbdata, Solvable *s, Repodata *data, Repokey *key, KeyValue *kv)
-{
- const char *keyname;
-
- keyname = id2str(data->repo->pool, key->name);
switch(key->type)
{
- case TYPE_ID:
- if (data->localpool)
- kv->str = stringpool_id2str(&data->spool, kv->id);
+ 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
- kv->str = id2str(data->repo->pool, kv->id);
- printf("%s: %s\n", keyname, kv->str);
+ str = pool_dep2str(repo->pool, kv->id);
+ printf("%s", jsonstring(pool, str));
break;
- case TYPE_STR:
- printf("%s: %s\n", keyname, kv->str);
+ 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 TYPE_VOID:
- printf("%s\n", keyname);
+ 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 TYPE_NUM:
- case TYPE_CONSTANT:
- printf("%s: %d\n", keyname, kv->num);
+ case REPOKEY_TYPE_VOID:
+ printf("null");
break;
- case TYPE_DIRNUMNUMARRAY:
- printf("%s: ", keyname);
- printdir(data, kv->id);
- printf(" %d %d\n", kv->num, kv->num2);
+ 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("%s: ?\n", keyname);
+ printf("\"?\"");
break;
}
+ if (isarray == 1)
+ {
+ if (!kv->eof)
+ printf(",\n");
+ else
+ printf("\n%*s]", indent, "");
+ }
return 0;
}
-/*
- * dump all attributes for Id <p>
- */
+static int
+dump_repodata_cb(void *vcbdata, Solvable *s, Repodata *data, Repokey *key, KeyValue *kv)
+{
+ if (key->name == REPOSITORY_SOLVABLES)
+ return SEARCH_NEXT_SOLVABLE;
+ if (!dump_json)
+ return dump_attr(data->repo, data, key, kv);
+ else
+ return dump_attr_json(data->repo, data, key, kv, vcbdata);
+}
-void
-dump_repoattrs(Repo *repo, Id p)
+static void
+dump_repodata(Repo *repo)
{
int i;
Repodata *data;
- /*
- * look through all repodata(s) to find the one covering the right range of Ids
- */
- for (i = 0, data = repo->repodata; i < repo->nrepodata; i++, data++)
+ if (repo->nrepodata == 0)
+ return;
+ printf("repo contains %d repodata sections:\n", repo->nrepodata - 1);
+ FOR_REPODATAS(repo, i, data)
{
- if (data->state == REPODATA_STUB || data->state == REPODATA_ERROR) /* skip repodata of wrong state */
- continue;
- if (p < data->start || p >= data->end) /* skip repodata of wrong range */
- continue;
- repodata_search(data, p - data->start, 0, dump_repoattrs_cb, 0);
+ 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_repodata_cb, 0);
}
+ printf("\n");
}
-void
-dump_some_attrs(Repo *repo, Solvable *s)
+static void
+dump_repodata_json(Repo *repo, struct cbdata *cbdata)
{
- Id name = str2id (repo->pool, "summary", 0);
- const char *summary = 0;
- unsigned int medianr = -1, downloadsize = -1;
- unsigned int time = -1;
- if (name)
- summary = repo_lookup_str (s, name);
- if ((name = str2id (repo->pool, "medianr", 0)))
- medianr = repo_lookup_num (s, name);
- if ((name = str2id (repo->pool, "downloadsize", 0)))
- downloadsize = repo_lookup_num (s, name);
- if ((name = str2id (repo->pool, "time", 0)))
- time = repo_lookup_num (s, name);
-
- printf (" XXX %d %d %u %s\n", medianr, downloadsize, time, summary);
+ int i;
+ Repodata *data;
+ if (repo->nrepodata == 0)
+ return;
+ cbdata->baseindent = 6;
+ FOR_REPODATAS(repo, i, data)
+ repodata_search(data, SOLVID_META, 0, SEARCH_ARRAYSENTINEL|SEARCH_SUB, dump_repodata_cb, cbdata);
}
+/*
+ * dump all attributes for Id <p>
+ */
+
+void
+dump_solvable(Repo *repo, Id p, struct cbdata *cbdata)
+{
+ Dataiterator di;
+ dataiterator_init(&di, repo->pool, repo, p, 0, 0, SEARCH_ARRAYSENTINEL|SEARCH_SUB);
+ if (cbdata && cbdata->first)
+ cbdata->first[0] = 0;
+ if (cbdata)
+ cbdata->baseindent = 10;
+ while (dataiterator_step(&di))
+ {
+ if (!dump_json)
+ dump_attr(repo, di.data, di.key, &di.kv);
+ else
+ dump_attr_json(repo, di.data, di.key, &di.kv, cbdata);
+ }
+ dataiterator_free(&di);
+}
-static FILE *
-loadcallback (Pool *pool, Repodata *data, void *vdata)
+static int
+loadcallback(Pool *pool, Repodata *data, void *vdata)
{
FILE *fp = 0;
- if (data->location && with_attr)
+ int r;
+ const char *location;
+
+ location = repodata_lookup_str(data, SOLVID_META, REPOSITORY_LOCATION);
+ if (!location || !with_attr)
+ return 0;
+ fprintf(stderr, "[Loading SOLV file %s]\n", location);
+ fp = fopen (location, "r");
+ if (!fp)
{
- fprintf (stderr, "Loading SOLV file %s\n", data->location);
- fp = fopen (data->location, "r");
- if (!fp)
- perror(data->location);
+ perror(location);
+ return 0;
}
- return fp;
+ r = repo_add_solv(data->repo, fp, REPO_USE_LOADING|REPO_LOCALPOOL);
+ fclose(fp);
+ return !r ? 1 : 0;
}
static void
-usage( const char *err )
+usage(int status)
{
- if (err)
- fprintf (stderr, "\n** Error:\n %s\n", err);
fprintf( stderr, "\nUsage:\n"
- "dumpsolv [-a] [<solvfile>]\n"
+ "dumpsolv [-a] [-j] [<solvfile>]\n"
" -a read attributes.\n"
+ " -j dump json format.\n"
);
- exit(0);
+ exit(status);
}
-
int main(int argc, char **argv)
{
Repo *repo;
Pool *pool;
- int i, n;
+ int c, i, j, n;
Solvable *s;
- argv++;
- argc--;
- while (argc--)
+ pool = pool_create();
+ pool_setloadcallback(pool, loadcallback, 0);
+
+ while ((c = getopt(argc, argv, "haj")) >= 0)
{
- const char *s = argv[0];
- if (*s++ == '-')
- while (*s)
- switch (*s++)
- {
- case 'h': usage(NULL); break;
- case 'a': with_attr = 1; break;
- default : break;
- }
- else
+ switch(c)
{
- if (freopen (argv[0], "r", stdin) == 0)
- {
- perror(argv[0]);
- exit(1);
- }
+ case 'h':
+ usage(0);
+ break;
+ 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)
+ {
+ perror(argv[optind]);
+ exit(1);
+ }
+ repo = repo_create(pool, argv[optind]);
+ if (repo_add_solv(repo, stdin, 0))
+ {
+ 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))
+ {
+ fprintf(stderr, "could not read repository: %s\n", pool_errstr(pool));
+ exit(1);
}
- argv++;
}
- pool = pool_create();
- pool_setdebuglevel(pool, 1);
- pool_setloadcallback(pool, loadcallback, 0);
+ if (dump_json)
+ {
+ int openrepo = 0;
+ struct cbdata cbdata;
+
+ memset(&cbdata, 0, sizeof(cbdata));
+ printf("{\n");
+ printf(" \"repositories\": [\n");
+ FOR_REPOS(j, repo)
+ {
+ int open = 0;
- repo = repo_create(pool, argc != 1 ? argv[1] : "<stdin>");
- if (repo_add_solv(repo, stdin))
- printf("could not read repository\n");
- dump_repodata (repo);
- printf("repo contains %d solvables\n", repo->nsolvables);
- for (i = repo->start, n = 1; i < repo->end; i++)
+ if (openrepo)
+ printf("\n },");
+ printf(" {\n");
+ openrepo = 1;
+ if (cbdata.first)
+ cbdata.first[0] = 0;
+ dump_repodata_json(repo, &cbdata);
+ if (cbdata.first[0])
+ printf(",\n");
+ printf(" \"solvables\": [\n");
+ FOR_REPO_SOLVABLES(repo, i, s)
+ {
+ if (open)
+ printf("\n },\n");
+ printf(" {\n");
+ open = 1;
+ dump_solvable(repo, i, &cbdata);
+ }
+ if (open)
+ printf("\n }\n");
+ printf(" ]\n");
+ }
+ if (openrepo)
+ printf(" }\n");
+ printf(" ]\n");
+ printf("}\n");
+ solv_free(cbdata.first);
+ }
+ else
{
- s = pool->solvables + i;
- if (s->repo != repo)
- continue;
- printf("\n");
- printf("solvable %d:\n", n);
- if (s->name || s->evr || s->arch)
- printf("name: %s %s %s\n", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch));
- if (s->vendor)
- printf("vendor: %s\n", 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);
- printids(repo, "freshens", s->freshens);
-#if 0
- dump_attrs (repo, n - 1);
-#endif
- dump_repoattrs(repo, i);
-#if 1
- dump_some_attrs(repo, s);
-#endif
- n++;
+ printf("pool contains %d strings, %d rels, string size is %d\n", pool->ss.nstrings, pool->nrels, pool->ss.sstrings);
+ 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);
+ FOR_REPO_SOLVABLES(repo, i, s)
+ {
+ n++;
+ printf("\n");
+ printf("solvable %d (%d):\n", n, i);
+ dump_solvable(repo, i, 0);
+ }
+ }
}
pool_free(pool);
exit(0);