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>
22 #include "repo_updateinfoxml.h"
25 #include "tools_util.h"
27 /* #define DUMPOUT 1 */
31 * <newpackage name="libtool" epoch="0" version="1.5.24" release="6.fc9" arch="i386">
32 * <delta oldepoch="0" oldversion="1.5.24" oldrelease="3.fc8">
33 * <filename>DRPMS/libtool-1.5.24-3.fc8_1.5.24-6.fc9.i386.drpm</filename>
34 * <sequence>libtool-1.5.24-3.fc8-d3571f98b048b1a870e40241bb46c67ab4</sequence>
36 * <checksum type="sha">8f05394695dee9399c204614e21e5f6848990ab7</checksum>
38 * <delta oldepoch="0" oldversion="1.5.22" oldrelease="11.fc7">
39 * <filename>DRPMS/libtool-1.5.22-11.fc7_1.5.24-6.fc9.i386.drpm</filename>
40 * <sequence>libtool-1.5.22-11.fc7-e82691677eee1e83b4812572c5c9ce8eb</sequence>
42 * <checksum type="sha">326658fee45c0baec1e70231046dbaf560f941ce</checksum>
50 STATE_NEWPACKAGE, /* 1 */
52 STATE_FILENAME, /* 3 */
53 STATE_SEQUENCE, /* 4 */
55 STATE_CHECKSUM, /* 6 */
56 STATE_LOCATION, /* 7 */
67 /* !! must be sorted by first column !! */
68 static struct stateswitch stateswitches[] = {
69 /* compatibility with old yum-presto */
70 { STATE_START, "prestodelta", STATE_START, 0 },
71 { STATE_START, "deltainfo", STATE_START, 0 },
72 { STATE_START, "newpackage", STATE_NEWPACKAGE, 0 },
73 { STATE_NEWPACKAGE, "delta", STATE_DELTA, 0 },
74 /* compatibility with yum-presto */
75 { STATE_DELTA, "filename", STATE_FILENAME, 1 },
76 { STATE_DELTA, "location", STATE_LOCATION, 0 },
77 { STATE_DELTA, "sequence", STATE_SEQUENCE, 1 },
78 { STATE_DELTA, "size", STATE_SIZE, 1 },
79 { STATE_DELTA, "checksum", STATE_CHECKSUM, 1 },
83 /* Cumulated info about the current deltarpm or patchrpm */
90 unsigned downloadsize, archivesize;
93 /* Baseversion. deltarpm only has one. */
113 struct stateswitch *swtab[NUMSTATES];
114 enum state sbtab[NUMSTATES];
118 struct deltarpm delta;
132 find_attr(const char *txt, const char **atts)
134 for (; *atts; atts += 2)
136 if (!strcmp(*atts, txt))
144 * create evr (as Id) from 'epoch', 'version' and 'release' attributes
148 makeevr_atts(Pool *pool, struct parsedata *pd, const char **atts)
150 const char *e, *v, *r, *v2;
155 for (; *atts; atts += 2)
157 if (!strcmp(*atts, "oldepoch"))
159 else if (!strcmp(*atts, "epoch"))
161 else if (!strcmp(*atts, "version"))
163 else if (!strcmp(*atts, "oldversion"))
165 else if (!strcmp(*atts, "release"))
167 else if (!strcmp(*atts, "oldrelease"))
170 if (e && !strcmp(e, "0"))
174 for (v2 = v; *v2 >= '0' && *v2 <= '9'; v2++)
176 if (v2 > v && *v2 == ':')
186 if (l > pd->acontent)
188 pd->content = solv_realloc(pd->content, l + 256);
189 pd->acontent = l + 256;
213 fprintf(stderr, "evr: %s\n", pd->content);
215 return pool_str2id(pool, pd->content, 1);
218 static void parse_delta_location( struct parsedata *pd,
221 Pool *pool = pd->pool;
224 /* Separate the filename into its different parts.
225 rpm/x86_64/alsa-1.0.14-31_31.2.x86_64.delta.rpm
229 suffix = x86_64.delta.rpm. */
230 char *real_str = solv_strdup(str);
233 s1 = strrchr (s, '/');
236 pd->delta.locdir = pool_strn2id(pool, s, s1 - s, 1);
240 s1 = strrchr (s, '.');
243 for (s2 = s1 - 1; s2 > s; s2--)
246 if (!strcmp (s2, ".delta.rpm") || !strcmp (s2, ".patch.rpm"))
249 /* We accept one more item as suffix. */
250 for (s2 = s1 - 1; s2 > s; s2--)
257 pd->delta.locsuffix = pool_str2id(pool, s1, 1);
260 s1 = strrchr (s, '-');
263 /* Second to last '-'. */
264 for (s2 = s1 - 1; s2 > s; s2--)
270 if (s2 > s && *s2 == '-')
273 pd->delta.locevr = pool_str2id(pool, s2, 1);
275 pd->delta.locname = pool_str2id(pool, s, 1);
281 startElement(void *userData, const char *name, const char **atts)
283 struct parsedata *pd = userData;
284 Pool *pool = pd->pool;
285 struct stateswitch *sw;
289 fprintf(stderr, "start: [%d]%s\n", pd->state, name);
291 if (pd->depth != pd->statedepth)
298 if (!pd->swtab[pd->state])
300 for (sw = pd->swtab[pd->state]; sw->from == pd->state; sw++) /* find name in statetable */
301 if (!strcmp(sw->ename, name))
303 if (sw->from != pd->state)
306 fprintf(stderr, "into unknown: [%d]%s (from: %d)\n", sw->to, name, sw->from);
312 pd->docontent = sw->docontent;
313 pd->statedepth = pd->depth;
321 case STATE_NEWPACKAGE:
322 if ((str = find_attr("name", atts)) != 0)
323 pd->newpkgname = pool_str2id(pool, str, 1);
324 pd->newpkgevr = makeevr_atts(pool, pd, atts);
325 if ((str = find_attr("arch", atts)) != 0)
326 pd->newpkgarch = pool_str2id(pool, str, 1);
330 memset(&pd->delta, 0, sizeof(pd->delta));
333 pd->delta.bevr = solv_extend(pd->delta.bevr, pd->delta.nbevr, 1, sizeof(Id), 7);
334 pd->delta.bevr[pd->delta.nbevr++] = makeevr_atts(pool, pd, atts);
339 parse_delta_location(pd, find_attr("href", atts));
344 pd->delta.filechecksum = 0;
345 pd->delta.filechecksumtype = REPOKEY_TYPE_SHA1;
346 if ((str = find_attr("type", atts)) != 0)
348 pd->delta.filechecksumtype = solv_chksum_str2type(str);
349 if (!pd->delta.filechecksumtype)
350 pool_debug(pool, SOLV_ERROR, "unknown checksum type: '%s'\n", str);
361 endElement(void *userData, const char *name)
363 struct parsedata *pd = userData;
364 Pool *pool = pd->pool;
368 fprintf(stderr, "end: %s\n", name);
370 if (pd->depth != pd->statedepth)
374 fprintf(stderr, "back from unknown %d %d %d\n", pd->state, pd->depth, pd->statedepth);
385 case STATE_NEWPACKAGE:
389 /* read all data for a deltarpm. commit into attributes */
391 struct deltarpm *d = &pd->delta;
398 fprintf (stderr, "found deltarpm for %s:\n", id2str(pool, pd->newpkgname));
400 handle = repodata_new_handle(pd->data);
401 /* we commit all handles later on in one go so that the
402 * repodata code doesn't need to realloc every time */
403 pd->handles = solv_extend(pd->handles, pd->nhandles, 1, sizeof(Id), 63);
404 pd->handles[pd->nhandles++] = handle;
405 repodata_set_id(pd->data, handle, DELTA_PACKAGE_NAME, pd->newpkgname);
406 repodata_set_id(pd->data, handle, DELTA_PACKAGE_EVR, pd->newpkgevr);
407 repodata_set_id(pd->data, handle, DELTA_PACKAGE_ARCH, pd->newpkgarch);
408 repodata_set_id(pd->data, handle, DELTA_LOCATION_NAME, d->locname);
409 repodata_set_id(pd->data, handle, DELTA_LOCATION_DIR, d->locdir);
410 repodata_set_id(pd->data, handle, DELTA_LOCATION_EVR, d->locevr);
411 repodata_set_id(pd->data, handle, DELTA_LOCATION_SUFFIX, d->locsuffix);
413 repodata_set_num(pd->data, handle, DELTA_DOWNLOADSIZE, (d->downloadsize + 1023) / 1024);
415 repodata_set_checksum(pd->data, handle, DELTA_CHECKSUM, d->filechecksumtype, d->filechecksum);
417 fprintf (stderr, " loc: %s %s %s %s\n", id2str(pool, d->locdir),
418 id2str(pool, d->locname), id2str(pool, d->locevr),
419 id2str(pool, d->locsuffix));
420 fprintf (stderr, " size: %d down\n", d->downloadsize);
421 fprintf (stderr, " chek: %s\n", d->filechecksum);
427 fprintf (stderr, " base: %s\n",
428 id2str(pool, d->bevr[0]));
429 fprintf (stderr, " seq: %s\n",
430 id2str(pool, d->seqname));
431 fprintf (stderr, " %s\n",
432 id2str(pool, d->seqevr));
433 fprintf (stderr, " %s\n",
436 repodata_set_id(pd->data, handle, DELTA_BASE_EVR, d->bevr[0]);
437 repodata_set_id(pd->data, handle, DELTA_SEQ_NAME, d->seqname);
438 repodata_set_id(pd->data, handle, DELTA_SEQ_EVR, d->seqevr);
439 /* should store as binary blob! */
440 repodata_set_str(pd->data, handle, DELTA_SEQ_NUM, d->seqnum);
443 fprintf(stderr, "OK\n");
447 if (d->seqevr != d->bevr[0])
448 fprintf (stderr, "XXXXX evr\n");
449 /* Name of package ("xxxx") should match the sequence info
451 if (strcmp(id2str(pool, d->seqname), id2str(pool, pd->newpkgname)))
452 fprintf (stderr, "XXXXX name\n");
459 fprintf (stderr, " base:");
460 for (i = 0; i < d->nbevr; i++)
461 fprintf (stderr, " %s", id2str(pool, d->bevr[i]));
462 fprintf (stderr, "\n");
467 pd->delta.filechecksum = solv_free(pd->delta.filechecksum);
468 pd->delta.bevr = solv_free(pd->delta.bevr);
470 pd->delta.seqnum = solv_free(pd->delta.seqnum);
473 parse_delta_location(pd, pd->content);
476 pd->delta.filechecksum = solv_strdup(pd->content);
479 pd->delta.downloadsize = atoi(pd->content);
482 if ((str = pd->content))
485 s1 = strrchr(str, '-');
488 for (s2 = s1 - 1; s2 > str; s2--)
493 for (s2 = s2 - 1; s2 > str; s2--)
498 pd->delta.seqevr = pool_strn2id(pool, s2 + 1, s1 - s2 - 1, 1);
499 pd->delta.seqname = pool_strn2id(pool, str, s2 - str, 1);
504 pd->delta.seqnum = solv_strdup(str);
510 pd->state = pd->sbtab[pd->state];
516 characterData(void *userData, const XML_Char *s, int len)
518 struct parsedata *pd = userData;
523 l = pd->lcontent + len + 1;
524 if (l > pd->acontent)
526 pd->content = solv_realloc(pd->content, l + 256);
527 pd->acontent = l + 256;
529 c = pd->content + pd->lcontent;
536 #define BUFF_SIZE 8192
539 repo_add_deltainfoxml(Repo *repo, FILE *fp, int flags)
541 Pool *pool = repo->pool;
545 struct stateswitch *sw;
549 data = repo_add_repodata(repo, flags);
551 memset(&pd, 0, sizeof(pd));
552 for (i = 0, sw = stateswitches; sw->from != NUMSTATES; i++, sw++)
554 if (!pd.swtab[sw->from])
555 pd.swtab[sw->from] = sw;
556 pd.sbtab[sw->to] = sw->from;
562 pd.content = solv_malloc(256);
565 pd.tempstr = malloc(256);
569 parser = XML_ParserCreate(NULL);
570 XML_SetUserData(parser, &pd);
571 XML_SetElementHandler(parser, startElement, endElement);
572 XML_SetCharacterDataHandler(parser, characterData);
575 l = fread(buf, 1, sizeof(buf), fp);
576 if (XML_Parse(parser, buf, l, l == 0) == XML_STATUS_ERROR)
578 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));
584 XML_ParserFree(parser);
585 solv_free(pd.content);
586 solv_free(pd.tempstr);
589 /* now commit all handles */
590 for (i = 0; i < pd.nhandles; i++)
591 repodata_add_flexarray(pd.data, SOLVID_META, REPOSITORY_DELTAINFO, pd.handles[i]);
592 solv_free(pd.handles);
594 if (!(flags & REPO_NO_INTERNALIZE))
595 repodata_internalize(data);