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