cosmetic: fix possibly uninitialized variable warning in solv.c.
[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   Id locdir;
81   Id locname;
82   Id locevr;
83   Id locsuffix;
84   unsigned int buildtime;
85   unsigned long long downloadsize;
86   char *filechecksum;
87   int filechecksumtype;
88   /* Baseversion.  deltarpm only has one. */
89   Id *bevr;
90   unsigned nbevr;
91   Id seqname;
92   Id seqevr;
93   char *seqnum;
94 };
95
96 struct parsedata {
97   int ret;
98   int depth;
99   enum state state;
100   int statedepth;
101   char *content;
102   int lcontent;
103   int acontent;
104   int docontent;
105   Pool *pool;
106   Repo *repo;
107   Repodata *data;
108
109   struct stateswitch *swtab[NUMSTATES];
110   enum state sbtab[NUMSTATES];
111   struct deltarpm delta;
112   Id newpkgevr;
113   Id newpkgname;
114   Id newpkgarch;
115
116   Id *handles;
117   int nhandles;
118 };
119
120 /*
121  * find attribute
122  */
123
124 static const char *
125 find_attr(const char *txt, const char **atts)
126 {
127   for (; *atts; atts += 2)
128     {
129       if (!strcmp(*atts, txt))
130         return atts[1];
131     }
132   return 0;
133 }
134
135
136 /*
137  * create evr (as Id) from 'epoch', 'version' and 'release' attributes
138  */
139
140 static Id
141 makeevr_atts(Pool *pool, struct parsedata *pd, const char **atts)
142 {
143   const char *e, *v, *r, *v2;
144   char *c;
145   int l;
146
147   e = v = r = 0;
148   for (; *atts; atts += 2)
149     {
150       if (!strcmp(*atts, "oldepoch"))
151         e = atts[1];
152       else if (!strcmp(*atts, "epoch"))
153         e = atts[1];
154       else if (!strcmp(*atts, "version"))
155         v = atts[1];
156       else if (!strcmp(*atts, "oldversion"))
157         v = atts[1];
158       else if (!strcmp(*atts, "release"))
159         r = atts[1];
160       else if (!strcmp(*atts, "oldrelease"))
161         r = atts[1];
162     }
163   if (e && !strcmp(e, "0"))
164     e = 0;
165   if (v && !e)
166     {
167       for (v2 = v; *v2 >= '0' && *v2 <= '9'; v2++)
168         ;
169       if (v2 > v && *v2 == ':')
170         e = "0";
171     }
172   l = 1;
173   if (e)
174     l += strlen(e) + 1;
175   if (v)
176     l += strlen(v);
177   if (r)
178     l += strlen(r) + 1;
179   if (l > pd->acontent)
180     {
181       pd->content = solv_realloc(pd->content, l + 256);
182       pd->acontent = l + 256;
183     }
184   c = pd->content;
185   if (e)
186     {
187       strcpy(c, e);
188       c += strlen(c);
189       *c++ = ':';
190     }
191   if (v)
192     {
193       strcpy(c, v);
194       c += strlen(c);
195     }
196   if (r)
197     {
198       *c++ = '-';
199       strcpy(c, r);
200       c += strlen(c);
201     }
202   *c = 0;
203   if (!*pd->content)
204     return 0;
205 #if 0
206   fprintf(stderr, "evr: %s\n", pd->content);
207 #endif
208   return pool_str2id(pool, pd->content, 1);
209 }
210
211 static void
212 parse_delta_location(struct parsedata *pd, const char* str)
213 {
214   Pool *pool = pd->pool;
215   if (str)
216     {
217       /* Separate the filename into its different parts.
218          rpm/x86_64/alsa-1.0.14-31_31.2.x86_64.delta.rpm
219          --> dir = rpm/x86_64
220          name = alsa
221          evr = 1.0.14-31_31.2
222          suffix = x86_64.delta.rpm.  */
223       char *real_str = solv_strdup(str);
224       char *s = real_str;
225       char *s1, *s2;
226       s1 = strrchr (s, '/');
227       if (s1)
228         {
229           pd->delta.locdir = pool_strn2id(pool, s, s1 - s, 1);
230           s = s1 + 1;
231         }
232       /* Guess suffix.  */
233       s1 = strrchr (s, '.');
234       if (s1)
235         {
236           for (s2 = s1 - 1; s2 > s; s2--)
237             if (*s2 == '.')
238               break;
239           if (!strcmp (s2, ".delta.rpm") || !strcmp (s2, ".patch.rpm"))
240             {
241               s1 = s2;
242               /* We accept one more item as suffix.  */
243               for (s2 = s1 - 1; s2 > s; s2--)
244                 if (*s2 == '.')
245                   break;
246               s1 = s2;
247             }
248           if (*s1 == '.')
249             *s1++ = 0;
250           pd->delta.locsuffix = pool_str2id(pool, s1, 1);
251         }
252       /* Last '-'.  */
253       s1 = strrchr (s, '-');
254       if (s1)
255         {
256           /* Second to last '-'.  */
257           for (s2 = s1 - 1; s2 > s; s2--)
258             if (*s2 == '-')
259               break;
260         }
261       else
262         s2 = 0;
263       if (s2 > s && *s2 == '-')
264         {
265           *s2++ = 0;
266           pd->delta.locevr = pool_str2id(pool, s2, 1);
267         }
268       pd->delta.locname = pool_str2id(pool, s, 1);
269       free(real_str);
270     }
271 }
272
273 static void XMLCALL
274 startElement(void *userData, const char *name, const char **atts)
275 {
276   struct parsedata *pd = userData;
277   Pool *pool = pd->pool;
278   struct stateswitch *sw;
279   const char *str;
280
281 #if 0
282   fprintf(stderr, "start: [%d]%s\n", pd->state, name);
283 #endif
284   if (pd->depth != pd->statedepth)
285     {
286       pd->depth++;
287       return;
288     }
289
290   pd->depth++;
291   if (!pd->swtab[pd->state])
292     return;
293   for (sw = pd->swtab[pd->state]; sw->from == pd->state; sw++)  /* find name in statetable */
294     if (!strcmp(sw->ename, name))
295       break;
296   if (sw->from != pd->state)
297     {
298 #if 0
299       fprintf(stderr, "into unknown: [%d]%s (from: %d)\n", sw->to, name, sw->from);
300 #endif
301       return;
302     }
303   pd->state = sw->to;
304   pd->docontent = sw->docontent;
305   pd->statedepth = pd->depth;
306   pd->lcontent = 0;
307   *pd->content = 0;
308
309   switch(pd->state)
310     {
311     case STATE_START:
312       break;
313     case STATE_NEWPACKAGE:
314       if ((str = find_attr("name", atts)) != 0)
315         pd->newpkgname = pool_str2id(pool, str, 1);
316       pd->newpkgevr = makeevr_atts(pool, pd, atts);
317       if ((str = find_attr("arch", atts)) != 0)
318         pd->newpkgarch = pool_str2id(pool, str, 1);
319       break;
320
321     case STATE_DELTA:
322       memset(&pd->delta, 0, sizeof(pd->delta));
323       pd->delta.bevr = solv_extend(pd->delta.bevr, pd->delta.nbevr, 1, sizeof(Id), 7);
324       pd->delta.bevr[pd->delta.nbevr++] = makeevr_atts(pool, pd, atts);
325       break;
326     case STATE_FILENAME:
327       break;
328     case STATE_LOCATION:
329       parse_delta_location(pd, find_attr("href", atts));
330       break;
331     case STATE_SIZE:
332       break;
333     case STATE_CHECKSUM:
334       pd->delta.filechecksum = 0;
335       pd->delta.filechecksumtype = REPOKEY_TYPE_SHA1;
336       if ((str = find_attr("type", atts)) != 0)
337         {
338           pd->delta.filechecksumtype = solv_chksum_str2type(str);
339           if (!pd->delta.filechecksumtype)
340             pool_debug(pool, SOLV_ERROR, "unknown checksum type: '%s'\n", str);
341         }
342       break;
343     case STATE_SEQUENCE:
344       break;
345     default:
346       break;
347     }
348 }
349
350
351 static void XMLCALL
352 endElement(void *userData, const char *name)
353 {
354   struct parsedata *pd = userData;
355   Pool *pool = pd->pool;
356   const char *str;
357
358 #if 0
359   fprintf(stderr, "end: %s\n", name);
360 #endif
361   if (pd->depth != pd->statedepth)
362     {
363       pd->depth--;
364 #if 0
365       fprintf(stderr, "back from unknown %d %d %d\n", pd->state, pd->depth, pd->statedepth);
366 #endif
367       return;
368     }
369
370   pd->depth--;
371   pd->statedepth--;
372   switch (pd->state)
373     {
374     case STATE_START:
375       break;
376     case STATE_NEWPACKAGE:
377       break;
378     case STATE_DELTA:
379       {
380         /* read all data for a deltarpm. commit into attributes */
381         Id handle;
382         struct deltarpm *d = &pd->delta;
383
384         handle = repodata_new_handle(pd->data);
385         /* we commit all handles later on in one go so that the
386          * repodata code doesn't need to realloc every time */
387         pd->handles = solv_extend(pd->handles, pd->nhandles, 1, sizeof(Id), 63);
388         pd->handles[pd->nhandles++] = handle;
389         repodata_set_id(pd->data, handle, DELTA_PACKAGE_NAME, pd->newpkgname);
390         repodata_set_id(pd->data, handle, DELTA_PACKAGE_EVR, pd->newpkgevr);
391         repodata_set_id(pd->data, handle, DELTA_PACKAGE_ARCH, pd->newpkgarch);
392         repodata_set_id(pd->data, handle, DELTA_LOCATION_NAME, d->locname);
393         repodata_set_id(pd->data, handle, DELTA_LOCATION_DIR, d->locdir);
394         repodata_set_id(pd->data, handle, DELTA_LOCATION_EVR, d->locevr);
395         repodata_set_id(pd->data, handle, DELTA_LOCATION_SUFFIX, d->locsuffix);
396         if (d->downloadsize)
397           repodata_set_num(pd->data, handle, DELTA_DOWNLOADSIZE, d->downloadsize);
398         if (d->filechecksum)
399           repodata_set_checksum(pd->data, handle, DELTA_CHECKSUM, d->filechecksumtype, d->filechecksum);
400         if (d->seqnum)
401           {
402             repodata_set_id(pd->data, handle, DELTA_BASE_EVR, d->bevr[0]);
403             repodata_set_id(pd->data, handle, DELTA_SEQ_NAME, d->seqname);
404             repodata_set_id(pd->data, handle, DELTA_SEQ_EVR, d->seqevr);
405             /* should store as binary blob! */
406             repodata_set_str(pd->data, handle, DELTA_SEQ_NUM, d->seqnum);
407           }
408       }
409       pd->delta.filechecksum = solv_free(pd->delta.filechecksum);
410       pd->delta.bevr = solv_free(pd->delta.bevr);
411       pd->delta.nbevr = 0;
412       pd->delta.seqnum = solv_free(pd->delta.seqnum);
413       break;
414     case STATE_FILENAME:
415       parse_delta_location(pd, pd->content);
416       break;
417     case STATE_CHECKSUM:
418       pd->delta.filechecksum = solv_strdup(pd->content);
419       break;
420     case STATE_SIZE:
421       pd->delta.downloadsize = strtoull(pd->content, 0, 10);
422       break;
423     case STATE_SEQUENCE:
424       if ((str = pd->content))
425         {
426           const char *s1, *s2;
427           s1 = strrchr(str, '-');
428           if (s1)
429             {
430               for (s2 = s1 - 1; s2 > str; s2--)
431                 if (*s2 == '-')
432                   break;
433               if (*s2 == '-')
434                 {
435                   for (s2 = s2 - 1; s2 > str; s2--)
436                     if (*s2 == '-')
437                       break;
438                   if (*s2 == '-')
439                     {
440                       pd->delta.seqevr = pool_strn2id(pool, s2 + 1, s1 - s2 - 1, 1);
441                       pd->delta.seqname = pool_strn2id(pool, str, s2 - str, 1);
442                       str = s1 + 1;
443                     }
444                 }
445             }
446           pd->delta.seqnum = solv_strdup(str);
447       }
448     default:
449       break;
450     }
451
452   pd->state = pd->sbtab[pd->state];
453   pd->docontent = 0;
454 }
455
456
457 static void XMLCALL
458 characterData(void *userData, const XML_Char *s, int len)
459 {
460   struct parsedata *pd = userData;
461   int l;
462   char *c;
463   if (!pd->docontent)
464     return;
465   l = pd->lcontent + len + 1;
466   if (l > pd->acontent)
467     {
468       pd->content = solv_realloc(pd->content, l + 256);
469       pd->acontent = l + 256;
470     }
471   c = pd->content + pd->lcontent;
472   pd->lcontent += len;
473   while (len-- > 0)
474     *c++ = *s++;
475   *c = 0;
476 }
477
478 #define BUFF_SIZE 8192
479
480 int
481 repo_add_deltainfoxml(Repo *repo, FILE *fp, int flags)
482 {
483   Pool *pool = repo->pool;
484   struct parsedata pd;
485   char buf[BUFF_SIZE];
486   int i, l;
487   struct stateswitch *sw;
488   Repodata *data;
489   XML_Parser parser;
490
491   data = repo_add_repodata(repo, flags);
492
493   memset(&pd, 0, sizeof(pd));
494   for (i = 0, sw = stateswitches; sw->from != NUMSTATES; i++, sw++)
495     {
496       if (!pd.swtab[sw->from])
497         pd.swtab[sw->from] = sw;
498       pd.sbtab[sw->to] = sw->from;
499     }
500   pd.pool = pool;
501   pd.repo = repo;
502   pd.data = data;
503
504   pd.content = solv_malloc(256);
505   pd.acontent = 256;
506   pd.lcontent = 0;
507
508   parser = XML_ParserCreate(NULL);
509   XML_SetUserData(parser, &pd);
510   XML_SetElementHandler(parser, startElement, endElement);
511   XML_SetCharacterDataHandler(parser, characterData);
512   for (;;)
513     {
514       l = fread(buf, 1, sizeof(buf), fp);
515       if (XML_Parse(parser, buf, l, l == 0) == XML_STATUS_ERROR)
516         {
517           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));
518           break;
519         }
520       if (l == 0)
521         break;
522     }
523   XML_ParserFree(parser);
524   solv_free(pd.content);
525
526   /* now commit all handles */
527   if (!pd.ret)
528     for (i = 0; i < pd.nhandles; i++)
529       repodata_add_flexarray(pd.data, SOLVID_META, REPOSITORY_DELTAINFO, pd.handles[i]);
530   solv_free(pd.handles);
531
532   if (!(flags & REPO_NO_INTERNALIZE))
533     repodata_internalize(data);
534   return pd.ret;
535 }
536
537 /* EOF */