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