cosmetic: fix possibly uninitialized variable warning in solv.c.
[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_repomdxml.h"
23
24 /*
25 <repomd>
26
27   <!-- these tags are available in create repo > 0.9.6 -->
28   <revision>timestamp_or_arbitrary_user_supplied_string</revision>
29   <tags>
30     <content>opensuse</content>
31     <content>i386</content>
32     <content>other string</content>
33     <distro cpeid="cpe://o:opensuse_project:opensuse:11">openSUSE 11.0</distro>
34   </tags>
35   <!-- end -->
36
37   <data type="primary">
38     <location href="repodata/primary.xml.gz"/>
39     <checksum type="sha">e9162516fa25fec8d60caaf4682d2e49967786cc</checksum>
40     <timestamp>1215708444</timestamp>
41     <open-checksum type="sha">c796c48184cd5abc260e4ba929bdf01be14778a7</open-checksum>
42   </data>
43   <data type="filelists">
44     <location href="repodata/filelists.xml.gz"/>
45     <checksum type="sha">1c638295c49e9707c22810004ebb0799791fcf45</checksum>
46     <timestamp>1215708445</timestamp>
47     <open-checksum type="sha">54a40d5db3df0813b8acbe58cea616987eb9dc16</open-checksum>
48   </data>
49   <data type="other">
50     <location href="repodata/other.xml.gz"/>
51     <checksum type="sha">a81ef39eaa70e56048f8351055119d8c82af2491</checksum>
52     <timestamp>1215708447</timestamp>
53     <open-checksum type="sha">4d1ee867c8864025575a2fb8fde3b85371d51978</open-checksum>
54   </data>
55   <data type="deltainfo">
56     <location href="repodata/deltainfo.xml.gz"/>
57     <checksum type="sha">5880cfa5187026a24a552d3c0650904a44908c28</checksum>
58     <timestamp>1215708447</timestamp>
59     <open-checksum type="sha">7c964a2c3b17df5bfdd962c3be952c9ca6978d8b</open-checksum>
60   </data>
61   <data type="updateinfo">
62     <location href="repodata/updateinfo.xml.gz"/>
63     <checksum type="sha">4097f7e25c7bb0770ae31b2471a9c8c077ee904b</checksum>
64     <timestamp>1215708447</timestamp>
65     <open-checksum type="sha">24f8252f3dd041e37e7c3feb2d57e02b4422d316</open-checksum>
66   </data>
67   <data type="diskusage">
68     <location href="repodata/diskusage.xml.gz"/>
69     <checksum type="sha">4097f7e25c7bb0770ae31b2471a9c8c077ee904b</checksum>
70     <timestamp>1215708447</timestamp>
71     <open-checksum type="sha">24f8252f3dd041e37e7c3feb2d57e02b4422d316</open-checksum>
72   </data>
73 </repomd>
74
75 support also extension suseinfo format
76 <suseinfo>
77   <expire>timestamp</expire>
78   <products>
79     <id>...</id>
80   </products>
81   <kewwords>
82     <k>...</k>
83   </keywords>
84 </suseinfo>
85
86 */
87
88 enum state {
89   STATE_START,
90   /* extension tags */
91   STATE_SUSEINFO,
92   STATE_EXPIRE,
93   STATE_KEYWORDS,
94   STATE_KEYWORD,
95
96   /* normal repomd.xml */
97   STATE_REPOMD,
98   STATE_REVISION,
99   STATE_TAGS,
100   STATE_REPO,
101   STATE_CONTENT,
102   STATE_DISTRO,
103   STATE_UPDATES,
104   STATE_DATA,
105   STATE_LOCATION,
106   STATE_CHECKSUM,
107   STATE_TIMESTAMP,
108   STATE_OPENCHECKSUM,
109   NUMSTATES
110 };
111
112 struct stateswitch {
113   enum state from;
114   char *ename;
115   enum state to;
116   int docontent;
117 };
118
119 /* !! must be sorted by first column !! */
120 static struct stateswitch stateswitches[] = {
121   /* suseinfo tags */
122   { STATE_START,       "repomd",          STATE_REPOMD, 0 },
123   { STATE_START,       "suseinfo",        STATE_SUSEINFO, 0 },
124   /* we support the tags element in suseinfo in case
125      createrepo version does not support it yet */
126   { STATE_SUSEINFO,    "tags",            STATE_TAGS, 0 },
127   { STATE_SUSEINFO,    "expire",          STATE_EXPIRE, 1 },
128   { STATE_SUSEINFO,    "keywords",        STATE_KEYWORDS, 0 },
129   /* keywords is the suse extension equivalent of
130      tags/content when this one was not yet available.
131      therefore we parse both */
132   { STATE_KEYWORDS,    "k",               STATE_KEYWORD, 1 },
133   /* standard tags */
134   { STATE_REPOMD,      "revision",        STATE_REVISION, 1 },
135   { STATE_REPOMD,      "tags",            STATE_TAGS,  0 },
136   { STATE_REPOMD,      "data",            STATE_DATA,  0 },
137
138   { STATE_TAGS,        "repo",            STATE_REPO,    1 },
139   { STATE_TAGS,        "content",         STATE_CONTENT, 1 },
140   { STATE_TAGS,        "distro",          STATE_DISTRO,  1 },
141   /* this tag is only valid in suseinfo.xml for now */
142   { STATE_TAGS,        "updates",         STATE_UPDATES,  1 },
143
144   { STATE_DATA,        "location",        STATE_LOCATION, 0 },
145   { STATE_DATA,        "checksum",        STATE_CHECKSUM, 1 },
146   { STATE_DATA,        "timestamp",       STATE_TIMESTAMP, 1 },
147   { STATE_DATA,        "open-checksum",    STATE_OPENCHECKSUM, 1 },
148   { NUMSTATES }
149 };
150
151
152 struct parsedata {
153   int ret;
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   Id chksumtype;
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 = solv_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                   repodata_add_poolstr_array(pd->data, 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         break;
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           pd->ret = pool_error(pd->pool, -1, "line %d: unknown checksum type: %s", (unsigned int)XML_GetCurrentLineNumber(*pd->parser), type ? type : "NULL");
311         break;
312       }
313     default:
314       break;
315     }
316   return;
317 }
318
319 static void XMLCALL
320 endElement(void *userData, const char *name)
321 {
322   struct parsedata *pd = userData;
323   /* Pool *pool = pd->pool; */
324
325 #if 0
326   fprintf(stderr, "endElement: %s\n", name);
327 #endif
328   if (pd->depth != pd->statedepth)
329     {
330       pd->depth--;
331 #if 0
332       fprintf(stderr, "back from unknown %d %d %d\n", pd->state, pd->depth, pd->statedepth);
333 #endif
334       return;
335     }
336
337   pd->depth--;
338   pd->statedepth--;
339   switch (pd->state)
340     {
341     case STATE_START: break;
342     case STATE_REPOMD:
343       if (pd->timestamp > 0)
344         repodata_set_num(pd->data, SOLVID_META, REPOSITORY_TIMESTAMP, pd->timestamp);
345       break;
346     case STATE_DATA:
347       if (pd->rdhandle)
348         repodata_add_flexarray(pd->data, SOLVID_META, REPOSITORY_REPOMD, pd->rdhandle);
349       pd->rdhandle = 0;
350       break;
351     case STATE_LOCATION: break;
352
353     case STATE_CHECKSUM:
354     case STATE_OPENCHECKSUM:
355       if (!pd->chksumtype)
356         break;
357       if (strlen(pd->content) != 2 * solv_chksum_len(pd->chksumtype))
358         pd->ret = pool_error(pd->pool, -1, "line %d: invalid checksum length for %s", (unsigned int)XML_GetCurrentLineNumber(*pd->parser), solv_chksum_type2str(pd->chksumtype));
359       else
360         repodata_set_checksum(pd->data, pd->rdhandle, pd->state == STATE_CHECKSUM ? REPOSITORY_REPOMD_CHECKSUM : REPOSITORY_REPOMD_OPENCHECKSUM, pd->chksumtype, pd->content);
361       break;
362
363     case STATE_TIMESTAMP:
364       {
365         /**
366          * we want to look for the newest timestamp
367          * of all resources to save it as the time
368          * the metadata was generated
369          */
370         int timestamp = atoi(pd->content);
371         if (timestamp)
372           repodata_set_num(pd->data, pd->rdhandle, REPOSITORY_REPOMD_TIMESTAMP, timestamp);
373         if (timestamp > pd->timestamp)
374           pd->timestamp = timestamp;
375         break;
376       }
377     case STATE_EXPIRE:
378       {
379         int expire = atoi(pd->content);
380         if (expire > 0)
381           repodata_set_num(pd->data, SOLVID_META, REPOSITORY_EXPIRE, expire);
382         break;
383       }
384       /* repomd.xml content and suseinfo.xml keywords are equivalent */
385     case STATE_CONTENT:
386     case STATE_KEYWORD:
387       if (pd->content)
388         repodata_add_poolstr_array(pd->data, SOLVID_META, REPOSITORY_KEYWORDS, pd->content);
389       break;
390     case STATE_REVISION:
391       if (pd->content)
392         repodata_add_poolstr_array(pd->data, SOLVID_META, REPOSITORY_REVISION, pd->content);
393       break;
394     case STATE_DISTRO:
395       /* distro tag is used in repomd.xml to say the product this repo is
396          made for */
397       if (pd->content)
398         repodata_set_str(pd->data, pd->rphandle, REPOSITORY_PRODUCT_LABEL, pd->content);
399       repodata_add_flexarray(pd->data, SOLVID_META, REPOSITORY_DISTROS, pd->rphandle);
400       break;
401     case STATE_UPDATES:
402       /* distro tag is used in suseinfo.xml to say the repo updates a product
403          however it s not yet a tag standarized for repomd.xml */
404       if (pd->content)
405         repodata_set_str(pd->data, pd->ruhandle, REPOSITORY_PRODUCT_LABEL, pd->content);
406       repodata_add_flexarray(pd->data, SOLVID_META, REPOSITORY_UPDATES, pd->ruhandle);
407       break;
408     case STATE_REPO:
409       if (pd->content)
410         repodata_add_poolstr_array(pd->data, SOLVID_META, REPOSITORY_REPOID, pd->content);
411       break;
412     case STATE_SUSEINFO: break;
413     case STATE_KEYWORDS: break;
414     case NUMSTATES: break;
415     default:
416       break;
417     }
418
419   pd->state = pd->sbtab[pd->state];
420   pd->docontent = 0;
421
422   return;
423 }
424
425
426 static void XMLCALL
427 characterData(void *userData, const XML_Char *s, int len)
428 {
429   struct parsedata *pd = userData;
430   int l;
431   char *c;
432   if (!pd->docontent)
433     return;
434   l = pd->lcontent + len + 1;
435   if (l > pd->acontent)
436     {
437       pd->content = realloc(pd->content, l + 256);
438       pd->acontent = l + 256;
439     }
440   c = pd->content + pd->lcontent;
441   pd->lcontent += len;
442   while (len-- > 0)
443     *c++ = *s++;
444   *c = 0;
445 }
446
447 #define BUFF_SIZE 8192
448
449 int
450 repo_add_repomdxml(Repo *repo, FILE *fp, int flags)
451 {
452   Pool *pool = repo->pool;
453   struct parsedata pd;
454   Repodata *data;
455   char buf[BUFF_SIZE];
456   int i, l;
457   struct stateswitch *sw;
458   XML_Parser parser;
459
460   data = repo_add_repodata(repo, flags);
461
462   memset(&pd, 0, sizeof(pd));
463   pd.timestamp = 0;
464   for (i = 0, sw = stateswitches; sw->from != NUMSTATES; i++, sw++)
465     {
466       if (!pd.swtab[sw->from])
467         pd.swtab[sw->from] = sw;
468       pd.sbtab[sw->to] = sw->from;
469     }
470   pd.pool = pool;
471   pd.repo = repo;
472   pd.data = data;
473
474   pd.content = malloc(256);
475   pd.acontent = 256;
476   pd.lcontent = 0;
477   parser = XML_ParserCreate(NULL);
478   XML_SetUserData(parser, &pd);
479   pd.parser = &parser;
480   XML_SetElementHandler(parser, startElement, endElement);
481   XML_SetCharacterDataHandler(parser, characterData);
482   for (;;)
483     {
484       l = fread(buf, 1, sizeof(buf), fp);
485       if (XML_Parse(parser, buf, l, l == 0) == XML_STATUS_ERROR)
486         {
487           pd.ret = pool_error(pool, -1, "repo_repomdxml: %s at line %u:%u", XML_ErrorString(XML_GetErrorCode(parser)), (unsigned int)XML_GetCurrentLineNumber(parser), (unsigned int)XML_GetCurrentColumnNumber(parser));
488           break;
489         }
490       if (l == 0)
491         break;
492     }
493   XML_ParserFree(parser);
494
495   if (!(flags & REPO_NO_INTERNALIZE))
496     repodata_internalize(data);
497
498   free(pd.content);
499   return pd.ret;
500 }
501
502 /* EOF */