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