add other.xml support to repomd parser
[platform/upstream/libsolv.git] / ext / 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 #include "chksum.h"
22
23
24 enum state {
25   STATE_START,
26
27   STATE_SOLVABLE,
28
29   STATE_NAME,
30   STATE_ARCH,
31   STATE_VERSION,
32
33   /* package rpm-md */
34   STATE_LOCATION,
35   STATE_CHECKSUM,
36   STATE_RPM_GROUP,
37   STATE_RPM_LICENSE,
38
39   /* resobject attributes */
40   STATE_SUMMARY,
41   STATE_DESCRIPTION,
42   STATE_DISTRIBUTION,
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   /* Novell/SUSE extended attributes */
55   STATE_EULA,
56   STATE_KEYWORD,
57   STATE_DISKUSAGE,
58   STATE_DIRS,
59   STATE_DIR,
60
61   /* patch */
62   STATE_ID,
63   STATE_TIMESTAMP,
64   STATE_AFFECTSPKG,
65   STATE_REBOOTNEEDED,
66
67   /* pattern attributes */
68   STATE_CATEGORY, /* pattern and patches */
69   STATE_ORDER,
70   STATE_INCLUDES,
71   STATE_INCLUDESENTRY,
72   STATE_EXTENDS,
73   STATE_EXTENDSENTRY,
74   STATE_SCRIPT,
75   STATE_ICON,
76   STATE_USERVISIBLE,
77   STATE_DEFAULT,
78   STATE_INSTALL_TIME,
79
80   /* product */
81   STATE_SHORTNAME,
82   STATE_DISTNAME, /* obsolete */
83   STATE_DISTEDITION, /* obsolete */
84   STATE_SOURCE,
85   STATE_TYPE,
86   STATE_RELNOTESURL,
87   STATE_UPDATEURL,
88   STATE_OPTIONALURL,
89   STATE_FLAG,
90
91   /* rpm-md dependencies inside the
92      format tag */
93   STATE_PROVIDES,
94   STATE_REQUIRES,
95   STATE_OBSOLETES,
96   STATE_CONFLICTS,
97   STATE_RECOMMENDS,
98   STATE_SUPPLEMENTS,
99   STATE_SUGGESTS,
100   STATE_ENHANCES,
101   STATE_FRESHENS,
102   STATE_SOURCERPM,
103   STATE_HEADERRANGE,
104
105   STATE_PROVIDESENTRY,
106   STATE_REQUIRESENTRY,
107   STATE_OBSOLETESENTRY,
108   STATE_CONFLICTSENTRY,
109   STATE_RECOMMENDSENTRY,
110   STATE_SUPPLEMENTSENTRY,
111   STATE_SUGGESTSENTRY,
112   STATE_ENHANCESENTRY,
113   STATE_FRESHENSENTRY,
114
115   STATE_FILE,
116
117   STATE_CHANGELOG,
118
119   /* general */
120   NUMSTATES
121 };
122
123 struct stateswitch {
124   enum state from;
125   char *ename;
126   enum state to;
127   int docontent;
128 };
129
130 static struct stateswitch stateswitches[] = {
131   /** fake tag used to enclose 2 different xml files in one **/
132   { STATE_START,       "rpmmd",           STATE_START,    0 },
133
134   /** tags for different package data, we just ignore the tag **/
135   { STATE_START,       "metadata",        STATE_START,    0 },
136   { STATE_START,       "otherdata",       STATE_START,    0 },
137   { STATE_START,       "filelists",       STATE_START,    0 },
138   { STATE_START,       "diskusagedata",   STATE_START,    0 },
139   { STATE_START,       "susedata",        STATE_START,    0 },
140
141   { STATE_START,       "product",         STATE_SOLVABLE, 0 },
142   { STATE_START,       "pattern",         STATE_SOLVABLE, 0 },
143   { STATE_START,       "patch",           STATE_SOLVABLE, 0 },
144   { STATE_START,       "package",         STATE_SOLVABLE, 0 },
145
146   { STATE_SOLVABLE,    "name",            STATE_NAME, 1 },
147   { STATE_SOLVABLE,    "arch",            STATE_ARCH, 1 },
148   { STATE_SOLVABLE,    "version",         STATE_VERSION, 0 },
149
150   /* package attributes rpm-md */
151   { STATE_SOLVABLE,    "location",        STATE_LOCATION, 0 },
152   { STATE_SOLVABLE,    "checksum",        STATE_CHECKSUM, 1 },
153
154   /* resobject attributes */
155
156   { STATE_SOLVABLE,    "summary",         STATE_SUMMARY,      1 },
157   { STATE_SOLVABLE,    "description",     STATE_DESCRIPTION,  1 },
158   { STATE_SOLVABLE,    "distribution",    STATE_DISTRIBUTION, 1 },
159   { STATE_SOLVABLE,    "url",             STATE_URL,          1 },
160   { STATE_SOLVABLE,    "packager",        STATE_PACKAGER,     1 },
161   { STATE_SOLVABLE,    "vendor",          STATE_VENDOR,       1 },
162   { STATE_SOLVABLE,    "size",            STATE_SIZE,         0 },
163   { STATE_SOLVABLE,    "archive-size",    STATE_DOWNLOADSIZE, 1 },
164   { STATE_SOLVABLE,    "install-time",    STATE_INSTALLTIME,  1 },
165   { STATE_SOLVABLE,    "install-only",    STATE_INSTALLONLY,  1 },
166   { STATE_SOLVABLE,    "time",            STATE_TIME,         0 },
167
168   /* extended Novell/SUSE attributes (susedata.xml) */
169   { STATE_SOLVABLE,    "eula",            STATE_EULA,         1 },
170   { STATE_SOLVABLE,    "keyword",         STATE_KEYWORD,      1 },
171   { STATE_SOLVABLE,    "diskusage",       STATE_DISKUSAGE,    0 },
172
173   /* pattern attribute */
174   { STATE_SOLVABLE,    "script",          STATE_SCRIPT,        1 },
175   { STATE_SOLVABLE,    "icon",            STATE_ICON,          1 },
176   { STATE_SOLVABLE,    "uservisible",     STATE_USERVISIBLE,   1 },
177   { STATE_SOLVABLE,    "category",        STATE_CATEGORY,      1 },
178   { STATE_SOLVABLE,    "order",           STATE_ORDER,         1 },
179   { STATE_SOLVABLE,    "includes",        STATE_INCLUDES,      0 },
180   { STATE_SOLVABLE,    "extends",         STATE_EXTENDS,       0 },
181   { STATE_SOLVABLE,    "default",         STATE_DEFAULT,       1 },
182   { STATE_SOLVABLE,    "install-time",    STATE_INSTALL_TIME,  1 },
183
184   /* product attributes */
185   /* note the product type is an attribute */
186   { STATE_SOLVABLE,    "release-notes-url", STATE_RELNOTESURL, 1 },
187   { STATE_SOLVABLE,    "update-url",        STATE_UPDATEURL,   1 },
188   { STATE_SOLVABLE,    "optional-url",      STATE_OPTIONALURL, 1 },
189   { STATE_SOLVABLE,    "flag",              STATE_FLAG,        1 },
190
191   { STATE_SOLVABLE,      "rpm:vendor",      STATE_VENDOR,      1 },
192   { STATE_SOLVABLE,      "rpm:group",       STATE_RPM_GROUP,   1 },
193   { STATE_SOLVABLE,      "rpm:license",     STATE_RPM_LICENSE, 1 },
194
195   /* rpm-md dependencies */
196   { STATE_SOLVABLE,      "rpm:provides",    STATE_PROVIDES,     0 },
197   { STATE_SOLVABLE,      "rpm:requires",    STATE_REQUIRES,     0 },
198   { STATE_SOLVABLE,      "rpm:obsoletes",   STATE_OBSOLETES,    0 },
199   { STATE_SOLVABLE,      "rpm:conflicts",   STATE_CONFLICTS,    0 },
200   { STATE_SOLVABLE,      "rpm:recommends",  STATE_RECOMMENDS ,  0 },
201   { STATE_SOLVABLE,      "rpm:supplements", STATE_SUPPLEMENTS,  0 },
202   { STATE_SOLVABLE,      "rpm:suggests",    STATE_SUGGESTS,     0 },
203   { STATE_SOLVABLE,      "rpm:enhances",    STATE_ENHANCES,     0 },
204   { STATE_SOLVABLE,      "rpm:freshens",    STATE_FRESHENS,     0 },
205   { STATE_SOLVABLE,      "rpm:sourcerpm",   STATE_SOURCERPM,    1 },
206   { STATE_SOLVABLE,      "rpm:header-range", STATE_HEADERRANGE, 0 },
207   { STATE_SOLVABLE,      "file",            STATE_FILE, 1 },
208   { STATE_SOLVABLE,      "changelog",       STATE_CHANGELOG, 1 },
209
210    /* extended Novell/SUSE diskusage attributes (susedata.xml) */
211   { STATE_DISKUSAGE,   "dirs",            STATE_DIRS,         0 },
212   { STATE_DIRS,        "dir",             STATE_DIR,          0 },
213
214   { STATE_PROVIDES,    "rpm:entry",       STATE_PROVIDESENTRY, 0 },
215   { STATE_REQUIRES,    "rpm:entry",       STATE_REQUIRESENTRY, 0 },
216   { STATE_OBSOLETES,   "rpm:entry",       STATE_OBSOLETESENTRY, 0 },
217   { STATE_CONFLICTS,   "rpm:entry",       STATE_CONFLICTSENTRY, 0 },
218   { STATE_RECOMMENDS,  "rpm:entry",       STATE_RECOMMENDSENTRY, 0 },
219   { STATE_SUPPLEMENTS, "rpm:entry",       STATE_SUPPLEMENTSENTRY, 0 },
220   { STATE_SUGGESTS,    "rpm:entry",       STATE_SUGGESTSENTRY, 0 },
221   { STATE_ENHANCES,    "rpm:entry",       STATE_ENHANCESENTRY, 0 },
222   { STATE_FRESHENS,    "rpm:entry",       STATE_FRESHENSENTRY, 0 },
223
224   { STATE_INCLUDES,    "item",            STATE_INCLUDESENTRY, 0 },
225   { STATE_EXTENDS,     "item",            STATE_EXTENDSENTRY,  0 },
226
227   { NUMSTATES}
228 };
229
230 struct parsedata {
231   int ret;
232   Pool *pool;
233   Repo *repo;
234   Repodata *data;
235   char *kind;
236   int depth;
237   enum state state;
238   int statedepth;
239   char *content;
240   int lcontent;
241   int acontent;
242   int docontent;
243   Solvable *solvable;
244   Offset freshens;
245   struct stateswitch *swtab[NUMSTATES];
246   enum state sbtab[NUMSTATES];
247   struct joindata jd;
248   /* temporal to store attribute tag language */
249   const char *tmplang;
250   Id chksumtype;
251   Id handle;
252   XML_Parser *parser;
253   Id (*dirs)[3]; /* dirid, size, nfiles */
254   int ndirs;
255   const char *language;                 /* default language */
256   Id langcache[ID_NUM_INTERNAL];        /* cache for the default language */
257
258   Id lastdir;
259   char *lastdirstr;
260   int lastdirstrl;
261
262   Id changelog_handle;
263
264   /** Hash to maps checksums to solv */
265   Stringpool cspool;
266   /** Cache of known checksums to solvable id */
267   Id *cscache;
268   /* the current longest index in the table */
269   int ncscache;
270 };
271
272 static Id
273 langtag(struct parsedata *pd, Id tag, const char *language)
274 {
275   if (language)
276     {
277       if (!language[0] || !strcmp(language, "en"))
278         return tag;
279       return pool_id2langid(pd->pool, tag, language, 1);
280     }
281   if (!pd->language)
282     return tag;
283   if (tag >= ID_NUM_INTERNAL)
284     return pool_id2langid(pd->pool, tag, pd->language, 1);
285   if (!pd->langcache[tag])
286     pd->langcache[tag] = pool_id2langid(pd->pool, tag, pd->language, 1);
287   return pd->langcache[tag];
288 }
289
290 static int
291 id3_cmp (const void *v1, const void *v2, void *dp)
292 {
293   Id *i1 = (Id*)v1;
294   Id *i2 = (Id*)v2;
295   return i1[0] - i2[0];
296 }
297
298 static void
299 commit_diskusage (struct parsedata *pd, Id handle)
300 {
301   int i;
302   Dirpool *dp = &pd->data->dirpool;
303   /* Now sort in dirid order.  This ensures that parents come before
304      their children.  */
305   if (pd->ndirs > 1)
306     solv_sort(pd->dirs, pd->ndirs, sizeof (pd->dirs[0]), id3_cmp, 0);
307   /* Substract leaf numbers from all parents to make the numbers
308      non-cumulative.  This must be done post-order (i.e. all leafs
309      adjusted before parents).  We ensure this by starting at the end of
310      the array moving to the start, hence seeing leafs before parents.  */
311   for (i = pd->ndirs; i--;)
312     {
313       Id p = dirpool_parent(dp, pd->dirs[i][0]);
314       int j = i;
315       for (; p; p = dirpool_parent(dp, p))
316         {
317           for (; j--;)
318             if (pd->dirs[j][0] == p)
319               break;
320           if (j >= 0)
321             {
322               if (pd->dirs[j][1] < pd->dirs[i][1])
323                 pd->dirs[j][1] = 0;
324               else
325                 pd->dirs[j][1] -= pd->dirs[i][1];
326               if (pd->dirs[j][2] < pd->dirs[i][2])
327                 pd->dirs[j][2] = 0;
328               else
329                 pd->dirs[j][2] -= pd->dirs[i][2];
330             }
331           else
332             /* Haven't found this parent in the list, look further if
333                we maybe find the parents parent.  */
334             j = i;
335         }
336     }
337 #if 0
338   char sbuf[1024];
339   char *buf = sbuf;
340   unsigned slen = sizeof (sbuf);
341   for (i = 0; i < pd->ndirs; i++)
342     {
343       dir2str (attr, pd->dirs[i][0], &buf, &slen);
344       fprintf (stderr, "have dir %d %d %d %s\n", pd->dirs[i][0], pd->dirs[i][1], pd->dirs[i][2], buf);
345     }
346   if (buf != sbuf)
347     free (buf);
348 #endif
349   for (i = 0; i < pd->ndirs; i++)
350     if (pd->dirs[i][1] || pd->dirs[i][2])
351       {
352         repodata_add_dirnumnum(pd->data, handle, SOLVABLE_DISKUSAGE, pd->dirs[i][0], pd->dirs[i][1], pd->dirs[i][2]);
353       }
354   pd->ndirs = 0;
355 }
356
357
358 /*
359  * makeevr_atts
360  * parse 'epoch', 'ver' and 'rel', return evr Id
361  *
362  */
363
364 static Id
365 makeevr_atts(Pool *pool, struct parsedata *pd, const char **atts)
366 {
367   const char *e, *v, *r, *v2;
368   char *c;
369   int l;
370
371   e = v = r = 0;
372   for (; *atts; atts += 2)
373     {
374       if (!strcmp(*atts, "epoch"))
375         e = atts[1];
376       else if (!strcmp(*atts, "ver"))
377         v = atts[1];
378       else if (!strcmp(*atts, "rel"))
379         r = atts[1];
380     }
381   if (e && !strcmp(e, "0"))
382     e = 0;
383   if (v && !e)
384     {
385       for (v2 = v; *v2 >= '0' && *v2 <= '9'; v2++)
386         ;
387       if (v2 > v && *v2 == ':')
388         e = "0";
389     }
390   l = 1;
391   if (e)
392     l += strlen(e) + 1;
393   if (v)
394     l += strlen(v);
395   if (r)
396     l += strlen(r) + 1;
397   if (l > pd->acontent)
398     {
399       pd->content = solv_realloc(pd->content, l + 256);
400       pd->acontent = l + 256;
401     }
402   c = pd->content;
403   if (e)
404     {
405       strcpy(c, e);
406       c += strlen(c);
407       *c++ = ':';
408     }
409   if (v)
410     {
411       strcpy(c, v);
412       c += strlen(c);
413     }
414   if (r)
415     {
416       *c++ = '-';
417       strcpy(c, r);
418       c += strlen(c);
419     }
420   *c = 0;
421   if (!*pd->content)
422     return 0;
423 #if 0
424   fprintf(stderr, "evr: %s\n", pd->content);
425 #endif
426   return pool_str2id(pool, pd->content, 1);
427 }
428
429
430 /*
431  * find_attr
432  * find value for xml attribute
433  * I: txt, name of attribute
434  * I: atts, list of key/value attributes
435  * O: pointer to value of matching key, or NULL
436  *
437  */
438
439 static inline const char *
440 find_attr(const char *txt, const char **atts)
441 {
442   for (; *atts; atts += 2)
443     {
444       if (!strcmp(*atts, txt))
445         return atts[1];
446     }
447   return 0;
448 }
449
450
451 /*
452  * dependency relations
453  */
454
455 static char *flagtab[] = {
456   "GT",
457   "EQ",
458   "GE",
459   "LT",
460   "NE",
461   "LE"
462 };
463
464
465 /*
466  * adddep
467  * parse attributes to reldep Id
468  *
469  */
470
471 static unsigned int
472 adddep(Pool *pool, struct parsedata *pd, unsigned int olddeps, const char **atts, int isreq)
473 {
474   Id id, name, marker;
475   const char *n, *f, *k;
476   const char **a;
477
478   n = f = k = 0;
479   marker = isreq ? -SOLVABLE_PREREQMARKER : 0;
480   for (a = atts; *a; a += 2)
481     {
482       if (!strcmp(*a, "name"))
483         n = a[1];
484       else if (!strcmp(*a, "flags"))
485         f = a[1];
486       else if (!strcmp(*a, "kind"))
487         k = a[1];
488       else if (isreq && !strcmp(*a, "pre") && a[1][0] == '1')
489         marker = SOLVABLE_PREREQMARKER;
490     }
491   if (!n)
492     return olddeps;
493   if (k && !strcmp(k, "package"))
494     k = 0;
495   if (k)
496     {
497       int l = strlen(k) + 1 + strlen(n) + 1;
498       if (l > pd->acontent)
499         {
500           pd->content = solv_realloc(pd->content, l + 256);
501           pd->acontent = l + 256;
502         }
503       sprintf(pd->content, "%s:%s", k, n);
504       name = pool_str2id(pool, pd->content, 1);
505     }
506   else
507     name = pool_str2id(pool, (char *)n, 1);
508   if (f)
509     {
510       Id evr = makeevr_atts(pool, pd, atts);
511       int flags;
512       for (flags = 0; flags < 6; flags++)
513         if (!strcmp(f, flagtab[flags]))
514           break;
515       flags = flags < 6 ? flags + 1 : 0;
516       id = pool_rel2id(pool, name, evr, flags, 1);
517     }
518   else
519     id = name;
520 #if 0
521   fprintf(stderr, "new dep %s%s%s\n", pool_id2str(pool, d), id2rel(pool, d), id2evr(pool, d));
522 #endif
523   return repo_addid_dep(pd->repo, olddeps, id, marker);
524 }
525
526
527 /*
528  * set_description_author
529  *
530  */
531 static void
532 set_description_author(Repodata *data, Id handle, char *str, struct parsedata *pd)
533 {
534   char *aut, *p;
535
536   if (!str || !*str)
537     return;
538   for (aut = str; (aut = strchr(aut, '\n')) != 0; aut++)
539     if (!strncmp(aut, "\nAuthors:\n--------\n", 19))
540       break;
541   if (aut)
542     {
543       /* oh my, found SUSE special author section */
544       int l = aut - str;
545       str[l] = 0;
546       while (l > 0 && str[l - 1] == '\n')
547         str[--l] = 0;
548       if (l)
549         repodata_set_str(data, handle, langtag(pd, SOLVABLE_DESCRIPTION, pd->tmplang), str);
550       p = aut + 19;
551       aut = str;        /* copy over */
552       while (*p == ' ' || *p == '\n')
553         p++;
554       while (*p)
555         {
556           if (*p == '\n')
557             {
558               *aut++ = *p++;
559               while (*p == ' ')
560                 p++;
561               continue;
562             }
563           *aut++ = *p++;
564         }
565       while (aut != str && aut[-1] == '\n')
566         aut--;
567       *aut = 0;
568       if (*str)
569         repodata_set_str(data, handle, SOLVABLE_AUTHORS, str);
570     }
571   else if (*str)
572     repodata_set_str(data, handle, langtag(pd, SOLVABLE_DESCRIPTION, pd->tmplang), str);
573 }
574
575
576 /*-----------------------------------------------*/
577 /* XML callbacks */
578
579 /*
580  * startElement
581  * XML callback
582  *
583  */
584
585 static void XMLCALL
586 startElement(void *userData, const char *name, const char **atts)
587 {
588   struct parsedata *pd = userData;
589   Pool *pool = pd->pool;
590   Solvable *s = pd->solvable;
591   struct stateswitch *sw;
592   const char *str;
593   Id handle = pd->handle;
594   const char *pkgid;
595
596   /* fprintf(stderr, "into %s, from %d, depth %d, statedepth %d\n", name, pd->state, pd->depth, pd->statedepth); */
597
598   if (pd->depth != pd->statedepth)
599     {
600       pd->depth++;
601       return;
602     }
603
604   if (pd->state == STATE_START && !strcmp(name, "patterns"))
605     return;
606   if (pd->state == STATE_START && !strcmp(name, "products"))
607     return;
608 #if 0
609   if (pd->state == STATE_START && !strcmp(name, "metadata"))
610     return;
611 #endif
612   if (pd->state == STATE_SOLVABLE && !strcmp(name, "format"))
613     return;
614
615   pd->depth++;
616   if (!pd->swtab[pd->state])
617     return;
618   for (sw = pd->swtab[pd->state]; sw->from == pd->state; sw++)
619     if (!strcmp(sw->ename, name))
620       break;
621   if (sw->from != pd->state)
622     {
623 #if 0
624       fprintf(stderr, "into unknown: %s\n", name);
625 #endif
626       return;
627     }
628   pd->state = sw->to;
629   pd->docontent = sw->docontent;
630   pd->statedepth = pd->depth;
631   pd->lcontent = 0;
632   *pd->content = 0;
633
634   if (!s && pd->state != STATE_SOLVABLE)
635     return;
636
637   switch(pd->state)
638     {
639     case STATE_SOLVABLE:
640       pd->kind = 0;
641       if (name[2] == 't' && name[3] == 't')
642         pd->kind = "pattern";
643       else if (name[1] == 'r')
644         pd->kind = "product";
645       else if (name[2] == 't' && name[3] == 'c')
646         pd->kind = "patch";
647
648       /* to support extension metadata files like others.xml which
649          have the following structure:
650
651          <otherdata xmlns="http://linux.duke.edu/metadata/other"
652                     packages="101">
653            <package pkgid="b78f8664cd90efe42e09a345e272997ef1b53c18"
654                     name="zaptel-kmp-default"
655                     arch="i586"><version epoch="0"
656                     ver="1.2.10_2.6.22_rc4_git6_2" rel="70"/>
657               ...
658
659          we need to check if the pkgid is there and if it matches
660          an already seen package, that means we don't need to create
661          a new solvable but just append the attributes to the existing
662          one.
663       */
664       if ((pkgid = find_attr("pkgid", atts)) != NULL)
665         {
666           /* look at the checksum cache */
667           Id index = stringpool_str2id(&pd->cspool, pkgid, 0);
668           if (!index || index >= pd->ncscache || !pd->cscache[index])
669             {
670               pool_debug(pool, SOLV_WARN, "the repository specifies extra information about package with checksum '%s', which does not exist in the repository.\n", pkgid);
671               pd->solvable = 0;
672               pd->handle = 0;
673               break;
674             }
675           pd->solvable = pool_id2solvable(pool, pd->cscache[index]);
676         }
677        else
678         {
679           /* this is a new package */
680           pd->solvable = pool_id2solvable(pool, repo_add_solvable(pd->repo));
681           pd->freshens = 0;
682         }
683       pd->handle = pd->solvable - pool->solvables;
684 #if 0
685       fprintf(stderr, "package #%d\n", pd->solvable - pool->solvables);
686 #endif
687
688       break;
689     case STATE_VERSION:
690       s->evr = makeevr_atts(pool, pd, atts);
691       break;
692     case STATE_PROVIDES:
693       s->provides = 0;
694       break;
695     case STATE_PROVIDESENTRY:
696       s->provides = adddep(pool, pd, s->provides, atts, 0);
697       break;
698     case STATE_REQUIRES:
699       s->requires = 0;
700       break;
701     case STATE_REQUIRESENTRY:
702       s->requires = adddep(pool, pd, s->requires, atts, 1);
703       break;
704     case STATE_OBSOLETES:
705       s->obsoletes = 0;
706       break;
707     case STATE_OBSOLETESENTRY:
708       s->obsoletes = adddep(pool, pd, s->obsoletes, atts, 0);
709       break;
710     case STATE_CONFLICTS:
711       s->conflicts = 0;
712       break;
713     case STATE_CONFLICTSENTRY:
714       s->conflicts = adddep(pool, pd, s->conflicts, atts, 0);
715       break;
716     case STATE_RECOMMENDS:
717       s->recommends = 0;
718       break;
719     case STATE_RECOMMENDSENTRY:
720       s->recommends = adddep(pool, pd, s->recommends, atts, 0);
721       break;
722     case STATE_SUPPLEMENTS:
723       s->supplements= 0;
724       break;
725     case STATE_SUPPLEMENTSENTRY:
726       s->supplements = adddep(pool, pd, s->supplements, atts, 0);
727       break;
728     case STATE_SUGGESTS:
729       s->suggests = 0;
730       break;
731     case STATE_SUGGESTSENTRY:
732       s->suggests = adddep(pool, pd, s->suggests, atts, 0);
733       break;
734     case STATE_ENHANCES:
735       s->enhances = 0;
736       break;
737     case STATE_ENHANCESENTRY:
738       s->enhances = adddep(pool, pd, s->enhances, atts, 0);
739       break;
740     case STATE_FRESHENS:
741       pd->freshens = 0;
742       break;
743     case STATE_FRESHENSENTRY:
744       pd->freshens = adddep(pool, pd, pd->freshens, atts, 0);
745       break;
746     case STATE_EULA:
747     case STATE_SUMMARY:
748     case STATE_CATEGORY:
749     case STATE_DESCRIPTION:
750       pd->tmplang = join_dup(&pd->jd, find_attr("lang", atts));
751       break;
752     case STATE_USERVISIBLE:
753       repodata_set_void(pd->data, handle, SOLVABLE_ISVISIBLE);
754       break;
755     case STATE_INCLUDESENTRY:
756       str = find_attr("pattern", atts);
757       if (str)
758         repodata_add_poolstr_array(pd->data, pd->handle, SOLVABLE_INCLUDES, join2(&pd->jd, "pattern", ":", str));
759       break;
760     case STATE_EXTENDSENTRY:
761       str = find_attr("pattern", atts);
762       if (str)
763         repodata_add_poolstr_array(pd->data, pd->handle, SOLVABLE_EXTENDS, join2(&pd->jd, "pattern", ":", str));
764       break;
765     case STATE_LOCATION:
766       str = find_attr("href", atts);
767       if (str)
768         repodata_set_location(pd->data, handle, 0, 0, str);
769       break;
770     case STATE_CHECKSUM:
771       str = find_attr("type", atts);
772       pd->chksumtype = str && *str ? solv_chksum_str2type(str) : 0;
773       if (!pd->chksumtype)
774         pd->ret = pool_error(pool, -1, "line %d: unknown checksum type: %s", (unsigned int)XML_GetCurrentLineNumber(*pd->parser), str ? str : "NULL");
775       break;
776     case STATE_TIME:
777       {
778         unsigned int t;
779         str = find_attr("build", atts);
780         if (str && (t = atoi(str)) != 0)
781           repodata_set_num(pd->data, handle, SOLVABLE_BUILDTIME, t);
782         break;
783       }
784     case STATE_SIZE:
785       if ((str = find_attr("installed", atts)) != 0)
786         repodata_set_num(pd->data, handle, SOLVABLE_INSTALLSIZE, strtoull(str, 0, 10));
787       if ((str = find_attr("package", atts)) != 0)
788         repodata_set_num(pd->data, handle, SOLVABLE_DOWNLOADSIZE, strtoull(str, 0, 10));
789       break;
790     case STATE_HEADERRANGE:
791       {
792         unsigned int end;
793         str = find_attr("end", atts);
794         if (str && (end = atoi(str)) != 0)
795           repodata_set_num(pd->data, handle, SOLVABLE_HEADEREND, end);
796         break;
797       }
798       /*
799         <diskusage>
800           <dirs>
801             <dir name="/" size="56" count="11"/>
802             <dir name="usr/" size="56" count="11"/>
803             <dir name="usr/bin/" size="38" count="10"/>
804             <dir name="usr/share/" size="18" count="1"/>
805             <dir name="usr/share/doc/" size="18" count="1"/>
806           </dirs>
807         </diskusage>
808       */
809     case STATE_DISKUSAGE:
810       {
811         /* Really, do nothing, wait for <dir> tag */
812         break;
813       }
814     case STATE_DIR:
815       {
816         long filesz = 0, filenum = 0;
817         Id dirid;
818         if ((str = find_attr("name", atts)) != 0)
819           dirid = repodata_str2dir(pd->data, str, 1);
820         else
821           {
822             pd->ret = pool_error(pool, -1, "<dir .../> tag without 'name' attribute");
823             break;
824           }
825         if ((str = find_attr("size", atts)) != 0)
826           filesz = strtol(str, 0, 0);
827         if ((str = find_attr("count", atts)) != 0)
828           filenum = strtol(str, 0, 0);
829         pd->dirs = solv_extend(pd->dirs, pd->ndirs, 1, sizeof(pd->dirs[0]), 31);
830         pd->dirs[pd->ndirs][0] = dirid;
831         pd->dirs[pd->ndirs][1] = filesz;
832         pd->dirs[pd->ndirs][2] = filenum;
833         pd->ndirs++;
834         break;
835       }
836     case STATE_CHANGELOG:
837       pd->changelog_handle = repodata_new_handle(pd->data);
838       if ((str = find_attr("date", atts)) != 0)
839         repodata_set_num(pd->data, pd->changelog_handle, SOLVABLE_CHANGELOG_TIME, strtoull(str, 0, 10));
840       if ((str = find_attr("author", atts)) != 0)
841         repodata_set_str(pd->data, pd->changelog_handle, SOLVABLE_CHANGELOG_AUTHOR, str);
842       break;
843     default:
844       break;
845     }
846 }
847
848
849 /*
850  * endElement
851  * XML callback
852  *
853  */
854
855 static void XMLCALL
856 endElement(void *userData, const char *name)
857 {
858   struct parsedata *pd = userData;
859   Pool *pool = pd->pool;
860   Solvable *s = pd->solvable;
861   Repo *repo = pd->repo;
862   Id handle = pd->handle;
863   Id id;
864   char *p;
865
866   if (pd->depth != pd->statedepth)
867     {
868       pd->depth--;
869       /* printf("back from unknown %d %d %d\n", pd->state, pd->depth, pd->statedepth); */
870       return;
871     }
872
873   /* ignore patterns & metadata */
874   if (pd->state == STATE_START && !strcmp(name, "patterns"))
875     return;
876   if (pd->state == STATE_START && !strcmp(name, "products"))
877     return;
878 #if 0
879   if (pd->state == STATE_START && !strcmp(name, "metadata"))
880     return;
881 #endif
882   if (pd->state == STATE_SOLVABLE && !strcmp(name, "format"))
883     return;
884
885   pd->depth--;
886   pd->statedepth--;
887
888
889   if (!s)
890     {
891       pd->state = pd->sbtab[pd->state];
892       pd->docontent = 0;
893       return;
894     }
895
896   switch (pd->state)
897     {
898     case STATE_SOLVABLE:
899       if (pd->kind && !s->name) /* add namespace in case of NULL name */
900         s->name = pool_str2id(pool, join2(&pd->jd, pd->kind, ":", 0), 1);
901       if (!s->arch)
902         s->arch = ARCH_NOARCH;
903       if (!s->evr)
904         s->evr = ID_EMPTY;      /* some patterns have this */
905       if (s->name && s->arch != ARCH_SRC && s->arch != ARCH_NOSRC)
906         s->provides = repo_addid_dep(repo, s->provides, pool_rel2id(pool, s->name, s->evr, REL_EQ, 1), 0);
907       s->supplements = repo_fix_supplements(repo, s->provides, s->supplements, pd->freshens);
908       s->conflicts = repo_fix_conflicts(repo, s->conflicts);
909       pd->freshens = 0;
910       pd->kind = 0;
911       pd->solvable = s = 0;
912       break;
913     case STATE_NAME:
914       if (pd->kind)
915         s->name = pool_str2id(pool, join2(&pd->jd, pd->kind, ":", pd->content), 1);
916       else
917         s->name = pool_str2id(pool, pd->content, 1);
918       break;
919     case STATE_ARCH:
920       s->arch = pool_str2id(pool, pd->content, 1);
921       break;
922     case STATE_VENDOR:
923       s->vendor = pool_str2id(pool, pd->content, 1);
924       break;
925     case STATE_RPM_GROUP:
926       repodata_set_poolstr(pd->data, handle, SOLVABLE_GROUP, pd->content);
927       break;
928     case STATE_RPM_LICENSE:
929       repodata_set_poolstr(pd->data, handle, SOLVABLE_LICENSE, pd->content);
930       break;
931     case STATE_CHECKSUM:
932       {
933         Id index;
934         
935         if (!pd->chksumtype)
936           break;
937         if (strlen(pd->content) != 2 * solv_chksum_len(pd->chksumtype))
938           {
939             pd->ret = pool_error(pool, -1, "line %d: invalid checksum length for %s", (unsigned int)XML_GetCurrentLineNumber(*pd->parser), solv_chksum_type2str(pd->chksumtype));
940             break;
941           }
942         repodata_set_checksum(pd->data, handle, SOLVABLE_CHECKSUM, pd->chksumtype, pd->content);
943         /* we save the checksum to solvable id relationship for extended
944            metadata */
945         index = stringpool_str2id(&pd->cspool, pd->content, 1 /* create it */);
946         if (index >= pd->ncscache)
947           {
948             pd->cscache = solv_zextend(pd->cscache, pd->ncscache, index + 1 - pd->ncscache, sizeof(Id), 255);
949             pd->ncscache = index + 1;
950           }
951         /* add the checksum to the cache */
952         pd->cscache[index] = s - pool->solvables;
953         break;
954       }
955     case STATE_FILE:
956 #if 0
957       id = pool_str2id(pool, pd->content, 1);
958       s->provides = repo_addid_dep(repo, s->provides, id, SOLVABLE_FILEMARKER);
959 #endif
960       if ((p = strrchr(pd->content, '/')) != 0)
961         {
962           *p++ = 0;
963           if (pd->lastdir && !strcmp(pd->lastdirstr, pd->content))
964             {
965               id = pd->lastdir;
966             }
967           else
968             {
969               int l;
970               id = repodata_str2dir(pd->data, pd->content, 1);
971               l = strlen(pd->content) + 1;
972               if (l > pd->lastdirstrl)
973                 {
974                   pd->lastdirstrl = l + 128;
975                   pd->lastdirstr = solv_realloc(pd->lastdirstr, pd->lastdirstrl);
976                 }
977               strcpy(pd->lastdirstr, pd->content);
978               pd->lastdir = id;
979             }
980         }
981       else
982         {
983           p = pd->content;
984           id = 0;
985         }
986       if (!id)
987         id = repodata_str2dir(pd->data, "/", 1);
988       repodata_add_dirstr(pd->data, handle, SOLVABLE_FILELIST, id, p);
989       break;
990     case STATE_SUMMARY:
991       repodata_set_str(pd->data, handle, langtag(pd, SOLVABLE_SUMMARY, pd->tmplang), pd->content);
992       break;
993     case STATE_DESCRIPTION:
994       set_description_author(pd->data, handle, pd->content, pd);
995       break;
996     case STATE_CATEGORY:
997       repodata_set_str(pd->data, handle, langtag(pd, SOLVABLE_CATEGORY, pd->tmplang), pd->content);
998       break;
999     case STATE_DISTRIBUTION:
1000         repodata_set_poolstr(pd->data, handle, SOLVABLE_DISTRIBUTION, pd->content);
1001         break;
1002     case STATE_URL:
1003       if (pd->content[0])
1004         repodata_set_str(pd->data, handle, SOLVABLE_URL, pd->content);
1005       break;
1006     case STATE_PACKAGER:
1007       if (pd->content[0])
1008         repodata_set_poolstr(pd->data, handle, SOLVABLE_PACKAGER, pd->content);
1009       break;
1010     case STATE_SOURCERPM:
1011       if (pd->content[0])
1012         repodata_set_sourcepkg(pd->data, handle, pd->content);
1013       break;
1014     case STATE_RELNOTESURL:
1015       if (pd->content[0])
1016         {
1017           repodata_add_poolstr_array(pd->data, pd->handle, PRODUCT_URL, pd->content);
1018           repodata_add_idarray(pd->data, pd->handle, PRODUCT_URL_TYPE, pool_str2id(pool, "releasenotes", 1));
1019         }
1020       break;
1021     case STATE_UPDATEURL:
1022       if (pd->content[0])
1023         {
1024           repodata_add_poolstr_array(pd->data, pd->handle, PRODUCT_URL, pd->content);
1025           repodata_add_idarray(pd->data, pd->handle, PRODUCT_URL_TYPE, pool_str2id(pool, "update", 1));
1026         }
1027       break;
1028     case STATE_OPTIONALURL:
1029       if (pd->content[0])
1030         {
1031           repodata_add_poolstr_array(pd->data, pd->handle, PRODUCT_URL, pd->content);
1032           repodata_add_idarray(pd->data, pd->handle, PRODUCT_URL_TYPE, pool_str2id(pool, "optional", 1));
1033         }
1034       break;
1035     case STATE_FLAG:
1036       if (pd->content[0])
1037           repodata_set_poolstr(pd->data, handle, PRODUCT_FLAGS, pd->content);
1038       break;
1039     case STATE_EULA:
1040       if (pd->content[0])
1041         repodata_set_str(pd->data, handle, langtag(pd, SOLVABLE_EULA, pd->tmplang), pd->content);
1042       break;
1043     case STATE_KEYWORD:
1044       if (pd->content[0])
1045         repodata_add_poolstr_array(pd->data, pd->handle, SOLVABLE_KEYWORDS, pd->content);
1046       break;
1047     case STATE_DISKUSAGE:
1048       if (pd->ndirs)
1049         commit_diskusage(pd, pd->handle);
1050       break;
1051     case STATE_ORDER:
1052       if (pd->content[0])
1053         repodata_set_str(pd->data, pd->handle, SOLVABLE_ORDER, pd->content);
1054       break;
1055     case STATE_CHANGELOG:
1056       repodata_set_str(pd->data, pd->changelog_handle, SOLVABLE_CHANGELOG_DESCRIPTION, pd->content);
1057       repodata_add_flexarray(pd->data, pd->handle, SOLVABLE_CHANGELOG, pd->changelog_handle);
1058       pd->changelog_handle = 0;
1059       break;
1060     default:
1061       break;
1062     }
1063   pd->state = pd->sbtab[pd->state];
1064   pd->docontent = 0;
1065   /* fprintf(stderr, "back from known %d %d %d\n", pd->state, pd->depth, pd->statedepth); */
1066 }
1067
1068
1069 /*
1070  * characterData
1071  * XML callback
1072  *
1073  */
1074
1075 static void XMLCALL
1076 characterData(void *userData, const XML_Char *s, int len)
1077 {
1078   struct parsedata *pd = userData;
1079   int l;
1080   char *c;
1081
1082   if (!pd->docontent)
1083     return;
1084   l = pd->lcontent + len + 1;
1085   if (l > pd->acontent)
1086     {
1087       pd->content = solv_realloc(pd->content, l + 256);
1088       pd->acontent = l + 256;
1089     }
1090   c = pd->content + pd->lcontent;
1091   pd->lcontent += len;
1092   while (len-- > 0)
1093     *c++ = *s++;
1094   *c = 0;
1095 }
1096
1097
1098 /*-----------------------------------------------*/
1099 /* 'main' */
1100
1101 #define BUFF_SIZE 8192
1102
1103 /*
1104  * repo_add_rpmmd
1105  * parse rpm-md metadata (primary, others)
1106  *
1107  */
1108
1109 int
1110 repo_add_rpmmd(Repo *repo, FILE *fp, const char *language, int flags)
1111 {
1112   Pool *pool = repo->pool;
1113   struct parsedata pd;
1114   char buf[BUFF_SIZE];
1115   int i, l;
1116   struct stateswitch *sw;
1117   Repodata *data;
1118   unsigned int now;
1119   XML_Parser parser;
1120
1121   now = solv_timems(0);
1122   data = repo_add_repodata(repo, flags);
1123
1124   memset(&pd, 0, sizeof(pd));
1125   for (i = 0, sw = stateswitches; sw->from != NUMSTATES; i++, sw++)
1126     {
1127       if (!pd.swtab[sw->from])
1128         pd.swtab[sw->from] = sw;
1129       pd.sbtab[sw->to] = sw->from;
1130     }
1131   pd.pool = pool;
1132   pd.repo = repo;
1133   pd.data = data;
1134
1135   pd.content = solv_malloc(256);
1136   pd.acontent = 256;
1137   pd.lcontent = 0;
1138   pd.kind = 0;
1139   pd.language = language && *language && strcmp(language, "en") != 0 ? language : 0;
1140
1141   /* initialize the string pool where we will store
1142      the package checksums we know about, to get an Id
1143      we can use in a cache */
1144   stringpool_init_empty(&pd.cspool);
1145   if ((flags & REPO_EXTEND_SOLVABLES) != 0)
1146     {
1147       /* setup join data */
1148       Dataiterator di;
1149       dataiterator_init(&di, pool, repo, 0, SOLVABLE_CHECKSUM, 0, 0);
1150       while (dataiterator_step(&di))
1151         {
1152           const char *str;
1153           int index;
1154
1155           if (!solv_chksum_len(di.key->type))
1156             continue;
1157           str = repodata_chk2str(di.data, di.key->type, (const unsigned char *)di.kv.str);
1158           index = stringpool_str2id(&pd.cspool, str, 1);
1159           if (index >= pd.ncscache)
1160             {
1161               pd.cscache = solv_zextend(pd.cscache, pd.ncscache, index + 1 - pd.ncscache, sizeof(Id), 255);
1162               pd.ncscache = index + 1;
1163             }
1164           pd.cscache[index] = di.solvid;
1165         }
1166       dataiterator_free(&di);
1167     }
1168
1169   parser = XML_ParserCreate(NULL);
1170   XML_SetUserData(parser, &pd);
1171   pd.parser = &parser;
1172   XML_SetElementHandler(parser, startElement, endElement);
1173   XML_SetCharacterDataHandler(parser, characterData);
1174   for (;;)
1175     {
1176       l = fread(buf, 1, sizeof(buf), fp);
1177       if (XML_Parse(parser, buf, l, l == 0) == XML_STATUS_ERROR)
1178         {
1179           pd.ret = pool_error(pool, -1, "repo_rpmmd: %s at line %u:%u", XML_ErrorString(XML_GetErrorCode(parser)), (unsigned int)XML_GetCurrentLineNumber(parser), (unsigned int)XML_GetCurrentColumnNumber(parser));
1180           break;
1181         }
1182       if (l == 0)
1183         break;
1184     }
1185   XML_ParserFree(parser);
1186   solv_free(pd.content);
1187   solv_free(pd.lastdirstr);
1188   join_freemem(&pd.jd);
1189   stringpool_free(&pd.cspool);
1190   solv_free(pd.cscache);
1191   repodata_free_dircache(data);
1192
1193   if (!(flags & REPO_NO_INTERNALIZE))
1194     repodata_internalize(data);
1195   POOL_DEBUG(SOLV_DEBUG_STATS, "repo_add_rpmmd took %d ms\n", solv_timems(now));
1196   POOL_DEBUG(SOLV_DEBUG_STATS, "repo size: %d solvables\n", repo->nsolvables);
1197   POOL_DEBUG(SOLV_DEBUG_STATS, "repo memory used: %d K incore, %d K idarray\n", repodata_memused(data)/1024, repo->idarraysize / (int)(1024/sizeof(Id)));
1198   return pd.ret;
1199 }