- fix filelist handling in repo_susetags and testcase
[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         break;
304       }
305     case STATE_CHECKSUM:
306     case STATE_OPENCHECKSUM:
307       {
308         const char *type= find_attr("type", atts);
309         pd->chksumtype = type && *type ? solv_chksum_str2type(type) : 0;
310         if (!pd->chksumtype)
311           {
312             fprintf(stderr, "Unknown checksum type: %d: %s\n", (unsigned int)XML_GetCurrentLineNumber(*pd->parser), type ? type : "NULL");
313             exit(1);
314           }
315         break;
316       }
317     default:
318       break;
319     }
320   return;
321 }
322
323 static void XMLCALL
324 endElement(void *userData, const char *name)
325 {
326   struct parsedata *pd = userData;
327   /* Pool *pool = pd->pool; */
328
329 #if 0
330   fprintf(stderr, "endElement: %s\n", name);
331 #endif
332   if (pd->depth != pd->statedepth)
333     {
334       pd->depth--;
335 #if 0
336       fprintf(stderr, "back from unknown %d %d %d\n", pd->state, pd->depth, pd->statedepth);
337 #endif
338       return;
339     }
340
341   pd->depth--;
342   pd->statedepth--;
343   switch (pd->state)
344     {
345     case STATE_START: break;
346     case STATE_REPOMD:
347       if (pd->timestamp > 0)
348         repodata_set_num(pd->data, SOLVID_META, REPOSITORY_TIMESTAMP, pd->timestamp);
349       break;
350     case STATE_DATA:
351       if (pd->rdhandle)
352         repodata_add_flexarray(pd->data, SOLVID_META, REPOSITORY_REPOMD, pd->rdhandle);
353       pd->rdhandle = 0;
354       break;
355     case STATE_LOCATION: break;
356
357     case STATE_CHECKSUM:
358     case STATE_OPENCHECKSUM:
359       if (strlen(pd->content) != 2 * solv_chksum_len(pd->chksumtype))
360         {
361           fprintf(stderr, "Invalid checksum length: %d: for %s\n", (unsigned int)XML_GetCurrentLineNumber(*pd->parser), solv_chksum_type2str(pd->chksumtype));
362           exit(1);
363         }
364       repodata_set_checksum(pd->data, pd->rdhandle, pd->state == STATE_CHECKSUM ? REPOSITORY_REPOMD_CHECKSUM : REPOSITORY_REPOMD_OPENCHECKSUM, pd->chksumtype, pd->content);
365       break;
366
367     case STATE_TIMESTAMP:
368       {
369         /**
370          * we want to look for the newest timestamp
371          * of all resources to save it as the time
372          * the metadata was generated
373          */
374         int timestamp = atoi(pd->content);
375         if (timestamp)
376           repodata_set_num(pd->data, pd->rdhandle, REPOSITORY_REPOMD_TIMESTAMP, timestamp);
377         if (timestamp > pd->timestamp)
378           pd->timestamp = timestamp;
379         break;
380       }
381     case STATE_EXPIRE:
382       {
383         int expire = atoi(pd->content);
384         if (expire > 0)
385           repodata_set_num(pd->data, SOLVID_META, REPOSITORY_EXPIRE, expire);
386         break;
387       }
388       /* repomd.xml content and suseinfo.xml keywords are equivalent */
389     case STATE_CONTENT:
390     case STATE_KEYWORD:
391       if (pd->content)
392         repodata_add_poolstr_array(pd->data, SOLVID_META, REPOSITORY_KEYWORDS, pd->content);
393       break;
394     case STATE_REVISION:
395       if (pd->content)
396         repodata_add_poolstr_array(pd->data, SOLVID_META, REPOSITORY_REVISION, pd->content);
397       break;
398     case STATE_DISTRO:
399       /* distro tag is used in repomd.xml to say the product this repo is
400          made for */
401       if (pd->content)
402         repodata_set_str(pd->data, pd->rphandle, REPOSITORY_PRODUCT_LABEL, pd->content);
403       repodata_add_flexarray(pd->data, SOLVID_META, REPOSITORY_DISTROS, pd->rphandle);
404       break;
405     case STATE_UPDATES:
406       /* distro tag is used in suseinfo.xml to say the repo updates a product
407          however it s not yet a tag standarized for repomd.xml */
408       if (pd->content)
409         repodata_set_str(pd->data, pd->ruhandle, REPOSITORY_PRODUCT_LABEL, pd->content);
410       repodata_add_flexarray(pd->data, SOLVID_META, REPOSITORY_UPDATES, pd->ruhandle);
411       break;
412     case STATE_REPO:
413       if (pd->content)
414         repodata_add_poolstr_array(pd->data, SOLVID_META, REPOSITORY_REPOID, pd->content);
415       break;
416     case STATE_SUSEINFO: break;
417     case STATE_KEYWORDS: break;
418     case NUMSTATES: break;
419     default:
420       break;
421     }
422
423   pd->state = pd->sbtab[pd->state];
424   pd->docontent = 0;
425
426   return;
427 }
428
429
430 static void XMLCALL
431 characterData(void *userData, const XML_Char *s, int len)
432 {
433   struct parsedata *pd = userData;
434   int l;
435   char *c;
436   if (!pd->docontent)
437     return;
438   l = pd->lcontent + len + 1;
439   if (l > pd->acontent)
440     {
441       pd->content = realloc(pd->content, l + 256);
442       pd->acontent = l + 256;
443     }
444   c = pd->content + pd->lcontent;
445   pd->lcontent += len;
446   while (len-- > 0)
447     *c++ = *s++;
448   *c = 0;
449 }
450
451 #define BUFF_SIZE 8192
452
453 void
454 repo_add_repomdxml(Repo *repo, FILE *fp, int flags)
455 {
456   Pool *pool = repo->pool;
457   struct parsedata pd;
458   Repodata *data;
459   char buf[BUFF_SIZE];
460   int i, l;
461   struct stateswitch *sw;
462   XML_Parser parser;
463
464   data = repo_add_repodata(repo, flags);
465
466   memset(&pd, 0, sizeof(pd));
467   pd.timestamp = 0;
468   for (i = 0, sw = stateswitches; sw->from != NUMSTATES; i++, sw++)
469     {
470       if (!pd.swtab[sw->from])
471         pd.swtab[sw->from] = sw;
472       pd.sbtab[sw->to] = sw->from;
473     }
474   pd.pool = pool;
475   pd.repo = repo;
476   pd.data = data;
477
478   pd.content = malloc(256);
479   pd.acontent = 256;
480   pd.lcontent = 0;
481   parser = XML_ParserCreate(NULL);
482   XML_SetUserData(parser, &pd);
483   pd.parser = &parser;
484   XML_SetElementHandler(parser, startElement, endElement);
485   XML_SetCharacterDataHandler(parser, characterData);
486   for (;;)
487     {
488       l = fread(buf, 1, sizeof(buf), fp);
489       if (XML_Parse(parser, buf, l, l == 0) == XML_STATUS_ERROR)
490         {
491           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));
492           exit(1);
493         }
494       if (l == 0)
495         break;
496     }
497   XML_ParserFree(parser);
498
499   if (!(flags & REPO_NO_INTERNALIZE))
500     repodata_internalize(data);
501
502   free(pd.content);
503 }
504
505 /* EOF */