2 * Copyright (c) 2007, Novell Inc.
4 * This program is licensed under the BSD license, read LICENSE.BSD
5 * for further information
16 static int dump_userdata;
20 #include "repo_solv.h"
24 dump_attr(Repo *repo, Repodata *data, Repokey *key, KeyValue *kv)
30 keyname = pool_id2str(repo->pool, key->name);
31 for (kvp = kv; (kvp = kvp->parent) != 0; indent += 2)
36 if (data && data->localpool)
37 kv->str = stringpool_id2str(&data->spool, kv->id);
39 kv->str = pool_dep2str(repo->pool, kv->id);
40 printf("%s: %s\n", keyname, kv->str);
42 case REPOKEY_TYPE_CONSTANTID:
43 printf("%s: %s\n", keyname, pool_dep2str(repo->pool, kv->id));
45 case REPOKEY_TYPE_IDARRAY:
47 printf("%s:\n%*s", keyname, indent, "");
48 if (data && data->localpool)
49 printf(" %s\n", stringpool_id2str(&data->spool, kv->id));
51 printf(" %s\n", pool_dep2str(repo->pool, kv->id));
53 case REPOKEY_TYPE_STR:
54 printf("%s: %s\n", keyname, kv->str);
56 case REPOKEY_TYPE_VOID:
57 printf("%s: (void)\n", keyname);
59 case REPOKEY_TYPE_CONSTANT:
60 printf("%s: %u\n", keyname, kv->num);
62 case REPOKEY_TYPE_NUM:
63 printf("%s: %llu\n", keyname, SOLV_KV_NUM64(kv));
65 case REPOKEY_TYPE_BINARY:
67 printf("%s: %02x..%02x len %u\n", keyname, (unsigned char)kv->str[0], (unsigned char)kv->str[kv->num - 1], kv->num);
69 printf("%s: len 0\n", keyname);
71 case REPOKEY_TYPE_DIRNUMNUMARRAY:
73 printf("%s:\n%*s", keyname, indent, "");
74 printf(" %s %u %u\n", repodata_dir2str(data, kv->id, 0), kv->num, kv->num2);
76 case REPOKEY_TYPE_DIRSTRARRAY:
78 printf("%s:\n%*s", keyname, indent, "");
79 printf(" %s\n", repodata_dir2str(data, kv->id, kv->str));
81 case REPOKEY_TYPE_FIXARRAY:
82 case REPOKEY_TYPE_FLEXARRAY:
84 printf("%s:\n", keyname);
89 if (solv_chksum_len(key->type))
91 printf("%s: %s (%s)\n", keyname, repodata_chk2str(data, key->type, (unsigned char *)kv->str), solv_chksum_type2str(key->type));
94 printf("%s: ?\n", keyname);
101 jsonstring(Pool *pool, const char *s)
104 const unsigned char *s1;
107 for (s1 = (const unsigned char *)s; *s1; s1++)
110 needed += *s1 == '\n' ? 2 : 6;
111 else if (*s1 == '\\' || *s1 == '\"')
116 r = rp = pool_alloctmpspace(pool, needed + 3);
118 for (s1 = (const unsigned char *)s; *s1; s1++)
134 *rp++ = (x < 10 ? '0' : 'a' - 10) + x;
136 *rp++ = (x < 10 ? '0' : 'a' - 10) + x;
138 else if (*s1 == '\\' || *s1 == '\"')
152 unsigned char *first;
158 dump_attr_json(Repo *repo, Repodata *data, Repokey *key, KeyValue *kv, struct cbdata *cbdata)
160 Pool *pool = repo->pool;
163 int indent = cbdata->baseindent;
168 keyname = pool_id2str(repo->pool, key->name);
169 for (kvp = kv; (kvp = kvp->parent) != 0; indent += 4)
171 if (cbdata->nfirst < depth + 1)
173 cbdata->first = solv_realloc(cbdata->first, depth + 16);
174 memset(cbdata->first + cbdata->nfirst, 0, depth + 16 - cbdata->nfirst);
175 cbdata->nfirst = depth + 16;
179 case REPOKEY_TYPE_IDARRAY:
180 case REPOKEY_TYPE_DIRNUMNUMARRAY:
181 case REPOKEY_TYPE_DIRSTRARRAY:
184 case REPOKEY_TYPE_FIXARRAY:
185 case REPOKEY_TYPE_FLEXARRAY:
191 if (!isarray || !kv->entry)
193 if (cbdata->first[depth])
195 printf("%*s%s: ", indent, "", jsonstring(pool, keyname));
196 cbdata->first[depth] = 1;
198 if (isarray == 1 && !kv->entry)
199 printf("[\n%*s", indent + 2, "");
200 else if (isarray == 1 && kv->entry)
201 printf("%*s", indent + 2, "");
204 case REPOKEY_TYPE_ID:
205 if (data && data->localpool)
206 str = stringpool_id2str(&data->spool, kv->id);
208 str = pool_dep2str(repo->pool, kv->id);
209 printf("%s", jsonstring(pool, str));
211 case REPOKEY_TYPE_CONSTANTID:
212 str = pool_dep2str(repo->pool, kv->id);
213 printf("%s", jsonstring(pool, str));
215 case REPOKEY_TYPE_IDARRAY:
216 if (data && data->localpool)
217 str = stringpool_id2str(&data->spool, kv->id);
219 str = pool_dep2str(repo->pool, kv->id);
220 printf("%s", jsonstring(pool, str));
222 case REPOKEY_TYPE_STR:
224 printf("%s", jsonstring(pool, str));
226 case REPOKEY_TYPE_VOID:
229 case REPOKEY_TYPE_CONSTANT:
230 printf("%u", kv->num);
232 case REPOKEY_TYPE_NUM:
233 printf("%llu", SOLV_KV_NUM64(kv));
235 case REPOKEY_TYPE_BINARY:
236 printf("\"<binary>\"");
238 case REPOKEY_TYPE_DIRNUMNUMARRAY:
240 printf("%*s \"dir\": %s,\n", indent, "", jsonstring(pool, repodata_dir2str(data, kv->id, 0)));
241 printf("%*s \"num1\": %u,\n", indent, "", kv->num);
242 printf("%*s \"num2\": %u\n", indent, "", kv->num2);
243 printf("%*s }", indent, "");
245 case REPOKEY_TYPE_DIRSTRARRAY:
246 printf("%s", jsonstring(pool, repodata_dir2str(data, kv->id, kv->str)));
248 case REPOKEY_TYPE_FIXARRAY:
249 case REPOKEY_TYPE_FLEXARRAY:
250 cbdata->first[depth + 1] = 0;
256 printf("\n%*s },\n", indent, "");
258 printf("\n%*s }\n", indent, "");
261 printf("%*s {\n", indent, "");
263 printf("%*s]", indent, "");
266 if (solv_chksum_len(key->type))
269 printf("%*s \"value\": %s,\n", indent, "", jsonstring(pool, repodata_chk2str(data, key->type, (unsigned char *)kv->str)));
270 printf("%*s \"type\": %s\n", indent, "", jsonstring(pool, solv_chksum_type2str(key->type)));
271 printf("%*s}", indent, "");
282 printf("\n%*s]", indent, "");
288 dump_repodata_cb(void *vcbdata, Solvable *s, Repodata *data, Repokey *key, KeyValue *kv)
290 if (key->name == REPOSITORY_SOLVABLES)
291 return SEARCH_NEXT_SOLVABLE;
293 return dump_attr(data->repo, data, key, kv);
295 return dump_attr_json(data->repo, data, key, kv, vcbdata);
299 dump_repodata(Repo *repo)
303 if (repo->nrepodata == 0)
305 printf("repo contains %d repodata sections:\n", repo->nrepodata - 1);
306 FOR_REPODATAS(repo, i, data)
309 printf("\nrepodata %d has %d keys, %d schemata\n", i, data->nkeys - 1, data->nschemata - 1);
310 for (j = 1; j < data->nkeys; j++)
311 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);
313 printf(" localpool has %d strings, size is %d\n", data->spool.nstrings, data->spool.sstrings);
314 if (data->dirpool.ndirs)
315 printf(" localpool has %d directories\n", data->dirpool.ndirs);
317 repodata_search(data, SOLVID_META, 0, SEARCH_ARRAYSENTINEL|SEARCH_SUB, dump_repodata_cb, 0);
323 dump_repodata_json(Repo *repo, struct cbdata *cbdata)
327 if (repo->nrepodata == 0)
329 cbdata->baseindent = 6;
330 FOR_REPODATAS(repo, i, data)
331 repodata_search(data, SOLVID_META, 0, SEARCH_ARRAYSENTINEL|SEARCH_SUB, dump_repodata_cb, cbdata);
335 * dump all attributes for Id <p>
339 dump_solvable(Repo *repo, Id p, struct cbdata *cbdata)
342 dataiterator_init(&di, repo->pool, repo, p, 0, 0, SEARCH_ARRAYSENTINEL|SEARCH_SUB);
343 if (cbdata && cbdata->first)
344 cbdata->first[0] = 0;
346 cbdata->baseindent = 10;
347 while (dataiterator_step(&di))
350 dump_attr(repo, di.data, di.key, &di.kv);
352 dump_attr_json(repo, di.data, di.key, &di.kv, cbdata);
354 dataiterator_free(&di);
358 loadcallback(Pool *pool, Repodata *data, void *vdata)
362 const char *location;
364 location = repodata_lookup_str(data, SOLVID_META, REPOSITORY_LOCATION);
365 if (!location || !with_attr)
367 fprintf(stderr, "[Loading SOLV file %s]\n", location);
368 fp = fopen (location, "r");
374 r = repo_add_solv(data->repo, fp, REPO_USE_LOADING|REPO_LOCALPOOL);
383 fprintf( stderr, "\nUsage:\n"
384 "dumpsolv [-a] [-j] [<solvfile>]\n"
385 " -a read attributes.\n"
386 " -j dump json format.\n"
391 int main(int argc, char **argv)
398 while ((c = getopt(argc, argv, "uhaj")) >= 0)
423 for (; optind < argc; optind++)
425 unsigned char *userdata = 0;
426 int r, userdatalen = 0;
427 if (argv[optind] && freopen(argv[optind], "r", stdin) == 0)
429 perror(argv[optind]);
432 r = solv_read_userdata(stdin, &userdata, &userdatalen);
435 fprintf(stderr, "could not read userdata: error %d\n", r);
438 if (dump_userdata > 1)
441 if (userdatalen && fwrite(userdata, userdatalen, 1, stdout) != 1)
449 for (r = 0; r < userdatalen; r++)
450 printf("%02x", userdata[r]);
458 pool = pool_create();
459 pool_setloadcallback(pool, loadcallback, 0);
461 pool_setdebuglevel(pool, 1);
463 pool->debugmask |= SOLV_DEBUG_TO_STDERR;
464 for (; optind < argc; optind++)
466 if (freopen(argv[optind], "r", stdin) == 0)
468 perror(argv[optind]);
471 repo = repo_create(pool, argv[optind]);
472 if (repo_add_solv(repo, stdin, 0))
474 fprintf(stderr, "could not read repository: %s\n", pool_errstr(pool));
480 repo = repo_create(pool, argc != 1 ? argv[1] : "<stdin>");
481 if (repo_add_solv(repo, stdin, 0))
483 fprintf(stderr, "could not read repository: %s\n", pool_errstr(pool));
491 struct cbdata cbdata;
493 memset(&cbdata, 0, sizeof(cbdata));
495 printf(" \"repositories\": [\n");
506 dump_repodata_json(repo, &cbdata);
509 printf(" \"solvables\": [\n");
510 FOR_REPO_SOLVABLES(repo, i, s)
516 dump_solvable(repo, i, &cbdata);
526 solv_free(cbdata.first);
530 printf("pool contains %d strings, %d rels, string size is %d\n", pool->ss.nstrings, pool->nrels, pool->ss.sstrings);
535 printf("repo %d contains %d solvables\n", j, repo->nsolvables);
536 printf("repo start: %d end: %d\n", repo->start, repo->end);
537 FOR_REPO_SOLVABLES(repo, i, s)
541 printf("solvable %d (%d):\n", n, i);
542 dump_solvable(repo, i, 0);