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"
22 #include "tools_util.h"
28 * <newpackage name="libtool" epoch="0" version="1.5.24" release="6.fc9" arch="i386">
29 * <delta oldepoch="0" oldversion="1.5.24" oldrelease="3.fc8">
30 * <filename>DRPMS/libtool-1.5.24-3.fc8_1.5.24-6.fc9.i386.drpm</filename>
31 * <sequence>libtool-1.5.24-3.fc8-d3571f98b048b1a870e40241bb46c67ab4</sequence>
33 * <checksum type="sha">8f05394695dee9399c204614e21e5f6848990ab7</checksum>
35 * <delta oldepoch="0" oldversion="1.5.22" oldrelease="11.fc7">
36 * <filename>DRPMS/libtool-1.5.22-11.fc7_1.5.24-6.fc9.i386.drpm</filename>
37 * <sequence>libtool-1.5.22-11.fc7-e82691677eee1e83b4812572c5c9ce8eb</sequence>
39 * <checksum type="sha">326658fee45c0baec1e70231046dbaf560f941ce</checksum>
47 STATE_NEWPACKAGE, /* 1 */
49 STATE_FILENAME, /* 3 */
50 STATE_SEQUENCE, /* 4 */
52 STATE_CHECKSUM, /* 6 */
53 STATE_LOCATION, /* 7 */
64 /* !! must be sorted by first column !! */
65 static struct stateswitch stateswitches[] = {
66 /* compatibility with old yum-presto */
67 { STATE_START, "prestodelta", STATE_START, 0 },
68 { STATE_START, "deltainfo", STATE_START, 0 },
69 { STATE_START, "newpackage", STATE_NEWPACKAGE, 0 },
70 { STATE_NEWPACKAGE, "delta", STATE_DELTA, 0 },
71 /* compatibility with yum-presto */
72 { STATE_DELTA, "filename", STATE_FILENAME, 1 },
73 { STATE_DELTA, "location", STATE_LOCATION, 0 },
74 { STATE_DELTA, "sequence", STATE_SEQUENCE, 1 },
75 { STATE_DELTA, "size", STATE_SIZE, 1 },
76 { STATE_DELTA, "checksum", STATE_CHECKSUM, 1 },
80 /* Cumulated info about the current deltarpm or patchrpm */
87 unsigned downloadsize, archivesize;
90 /* Baseversion. deltarpm only has one. */
111 struct stateswitch *swtab[NUMSTATES];
112 enum state sbtab[NUMSTATES];
116 struct deltarpm delta;
126 find_attr(const char *txt, const char **atts)
128 for (; *atts; atts += 2)
130 if (!strcmp(*atts, txt))
138 * create evr (as Id) from 'epoch', 'version' and 'release' attributes
142 makeevr_atts(Pool *pool, struct parsedata *pd, const char **atts)
144 const char *e, *v, *r, *v2;
149 for (; *atts; atts += 2)
151 if (!strcmp(*atts, "oldepoch"))
153 else if (!strcmp(*atts, "epoch"))
155 else if (!strcmp(*atts, "version"))
157 else if (!strcmp(*atts, "oldversion"))
159 else if (!strcmp(*atts, "release"))
161 else if (!strcmp(*atts, "oldrelease"))
164 if (e && !strcmp(e, "0"))
168 for (v2 = v; *v2 >= '0' && *v2 <= '9'; v2++)
170 if (v2 > v && *v2 == ':')
180 if (l > pd->acontent)
182 pd->content = realloc(pd->content, l + 256);
183 pd->acontent = l + 256;
207 fprintf(stderr, "evr: %s\n", pd->content);
209 return str2id(pool, pd->content, 1);
212 static void parse_delta_location( struct parsedata *pd,
215 Pool *pool = pd->pool;
218 /* Separate the filename into its different parts.
219 rpm/x86_64/alsa-1.0.14-31_31.2.x86_64.delta.rpm
223 suffix = x86_64.delta.rpm. */
224 char *real_str = strdup(str);
227 s1 = strrchr (s, '/');
230 pd->delta.locdir = strn2id(pool, s, s1 - s, 1);
234 s1 = strrchr (s, '.');
237 for (s2 = s1 - 1; s2 > s; s2--)
240 if (!strcmp (s2, ".delta.rpm") || !strcmp (s2, ".patch.rpm"))
243 /* We accept one more item as suffix. */
244 for (s2 = s1 - 1; s2 > s; s2--)
251 pd->delta.locsuffix = str2id(pool, s1, 1);
254 s1 = strrchr (s, '-');
257 /* Second to last '-'. */
258 for (s2 = s1 - 1; s2 > s; s2--)
264 if (s2 > s && *s2 == '-')
267 pd->delta.locevr = str2id(pool, s2, 1);
269 pd->delta.locname = str2id(pool, s, 1);
275 startElement(void *userData, const char *name, const char **atts)
277 struct parsedata *pd = userData;
278 Pool *pool = pd->pool;
279 struct stateswitch *sw;
283 fprintf(stderr, "start: [%d]%s\n", pd->state, name);
285 if (pd->depth != pd->statedepth)
292 for (sw = pd->swtab[pd->state]; sw->from == pd->state; sw++) /* find name in statetable */
293 if (!strcmp(sw->ename, name))
296 if (sw->from != pd->state)
299 fprintf(stderr, "into unknown: [%d]%s (from: %d)\n", sw->to, name, sw->from);
305 pd->docontent = sw->docontent;
306 pd->statedepth = pd->depth;
314 case STATE_NEWPACKAGE:
315 if ( (str = find_attr("name", atts)) )
317 pd->newpkgname = str2id(pool, str, 1);
322 memset(&pd->delta, 0, sizeof (pd->delta));
326 pd->delta.bevr = sat_realloc (pd->delta.bevr, pd->delta.nbevr * sizeof(Id));
327 pd->delta.bevr[pd->delta.nbevr - 1] = makeevr_atts(pool, pd, atts);
333 parse_delta_location( pd, find_attr("href", atts));
341 split(NULL, NULL, 0); /* just to keep gcc happy about tools_util.h: static ... split() {...} Urgs!*/
351 endElement(void *userData, const char *name)
353 struct parsedata *pd = userData;
354 Pool *pool = pd->pool;
358 fprintf(stderr, "end: %s\n", name);
360 if (pd->depth != pd->statedepth)
364 fprintf(stderr, "back from unknown %d %d %d\n", pd->state, pd->depth, pd->statedepth);
375 case STATE_NEWPACKAGE:
382 struct deltarpm *d = &pd->delta;
386 fprintf (stderr, "found deltarpm for %s:\n", id2str(pool, pd->newpkgname));
388 repo_set_id(pd->repo, pd->datanum, DELTA_PACKAGE_NAME, pd->newpkgname);
389 repo_set_id(pd->repo, pd->datanum, DELTA_LOCATION_NAME, d->locname);
390 repo_set_id(pd->repo, pd->datanum, DELTA_LOCATION_DIR, d->locdir);
391 repo_set_id(pd->repo, pd->datanum, DELTA_LOCATION_EVR, d->locevr);
392 repo_set_id(pd->repo, pd->datanum, DELTA_LOCATION_SUFFIX, d->locsuffix);
395 fprintf (stderr, " loc: %s %s %s %s\n", id2str(pool, d->locdir),
396 id2str(pool, d->locname), id2str(pool, d->locevr),
397 id2str(pool, d->locsuffix));
398 fprintf (stderr, " size: %d down\n", d->downloadsize);
399 fprintf (stderr, " chek: %s\n", d->filechecksum);
402 repo_set_num(pd->repo, pd->datanum, DELTA_DOWNLOADSIZE, d->downloadsize);
403 repo_set_str(pd->repo, pd->datanum, DELTA_CHECKSUM, d->filechecksum);
409 fprintf (stderr, " base: %s\n",
410 id2str(pool, d->bevr[0]));
411 fprintf (stderr, " seq: %s\n",
412 id2str(pool, d->seqname));
413 fprintf (stderr, " %s\n",
414 id2str(pool, d->seqevr));
415 fprintf (stderr, " %s\n",
418 repo_set_id(pd->repo, pd->datanum, DELTA_BASE_EVR, d->bevr[0]);
419 repo_set_id(pd->repo, pd->datanum, DELTA_SEQ_NAME, d->seqname);
420 repo_set_id(pd->repo, pd->datanum, DELTA_SEQ_EVR, d->seqevr);
421 repo_set_str(pd->repo, pd->datanum, DELTA_SEQ_NUM, d->seqnum);
424 fprintf(stderr, "OK\n");
428 if (d->seqevr != d->bevr[0])
429 fprintf (stderr, "XXXXX evr\n");
430 /* Name of package ("atom:xxxx") should match the sequence info
432 if (strcmp(id2str(pool, d->seqname), id2str(pool, pd->newpkgname) + 5))
433 fprintf (stderr, "XXXXX name\n");
440 fprintf (stderr, " base:");
441 for (i = 0; i < d->nbevr; i++)
442 fprintf (stderr, " %s", id2str(pool, d->bevr[i]));
443 fprintf (stderr, "\n");
448 free(pd->delta.filechecksum);
449 free(pd->delta.bevr);
450 free(pd->delta.seqnum);
453 parse_delta_location(pd, pd->content);
456 pd->delta.filechecksum = strdup(pd->content);
459 pd->delta.downloadsize = atoi(pd->content);
462 if ((str = pd->content))
465 s1 = strrchr(str, '-');
468 for (s2 = s1 - 1; s2 > str; s2--)
473 for (s2 = s2 - 1; s2 > str; s2--)
478 pd->delta.seqevr = strn2id(pool, s2 + 1, s1 - s2 - 1, 1);
479 pd->delta.seqname = strn2id(pool, str, s2 - str, 1);
484 pd->delta.seqnum = strdup(str);
490 pd->state = pd->sbtab[pd->state];
498 characterData(void *userData, const XML_Char *s, int len)
500 struct parsedata *pd = userData;
503 if (!pd->docontent) {
505 char *dup = strndup( s, len );
506 fprintf(stderr, "Content: [%d]'%s'\n", pd->state, dup );
511 l = pd->lcontent + len + 1;
512 if (l > pd->acontent)
514 pd->content = realloc(pd->content, l + 256);
515 pd->acontent = l + 256;
517 c = pd->content + pd->lcontent;
524 #define BUFF_SIZE 8192
527 repo_add_deltainfoxml(Repo *repo, FILE *fp, int flags)
529 Pool *pool = repo->pool;
533 struct stateswitch *sw;
535 memset(&pd, 0, sizeof(pd));
536 for (i = 0, sw = stateswitches; sw->from != NUMSTATES; i++, sw++)
538 if (!pd.swtab[sw->from])
539 pd.swtab[sw->from] = sw;
540 pd.sbtab[sw->to] = sw->from;
544 pd.data = repo_add_repodata(pd.repo, 0);
546 pd.content = malloc(256);
549 pd.tempstr = malloc(256);
553 XML_Parser parser = XML_ParserCreate(NULL);
554 XML_SetUserData(parser, &pd);
555 XML_SetElementHandler(parser, startElement, endElement);
556 XML_SetCharacterDataHandler(parser, characterData);
559 l = fread(buf, 1, sizeof(buf), fp);
560 if (XML_Parse(parser, buf, l, l == 0) == XML_STATUS_ERROR)
562 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));
568 XML_ParserFree(parser);
571 repodata_internalize(pd.data);