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