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