Imported Upstream version 0.6.23
[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   int extending;                        /* are we extending an existing solvable? */
262   int first;                            /* first solvable we added */
263   int cshash_filled;                    /* hash is filled with data */
264
265   Hashtable cshash;                     /* checksum hash -> offset into csdata */
266   Hashval cshashm;                      /* hash mask */
267   int ncshash;                          /* entries used */
268   unsigned char *csdata;                /* [len, checksum, id] */
269   int ncsdata;                          /* used bytes */
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 && (!*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, 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       id = pool_str2id(pool, pd->content, 1);
505     }
506 #ifdef ENABLE_COMPLEX_DEPS
507   else if (!f && n[0] == '(')
508     {
509       id = pool_parserpmrichdep(pool, n);
510       if (!id)
511         return olddeps;
512     }
513 #endif
514   else
515     id = pool_str2id(pool, (char *)n, 1);
516   if (f)
517     {
518       Id evr = makeevr_atts(pool, pd, atts);
519       int flags;
520       for (flags = 0; flags < 6; flags++)
521         if (!strcmp(f, flagtab[flags]))
522           break;
523       flags = flags < 6 ? flags + 1 : 0;
524       id = pool_rel2id(pool, id, evr, flags, 1);
525     }
526 #if 0
527   fprintf(stderr, "new dep %s\n", pool_dep2str(pool, id));
528 #endif
529   return repo_addid_dep(pd->repo, olddeps, id, marker);
530 }
531
532
533 /*
534  * set_description_author
535  *
536  */
537 static void
538 set_description_author(Repodata *data, Id handle, char *str, struct parsedata *pd)
539 {
540   char *aut, *p;
541
542   if (!str || !*str)
543     return;
544   for (aut = str; (aut = strchr(aut, '\n')) != 0; aut++)
545     if (!strncmp(aut, "\nAuthors:\n--------\n", 19))
546       break;
547   if (aut)
548     {
549       /* oh my, found SUSE special author section */
550       int l = aut - str;
551       str[l] = 0;
552       while (l > 0 && str[l - 1] == '\n')
553         str[--l] = 0;
554       if (l)
555         repodata_set_str(data, handle, langtag(pd, SOLVABLE_DESCRIPTION, pd->tmplang), str);
556       p = aut + 19;
557       aut = str;        /* copy over */
558       while (*p == ' ' || *p == '\n')
559         p++;
560       while (*p)
561         {
562           if (*p == '\n')
563             {
564               *aut++ = *p++;
565               while (*p == ' ')
566                 p++;
567               continue;
568             }
569           *aut++ = *p++;
570         }
571       while (aut != str && aut[-1] == '\n')
572         aut--;
573       *aut = 0;
574       if (*str)
575         repodata_set_str(data, handle, SOLVABLE_AUTHORS, str);
576     }
577   else if (*str)
578     repodata_set_str(data, handle, langtag(pd, SOLVABLE_DESCRIPTION, pd->tmplang), str);
579 }
580
581
582 /*-----------------------------------------------*/
583 /* checksum hash functions
584  *
585  * used to look up a solvable with the checksum for solvable extension purposes.
586  *
587  */
588
589 static void
590 init_cshash(struct parsedata *pd)
591 {
592 }
593
594 static void
595 free_cshash(struct parsedata *pd)
596 {
597   pd->cshash = solv_free(pd->cshash);
598   pd->ncshash = 0;
599   pd->cshashm = 0;
600   pd->csdata = solv_free(pd->csdata);
601   pd->ncsdata = 0;
602 }
603
604 static inline Hashval
605 hashkey(const unsigned char *key, int keyl)
606 {
607   return key[0] << 24 | key[1] << 16 | key[2] << 8 | key[3];
608 }
609
610 static void
611 rebuild_cshash(struct parsedata *pd)
612 {
613   Hashval h, hh, hm;
614   Hashtable ht;
615   unsigned char *d, *de;
616
617   hm = pd->cshashm;
618 #if 0
619   fprintf(stderr, "rebuild cshash with mask 0x%x\n", hm);
620 #endif
621   solv_free(pd->cshash);
622   ht = pd->cshash = (Hashtable)solv_calloc(hm + 1, sizeof(Id));
623   d = pd->csdata;
624   de = d + pd->ncsdata;
625   while (d != de)
626     {
627       h = hashkey(d + 1, d[0] + 1) & hm;
628       hh = HASHCHAIN_START;
629       while (ht[h])
630         h = HASHCHAIN_NEXT(h, hh, hm);
631       ht[h] = d + 1 - pd->csdata;
632       d += 2 + d[0] + sizeof(Id);
633     }
634 }
635
636 static void
637 put_in_cshash(struct parsedata *pd, const unsigned char *key, int keyl, Id id)
638 {
639   Hashtable ht;
640   Hashval h, hh, hm;
641   unsigned char *d;
642
643   if (keyl < 4 || keyl > 256)
644     return;
645   ht = pd->cshash;
646   hm = pd->cshashm;
647   h = hashkey(key, keyl) & hm;
648   hh = HASHCHAIN_START;
649   if (ht)
650     {
651       while (ht[h])
652         {
653           unsigned char *d = pd->csdata + ht[h];
654           if (d[-1] == keyl - 1 && !memcmp(key, d, keyl))
655             return;             /* XXX: first id wins... */
656           h = HASHCHAIN_NEXT(h, hh, hm);
657         }
658     }
659   /* a new entry. put in csdata */
660   pd->csdata = solv_extend(pd->csdata, pd->ncsdata, 1 + keyl + sizeof(Id), 1, 4095);
661   d = pd->csdata + pd->ncsdata;
662   d[0] = keyl - 1;
663   memcpy(d + 1, key, keyl);
664   memcpy(d + 1 + keyl, &id, sizeof(Id));
665   pd->ncsdata += 1 + keyl + sizeof(Id);
666   if ((Hashval)++pd->ncshash * 2 > hm)
667     {
668       pd->cshashm = pd->cshashm ? (2 * pd->cshashm + 1) : 4095;
669       rebuild_cshash(pd);
670     }
671   else
672     ht[h] = pd->ncsdata - (keyl + sizeof(Id));
673 }
674
675 static Id
676 lookup_cshash(struct parsedata *pd, const unsigned char *key, int keyl)
677 {
678   Hashtable ht;
679   Hashval h, hh, hm;
680
681   if (keyl < 4 || keyl > 256)
682     return 0;
683   ht = pd->cshash;
684   if (!ht)
685     return 0;
686   hm = pd->cshashm;
687   h = hashkey(key, keyl) & hm;
688   hh = HASHCHAIN_START;
689   while (ht[h])
690     {
691       unsigned char *d = pd->csdata + ht[h];
692       if (d[-1] == keyl - 1 && !memcmp(key, d, keyl))
693         {
694           Id id;
695           memcpy(&id, d + keyl, sizeof(Id));
696           return id;
697         }
698       h = HASHCHAIN_NEXT(h, hh, hm);
699     }
700   return 0;
701 }
702
703 static void
704 fill_cshash_from_repo(struct parsedata *pd)
705 {
706   Dataiterator di;
707   /* setup join data */
708   dataiterator_init(&di, pd->pool, pd->repo, 0, SOLVABLE_CHECKSUM, 0, 0);
709   while (dataiterator_step(&di))
710     put_in_cshash(pd, (const unsigned char *)di.kv.str, solv_chksum_len(di.key->type), di.solvid);
711   dataiterator_free(&di);
712 }
713
714 static void
715 fill_cshash_from_new_solvables(struct parsedata *pd)
716 {
717   Pool *pool = pd->pool;
718   Id cstype = 0;
719   unsigned const char *cs;
720   int i;
721
722   for (i = pd->first; i < pool->nsolvables; i++)
723     {
724       if (pool->solvables[i].repo != pd->repo)
725         continue;
726       cs = repodata_lookup_bin_checksum_uninternalized(pd->data, i, SOLVABLE_CHECKSUM, &cstype);
727       if (cs)
728         put_in_cshash(pd, cs, solv_chksum_len(cstype), i);
729     }
730 }
731
732 /*-----------------------------------------------*/
733 /* XML callbacks */
734
735 /*
736  * startElement
737  * XML callback
738  *
739  */
740
741 static void XMLCALL
742 startElement(void *userData, const char *name, const char **atts)
743 {
744   struct parsedata *pd = userData;
745   Pool *pool = pd->pool;
746   Solvable *s = pd->solvable;
747   struct stateswitch *sw;
748   const char *str;
749   Id handle = pd->handle;
750   const char *pkgid;
751
752   /* fprintf(stderr, "into %s, from %d, depth %d, statedepth %d\n", name, pd->state, pd->depth, pd->statedepth); */
753
754   if (pd->depth != pd->statedepth)
755     {
756       pd->depth++;
757       return;
758     }
759
760   if (pd->state == STATE_START && !strcmp(name, "patterns"))
761     return;
762   if (pd->state == STATE_START && !strcmp(name, "products"))
763     return;
764 #if 0
765   if (pd->state == STATE_START && !strcmp(name, "metadata"))
766     return;
767 #endif
768   if (pd->state == STATE_SOLVABLE && !strcmp(name, "format"))
769     return;
770
771   pd->depth++;
772   if (!pd->swtab[pd->state])
773     return;
774   for (sw = pd->swtab[pd->state]; sw->from == pd->state; sw++)
775     if (!strcmp(sw->ename, name))
776       break;
777   if (sw->from != pd->state)
778     {
779 #if 0
780       fprintf(stderr, "into unknown: %s\n", name);
781 #endif
782       return;
783     }
784   pd->state = sw->to;
785   pd->docontent = sw->docontent;
786   pd->statedepth = pd->depth;
787   pd->lcontent = 0;
788   *pd->content = 0;
789
790   if (!s && pd->state != STATE_SOLVABLE)
791     return;
792
793   switch(pd->state)
794     {
795     case STATE_SOLVABLE:
796       pd->kind = 0;
797       if (name[2] == 't' && name[3] == 't')
798         pd->kind = "pattern";
799       else if (name[1] == 'r')
800         pd->kind = "product";
801       else if (name[2] == 't' && name[3] == 'c')
802         pd->kind = "patch";
803
804       /* to support extension metadata files like others.xml which
805          have the following structure:
806
807          <otherdata xmlns="http://linux.duke.edu/metadata/other"
808                     packages="101">
809            <package pkgid="b78f8664cd90efe42e09a345e272997ef1b53c18"
810                     name="zaptel-kmp-default"
811                     arch="i586"><version epoch="0"
812                     ver="1.2.10_2.6.22_rc4_git6_2" rel="70"/>
813               ...
814
815          we need to check if the pkgid is there and if it matches
816          an already seen package, that means we don't need to create
817          a new solvable but just append the attributes to the existing
818          one.
819       */
820       pd->extending = 0;
821       if ((pkgid = find_attr("pkgid", atts)) != NULL)
822         {
823           unsigned char chk[256];
824           int l;
825           const char *str = pkgid;
826           if (!pd->cshash_filled)
827             {
828               pd->cshash_filled = 1;
829               fill_cshash_from_new_solvables(pd);
830             }
831           handle = 0;
832           /* convert into bin checksum */
833           l = solv_hex2bin(&str, chk, sizeof(chk));
834           /* look at the checksum cache */
835           if (l >= 4 && !pkgid[2 * l])
836             handle = lookup_cshash(pd, chk, l);
837 #if 0
838           fprintf(stderr, "Lookup %s -> %d\n", pkgid, handle);
839 #endif
840           if (!handle)
841             {
842               pool_debug(pool, SOLV_WARN, "the repository specifies extra information about package with checksum '%s', which does not exist in the repository.\n", pkgid);
843               pd->handle = 0;
844               pd->solvable = 0;
845               break;
846             }
847           pd->extending = 1;
848         }
849       else
850         {
851           /* this is a new package */
852           handle = repo_add_solvable(pd->repo);
853           if (!pd->first)
854             pd->first = handle;
855           pd->freshens = 0;
856         }
857       pd->handle = handle;
858       pd->solvable = pool_id2solvable(pool, handle);
859       if (pd->kind && pd->kind[1] == 'r')
860         {
861           /* products can have a type */
862           const char *type = find_attr("type", atts);
863           if (type && *type)
864             repodata_set_str(pd->data, handle, PRODUCT_TYPE, type);
865         }
866 #if 0
867       fprintf(stderr, "package #%d\n", pd->solvable - pool->solvables);
868 #endif
869
870       break;
871     case STATE_VERSION:
872       if (pd->extending && s->evr)
873         break;          /* ignore version tag repetition in extend data */
874       s->evr = makeevr_atts(pool, pd, atts);
875       break;
876     case STATE_PROVIDES:
877       s->provides = 0;
878       break;
879     case STATE_PROVIDESENTRY:
880       s->provides = adddep(pool, pd, s->provides, atts, 0);
881       break;
882     case STATE_REQUIRES:
883       s->requires = 0;
884       break;
885     case STATE_REQUIRESENTRY:
886       s->requires = adddep(pool, pd, s->requires, atts, 1);
887       break;
888     case STATE_OBSOLETES:
889       s->obsoletes = 0;
890       break;
891     case STATE_OBSOLETESENTRY:
892       s->obsoletes = adddep(pool, pd, s->obsoletes, atts, 0);
893       break;
894     case STATE_CONFLICTS:
895       s->conflicts = 0;
896       break;
897     case STATE_CONFLICTSENTRY:
898       s->conflicts = adddep(pool, pd, s->conflicts, atts, 0);
899       break;
900     case STATE_RECOMMENDS:
901       s->recommends = 0;
902       break;
903     case STATE_RECOMMENDSENTRY:
904       s->recommends = adddep(pool, pd, s->recommends, atts, 0);
905       break;
906     case STATE_SUPPLEMENTS:
907       s->supplements= 0;
908       break;
909     case STATE_SUPPLEMENTSENTRY:
910       s->supplements = adddep(pool, pd, s->supplements, atts, 0);
911       break;
912     case STATE_SUGGESTS:
913       s->suggests = 0;
914       break;
915     case STATE_SUGGESTSENTRY:
916       s->suggests = adddep(pool, pd, s->suggests, atts, 0);
917       break;
918     case STATE_ENHANCES:
919       s->enhances = 0;
920       break;
921     case STATE_ENHANCESENTRY:
922       s->enhances = adddep(pool, pd, s->enhances, atts, 0);
923       break;
924     case STATE_FRESHENS:
925       pd->freshens = 0;
926       break;
927     case STATE_FRESHENSENTRY:
928       pd->freshens = adddep(pool, pd, pd->freshens, atts, 0);
929       break;
930     case STATE_EULA:
931     case STATE_SUMMARY:
932     case STATE_CATEGORY:
933     case STATE_DESCRIPTION:
934       pd->tmplang = join_dup(&pd->jd, find_attr("lang", atts));
935       break;
936     case STATE_USERVISIBLE:
937       repodata_set_void(pd->data, handle, SOLVABLE_ISVISIBLE);
938       break;
939     case STATE_INCLUDESENTRY:
940       str = find_attr("pattern", atts);
941       if (str)
942         repodata_add_poolstr_array(pd->data, handle, SOLVABLE_INCLUDES, join2(&pd->jd, "pattern", ":", str));
943       break;
944     case STATE_EXTENDSENTRY:
945       str = find_attr("pattern", atts);
946       if (str)
947         repodata_add_poolstr_array(pd->data, handle, SOLVABLE_EXTENDS, join2(&pd->jd, "pattern", ":", str));
948       break;
949     case STATE_LOCATION:
950       str = find_attr("href", atts);
951       if (str)
952         {
953           int medianr = 0;
954           const char *base = find_attr("xml:base", atts);
955           if (base  && !strncmp(base, "media:", 6))
956             {
957               /* check for the media number in the fragment */
958               int l = strlen(base);
959               while (l && base[l - 1] >= '0' && base[l - 1] <= '9')
960                 l--;
961               if (l && base[l - 1] == '#' && base[l])
962                 medianr = atoi(base + l);
963             }
964           repodata_set_location(pd->data, handle, medianr, 0, str);
965           if (base)
966             repodata_set_poolstr(pd->data, handle, SOLVABLE_MEDIABASE, base);
967         }
968       break;
969     case STATE_CHECKSUM:
970       str = find_attr("type", atts);
971       pd->chksumtype = str && *str ? solv_chksum_str2type(str) : 0;
972       if (!pd->chksumtype)
973         pd->ret = pool_error(pool, -1, "line %d: unknown checksum type: %s", (unsigned int)XML_GetCurrentLineNumber(*pd->parser), str ? str : "NULL");
974       break;
975     case STATE_TIME:
976       {
977         unsigned int t;
978         str = find_attr("build", atts);
979         if (str && (t = atoi(str)) != 0)
980           repodata_set_num(pd->data, handle, SOLVABLE_BUILDTIME, t);
981         break;
982       }
983     case STATE_SIZE:
984       if ((str = find_attr("installed", atts)) != 0)
985         repodata_set_num(pd->data, handle, SOLVABLE_INSTALLSIZE, strtoull(str, 0, 10));
986       if ((str = find_attr("package", atts)) != 0)
987         repodata_set_num(pd->data, handle, SOLVABLE_DOWNLOADSIZE, strtoull(str, 0, 10));
988       break;
989     case STATE_HEADERRANGE:
990       {
991         unsigned int end;
992         str = find_attr("end", atts);
993         if (str && (end = atoi(str)) != 0)
994           repodata_set_num(pd->data, handle, SOLVABLE_HEADEREND, end);
995         break;
996       }
997       /*
998         <diskusage>
999           <dirs>
1000             <dir name="/" size="56" count="11"/>
1001             <dir name="usr/" size="56" count="11"/>
1002             <dir name="usr/bin/" size="38" count="10"/>
1003             <dir name="usr/share/" size="18" count="1"/>
1004             <dir name="usr/share/doc/" size="18" count="1"/>
1005           </dirs>
1006         </diskusage>
1007       */
1008     case STATE_DISKUSAGE:
1009       {
1010         /* Really, do nothing, wait for <dir> tag */
1011         break;
1012       }
1013     case STATE_DIR:
1014       {
1015         long filesz = 0, filenum = 0;
1016         Id dirid;
1017         if ((str = find_attr("name", atts)) == 0)
1018           {
1019             pd->ret = pool_error(pool, -1, "<dir .../> tag without 'name' attribute");
1020             break;
1021           }
1022         if (*str != '/')
1023           {
1024             int l = strlen(str) + 2;
1025             if (l > pd->acontent)
1026               {
1027                 pd->content = solv_realloc(pd->content, l + 256);
1028                 pd->acontent = l + 256;
1029               }
1030             *pd->content = '/';
1031             strcpy(pd->content + 1, str);
1032             str = pd->content;
1033           }
1034         dirid = repodata_str2dir(pd->data, str, 1);
1035         if ((str = find_attr("size", atts)) != 0)
1036           filesz = strtol(str, 0, 0);
1037         if ((str = find_attr("count", atts)) != 0)
1038           filenum = strtol(str, 0, 0);
1039         pd->dirs = solv_extend(pd->dirs, pd->ndirs, 1, sizeof(pd->dirs[0]), 31);
1040         pd->dirs[pd->ndirs][0] = dirid;
1041         pd->dirs[pd->ndirs][1] = filesz;
1042         pd->dirs[pd->ndirs][2] = filenum;
1043         pd->ndirs++;
1044         break;
1045       }
1046     case STATE_CHANGELOG:
1047       pd->changelog_handle = repodata_new_handle(pd->data);
1048       if ((str = find_attr("date", atts)) != 0)
1049         repodata_set_num(pd->data, pd->changelog_handle, SOLVABLE_CHANGELOG_TIME, strtoull(str, 0, 10));
1050       if ((str = find_attr("author", atts)) != 0)
1051         repodata_set_str(pd->data, pd->changelog_handle, SOLVABLE_CHANGELOG_AUTHOR, str);
1052       break;
1053     default:
1054       break;
1055     }
1056 }
1057
1058
1059 /*
1060  * endElement
1061  * XML callback
1062  *
1063  */
1064
1065 static void XMLCALL
1066 endElement(void *userData, const char *name)
1067 {
1068   struct parsedata *pd = userData;
1069   Pool *pool = pd->pool;
1070   Solvable *s = pd->solvable;
1071   Repo *repo = pd->repo;
1072   Id handle = pd->handle;
1073   Id id;
1074   char *p;
1075
1076   if (pd->depth != pd->statedepth)
1077     {
1078       pd->depth--;
1079       /* printf("back from unknown %d %d %d\n", pd->state, pd->depth, pd->statedepth); */
1080       return;
1081     }
1082
1083   /* ignore patterns & metadata */
1084   if (pd->state == STATE_START && !strcmp(name, "patterns"))
1085     return;
1086   if (pd->state == STATE_START && !strcmp(name, "products"))
1087     return;
1088 #if 0
1089   if (pd->state == STATE_START && !strcmp(name, "metadata"))
1090     return;
1091 #endif
1092   if (pd->state == STATE_SOLVABLE && !strcmp(name, "format"))
1093     return;
1094
1095   pd->depth--;
1096   pd->statedepth--;
1097
1098
1099   if (!s)
1100     {
1101       pd->state = pd->sbtab[pd->state];
1102       pd->docontent = 0;
1103       return;
1104     }
1105
1106   switch (pd->state)
1107     {
1108     case STATE_SOLVABLE:
1109       if (pd->extending)
1110         {
1111           pd->solvable = 0;
1112           break;
1113         }
1114       if (pd->kind && !s->name) /* add namespace in case of NULL name */
1115         s->name = pool_str2id(pool, join2(&pd->jd, pd->kind, ":", 0), 1);
1116       if (!s->arch)
1117         s->arch = ARCH_NOARCH;
1118       if (!s->evr)
1119         s->evr = ID_EMPTY;      /* some patterns have this */
1120       if (s->name && s->arch != ARCH_SRC && s->arch != ARCH_NOSRC)
1121         s->provides = repo_addid_dep(repo, s->provides, pool_rel2id(pool, s->name, s->evr, REL_EQ, 1), 0);
1122       repo_rewrite_suse_deps(s, pd->freshens);
1123       pd->freshens = 0;
1124       pd->kind = 0;
1125       pd->solvable = 0;
1126       break;
1127     case STATE_NAME:
1128       if (pd->kind)
1129         s->name = pool_str2id(pool, join2(&pd->jd, pd->kind, ":", pd->content), 1);
1130       else
1131         s->name = pool_str2id(pool, pd->content, 1);
1132       break;
1133     case STATE_ARCH:
1134       s->arch = pool_str2id(pool, pd->content, 1);
1135       break;
1136     case STATE_VENDOR:
1137       s->vendor = pool_str2id(pool, pd->content, 1);
1138       break;
1139     case STATE_RPM_GROUP:
1140       repodata_set_poolstr(pd->data, handle, SOLVABLE_GROUP, pd->content);
1141       break;
1142     case STATE_RPM_LICENSE:
1143       repodata_set_poolstr(pd->data, handle, SOLVABLE_LICENSE, pd->content);
1144       break;
1145     case STATE_CHECKSUM:
1146       {
1147         unsigned char chk[256];
1148         int l = solv_chksum_len(pd->chksumtype);
1149         const char *str = pd->content;
1150         if (!l || l > sizeof(chk))
1151           break;
1152         if (solv_hex2bin(&str, chk, l) != l || pd->content[2 * l])
1153           {
1154             pd->ret = pool_error(pool, -1, "line %u: invalid %s checksum", (unsigned int)XML_GetCurrentLineNumber(*pd->parser), solv_chksum_type2str(pd->chksumtype));
1155             break;
1156           }
1157         repodata_set_bin_checksum(pd->data, handle, SOLVABLE_CHECKSUM, pd->chksumtype, chk);
1158         /* we save the checksum to solvable id relationship for extending metadata */
1159         if (pd->cshash_filled)
1160           put_in_cshash(pd, chk, l, s - pool->solvables);
1161         break;
1162       }
1163     case STATE_FILE:
1164 #if 0
1165       id = pool_str2id(pool, pd->content, 1);
1166       s->provides = repo_addid_dep(repo, s->provides, id, SOLVABLE_FILEMARKER);
1167 #endif
1168       if ((p = strrchr(pd->content, '/')) != 0)
1169         {
1170           *p++ = 0;
1171           if (pd->lastdir && !strcmp(pd->lastdirstr, pd->content))
1172             {
1173               id = pd->lastdir;
1174             }
1175           else
1176             {
1177               int l;
1178               id = repodata_str2dir(pd->data, pd->content, 1);
1179               l = strlen(pd->content) + 1;
1180               if (l > pd->lastdirstrl)
1181                 {
1182                   pd->lastdirstrl = l + 128;
1183                   pd->lastdirstr = solv_realloc(pd->lastdirstr, pd->lastdirstrl);
1184                 }
1185               strcpy(pd->lastdirstr, pd->content);
1186               pd->lastdir = id;
1187             }
1188         }
1189       else
1190         {
1191           p = pd->content;
1192           id = 0;
1193         }
1194       if (!id)
1195         id = repodata_str2dir(pd->data, "/", 1);
1196       repodata_add_dirstr(pd->data, handle, SOLVABLE_FILELIST, id, p);
1197       break;
1198     case STATE_SUMMARY:
1199       repodata_set_str(pd->data, handle, langtag(pd, SOLVABLE_SUMMARY, pd->tmplang), pd->content);
1200       break;
1201     case STATE_DESCRIPTION:
1202       set_description_author(pd->data, handle, pd->content, pd);
1203       break;
1204     case STATE_CATEGORY:
1205       repodata_set_str(pd->data, handle, langtag(pd, SOLVABLE_CATEGORY, pd->tmplang), pd->content);
1206       break;
1207     case STATE_DISTRIBUTION:
1208         repodata_set_poolstr(pd->data, handle, SOLVABLE_DISTRIBUTION, pd->content);
1209         break;
1210     case STATE_URL:
1211       if (pd->content[0])
1212         repodata_set_str(pd->data, handle, SOLVABLE_URL, pd->content);
1213       break;
1214     case STATE_PACKAGER:
1215       if (pd->content[0])
1216         repodata_set_poolstr(pd->data, handle, SOLVABLE_PACKAGER, pd->content);
1217       break;
1218     case STATE_SOURCERPM:
1219       if (pd->content[0])
1220         repodata_set_sourcepkg(pd->data, handle, pd->content);
1221       break;
1222     case STATE_RELNOTESURL:
1223       if (pd->content[0])
1224         {
1225           repodata_add_poolstr_array(pd->data, handle, PRODUCT_URL, pd->content);
1226           repodata_add_idarray(pd->data, handle, PRODUCT_URL_TYPE, pool_str2id(pool, "releasenotes", 1));
1227         }
1228       break;
1229     case STATE_UPDATEURL:
1230       if (pd->content[0])
1231         {
1232           repodata_add_poolstr_array(pd->data, handle, PRODUCT_URL, pd->content);
1233           repodata_add_idarray(pd->data, handle, PRODUCT_URL_TYPE, pool_str2id(pool, "update", 1));
1234         }
1235       break;
1236     case STATE_OPTIONALURL:
1237       if (pd->content[0])
1238         {
1239           repodata_add_poolstr_array(pd->data, handle, PRODUCT_URL, pd->content);
1240           repodata_add_idarray(pd->data, handle, PRODUCT_URL_TYPE, pool_str2id(pool, "optional", 1));
1241         }
1242       break;
1243     case STATE_FLAG:
1244       if (pd->content[0])
1245         repodata_add_poolstr_array(pd->data, handle, PRODUCT_FLAGS, pd->content);
1246       break;
1247     case STATE_EULA:
1248       if (pd->content[0])
1249         repodata_set_str(pd->data, handle, langtag(pd, SOLVABLE_EULA, pd->tmplang), pd->content);
1250       break;
1251     case STATE_KEYWORD:
1252       if (pd->content[0])
1253         repodata_add_poolstr_array(pd->data, handle, SOLVABLE_KEYWORDS, pd->content);
1254       break;
1255     case STATE_DISKUSAGE:
1256       if (pd->ndirs)
1257         commit_diskusage(pd, handle);
1258       break;
1259     case STATE_ORDER:
1260       if (pd->content[0])
1261         repodata_set_str(pd->data, handle, SOLVABLE_ORDER, pd->content);
1262       break;
1263     case STATE_CHANGELOG:
1264       repodata_set_str(pd->data, pd->changelog_handle, SOLVABLE_CHANGELOG_TEXT, pd->content);
1265       repodata_add_flexarray(pd->data, handle, SOLVABLE_CHANGELOG, pd->changelog_handle);
1266       pd->changelog_handle = 0;
1267       break;
1268     default:
1269       break;
1270     }
1271   pd->state = pd->sbtab[pd->state];
1272   pd->docontent = 0;
1273   /* fprintf(stderr, "back from known %d %d %d\n", pd->state, pd->depth, pd->statedepth); */
1274 }
1275
1276
1277 /*
1278  * characterData
1279  * XML callback
1280  *
1281  */
1282
1283 static void XMLCALL
1284 characterData(void *userData, const XML_Char *s, int len)
1285 {
1286   struct parsedata *pd = userData;
1287   int l;
1288   char *c;
1289
1290   if (!pd->docontent)
1291     return;
1292   l = pd->lcontent + len + 1;
1293   if (l > pd->acontent)
1294     {
1295       pd->content = solv_realloc(pd->content, l + 256);
1296       pd->acontent = l + 256;
1297     }
1298   c = pd->content + pd->lcontent;
1299   pd->lcontent += len;
1300   while (len-- > 0)
1301     *c++ = *s++;
1302   *c = 0;
1303 }
1304
1305
1306 /*-----------------------------------------------*/
1307 /* 'main' */
1308
1309 #define BUFF_SIZE 8192
1310
1311 /*
1312  * repo_add_rpmmd
1313  * parse rpm-md metadata (primary, others)
1314  *
1315  */
1316
1317 int
1318 repo_add_rpmmd(Repo *repo, FILE *fp, const char *language, int flags)
1319 {
1320   Pool *pool = repo->pool;
1321   struct parsedata pd;
1322   char buf[BUFF_SIZE];
1323   int i, l;
1324   struct stateswitch *sw;
1325   Repodata *data;
1326   unsigned int now;
1327   XML_Parser parser;
1328
1329   now = solv_timems(0);
1330   data = repo_add_repodata(repo, flags);
1331
1332   memset(&pd, 0, sizeof(pd));
1333   for (i = 0, sw = stateswitches; sw->from != NUMSTATES; i++, sw++)
1334     {
1335       if (!pd.swtab[sw->from])
1336         pd.swtab[sw->from] = sw;
1337       pd.sbtab[sw->to] = sw->from;
1338     }
1339   pd.pool = pool;
1340   pd.repo = repo;
1341   pd.data = data;
1342
1343   pd.content = solv_malloc(256);
1344   pd.acontent = 256;
1345   pd.lcontent = 0;
1346   pd.kind = 0;
1347   pd.language = language && *language && strcmp(language, "en") != 0 ? language : 0;
1348
1349   init_cshash(&pd);
1350   if ((flags & REPO_EXTEND_SOLVABLES) != 0)
1351     {
1352       /* setup join data */
1353       pd.cshash_filled = 1;
1354       fill_cshash_from_repo(&pd);
1355     }
1356
1357   parser = XML_ParserCreate(NULL);
1358   XML_SetUserData(parser, &pd);
1359   pd.parser = &parser;
1360   XML_SetElementHandler(parser, startElement, endElement);
1361   XML_SetCharacterDataHandler(parser, characterData);
1362   for (;;)
1363     {
1364       l = fread(buf, 1, sizeof(buf), fp);
1365       if (XML_Parse(parser, buf, l, l == 0) == XML_STATUS_ERROR)
1366         {
1367           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));
1368           break;
1369         }
1370       if (l == 0)
1371         break;
1372     }
1373   XML_ParserFree(parser);
1374   solv_free(pd.content);
1375   solv_free(pd.lastdirstr);
1376   join_freemem(&pd.jd);
1377   free_cshash(&pd);
1378   repodata_free_dircache(data);
1379
1380   if (!(flags & REPO_NO_INTERNALIZE))
1381     repodata_internalize(data);
1382   POOL_DEBUG(SOLV_DEBUG_STATS, "repo_add_rpmmd took %d ms\n", solv_timems(now));
1383   POOL_DEBUG(SOLV_DEBUG_STATS, "repo size: %d solvables\n", repo->nsolvables);
1384   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)));
1385   return pd.ret;
1386 }