3f682d023518e0d274bc57e6de7fde484a5e6140
[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     default:
173       printf("%s: ?\n", keyname);
174       break;
175     }
176   return 0;
177 }
178
179 /*
180  * dump all attributes for Id <p>
181  */
182
183 void
184 dump_repoattrs(Repo *repo, Id p)
185 {
186   int i;
187   Repodata *data;
188   /*
189    * look through all repodata(s) to find the one covering the right range of Ids
190    */
191   for (i = 0, data = repo->repodata; i < repo->nrepodata; i++, data++)
192     {
193       if (data->state == REPODATA_STUB || data->state == REPODATA_ERROR) /* skip repodata of wrong state */
194         continue;
195       if (p < data->start || p >= data->end) /* skip repodata of wrong range */
196         continue;
197       repodata_search(data, p - data->start, 0, dump_repoattrs_cb, 0);
198     }
199 }
200
201 void
202 dump_some_attrs(Repo *repo, Solvable *s)
203 {
204   Id name = str2id (repo->pool, "summary", 0);
205   const char *summary = 0;
206   unsigned int medianr = -1, downloadsize = -1;
207   unsigned int time = -1;
208   if (name)
209     summary = repo_lookup_str (s, name);
210   if ((name = str2id (repo->pool, "medianr", 0)))
211     medianr = repo_lookup_num (s, name);
212   if ((name = str2id (repo->pool, "downloadsize", 0)))
213     downloadsize = repo_lookup_num (s, name);
214   if ((name = str2id (repo->pool, "time", 0)))
215     time = repo_lookup_num (s, name);
216
217   printf ("  XXX %d %d %u %s\n", medianr, downloadsize, time, summary);
218 }
219
220
221 static FILE *
222 loadcallback (Pool *pool, Repodata *data, void *vdata)
223 {
224   FILE *fp = 0;
225   if (data->location && with_attr)
226     {
227       fprintf (stderr, "Loading SOLV file %s\n", data->location);
228       fp = fopen (data->location, "r");
229       if (!fp)
230         perror(data->location);
231     }
232   return fp;
233 }
234
235
236 static void
237 usage( const char *err )
238 {
239   if (err)
240     fprintf (stderr, "\n** Error:\n  %s\n", err);
241   fprintf( stderr, "\nUsage:\n"
242            "dumpsolv [-a] [<solvfile>]\n"
243            "  -a  read attributes.\n"
244            );
245   exit(0);
246 }
247
248
249 int main(int argc, char **argv)
250 {
251   Repo *repo;
252   Pool *pool;
253   int i, n;
254   Solvable *s;
255   
256   argv++;
257   argc--;
258   while (argc--)
259     {
260       const char *s = argv[0];
261       if (*s++ == '-')
262         while (*s)
263           switch (*s++)
264             {
265               case 'h': usage(NULL); break;
266               case 'a': with_attr = 1; break;
267               default : break;
268             }
269       else
270         {
271           if (freopen (argv[0], "r", stdin) == 0)
272             {
273               perror(argv[0]);
274               exit(1);
275             }
276           break;
277         }
278       argv++;
279     }
280
281   pool = pool_create();
282   pool_setdebuglevel(pool, 1);
283   pool_setloadcallback(pool, loadcallback, 0);
284
285   repo = repo_create(pool, argc != 1 ? argv[1] : "<stdin>");
286   if (repo_add_solv(repo, stdin))
287     printf("could not read repository\n");
288   dump_repodata (repo);
289   printf("repo contains %d solvables\n", repo->nsolvables);
290   for (i = repo->start, n = 1; i < repo->end; i++)
291     {
292       s = pool->solvables + i;
293       if (s->repo != repo)
294         continue;
295       printf("\n");
296       printf("solvable %d:\n", n);
297       if (s->name || s->evr || s->arch)
298         printf("name: %s(%s) %s %s\n", id2str(pool, s->name) + s->kind, id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch));
299       if (s->vendor)
300         printf("vendor: %s\n", id2str(pool, s->vendor));
301       printids(repo, "provides", s->provides);
302       printids(repo, "obsoletes", s->obsoletes);
303       printids(repo, "conflicts", s->conflicts);
304       printids(repo, "requires", s->requires);
305       printids(repo, "recommends", s->recommends);
306       printids(repo, "suggests", s->suggests);
307       printids(repo, "supplements", s->supplements);
308       printids(repo, "enhances", s->enhances);
309       printids(repo, "freshens", s->freshens);
310 #if 0
311       dump_attrs (repo, n - 1);
312 #endif
313       dump_repoattrs(repo, i);
314 #if 1
315       dump_some_attrs(repo, s);
316 #endif
317       n++;
318     }
319   pool_free(pool);
320   exit(0);
321 }