37bed03dbe0963d1ecb8f6cb2b213e6d257a00a2
[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 #if 0
18 #include "attr_store.h"
19 #include "attr_store_p.h"
20
21 static void
22 dump_attrs_1 (Attrstore *s, unsigned int entry)
23 {
24   attr_iterator ai;
25   FOR_ATTRS (s, entry, &ai)
26     {
27       fprintf (stdout, "%s:", id2str (s->pool, ai.name));
28       switch (ai.type)
29         {
30         case TYPE_ATTR_INT:
31           fprintf (stdout, "int  %u\n", ai.as_int);
32           break;
33         case TYPE_ATTR_CHUNK:
34           {
35             const char *str = attr_retrieve_blob (s, ai.as_chunk[0], ai.as_chunk[1]);
36             if (str)
37               fprintf (stdout, "blob %s\n", str);
38             else
39               fprintf (stdout, "blob %u+%u\n", ai.as_chunk[0], ai.as_chunk[1]);
40           }
41           break;
42         case TYPE_ATTR_STRING:
43           fprintf (stdout, "str  %s\n", ai.as_string);
44           break;
45         case TYPE_ATTR_INTLIST:
46           {
47             fprintf (stdout, "lint\n ");
48             while (1)
49               {
50                 int val;
51                 get_num (ai.as_numlist, val);
52                 fprintf (stdout, " %d", (val & 63) | ((val >> 1) & ~63));
53                 if (!(val & 64))
54                   break;
55               }
56             fprintf (stdout, "\n");
57             break;
58           }
59         case TYPE_ATTR_LOCALIDS:
60           {
61             fprintf (stdout, "lids");
62             while (1)
63               {
64                 Id val;
65                 get_num (ai.as_numlist, val);
66                 if (!val)
67                   break;
68                 fprintf (stdout, "\n  %s(%d)", localid2str (s, val), val);
69               }
70             fprintf (stdout, "\n");
71             break;
72           }
73         default:
74           fprintf (stdout, "\n");
75           break;
76         }
77     }
78 }
79
80 static void
81 dump_attrs (Repo *repo, unsigned int entry)
82 {
83   unsigned i;
84   for (i = 0; i < repo->nrepodata; i++)
85     {
86       Attrstore *s = repo->repodata[i].s;
87       if (s && entry < s->entries)
88         dump_attrs_1 (s, entry);
89     }
90 }
91 #endif
92
93 static void
94 dump_repodata (Repo *repo)
95 {
96   unsigned i;
97   Repodata *data;
98   if (repo->nrepodata == 0)
99     return;
100   printf("repo refers to %d subfiles:\n", repo->nrepodata);
101   for (i = 0, data = repo->repodata; i < repo->nrepodata; i++, data++)
102     {
103       unsigned int j;
104       printf("%s has %d keys", data->location ? data->location : "**EMBED**", data->nkeys);
105       for (j = 1; j < data->nkeys; j++)
106         printf("\n  %s", id2str(repo->pool, data->keys[j].name));
107       printf("\n");
108     }
109   printf("\n");
110 }
111
112 static void
113 printids(Repo *repo, char *kind, Offset ido)
114 {
115   Pool *pool = repo->pool;
116   Id id, *ids;
117   if (!ido)
118     return;
119   printf("%s:\n", kind);
120   ids = repo->idarraydata + ido;
121   while((id = *ids++) != 0)
122     printf("  %s\n", dep2str(pool, id));
123 }
124
125 static void
126 printdir(Repodata *data, Id dir)
127 {
128   Id comp;
129   Id parent = dirpool_parent(&data->dirpool, dir);
130   if (parent)
131     {
132       printdir(data, parent);
133       putchar('/');
134     }
135   comp = dirpool_compid(&data->dirpool, dir);
136   if (data->localpool)
137     printf("%s", stringpool_id2str(&data->spool, comp));
138   else
139     printf("%s", id2str(data->repo->pool, comp));
140 }
141
142 int
143 dump_repoattrs_cb(void *vcbdata, Solvable *s, Repodata *data, Repokey *key, KeyValue *kv)
144 {
145   const char *keyname;
146
147   keyname = id2str(data->repo->pool, key->name);
148   switch(key->type)
149     {
150     case TYPE_ID:
151       if (data->localpool)
152         kv->str = stringpool_id2str(&data->spool, kv->id);
153       else
154         kv->str = id2str(data->repo->pool, kv->id);
155       printf("%s: %s\n", keyname, kv->str);
156       break;
157     case TYPE_STR:
158       printf("%s: %s\n", keyname, kv->str);
159       break;
160     case TYPE_VOID:
161       printf("%s\n", keyname);
162       break;
163     case TYPE_NUM:
164     case TYPE_CONSTANT:
165       printf("%s: %d\n", keyname, kv->num);
166       break;
167     case TYPE_DIRNUMNUMARRAY:
168       printf("%s: ", keyname);
169       printdir(data, kv->id);
170       printf(" %d %d\n", kv->num, kv->num2);
171       break;
172     case TYPE_DIRSTRARRAY:
173       printf("%s: ", keyname);
174       printdir(data, kv->id);
175       printf("/%s\n", kv->str);
176       break;
177     default:
178       printf("%s: ?\n", keyname);
179       break;
180     }
181   return 0;
182 }
183
184 /*
185  * dump all attributes for Id <p>
186  */
187
188 void
189 dump_repoattrs(Repo *repo, Id p)
190 {
191   int i;
192   Repodata *data;
193   /*
194    * look through all repodata(s) to find the one covering the right range of Ids
195    */
196   for (i = 0, data = repo->repodata; i < repo->nrepodata; i++, data++)
197     {
198       if (data->state == REPODATA_STUB || data->state == REPODATA_ERROR) /* skip repodata of wrong state */
199         continue;
200       if (p < data->start || p >= data->end) /* skip repodata of wrong range */
201         continue;
202       repodata_search(data, p - data->start, 0, dump_repoattrs_cb, 0);
203     }
204 }
205
206 void
207 dump_some_attrs(Repo *repo, Solvable *s)
208 {
209   Id name = str2id (repo->pool, "summary", 0);
210   const char *summary = 0;
211   unsigned int medianr = -1, downloadsize = -1;
212   unsigned int time = -1;
213   if (name)
214     summary = repo_lookup_str (s, name);
215   if ((name = str2id (repo->pool, "medianr", 0)))
216     medianr = repo_lookup_num (s, name);
217   if ((name = str2id (repo->pool, "downloadsize", 0)))
218     downloadsize = repo_lookup_num (s, name);
219   if ((name = str2id (repo->pool, "time", 0)))
220     time = repo_lookup_num (s, name);
221
222   printf ("  XXX %d %d %u %s\n", medianr, downloadsize, time, summary);
223 }
224
225
226 static FILE *
227 loadcallback (Pool *pool, Repodata *data, void *vdata)
228 {
229   FILE *fp = 0;
230   if (data->location && with_attr)
231     {
232       fprintf (stderr, "Loading SOLV file %s\n", data->location);
233       fp = fopen (data->location, "r");
234       if (!fp)
235         perror(data->location);
236     }
237   return fp;
238 }
239
240
241 static void
242 usage( const char *err )
243 {
244   if (err)
245     fprintf (stderr, "\n** Error:\n  %s\n", err);
246   fprintf( stderr, "\nUsage:\n"
247            "dumpsolv [-a] [<solvfile>]\n"
248            "  -a  read attributes.\n"
249            );
250   exit(0);
251 }
252
253
254 int main(int argc, char **argv)
255 {
256   Repo *repo;
257   Pool *pool;
258   int i, n;
259   Solvable *s;
260   
261   argv++;
262   argc--;
263   while (argc--)
264     {
265       const char *s = argv[0];
266       if (*s++ == '-')
267         while (*s)
268           switch (*s++)
269             {
270               case 'h': usage(NULL); break;
271               case 'a': with_attr = 1; break;
272               default : break;
273             }
274       else
275         {
276           if (freopen (argv[0], "r", stdin) == 0)
277             {
278               perror(argv[0]);
279               exit(1);
280             }
281           break;
282         }
283       argv++;
284     }
285
286   pool = pool_create();
287   pool_setdebuglevel(pool, 1);
288   pool_setloadcallback(pool, loadcallback, 0);
289
290   repo = repo_create(pool, argc != 1 ? argv[1] : "<stdin>");
291   if (repo_add_solv(repo, stdin))
292     printf("could not read repository\n");
293   dump_repodata (repo);
294   printf("repo contains %d solvables\n", repo->nsolvables);
295   for (i = repo->start, n = 1; i < repo->end; i++)
296     {
297       s = pool->solvables + i;
298       if (s->repo != repo)
299         continue;
300       printf("\n");
301       printf("solvable %d:\n", n);
302       if (s->name || s->evr || s->arch)
303         printf("name: %s %s %s\n", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch));
304       if (s->vendor)
305         printf("vendor: %s\n", id2str(pool, s->vendor));
306       printids(repo, "provides", s->provides);
307       printids(repo, "obsoletes", s->obsoletes);
308       printids(repo, "conflicts", s->conflicts);
309       printids(repo, "requires", s->requires);
310       printids(repo, "recommends", s->recommends);
311       printids(repo, "suggests", s->suggests);
312       printids(repo, "supplements", s->supplements);
313       printids(repo, "enhances", s->enhances);
314       printids(repo, "freshens", s->freshens);
315 #if 0
316       dump_attrs (repo, n - 1);
317 #endif
318       dump_repoattrs(repo, i);
319 #if 1
320       dump_some_attrs(repo, s);
321 #endif
322       n++;
323     }
324   pool_free(pool);
325   exit(0);
326 }