Support loading/dumping multiple repos, and some testcode for the new
[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     default:
107       printf("%s: ?\n", keyname);
108       break;
109     }
110   return 0;
111 }
112
113 static int
114 dump_repoattrs_cb(void *vcbdata, Solvable *s, Repodata *data, Repokey *key, KeyValue *kv)
115 {
116   return dump_attr(s->repo, data, key, kv);
117 }
118
119 /*
120  * dump all attributes for Id <p>
121  */
122
123 void
124 dump_repoattrs(Repo *repo, Id p)
125 {
126 #if 1
127   repo_search(repo, p, 0, 0, SEARCH_NO_STORAGE_SOLVABLE, dump_repoattrs_cb, 0);
128 #else
129   Dataiterator di;
130   dataiterator_init(&di, repo, p, 0, 0, SEARCH_NO_STORAGE_SOLVABLE);
131   while (dataiterator_step(&di))
132     dump_attr(repo, di.data, di.key, &di.kv);
133 #endif
134 }
135
136 #if 0
137 void
138 dump_some_attrs(Repo *repo, Solvable *s)
139 {
140   const char *summary = 0;
141   unsigned int medianr = -1, downloadsize = -1;
142   unsigned int time = -1;
143   summary = repo_lookup_str(s, SOLVABLE_SUMMARY);
144   medianr = repo_lookup_num(s, SOLVABLE_MEDIANR);
145   downloadsize = repo_lookup_num (s, SOLVABLE_DOWNLOADSIZE);
146   time = repo_lookup_num(s, SOLVABLE_BUILDTIME);
147   printf ("  XXX %d %d %u %s\n", medianr, downloadsize, time, summary);
148 }
149 #endif
150
151
152 static FILE *
153 loadcallback (Pool *pool, Repodata *data, void *vdata)
154 {
155   FILE *fp = 0;
156   if (data->location && with_attr)
157     {
158       fprintf (stderr, "Loading SOLV file %s\n", data->location);
159       fp = fopen (data->location, "r");
160       if (!fp)
161         perror(data->location);
162     }
163   return fp;
164 }
165
166
167 static void
168 usage( const char *err )
169 {
170   if (err)
171     fprintf (stderr, "\n** Error:\n  %s\n", err);
172   fprintf( stderr, "\nUsage:\n"
173            "dumpsolv [-a] [<solvfile>]\n"
174            "  -a  read attributes.\n"
175            );
176   exit(0);
177 }
178
179 #if 0
180 static void
181 tryme (Repo *repo, Id p, Id keyname, const char *match, int flags)
182 {
183   Dataiterator di;
184   dataiterator_init(&di, repo, p, keyname, match, flags);
185   while (dataiterator_step(&di))
186     {
187       switch (di.key->type)
188         {
189           case REPOKEY_TYPE_ID:
190           case REPOKEY_TYPE_IDARRAY:
191               if (di.data && di.data->localpool)
192                 di.kv.str = stringpool_id2str(&di.data->spool, di.kv.id);
193               else
194                 di.kv.str = id2str(repo->pool, di.kv.id);
195               break;
196           case REPOKEY_TYPE_STR:
197           case REPOKEY_TYPE_DIRSTRARRAY:
198               break;
199           default:
200               di.kv.str = 0;
201         }
202       fprintf (stdout, "found: %d:%s %d %s %d %d %d\n",
203                di.solvid,
204                id2str(repo->pool, di.key->name),
205                di.kv.id,
206                di.kv.str, di.kv.num, di.kv.num2, di.kv.eof);
207     }
208 }
209 #endif
210
211 int main(int argc, char **argv)
212 {
213   Repo *repo;
214   Pool *pool;
215   int i, j, n;
216   Solvable *s;
217   
218   pool = pool_create();
219   pool_setdebuglevel(pool, 1);
220   pool_setloadcallback(pool, loadcallback, 0);
221
222   argv++;
223   argc--;
224   while (argc--)
225     {
226       const char *s = argv[0];
227       if (*s++ == '-')
228         while (*s)
229           switch (*s++)
230             {
231               case 'h': usage(NULL); break;
232               case 'a': with_attr = 1; break;
233               default : break;
234             }
235       else
236         {
237           if (freopen (argv[0], "r", stdin) == 0)
238             {
239               perror(argv[0]);
240               exit(1);
241             }
242           repo = repo_create(pool, argv[0]);
243           if (repo_add_solv(repo, stdin))
244             printf("could not read repository\n");
245         }
246       argv++;
247     }
248
249   if (!pool->nrepos)
250     {
251       repo = repo_create(pool, argc != 1 ? argv[1] : "<stdin>");
252       if (repo_add_solv(repo, stdin))
253         printf("could not read repository\n");
254     }
255   printf("pool contains %d strings, %d rels, string size is %d\n", pool->ss.nstrings, pool->nrels, pool->ss.sstrings);
256   for (j = 0; 1 && j < pool->nrepos; j++)
257     {
258       repo = pool->repos[j];
259       dump_repodata(repo);
260       printf("repo %d contains %d solvables %d non-solvables\n", j, repo->nsolvables, repo->nextra);
261       for (i = repo->start, n = 1; i < repo->end; i++)
262         {
263           s = pool->solvables + i;
264           if (s->repo != repo)
265             continue;
266           printf("\n");
267           printf("solvable %d:\n", n);
268           if (s->name || s->evr || s->arch)
269             printf("name: %s %s %s\n", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch));
270           if (s->vendor)
271             printf("vendor: %s\n", id2str(pool, s->vendor));
272           printids(repo, "provides", s->provides);
273           printids(repo, "obsoletes", s->obsoletes);
274           printids(repo, "conflicts", s->conflicts);
275           printids(repo, "requires", s->requires);
276           printids(repo, "recommends", s->recommends);
277           printids(repo, "suggests", s->suggests);
278           printids(repo, "supplements", s->supplements);
279           printids(repo, "enhances", s->enhances);
280           printids(repo, "freshens", s->freshens);
281           if (repo->rpmdbid)
282             printf("rpmdbid: %u\n", repo->rpmdbid[i - repo->start]);
283 #if 0
284           dump_attrs (repo, n - 1);
285 #endif
286           dump_repoattrs(repo, i);
287 #if 0
288           dump_some_attrs(repo, s);
289 #endif
290           n++;
291         }
292       for (i = 0; i < repo->nextra; i++)
293         {
294           printf("\nextra %d:\n", i);
295           Dataiterator di;
296           dataiterator_init(&di, repo, -1 - i, 0, 0, SEARCH_EXTRA | SEARCH_NO_STORAGE_SOLVABLE);
297           while (dataiterator_step(&di))
298             dump_attr(repo, di.data, di.key, &di.kv);
299         }
300 #if 0
301       tryme(repo, 0, SOLVABLE_MEDIANR, 0, 0);
302       printf("\n");
303       tryme(repo, 0, 0, 0, 0);
304       printf("\n");
305       tryme(repo, 0, 0, "*y*e*", SEARCH_GLOB);
306 #endif
307     }
308 #if 0
309   printf ("\nSearchresults:\n");
310   Dataiterator di;
311   dataiterator_init(&di, pool->repos[0], 0, 0, "3", SEARCH_EXTRA | SEARCH_SUBSTRING | SEARCH_ALL_REPOS);
312   //int count = 0;
313   while (dataiterator_step(&di))
314     {
315       printf("%d:", di.solvid);
316       dump_attr(repo, di.data, di.key, &di.kv);
317       /*if (di.solvid == 4 && count++ == 0)
318         dataiterator_jump_to_solvable(&di, pool->solvables + 3);*/
319       //dataiterator_skip_attribute(&di);
320       //dataiterator_skip_solvable(&di);
321       //dataiterator_skip_repo(&di);
322     }
323 #endif
324   pool_free(pool);
325   exit(0);
326 }