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;
127 find_attr(const char *txt, const char **atts)
129 for (; *atts; atts += 2)
131 if (!strcmp(*atts, txt))
139 * create evr (as Id) from 'epoch', 'version' and 'release' attributes
143 makeevr_atts(Pool *pool, struct parsedata *pd, const char **atts)
145 const char *e, *v, *r, *v2;
150 for (; *atts; atts += 2)
152 if (!strcmp(*atts, "oldepoch"))
154 else if (!strcmp(*atts, "epoch"))
156 else if (!strcmp(*atts, "version"))
158 else if (!strcmp(*atts, "oldversion"))
160 else if (!strcmp(*atts, "release"))
162 else if (!strcmp(*atts, "oldrelease"))
165 if (e && !strcmp(e, "0"))
169 for (v2 = v; *v2 >= '0' && *v2 <= '9'; v2++)
171 if (v2 > v && *v2 == ':')
181 if (l > pd->acontent)
183 pd->content = realloc(pd->content, l + 256);
184 pd->acontent = l + 256;
208 fprintf(stderr, "evr: %s\n", pd->content);
210 return str2id(pool, pd->content, 1);
213 static void parse_delta_location( struct parsedata *pd,
216 Pool *pool = pd->pool;
219 /* Separate the filename into its different parts.
220 rpm/x86_64/alsa-1.0.14-31_31.2.x86_64.delta.rpm
224 suffix = x86_64.delta.rpm. */
225 char *real_str = strdup(str);
228 s1 = strrchr (s, '/');
231 pd->delta.locdir = strn2id(pool, s, s1 - s, 1);
235 s1 = strrchr (s, '.');
238 for (s2 = s1 - 1; s2 > s; s2--)
241 if (!strcmp (s2, ".delta.rpm") || !strcmp (s2, ".patch.rpm"))
244 /* We accept one more item as suffix. */
245 for (s2 = s1 - 1; s2 > s; s2--)
252 pd->delta.locsuffix = str2id(pool, s1, 1);
255 s1 = strrchr (s, '-');
258 /* Second to last '-'. */
259 for (s2 = s1 - 1; s2 > s; s2--)
265 if (s2 > s && *s2 == '-')
268 pd->delta.locevr = str2id(pool, s2, 1);
270 pd->delta.locname = str2id(pool, s, 1);
276 startElement(void *userData, const char *name, const char **atts)
278 struct parsedata *pd = userData;
279 Pool *pool = pd->pool;
280 struct stateswitch *sw;
284 fprintf(stderr, "start: [%d]%s\n", pd->state, name);
286 if (pd->depth != pd->statedepth)
293 if (!pd->swtab[pd->state])
295 for (sw = pd->swtab[pd->state]; sw->from == pd->state; sw++) /* find name in statetable */
296 if (!strcmp(sw->ename, name))
298 if (sw->from != pd->state)
301 fprintf(stderr, "into unknown: [%d]%s (from: %d)\n", sw->to, name, sw->from);
307 pd->docontent = sw->docontent;
308 pd->statedepth = pd->depth;
316 case STATE_NEWPACKAGE:
317 if ( (str = find_attr("name", atts)) )
319 pd->newpkgname = str2id(pool, str, 1);
321 pd->newpkgevr = makeevr_atts(pool, pd, atts);
322 if ( (str = find_attr("arch", atts)) )
324 pd->newpkgarch = str2id(pool, str, 1);
329 memset(&pd->delta, 0, sizeof (pd->delta));
333 pd->delta.bevr = sat_realloc (pd->delta.bevr, pd->delta.nbevr * sizeof(Id));
334 pd->delta.bevr[pd->delta.nbevr - 1] = makeevr_atts(pool, pd, atts);
340 parse_delta_location( pd, find_attr("href", atts));
348 split(NULL, NULL, 0); /* just to keep gcc happy about tools_util.h: static ... split() {...} Urgs!*/
358 endElement(void *userData, const char *name)
360 struct parsedata *pd = userData;
361 Pool *pool = pd->pool;
365 fprintf(stderr, "end: %s\n", name);
367 if (pd->depth != pd->statedepth)
371 fprintf(stderr, "back from unknown %d %d %d\n", pd->state, pd->depth, pd->statedepth);
382 case STATE_NEWPACKAGE:
389 struct deltarpm *d = &pd->delta;
393 fprintf (stderr, "found deltarpm for %s:\n", id2str(pool, pd->newpkgname));
395 repo_set_id(pd->repo, pd->datanum, DELTA_PACKAGE_NAME, pd->newpkgname);
396 repo_set_id(pd->repo, pd->datanum, DELTA_PACKAGE_EVR, pd->newpkgevr);
397 repo_set_id(pd->repo, pd->datanum, DELTA_PACKAGE_ARCH, pd->newpkgarch);
398 repo_set_id(pd->repo, pd->datanum, DELTA_LOCATION_NAME, d->locname);
399 repo_set_id(pd->repo, pd->datanum, DELTA_LOCATION_DIR, d->locdir);
400 repo_set_id(pd->repo, pd->datanum, DELTA_LOCATION_EVR, d->locevr);
401 repo_set_id(pd->repo, pd->datanum, DELTA_LOCATION_SUFFIX, d->locsuffix);
404 fprintf (stderr, " loc: %s %s %s %s\n", id2str(pool, d->locdir),
405 id2str(pool, d->locname), id2str(pool, d->locevr),
406 id2str(pool, d->locsuffix));
407 fprintf (stderr, " size: %d down\n", d->downloadsize);
408 fprintf (stderr, " chek: %s\n", d->filechecksum);
411 repo_set_num(pd->repo, pd->datanum, DELTA_DOWNLOADSIZE, d->downloadsize);
412 repo_set_str(pd->repo, pd->datanum, DELTA_CHECKSUM, d->filechecksum);
418 fprintf (stderr, " base: %s\n",
419 id2str(pool, d->bevr[0]));
420 fprintf (stderr, " seq: %s\n",
421 id2str(pool, d->seqname));
422 fprintf (stderr, " %s\n",
423 id2str(pool, d->seqevr));
424 fprintf (stderr, " %s\n",
427 repo_set_id(pd->repo, pd->datanum, DELTA_BASE_EVR, d->bevr[0]);
428 repo_set_id(pd->repo, pd->datanum, DELTA_SEQ_NAME, d->seqname);
429 repo_set_id(pd->repo, pd->datanum, DELTA_SEQ_EVR, d->seqevr);
430 repo_set_str(pd->repo, pd->datanum, DELTA_SEQ_NUM, d->seqnum);
433 fprintf(stderr, "OK\n");
437 if (d->seqevr != d->bevr[0])
438 fprintf (stderr, "XXXXX evr\n");
439 /* Name of package ("atom:xxxx") should match the sequence info
441 if (strcmp(id2str(pool, d->seqname), id2str(pool, pd->newpkgname) + 5))
442 fprintf (stderr, "XXXXX name\n");
449 fprintf (stderr, " base:");
450 for (i = 0; i < d->nbevr; i++)
451 fprintf (stderr, " %s", id2str(pool, d->bevr[i]));
452 fprintf (stderr, "\n");
457 free(pd->delta.filechecksum);
458 free(pd->delta.bevr);
459 free(pd->delta.seqnum);
462 parse_delta_location(pd, pd->content);
465 pd->delta.filechecksum = strdup(pd->content);
468 pd->delta.downloadsize = atoi(pd->content);
471 if ((str = pd->content))
474 s1 = strrchr(str, '-');
477 for (s2 = s1 - 1; s2 > str; s2--)
482 for (s2 = s2 - 1; s2 > str; s2--)
487 pd->delta.seqevr = strn2id(pool, s2 + 1, s1 - s2 - 1, 1);
488 pd->delta.seqname = strn2id(pool, str, s2 - str, 1);
493 pd->delta.seqnum = strdup(str);
499 pd->state = pd->sbtab[pd->state];
507 characterData(void *userData, const XML_Char *s, int len)
509 struct parsedata *pd = userData;
512 if (!pd->docontent) {
514 char *dup = strndup( s, len );
515 fprintf(stderr, "Content: [%d]'%s'\n", pd->state, dup );
520 l = pd->lcontent + len + 1;
521 if (l > pd->acontent)
523 pd->content = realloc(pd->content, l + 256);
524 pd->acontent = l + 256;
526 c = pd->content + pd->lcontent;
533 #define BUFF_SIZE 8192
536 repo_add_deltainfoxml(Repo *repo, FILE *fp, int flags)
538 Pool *pool = repo->pool;
542 struct stateswitch *sw;
544 memset(&pd, 0, sizeof(pd));
545 for (i = 0, sw = stateswitches; sw->from != NUMSTATES; i++, sw++)
547 if (!pd.swtab[sw->from])
548 pd.swtab[sw->from] = sw;
549 pd.sbtab[sw->to] = sw->from;
553 pd.data = repo_add_repodata(pd.repo, 0);
555 pd.content = malloc(256);
558 pd.tempstr = malloc(256);
562 XML_Parser parser = XML_ParserCreate(NULL);
563 XML_SetUserData(parser, &pd);
564 XML_SetElementHandler(parser, startElement, endElement);
565 XML_SetCharacterDataHandler(parser, characterData);
568 l = fread(buf, 1, sizeof(buf), fp);
569 if (XML_Parse(parser, buf, l, l == 0) == XML_STATUS_ERROR)
571 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));
577 XML_ParserFree(parser);
580 repodata_internalize(pd.data);