Imported Upstream version 0.6.24
[platform/upstream/libsolv.git] / ext / repo_deltainfoxml.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_deltainfoxml.h"
23
24 /*
25  * <deltainfo>
26  *   <newpackage name="libtool" epoch="0" version="1.5.24" release="6.fc9" arch="i386">
27  *     <delta oldepoch="0" oldversion="1.5.24" oldrelease="3.fc8">
28  *       <filename>DRPMS/libtool-1.5.24-3.fc8_1.5.24-6.fc9.i386.drpm</filename>
29  *       <sequence>libtool-1.5.24-3.fc8-d3571f98b048b1a870e40241bb46c67ab4</sequence>
30  *       <size>22452</size>
31  *       <checksum type="sha">8f05394695dee9399c204614e21e5f6848990ab7</checksum>
32  *     </delta>
33  *     <delta oldepoch="0" oldversion="1.5.22" oldrelease="11.fc7">
34  *       <filename>DRPMS/libtool-1.5.22-11.fc7_1.5.24-6.fc9.i386.drpm</filename>
35  *        <sequence>libtool-1.5.22-11.fc7-e82691677eee1e83b4812572c5c9ce8eb</sequence>
36  *        <size>110362</size>
37  *        <checksum type="sha">326658fee45c0baec1e70231046dbaf560f941ce</checksum>
38  *      </delta>
39  *    </newpackage>
40  *  </deltainfo>
41  */
42
43 enum state {
44   STATE_START,
45   STATE_NEWPACKAGE,     /* 1 */
46   STATE_DELTA,          /* 2 */
47   STATE_FILENAME,       /* 3 */
48   STATE_SEQUENCE,       /* 4 */
49   STATE_SIZE,           /* 5 */
50   STATE_CHECKSUM,       /* 6 */
51   STATE_LOCATION,       /* 7 */
52   NUMSTATES
53 };
54
55 struct stateswitch {
56   enum state from;
57   char *ename;
58   enum state to;
59   int docontent;
60 };
61
62 /* !! must be sorted by first column !! */
63 static struct stateswitch stateswitches[] = {
64   /* compatibility with old yum-presto */
65   { STATE_START,       "prestodelta",     STATE_START, 0 },
66   { STATE_START,       "deltainfo",       STATE_START, 0 },
67   { STATE_START,       "newpackage",      STATE_NEWPACKAGE,  0 },
68   { STATE_NEWPACKAGE,  "delta",           STATE_DELTA,       0 },
69   /* compatibility with yum-presto */
70   { STATE_DELTA,       "filename",        STATE_FILENAME,    1 },
71   { STATE_DELTA,       "location",        STATE_LOCATION,    0 },
72   { STATE_DELTA,       "sequence",        STATE_SEQUENCE,    1 },
73   { STATE_DELTA,       "size",            STATE_SIZE,        1 },
74   { STATE_DELTA,       "checksum",        STATE_CHECKSUM,    1 },
75   { NUMSTATES }
76 };
77
78 /* Cumulated info about the current deltarpm or patchrpm */
79 struct deltarpm {
80   char *location;
81   char *locbase;
82   unsigned int buildtime;
83   unsigned long long downloadsize;
84   char *filechecksum;
85   int filechecksumtype;
86   /* Baseversion.  deltarpm only has one. */
87   Id *bevr;
88   unsigned nbevr;
89   Id seqname;
90   Id seqevr;
91   char *seqnum;
92 };
93
94 struct parsedata {
95   int ret;
96   int depth;
97   enum state state;
98   int statedepth;
99   char *content;
100   int lcontent;
101   int acontent;
102   int docontent;
103   Pool *pool;
104   Repo *repo;
105   Repodata *data;
106
107   struct stateswitch *swtab[NUMSTATES];
108   enum state sbtab[NUMSTATES];
109   struct deltarpm delta;
110   Id newpkgevr;
111   Id newpkgname;
112   Id newpkgarch;
113
114   Id *handles;
115   int nhandles;
116 };
117
118 /*
119  * find attribute
120  */
121
122 static const char *
123 find_attr(const char *txt, const char **atts)
124 {
125   for (; *atts; atts += 2)
126     {
127       if (!strcmp(*atts, txt))
128         return atts[1];
129     }
130   return 0;
131 }
132
133
134 /*
135  * create evr (as Id) from 'epoch', 'version' and 'release' attributes
136  */
137
138 static Id
139 makeevr_atts(Pool *pool, struct parsedata *pd, const char **atts)
140 {
141   const char *e, *v, *r, *v2;
142   char *c;
143   int l;
144
145   e = v = r = 0;
146   for (; *atts; atts += 2)
147     {
148       if (!strcmp(*atts, "oldepoch"))
149         e = atts[1];
150       else if (!strcmp(*atts, "epoch"))
151         e = atts[1];
152       else if (!strcmp(*atts, "version"))
153         v = atts[1];
154       else if (!strcmp(*atts, "oldversion"))
155         v = atts[1];
156       else if (!strcmp(*atts, "release"))
157         r = atts[1];
158       else if (!strcmp(*atts, "oldrelease"))
159         r = atts[1];
160     }
161   if (e && (!*e || !strcmp(e, "0")))
162     e = 0;
163   if (v && !e)
164     {
165       for (v2 = v; *v2 >= '0' && *v2 <= '9'; v2++)
166         ;
167       if (v2 > v && *v2 == ':')
168         e = "0";
169     }
170   l = 1;
171   if (e)
172     l += strlen(e) + 1;
173   if (v)
174     l += strlen(v);
175   if (r)
176     l += strlen(r) + 1;
177   if (l > pd->acontent)
178     {
179       pd->content = solv_realloc(pd->content, l + 256);
180       pd->acontent = l + 256;
181     }
182   c = pd->content;
183   if (e)
184     {
185       strcpy(c, e);
186       c += strlen(c);
187       *c++ = ':';
188     }
189   if (v)
190     {
191       strcpy(c, v);
192       c += strlen(c);
193     }
194   if (r)
195     {
196       *c++ = '-';
197       strcpy(c, r);
198       c += strlen(c);
199     }
200   *c = 0;
201   if (!*pd->content)
202     return 0;
203 #if 0
204   fprintf(stderr, "evr: %s\n", pd->content);
205 #endif
206   return pool_str2id(pool, pd->content, 1);
207 }
208
209 static void XMLCALL
210 startElement(void *userData, const char *name, const char **atts)
211 {
212   struct parsedata *pd = userData;
213   Pool *pool = pd->pool;
214   struct stateswitch *sw;
215   const char *str;
216
217 #if 0
218   fprintf(stderr, "start: [%d]%s\n", pd->state, name);
219 #endif
220   if (pd->depth != pd->statedepth)
221     {
222       pd->depth++;
223       return;
224     }
225
226   pd->depth++;
227   if (!pd->swtab[pd->state])
228     return;
229   for (sw = pd->swtab[pd->state]; sw->from == pd->state; sw++)  /* find name in statetable */
230     if (!strcmp(sw->ename, name))
231       break;
232   if (sw->from != pd->state)
233     {
234 #if 0
235       fprintf(stderr, "into unknown: [%d]%s (from: %d)\n", sw->to, name, sw->from);
236 #endif
237       return;
238     }
239   pd->state = sw->to;
240   pd->docontent = sw->docontent;
241   pd->statedepth = pd->depth;
242   pd->lcontent = 0;
243   *pd->content = 0;
244
245   switch(pd->state)
246     {
247     case STATE_START:
248       break;
249     case STATE_NEWPACKAGE:
250       if ((str = find_attr("name", atts)) != 0)
251         pd->newpkgname = pool_str2id(pool, str, 1);
252       pd->newpkgevr = makeevr_atts(pool, pd, atts);
253       if ((str = find_attr("arch", atts)) != 0)
254         pd->newpkgarch = pool_str2id(pool, str, 1);
255       break;
256
257     case STATE_DELTA:
258       memset(&pd->delta, 0, sizeof(pd->delta));
259       pd->delta.bevr = solv_extend(pd->delta.bevr, pd->delta.nbevr, 1, sizeof(Id), 7);
260       pd->delta.bevr[pd->delta.nbevr++] = makeevr_atts(pool, pd, atts);
261       break;
262     case STATE_FILENAME:
263       if ((str = find_attr("xml:base", atts)))
264         pd->delta.locbase = solv_strdup(str);
265       break;
266     case STATE_LOCATION:
267       pd->delta.location = solv_strdup(find_attr("href", atts));
268       if ((str = find_attr("xml:base", atts)))
269         pd->delta.locbase = solv_strdup(str);
270       break;
271     case STATE_SIZE:
272       break;
273     case STATE_CHECKSUM:
274       pd->delta.filechecksum = 0;
275       pd->delta.filechecksumtype = REPOKEY_TYPE_SHA1;
276       if ((str = find_attr("type", atts)) != 0)
277         {
278           pd->delta.filechecksumtype = solv_chksum_str2type(str);
279           if (!pd->delta.filechecksumtype)
280             pool_debug(pool, SOLV_ERROR, "unknown checksum type: '%s'\n", str);
281         }
282       break;
283     case STATE_SEQUENCE:
284       break;
285     default:
286       break;
287     }
288 }
289
290
291 static void XMLCALL
292 endElement(void *userData, const char *name)
293 {
294   struct parsedata *pd = userData;
295   Pool *pool = pd->pool;
296   const char *str;
297
298 #if 0
299   fprintf(stderr, "end: %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:
315       break;
316     case STATE_NEWPACKAGE:
317       break;
318     case STATE_DELTA:
319       {
320         /* read all data for a deltarpm. commit into attributes */
321         Id handle;
322         struct deltarpm *d = &pd->delta;
323
324         handle = repodata_new_handle(pd->data);
325         /* we commit all handles later on in one go so that the
326          * repodata code doesn't need to realloc every time */
327         pd->handles = solv_extend(pd->handles, pd->nhandles, 1, sizeof(Id), 63);
328         pd->handles[pd->nhandles++] = handle;
329         repodata_set_id(pd->data, handle, DELTA_PACKAGE_NAME, pd->newpkgname);
330         repodata_set_id(pd->data, handle, DELTA_PACKAGE_EVR, pd->newpkgevr);
331         repodata_set_id(pd->data, handle, DELTA_PACKAGE_ARCH, pd->newpkgarch);
332         if (d->location)
333           {
334             repodata_set_deltalocation(pd->data, handle, 0, 0, d->location);
335             if (d->locbase)
336               repodata_set_poolstr(pd->data, handle, DELTA_LOCATION_BASE, d->locbase);
337           }
338         if (d->downloadsize)
339           repodata_set_num(pd->data, handle, DELTA_DOWNLOADSIZE, d->downloadsize);
340         if (d->filechecksum)
341           repodata_set_checksum(pd->data, handle, DELTA_CHECKSUM, d->filechecksumtype, d->filechecksum);
342         if (d->seqnum)
343           {
344             repodata_set_id(pd->data, handle, DELTA_BASE_EVR, d->bevr[0]);
345             repodata_set_id(pd->data, handle, DELTA_SEQ_NAME, d->seqname);
346             repodata_set_id(pd->data, handle, DELTA_SEQ_EVR, d->seqevr);
347             /* should store as binary blob! */
348             repodata_set_str(pd->data, handle, DELTA_SEQ_NUM, d->seqnum);
349           }
350       }
351       pd->delta.filechecksum = solv_free(pd->delta.filechecksum);
352       pd->delta.bevr = solv_free(pd->delta.bevr);
353       pd->delta.nbevr = 0;
354       pd->delta.seqnum = solv_free(pd->delta.seqnum);
355       pd->delta.location = solv_free(pd->delta.location);
356       pd->delta.locbase = solv_free(pd->delta.locbase);
357       break;
358     case STATE_FILENAME:
359       pd->delta.location = solv_strdup(pd->content);
360       break;
361     case STATE_CHECKSUM:
362       pd->delta.filechecksum = solv_strdup(pd->content);
363       break;
364     case STATE_SIZE:
365       pd->delta.downloadsize = strtoull(pd->content, 0, 10);
366       break;
367     case STATE_SEQUENCE:
368       if ((str = pd->content))
369         {
370           const char *s1, *s2;
371           s1 = strrchr(str, '-');
372           if (s1)
373             {
374               for (s2 = s1 - 1; s2 > str; s2--)
375                 if (*s2 == '-')
376                   break;
377               if (*s2 == '-')
378                 {
379                   for (s2 = s2 - 1; s2 > str; s2--)
380                     if (*s2 == '-')
381                       break;
382                   if (*s2 == '-')
383                     {
384                       pd->delta.seqevr = pool_strn2id(pool, s2 + 1, s1 - s2 - 1, 1);
385                       pd->delta.seqname = pool_strn2id(pool, str, s2 - str, 1);
386                       str = s1 + 1;
387                     }
388                 }
389             }
390           pd->delta.seqnum = solv_strdup(str);
391       }
392     default:
393       break;
394     }
395
396   pd->state = pd->sbtab[pd->state];
397   pd->docontent = 0;
398 }
399
400
401 static void XMLCALL
402 characterData(void *userData, const XML_Char *s, int len)
403 {
404   struct parsedata *pd = userData;
405   int l;
406   char *c;
407   if (!pd->docontent)
408     return;
409   l = pd->lcontent + len + 1;
410   if (l > pd->acontent)
411     {
412       pd->content = solv_realloc(pd->content, l + 256);
413       pd->acontent = l + 256;
414     }
415   c = pd->content + pd->lcontent;
416   pd->lcontent += len;
417   while (len-- > 0)
418     *c++ = *s++;
419   *c = 0;
420 }
421
422 #define BUFF_SIZE 8192
423
424 int
425 repo_add_deltainfoxml(Repo *repo, FILE *fp, int flags)
426 {
427   Pool *pool = repo->pool;
428   struct parsedata pd;
429   char buf[BUFF_SIZE];
430   int i, l;
431   struct stateswitch *sw;
432   Repodata *data;
433   XML_Parser parser;
434
435   data = repo_add_repodata(repo, flags);
436
437   memset(&pd, 0, sizeof(pd));
438   for (i = 0, sw = stateswitches; sw->from != NUMSTATES; i++, sw++)
439     {
440       if (!pd.swtab[sw->from])
441         pd.swtab[sw->from] = sw;
442       pd.sbtab[sw->to] = sw->from;
443     }
444   pd.pool = pool;
445   pd.repo = repo;
446   pd.data = data;
447
448   pd.content = solv_malloc(256);
449   pd.acontent = 256;
450   pd.lcontent = 0;
451
452   parser = XML_ParserCreate(NULL);
453   XML_SetUserData(parser, &pd);
454   XML_SetElementHandler(parser, startElement, endElement);
455   XML_SetCharacterDataHandler(parser, characterData);
456   for (;;)
457     {
458       l = fread(buf, 1, sizeof(buf), fp);
459       if (XML_Parse(parser, buf, l, l == 0) == XML_STATUS_ERROR)
460         {
461           pd.ret = pool_error(pool, -1, "repo_updateinfoxml: %s at line %u:%u", XML_ErrorString(XML_GetErrorCode(parser)), (unsigned int)XML_GetCurrentLineNumber(parser), (unsigned int)XML_GetCurrentColumnNumber(parser));
462           break;
463         }
464       if (l == 0)
465         break;
466     }
467   XML_ParserFree(parser);
468   solv_free(pd.content);
469
470   /* now commit all handles */
471   if (!pd.ret)
472     for (i = 0; i < pd.nhandles; i++)
473       repodata_add_flexarray(pd.data, SOLVID_META, REPOSITORY_DELTAINFO, pd.handles[i]);
474   solv_free(pd.handles);
475
476   if (!(flags & REPO_NO_INTERNALIZE))
477     repodata_internalize(data);
478   return pd.ret;
479 }
480
481 /* EOF */