2 * Copyright (c) 2007, Novell Inc.
4 * This program is licensed under the BSD license, read LICENSE.BSD
5 * for further information
11 #include <sys/types.h>
21 #include "repo_updateinfoxml.h"
24 #include "tools_util.h"
30 * <newpackage name="libtool" epoch="0" version="1.5.24" release="6.fc9" arch="i386">
31 * <delta oldepoch="0" oldversion="1.5.24" oldrelease="3.fc8">
32 * <filename>DRPMS/libtool-1.5.24-3.fc8_1.5.24-6.fc9.i386.drpm</filename>
33 * <sequence>libtool-1.5.24-3.fc8-d3571f98b048b1a870e40241bb46c67ab4</sequence>
35 * <checksum type="sha">8f05394695dee9399c204614e21e5f6848990ab7</checksum>
37 * <delta oldepoch="0" oldversion="1.5.22" oldrelease="11.fc7">
38 * <filename>DRPMS/libtool-1.5.22-11.fc7_1.5.24-6.fc9.i386.drpm</filename>
39 * <sequence>libtool-1.5.22-11.fc7-e82691677eee1e83b4812572c5c9ce8eb</sequence>
41 * <checksum type="sha">326658fee45c0baec1e70231046dbaf560f941ce</checksum>
49 STATE_NEWPACKAGE, /* 1 */
51 STATE_FILENAME, /* 3 */
52 STATE_SEQUENCE, /* 4 */
54 STATE_CHECKSUM, /* 6 */
55 STATE_LOCATION, /* 7 */
66 /* !! must be sorted by first column !! */
67 static struct stateswitch stateswitches[] = {
68 /* compatibility with old yum-presto */
69 { STATE_START, "prestodelta", STATE_START, 0 },
70 { STATE_START, "deltainfo", STATE_START, 0 },
71 { STATE_START, "newpackage", STATE_NEWPACKAGE, 0 },
72 { STATE_NEWPACKAGE, "delta", STATE_DELTA, 0 },
73 /* compatibility with yum-presto */
74 { STATE_DELTA, "filename", STATE_FILENAME, 1 },
75 { STATE_DELTA, "location", STATE_LOCATION, 0 },
76 { STATE_DELTA, "sequence", STATE_SEQUENCE, 1 },
77 { STATE_DELTA, "size", STATE_SIZE, 1 },
78 { STATE_DELTA, "checksum", STATE_CHECKSUM, 1 },
82 /* Cumulated info about the current deltarpm or patchrpm */
89 unsigned downloadsize, archivesize;
92 /* Baseversion. deltarpm only has one. */
113 struct stateswitch *swtab[NUMSTATES];
114 enum state sbtab[NUMSTATES];
118 struct deltarpm delta;
129 find_attr(const char *txt, const char **atts)
131 for (; *atts; atts += 2)
133 if (!strcmp(*atts, txt))
141 * create evr (as Id) from 'epoch', 'version' and 'release' attributes
145 makeevr_atts(Pool *pool, struct parsedata *pd, const char **atts)
147 const char *e, *v, *r, *v2;
152 for (; *atts; atts += 2)
154 if (!strcmp(*atts, "oldepoch"))
156 else if (!strcmp(*atts, "epoch"))
158 else if (!strcmp(*atts, "version"))
160 else if (!strcmp(*atts, "oldversion"))
162 else if (!strcmp(*atts, "release"))
164 else if (!strcmp(*atts, "oldrelease"))
167 if (e && !strcmp(e, "0"))
171 for (v2 = v; *v2 >= '0' && *v2 <= '9'; v2++)
173 if (v2 > v && *v2 == ':')
183 if (l > pd->acontent)
185 pd->content = realloc(pd->content, l + 256);
186 pd->acontent = l + 256;
210 fprintf(stderr, "evr: %s\n", pd->content);
212 return str2id(pool, pd->content, 1);
215 static void parse_delta_location( struct parsedata *pd,
218 Pool *pool = pd->pool;
221 /* Separate the filename into its different parts.
222 rpm/x86_64/alsa-1.0.14-31_31.2.x86_64.delta.rpm
226 suffix = x86_64.delta.rpm. */
227 char *real_str = strdup(str);
230 s1 = strrchr (s, '/');
233 pd->delta.locdir = strn2id(pool, s, s1 - s, 1);
237 s1 = strrchr (s, '.');
240 for (s2 = s1 - 1; s2 > s; s2--)
243 if (!strcmp (s2, ".delta.rpm") || !strcmp (s2, ".patch.rpm"))
246 /* We accept one more item as suffix. */
247 for (s2 = s1 - 1; s2 > s; s2--)
254 pd->delta.locsuffix = str2id(pool, s1, 1);
257 s1 = strrchr (s, '-');
260 /* Second to last '-'. */
261 for (s2 = s1 - 1; s2 > s; s2--)
267 if (s2 > s && *s2 == '-')
270 pd->delta.locevr = str2id(pool, s2, 1);
272 pd->delta.locname = str2id(pool, s, 1);
278 startElement(void *userData, const char *name, const char **atts)
280 struct parsedata *pd = userData;
281 Pool *pool = pd->pool;
282 struct stateswitch *sw;
286 fprintf(stderr, "start: [%d]%s\n", pd->state, name);
288 if (pd->depth != pd->statedepth)
295 if (!pd->swtab[pd->state])
297 for (sw = pd->swtab[pd->state]; sw->from == pd->state; sw++) /* find name in statetable */
298 if (!strcmp(sw->ename, name))
300 if (sw->from != pd->state)
303 fprintf(stderr, "into unknown: [%d]%s (from: %d)\n", sw->to, name, sw->from);
309 pd->docontent = sw->docontent;
310 pd->statedepth = pd->depth;
318 case STATE_NEWPACKAGE:
319 if ( (str = find_attr("name", atts)) )
321 pd->newpkgname = str2id(pool, str, 1);
323 pd->newpkgevr = makeevr_atts(pool, pd, atts);
324 if ( (str = find_attr("arch", atts)) )
326 pd->newpkgarch = str2id(pool, str, 1);
331 memset(&pd->delta, 0, sizeof (pd->delta));
335 pd->delta.bevr = sat_realloc (pd->delta.bevr, pd->delta.nbevr * sizeof(Id));
336 pd->delta.bevr[pd->delta.nbevr - 1] = makeevr_atts(pool, pd, atts);
342 parse_delta_location(pd, find_attr("href", atts));
355 endElement(void *userData, const char *name)
357 struct parsedata *pd = userData;
358 Pool *pool = pd->pool;
362 fprintf(stderr, "end: %s\n", name);
364 if (pd->depth != pd->statedepth)
368 fprintf(stderr, "back from unknown %d %d %d\n", pd->state, pd->depth, pd->statedepth);
379 case STATE_NEWPACKAGE:
386 struct deltarpm *d = &pd->delta;
390 fprintf (stderr, "found deltarpm for %s:\n", id2str(pool, pd->newpkgname));
392 repo_set_id(pd->repo, pd->datanum, DELTA_PACKAGE_NAME, pd->newpkgname);
393 repo_set_id(pd->repo, pd->datanum, DELTA_PACKAGE_EVR, pd->newpkgevr);
394 repo_set_id(pd->repo, pd->datanum, DELTA_PACKAGE_ARCH, pd->newpkgarch);
395 repo_set_id(pd->repo, pd->datanum, DELTA_LOCATION_NAME, d->locname);
396 repo_set_id(pd->repo, pd->datanum, DELTA_LOCATION_DIR, d->locdir);
397 repo_set_id(pd->repo, pd->datanum, DELTA_LOCATION_EVR, d->locevr);
398 repo_set_id(pd->repo, pd->datanum, DELTA_LOCATION_SUFFIX, d->locsuffix);
401 fprintf (stderr, " loc: %s %s %s %s\n", id2str(pool, d->locdir),
402 id2str(pool, d->locname), id2str(pool, d->locevr),
403 id2str(pool, d->locsuffix));
404 fprintf (stderr, " size: %d down\n", d->downloadsize);
405 fprintf (stderr, " chek: %s\n", d->filechecksum);
408 repo_set_num(pd->repo, pd->datanum, DELTA_DOWNLOADSIZE, d->downloadsize);
409 repo_set_str(pd->repo, pd->datanum, DELTA_CHECKSUM, d->filechecksum);
414 fprintf (stderr, " base: %s\n",
415 id2str(pool, d->bevr[0]));
416 fprintf (stderr, " seq: %s\n",
417 id2str(pool, d->seqname));
418 fprintf (stderr, " %s\n",
419 id2str(pool, d->seqevr));
420 fprintf (stderr, " %s\n",
423 repo_set_id(pd->repo, pd->datanum, DELTA_BASE_EVR, d->bevr[0]);
424 repo_set_id(pd->repo, pd->datanum, DELTA_SEQ_NAME, d->seqname);
425 repo_set_id(pd->repo, pd->datanum, DELTA_SEQ_EVR, d->seqevr);
426 repo_set_str(pd->repo, pd->datanum, DELTA_SEQ_NUM, d->seqnum);
429 fprintf(stderr, "OK\n");
433 if (d->seqevr != d->bevr[0])
434 fprintf (stderr, "XXXXX evr\n");
435 /* Name of package ("atom:xxxx") should match the sequence info
437 if (strcmp(id2str(pool, d->seqname), id2str(pool, pd->newpkgname) + 5))
438 fprintf (stderr, "XXXXX name\n");
445 fprintf (stderr, " base:");
446 for (i = 0; i < d->nbevr; i++)
447 fprintf (stderr, " %s", id2str(pool, d->bevr[i]));
448 fprintf (stderr, "\n");
453 free(pd->delta.filechecksum);
454 free(pd->delta.bevr);
455 free(pd->delta.seqnum);
458 parse_delta_location(pd, pd->content);
461 pd->delta.filechecksum = strdup(pd->content);
464 pd->delta.downloadsize = atoi(pd->content);
467 if ((str = pd->content))
470 s1 = strrchr(str, '-');
473 for (s2 = s1 - 1; s2 > str; s2--)
478 for (s2 = s2 - 1; s2 > str; s2--)
483 pd->delta.seqevr = strn2id(pool, s2 + 1, s1 - s2 - 1, 1);
484 pd->delta.seqname = strn2id(pool, str, s2 - str, 1);
489 pd->delta.seqnum = strdup(str);
495 pd->state = pd->sbtab[pd->state];
501 characterData(void *userData, const XML_Char *s, int len)
503 struct parsedata *pd = userData;
508 l = pd->lcontent + len + 1;
509 if (l > pd->acontent)
511 pd->content = realloc(pd->content, l + 256);
512 pd->acontent = l + 256;
514 c = pd->content + pd->lcontent;
521 #define BUFF_SIZE 8192
524 repo_add_deltainfoxml(Repo *repo, FILE *fp, int flags)
526 Pool *pool = repo->pool;
530 struct stateswitch *sw;
532 memset(&pd, 0, sizeof(pd));
533 for (i = 0, sw = stateswitches; sw->from != NUMSTATES; i++, sw++)
535 if (!pd.swtab[sw->from])
536 pd.swtab[sw->from] = sw;
537 pd.sbtab[sw->to] = sw->from;
541 pd.data = repo_add_repodata(pd.repo, 0);
543 pd.content = malloc(256);
546 pd.tempstr = malloc(256);
550 XML_Parser parser = XML_ParserCreate(NULL);
551 XML_SetUserData(parser, &pd);
552 XML_SetElementHandler(parser, startElement, endElement);
553 XML_SetCharacterDataHandler(parser, characterData);
556 l = fread(buf, 1, sizeof(buf), fp);
557 if (XML_Parse(parser, buf, l, l == 0) == XML_STATUS_ERROR)
559 fprintf(stderr, "repo_updateinfoxml: %s at line %u:%u\n", XML_ErrorString(XML_GetErrorCode(parser)), (unsigned int)XML_GetCurrentLineNumber(parser), (unsigned int)XML_GetCurrentColumnNumber(parser));
565 XML_ParserFree(parser);
568 repodata_internalize(pd.data);