parse global repository ids. [bnc#377568]
[platform/upstream/libsolv.git] / ext / repo_repomdxml.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 #define DO_ARRAY 1
9
10 #define _GNU_SOURCE
11 #include <sys/types.h>
12 #include <limits.h>
13 #include <fcntl.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include <expat.h>
18
19 #include "pool.h"
20 #include "repo.h"
21 #include "repo_updateinfoxml.h"
22
23 //#define DUMPOUT 0
24
25 /*
26 <repomd>
27
28   <!-- these tags are available in create repo > 0.9.6 -->
29   <revision>timestamp_or_arbitrary_user_supplied_string</revision>
30   <tags>
31     <content>opensuse</content>
32     <content>i386</content>
33     <content>other string</content>
34     <distro cpeid="cpe://o:opensuse_project:opensuse:11">openSUSE 11.0</distro>
35   </tags>
36   <!-- end -->
37
38   <data type="primary">
39     <location href="repodata/primary.xml.gz"/>
40     <checksum type="sha">e9162516fa25fec8d60caaf4682d2e49967786cc</checksum>
41     <timestamp>1215708444</timestamp>
42     <open-checksum type="sha">c796c48184cd5abc260e4ba929bdf01be14778a7</open-checksum>
43   </data>
44   <data type="filelists">
45     <location href="repodata/filelists.xml.gz"/>
46     <checksum type="sha">1c638295c49e9707c22810004ebb0799791fcf45</checksum>
47     <timestamp>1215708445</timestamp>
48     <open-checksum type="sha">54a40d5db3df0813b8acbe58cea616987eb9dc16</open-checksum>
49   </data>
50   <data type="other">
51     <location href="repodata/other.xml.gz"/>
52     <checksum type="sha">a81ef39eaa70e56048f8351055119d8c82af2491</checksum>
53     <timestamp>1215708447</timestamp>
54     <open-checksum type="sha">4d1ee867c8864025575a2fb8fde3b85371d51978</open-checksum>
55   </data>
56   <data type="deltainfo">
57     <location href="repodata/deltainfo.xml.gz"/>
58     <checksum type="sha">5880cfa5187026a24a552d3c0650904a44908c28</checksum>
59     <timestamp>1215708447</timestamp>
60     <open-checksum type="sha">7c964a2c3b17df5bfdd962c3be952c9ca6978d8b</open-checksum>
61   </data>
62   <data type="updateinfo">
63     <location href="repodata/updateinfo.xml.gz"/>
64     <checksum type="sha">4097f7e25c7bb0770ae31b2471a9c8c077ee904b</checksum>
65     <timestamp>1215708447</timestamp>
66     <open-checksum type="sha">24f8252f3dd041e37e7c3feb2d57e02b4422d316</open-checksum>
67   </data>
68   <data type="diskusage">
69     <location href="repodata/diskusage.xml.gz"/>
70     <checksum type="sha">4097f7e25c7bb0770ae31b2471a9c8c077ee904b</checksum>
71     <timestamp>1215708447</timestamp>
72     <open-checksum type="sha">24f8252f3dd041e37e7c3feb2d57e02b4422d316</open-checksum>
73   </data>
74 </repomd>
75
76 support also extension suseinfo format
77 <suseinfo>
78   <expire>timestamp</expire>
79   <products>
80     <id>...</id>
81   </products>
82   <kewwords>
83     <k>...</k>
84   </keywords>
85 </suseinfo>
86
87 */
88
89 enum state {
90   STATE_START,
91   /* extension tags */
92   STATE_SUSEINFO,
93   STATE_EXPIRE,
94   STATE_KEYWORDS,
95   STATE_KEYWORD,
96
97   /* normal repomd.xml */
98   STATE_REPOMD,
99   STATE_REVISION,
100   STATE_TAGS,
101   STATE_REPO,
102   STATE_CONTENT,
103   STATE_DISTRO,
104   STATE_UPDATES,
105   STATE_DATA,
106   STATE_LOCATION,
107   STATE_CHECKSUM,
108   STATE_TIMESTAMP,
109   STATE_OPENCHECKSUM,
110   NUMSTATES
111 };
112
113 struct stateswitch {
114   enum state from;
115   char *ename;
116   enum state to;
117   int docontent;
118 };
119
120 /* !! must be sorted by first column !! */
121 static struct stateswitch stateswitches[] = {
122   /* suseinfo tags */
123   { STATE_START,       "repomd",          STATE_REPOMD, 0 },
124   { STATE_START,       "suseinfo",        STATE_SUSEINFO, 0 },
125   /* we support the tags element in suseinfo in case
126      createrepo version does not support it yet */
127   { STATE_SUSEINFO,    "tags",            STATE_TAGS, 0 },
128   { STATE_SUSEINFO,    "expire",          STATE_EXPIRE, 1 },
129   { STATE_SUSEINFO,    "keywords",        STATE_KEYWORDS, 0 },
130   /* keywords is the suse extension equivalent of
131      tags/content when this one was not yet available.
132      therefore we parse both */
133   { STATE_KEYWORDS,    "k",               STATE_KEYWORD, 1 },
134   /* standard tags */
135   { STATE_REPOMD,      "revision",        STATE_REVISION, 1 },
136   { STATE_REPOMD,      "tags",            STATE_TAGS,  0 },
137   { STATE_REPOMD,      "data",            STATE_DATA,  0 },
138
139   { STATE_TAGS,        "repo",            STATE_REPO,    1 },
140   { STATE_TAGS,        "content",         STATE_CONTENT, 1 },
141   { STATE_TAGS,        "distro",          STATE_DISTRO,  1 },
142   /* this tag is only valid in suseinfo.xml for now */
143   { STATE_TAGS,        "updates",         STATE_UPDATES,  1 },
144
145   { STATE_DATA,        "location",        STATE_LOCATION, 0 },
146   { STATE_DATA,        "checksum",        STATE_CHECKSUM, 1 },
147   { STATE_DATA,        "timestamp",       STATE_TIMESTAMP, 1 },
148   { STATE_DATA,        "open-checksum",    STATE_OPENCHECKSUM, 1 },
149   { NUMSTATES }
150 };
151
152
153 struct parsedata {
154   int depth;
155   enum state state;
156   int statedepth;
157   char *content;
158   int lcontent;
159   int acontent;
160   int docontent;
161   Pool *pool;
162   Repo *repo;
163   Repodata *data;
164
165   XML_Parser *parser;
166   struct stateswitch *swtab[NUMSTATES];
167   enum state sbtab[NUMSTATES];
168   int timestamp;
169   /* handles for collection
170      structures */
171   /* repo updates */
172   Id ruhandle;
173   /* repo products */
174   Id rphandle;
175   /* repo data handle */
176   Id rdhandle;
177
178   const char *tmpattr;
179 };
180
181 /*
182  * find attribute
183  */
184
185 static inline const char *
186 find_attr(const char *txt, const char **atts)
187 {
188   for (; *atts; atts += 2)
189     {
190       if (!strcmp(*atts, txt))
191         return atts[1];
192     }
193   return 0;
194 }
195
196
197 static void XMLCALL
198 startElement(void *userData, const char *name, const char **atts)
199 {
200   struct parsedata *pd = userData;
201   /*Pool *pool = pd->pool;*/
202   struct stateswitch *sw;
203
204 #if 0
205   fprintf(stderr, "start: [%d]%s\n", pd->state, name);
206 #endif
207   if (pd->depth != pd->statedepth)
208     {
209       pd->depth++;
210       return;
211     }
212
213   pd->depth++;
214   if (!pd->swtab[pd->state])
215     return;
216   for (sw = pd->swtab[pd->state]; sw->from == pd->state; sw++)  /* find name in statetable */
217     if (!strcmp(sw->ename, name))
218       break;
219
220   if (sw->from != pd->state)
221     {
222 #if 0
223       fprintf(stderr, "into unknown: %s (from: %d)\n", name, pd->state);
224 #endif
225       return;
226     }
227   pd->state = sw->to;
228   pd->docontent = sw->docontent;
229   pd->statedepth = pd->depth;
230   pd->lcontent = 0;
231   *pd->content = 0;
232
233   switch(pd->state)
234     {
235     case STATE_START: break;
236     case STATE_REPOMD:
237       {
238         const char *updstr;
239
240         /* this should be OBSOLETE soon */
241         updstr = find_attr("updates", atts);
242         if (updstr)
243           {
244             char *value = strdup(updstr);
245             char *fvalue = value; /* save the first */
246             while (value)
247               {
248                 char *p = strchr(value, ',');
249                 if (*p)
250                   *p++ = 0;
251                 if (*value)
252                   repo_add_poolstr_array(pd->repo, SOLVID_META, REPOSITORY_UPDATES, value);
253                 value = p;
254               }
255             free(fvalue);
256           }
257           break;
258         }
259     case STATE_SUSEINFO: break;
260     case STATE_EXPIRE: break;
261     case STATE_KEYWORDS: break;
262     case STATE_KEYWORD: break;
263     case STATE_CONTENT: break;
264     case STATE_REVISION: break;
265     case STATE_DISTRO:
266       {
267         /* this is extra metadata about the product this repository
268            was designed for */
269         const char *cpeid = find_attr("cpeid", atts);
270         pd->rphandle = repodata_new_handle(pd->data);
271         /* set the cpeid for the product
272            the label is set in the content of the tag */
273         if (cpeid)
274           repodata_set_poolstr(pd->data, pd->rphandle, REPOSITORY_PRODUCT_CPEID, cpeid);
275         break;
276       }
277     case STATE_UPDATES:
278       {
279         /* this is extra metadata about the product this repository
280            was designed for */
281         const char *cpeid = find_attr("cpeid", atts);
282         pd->ruhandle = repodata_new_handle(pd->data);
283         /* set the cpeid for the product
284            the label is set in the content of the tag */
285         if (cpeid)
286           repodata_set_poolstr(pd->data, pd->ruhandle, REPOSITORY_PRODUCT_CPEID, cpeid);
287         break;
288       }
289     case STATE_DATA:
290       {
291         const char *type= find_attr("type", atts);
292         pd->rdhandle = repodata_new_handle(pd->data);
293         if (type)
294           repodata_set_poolstr(pd->data, pd->rdhandle, REPOSITORY_REPOMD_TYPE, type);
295         break;
296       }
297     case STATE_LOCATION:
298       {
299         const char *href = find_attr("href", atts);
300         if (href)
301           repodata_set_str(pd->data, pd->rdhandle, REPOSITORY_REPOMD_LOCATION, href);
302       }
303     case STATE_CHECKSUM:
304     case STATE_OPENCHECKSUM:
305       pd->tmpattr= find_attr("type", atts);
306       break;
307     default:
308       break;
309     }
310   return;
311 }
312
313 static void XMLCALL
314 endElement(void *userData, const char *name)
315 {
316   struct parsedata *pd = userData;
317   /* Pool *pool = pd->pool; */
318
319 #if 0
320   fprintf(stderr, "endElement: %s\n", name);
321 #endif
322   if (pd->depth != pd->statedepth)
323     {
324       pd->depth--;
325 #if 0
326       fprintf(stderr, "back from unknown %d %d %d\n", pd->state, pd->depth, pd->statedepth);
327 #endif
328       return;
329     }
330
331   pd->depth--;
332   pd->statedepth--;
333   switch (pd->state)
334     {
335     case STATE_START: break;
336     case STATE_REPOMD:
337       if (pd->timestamp > 0)
338         repodata_set_num(pd->data, SOLVID_META, REPOSITORY_TIMESTAMP, pd->timestamp);
339       break;
340     case STATE_DATA:
341       if (pd->rdhandle)
342         repodata_add_flexarray(pd->data, SOLVID_META, REPOSITORY_REPOMD, pd->rdhandle);
343       pd->rdhandle = 0;
344       break;
345     case STATE_LOCATION: break;
346
347     case STATE_CHECKSUM:
348     case STATE_OPENCHECKSUM:
349       {
350         int l;
351         Id type;
352         if (!strcasecmp(pd->tmpattr, "sha") || !strcasecmp(pd->tmpattr, "sha1"))
353           l = SIZEOF_SHA1 * 2, type = REPOKEY_TYPE_SHA1;
354         else if (!strcasecmp(pd->tmpattr, "sha256"))
355           l = SIZEOF_SHA256 * 2, type = REPOKEY_TYPE_SHA256;
356         else if (!strcasecmp(pd->tmpattr, "md5"))
357           l = SIZEOF_MD5 * 2, type = REPOKEY_TYPE_MD5;
358         else
359           {
360             fprintf(stderr, "Unknown checksum type: %d: %s\n", (unsigned int)XML_GetCurrentLineNumber(*pd->parser), pd->tmpattr);
361             exit(1);
362           }
363         if (strlen(pd->content) != l)
364           {
365             fprintf(stderr, "Invalid checksum length: %d: for %s\n", (unsigned int)XML_GetCurrentLineNumber(*pd->parser), pd->tmpattr);
366             exit(1);
367           }
368         repodata_set_checksum(pd->data, pd->rdhandle, pd->state == STATE_CHECKSUM ? REPOSITORY_REPOMD_CHECKSUM : REPOSITORY_REPOMD_OPENCHECKSUM, type, pd->content);
369         break;
370       }
371
372     case STATE_TIMESTAMP:
373       {
374         /**
375          * we want to look for the newest timestamp
376          * of all resources to save it as the time
377          * the metadata was generated
378          */
379         int timestamp = atoi(pd->content);
380         if (timestamp)
381           repodata_set_num(pd->data, pd->rdhandle, REPOSITORY_REPOMD_TIMESTAMP, timestamp);
382         if (timestamp > pd->timestamp)
383           pd->timestamp = timestamp;
384         break;
385       }
386     case STATE_EXPIRE:
387       {
388         int expire = atoi(pd->content);
389         if (expire > 0)
390           repodata_set_num(pd->data, SOLVID_META, REPOSITORY_EXPIRE, expire);
391         break;
392       }
393       /* repomd.xml content and suseinfo.xml keywords are equivalent */
394     case STATE_CONTENT:
395     case STATE_KEYWORD:
396       if (pd->content)
397         repodata_add_poolstr_array(pd->data, SOLVID_META, REPOSITORY_KEYWORDS, pd->content);
398       break;
399     case STATE_REVISION:
400       if (pd->content)
401         repodata_add_poolstr_array(pd->data, SOLVID_META, REPOSITORY_REVISION, pd->content);
402       break;
403     case STATE_DISTRO:
404       /* distro tag is used in repomd.xml to say the product this repo is
405          made for */
406       if (pd->content)
407         repodata_set_str(pd->data, pd->rphandle, REPOSITORY_PRODUCT_LABEL, pd->content);
408       repodata_add_flexarray(pd->data, SOLVID_META, REPOSITORY_DISTROS, pd->rphandle);
409       break;
410     case STATE_UPDATES:
411       /* distro tag is used in suseinfo.xml to say the repo updates a product
412          however it s not yet a tag standarized for repomd.xml */
413       if (pd->content)
414         repodata_set_str(pd->data, pd->ruhandle, REPOSITORY_PRODUCT_LABEL, pd->content);
415       repodata_add_flexarray(pd->data, SOLVID_META, REPOSITORY_UPDATES, pd->ruhandle);
416       break;
417     case STATE_REPO:
418       if (pd->content)
419         repodata_add_poolstr_array(pd->data, SOLVID_META, REPOSITORY_GLOBALID, pd->content);
420       break;
421     case STATE_SUSEINFO: break;
422     case STATE_KEYWORDS: break;
423     case NUMSTATES: break;
424     default:
425       break;
426     }
427
428   pd->state = pd->sbtab[pd->state];
429   pd->docontent = 0;
430
431   return;
432 }
433
434
435 static void XMLCALL
436 characterData(void *userData, const XML_Char *s, int len)
437 {
438   struct parsedata *pd = userData;
439   int l;
440   char *c;
441   if (!pd->docontent)
442     return;
443   l = pd->lcontent + len + 1;
444   if (l > pd->acontent)
445     {
446       pd->content = realloc(pd->content, l + 256);
447       pd->acontent = l + 256;
448     }
449   c = pd->content + pd->lcontent;
450   pd->lcontent += len;
451   while (len-- > 0)
452     *c++ = *s++;
453   *c = 0;
454 }
455
456 #define BUFF_SIZE 8192
457
458 void
459 repo_add_repomdxml(Repo *repo, FILE *fp, int flags)
460 {
461   Pool *pool = repo->pool;
462   struct parsedata pd;
463   Repodata *data;
464   char buf[BUFF_SIZE];
465   int i, l;
466   struct stateswitch *sw;
467
468   data = repo_add_repodata(repo, flags);
469
470   memset(&pd, 0, sizeof(pd));
471   pd.timestamp = 0;
472   for (i = 0, sw = stateswitches; sw->from != NUMSTATES; i++, sw++)
473     {
474       if (!pd.swtab[sw->from])
475         pd.swtab[sw->from] = sw;
476       pd.sbtab[sw->to] = sw->from;
477     }
478   pd.pool = pool;
479   pd.repo = repo;
480   pd.data = data;
481
482   pd.content = malloc(256);
483   pd.acontent = 256;
484   pd.lcontent = 0;
485   XML_Parser parser = XML_ParserCreate(NULL);
486   XML_SetUserData(parser, &pd);
487   pd.parser = &parser;
488   XML_SetElementHandler(parser, startElement, endElement);
489   XML_SetCharacterDataHandler(parser, characterData);
490   for (;;)
491     {
492       l = fread(buf, 1, sizeof(buf), fp);
493       if (XML_Parse(parser, buf, l, l == 0) == XML_STATUS_ERROR)
494         {
495           pool_debug(pool, SAT_FATAL, "repo_repomdxml: %s at line %u:%u\n", XML_ErrorString(XML_GetErrorCode(parser)), (unsigned int)XML_GetCurrentLineNumber(parser), (unsigned int)XML_GetCurrentColumnNumber(parser));
496           exit(1);
497         }
498       if (l == 0)
499         break;
500     }
501   XML_ParserFree(parser);
502
503   if (!(flags & REPO_NO_INTERNALIZE))
504     repodata_internalize(data);
505
506   free(pd.content);
507 }
508
509 /* EOF */