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