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