be more tolerant about bad xml: an empty epoch attribute means no epoch
[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 && (!*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\n", pool_dep2str(pool, id));
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         {
769           repodata_set_location(pd->data, handle, 0, 0, str);
770           str = find_attr("xml:base", atts);
771           if (str)
772             repodata_set_poolstr(pd->data, handle, SOLVABLE_MEDIABASE, str);
773         }
774       break;
775     case STATE_CHECKSUM:
776       str = find_attr("type", atts);
777       pd->chksumtype = str && *str ? solv_chksum_str2type(str) : 0;
778       if (!pd->chksumtype)
779         pd->ret = pool_error(pool, -1, "line %d: unknown checksum type: %s", (unsigned int)XML_GetCurrentLineNumber(*pd->parser), str ? str : "NULL");
780       break;
781     case STATE_TIME:
782       {
783         unsigned int t;
784         str = find_attr("build", atts);
785         if (str && (t = atoi(str)) != 0)
786           repodata_set_num(pd->data, handle, SOLVABLE_BUILDTIME, t);
787         break;
788       }
789     case STATE_SIZE:
790       if ((str = find_attr("installed", atts)) != 0)
791         repodata_set_num(pd->data, handle, SOLVABLE_INSTALLSIZE, strtoull(str, 0, 10));
792       if ((str = find_attr("package", atts)) != 0)
793         repodata_set_num(pd->data, handle, SOLVABLE_DOWNLOADSIZE, strtoull(str, 0, 10));
794       break;
795     case STATE_HEADERRANGE:
796       {
797         unsigned int end;
798         str = find_attr("end", atts);
799         if (str && (end = atoi(str)) != 0)
800           repodata_set_num(pd->data, handle, SOLVABLE_HEADEREND, end);
801         break;
802       }
803       /*
804         <diskusage>
805           <dirs>
806             <dir name="/" size="56" count="11"/>
807             <dir name="usr/" size="56" count="11"/>
808             <dir name="usr/bin/" size="38" count="10"/>
809             <dir name="usr/share/" size="18" count="1"/>
810             <dir name="usr/share/doc/" size="18" count="1"/>
811           </dirs>
812         </diskusage>
813       */
814     case STATE_DISKUSAGE:
815       {
816         /* Really, do nothing, wait for <dir> tag */
817         break;
818       }
819     case STATE_DIR:
820       {
821         long filesz = 0, filenum = 0;
822         Id dirid;
823         if ((str = find_attr("name", atts)) != 0)
824           dirid = repodata_str2dir(pd->data, str, 1);
825         else
826           {
827             pd->ret = pool_error(pool, -1, "<dir .../> tag without 'name' attribute");
828             break;
829           }
830         if ((str = find_attr("size", atts)) != 0)
831           filesz = strtol(str, 0, 0);
832         if ((str = find_attr("count", atts)) != 0)
833           filenum = strtol(str, 0, 0);
834         pd->dirs = solv_extend(pd->dirs, pd->ndirs, 1, sizeof(pd->dirs[0]), 31);
835         pd->dirs[pd->ndirs][0] = dirid;
836         pd->dirs[pd->ndirs][1] = filesz;
837         pd->dirs[pd->ndirs][2] = filenum;
838         pd->ndirs++;
839         break;
840       }
841     case STATE_CHANGELOG:
842       pd->changelog_handle = repodata_new_handle(pd->data);
843       if ((str = find_attr("date", atts)) != 0)
844         repodata_set_num(pd->data, pd->changelog_handle, SOLVABLE_CHANGELOG_TIME, strtoull(str, 0, 10));
845       if ((str = find_attr("author", atts)) != 0)
846         repodata_set_str(pd->data, pd->changelog_handle, SOLVABLE_CHANGELOG_AUTHOR, str);
847       break;
848     default:
849       break;
850     }
851 }
852
853
854 /*
855  * endElement
856  * XML callback
857  *
858  */
859
860 static void XMLCALL
861 endElement(void *userData, const char *name)
862 {
863   struct parsedata *pd = userData;
864   Pool *pool = pd->pool;
865   Solvable *s = pd->solvable;
866   Repo *repo = pd->repo;
867   Id handle = pd->handle;
868   Id id;
869   char *p;
870
871   if (pd->depth != pd->statedepth)
872     {
873       pd->depth--;
874       /* printf("back from unknown %d %d %d\n", pd->state, pd->depth, pd->statedepth); */
875       return;
876     }
877
878   /* ignore patterns & metadata */
879   if (pd->state == STATE_START && !strcmp(name, "patterns"))
880     return;
881   if (pd->state == STATE_START && !strcmp(name, "products"))
882     return;
883 #if 0
884   if (pd->state == STATE_START && !strcmp(name, "metadata"))
885     return;
886 #endif
887   if (pd->state == STATE_SOLVABLE && !strcmp(name, "format"))
888     return;
889
890   pd->depth--;
891   pd->statedepth--;
892
893
894   if (!s)
895     {
896       pd->state = pd->sbtab[pd->state];
897       pd->docontent = 0;
898       return;
899     }
900
901   switch (pd->state)
902     {
903     case STATE_SOLVABLE:
904       if (pd->kind && !s->name) /* add namespace in case of NULL name */
905         s->name = pool_str2id(pool, join2(&pd->jd, pd->kind, ":", 0), 1);
906       if (!s->arch)
907         s->arch = ARCH_NOARCH;
908       if (!s->evr)
909         s->evr = ID_EMPTY;      /* some patterns have this */
910       if (s->name && s->arch != ARCH_SRC && s->arch != ARCH_NOSRC)
911         s->provides = repo_addid_dep(repo, s->provides, pool_rel2id(pool, s->name, s->evr, REL_EQ, 1), 0);
912       s->supplements = repo_fix_supplements(repo, s->provides, s->supplements, pd->freshens);
913       s->conflicts = repo_fix_conflicts(repo, s->conflicts);
914       pd->freshens = 0;
915       pd->kind = 0;
916       pd->solvable = s = 0;
917       break;
918     case STATE_NAME:
919       if (pd->kind)
920         s->name = pool_str2id(pool, join2(&pd->jd, pd->kind, ":", pd->content), 1);
921       else
922         s->name = pool_str2id(pool, pd->content, 1);
923       break;
924     case STATE_ARCH:
925       s->arch = pool_str2id(pool, pd->content, 1);
926       break;
927     case STATE_VENDOR:
928       s->vendor = pool_str2id(pool, pd->content, 1);
929       break;
930     case STATE_RPM_GROUP:
931       repodata_set_poolstr(pd->data, handle, SOLVABLE_GROUP, pd->content);
932       break;
933     case STATE_RPM_LICENSE:
934       repodata_set_poolstr(pd->data, handle, SOLVABLE_LICENSE, pd->content);
935       break;
936     case STATE_CHECKSUM:
937       {
938         Id index;
939         
940         if (!pd->chksumtype)
941           break;
942         if (strlen(pd->content) != 2 * solv_chksum_len(pd->chksumtype))
943           {
944             pd->ret = pool_error(pool, -1, "line %d: invalid checksum length for %s", (unsigned int)XML_GetCurrentLineNumber(*pd->parser), solv_chksum_type2str(pd->chksumtype));
945             break;
946           }
947         repodata_set_checksum(pd->data, handle, SOLVABLE_CHECKSUM, pd->chksumtype, pd->content);
948         /* we save the checksum to solvable id relationship for extended
949            metadata */
950         index = stringpool_str2id(&pd->cspool, pd->content, 1 /* create it */);
951         if (index >= pd->ncscache)
952           {
953             pd->cscache = solv_zextend(pd->cscache, pd->ncscache, index + 1 - pd->ncscache, sizeof(Id), 255);
954             pd->ncscache = index + 1;
955           }
956         /* add the checksum to the cache */
957         pd->cscache[index] = s - pool->solvables;
958         break;
959       }
960     case STATE_FILE:
961 #if 0
962       id = pool_str2id(pool, pd->content, 1);
963       s->provides = repo_addid_dep(repo, s->provides, id, SOLVABLE_FILEMARKER);
964 #endif
965       if ((p = strrchr(pd->content, '/')) != 0)
966         {
967           *p++ = 0;
968           if (pd->lastdir && !strcmp(pd->lastdirstr, pd->content))
969             {
970               id = pd->lastdir;
971             }
972           else
973             {
974               int l;
975               id = repodata_str2dir(pd->data, pd->content, 1);
976               l = strlen(pd->content) + 1;
977               if (l > pd->lastdirstrl)
978                 {
979                   pd->lastdirstrl = l + 128;
980                   pd->lastdirstr = solv_realloc(pd->lastdirstr, pd->lastdirstrl);
981                 }
982               strcpy(pd->lastdirstr, pd->content);
983               pd->lastdir = id;
984             }
985         }
986       else
987         {
988           p = pd->content;
989           id = 0;
990         }
991       if (!id)
992         id = repodata_str2dir(pd->data, "/", 1);
993       repodata_add_dirstr(pd->data, handle, SOLVABLE_FILELIST, id, p);
994       break;
995     case STATE_SUMMARY:
996       repodata_set_str(pd->data, handle, langtag(pd, SOLVABLE_SUMMARY, pd->tmplang), pd->content);
997       break;
998     case STATE_DESCRIPTION:
999       set_description_author(pd->data, handle, pd->content, pd);
1000       break;
1001     case STATE_CATEGORY:
1002       repodata_set_str(pd->data, handle, langtag(pd, SOLVABLE_CATEGORY, pd->tmplang), pd->content);
1003       break;
1004     case STATE_DISTRIBUTION:
1005         repodata_set_poolstr(pd->data, handle, SOLVABLE_DISTRIBUTION, pd->content);
1006         break;
1007     case STATE_URL:
1008       if (pd->content[0])
1009         repodata_set_str(pd->data, handle, SOLVABLE_URL, pd->content);
1010       break;
1011     case STATE_PACKAGER:
1012       if (pd->content[0])
1013         repodata_set_poolstr(pd->data, handle, SOLVABLE_PACKAGER, pd->content);
1014       break;
1015     case STATE_SOURCERPM:
1016       if (pd->content[0])
1017         repodata_set_sourcepkg(pd->data, handle, pd->content);
1018       break;
1019     case STATE_RELNOTESURL:
1020       if (pd->content[0])
1021         {
1022           repodata_add_poolstr_array(pd->data, pd->handle, PRODUCT_URL, pd->content);
1023           repodata_add_idarray(pd->data, pd->handle, PRODUCT_URL_TYPE, pool_str2id(pool, "releasenotes", 1));
1024         }
1025       break;
1026     case STATE_UPDATEURL:
1027       if (pd->content[0])
1028         {
1029           repodata_add_poolstr_array(pd->data, pd->handle, PRODUCT_URL, pd->content);
1030           repodata_add_idarray(pd->data, pd->handle, PRODUCT_URL_TYPE, pool_str2id(pool, "update", 1));
1031         }
1032       break;
1033     case STATE_OPTIONALURL:
1034       if (pd->content[0])
1035         {
1036           repodata_add_poolstr_array(pd->data, pd->handle, PRODUCT_URL, pd->content);
1037           repodata_add_idarray(pd->data, pd->handle, PRODUCT_URL_TYPE, pool_str2id(pool, "optional", 1));
1038         }
1039       break;
1040     case STATE_FLAG:
1041       if (pd->content[0])
1042           repodata_set_poolstr(pd->data, handle, PRODUCT_FLAGS, pd->content);
1043       break;
1044     case STATE_EULA:
1045       if (pd->content[0])
1046         repodata_set_str(pd->data, handle, langtag(pd, SOLVABLE_EULA, pd->tmplang), pd->content);
1047       break;
1048     case STATE_KEYWORD:
1049       if (pd->content[0])
1050         repodata_add_poolstr_array(pd->data, pd->handle, SOLVABLE_KEYWORDS, pd->content);
1051       break;
1052     case STATE_DISKUSAGE:
1053       if (pd->ndirs)
1054         commit_diskusage(pd, pd->handle);
1055       break;
1056     case STATE_ORDER:
1057       if (pd->content[0])
1058         repodata_set_str(pd->data, pd->handle, SOLVABLE_ORDER, pd->content);
1059       break;
1060     case STATE_CHANGELOG:
1061       repodata_set_str(pd->data, pd->changelog_handle, SOLVABLE_CHANGELOG_TEXT, pd->content);
1062       repodata_add_flexarray(pd->data, pd->handle, SOLVABLE_CHANGELOG, pd->changelog_handle);
1063       pd->changelog_handle = 0;
1064       break;
1065     default:
1066       break;
1067     }
1068   pd->state = pd->sbtab[pd->state];
1069   pd->docontent = 0;
1070   /* fprintf(stderr, "back from known %d %d %d\n", pd->state, pd->depth, pd->statedepth); */
1071 }
1072
1073
1074 /*
1075  * characterData
1076  * XML callback
1077  *
1078  */
1079
1080 static void XMLCALL
1081 characterData(void *userData, const XML_Char *s, int len)
1082 {
1083   struct parsedata *pd = userData;
1084   int l;
1085   char *c;
1086
1087   if (!pd->docontent)
1088     return;
1089   l = pd->lcontent + len + 1;
1090   if (l > pd->acontent)
1091     {
1092       pd->content = solv_realloc(pd->content, l + 256);
1093       pd->acontent = l + 256;
1094     }
1095   c = pd->content + pd->lcontent;
1096   pd->lcontent += len;
1097   while (len-- > 0)
1098     *c++ = *s++;
1099   *c = 0;
1100 }
1101
1102
1103 /*-----------------------------------------------*/
1104 /* 'main' */
1105
1106 #define BUFF_SIZE 8192
1107
1108 /*
1109  * repo_add_rpmmd
1110  * parse rpm-md metadata (primary, others)
1111  *
1112  */
1113
1114 int
1115 repo_add_rpmmd(Repo *repo, FILE *fp, const char *language, int flags)
1116 {
1117   Pool *pool = repo->pool;
1118   struct parsedata pd;
1119   char buf[BUFF_SIZE];
1120   int i, l;
1121   struct stateswitch *sw;
1122   Repodata *data;
1123   unsigned int now;
1124   XML_Parser parser;
1125
1126   now = solv_timems(0);
1127   data = repo_add_repodata(repo, flags);
1128
1129   memset(&pd, 0, sizeof(pd));
1130   for (i = 0, sw = stateswitches; sw->from != NUMSTATES; i++, sw++)
1131     {
1132       if (!pd.swtab[sw->from])
1133         pd.swtab[sw->from] = sw;
1134       pd.sbtab[sw->to] = sw->from;
1135     }
1136   pd.pool = pool;
1137   pd.repo = repo;
1138   pd.data = data;
1139
1140   pd.content = solv_malloc(256);
1141   pd.acontent = 256;
1142   pd.lcontent = 0;
1143   pd.kind = 0;
1144   pd.language = language && *language && strcmp(language, "en") != 0 ? language : 0;
1145
1146   /* initialize the string pool where we will store
1147      the package checksums we know about, to get an Id
1148      we can use in a cache */
1149   stringpool_init_empty(&pd.cspool);
1150   if ((flags & REPO_EXTEND_SOLVABLES) != 0)
1151     {
1152       /* setup join data */
1153       Dataiterator di;
1154       dataiterator_init(&di, pool, repo, 0, SOLVABLE_CHECKSUM, 0, 0);
1155       while (dataiterator_step(&di))
1156         {
1157           const char *str;
1158           int index;
1159
1160           if (!solv_chksum_len(di.key->type))
1161             continue;
1162           str = repodata_chk2str(di.data, di.key->type, (const unsigned char *)di.kv.str);
1163           index = stringpool_str2id(&pd.cspool, str, 1);
1164           if (index >= pd.ncscache)
1165             {
1166               pd.cscache = solv_zextend(pd.cscache, pd.ncscache, index + 1 - pd.ncscache, sizeof(Id), 255);
1167               pd.ncscache = index + 1;
1168             }
1169           pd.cscache[index] = di.solvid;
1170         }
1171       dataiterator_free(&di);
1172     }
1173
1174   parser = XML_ParserCreate(NULL);
1175   XML_SetUserData(parser, &pd);
1176   pd.parser = &parser;
1177   XML_SetElementHandler(parser, startElement, endElement);
1178   XML_SetCharacterDataHandler(parser, characterData);
1179   for (;;)
1180     {
1181       l = fread(buf, 1, sizeof(buf), fp);
1182       if (XML_Parse(parser, buf, l, l == 0) == XML_STATUS_ERROR)
1183         {
1184           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));
1185           break;
1186         }
1187       if (l == 0)
1188         break;
1189     }
1190   XML_ParserFree(parser);
1191   solv_free(pd.content);
1192   solv_free(pd.lastdirstr);
1193   join_freemem(&pd.jd);
1194   stringpool_free(&pd.cspool);
1195   solv_free(pd.cscache);
1196   repodata_free_dircache(data);
1197
1198   if (!(flags & REPO_NO_INTERNALIZE))
1199     repodata_internalize(data);
1200   POOL_DEBUG(SOLV_DEBUG_STATS, "repo_add_rpmmd took %d ms\n", solv_timems(now));
1201   POOL_DEBUG(SOLV_DEBUG_STATS, "repo size: %d solvables\n", repo->nsolvables);
1202   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)));
1203   return pd.ret;
1204 }