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