generate architecture dependencies for the patch->package 'conflicts'
[platform/upstream/libsolv.git] / tools / repo_updateinfoxml.c
1
2 /*
3  * Copyright (c) 2007, Novell Inc.
4  *
5  * This program is licensed under the BSD license, read LICENSE.BSD
6  * for further information
7  */
8
9 #define _GNU_SOURCE
10 #include <sys/types.h>
11 #include <limits.h>
12 #include <fcntl.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <expat.h>
17
18 #include "pool.h"
19 #include "repo.h"
20 #include "repo_updateinfoxml.h"
21 #include "tools_util.h"
22
23 //#define TESTMM
24
25 /*
26  * <updates>
27  *   <update from="rel-eng@fedoraproject.org" status="stable" type="security" version="1.4">
28  *     <id>FEDORA-2007-4594</id>
29  *     <title>imlib-1.9.15-6.fc8</title>
30  *     <release>Fedora 8</release>
31  *     <issued date="2007-12-28 16:42:30"/>
32  *     <references>
33  *       <reference href="https://bugzilla.redhat.com/show_bug.cgi?id=426091" id="426091" title="CVE-2007-3568 imlib: infinite loop DoS using crafted BMP image" type="bugzilla"/>
34  *     </references>
35  *     <description>This update includes a fix for a denial-of-service issue (CVE-2007-3568) whereby an attacker who could get an imlib-using user to view a  specially-crafted BMP image could cause the user's CPU to go into an infinite loop.</description>
36  *     <pkglist>
37  *       <collection short="F8">
38  *         <name>Fedora 8</name>
39  *         <package arch="ppc64" name="imlib-debuginfo" release="6.fc8" src="http://download.fedoraproject.org/pub/fedora/linux/updates/8/ppc64/imlib-debuginfo-1.9.15-6.fc8.ppc64.rpm" version="1.9.15">
40  *           <filename>imlib-debuginfo-1.9.15-6.fc8.ppc64.rpm</filename>
41  *           <reboot_suggested>True</reboot_suggested>
42  *         </package>
43  *       </collection>
44  *     </pkglist>
45  *   </update>
46  * </updates>
47 */
48
49 enum state {
50   STATE_START,
51   STATE_UPDATES,      /* 1 */
52   STATE_UPDATE,       /* 2 */
53   STATE_ID,           /* 3 */
54   STATE_TITLE,        /* 4 */
55   STATE_RELEASE,      /* 5 */
56   STATE_ISSUED,       /* 6 */
57   STATE_REFERENCES,   /* 7 */
58   STATE_REFERENCE,    /* 8 */
59   STATE_DESCRIPTION,  /* 9 */
60   STATE_PKGLIST,     /* 10 */
61   STATE_COLLECTION,  /* 11 */
62   STATE_NAME,        /* 12 */
63   STATE_PACKAGE,     /* 13 */
64   STATE_FILENAME,    /* 14 */
65   STATE_REBOOT,      /* 15 */
66   STATE_RESTART,     /* 16 */
67   NUMSTATES
68 };
69
70 struct stateswitch {
71   enum state from;
72   char *ename;
73   enum state to;
74   int docontent;
75 };
76
77
78 /* !! must be sorted by first column !! */
79 static struct stateswitch stateswitches[] = {
80   { STATE_START,       "updates",         STATE_UPDATES,     0 },
81   { STATE_START,       "update",          STATE_UPDATE,      0 },
82   { STATE_UPDATES,     "update",          STATE_UPDATE,      0 },
83   { STATE_UPDATE,      "id",              STATE_ID,          1 },
84   { STATE_UPDATE,      "title",           STATE_TITLE,       1 },
85   { STATE_UPDATE,      "release",         STATE_RELEASE,     1 },
86   { STATE_UPDATE,      "issued",          STATE_ISSUED,      1 },
87   { STATE_UPDATE,      "description",     STATE_DESCRIPTION, 1 },
88   { STATE_UPDATE,      "references",      STATE_REFERENCES,  0 },
89   { STATE_UPDATE,      "pkglist",         STATE_PKGLIST,     0 },
90   { STATE_REFERENCES,  "reference",       STATE_REFERENCE,   0 },
91   { STATE_PKGLIST,     "collection",      STATE_COLLECTION,  0 },
92   { STATE_COLLECTION,  "name",            STATE_NAME,        1 },
93   { STATE_COLLECTION,  "package",         STATE_PACKAGE,     0 },
94   { STATE_PACKAGE,     "filename",        STATE_FILENAME,    1 },
95   { STATE_PACKAGE,     "reboot_suggested",STATE_REBOOT,      1 },
96   { STATE_PACKAGE,     "restart_suggested",STATE_RESTART,    1 },
97   { NUMSTATES }
98 };
99
100 struct parsedata {
101   int depth;
102   enum state state;
103   int statedepth;
104   char *content;
105   int lcontent;
106   int acontent;
107   int docontent;
108   Pool *pool;
109   Repo *repo;
110   Repodata *data;
111   unsigned int datanum;
112   Solvable *solvable;
113   unsigned int timestamp;
114   
115   struct stateswitch *swtab[NUMSTATES];
116   enum state sbtab[NUMSTATES];
117   char *tempstr;
118   int ltemp;
119   int atemp;
120 };
121
122 /*
123  * find attribute
124  */
125
126 /*
127 static const char *
128 find_attr(const char *txt, const char **atts)
129 {
130   for (; *atts; atts += 2)
131     {
132       if (!strcmp(*atts, txt))
133         return atts[1];
134     }
135   return 0;
136 }
137 */
138
139
140 /*
141  * create evr (as Id) from 'epoch', 'version' and 'release' attributes
142  */
143
144 static Id
145 makeevr_atts(Pool *pool, struct parsedata *pd, const char **atts)
146 {
147   const char *e, *v, *r, *v2;
148   char *c;
149   int l;
150
151   e = v = r = 0;
152   for (; *atts; atts += 2)
153     {
154       if (!strcmp(*atts, "epoch"))
155         e = atts[1];
156       else if (!strcmp(*atts, "version"))
157         v = atts[1];
158       else if (!strcmp(*atts, "release"))
159         r = atts[1];
160     }
161   if (e && !strcmp(e, "0"))
162     e = 0;
163   if (v && !e)
164     {
165       for (v2 = v; *v2 >= '0' && *v2 <= '9'; v2++)
166         ;
167       if (v2 > v && *v2 == ':')
168         e = "0";
169     }
170   l = 1;
171   if (e)
172     l += strlen(e) + 1;
173   if (v)
174     l += strlen(v);
175   if (r)
176     l += strlen(r) + 1;
177   if (l > pd->acontent)
178     {
179       pd->content = realloc(pd->content, l + 256);
180       pd->acontent = l + 256;
181     }
182   c = pd->content;
183   if (e)
184     {
185       strcpy(c, e);
186       c += strlen(c);
187       *c++ = ':';
188     }
189   if (v)
190     {
191       strcpy(c, v);
192       c += strlen(c);
193     }
194   if (r)
195     {
196       *c++ = '-';
197       strcpy(c, r);
198       c += strlen(c);
199     }
200   *c = 0;
201   if (!*pd->content)
202     return 0;
203 #if 0
204   fprintf(stderr, "evr: %s\n", pd->content);
205 #endif
206   return str2id(pool, pd->content, 1);
207 }
208
209
210
211 static void XMLCALL
212 startElement(void *userData, const char *name, const char **atts)
213 {
214   struct parsedata *pd = userData;
215   Pool *pool = pd->pool;
216   Solvable *solvable = pd->solvable;
217   struct stateswitch *sw;
218   /*const char *str; */
219
220 #if 0
221       fprintf(stderr, "start: [%d]%s\n", pd->state, name);
222 #endif
223   if (pd->depth != pd->statedepth)
224     {
225       pd->depth++;
226       return;
227     }
228
229   pd->depth++;
230   for (sw = pd->swtab[pd->state]; sw->from == pd->state; sw++)  /* find name in statetable */
231     if (!strcmp(sw->ename, name))
232       break;
233   
234   if (sw->from != pd->state)
235     {
236 #if 1
237       fprintf(stderr, "into unknown: [%d]%s (from: %d)\n", sw->to, name, sw->from);
238       exit( 1 );
239 #endif
240       return;
241     }
242   pd->state = sw->to;
243   pd->docontent = sw->docontent;
244   pd->statedepth = pd->depth;
245   pd->lcontent = 0;
246   *pd->content = 0;
247
248   switch(pd->state)
249     {
250       case STATE_START:
251       break;
252       case STATE_UPDATES:
253       break;
254       /*
255        * <update from="rel-eng@fedoraproject.org"
256        *         status="stable"
257        *         type="bugfix" (enhancement, security)
258        *         version="1.4">
259        */
260       case STATE_UPDATE:
261       {
262         const char *from = 0, *status = 0, *type = 0, *version = 0;
263         for (; *atts; atts += 2)
264         {
265           if (!strcmp(*atts, "from"))
266             from = atts[1];
267           else if (!strcmp(*atts, "status"))
268             status = atts[1];
269           else if (!strcmp(*atts, "type"))
270             type = atts[1];
271           else if (!strcmp(*atts, "version"))
272             version = atts[1];
273         }
274         
275         solvable = pd->solvable = pool_id2solvable(pool, repo_add_solvable(pd->repo));
276         pd->datanum = (pd->solvable - pool->solvables) - pd->repo->start;
277         repodata_extend(pd->data, pd->solvable - pool->solvables);      
278         
279         solvable->vendor = str2id(pool, from, 1);
280         solvable->evr = str2id(pool, version, 1);
281         solvable->arch = ARCH_NOARCH;
282         repodata_set_str(pd->data, pd->datanum, SOLVABLE_PATCHCATEGORY, type);
283       }
284       break;
285       /* <id>FEDORA-2007-4594</id> */
286       case STATE_ID:
287       break;
288       /* <title>imlib-1.9.15-6.fc8</title> */
289       case STATE_TITLE:
290       break;
291       /* <release>Fedora 8</release> */
292       case STATE_RELEASE:
293       break;
294       /*  <issued date="2008-03-21 21:36:55"/>
295       */
296       case STATE_ISSUED:
297       {
298         const char *date = 0;
299         for (; *atts; atts += 2)
300         {
301           if (!strcmp(*atts, "date"))
302             date = atts[1];
303         }
304         repodata_set_str(pd->data, pd->datanum, SOLVABLE_BUILDTIME, date);
305       }
306       break;
307       case STATE_REFERENCES:
308       break;
309       /*  <reference href="https://bugzilla.redhat.com/show_bug.cgi?id=330471"
310        *             id="330471"
311        *             title="LDAP schema file missing for dhcpd"
312        *             type="bugzilla"/>
313        */
314       case STATE_REFERENCE:
315       break;
316       /* <description>This update ...</description> */
317       case STATE_DESCRIPTION:
318       break;
319       case STATE_PKGLIST:
320       break;
321       /* <collection short="F8"> */
322       case STATE_COLLECTION:
323       break;
324       /* <name>Fedora 8</name> */ 
325       case STATE_NAME:
326       break;
327       /*   <package arch="ppc64" name="imlib-debuginfo" release="6.fc8"
328        *            src="http://download.fedoraproject.org/pub/fedora/linux/updates/8/ppc64/imlib-debuginfo-1.9.15-6.fc8.ppc64.rpm"
329        *            version="1.9.15">
330        * 
331        * -> patch.conflicts: {name} < {version}.{release}
332        */
333       case STATE_PACKAGE:
334       {
335         const char *arch = 0, *name = 0, *src = 0;
336         Id evr = makeevr_atts(pool, pd, atts); /* parse "epoch", "version", "release" */
337         Id n, a, na;
338         Id rel_id;
339         for (; *atts; atts += 2)
340         {
341           if (!strcmp(*atts, "arch"))
342             arch = atts[1];
343           else if (!strcmp(*atts, "name"))
344             name = atts[1];
345           else if (!strcmp(*atts, "src"))
346             src = atts[1];
347         }
348         /* generated Ids for name and arch */
349         n = str2id(pool, name, 1);
350         a = str2id(pool, arch, 1);
351         /*  now combine both to a single Id */
352         na = rel2id(pool, n, a, REL_ARCH, 1);
353         
354         rel_id = rel2id(pool, na, evr, REL_LT, 1);
355
356         solvable->conflicts = repo_addid_dep(pd->repo, solvable->conflicts, rel_id, 0);
357       }
358       break;
359       /* <filename>libntlm-0.4.2-1.fc8.x86_64.rpm</filename> */ 
360       case STATE_FILENAME:
361       break;
362       /* <reboot_suggested>True</reboot_suggested> */
363       case STATE_REBOOT:
364       break;
365       /* <restart_suggested>True</restart_suggested> */
366       case STATE_RESTART:
367       break;
368       case NUMSTATES+1:
369         split(NULL, NULL, 0); /* just to keep gcc happy about tools_util.h: static ... split() {...}  Urgs!*/
370       break;
371       default:
372       break;
373     }
374   return;
375 }
376
377
378 static void XMLCALL
379 endElement(void *userData, const char *name)
380 {
381   struct parsedata *pd = userData;
382   Pool *pool = pd->pool;
383   Solvable *s = pd->solvable;
384
385 #if 0
386       fprintf(stderr, "end: %s\n", name);
387 #endif
388   if (pd->depth != pd->statedepth)
389     {
390       pd->depth--;
391 #if 1
392       fprintf(stderr, "back from unknown %d %d %d\n", pd->state, pd->depth, pd->statedepth);
393 #endif
394       return;
395     }
396
397   pd->depth--;
398   pd->statedepth--;
399   switch (pd->state)
400     {
401       case STATE_START:
402       break;
403       case STATE_UPDATES:
404       break;
405       case STATE_UPDATE:
406       break;
407       case STATE_ID:
408       {
409         if (pd->content) {
410           s->name = str2id(pool, join2("patch", ":", pd->content), 1);
411         }
412       }
413       break;
414       /* <title>imlib-1.9.15-6.fc8</title> */
415       case STATE_TITLE:
416       {
417         while (pd->lcontent > 0
418                && *(pd->content + pd->lcontent - 1) == '\n')
419         {
420           --pd->lcontent;
421           *(pd->content + pd->lcontent) = 0;
422         }
423         repodata_set_str(pd->data, pd->datanum, SOLVABLE_SUMMARY, pd->content);
424       }
425       break;
426       /*
427        * <release>Fedora 8</release>
428        */
429       case STATE_RELEASE:
430       break;
431       case STATE_ISSUED:
432       break;
433       case STATE_REFERENCES:
434       break;
435       case STATE_REFERENCE:
436       break;
437       /*
438        * <description>This update ...</description>
439        */
440       case STATE_DESCRIPTION:
441       {
442         repodata_set_str(pd->data, pd->datanum, SOLVABLE_DESCRIPTION, pd->content);
443       }
444       break;   
445       case STATE_PKGLIST:
446       break;
447       case STATE_COLLECTION:
448       break;
449       case STATE_NAME:
450       break;
451       case STATE_PACKAGE:
452       break;
453       /* <filename>libntlm-0.4.2-1.fc8.x86_64.rpm</filename> */ 
454       case STATE_FILENAME:
455       break;
456       /* <reboot_suggested>True</reboot_suggested> */
457       case STATE_REBOOT:
458       {
459         repodata_set_void(pd->data, pd->datanum, UPDATE_REBOOT);
460       }
461       break;
462       /* <restart_suggested>True</restart_suggested> */
463       case STATE_RESTART:
464       {
465         repodata_set_void(pd->data, pd->datanum, UPDATE_RESTART);
466       }
467       break;
468       default:
469       break;
470     }
471
472   pd->state = pd->sbtab[pd->state];
473   pd->docontent = 0;
474   
475   return;
476 }
477
478
479 static void XMLCALL
480 characterData(void *userData, const XML_Char *s, int len)
481 {
482   struct parsedata *pd = userData;
483   int l;
484   char *c;
485   if (!pd->docontent) {
486 #if 0
487     char *dup = strndup( s, len );
488   fprintf(stderr, "Content: [%d]'%s'\n", pd->state, dup );
489   free( dup );
490 #endif
491     return;
492   }
493   l = pd->lcontent + len + 1;
494   if (l > pd->acontent)
495     {
496       pd->content = realloc(pd->content, l + 256);
497       pd->acontent = l + 256;
498     }
499   c = pd->content + pd->lcontent;
500   pd->lcontent += len;
501   while (len-- > 0)
502     *c++ = *s++;
503   *c = 0;
504 }
505
506
507 #define BUFF_SIZE 8192
508
509 void
510 repo_add_updateinfoxml(Repo *repo, FILE *fp, int flags)
511 {
512   Pool *pool = repo->pool;
513   struct parsedata pd;
514   char buf[BUFF_SIZE];
515   int i, l;
516   struct stateswitch *sw;
517
518   memset(&pd, 0, sizeof(pd));
519   for (i = 0, sw = stateswitches; sw->from != NUMSTATES; i++, sw++)
520     {
521       if (!pd.swtab[sw->from])
522         pd.swtab[sw->from] = sw;
523       pd.sbtab[sw->to] = sw->from;
524     }
525   pd.pool = pool;
526   pd.repo = repo;
527   pd.data = repo_add_repodata(pd.repo, 0);
528
529   pd.content = malloc(256);
530   pd.acontent = 256;
531   pd.lcontent = 0;
532   pd.tempstr = malloc(256);
533   pd.atemp = 256;
534   pd.ltemp = 0;
535   XML_Parser parser = XML_ParserCreate(NULL);
536   XML_SetUserData(parser, &pd);
537   XML_SetElementHandler(parser, startElement, endElement);
538   XML_SetCharacterDataHandler(parser, characterData);
539   for (;;)
540     {
541       l = fread(buf, 1, sizeof(buf), fp);
542       if (XML_Parse(parser, buf, l, l == 0) == XML_STATUS_ERROR)
543         {
544           fprintf(stderr, "repo_updateinfoxml: %s at line %u:%u\n", XML_ErrorString(XML_GetErrorCode(parser)), (unsigned int)XML_GetCurrentLineNumber(parser), (unsigned int)XML_GetCurrentColumnNumber(parser));
545           exit(1);
546         }
547       if (l == 0)
548         break;
549     }
550   XML_ParserFree(parser);
551
552   if (pd.data)
553     repodata_internalize(pd.data);
554
555   free(pd.content);
556 }
557
558 /* EOF */