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