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