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_deltainfoxml.h"
26 * <newpackage name="libtool" epoch="0" version="1.5.24" release="6.fc9" arch="i386">
27 * <delta oldepoch="0" oldversion="1.5.24" oldrelease="3.fc8">
28 * <filename>DRPMS/libtool-1.5.24-3.fc8_1.5.24-6.fc9.i386.drpm</filename>
29 * <sequence>libtool-1.5.24-3.fc8-d3571f98b048b1a870e40241bb46c67ab4</sequence>
31 * <checksum type="sha">8f05394695dee9399c204614e21e5f6848990ab7</checksum>
33 * <delta oldepoch="0" oldversion="1.5.22" oldrelease="11.fc7">
34 * <filename>DRPMS/libtool-1.5.22-11.fc7_1.5.24-6.fc9.i386.drpm</filename>
35 * <sequence>libtool-1.5.22-11.fc7-e82691677eee1e83b4812572c5c9ce8eb</sequence>
37 * <checksum type="sha">326658fee45c0baec1e70231046dbaf560f941ce</checksum>
45 STATE_NEWPACKAGE, /* 1 */
47 STATE_FILENAME, /* 3 */
48 STATE_SEQUENCE, /* 4 */
50 STATE_CHECKSUM, /* 6 */
51 STATE_LOCATION, /* 7 */
62 /* !! must be sorted by first column !! */
63 static struct stateswitch stateswitches[] = {
64 /* compatibility with old yum-presto */
65 { STATE_START, "prestodelta", STATE_START, 0 },
66 { STATE_START, "deltainfo", STATE_START, 0 },
67 { STATE_START, "newpackage", STATE_NEWPACKAGE, 0 },
68 { STATE_NEWPACKAGE, "delta", STATE_DELTA, 0 },
69 /* compatibility with yum-presto */
70 { STATE_DELTA, "filename", STATE_FILENAME, 1 },
71 { STATE_DELTA, "location", STATE_LOCATION, 0 },
72 { STATE_DELTA, "sequence", STATE_SEQUENCE, 1 },
73 { STATE_DELTA, "size", STATE_SIZE, 1 },
74 { STATE_DELTA, "checksum", STATE_CHECKSUM, 1 },
78 /* Cumulated info about the current deltarpm or patchrpm */
84 unsigned int buildtime;
85 unsigned long long downloadsize;
88 /* Baseversion. deltarpm only has one. */
109 struct stateswitch *swtab[NUMSTATES];
110 enum state sbtab[NUMSTATES];
111 struct deltarpm delta;
125 find_attr(const char *txt, const char **atts)
127 for (; *atts; atts += 2)
129 if (!strcmp(*atts, txt))
137 * create evr (as Id) from 'epoch', 'version' and 'release' attributes
141 makeevr_atts(Pool *pool, struct parsedata *pd, const char **atts)
143 const char *e, *v, *r, *v2;
148 for (; *atts; atts += 2)
150 if (!strcmp(*atts, "oldepoch"))
152 else if (!strcmp(*atts, "epoch"))
154 else if (!strcmp(*atts, "version"))
156 else if (!strcmp(*atts, "oldversion"))
158 else if (!strcmp(*atts, "release"))
160 else if (!strcmp(*atts, "oldrelease"))
163 if (e && !strcmp(e, "0"))
167 for (v2 = v; *v2 >= '0' && *v2 <= '9'; v2++)
169 if (v2 > v && *v2 == ':')
179 if (l > pd->acontent)
181 pd->content = solv_realloc(pd->content, l + 256);
182 pd->acontent = l + 256;
206 fprintf(stderr, "evr: %s\n", pd->content);
208 return pool_str2id(pool, pd->content, 1);
212 parse_delta_location(struct parsedata *pd, const char* str)
214 Pool *pool = pd->pool;
217 /* Separate the filename into its different parts.
218 rpm/x86_64/alsa-1.0.14-31_31.2.x86_64.delta.rpm
222 suffix = x86_64.delta.rpm. */
223 char *real_str = solv_strdup(str);
226 s1 = strrchr (s, '/');
229 pd->delta.locdir = pool_strn2id(pool, s, s1 - s, 1);
233 s1 = strrchr (s, '.');
236 for (s2 = s1 - 1; s2 > s; s2--)
239 if (!strcmp (s2, ".delta.rpm") || !strcmp (s2, ".patch.rpm"))
242 /* We accept one more item as suffix. */
243 for (s2 = s1 - 1; s2 > s; s2--)
250 pd->delta.locsuffix = pool_str2id(pool, s1, 1);
253 s1 = strrchr (s, '-');
256 /* Second to last '-'. */
257 for (s2 = s1 - 1; s2 > s; s2--)
263 if (s2 > s && *s2 == '-')
266 pd->delta.locevr = pool_str2id(pool, s2, 1);
268 pd->delta.locname = pool_str2id(pool, s, 1);
274 startElement(void *userData, const char *name, const char **atts)
276 struct parsedata *pd = userData;
277 Pool *pool = pd->pool;
278 struct stateswitch *sw;
282 fprintf(stderr, "start: [%d]%s\n", pd->state, name);
284 if (pd->depth != pd->statedepth)
291 if (!pd->swtab[pd->state])
293 for (sw = pd->swtab[pd->state]; sw->from == pd->state; sw++) /* find name in statetable */
294 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);
304 pd->docontent = sw->docontent;
305 pd->statedepth = pd->depth;
313 case STATE_NEWPACKAGE:
314 if ((str = find_attr("name", atts)) != 0)
315 pd->newpkgname = pool_str2id(pool, str, 1);
316 pd->newpkgevr = makeevr_atts(pool, pd, atts);
317 if ((str = find_attr("arch", atts)) != 0)
318 pd->newpkgarch = pool_str2id(pool, str, 1);
322 memset(&pd->delta, 0, sizeof(pd->delta));
323 pd->delta.bevr = solv_extend(pd->delta.bevr, pd->delta.nbevr, 1, sizeof(Id), 7);
324 pd->delta.bevr[pd->delta.nbevr++] = makeevr_atts(pool, pd, atts);
329 parse_delta_location(pd, find_attr("href", atts));
334 pd->delta.filechecksum = 0;
335 pd->delta.filechecksumtype = REPOKEY_TYPE_SHA1;
336 if ((str = find_attr("type", atts)) != 0)
338 pd->delta.filechecksumtype = solv_chksum_str2type(str);
339 if (!pd->delta.filechecksumtype)
340 pool_debug(pool, SOLV_ERROR, "unknown checksum type: '%s'\n", str);
352 endElement(void *userData, const char *name)
354 struct parsedata *pd = userData;
355 Pool *pool = pd->pool;
359 fprintf(stderr, "end: %s\n", name);
361 if (pd->depth != pd->statedepth)
365 fprintf(stderr, "back from unknown %d %d %d\n", pd->state, pd->depth, pd->statedepth);
376 case STATE_NEWPACKAGE:
380 /* read all data for a deltarpm. commit into attributes */
382 struct deltarpm *d = &pd->delta;
384 handle = repodata_new_handle(pd->data);
385 /* we commit all handles later on in one go so that the
386 * repodata code doesn't need to realloc every time */
387 pd->handles = solv_extend(pd->handles, pd->nhandles, 1, sizeof(Id), 63);
388 pd->handles[pd->nhandles++] = handle;
389 repodata_set_id(pd->data, handle, DELTA_PACKAGE_NAME, pd->newpkgname);
390 repodata_set_id(pd->data, handle, DELTA_PACKAGE_EVR, pd->newpkgevr);
391 repodata_set_id(pd->data, handle, DELTA_PACKAGE_ARCH, pd->newpkgarch);
392 repodata_set_id(pd->data, handle, DELTA_LOCATION_NAME, d->locname);
393 repodata_set_id(pd->data, handle, DELTA_LOCATION_DIR, d->locdir);
394 repodata_set_id(pd->data, handle, DELTA_LOCATION_EVR, d->locevr);
395 repodata_set_id(pd->data, handle, DELTA_LOCATION_SUFFIX, d->locsuffix);
397 repodata_set_num(pd->data, handle, DELTA_DOWNLOADSIZE, d->downloadsize);
399 repodata_set_checksum(pd->data, handle, DELTA_CHECKSUM, d->filechecksumtype, d->filechecksum);
402 repodata_set_id(pd->data, handle, DELTA_BASE_EVR, d->bevr[0]);
403 repodata_set_id(pd->data, handle, DELTA_SEQ_NAME, d->seqname);
404 repodata_set_id(pd->data, handle, DELTA_SEQ_EVR, d->seqevr);
405 /* should store as binary blob! */
406 repodata_set_str(pd->data, handle, DELTA_SEQ_NUM, d->seqnum);
409 pd->delta.filechecksum = solv_free(pd->delta.filechecksum);
410 pd->delta.bevr = solv_free(pd->delta.bevr);
412 pd->delta.seqnum = solv_free(pd->delta.seqnum);
415 parse_delta_location(pd, pd->content);
418 pd->delta.filechecksum = solv_strdup(pd->content);
421 pd->delta.downloadsize = strtoull(pd->content, 0, 10);
424 if ((str = pd->content))
427 s1 = strrchr(str, '-');
430 for (s2 = s1 - 1; s2 > str; s2--)
435 for (s2 = s2 - 1; s2 > str; s2--)
440 pd->delta.seqevr = pool_strn2id(pool, s2 + 1, s1 - s2 - 1, 1);
441 pd->delta.seqname = pool_strn2id(pool, str, s2 - str, 1);
446 pd->delta.seqnum = solv_strdup(str);
452 pd->state = pd->sbtab[pd->state];
458 characterData(void *userData, const XML_Char *s, int len)
460 struct parsedata *pd = userData;
465 l = pd->lcontent + len + 1;
466 if (l > pd->acontent)
468 pd->content = solv_realloc(pd->content, l + 256);
469 pd->acontent = l + 256;
471 c = pd->content + pd->lcontent;
478 #define BUFF_SIZE 8192
481 repo_add_deltainfoxml(Repo *repo, FILE *fp, int flags)
483 Pool *pool = repo->pool;
487 struct stateswitch *sw;
491 data = repo_add_repodata(repo, flags);
493 memset(&pd, 0, sizeof(pd));
494 for (i = 0, sw = stateswitches; sw->from != NUMSTATES; i++, sw++)
496 if (!pd.swtab[sw->from])
497 pd.swtab[sw->from] = sw;
498 pd.sbtab[sw->to] = sw->from;
504 pd.content = solv_malloc(256);
508 parser = XML_ParserCreate(NULL);
509 XML_SetUserData(parser, &pd);
510 XML_SetElementHandler(parser, startElement, endElement);
511 XML_SetCharacterDataHandler(parser, characterData);
514 l = fread(buf, 1, sizeof(buf), fp);
515 if (XML_Parse(parser, buf, l, l == 0) == XML_STATUS_ERROR)
517 pd.ret = pool_error(pool, -1, "repo_updateinfoxml: %s at line %u:%u", XML_ErrorString(XML_GetErrorCode(parser)), (unsigned int)XML_GetCurrentLineNumber(parser), (unsigned int)XML_GetCurrentColumnNumber(parser));
523 XML_ParserFree(parser);
524 solv_free(pd.content);
526 /* now commit all handles */
528 for (i = 0; i < pd.nhandles; i++)
529 repodata_add_flexarray(pd.data, SOLVID_META, REPOSITORY_DELTAINFO, pd.handles[i]);
530 solv_free(pd.handles);
532 if (!(flags & REPO_NO_INTERNALIZE))
533 repodata_internalize(data);