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