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"
26 /* #define DUMPOUT 1 */
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. */
112 struct stateswitch *swtab[NUMSTATES];
113 enum state sbtab[NUMSTATES];
117 struct deltarpm delta;
131 find_attr(const char *txt, const char **atts)
133 for (; *atts; atts += 2)
135 if (!strcmp(*atts, txt))
143 * create evr (as Id) from 'epoch', 'version' and 'release' attributes
147 makeevr_atts(Pool *pool, struct parsedata *pd, const char **atts)
149 const char *e, *v, *r, *v2;
154 for (; *atts; atts += 2)
156 if (!strcmp(*atts, "oldepoch"))
158 else if (!strcmp(*atts, "epoch"))
160 else if (!strcmp(*atts, "version"))
162 else if (!strcmp(*atts, "oldversion"))
164 else if (!strcmp(*atts, "release"))
166 else if (!strcmp(*atts, "oldrelease"))
169 if (e && !strcmp(e, "0"))
173 for (v2 = v; *v2 >= '0' && *v2 <= '9'; v2++)
175 if (v2 > v && *v2 == ':')
185 if (l > pd->acontent)
187 pd->content = sat_realloc(pd->content, l + 256);
188 pd->acontent = l + 256;
212 fprintf(stderr, "evr: %s\n", pd->content);
214 return str2id(pool, pd->content, 1);
217 static void parse_delta_location( struct parsedata *pd,
220 Pool *pool = pd->pool;
223 /* Separate the filename into its different parts.
224 rpm/x86_64/alsa-1.0.14-31_31.2.x86_64.delta.rpm
228 suffix = x86_64.delta.rpm. */
229 char *real_str = strdup(str);
232 s1 = strrchr (s, '/');
235 pd->delta.locdir = strn2id(pool, s, s1 - s, 1);
239 s1 = strrchr (s, '.');
242 for (s2 = s1 - 1; s2 > s; s2--)
245 if (!strcmp (s2, ".delta.rpm") || !strcmp (s2, ".patch.rpm"))
248 /* We accept one more item as suffix. */
249 for (s2 = s1 - 1; s2 > s; s2--)
256 pd->delta.locsuffix = str2id(pool, s1, 1);
259 s1 = strrchr (s, '-');
262 /* Second to last '-'. */
263 for (s2 = s1 - 1; s2 > s; s2--)
269 if (s2 > s && *s2 == '-')
272 pd->delta.locevr = str2id(pool, s2, 1);
274 pd->delta.locname = str2id(pool, s, 1);
280 startElement(void *userData, const char *name, const char **atts)
282 struct parsedata *pd = userData;
283 Pool *pool = pd->pool;
284 struct stateswitch *sw;
288 fprintf(stderr, "start: [%d]%s\n", pd->state, name);
290 if (pd->depth != pd->statedepth)
297 if (!pd->swtab[pd->state])
299 for (sw = pd->swtab[pd->state]; sw->from == pd->state; sw++) /* find name in statetable */
300 if (!strcmp(sw->ename, name))
302 if (sw->from != pd->state)
305 fprintf(stderr, "into unknown: [%d]%s (from: %d)\n", sw->to, name, sw->from);
311 pd->docontent = sw->docontent;
312 pd->statedepth = pd->depth;
320 case STATE_NEWPACKAGE:
321 if ((str = find_attr("name", atts)) != 0)
323 pd->newpkgname = str2id(pool, str, 1);
325 pd->newpkgevr = makeevr_atts(pool, pd, atts);
326 if ((str = find_attr("arch", atts)) != 0)
328 pd->newpkgarch = str2id(pool, str, 1);
333 memset(&pd->delta, 0, sizeof(pd->delta));
336 pd->delta.bevr = sat_extend(pd->delta.bevr, pd->delta.nbevr, 1, sizeof(Id), 7);
337 pd->delta.bevr[pd->delta.nbevr++] = makeevr_atts(pool, pd, atts);
342 parse_delta_location(pd, find_attr("href", atts));
347 pd->delta.filechecksum = 0;
348 pd->delta.filechecksumtype = REPOKEY_TYPE_SHA1;
349 if ((str = find_attr("type", atts)) != 0)
351 if (!strcasecmp(str, "sha"))
352 pd->delta.filechecksumtype = REPOKEY_TYPE_SHA1;
353 else if (!strcasecmp(str, "sha256"))
354 pd->delta.filechecksumtype = REPOKEY_TYPE_SHA256;
355 else if (!strcasecmp(str, "md5"))
356 pd->delta.filechecksumtype = REPOKEY_TYPE_MD5;
358 pool_debug(pool, SAT_ERROR, "unknown checksum type: '%s'\n", str);
369 endElement(void *userData, const char *name)
371 struct parsedata *pd = userData;
372 Pool *pool = pd->pool;
376 fprintf(stderr, "end: %s\n", name);
378 if (pd->depth != pd->statedepth)
382 fprintf(stderr, "back from unknown %d %d %d\n", pd->state, pd->depth, pd->statedepth);
393 case STATE_NEWPACKAGE:
397 /* read all data for a deltarpm. commit into attributes */
399 struct deltarpm *d = &pd->delta;
406 fprintf (stderr, "found deltarpm for %s:\n", id2str(pool, pd->newpkgname));
408 handle = repodata_new_handle(pd->data);
409 /* we commit all handles later on in one go so that the
410 * repodata code doesn't need to realloc every time */
411 pd->handles = sat_extend(pd->handles, pd->nhandles, 1, sizeof(Id), 63);
412 pd->handles[pd->nhandles++] = handle;
413 repodata_set_id(pd->data, handle, DELTA_PACKAGE_NAME, pd->newpkgname);
414 repodata_set_id(pd->data, handle, DELTA_PACKAGE_EVR, pd->newpkgevr);
415 repodata_set_id(pd->data, handle, DELTA_PACKAGE_ARCH, pd->newpkgarch);
416 repodata_set_id(pd->data, handle, DELTA_LOCATION_NAME, d->locname);
417 repodata_set_id(pd->data, handle, DELTA_LOCATION_DIR, d->locdir);
418 repodata_set_id(pd->data, handle, DELTA_LOCATION_EVR, d->locevr);
419 repodata_set_id(pd->data, handle, DELTA_LOCATION_SUFFIX, d->locsuffix);
421 repodata_set_num(pd->data, handle, DELTA_DOWNLOADSIZE, (d->downloadsize + 1023) / 1024);
423 repodata_set_checksum(pd->data, handle, DELTA_CHECKSUM, d->filechecksumtype, d->filechecksum);
425 fprintf (stderr, " loc: %s %s %s %s\n", id2str(pool, d->locdir),
426 id2str(pool, d->locname), id2str(pool, d->locevr),
427 id2str(pool, d->locsuffix));
428 fprintf (stderr, " size: %d down\n", d->downloadsize);
429 fprintf (stderr, " chek: %s\n", d->filechecksum);
435 fprintf (stderr, " base: %s\n",
436 id2str(pool, d->bevr[0]));
437 fprintf (stderr, " seq: %s\n",
438 id2str(pool, d->seqname));
439 fprintf (stderr, " %s\n",
440 id2str(pool, d->seqevr));
441 fprintf (stderr, " %s\n",
444 repodata_set_id(pd->data, handle, DELTA_BASE_EVR, d->bevr[0]);
445 repodata_set_id(pd->data, handle, DELTA_SEQ_NAME, d->seqname);
446 repodata_set_id(pd->data, handle, DELTA_SEQ_EVR, d->seqevr);
447 /* should store as binary blob! */
448 repodata_set_str(pd->data, handle, DELTA_SEQ_NUM, d->seqnum);
451 fprintf(stderr, "OK\n");
455 if (d->seqevr != d->bevr[0])
456 fprintf (stderr, "XXXXX evr\n");
457 /* Name of package ("xxxx") should match the sequence info
459 if (strcmp(id2str(pool, d->seqname), id2str(pool, pd->newpkgname)))
460 fprintf (stderr, "XXXXX name\n");
467 fprintf (stderr, " base:");
468 for (i = 0; i < d->nbevr; i++)
469 fprintf (stderr, " %s", id2str(pool, d->bevr[i]));
470 fprintf (stderr, "\n");
475 pd->delta.filechecksum = sat_free(pd->delta.filechecksum);
476 pd->delta.bevr = sat_free(pd->delta.bevr);
478 pd->delta.seqnum = sat_free(pd->delta.seqnum);
481 parse_delta_location(pd, pd->content);
484 pd->delta.filechecksum = strdup(pd->content);
487 pd->delta.downloadsize = atoi(pd->content);
490 if ((str = pd->content))
493 s1 = strrchr(str, '-');
496 for (s2 = s1 - 1; s2 > str; s2--)
501 for (s2 = s2 - 1; s2 > str; s2--)
506 pd->delta.seqevr = strn2id(pool, s2 + 1, s1 - s2 - 1, 1);
507 pd->delta.seqname = strn2id(pool, str, s2 - str, 1);
512 pd->delta.seqnum = strdup(str);
518 pd->state = pd->sbtab[pd->state];
524 characterData(void *userData, const XML_Char *s, int len)
526 struct parsedata *pd = userData;
531 l = pd->lcontent + len + 1;
532 if (l > pd->acontent)
534 pd->content = sat_realloc(pd->content, l + 256);
535 pd->acontent = l + 256;
537 c = pd->content + pd->lcontent;
544 #define BUFF_SIZE 8192
547 repo_add_deltainfoxml(Repo *repo, FILE *fp, int flags)
549 Pool *pool = repo->pool;
553 struct stateswitch *sw;
556 data = repo_add_repodata(repo, flags);
558 memset(&pd, 0, sizeof(pd));
559 for (i = 0, sw = stateswitches; sw->from != NUMSTATES; i++, sw++)
561 if (!pd.swtab[sw->from])
562 pd.swtab[sw->from] = sw;
563 pd.sbtab[sw->to] = sw->from;
569 pd.content = sat_malloc(256);
572 pd.tempstr = malloc(256);
576 XML_Parser parser = XML_ParserCreate(NULL);
577 XML_SetUserData(parser, &pd);
578 XML_SetElementHandler(parser, startElement, endElement);
579 XML_SetCharacterDataHandler(parser, characterData);
582 l = fread(buf, 1, sizeof(buf), fp);
583 if (XML_Parse(parser, buf, l, l == 0) == XML_STATUS_ERROR)
585 pool_debug(pool, SAT_FATAL, "repo_updateinfoxml: %s at line %u:%u\n", XML_ErrorString(XML_GetErrorCode(parser)), (unsigned int)XML_GetCurrentLineNumber(parser), (unsigned int)XML_GetCurrentColumnNumber(parser));
591 XML_ParserFree(parser);
592 sat_free(pd.content);
593 sat_free(pd.tempstr);
596 /* now commit all handles */
597 for (i = 0; i < pd.nhandles; i++)
598 repodata_add_flexarray(pd.data, SOLVID_META, REPOSITORY_DELTAINFO, pd.handles[i]);
599 sat_free(pd.handles);
601 if (!(flags & REPO_NO_INTERNALIZE))
602 repodata_internalize(data);