1d1197e2fee69239946ae08070ef17d36e55835c
[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   STATE_SIZE,
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   { STATE_DATA,        "size",            STATE_SIZE, 1 },
150   { NUMSTATES }
151 };
152
153
154 struct parsedata {
155   int ret;
156   int depth;
157   enum state state;
158   int statedepth;
159   char *content;
160   int lcontent;
161   int acontent;
162   int docontent;
163   Pool *pool;
164   Repo *repo;
165   Repodata *data;
166
167   XML_Parser *parser;
168   struct stateswitch *swtab[NUMSTATES];
169   enum state sbtab[NUMSTATES];
170   int timestamp;
171   /* handles for collection
172      structures */
173   /* repo updates */
174   Id ruhandle;
175   /* repo products */
176   Id rphandle;
177   /* repo data handle */
178   Id rdhandle;
179
180   Id chksumtype;
181 };
182
183 /*
184  * find attribute
185  */
186
187 static inline const char *
188 find_attr(const char *txt, const char **atts)
189 {
190   for (; *atts; atts += 2)
191     {
192       if (!strcmp(*atts, txt))
193         return atts[1];
194     }
195   return 0;
196 }
197
198
199 static void XMLCALL
200 startElement(void *userData, const char *name, const char **atts)
201 {
202   struct parsedata *pd = userData;
203   /*Pool *pool = pd->pool;*/
204   struct stateswitch *sw;
205
206 #if 0
207   fprintf(stderr, "start: [%d]%s\n", pd->state, name);
208 #endif
209   if (pd->depth != pd->statedepth)
210     {
211       pd->depth++;
212       return;
213     }
214
215   pd->depth++;
216   if (!pd->swtab[pd->state])
217     return;
218   for (sw = pd->swtab[pd->state]; sw->from == pd->state; sw++)  /* find name in statetable */
219     if (!strcmp(sw->ename, name))
220       break;
221
222   if (sw->from != pd->state)
223     {
224 #if 0
225       fprintf(stderr, "into unknown: %s (from: %d)\n", name, pd->state);
226 #endif
227       return;
228     }
229   pd->state = sw->to;
230   pd->docontent = sw->docontent;
231   pd->statedepth = pd->depth;
232   pd->lcontent = 0;
233   *pd->content = 0;
234
235   switch(pd->state)
236     {
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_DISTRO:
261       {
262         /* this is extra metadata about the product this repository
263            was designed for */
264         const char *cpeid = find_attr("cpeid", atts);
265         pd->rphandle = repodata_new_handle(pd->data);
266         /* set the cpeid for the product
267            the label is set in the content of the tag */
268         if (cpeid)
269           repodata_set_poolstr(pd->data, pd->rphandle, REPOSITORY_PRODUCT_CPEID, cpeid);
270         break;
271       }
272     case STATE_UPDATES:
273       {
274         /* this is extra metadata about the product this repository
275            was designed for */
276         const char *cpeid = find_attr("cpeid", atts);
277         pd->ruhandle = repodata_new_handle(pd->data);
278         /* set the cpeid for the product
279            the label is set in the content of the tag */
280         if (cpeid)
281           repodata_set_poolstr(pd->data, pd->ruhandle, REPOSITORY_PRODUCT_CPEID, cpeid);
282         break;
283       }
284     case STATE_DATA:
285       {
286         const char *type= find_attr("type", atts);
287         pd->rdhandle = repodata_new_handle(pd->data);
288         if (type)
289           repodata_set_poolstr(pd->data, pd->rdhandle, REPOSITORY_REPOMD_TYPE, type);
290         break;
291       }
292     case STATE_LOCATION:
293       {
294         const char *href = find_attr("href", atts);
295         if (href)
296           repodata_set_str(pd->data, pd->rdhandle, REPOSITORY_REPOMD_LOCATION, href);
297         break;
298       }
299     case STATE_CHECKSUM:
300     case STATE_OPENCHECKSUM:
301       {
302         const char *type= find_attr("type", atts);
303         pd->chksumtype = type && *type ? solv_chksum_str2type(type) : 0;
304         if (!pd->chksumtype)
305           pd->ret = pool_error(pd->pool, -1, "line %d: unknown checksum type: %s", (unsigned int)XML_GetCurrentLineNumber(*pd->parser), type ? type : "NULL");
306         break;
307       }
308     default:
309       break;
310     }
311   return;
312 }
313
314 static void XMLCALL
315 endElement(void *userData, const char *name)
316 {
317   struct parsedata *pd = userData;
318   /* Pool *pool = pd->pool; */
319
320 #if 0
321   fprintf(stderr, "endElement: %s\n", name);
322 #endif
323   if (pd->depth != pd->statedepth)
324     {
325       pd->depth--;
326 #if 0
327       fprintf(stderr, "back from unknown %d %d %d\n", pd->state, pd->depth, pd->statedepth);
328 #endif
329       return;
330     }
331
332   pd->depth--;
333   pd->statedepth--;
334   switch (pd->state)
335     {
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
346     case STATE_CHECKSUM:
347     case STATE_OPENCHECKSUM:
348       if (!pd->chksumtype)
349         break;
350       if (strlen(pd->content) != 2 * solv_chksum_len(pd->chksumtype))
351         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));
352       else
353         repodata_set_checksum(pd->data, pd->rdhandle, pd->state == STATE_CHECKSUM ? REPOSITORY_REPOMD_CHECKSUM : REPOSITORY_REPOMD_OPENCHECKSUM, pd->chksumtype, pd->content);
354       break;
355
356     case STATE_TIMESTAMP:
357       {
358         /**
359          * we want to look for the newest timestamp
360          * of all resources to save it as the time
361          * the metadata was generated
362          */
363         int timestamp = atoi(pd->content);
364         if (timestamp)
365           repodata_set_num(pd->data, pd->rdhandle, REPOSITORY_REPOMD_TIMESTAMP, timestamp);
366         if (timestamp > pd->timestamp)
367           pd->timestamp = timestamp;
368         break;
369       }
370     case STATE_EXPIRE:
371       {
372         int expire = atoi(pd->content);
373         if (expire > 0)
374           repodata_set_num(pd->data, SOLVID_META, REPOSITORY_EXPIRE, expire);
375         break;
376       }
377       /* repomd.xml content and suseinfo.xml keywords are equivalent */
378     case STATE_CONTENT:
379     case STATE_KEYWORD:
380       if (*pd->content)
381         repodata_add_poolstr_array(pd->data, SOLVID_META, REPOSITORY_KEYWORDS, pd->content);
382       break;
383     case STATE_REVISION:
384       if (*pd->content)
385         repodata_set_str(pd->data, SOLVID_META, REPOSITORY_REVISION, pd->content);
386       break;
387     case STATE_DISTRO:
388       /* distro tag is used in repomd.xml to say the product this repo is
389          made for */
390       if (*pd->content)
391         repodata_set_str(pd->data, pd->rphandle, REPOSITORY_PRODUCT_LABEL, pd->content);
392       repodata_add_flexarray(pd->data, SOLVID_META, REPOSITORY_DISTROS, pd->rphandle);
393       break;
394     case STATE_UPDATES:
395       /* updates tag is used in suseinfo.xml to say the repo updates a product
396          however it s not yet a tag standarized for repomd.xml */
397       if (*pd->content)
398         repodata_set_str(pd->data, pd->ruhandle, REPOSITORY_PRODUCT_LABEL, pd->content);
399       repodata_add_flexarray(pd->data, SOLVID_META, REPOSITORY_UPDATES, pd->ruhandle);
400       break;
401     case STATE_REPO:
402       if (*pd->content)
403         repodata_add_poolstr_array(pd->data, SOLVID_META, REPOSITORY_REPOID, pd->content);
404       break;
405     case STATE_SIZE:
406       if (*pd->content)
407         repodata_set_num(pd->data, pd->rdhandle, REPOSITORY_REPOMD_SIZE, strtoull(pd->content, 0, 10));
408       break;
409     default:
410       break;
411     }
412
413   pd->state = pd->sbtab[pd->state];
414   pd->docontent = 0;
415
416   return;
417 }
418
419
420 static void XMLCALL
421 characterData(void *userData, const XML_Char *s, int len)
422 {
423   struct parsedata *pd = userData;
424   int l;
425   char *c;
426   if (!pd->docontent)
427     return;
428   l = pd->lcontent + len + 1;
429   if (l > pd->acontent)
430     {
431       pd->content = realloc(pd->content, l + 256);
432       pd->acontent = l + 256;
433     }
434   c = pd->content + pd->lcontent;
435   pd->lcontent += len;
436   while (len-- > 0)
437     *c++ = *s++;
438   *c = 0;
439 }
440
441 #define BUFF_SIZE 8192
442
443 int
444 repo_add_repomdxml(Repo *repo, FILE *fp, int flags)
445 {
446   Pool *pool = repo->pool;
447   struct parsedata pd;
448   Repodata *data;
449   char buf[BUFF_SIZE];
450   int i, l;
451   struct stateswitch *sw;
452   XML_Parser parser;
453
454   data = repo_add_repodata(repo, flags);
455
456   memset(&pd, 0, sizeof(pd));
457   pd.timestamp = 0;
458   for (i = 0, sw = stateswitches; sw->from != NUMSTATES; i++, sw++)
459     {
460       if (!pd.swtab[sw->from])
461         pd.swtab[sw->from] = sw;
462       pd.sbtab[sw->to] = sw->from;
463     }
464   pd.pool = pool;
465   pd.repo = repo;
466   pd.data = data;
467
468   pd.content = malloc(256);
469   pd.acontent = 256;
470   pd.lcontent = 0;
471   parser = XML_ParserCreate(NULL);
472   XML_SetUserData(parser, &pd);
473   pd.parser = &parser;
474   XML_SetElementHandler(parser, startElement, endElement);
475   XML_SetCharacterDataHandler(parser, characterData);
476   for (;;)
477     {
478       l = fread(buf, 1, sizeof(buf), fp);
479       if (XML_Parse(parser, buf, l, l == 0) == XML_STATUS_ERROR)
480         {
481           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));
482           break;
483         }
484       if (l == 0)
485         break;
486     }
487   XML_ParserFree(parser);
488
489   if (!(flags & REPO_NO_INTERNALIZE))
490     repodata_internalize(data);
491
492   free(pd.content);
493   return pd.ret;
494 }
495
496 /* EOF */