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