- more memory usage statistics
[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 buildtime;
85   unsigned downloadsize, archivesize;
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 depth;
98   enum state state;
99   int statedepth;
100   char *content;
101   int lcontent;
102   int acontent;
103   int docontent;
104   Pool *pool;
105   Repo *repo;
106   Repodata *data;
107
108   struct stateswitch *swtab[NUMSTATES];
109   enum state sbtab[NUMSTATES];
110   struct deltarpm delta;
111   Id newpkgevr;
112   Id newpkgname;
113   Id newpkgarch;
114
115   Id *handles;
116   int nhandles;
117 };
118
119 /*
120  * find attribute
121  */
122
123 static const char *
124 find_attr(const char *txt, const char **atts)
125 {
126   for (; *atts; atts += 2)
127     {
128       if (!strcmp(*atts, txt))
129         return atts[1];
130     }
131   return 0;
132 }
133
134
135 /*
136  * create evr (as Id) from 'epoch', 'version' and 'release' attributes
137  */
138
139 static Id
140 makeevr_atts(Pool *pool, struct parsedata *pd, const char **atts)
141 {
142   const char *e, *v, *r, *v2;
143   char *c;
144   int l;
145
146   e = v = r = 0;
147   for (; *atts; atts += 2)
148     {
149       if (!strcmp(*atts, "oldepoch"))
150         e = atts[1];
151       else if (!strcmp(*atts, "epoch"))
152         e = atts[1];
153       else if (!strcmp(*atts, "version"))
154         v = atts[1];
155       else if (!strcmp(*atts, "oldversion"))
156         v = atts[1];
157       else if (!strcmp(*atts, "release"))
158         r = atts[1];
159       else if (!strcmp(*atts, "oldrelease"))
160         r = atts[1];
161     }
162   if (e && !strcmp(e, "0"))
163     e = 0;
164   if (v && !e)
165     {
166       for (v2 = v; *v2 >= '0' && *v2 <= '9'; v2++)
167         ;
168       if (v2 > v && *v2 == ':')
169         e = "0";
170     }
171   l = 1;
172   if (e)
173     l += strlen(e) + 1;
174   if (v)
175     l += strlen(v);
176   if (r)
177     l += strlen(r) + 1;
178   if (l > pd->acontent)
179     {
180       pd->content = solv_realloc(pd->content, l + 256);
181       pd->acontent = l + 256;
182     }
183   c = pd->content;
184   if (e)
185     {
186       strcpy(c, e);
187       c += strlen(c);
188       *c++ = ':';
189     }
190   if (v)
191     {
192       strcpy(c, v);
193       c += strlen(c);
194     }
195   if (r)
196     {
197       *c++ = '-';
198       strcpy(c, r);
199       c += strlen(c);
200     }
201   *c = 0;
202   if (!*pd->content)
203     return 0;
204 #if 0
205   fprintf(stderr, "evr: %s\n", pd->content);
206 #endif
207   return pool_str2id(pool, pd->content, 1);
208 }
209
210 static void
211 parse_delta_location(struct parsedata *pd, const char* str)
212 {
213   Pool *pool = pd->pool;
214   if (str)
215     {
216       /* Separate the filename into its different parts.
217          rpm/x86_64/alsa-1.0.14-31_31.2.x86_64.delta.rpm
218          --> dir = rpm/x86_64
219          name = alsa
220          evr = 1.0.14-31_31.2
221          suffix = x86_64.delta.rpm.  */
222       char *real_str = solv_strdup(str);
223       char *s = real_str;
224       char *s1, *s2;
225       s1 = strrchr (s, '/');
226       if (s1)
227         {
228           pd->delta.locdir = pool_strn2id(pool, s, s1 - s, 1);
229           s = s1 + 1;
230         }
231       /* Guess suffix.  */
232       s1 = strrchr (s, '.');
233       if (s1)
234         {
235           for (s2 = s1 - 1; s2 > s; s2--)
236             if (*s2 == '.')
237               break;
238           if (!strcmp (s2, ".delta.rpm") || !strcmp (s2, ".patch.rpm"))
239             {
240               s1 = s2;
241               /* We accept one more item as suffix.  */
242               for (s2 = s1 - 1; s2 > s; s2--)
243                 if (*s2 == '.')
244                   break;
245               s1 = s2;
246             }
247           if (*s1 == '.')
248             *s1++ = 0;
249           pd->delta.locsuffix = pool_str2id(pool, s1, 1);
250         }
251       /* Last '-'.  */
252       s1 = strrchr (s, '-');
253       if (s1)
254         {
255           /* Second to last '-'.  */
256           for (s2 = s1 - 1; s2 > s; s2--)
257             if (*s2 == '-')
258               break;
259         }
260       else
261         s2 = 0;
262       if (s2 > s && *s2 == '-')
263         {
264           *s2++ = 0;
265           pd->delta.locevr = pool_str2id(pool, s2, 1);
266         }
267       pd->delta.locname = pool_str2id(pool, s, 1);
268       free(real_str);
269     }
270 }
271
272 static void XMLCALL
273 startElement(void *userData, const char *name, const char **atts)
274 {
275   struct parsedata *pd = userData;
276   Pool *pool = pd->pool;
277   struct stateswitch *sw;
278   const char *str;
279
280 #if 0
281   fprintf(stderr, "start: [%d]%s\n", pd->state, name);
282 #endif
283   if (pd->depth != pd->statedepth)
284     {
285       pd->depth++;
286       return;
287     }
288
289   pd->depth++;
290   if (!pd->swtab[pd->state])
291     return;
292   for (sw = pd->swtab[pd->state]; sw->from == pd->state; sw++)  /* find name in statetable */
293     if (!strcmp(sw->ename, name))
294       break;
295   if (sw->from != pd->state)
296     {
297 #if 0
298       fprintf(stderr, "into unknown: [%d]%s (from: %d)\n", sw->to, name, sw->from);
299       exit( 1 );
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 + 1023) / 1024);
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 = atoi(pd->content);
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           pool_debug(pool, SOLV_FATAL, "repo_updateinfoxml: %s at line %u:%u\n", XML_ErrorString(XML_GetErrorCode(parser)), (unsigned int)XML_GetCurrentLineNumber(parser), (unsigned int)XML_GetCurrentColumnNumber(parser));
518           exit(1);
519         }
520       if (l == 0)
521         break;
522     }
523   XML_ParserFree(parser);
524   solv_free(pd.content);
525
526   /* now commit all handles */
527   for (i = 0; i < pd.nhandles; i++)
528     repodata_add_flexarray(pd.data, SOLVID_META, REPOSITORY_DELTAINFO, pd.handles[i]);
529   solv_free(pd.handles);
530
531   if (!(flags & REPO_NO_INTERNALIZE))
532     repodata_internalize(data);
533   return 0;
534 }
535
536 /* EOF */