(De-)Serialize structured types. Dataiterator and repo_search support
[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
13 static int with_attr = 0;
14
15 #include "pool.h"
16 #include "repo_solv.h"
17
18 static void
19 dump_repodata (Repo *repo)
20 {
21   unsigned i;
22   Repodata *data;
23   if (repo->nrepodata == 0)
24     return;
25   printf("repo refers to %d subfiles:\n", repo->nrepodata);
26   for (i = 0, data = repo->repodata; i < repo->nrepodata; i++, data++)
27     {
28       unsigned int j;
29       printf("%s has %d keys, %d schemata\n", data->location ? data->location : "**EMBED**", data->nkeys, data->nschemata);
30       for (j = 1; j < data->nkeys; j++)
31         printf("  %s (type %s size %d storage %d)\n", id2str(repo->pool, data->keys[j].name), id2str(repo->pool, data->keys[j].type), data->keys[j].size, data->keys[j].storage);
32       if (data->localpool)
33         printf("  localpool has %d strings, size is %d\n", data->spool.nstrings, data->spool.sstrings);
34       if (data->dirpool.ndirs)
35         printf("  localpool has %d directories\n", data->dirpool.ndirs);
36       if (data->addedfileprovides)
37         {
38           printf("  added file provides:\n");
39           for (j = 0; data->addedfileprovides[j]; j++)
40             printf("    %s\n", id2str(repo->pool, data->addedfileprovides[j]));
41         }
42       printf("\n");
43     }
44   printf("\n");
45 }
46
47 static void
48 printids(Repo *repo, char *kind, Offset ido)
49 {
50   Pool *pool = repo->pool;
51   Id id, *ids;
52   if (!ido)
53     return;
54   printf("%s:\n", kind);
55   ids = repo->idarraydata + ido;
56   while((id = *ids++) != 0)
57     printf("  %s\n", dep2str(pool, id));
58 }
59
60 int
61 dump_attr(Repo *repo, Repodata *data, Repokey *key, KeyValue *kv)
62 {
63   const char *keyname;
64
65   keyname = id2str(repo->pool, key->name);
66   switch(key->type)
67     {
68     case REPOKEY_TYPE_ID:
69       if (data && data->localpool)
70         kv->str = stringpool_id2str(&data->spool, kv->id);
71       else
72         kv->str = id2str(repo->pool, kv->id);
73       printf("%s: %s\n", keyname, kv->str);
74       break;
75     case REPOKEY_TYPE_CONSTANTID:
76       printf("%s: %s\n", keyname, dep2str(repo->pool, kv->id));
77       break;
78     case REPOKEY_TYPE_IDARRAY:
79       if (data && data->localpool)
80         printf("%s: %s\n", keyname, stringpool_id2str(&data->spool, kv->id));
81       else
82         printf("%s: %s\n", keyname, dep2str(repo->pool, kv->id));
83       break;
84     case REPOKEY_TYPE_STR:
85       printf("%s: %s\n", keyname, kv->str);
86       break;
87     case REPOKEY_TYPE_MD5:
88     case REPOKEY_TYPE_SHA1:
89     case REPOKEY_TYPE_SHA256:
90       printf("%s: %s\n", keyname, repodata_chk2str(data, key->type, (unsigned char *)kv->str));
91       break;
92     case REPOKEY_TYPE_VOID:
93       printf("%s: (void)\n", keyname);
94       break;
95     case REPOKEY_TYPE_U32:
96     case REPOKEY_TYPE_NUM:
97     case REPOKEY_TYPE_CONSTANT:
98       printf("%s: %d\n", keyname, kv->num);
99       break;
100     case REPOKEY_TYPE_DIRNUMNUMARRAY:
101       printf("%s: %s %d %d\n", keyname, repodata_dir2str(data, kv->id, 0), kv->num, kv->num2);
102       break;
103     case REPOKEY_TYPE_DIRSTRARRAY:
104       printf("%s: %s\n", keyname, repodata_dir2str(data, kv->id, kv->str));
105       break;
106     case REPOKEY_TYPE_COUNTED:
107       printf("%s: %s\n", keyname, kv->eof == 0 ? "open" : kv->eof == 1 ? "next" : "close");
108       break;
109     default:
110       printf("%s: ?\n", keyname);
111       break;
112     }
113   return 0;
114 }
115
116 /*static int
117 dump_repoattrs_cb(void *vcbdata, Solvable *s, Repodata *data, Repokey *key, KeyValue *kv)
118 {
119   return dump_attr(s->repo, data, key, kv);
120 }*/
121
122 /*
123  * dump all attributes for Id <p>
124  */
125
126 void
127 dump_repoattrs(Repo *repo, Id p)
128 {
129 #if 0
130   repo_search(repo, p, 0, 0, SEARCH_NO_STORAGE_SOLVABLE, dump_repoattrs_cb, 0);
131 #else
132   Dataiterator di;
133   dataiterator_init(&di, repo, p, 0, 0, SEARCH_NO_STORAGE_SOLVABLE);
134   while (dataiterator_step(&di))
135     dump_attr(repo, di.data, di.key, &di.kv);
136 #endif
137 }
138
139 #if 0
140 void
141 dump_some_attrs(Repo *repo, Solvable *s)
142 {
143   const char *summary = 0;
144   unsigned int medianr = -1, downloadsize = -1;
145   unsigned int time = -1;
146   summary = repo_lookup_str(s, SOLVABLE_SUMMARY);
147   medianr = repo_lookup_num(s, SOLVABLE_MEDIANR);
148   downloadsize = repo_lookup_num (s, SOLVABLE_DOWNLOADSIZE);
149   time = repo_lookup_num(s, SOLVABLE_BUILDTIME);
150   printf ("  XXX %d %d %u %s\n", medianr, downloadsize, time, summary);
151 }
152 #endif
153
154
155 static FILE *
156 loadcallback (Pool *pool, Repodata *data, void *vdata)
157 {
158   FILE *fp = 0;
159   if (data->location && with_attr)
160     {
161       fprintf (stderr, "Loading SOLV file %s\n", data->location);
162       fp = fopen (data->location, "r");
163       if (!fp)
164         perror(data->location);
165     }
166   return fp;
167 }
168
169
170 static void
171 usage( const char *err )
172 {
173   if (err)
174     fprintf (stderr, "\n** Error:\n  %s\n", err);
175   fprintf( stderr, "\nUsage:\n"
176            "dumpsolv [-a] [<solvfile>]\n"
177            "  -a  read attributes.\n"
178            );
179   exit(0);
180 }
181
182 #if 0
183 static void
184 tryme (Repo *repo, Id p, Id keyname, const char *match, int flags)
185 {
186   Dataiterator di;
187   dataiterator_init(&di, repo, p, keyname, match, flags);
188   while (dataiterator_step(&di))
189     {
190       switch (di.key->type)
191         {
192           case REPOKEY_TYPE_ID:
193           case REPOKEY_TYPE_IDARRAY:
194               if (di.data && di.data->localpool)
195                 di.kv.str = stringpool_id2str(&di.data->spool, di.kv.id);
196               else
197                 di.kv.str = id2str(repo->pool, di.kv.id);
198               break;
199           case REPOKEY_TYPE_STR:
200           case REPOKEY_TYPE_DIRSTRARRAY:
201               break;
202           default:
203               di.kv.str = 0;
204         }
205       fprintf (stdout, "found: %d:%s %d %s %d %d %d\n",
206                di.solvid,
207                id2str(repo->pool, di.key->name),
208                di.kv.id,
209                di.kv.str, di.kv.num, di.kv.num2, di.kv.eof);
210     }
211 }
212 #endif
213
214 int main(int argc, char **argv)
215 {
216   Repo *repo;
217   Pool *pool;
218   int i, j, n;
219   Solvable *s;
220   
221   pool = pool_create();
222   pool_setdebuglevel(pool, 1);
223   pool_setloadcallback(pool, loadcallback, 0);
224
225   argv++;
226   argc--;
227   while (argc--)
228     {
229       const char *s = argv[0];
230       if (*s++ == '-')
231         while (*s)
232           switch (*s++)
233             {
234               case 'h': usage(NULL); break;
235               case 'a': with_attr = 1; break;
236               default : break;
237             }
238       else
239         {
240           if (freopen (argv[0], "r", stdin) == 0)
241             {
242               perror(argv[0]);
243               exit(1);
244             }
245           repo = repo_create(pool, argv[0]);
246           if (repo_add_solv(repo, stdin))
247             printf("could not read repository\n");
248         }
249       argv++;
250     }
251
252   if (!pool->nrepos)
253     {
254       repo = repo_create(pool, argc != 1 ? argv[1] : "<stdin>");
255       if (repo_add_solv(repo, stdin))
256         printf("could not read repository\n");
257     }
258   printf("pool contains %d strings, %d rels, string size is %d\n", pool->ss.nstrings, pool->nrels, pool->ss.sstrings);
259   for (j = 0; 1 && j < pool->nrepos; j++)
260     {
261       repo = pool->repos[j];
262       dump_repodata(repo);
263       printf("repo %d contains %d solvables %d non-solvables\n", j, repo->nsolvables, repo->nextra);
264       printf("repo start: %d end: %d\n", repo->start, repo->end);
265       for (i = repo->start, n = 1; i < repo->end; i++)
266         {
267           s = pool->solvables + i;
268           if (s->repo != repo)
269             continue;
270           printf("\n");
271           printf("solvable %d (%d):\n", n, i);
272           if (s->name || s->evr || s->arch)
273             printf("name: %s %s %s\n", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch));
274           if (s->vendor)
275             printf("vendor: %s\n", id2str(pool, s->vendor));
276           printids(repo, "provides", s->provides);
277           printids(repo, "obsoletes", s->obsoletes);
278           printids(repo, "conflicts", s->conflicts);
279           printids(repo, "requires", s->requires);
280           printids(repo, "recommends", s->recommends);
281           printids(repo, "suggests", s->suggests);
282           printids(repo, "supplements", s->supplements);
283           printids(repo, "enhances", s->enhances);
284           printids(repo, "freshens", s->freshens);
285           if (repo->rpmdbid)
286             printf("rpmdbid: %u\n", repo->rpmdbid[i - repo->start]);
287 #if 0
288           dump_attrs (repo, n - 1);
289 #endif
290           dump_repoattrs(repo, i);
291 #if 0
292           dump_some_attrs(repo, s);
293 #endif
294           n++;
295         }
296       for (i = 0; i < repo->nextra; i++)
297         {
298           printf("\nextra %d:\n", i);
299           Dataiterator di;
300           dataiterator_init(&di, repo, -1 - i, 0, 0, SEARCH_EXTRA | SEARCH_NO_STORAGE_SOLVABLE);
301           while (dataiterator_step(&di))
302             dump_attr(repo, di.data, di.key, &di.kv);
303         }
304 #if 0
305       tryme(repo, 0, SOLVABLE_MEDIANR, 0, 0);
306       printf("\n");
307       tryme(repo, 0, 0, 0, 0);
308       printf("\n");
309       tryme(repo, 0, 0, "*y*e*", SEARCH_GLOB);
310 #endif
311     }
312 #if 0
313   printf ("\nSearchresults:\n");
314   Dataiterator di;
315   dataiterator_init(&di, pool->repos[0], 0, 0, "3", SEARCH_EXTRA | SEARCH_SUBSTRING | SEARCH_ALL_REPOS);
316   //int count = 0;
317   while (dataiterator_step(&di))
318     {
319       printf("%d:", di.solvid);
320       dump_attr(repo, di.data, di.key, &di.kv);
321       /*if (di.solvid == 4 && count++ == 0)
322         dataiterator_jump_to_solvable(&di, pool->solvables + 3);*/
323       //dataiterator_skip_attribute(&di);
324       //dataiterator_skip_solvable(&di);
325       //dataiterator_skip_repo(&di);
326     }
327 #endif
328   pool_free(pool);
329   exit(0);
330 }