add repodata_set_sourcepkg and solvable_lookup_sourcepkg, fix solvable_get_location...
[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   /* general */
118   NUMSTATES
119 };
120
121 struct stateswitch {
122   enum state from;
123   char *ename;
124   enum state to;
125   int docontent;
126 };
127
128 static struct stateswitch stateswitches[] = {
129   /** fake tag used to enclose 2 different xml files in one **/
130   { STATE_START,       "rpmmd",           STATE_START,    0 },
131
132   /** tags for different package data, we just ignore the tag **/
133   { STATE_START,       "metadata",        STATE_START,    0 },
134   { STATE_START,       "otherdata",       STATE_START,    0 },
135   { STATE_START,       "filelists",       STATE_START,    0 },
136   { STATE_START,       "diskusagedata",   STATE_START,    0 },
137   { STATE_START,       "susedata",        STATE_START,    0 },
138
139   { STATE_START,       "product",         STATE_SOLVABLE, 0 },
140   { STATE_START,       "pattern",         STATE_SOLVABLE, 0 },
141   { STATE_START,       "patch",           STATE_SOLVABLE, 0 },
142   { STATE_START,       "package",         STATE_SOLVABLE, 0 },
143
144   { STATE_SOLVABLE,    "name",            STATE_NAME, 1 },
145   { STATE_SOLVABLE,    "arch",            STATE_ARCH, 1 },
146   { STATE_SOLVABLE,    "version",         STATE_VERSION, 0 },
147
148   /* package attributes rpm-md */
149   { STATE_SOLVABLE,    "location",        STATE_LOCATION, 0 },
150   { STATE_SOLVABLE,    "checksum",        STATE_CHECKSUM, 1 },
151
152   /* resobject attributes */
153
154   { STATE_SOLVABLE,    "summary",         STATE_SUMMARY,      1 },
155   { STATE_SOLVABLE,    "description",     STATE_DESCRIPTION,  1 },
156   { STATE_SOLVABLE,    "distribution",    STATE_DISTRIBUTION, 1 },
157   { STATE_SOLVABLE,    "url",             STATE_URL,          1 },
158   { STATE_SOLVABLE,    "packager",        STATE_PACKAGER,     1 },
159   { STATE_SOLVABLE,    "vendor",          STATE_VENDOR,       1 },
160   { STATE_SOLVABLE,    "size",            STATE_SIZE,         0 },
161   { STATE_SOLVABLE,    "archive-size",    STATE_DOWNLOADSIZE, 1 },
162   { STATE_SOLVABLE,    "install-time",    STATE_INSTALLTIME,  1 },
163   { STATE_SOLVABLE,    "install-only",    STATE_INSTALLONLY,  1 },
164   { STATE_SOLVABLE,    "time",            STATE_TIME,         0 },
165
166   /* extended Novell/SUSE attributes (susedata.xml) */
167   { STATE_SOLVABLE,    "eula",            STATE_EULA,         1 },
168   { STATE_SOLVABLE,    "keyword",         STATE_KEYWORD,      1 },
169   { STATE_SOLVABLE,    "diskusage",       STATE_DISKUSAGE,    0 },
170
171   /* pattern attribute */
172   { STATE_SOLVABLE,    "script",          STATE_SCRIPT,        1 },
173   { STATE_SOLVABLE,    "icon",            STATE_ICON,          1 },
174   { STATE_SOLVABLE,    "uservisible",     STATE_USERVISIBLE,   1 },
175   { STATE_SOLVABLE,    "category",        STATE_CATEGORY,      1 },
176   { STATE_SOLVABLE,    "order",           STATE_ORDER,         1 },
177   { STATE_SOLVABLE,    "includes",        STATE_INCLUDES,      0 },
178   { STATE_SOLVABLE,    "extends",         STATE_EXTENDS,       0 },
179   { STATE_SOLVABLE,    "default",         STATE_DEFAULT,       1 },
180   { STATE_SOLVABLE,    "install-time",    STATE_INSTALL_TIME,  1 },
181
182   /* product attributes */
183   /* note the product type is an attribute */
184   { STATE_SOLVABLE,    "release-notes-url", STATE_RELNOTESURL, 1 },
185   { STATE_SOLVABLE,    "update-url",        STATE_UPDATEURL,   1 },
186   { STATE_SOLVABLE,    "optional-url",      STATE_OPTIONALURL, 1 },
187   { STATE_SOLVABLE,    "flag",              STATE_FLAG,        1 },
188
189   { STATE_SOLVABLE,      "rpm:vendor",      STATE_VENDOR,      1 },
190   { STATE_SOLVABLE,      "rpm:group",       STATE_RPM_GROUP,   1 },
191   { STATE_SOLVABLE,      "rpm:license",     STATE_RPM_LICENSE, 1 },
192
193   /* rpm-md dependencies */
194   { STATE_SOLVABLE,      "rpm:provides",    STATE_PROVIDES,     0 },
195   { STATE_SOLVABLE,      "rpm:requires",    STATE_REQUIRES,     0 },
196   { STATE_SOLVABLE,      "rpm:obsoletes",   STATE_OBSOLETES,    0 },
197   { STATE_SOLVABLE,      "rpm:conflicts",   STATE_CONFLICTS,    0 },
198   { STATE_SOLVABLE,      "rpm:recommends",  STATE_RECOMMENDS ,  0 },
199   { STATE_SOLVABLE,      "rpm:supplements", STATE_SUPPLEMENTS,  0 },
200   { STATE_SOLVABLE,      "rpm:suggests",    STATE_SUGGESTS,     0 },
201   { STATE_SOLVABLE,      "rpm:enhances",    STATE_ENHANCES,     0 },
202   { STATE_SOLVABLE,      "rpm:freshens",    STATE_FRESHENS,     0 },
203   { STATE_SOLVABLE,      "rpm:sourcerpm",   STATE_SOURCERPM,    1 },
204   { STATE_SOLVABLE,      "rpm:header-range", STATE_HEADERRANGE, 0 },
205   { STATE_SOLVABLE,      "file",            STATE_FILE, 1 },
206
207    /* extended Novell/SUSE diskusage attributes (susedata.xml) */
208   { STATE_DISKUSAGE,   "dirs",            STATE_DIRS,         0 },
209   { STATE_DIRS,        "dir",             STATE_DIR,          0 },
210
211   { STATE_PROVIDES,    "rpm:entry",       STATE_PROVIDESENTRY, 0 },
212   { STATE_REQUIRES,    "rpm:entry",       STATE_REQUIRESENTRY, 0 },
213   { STATE_OBSOLETES,   "rpm:entry",       STATE_OBSOLETESENTRY, 0 },
214   { STATE_CONFLICTS,   "rpm:entry",       STATE_CONFLICTSENTRY, 0 },
215   { STATE_RECOMMENDS,  "rpm:entry",       STATE_RECOMMENDSENTRY, 0 },
216   { STATE_SUPPLEMENTS, "rpm:entry",       STATE_SUPPLEMENTSENTRY, 0 },
217   { STATE_SUGGESTS,    "rpm:entry",       STATE_SUGGESTSENTRY, 0 },
218   { STATE_ENHANCES,    "rpm:entry",       STATE_ENHANCESENTRY, 0 },
219   { STATE_FRESHENS,    "rpm:entry",       STATE_FRESHENSENTRY, 0 },
220
221   { STATE_INCLUDES,    "item",            STATE_INCLUDESENTRY, 0 },
222   { STATE_EXTENDS,     "item",            STATE_EXTENDSENTRY,  0 },
223
224   { NUMSTATES}
225 };
226
227 /* maxmum initial size of
228    the checksum cache */
229 #define MAX_CSCACHE 32768
230 #define CSREALLOC_STEP 1024
231
232 struct parsedata {
233   int ret;
234   Pool *pool;
235   Repo *repo;
236   Repodata *data;
237   char *kind;
238   int depth;
239   enum state state;
240   int statedepth;
241   char *content;
242   int lcontent;
243   int acontent;
244   int docontent;
245   Solvable *solvable;
246   Offset freshens;
247   struct stateswitch *swtab[NUMSTATES];
248   enum state sbtab[NUMSTATES];
249   struct joindata jd;
250   /* temporal to store attribute tag language */
251   const char *tmplang;
252   Id chksumtype;
253   Id handle;
254   XML_Parser *parser;
255   Id (*dirs)[3]; /* dirid, size, nfiles */
256   int ndirs;
257   const char *language;                 /* default language */
258   Id langcache[ID_NUM_INTERNAL];        /* cache for the default language */
259
260   Id lastdir;
261   char *lastdirstr;
262   int lastdirstrl;
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       {
757         const char *tmp = find_attr("pattern", atts);
758         if (tmp)
759           repodata_add_poolstr_array(pd->data, pd->handle, SOLVABLE_INCLUDES, join2(&pd->jd, "pattern", ":", tmp));
760         break;
761       }
762     case STATE_EXTENDSENTRY:
763       {
764         const char *tmp = find_attr("pattern", atts);
765         if (tmp)
766           repodata_add_poolstr_array(pd->data, pd->handle, SOLVABLE_EXTENDS, join2(&pd->jd, "pattern", ":", tmp));
767         break;
768       }
769     case STATE_LOCATION:
770       str = find_attr("href", atts);
771       if (str)
772         repodata_set_location(pd->data, handle, 0, 0, str);
773       break;
774     case STATE_CHECKSUM:
775       {
776         const char *tmp = find_attr("type", atts);
777         pd->chksumtype = tmp && *tmp ? solv_chksum_str2type(tmp) : 0;
778         if (!pd->chksumtype)
779           pd->ret = pool_error(pool, -1, "line %d: unknown checksum type: %s", (unsigned int)XML_GetCurrentLineNumber(*pd->parser), tmp ? tmp: "NULL");
780         break;
781       }
782     case STATE_TIME:
783       {
784         unsigned int t;
785         str = find_attr("build", atts);
786         if (str && (t = atoi(str)) != 0)
787           repodata_set_num(pd->data, handle, SOLVABLE_BUILDTIME, t);
788         break;
789       }
790     case STATE_SIZE:
791       {
792         unsigned long long k;
793         str = find_attr("installed", atts);
794         if (str && (k = strtoull(str, 0, 10)) != 0)
795           repodata_set_num(pd->data, handle, SOLVABLE_INSTALLSIZE, k);
796         /* XXX the "package" attribute gives the size of the rpm file,
797            i.e. the download size.  Except on packman, there it seems to be
798            something else entirely, it has a value near to the other two
799            values, as if the rpm is uncompressed.  */
800         str = find_attr("package", atts);
801         if (str && (k = strtoull(str, 0, 10)) != 0)
802           repodata_set_num(pd->data, handle, SOLVABLE_DOWNLOADSIZE, k);
803         break;
804       }
805     case STATE_HEADERRANGE:
806       {
807         unsigned int end;
808         str = find_attr("end", atts);
809         if (str && (end = atoi(str)) != 0)
810           repodata_set_num(pd->data, handle, SOLVABLE_HEADEREND, end);
811         break;
812       }
813       /*
814         <diskusage>
815           <dirs>
816             <dir name="/" size="56" count="11"/>
817             <dir name="usr/" size="56" count="11"/>
818             <dir name="usr/bin/" size="38" count="10"/>
819             <dir name="usr/share/" size="18" count="1"/>
820             <dir name="usr/share/doc/" size="18" count="1"/>
821           </dirs>
822         </diskusage>
823       */
824     case STATE_DISKUSAGE:
825       {
826         /* Really, do nothing, wait for <dir> tag */
827         break;
828       }
829     case STATE_DIR:
830       {
831         long filesz = 0, filenum = 0;
832         Id dirid;
833         if ((str = find_attr("name", atts)) != 0)
834           dirid = repodata_str2dir(pd->data, str, 1);
835         else
836           {
837             pd->ret = pool_error(pool, -1, "<dir .../> tag without 'name' attribute");
838             break;
839           }
840         if ((str = find_attr("size", atts)) != 0)
841           filesz = strtol(str, 0, 0);
842         if ((str = find_attr("count", atts)) != 0)
843           filenum = strtol(str, 0, 0);
844         pd->dirs = solv_extend(pd->dirs, pd->ndirs, 1, sizeof(pd->dirs[0]), 31);
845         pd->dirs[pd->ndirs][0] = dirid;
846         pd->dirs[pd->ndirs][1] = filesz;
847         pd->dirs[pd->ndirs][2] = filenum;
848         pd->ndirs++;
849         break;
850       }
851     default:
852       break;
853     }
854 }
855
856
857 /*
858  * endElement
859  * XML callback
860  *
861  */
862
863 static void XMLCALL
864 endElement(void *userData, const char *name)
865 {
866   struct parsedata *pd = userData;
867   Pool *pool = pd->pool;
868   Solvable *s = pd->solvable;
869   Repo *repo = pd->repo;
870   Id handle = pd->handle;
871   Id id;
872   char *p;
873
874   if (pd->depth != pd->statedepth)
875     {
876       pd->depth--;
877       /* printf("back from unknown %d %d %d\n", pd->state, pd->depth, pd->statedepth); */
878       return;
879     }
880
881   /* ignore patterns & metadata */
882   if (pd->state == STATE_START && !strcmp(name, "patterns"))
883     return;
884   if (pd->state == STATE_START && !strcmp(name, "products"))
885     return;
886 #if 0
887   if (pd->state == STATE_START && !strcmp(name, "metadata"))
888     return;
889 #endif
890   if (pd->state == STATE_SOLVABLE && !strcmp(name, "format"))
891     return;
892
893   pd->depth--;
894   pd->statedepth--;
895
896
897   if (!s)
898     {
899       pd->state = pd->sbtab[pd->state];
900       pd->docontent = 0;
901       return;
902     }
903
904   switch (pd->state)
905     {
906     case STATE_SOLVABLE:
907       if (pd->kind && !s->name) /* add namespace in case of NULL name */
908         s->name = pool_str2id(pool, join2(&pd->jd, pd->kind, ":", 0), 1);
909       if (!s->arch)
910         s->arch = ARCH_NOARCH;
911       if (!s->evr)
912         s->evr = ID_EMPTY;      /* some patterns have this */
913       if (s->name && s->arch != ARCH_SRC && s->arch != ARCH_NOSRC)
914         s->provides = repo_addid_dep(repo, s->provides, pool_rel2id(pool, s->name, s->evr, REL_EQ, 1), 0);
915       s->supplements = repo_fix_supplements(repo, s->provides, s->supplements, pd->freshens);
916       s->conflicts = repo_fix_conflicts(repo, s->conflicts);
917       pd->freshens = 0;
918       pd->kind = 0;
919       pd->solvable = s = 0;
920       break;
921     case STATE_NAME:
922       if (pd->kind)
923         s->name = pool_str2id(pool, join2(&pd->jd, pd->kind, ":", pd->content), 1);
924       else
925         s->name = pool_str2id(pool, pd->content, 1);
926       break;
927     case STATE_ARCH:
928       s->arch = pool_str2id(pool, pd->content, 1);
929       break;
930     case STATE_VENDOR:
931       s->vendor = pool_str2id(pool, pd->content, 1);
932       break;
933     case STATE_RPM_GROUP:
934       repodata_set_poolstr(pd->data, handle, SOLVABLE_GROUP, pd->content);
935       break;
936     case STATE_RPM_LICENSE:
937       repodata_set_poolstr(pd->data, handle, SOLVABLE_LICENSE, pd->content);
938       break;
939     case STATE_CHECKSUM:
940       {
941         Id index;
942         
943         if (!pd->chksumtype)
944           break;
945         if (strlen(pd->content) != 2 * solv_chksum_len(pd->chksumtype))
946           {
947             pd->ret = pool_error(pool, -1, "line %d: invalid checksum length for %s", (unsigned int)XML_GetCurrentLineNumber(*pd->parser), solv_chksum_type2str(pd->chksumtype));
948             break;
949           }
950         repodata_set_checksum(pd->data, handle, SOLVABLE_CHECKSUM, pd->chksumtype, pd->content);
951         /* we save the checksum to solvable id relationship for extended
952            metadata */
953         index = stringpool_str2id(&pd->cspool, pd->content, 1 /* create it */);
954         if (index >= pd->ncscache)
955           {
956             pd->cscache = solv_zextend(pd->cscache, pd->ncscache, index + 1 - pd->ncscache, sizeof(Id), 255);
957             pd->ncscache = index + 1;
958           }
959         /* add the checksum to the cache */
960         pd->cscache[index] = s - pool->solvables;
961         break;
962       }
963     case STATE_FILE:
964 #if 0
965       id = pool_str2id(pool, pd->content, 1);
966       s->provides = repo_addid_dep(repo, s->provides, id, SOLVABLE_FILEMARKER);
967 #endif
968       if ((p = strrchr(pd->content, '/')) != 0)
969         {
970           *p++ = 0;
971           if (pd->lastdir && !strcmp(pd->lastdirstr, pd->content))
972             {
973               id = pd->lastdir;
974             }
975           else
976             {
977               int l;
978               id = repodata_str2dir(pd->data, pd->content, 1);
979               l = strlen(pd->content) + 1;
980               if (l > pd->lastdirstrl)
981                 {
982                   pd->lastdirstrl = l + 128;
983                   pd->lastdirstr = solv_realloc(pd->lastdirstr, pd->lastdirstrl);
984                 }
985               strcpy(pd->lastdirstr, pd->content);
986               pd->lastdir = id;
987             }
988         }
989       else
990         {
991           p = pd->content;
992           id = 0;
993         }
994       if (!id)
995         id = repodata_str2dir(pd->data, "/", 1);
996       repodata_add_dirstr(pd->data, handle, SOLVABLE_FILELIST, id, p);
997       break;
998     case STATE_SUMMARY:
999       repodata_set_str(pd->data, handle, langtag(pd, SOLVABLE_SUMMARY, pd->tmplang), pd->content);
1000       break;
1001     case STATE_DESCRIPTION:
1002       set_description_author(pd->data, handle, pd->content, pd);
1003       break;
1004     case STATE_CATEGORY:
1005       repodata_set_str(pd->data, handle, langtag(pd, SOLVABLE_CATEGORY, pd->tmplang), pd->content);
1006       break;
1007     case STATE_DISTRIBUTION:
1008         repodata_set_poolstr(pd->data, handle, SOLVABLE_DISTRIBUTION, pd->content);
1009         break;
1010     case STATE_URL:
1011       if (pd->content[0])
1012         repodata_set_str(pd->data, handle, SOLVABLE_URL, pd->content);
1013       break;
1014     case STATE_PACKAGER:
1015       if (pd->content[0])
1016         repodata_set_poolstr(pd->data, handle, SOLVABLE_PACKAGER, pd->content);
1017       break;
1018     case STATE_SOURCERPM:
1019       if (pd->content[0])
1020         repodata_set_sourcepkg(pd->data, handle, pd->content);
1021       break;
1022     case STATE_RELNOTESURL:
1023       if (pd->content[0])
1024         {
1025           repodata_add_poolstr_array(pd->data, pd->handle, PRODUCT_URL, pd->content);
1026           repodata_add_idarray(pd->data, pd->handle, PRODUCT_URL_TYPE, pool_str2id(pool, "releasenotes", 1));
1027         }
1028       break;
1029     case STATE_UPDATEURL:
1030       if (pd->content[0])
1031         {
1032           repodata_add_poolstr_array(pd->data, pd->handle, PRODUCT_URL, pd->content);
1033           repodata_add_idarray(pd->data, pd->handle, PRODUCT_URL_TYPE, pool_str2id(pool, "update", 1));
1034         }
1035       break;
1036     case STATE_OPTIONALURL:
1037       if (pd->content[0])
1038         {
1039           repodata_add_poolstr_array(pd->data, pd->handle, PRODUCT_URL, pd->content);
1040           repodata_add_idarray(pd->data, pd->handle, PRODUCT_URL_TYPE, pool_str2id(pool, "optional", 1));
1041         }
1042       break;
1043     case STATE_FLAG:
1044       if (pd->content[0])
1045           repodata_set_poolstr(pd->data, handle, PRODUCT_FLAGS, pd->content);
1046       break;
1047     case STATE_EULA:
1048       if (pd->content[0])
1049         repodata_set_str(pd->data, handle, langtag(pd, SOLVABLE_EULA, pd->tmplang), pd->content);
1050       break;
1051     case STATE_KEYWORD:
1052       if (pd->content[0])
1053         repodata_add_poolstr_array(pd->data, pd->handle, SOLVABLE_KEYWORDS, pd->content);
1054       break;
1055     case STATE_DISKUSAGE:
1056       if (pd->ndirs)
1057         commit_diskusage(pd, pd->handle);
1058       break;
1059     case STATE_ORDER:
1060       if (pd->content[0])
1061         repodata_set_str(pd->data, pd->handle, SOLVABLE_ORDER, pd->content);
1062     default:
1063       break;
1064     }
1065   pd->state = pd->sbtab[pd->state];
1066   pd->docontent = 0;
1067   /* fprintf(stderr, "back from known %d %d %d\n", pd->state, pd->depth, pd->statedepth); */
1068 }
1069
1070
1071 /*
1072  * characterData
1073  * XML callback
1074  *
1075  */
1076
1077 static void XMLCALL
1078 characterData(void *userData, const XML_Char *s, int len)
1079 {
1080   struct parsedata *pd = userData;
1081   int l;
1082   char *c;
1083
1084   if (!pd->docontent)
1085     return;
1086   l = pd->lcontent + len + 1;
1087   if (l > pd->acontent)
1088     {
1089       pd->content = solv_realloc(pd->content, l + 256);
1090       pd->acontent = l + 256;
1091     }
1092   c = pd->content + pd->lcontent;
1093   pd->lcontent += len;
1094   while (len-- > 0)
1095     *c++ = *s++;
1096   *c = 0;
1097 }
1098
1099
1100 /*-----------------------------------------------*/
1101 /* 'main' */
1102
1103 #define BUFF_SIZE 8192
1104
1105 /*
1106  * repo_add_rpmmd
1107  * parse rpm-md metadata (primary, others)
1108  *
1109  */
1110
1111 int
1112 repo_add_rpmmd(Repo *repo, FILE *fp, const char *language, int flags)
1113 {
1114   Pool *pool = repo->pool;
1115   struct parsedata pd;
1116   char buf[BUFF_SIZE];
1117   int i, l;
1118   struct stateswitch *sw;
1119   Repodata *data;
1120   unsigned int now;
1121   XML_Parser parser;
1122
1123   now = solv_timems(0);
1124   data = repo_add_repodata(repo, flags);
1125
1126   memset(&pd, 0, sizeof(pd));
1127   for (i = 0, sw = stateswitches; sw->from != NUMSTATES; i++, sw++)
1128     {
1129       if (!pd.swtab[sw->from])
1130         pd.swtab[sw->from] = sw;
1131       pd.sbtab[sw->to] = sw->from;
1132     }
1133   pd.pool = pool;
1134   pd.repo = repo;
1135   pd.data = data;
1136
1137   pd.content = solv_malloc(256);
1138   pd.acontent = 256;
1139   pd.lcontent = 0;
1140   pd.kind = 0;
1141   pd.language = language && *language && strcmp(language, "en") != 0 ? language : 0;
1142
1143   /* initialize the string pool where we will store
1144      the package checksums we know about, to get an Id
1145      we can use in a cache */
1146   stringpool_init_empty(&pd.cspool);
1147   if ((flags & REPO_EXTEND_SOLVABLES) != 0)
1148     {
1149       /* setup join data */
1150       Dataiterator di;
1151       dataiterator_init(&di, pool, repo, 0, SOLVABLE_CHECKSUM, 0, 0);
1152       while (dataiterator_step(&di))
1153         {
1154           const char *str;
1155           int index;
1156
1157           if (!solv_chksum_len(di.key->type))
1158             continue;
1159           str = repodata_chk2str(di.data, di.key->type, (const unsigned char *)di.kv.str);
1160           index = stringpool_str2id(&pd.cspool, str, 1);
1161           if (index >= pd.ncscache)
1162             {
1163               pd.cscache = solv_zextend(pd.cscache, pd.ncscache, index + 1 - pd.ncscache, sizeof(Id), 255);
1164               pd.ncscache = index + 1;
1165             }
1166           pd.cscache[index] = di.solvid;
1167         }
1168       dataiterator_free(&di);
1169     }
1170
1171   parser = XML_ParserCreate(NULL);
1172   XML_SetUserData(parser, &pd);
1173   pd.parser = &parser;
1174   XML_SetElementHandler(parser, startElement, endElement);
1175   XML_SetCharacterDataHandler(parser, characterData);
1176   for (;;)
1177     {
1178       l = fread(buf, 1, sizeof(buf), fp);
1179       if (XML_Parse(parser, buf, l, l == 0) == XML_STATUS_ERROR)
1180         {
1181           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));
1182           break;
1183         }
1184       if (l == 0)
1185         break;
1186     }
1187   XML_ParserFree(parser);
1188   solv_free(pd.content);
1189   solv_free(pd.lastdirstr);
1190   join_freemem(&pd.jd);
1191   stringpool_free(&pd.cspool);
1192   solv_free(pd.cscache);
1193   repodata_free_dircache(data);
1194
1195   if (!(flags & REPO_NO_INTERNALIZE))
1196     repodata_internalize(data);
1197   POOL_DEBUG(SOLV_DEBUG_STATS, "repo_add_rpmmd took %d ms\n", solv_timems(now));
1198   POOL_DEBUG(SOLV_DEBUG_STATS, "repo size: %d solvables\n", repo->nsolvables);
1199   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)));
1200   return pd.ret;
1201 }