prepare to parse diskusage and translations through rpmmd files
[platform/upstream/libsolv.git] / tools / repo_rpmmd.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 <sys/types.h>
9 #include <limits.h>
10 #include <fcntl.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <expat.h>
15
16 #include "pool.h"
17 #include "repo.h"
18 #define DISABLE_SPLIT
19 #include "tools_util.h"
20 #include "repo_rpmmd.h"
21
22
23 enum state {
24   STATE_START,
25   STATE_METADATA,
26   STATE_SOLVABLE,
27   STATE_PRODUCT,
28   STATE_PATTERN,
29   STATE_PATCH,
30   STATE_NAME,
31   STATE_ARCH,
32   STATE_VERSION,
33
34   // package rpm-md
35   STATE_LOCATION,
36   STATE_CHECKSUM,
37   STATE_RPM_GROUP,
38   STATE_RPM_LICENSE,
39
40   /* resobject attributes */
41   STATE_SUMMARY,
42   STATE_DESCRIPTION,
43   STATE_PACKAGER,
44   STATE_URL,
45   STATE_INSNOTIFY,
46   STATE_DELNOTIFY,
47   STATE_VENDOR,
48   STATE_SIZE,
49   STATE_TIME,
50   STATE_DOWNLOADSIZE,
51   STATE_INSTALLTIME,
52   STATE_INSTALLONLY,
53   
54   /* patch */
55   STATE_ID,
56   STATE_TIMESTAMP,
57   STATE_AFFECTSPKG,
58   STATE_REBOOTNEEDED,
59
60    // xml store pattern attributes
61   STATE_CATEGORY, /* pattern and patches */
62   STATE_SCRIPT,
63   STATE_ICON,
64   STATE_USERVISIBLE,
65   STATE_DEFAULT,
66   STATE_INSTALL_TIME,
67
68   /* product */
69   STATE_SHORTNAME,
70   STATE_DISTNAME,
71   STATE_DISTEDITION,
72   STATE_SOURCE,
73   STATE_RELNOTESURL,
74
75   /* rpm-md dependencies inside the
76      format tag */
77   STATE_PROVIDES,
78   STATE_REQUIRES,
79   STATE_OBSOLETES,
80   STATE_CONFLICTS,
81   STATE_RECOMMENDS,
82   STATE_SUPPLEMENTS,
83   STATE_SUGGESTS,
84   STATE_ENHANCES,
85   STATE_FRESHENS,
86   STATE_SOURCERPM,
87   STATE_HEADERRANGE,
88
89   STATE_PROVIDESENTRY,
90   STATE_REQUIRESENTRY,
91   STATE_OBSOLETESENTRY,
92   STATE_CONFLICTSENTRY,
93   STATE_RECOMMENDSENTRY,
94   STATE_SUPPLEMENTSENTRY,
95   STATE_SUGGESTSENTRY,
96   STATE_ENHANCESENTRY,
97   STATE_FRESHENSENTRY,
98
99   STATE_FILE,
100
101   // general
102   NUMSTATES
103 };
104
105 struct stateswitch {
106   enum state from;
107   char *ename;
108   enum state to;
109   int docontent;
110 };
111
112 static struct stateswitch stateswitches[] = {
113
114   { STATE_START,       "product",         STATE_SOLVABLE, 0 },
115   { STATE_START,       "pattern",         STATE_SOLVABLE, 0 },
116   { STATE_START,       "patch",           STATE_SOLVABLE, 0 },
117   { STATE_START,       "package",         STATE_SOLVABLE, 0 },
118   
119   { STATE_SOLVABLE,    "name",            STATE_NAME, 1 },
120   { STATE_SOLVABLE,    "arch",            STATE_ARCH, 1 },
121   { STATE_SOLVABLE,    "version",         STATE_VERSION, 0 },
122
123   // package attributes rpm-md
124   { STATE_SOLVABLE,    "location",        STATE_LOCATION, 0 },
125   { STATE_SOLVABLE,    "checksum",        STATE_CHECKSUM, 1 },
126   
127   /* resobject attributes */
128
129   { STATE_SOLVABLE,    "summary",         STATE_SUMMARY, 1 },
130   { STATE_SOLVABLE,    "description",     STATE_DESCRIPTION, 1 },
131   { STATE_SOLVABLE,    "url",             STATE_URL, 1 },
132   { STATE_SOLVABLE,    "packager",        STATE_PACKAGER, 1 },
133   //{ STATE_SOLVABLE,    "???",         STATE_INSNOTIFY, 1 },
134   //{ STATE_SOLVABLE,    "??",     STATE_DELNOTIFY, 1 },
135   { STATE_SOLVABLE,    "vendor",          STATE_VENDOR, 1 },
136   { STATE_SOLVABLE,    "size",            STATE_SIZE, 0 },
137   { STATE_SOLVABLE,    "archive-size",    STATE_DOWNLOADSIZE, 1 },
138   { STATE_SOLVABLE,    "install-time",    STATE_INSTALLTIME, 1 },
139   { STATE_SOLVABLE,    "install-only",    STATE_INSTALLONLY, 1 },
140   { STATE_SOLVABLE,    "time",            STATE_TIME, 0 },
141
142   // xml store pattern attributes
143   { STATE_SOLVABLE,    "script",          STATE_SCRIPT, 1 },
144   { STATE_SOLVABLE,    "icon",            STATE_ICON, 1 },
145   { STATE_SOLVABLE,    "uservisible",     STATE_USERVISIBLE, 1 },
146   { STATE_SOLVABLE,    "category",        STATE_CATEGORY, 1 },
147   { STATE_SOLVABLE,    "default",         STATE_DEFAULT, 1 },
148   { STATE_SOLVABLE,    "install-time",    STATE_INSTALL_TIME, 1 },
149
150   { STATE_SOLVABLE,      "rpm:vendor",      STATE_VENDOR, 1 },
151   { STATE_SOLVABLE,      "rpm:group",       STATE_RPM_GROUP, 1 },
152   { STATE_SOLVABLE,      "rpm:license",     STATE_RPM_LICENSE, 1 },
153
154   /* rpm-md dependencies */ 
155   { STATE_SOLVABLE,      "rpm:provides",    STATE_PROVIDES, 0 },
156   { STATE_SOLVABLE,      "rpm:requires",    STATE_REQUIRES, 0 },
157   { STATE_SOLVABLE,      "rpm:obsoletes",   STATE_OBSOLETES , 0 },
158   { STATE_SOLVABLE,      "rpm:conflicts",   STATE_CONFLICTS , 0 },
159   { STATE_SOLVABLE,      "rpm:recommends",  STATE_RECOMMENDS , 0 },
160   { STATE_SOLVABLE,      "rpm:supplements", STATE_SUPPLEMENTS, 0 },
161   { STATE_SOLVABLE,      "rpm:suggests",    STATE_SUGGESTS, 0 },
162   { STATE_SOLVABLE,      "rpm:enhances",    STATE_ENHANCES, 0 },
163   { STATE_SOLVABLE,      "rpm:freshens",    STATE_FRESHENS, 0 },
164   { STATE_SOLVABLE,      "rpm:sourcerpm",   STATE_SOURCERPM, 1 },
165   { STATE_SOLVABLE,      "rpm:header-range", STATE_HEADERRANGE, 0 },
166   { STATE_SOLVABLE,      "file",            STATE_FILE, 1 },
167   
168   { STATE_PROVIDES,    "rpm:entry",       STATE_PROVIDESENTRY, 0 },
169   { STATE_REQUIRES,    "rpm:entry",       STATE_REQUIRESENTRY, 0 },
170   { STATE_OBSOLETES,   "rpm:entry",       STATE_OBSOLETESENTRY, 0 },
171   { STATE_CONFLICTS,   "rpm:entry",       STATE_CONFLICTSENTRY, 0 },
172   { STATE_RECOMMENDS,  "rpm:entry",       STATE_RECOMMENDSENTRY, 0 },
173   { STATE_SUPPLEMENTS, "rpm:entry",       STATE_SUPPLEMENTSENTRY, 0 },
174   { STATE_SUGGESTS,    "rpm:entry",       STATE_SUGGESTSENTRY, 0 },
175   { STATE_ENHANCES,    "rpm:entry",       STATE_ENHANCESENTRY, 0 },
176   { STATE_FRESHENS,    "rpm:entry",       STATE_FRESHENSENTRY, 0 },
177
178   { NUMSTATES}
179 };
180
181
182 struct parsedata {
183   struct parsedata_common common;
184   char *kind;
185   int depth;
186   enum state state;
187   int statedepth;
188   char *content;
189   int lcontent;
190   int acontent;
191   int docontent;
192   Solvable *solvable;
193   Offset freshens;
194   struct stateswitch *swtab[NUMSTATES];
195   enum state sbtab[NUMSTATES];
196   const char *lang;
197   const char *capkind;
198   // used to store tmp attributes
199   // while the tag ends
200   const char *tmpattr;
201   Repodata *data;
202   Id handle;
203   XML_Parser *parser;
204   Id (*dirs)[3]; // dirid, size, nfiles
205   int ndirs;
206   Id langcache[ID_NUM_INTERNAL];
207 };
208
209
210 static Id
211 makeevr_atts(Pool *pool, struct parsedata *pd, const char **atts)
212 {
213   const char *e, *v, *r, *v2;
214   char *c;
215   int l;
216
217   e = v = r = 0;
218   for (; *atts; atts += 2)
219     {
220       if (!strcmp(*atts, "epoch"))
221         e = atts[1];
222       else if (!strcmp(*atts, "ver"))
223         v = atts[1];
224       else if (!strcmp(*atts, "rel"))
225         r = atts[1];
226     }
227   if (e && !strcmp(e, "0"))
228     e = 0;
229   if (v && !e)
230     {
231       for (v2 = v; *v2 >= '0' && *v2 <= '9'; v2++)
232         ;
233       if (v2 > v && *v2 == ':')
234         e = "0";
235     }
236   l = 1;
237   if (e)
238     l += strlen(e) + 1;
239   if (v)
240     l += strlen(v);
241   if (r)
242     l += strlen(r) + 1;
243   if (l > pd->acontent)
244     {
245       pd->content = sat_realloc(pd->content, l + 256);
246       pd->acontent = l + 256;
247     }
248   c = pd->content;
249   if (e)
250     {
251       strcpy(c, e);
252       c += strlen(c);
253       *c++ = ':';
254     }
255   if (v)
256     {
257       strcpy(c, v);
258       c += strlen(c);
259     }
260   if (r)
261     {
262       *c++ = '-';
263       strcpy(c, r);
264       c += strlen(c);
265     }
266   *c = 0;
267   if (!*pd->content)
268     return 0;
269 #if 0
270   fprintf(stderr, "evr: %s\n", pd->content);
271 #endif
272   return str2id(pool, pd->content, 1);
273 }
274
275 static inline const char *
276 find_attr(const char *txt, const char **atts)
277 {
278   for (; *atts; atts += 2)
279     {
280       if (!strcmp(*atts, txt))
281         return atts[1];
282     }
283   return 0;
284 }
285
286 static char *flagtab[] = {
287   "GT",
288   "EQ",
289   "GE",
290   "LT",
291   "NE",
292   "LE"
293 };
294
295 static unsigned int
296 adddep(Pool *pool, struct parsedata *pd, unsigned int olddeps, const char **atts, int isreq)
297 {
298   Id id, name, marker;
299   const char *n, *f, *k;
300   const char **a;
301
302   n = f = k = 0;
303   marker = isreq ? -SOLVABLE_PREREQMARKER : 0;
304   for (a = atts; *a; a += 2)
305     {
306       if (!strcmp(*a, "name"))
307         n = a[1];
308       else if (!strcmp(*a, "flags"))
309         f = a[1];
310       else if (!strcmp(*a, "kind"))
311         k = a[1];
312       else if (isreq && !strcmp(*a, "pre") && a[1][0] == '1')
313         marker = SOLVABLE_PREREQMARKER;
314     }
315   if (!n)
316     return olddeps;
317   if (k && !strcmp(k, "package"))
318     k = 0;
319   if (k)
320     {
321       int l = strlen(k) + 1 + strlen(n) + 1;
322       if (l > pd->acontent)
323         {
324           pd->content = sat_realloc(pd->content, l + 256);
325           pd->acontent = l + 256;
326         }
327       sprintf(pd->content, "%s:%s", k, n); 
328       name = str2id(pool, pd->content, 1); 
329     }
330   else
331     name = str2id(pool, (char *)n, 1);
332   if (f)
333     {
334       Id evr = makeevr_atts(pool, pd, atts);
335       int flags;
336       for (flags = 0; flags < 6; flags++)
337         if (!strcmp(f, flagtab[flags]))
338           break;
339       flags = flags < 6 ? flags + 1 : 0;
340       id = rel2id(pool, name, evr, flags, 1);
341     }
342   else
343     id = name;
344 #if 0
345   fprintf(stderr, "new dep %s%s%s\n", id2str(pool, d), id2rel(pool, d), id2evr(pool, d));
346 #endif
347   return repo_addid_dep(pd->common.repo, olddeps, id, marker);
348 }
349
350 static void
351 set_desciption_author(Repodata *data, Id handle, char *str)
352 {
353   char *aut, *p;
354
355   if (!str || !*str)
356     return;
357   for (aut = str; (aut = strchr(aut, '\n')) != 0; aut++)
358     if (!strncmp(aut, "\nAuthors:\n--------\n", 19)) 
359       break;
360   if (aut)
361     {
362       /* oh my, found SUSE special author section */
363       int l = aut - str; 
364       str[l] = 0; 
365       while (l > 0 && str[l - 1] == '\n')
366         str[--l] = 0; 
367       if (l)
368         repodata_set_str(data, handle, SOLVABLE_DESCRIPTION, str);
369       p = aut + 19;
370       aut = str;        /* copy over */
371       while (*p == ' ' || *p == '\n')
372         p++;
373       while (*p) 
374         {
375           if (*p == '\n')
376             {
377               *aut++ = *p++;
378               while (*p == ' ') 
379                 p++;
380               continue;
381             }
382           *aut++ = *p++;
383         }
384       while (aut != str && aut[-1] == '\n')
385         aut--;
386       *aut = 0; 
387       if (*str)
388         repodata_set_str(data, handle, SOLVABLE_AUTHORS, str);
389     }
390   else if (*str)
391     repodata_set_str(data, handle, SOLVABLE_DESCRIPTION, str);
392 }
393
394 static void
395 set_sourcerpm(Repodata *data, Solvable *s, Id handle, char *sourcerpm)
396 {
397   const char *p, *sevr, *sarch, *name, *evr;
398   Pool *pool;
399
400   p = strrchr(sourcerpm, '.');
401   if (!p || strcmp(p, ".rpm") != 0)
402     return;
403   p--;
404   while (p > sourcerpm && *p != '.')
405     p--;
406   if (*p != '.' || p == sourcerpm)
407     return;
408   sarch = p-- + 1;
409   while (p > sourcerpm && *p != '-')
410     p--;
411   if (*p != '-' || p == sourcerpm)
412     return;
413   p--;
414   while (p > sourcerpm && *p != '-')
415     p--;
416   if (*p != '-' || p == sourcerpm)
417     return;
418   sevr = p + 1;
419   pool = s->repo->pool;
420   name = id2str(pool, s->name);
421   evr = id2str(pool, s->evr);
422   if (!strcmp(sarch, "src.rpm"))
423     repodata_set_constantid(data, handle, SOLVABLE_SOURCEARCH, ARCH_SRC);
424   else if (!strcmp(sarch, "nosrc.rpm"))
425     repodata_set_constantid(data, handle, SOLVABLE_SOURCEARCH, ARCH_NOSRC);
426   else
427     repodata_set_constantid(data, handle, SOLVABLE_SOURCEARCH, strn2id(pool, sarch, strlen(sarch) - 4, 1));
428   if (!strncmp(sevr, evr, sarch - sevr - 1) && evr[sarch - sevr - 1] == 0)
429     repodata_set_void(data, handle, SOLVABLE_SOURCEEVR);
430   else
431     repodata_set_id(data, handle, SOLVABLE_SOURCEEVR, strn2id(pool, sevr, sarch - sevr - 1, 1));
432   if (!strncmp(sourcerpm, name, sevr - sourcerpm - 1) && name[sevr - sourcerpm -
433  1] == 0)
434     repodata_set_void(data, handle, SOLVABLE_SOURCENAME);
435   else
436     repodata_set_id(data, handle, SOLVABLE_SOURCENAME, strn2id(pool, sourcerpm, sevr - sourcerpm - 1, 1));
437 }
438
439 static void XMLCALL
440 startElement(void *userData, const char *name, const char **atts)
441 {
442   //fprintf(stderr,"+tag: %s\n", name);
443   struct parsedata *pd = userData;
444   Pool *pool = pd->common.pool;
445   Solvable *s = pd->solvable;
446   struct stateswitch *sw;
447   const char *str;
448   Id handle = pd->handle;
449
450   // fprintf(stderr, "into %s, from %d, depth %d, statedepth %d\n", name, pd->state, pd->depth, pd->statedepth);
451
452   if (pd->depth != pd->statedepth)
453     {
454       pd->depth++;
455       return;
456     }
457
458   if (pd->state == STATE_START && !strcmp(name, "patterns"))
459     return;
460   if (pd->state == STATE_START && !strcmp(name, "metadata"))
461     return;
462   if (pd->state == STATE_SOLVABLE && !strcmp(name, "format"))
463     return;
464
465   pd->depth++;
466   for (sw = pd->swtab[pd->state]; sw->from == pd->state; sw++)
467     if (!strcmp(sw->ename, name))
468       break;
469   if (sw->from != pd->state)
470     {
471 #if 0
472       fprintf(stderr, "into unknown: %s\n", name);
473 #endif
474       return;
475     }
476   pd->state = sw->to;
477   pd->docontent = sw->docontent;
478   pd->statedepth = pd->depth;
479   pd->lcontent = 0;
480   *pd->content = 0;
481   switch(pd->state)
482     {
483     case STATE_SOLVABLE:
484       pd->kind = 0;
485       if (name[2] == 't' && name[3] == 't')
486         pd->kind = "pattern";
487       else if (name[1] == 'r')
488         pd->kind = "product";
489       else if (name[2] == 't' && name[3] == 'c')
490         pd->kind = "patch";
491       
492       /**
493        * now we have two cases, one is that we are in the package tag of a new package
494        * and the other is that we are in the tag of an extension file like other.xml or
495        * filelist.xml.
496        * We identify it by looking the attributes
497        *
498        * new package
499        * <package>
500        *     <name>...
501        *     <version...>
502        *
503        * extension package:
504        *
505        * For other.xml
506        *
507        * <package pkgid="b78f8664cd90efe42e09a345e272997ef1b53c18"
508        *          name="zaptel-kmp-default"
509        *          arch="i586">
510        *     <version epoch="0"
511        *              ver="1.2.10_2.6.22_rc4_git6_2"
512        *              rel="70"/>
513        */
514
515       /* first check if there is a pkgid attribute */
516       const char *pkgid = find_attr( "pkgid", atts);
517
518       if ( pkgid == NULL )
519         {
520           /* this is a new package */
521           /*fprintf(stderr, "new package\n");*/
522           pd->solvable = pool_id2solvable(pool, repo_add_solvable(pd->common.repo));
523           pd->freshens = 0;
524           repodata_extend(pd->data, pd->solvable - pool->solvables);
525         }
526       else
527         {
528           /* we need to look for the package based on the pkgid */
529           Dataiterator di;
530           dataiterator_init(&di, pd->common.repo, -1, SOLVABLE_CHECKSUM, pkgid, SEARCH_STRING);
531           if (dataiterator_step(&di))
532             {
533               /* we found it */
534               /*fprintf(stderr, "package found '%i'\n", di.solvid);*/
535               pd->solvable = pool_id2solvable(pool, di.solvid);
536             }
537           else
538             {
539               /* this is bad, here we should ignore the whole solvable */
540               fprintf(stderr, "can't find package with checksum '%s'\n", pkgid);
541               //exit(1);
542             }
543         }
544
545       pd->handle = repodata_get_handle(pd->data, (pd->solvable - pool->solvables) - pd->data->start);
546 #if 0
547       fprintf(stderr, "package #%d\n", pd->solvable - pool->solvables);
548 #endif
549       break;
550     case STATE_VERSION:
551       s->evr = makeevr_atts(pool, pd, atts);
552       break;
553     case STATE_PROVIDES:
554       s->provides = 0;
555       break;
556     case STATE_PROVIDESENTRY:
557       s->provides = adddep(pool, pd, s->provides, atts, 0);
558       break;
559     case STATE_REQUIRES:
560       s->requires = 0;
561       break;
562     case STATE_REQUIRESENTRY:
563       s->requires = adddep(pool, pd, s->requires, atts, 1);
564       break;
565     case STATE_OBSOLETES:
566       s->obsoletes = 0;
567       break;
568     case STATE_OBSOLETESENTRY:
569       s->obsoletes = adddep(pool, pd, s->obsoletes, atts, 0);
570       break;
571     case STATE_CONFLICTS:
572       s->conflicts = 0;
573       break;
574     case STATE_CONFLICTSENTRY:
575       s->conflicts = adddep(pool, pd, s->conflicts, atts, 0);
576       break;
577     case STATE_RECOMMENDS:
578       s->recommends = 0;
579       break;
580     case STATE_RECOMMENDSENTRY:
581       s->recommends = adddep(pool, pd, s->recommends, atts, 0);
582       break;
583     case STATE_SUPPLEMENTS:
584       s->supplements= 0;
585       break;
586     case STATE_SUPPLEMENTSENTRY:
587       s->supplements = adddep(pool, pd, s->supplements, atts, 0);
588       break;
589     case STATE_SUGGESTS:
590       s->suggests = 0;
591       break;
592     case STATE_SUGGESTSENTRY:
593       s->suggests = adddep(pool, pd, s->suggests, atts, 0);
594       break;
595     case STATE_ENHANCES:
596       s->enhances = 0;
597       break;
598     case STATE_ENHANCESENTRY:
599       s->enhances = adddep(pool, pd, s->enhances, atts, 0);
600       break;
601     case STATE_FRESHENS:
602       pd->freshens = 0;
603       break;
604     case STATE_FRESHENSENTRY:
605       pd->freshens = adddep(pool, pd, pd->freshens, atts, 0);
606       break;
607     case STATE_SUMMARY:
608     case STATE_DESCRIPTION:
609       pd->lang = find_attr("lang", atts);
610       break;
611     case STATE_LOCATION:
612       str = find_attr("href", atts);
613       if (str)
614         {
615           const char *str2 = strrchr(str, '/');
616           if (str2)
617             {
618               char *str3 = strdup(str);
619               str3[str2 - str] = 0;
620               repodata_set_poolstr(pd->data, handle, SOLVABLE_MEDIADIR, str3);
621               free(str3);
622               repodata_set_str(pd->data, handle, SOLVABLE_MEDIAFILE, str2 + 1);
623             }
624           else
625             repodata_set_str(pd->data, handle, SOLVABLE_MEDIAFILE, str);
626         }
627       break;
628     case STATE_CHECKSUM:
629       pd->tmpattr = find_attr("type", atts);
630       break;
631     case STATE_TIME:
632       {
633         unsigned int t;
634         str = find_attr("build", atts);
635         if (str && (t = atoi(str)) != 0)
636           repodata_set_num(pd->data, handle, SOLVABLE_BUILDTIME, t);
637         break;
638       }
639     case STATE_SIZE:
640       {
641         unsigned int k;
642         str = find_attr("installed", atts);
643         if (str && (k = atoi(str)) != 0)
644           repodata_set_num(pd->data, handle, SOLVABLE_INSTALLSIZE, (k + 1023) / 1024);
645         /* XXX the "package" attribute gives the size of the rpm file,
646            i.e. the download size.  Except on packman, there it seems to be
647            something else entirely, it has a value near to the other two
648            values, as if the rpm is uncompressed.  */
649         str = find_attr("package", atts);
650         if (str && (k = atoi(str)) != 0)
651           repodata_set_num(pd->data, handle, SOLVABLE_DOWNLOADSIZE, (k + 1023) / 1024);
652         break;
653       }
654     case STATE_HEADERRANGE:
655       {
656         unsigned int end;
657         str = find_attr("end", atts);
658         if (str && (end = atoi(str)) != 0)
659           repodata_set_num(pd->data, handle, SOLVABLE_HEADEREND, end);
660       }
661     default:
662       break;
663     }
664 }
665
666 static void XMLCALL
667 endElement(void *userData, const char *name)
668 {
669   //fprintf(stderr,"-tag: %s\n", name);
670   struct parsedata *pd = userData;
671   Pool *pool = pd->common.pool;
672   Solvable *s = pd->solvable;
673   Repo *repo = pd->common.repo;
674   Id handle = pd->handle;
675   Id id;
676   char *p;
677
678   if (pd->depth != pd->statedepth)
679     {
680       pd->depth--;
681       // printf("back from unknown %d %d %d\n", pd->state, pd->depth, pd->statedepth);
682       return;
683     }
684
685   /* ignore patterns & metadata */
686   if (pd->state == STATE_START && !strcmp(name, "patterns"))
687     return;
688   if (pd->state == STATE_START && !strcmp(name, "metadata"))
689     return;
690   if (pd->state == STATE_SOLVABLE && !strcmp(name, "format"))
691     return;
692
693   pd->depth--;
694   pd->statedepth--;
695   switch (pd->state)
696     {
697     case STATE_PATTERN:
698     case STATE_PRODUCT:
699     case STATE_SOLVABLE:
700       if (!s->arch)
701         s->arch = ARCH_NOARCH;
702       if (!s->evr)
703         s->evr = ID_EMPTY;      /* some patterns have this */
704       if (s->arch != ARCH_SRC && s->arch != ARCH_NOSRC)
705         s->provides = repo_addid_dep(repo, s->provides, rel2id(pool, s->name, s->evr, REL_EQ, 1), 0);
706       s->supplements = repo_fix_legacy(repo, s->provides, s->supplements, pd->freshens);
707       pd->freshens = 0;
708       pd->kind = 0;
709       break;
710     case STATE_NAME:
711       if ( pd->kind )
712           s->name = str2id(pool, join2( pd->kind, ":", pd->content), 1);
713       else
714           s->name = str2id(pool, pd->content, 1);
715       break;
716     case STATE_ARCH:
717       s->arch = str2id(pool, pd->content, 1);
718       break;
719     case STATE_VENDOR:
720       s->vendor = str2id(pool, pd->content, 1);
721       break;
722     case STATE_RPM_GROUP:
723       repodata_set_poolstr(pd->data, handle, SOLVABLE_GROUP, pd->content);
724       break;
725     case STATE_RPM_LICENSE:
726       repodata_set_poolstr(pd->data, handle, SOLVABLE_LICENSE, pd->content);
727       break;
728     case STATE_CHECKSUM:
729       { 
730         int l;
731         Id type;
732         if (!strcasecmp (pd->tmpattr, "sha") || !strcasecmp (pd->tmpattr, "sha1"))
733           l = SIZEOF_SHA1 * 2, type = REPOKEY_TYPE_SHA1;
734         else if (!strcasecmp (pd->tmpattr, "md5"))
735           l = SIZEOF_MD5 * 2, type = REPOKEY_TYPE_MD5;
736         else
737           {
738             fprintf(stderr, "Unknown checksum type: %d: %s\n", (unsigned int)XML_GetCurrentLineNumber(*pd->parser), pd->tmpattr);
739             exit(1);
740           }
741         if (strlen(pd->content) != l)
742           {
743             fprintf(stderr, "Invalid checksum length: %d: for %s\n", (unsigned int)XML_GetCurrentLineNumber(*pd->parser), pd->tmpattr);
744             exit(1);
745           }
746         repodata_set_checksum(pd->data, handle, SOLVABLE_CHECKSUM, type, pd->content);
747       }
748       break;
749     case STATE_FILE:
750 #if 0
751       id = str2id(pool, pd->content, 1);
752       s->provides = repo_addid_dep(repo, s->provides, id, SOLVABLE_FILEMARKER);
753 #endif
754       if ((p = strrchr(pd->content, '/')) != 0)
755         {
756           *p++ = 0;
757           id = repodata_str2dir(pd->data, pd->content, 1);
758         }
759       else
760         {
761           p = pd->content;
762           id = 0;
763         }
764       if (!id)
765         id = repodata_str2dir(pd->data, "/", 1);
766       repodata_add_dirstr(pd->data, handle, SOLVABLE_FILELIST, id, p);
767       break;
768     case STATE_SUMMARY:
769       pd->lang = 0;
770       repodata_set_str(pd->data, handle, SOLVABLE_SUMMARY, pd->content);
771       break;
772     case STATE_DESCRIPTION:
773       pd->lang = 0;
774       set_desciption_author(pd->data, handle, pd->content);
775       break;
776     case STATE_URL:
777       if (pd->content[0])
778         repodata_set_str(pd->data, handle, SOLVABLE_URL, pd->content);
779       break;
780     case STATE_PACKAGER:
781       if (pd->content[0])
782         repodata_set_poolstr(pd->data, handle, SOLVABLE_PACKAGER, pd->content);
783       break;
784     case STATE_SOURCERPM:
785       set_sourcerpm(pd->data, s, handle, pd->content);
786       break;
787     default:
788       break;
789     }
790   pd->state = pd->sbtab[pd->state];
791   pd->docontent = 0;
792   // fprintf(stderr, "back from known %d %d %d\n", pd->state, pd->depth, pd->statedepth);
793 }
794
795 static void XMLCALL
796 characterData(void *userData, const XML_Char *s, int len)
797 {
798   struct parsedata *pd = userData;
799   int l;
800   char *c;
801
802   if (!pd->docontent)
803     return;
804   l = pd->lcontent + len + 1;
805   if (l > pd->acontent)
806     {
807       pd->content = sat_realloc(pd->content, l + 256);
808       pd->acontent = l + 256;
809     }
810   c = pd->content + pd->lcontent;
811   pd->lcontent += len;
812   while (len-- > 0)
813     *c++ = *s++;
814   *c = 0;
815 }
816
817
818 #define BUFF_SIZE 8192
819
820 void
821 repo_add_rpmmd(Repo *repo, FILE *fp, int flags)
822 {
823   Pool *pool = repo->pool;
824   struct parsedata pd;
825   char buf[BUFF_SIZE];
826   int i, l;
827   struct stateswitch *sw;
828
829   memset(&pd, 0, sizeof(pd));
830   for (i = 0, sw = stateswitches; sw->from != NUMSTATES; i++, sw++)
831     {
832       if (!pd.swtab[sw->from])
833         pd.swtab[sw->from] = sw;
834       pd.sbtab[sw->to] = sw->from;
835     }
836   pd.common.pool = pool;
837   pd.common.repo = repo;
838
839   pd.data = repo_add_repodata(repo, 0);
840
841   pd.content = sat_malloc(256);
842   pd.acontent = 256;
843   pd.lcontent = 0;
844   pd.common.tmp = 0;
845   pd.common.tmpl = 0;
846   pd.kind = 0;
847   XML_Parser parser = XML_ParserCreate(NULL);
848   XML_SetUserData(parser, &pd);
849   pd.parser = &parser;
850   XML_SetElementHandler(parser, startElement, endElement);
851   XML_SetCharacterDataHandler(parser, characterData);
852   for (;;)
853     {
854       l = fread(buf, 1, sizeof(buf), fp);
855       if (XML_Parse(parser, buf, l, l == 0) == XML_STATUS_ERROR)
856         {
857           fprintf(stderr, "repo_rpmmd: %s at line %u:%u\n", XML_ErrorString(XML_GetErrorCode(parser)), (unsigned int)XML_GetCurrentLineNumber(parser), (unsigned int)XML_GetCurrentColumnNumber(parser));
858           exit(1);
859         }
860       if (l == 0)
861         break;
862     }
863   XML_ParserFree(parser);
864
865   if (pd.data)
866     repodata_internalize(pd.data);
867   sat_free(pd.content);
868   join_freemem();
869 }