Imported Upstream version 0.7.27
[platform/upstream/libsolv.git] / tools / dumpsolv.c
1 /*
2  * Copyright (c) 2007, Novell Inc.
3  *
4  * This program is licensed under the BSD license, read LICENSE.BSD
5  * for further information
6  */
7
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <string.h>
12 #include <getopt.h>
13
14 static int with_attr;
15 static int dump_json;
16 static int dump_userdata;
17
18 #include "pool.h"
19 #include "chksum.h"
20 #include "repo_solv.h"
21
22
23 static int
24 dump_attr(Repo *repo, Repodata *data, Repokey *key, KeyValue *kv)
25 {
26   const char *keyname;
27   KeyValue *kvp;
28   int indent = 0;
29
30   keyname = pool_id2str(repo->pool, key->name);
31   for (kvp = kv; (kvp = kvp->parent) != 0; indent += 2)
32     printf("  ");
33   switch(key->type)
34     {
35     case REPOKEY_TYPE_ID:
36       if (data && data->localpool)
37         kv->str = stringpool_id2str(&data->spool, kv->id);
38       else
39         kv->str = pool_dep2str(repo->pool, kv->id);
40       printf("%s: %s\n", keyname, kv->str);
41       break;
42     case REPOKEY_TYPE_CONSTANTID:
43       printf("%s: %s\n", keyname, pool_dep2str(repo->pool, kv->id));
44       break;
45     case REPOKEY_TYPE_IDARRAY:
46       if (!kv->entry)
47         printf("%s:\n%*s", keyname, indent, "");
48       if (data && data->localpool)
49         printf("  %s\n", stringpool_id2str(&data->spool, kv->id));
50       else
51         printf("  %s\n", pool_dep2str(repo->pool, kv->id));
52       break;
53     case REPOKEY_TYPE_STR:
54       printf("%s: %s\n", keyname, kv->str);
55       break;
56     case REPOKEY_TYPE_VOID:
57       printf("%s: (void)\n", keyname);
58       break;
59     case REPOKEY_TYPE_CONSTANT:
60       printf("%s: %u\n", keyname, kv->num);
61       break;
62     case REPOKEY_TYPE_NUM:
63       printf("%s: %llu\n", keyname, SOLV_KV_NUM64(kv));
64       break;
65     case REPOKEY_TYPE_BINARY:
66       if (kv->num)
67         printf("%s: %02x..%02x len %u\n", keyname, (unsigned char)kv->str[0], (unsigned char)kv->str[kv->num - 1], kv->num);
68       else
69         printf("%s: len 0\n", keyname);
70       break;
71     case REPOKEY_TYPE_DIRNUMNUMARRAY:
72       if (!kv->entry)
73         printf("%s:\n%*s", keyname, indent, "");
74       printf("  %s %u %u\n", repodata_dir2str(data, kv->id, 0), kv->num, kv->num2);
75       break;
76     case REPOKEY_TYPE_DIRSTRARRAY:
77       if (!kv->entry)
78         printf("%s:\n%*s", keyname, indent, "");
79       printf("  %s\n", repodata_dir2str(data, kv->id, kv->str));
80       break;
81     case REPOKEY_TYPE_FIXARRAY:
82     case REPOKEY_TYPE_FLEXARRAY:
83       if (!kv->entry)
84         printf("%s:\n", keyname);
85       else
86         printf("\n");
87       break;
88     default:
89       if (solv_chksum_len(key->type))
90         {
91           printf("%s: %s (%s)\n", keyname, repodata_chk2str(data, key->type, (unsigned char *)kv->str), solv_chksum_type2str(key->type));
92           break;
93         }
94       printf("%s: ?\n", keyname);
95       break;
96     }
97   return 0;
98 }
99
100 static const char *
101 jsonstring(Pool *pool, const char *s)
102 {
103   int needed = 0;
104   const unsigned char *s1;
105   char *r, *rp;
106   
107   for (s1 = (const unsigned char *)s; *s1; s1++)
108     {
109       if (*s1 < 32)
110         needed += *s1 == '\n' ? 2 : 6;
111       else if (*s1 == '\\' || *s1 == '\"')
112         needed += 2;
113       else
114         needed++;
115     }
116   r = rp = pool_alloctmpspace(pool, needed + 3);
117   *rp++ = '\"';
118   for (s1 = (const unsigned char *)s; *s1; s1++)
119     {
120       if (*s1 < 32)
121         {
122           int x;
123           if (*s1 == '\n')
124             {
125               *rp++ = '\\';
126               *rp++ = 'n';
127               continue;
128             }
129           *rp++ = '\\';
130           *rp++ = 'u';
131           *rp++ = '0';
132           *rp++ = '0';
133           x = *s1 / 16;
134           *rp++ = (x < 10 ? '0' : 'a' - 10) + x;
135           x = *s1 & 15;
136           *rp++ = (x < 10 ? '0' : 'a' - 10) + x;
137         }
138       else if (*s1 == '\\' || *s1 == '\"')
139         {
140           *rp++ = '\\';
141           *rp++ = *s1;
142         }
143       else
144         *rp++ = *s1;
145     }
146   *rp++ = '\"';
147   *rp = 0;
148   return r;
149 }
150
151 struct cbdata {
152   unsigned char *first;
153   int nfirst;
154   int baseindent;
155 };
156
157 static int
158 dump_attr_json(Repo *repo, Repodata *data, Repokey *key, KeyValue *kv, struct cbdata *cbdata)
159 {
160   Pool *pool = repo->pool;
161   const char *keyname;
162   KeyValue *kvp;
163   int indent = cbdata->baseindent;
164   int isarray = 0;
165   const char *str;
166   int depth = 0;
167
168   keyname = pool_id2str(repo->pool, key->name);
169   for (kvp = kv; (kvp = kvp->parent) != 0; indent += 4)
170     depth++;
171   if (cbdata->nfirst < depth + 1)
172     {
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;
176     }
177   switch(key->type)
178     {
179     case REPOKEY_TYPE_IDARRAY:
180     case REPOKEY_TYPE_DIRNUMNUMARRAY:
181     case REPOKEY_TYPE_DIRSTRARRAY:
182       isarray = 1;
183       break;
184     case REPOKEY_TYPE_FIXARRAY:
185     case REPOKEY_TYPE_FLEXARRAY:
186       isarray = 2;
187       break;
188     default:
189       break;
190     }
191   if (!isarray || !kv->entry)
192     {
193       if (cbdata->first[depth])
194         printf(",\n");
195       printf("%*s%s: ", indent, "", jsonstring(pool, keyname));
196       cbdata->first[depth] = 1;
197     }
198   if (isarray == 1 && !kv->entry)
199     printf("[\n%*s", indent + 2, "");
200   else if (isarray == 1 && kv->entry)
201     printf("%*s", indent + 2, "");
202   switch(key->type)
203     {
204     case REPOKEY_TYPE_ID:
205       if (data && data->localpool)
206         str = stringpool_id2str(&data->spool, kv->id);
207       else
208         str = pool_dep2str(repo->pool, kv->id);
209       printf("%s", jsonstring(pool, str));
210       break;
211     case REPOKEY_TYPE_CONSTANTID:
212       str = pool_dep2str(repo->pool, kv->id);
213       printf("%s", jsonstring(pool, str));
214       break;
215     case REPOKEY_TYPE_IDARRAY:
216       if (data && data->localpool)
217         str = stringpool_id2str(&data->spool, kv->id);
218       else
219         str = pool_dep2str(repo->pool, kv->id);
220       printf("%s", jsonstring(pool, str));
221       break;
222     case REPOKEY_TYPE_STR:
223       str = kv->str;
224       printf("%s", jsonstring(pool, str));
225       break;
226     case REPOKEY_TYPE_VOID:
227       printf("null");
228       break;
229     case REPOKEY_TYPE_CONSTANT:
230       printf("%u", kv->num);
231       break;
232     case REPOKEY_TYPE_NUM:
233       printf("%llu", SOLV_KV_NUM64(kv));
234       break;
235     case REPOKEY_TYPE_BINARY:
236       printf("\"<binary>\"");
237       break;
238     case REPOKEY_TYPE_DIRNUMNUMARRAY:
239       printf("{\n");
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, "");
244       break;
245     case REPOKEY_TYPE_DIRSTRARRAY:
246       printf("%s", jsonstring(pool, repodata_dir2str(data, kv->id, kv->str)));
247       break;
248     case REPOKEY_TYPE_FIXARRAY:
249     case REPOKEY_TYPE_FLEXARRAY:
250       cbdata->first[depth + 1] = 0;
251       if (!kv->entry)
252         printf("[\n");
253       else
254         {
255           if (kv->eof != 2)
256             printf("\n%*s  },\n", indent, "");
257           else
258             printf("\n%*s  }\n", indent, "");
259         }
260       if (kv->eof != 2)
261         printf("%*s  {\n", indent, "");
262       else
263         printf("%*s]", indent, "");
264       break;
265     default:
266       if (solv_chksum_len(key->type))
267         {
268           printf("{\n");
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, "");
272           break;
273         }
274       printf("\"?\"");
275       break;
276     }
277   if (isarray == 1)
278     {
279       if (!kv->eof)
280         printf(",\n");
281       else
282         printf("\n%*s]", indent, "");
283     }
284   return 0;
285 }
286
287 static int
288 dump_repodata_cb(void *vcbdata, Solvable *s, Repodata *data, Repokey *key, KeyValue *kv)
289 {
290   if (key->name == REPOSITORY_SOLVABLES)
291     return SEARCH_NEXT_SOLVABLE;
292   if (!dump_json)
293     return dump_attr(data->repo, data, key, kv);
294   else
295     return dump_attr_json(data->repo, data, key, kv, vcbdata);
296 }
297
298 static void
299 dump_repodata(Repo *repo)
300 {
301   int i;
302   Repodata *data;
303   if (repo->nrepodata == 0)
304     return;
305   printf("repo contains %d repodata sections:\n", repo->nrepodata - 1);
306   FOR_REPODATAS(repo, i, data)
307     {
308       unsigned int j;
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);
312       if (data->localpool)
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);
316       printf("\n");
317       repodata_search(data, SOLVID_META, 0, SEARCH_ARRAYSENTINEL|SEARCH_SUB, dump_repodata_cb, 0);
318     }
319   printf("\n");
320 }
321
322 static void
323 dump_repodata_json(Repo *repo, struct cbdata *cbdata)
324 {
325   int i;
326   Repodata *data;
327   if (repo->nrepodata == 0)
328     return;
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);
332 }
333
334 /*
335  * dump all attributes for Id <p>
336  */
337
338 void
339 dump_solvable(Repo *repo, Id p, struct cbdata *cbdata)
340 {
341   Dataiterator di;
342   dataiterator_init(&di, repo->pool, repo, p, 0, 0, SEARCH_ARRAYSENTINEL|SEARCH_SUB);
343   if (cbdata && cbdata->first)
344     cbdata->first[0] = 0;
345   if (cbdata)
346     cbdata->baseindent = 10;
347   while (dataiterator_step(&di))
348     {
349       if (!dump_json)
350         dump_attr(repo, di.data, di.key, &di.kv);
351       else
352         dump_attr_json(repo, di.data, di.key, &di.kv, cbdata);
353     }
354   dataiterator_free(&di);
355 }
356
357 static int
358 loadcallback(Pool *pool, Repodata *data, void *vdata)
359 {
360   FILE *fp = 0;
361   int r;
362   const char *location;
363
364   location = repodata_lookup_str(data, SOLVID_META, REPOSITORY_LOCATION);
365   if (!location || !with_attr)
366     return 0;
367   fprintf(stderr, "[Loading SOLV file %s]\n", location);
368   fp = fopen (location, "r");
369   if (!fp)
370     {
371       perror(location);
372       return 0;
373     }
374   r = repo_add_solv(data->repo, fp, REPO_USE_LOADING|REPO_LOCALPOOL);
375   fclose(fp);
376   return !r ? 1 : 0;
377 }
378
379
380 static void
381 usage(int status)
382 {
383   fprintf( stderr, "\nUsage:\n"
384            "dumpsolv [-a] [-j] [<solvfile>]\n"
385            "  -a  read attributes.\n"
386            "  -j  dump json format.\n"
387            );
388   exit(status);
389 }
390
391 int main(int argc, char **argv)
392 {
393   Repo *repo;
394   Pool *pool;
395   int c, i, j, n;
396   Solvable *s;
397   
398   while ((c = getopt(argc, argv, "uhaj")) >= 0)
399     {
400       switch(c)
401         {
402         case 'h':
403           usage(0);
404           break;
405         case 'a':
406           with_attr = 1;
407           break;
408         case 'j':
409           dump_json = 1;
410           break;
411         case 'u':
412           dump_userdata++;
413           break;
414         default:
415           usage(1);
416           break;
417         }
418     }
419   if (dump_userdata)
420     {
421       if (optind == argc)
422         argc++;
423       for (; optind < argc; optind++)
424         {
425           unsigned char *userdata = 0;
426           int r, userdatalen = 0;
427           if (argv[optind] && freopen(argv[optind], "r", stdin) == 0)
428             {
429               perror(argv[optind]);
430               exit(1);
431             }
432           r = solv_read_userdata(stdin, &userdata, &userdatalen);
433           if (r)
434             {
435               fprintf(stderr, "could not read userdata: error %d\n", r);
436               exit(1);
437             }
438           if (dump_userdata > 1)
439             {
440               /* dump raw */
441               if (userdatalen && fwrite(userdata, userdatalen, 1, stdout) != 1)
442                 {
443                   perror("fwrite");
444                   exit(1);
445                 }
446             }
447           else
448             {
449               for (r = 0; r < userdatalen; r++)
450                 printf("%02x", userdata[r]);
451               printf("\n");
452             }
453           solv_free(userdata);
454         }
455       exit(0);
456     }
457
458   pool = pool_create();
459   pool_setloadcallback(pool, loadcallback, 0);
460   if (!dump_json)
461     pool_setdebuglevel(pool, 1);
462   if (dump_json)
463     pool->debugmask |= SOLV_DEBUG_TO_STDERR;
464   for (; optind < argc; optind++)
465     {
466       if (freopen(argv[optind], "r", stdin) == 0)
467         {
468           perror(argv[optind]);
469           exit(1);
470         }
471       repo = repo_create(pool, argv[optind]);
472       if (repo_add_solv(repo, stdin, 0))
473         {
474           fprintf(stderr, "could not read repository: %s\n", pool_errstr(pool));
475           exit(1);
476         }
477     }
478   if (!pool->urepos)
479     {
480       repo = repo_create(pool, argc != 1 ? argv[1] : "<stdin>");
481       if (repo_add_solv(repo, stdin, 0))
482         {
483           fprintf(stderr, "could not read repository: %s\n", pool_errstr(pool));
484           exit(1);
485         }
486     }
487
488   if (dump_json)
489     {
490       int openrepo = 0;
491       struct cbdata cbdata;
492
493       memset(&cbdata, 0, sizeof(cbdata));
494       printf("{\n");
495       printf("  \"repositories\": [\n");
496       FOR_REPOS(j, repo)
497         {
498           int open = 0;
499
500           if (openrepo)
501             printf("\n    },");
502           printf("    {\n");
503           openrepo = 1;
504           if (cbdata.first)
505             cbdata.first[0] = 0;
506           dump_repodata_json(repo, &cbdata);
507           if (cbdata.first[0])
508             printf(",\n");
509           printf("      \"solvables\": [\n");
510           FOR_REPO_SOLVABLES(repo, i, s)
511             {
512               if (open)
513                 printf("\n        },\n");
514               printf("        {\n");
515               open = 1;
516               dump_solvable(repo, i, &cbdata);
517             }
518           if (open)
519             printf("\n        }\n");
520           printf("      ]\n");
521         }
522       if (openrepo)
523         printf("    }\n");
524       printf("  ]\n");
525       printf("}\n");
526       solv_free(cbdata.first);
527     }
528   else
529     {
530       printf("pool contains %d strings, %d rels, string size is %d\n", pool->ss.nstrings, pool->nrels, pool->ss.sstrings);
531       n = 0;
532       FOR_REPOS(j, repo)
533         {
534           dump_repodata(repo);
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)
538             {
539               n++;
540               printf("\n");
541               printf("solvable %d (%d):\n", n, i);
542               dump_solvable(repo, i, 0);
543             }
544         }
545     }
546   pool_free(pool);
547   exit(0);
548 }