- change solv format in an incompatible way, breaking compatibility
[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", data->location ? data->location : "**EMBED**", data->nkeys, data->nschemata);
30       for (j = 1; j < data->nkeys; j++)
31         printf("\n  %s (type %s size %d storage %d)", id2str(repo->pool, data->keys[j].name), id2str(repo->pool, data->keys[j].type), data->keys[j].size, data->keys[j].storage);
32       printf("\n");
33     }
34   printf("\n");
35 }
36
37 static void
38 printids(Repo *repo, char *kind, Offset ido)
39 {
40   Pool *pool = repo->pool;
41   Id id, *ids;
42   if (!ido)
43     return;
44   printf("%s:\n", kind);
45   ids = repo->idarraydata + ido;
46   while((id = *ids++) != 0)
47     printf("  %s\n", dep2str(pool, id));
48 }
49
50 static void
51 printdir(Repodata *data, Id dir)
52 {
53   Id comp;
54   Id parent = dirpool_parent(&data->dirpool, dir);
55   if (parent)
56     {
57       printdir(data, parent);
58       putchar('/');
59     }
60   comp = dirpool_compid(&data->dirpool, dir);
61   if (data->localpool)
62     printf("%s", stringpool_id2str(&data->spool, comp));
63   else
64     printf("%s", id2str(data->repo->pool, comp));
65 }
66
67 int
68 dump_repoattrs_cb(void *vcbdata, Solvable *s, Repodata *data, Repokey *key, KeyValue *kv)
69 {
70   const char *keyname;
71
72   keyname = id2str(s->repo->pool, key->name);
73   switch(key->type)
74     {
75     case REPOKEY_TYPE_ID:
76       if (data && data->localpool)
77         kv->str = stringpool_id2str(&data->spool, kv->id);
78       else
79         kv->str = id2str(s->repo->pool, kv->id);
80       printf("%s: %s\n", keyname, kv->str);
81       break;
82     case REPOKEY_TYPE_CONSTANTID:
83       printf("%s: %s\n", keyname, dep2str(s->repo->pool, kv->id));
84       break;
85     case REPOKEY_TYPE_IDARRAY:
86       if (data && data->localpool)
87         printf("%s: %s\n", keyname, stringpool_id2str(&data->spool, kv->id));
88       else
89         printf("%s: %s\n", keyname, dep2str(s->repo->pool, kv->id));
90       break;
91     case REPOKEY_TYPE_STR:
92       printf("%s: %s\n", keyname, kv->str);
93       break;
94     case REPOKEY_TYPE_VOID:
95       printf("%s: (void)\n", keyname);
96       break;
97     case REPOKEY_TYPE_U32:
98     case REPOKEY_TYPE_NUM:
99     case REPOKEY_TYPE_CONSTANT:
100       printf("%s: %d\n", keyname, kv->num);
101       break;
102     case REPOKEY_TYPE_DIRNUMNUMARRAY:
103       printf("%s: ", keyname);
104       printdir(data, kv->id);
105       printf(" %d %d\n", kv->num, kv->num2);
106       break;
107     case REPOKEY_TYPE_DIRSTRARRAY:
108       printf("%s: ", keyname);
109       printdir(data, kv->id);
110       printf("/%s\n", kv->str);
111       break;
112     default:
113       printf("%s: ?\n", keyname);
114       break;
115     }
116   return 0;
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_repoattrs_cb(0, repo->pool->solvables + di.solvid, di.data, di.key,
133                       &di.kv);
134 #endif
135 }
136
137 #if 0
138 void
139 dump_some_attrs(Repo *repo, Solvable *s)
140 {
141   const char *summary = 0;
142   unsigned int medianr = -1, downloadsize = -1;
143   unsigned int time = -1;
144   summary = repo_lookup_str(s, SOLVABLE_SUMMARY);
145   medianr = repo_lookup_num(s, SOLVABLE_MEDIANR);
146   downloadsize = repo_lookup_num (s, SOLVABLE_DOWNLOADSIZE);
147   time = repo_lookup_num(s, SOLVABLE_BUILDTIME);
148   printf ("  XXX %d %d %u %s\n", medianr, downloadsize, time, summary);
149 }
150 #endif
151
152
153 static FILE *
154 loadcallback (Pool *pool, Repodata *data, void *vdata)
155 {
156   FILE *fp = 0;
157   if (data->location && with_attr)
158     {
159       fprintf (stderr, "Loading SOLV file %s\n", data->location);
160       fp = fopen (data->location, "r");
161       if (!fp)
162         perror(data->location);
163     }
164   return fp;
165 }
166
167
168 static void
169 usage( const char *err )
170 {
171   if (err)
172     fprintf (stderr, "\n** Error:\n  %s\n", err);
173   fprintf( stderr, "\nUsage:\n"
174            "dumpsolv [-a] [<solvfile>]\n"
175            "  -a  read attributes.\n"
176            );
177   exit(0);
178 }
179
180 #if 0
181 static void
182 tryme (Repo *repo, Id p, Id keyname, const char *match, int flags)
183 {
184   Dataiterator di;
185   dataiterator_init(&di, repo, p, keyname, match, flags);
186   while (dataiterator_step(&di))
187     {
188       switch (di.key->type)
189         {
190           case REPOKEY_TYPE_ID:
191           case REPOKEY_TYPE_IDARRAY:
192               if (di.data && di.data->localpool)
193                 di.kv.str = stringpool_id2str(&di.data->spool, di.kv.id);
194               else
195                 di.kv.str = id2str(repo->pool, di.kv.id);
196               break;
197           case REPOKEY_TYPE_STR:
198           case REPOKEY_TYPE_DIRSTRARRAY:
199               break;
200           default:
201               di.kv.str = 0;
202         }
203       fprintf (stdout, "found: %d:%s %d %s %d %d %d\n",
204                di.solvid,
205                id2str(repo->pool, di.key->name),
206                di.kv.id,
207                di.kv.str, di.kv.num, di.kv.num2, di.kv.eof);
208     }
209 }
210 #endif
211
212 int main(int argc, char **argv)
213 {
214   Repo *repo;
215   Pool *pool;
216   int i, n;
217   Solvable *s;
218   
219   argv++;
220   argc--;
221   while (argc--)
222     {
223       const char *s = argv[0];
224       if (*s++ == '-')
225         while (*s)
226           switch (*s++)
227             {
228               case 'h': usage(NULL); break;
229               case 'a': with_attr = 1; break;
230               default : break;
231             }
232       else
233         {
234           if (freopen (argv[0], "r", stdin) == 0)
235             {
236               perror(argv[0]);
237               exit(1);
238             }
239           break;
240         }
241       argv++;
242     }
243
244   pool = pool_create();
245   pool_setdebuglevel(pool, 1);
246   pool_setloadcallback(pool, loadcallback, 0);
247
248   repo = repo_create(pool, argc != 1 ? argv[1] : "<stdin>");
249   if (repo_add_solv(repo, stdin))
250     printf("could not read repository\n");
251   dump_repodata (repo);
252   printf("repo contains %d solvables\n", repo->nsolvables);
253   for (i = repo->start, n = 1; i < repo->end; i++)
254     {
255       s = pool->solvables + i;
256       if (s->repo != repo)
257         continue;
258       printf("\n");
259       printf("solvable %d:\n", n);
260       if (s->name || s->evr || s->arch)
261         printf("name: %s %s %s\n", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch));
262       if (s->vendor)
263         printf("vendor: %s\n", id2str(pool, s->vendor));
264       printids(repo, "provides", s->provides);
265       printids(repo, "obsoletes", s->obsoletes);
266       printids(repo, "conflicts", s->conflicts);
267       printids(repo, "requires", s->requires);
268       printids(repo, "recommends", s->recommends);
269       printids(repo, "suggests", s->suggests);
270       printids(repo, "supplements", s->supplements);
271       printids(repo, "enhances", s->enhances);
272       printids(repo, "freshens", s->freshens);
273       if (repo->rpmdbid)
274         printf("rpmdbid: %u\n", repo->rpmdbid[i - repo->start]);
275 #if 0
276       dump_attrs (repo, n - 1);
277 #endif
278       dump_repoattrs(repo, i);
279 #if 0
280       dump_some_attrs(repo, s);
281 #endif
282       n++;
283     }
284 #if 0
285   tryme(repo, 0, SOLVABLE_MEDIANR, 0, 0);
286   printf("\n");
287   tryme(repo, 0, 0, 0, 0);
288   printf("\n");
289   tryme(repo, 0, 0, "*y*e*", SEARCH_GLOB);
290 #endif
291   pool_free(pool);
292   exit(0);
293 }