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 */
82 unsigned int buildtime;
83 unsigned long long downloadsize;
86 /* Baseversion. deltarpm only has one. */
107 struct stateswitch *swtab[NUMSTATES];
108 enum state sbtab[NUMSTATES];
109 struct deltarpm delta;
123 find_attr(const char *txt, const char **atts)
125 for (; *atts; atts += 2)
127 if (!strcmp(*atts, txt))
135 * create evr (as Id) from 'epoch', 'version' and 'release' attributes
139 makeevr_atts(Pool *pool, struct parsedata *pd, const char **atts)
141 const char *e, *v, *r, *v2;
146 for (; *atts; atts += 2)
148 if (!strcmp(*atts, "oldepoch"))
150 else if (!strcmp(*atts, "epoch"))
152 else if (!strcmp(*atts, "version"))
154 else if (!strcmp(*atts, "oldversion"))
156 else if (!strcmp(*atts, "release"))
158 else if (!strcmp(*atts, "oldrelease"))
161 if (e && (!*e || !strcmp(e, "0")))
165 for (v2 = v; *v2 >= '0' && *v2 <= '9'; v2++)
167 if (v2 > v && *v2 == ':')
177 if (l > pd->acontent)
179 pd->content = solv_realloc(pd->content, l + 256);
180 pd->acontent = l + 256;
204 fprintf(stderr, "evr: %s\n", pd->content);
206 return pool_str2id(pool, pd->content, 1);
210 startElement(void *userData, const char *name, const char **atts)
212 struct parsedata *pd = userData;
213 Pool *pool = pd->pool;
214 struct stateswitch *sw;
218 fprintf(stderr, "start: [%d]%s\n", pd->state, name);
220 if (pd->depth != pd->statedepth)
227 if (!pd->swtab[pd->state])
229 for (sw = pd->swtab[pd->state]; sw->from == pd->state; sw++) /* find name in statetable */
230 if (!strcmp(sw->ename, name))
232 if (sw->from != pd->state)
235 fprintf(stderr, "into unknown: [%d]%s (from: %d)\n", sw->to, name, sw->from);
240 pd->docontent = sw->docontent;
241 pd->statedepth = pd->depth;
249 case STATE_NEWPACKAGE:
250 if ((str = find_attr("name", atts)) != 0)
251 pd->newpkgname = pool_str2id(pool, str, 1);
252 pd->newpkgevr = makeevr_atts(pool, pd, atts);
253 if ((str = find_attr("arch", atts)) != 0)
254 pd->newpkgarch = pool_str2id(pool, str, 1);
258 memset(&pd->delta, 0, sizeof(pd->delta));
259 pd->delta.bevr = solv_extend(pd->delta.bevr, pd->delta.nbevr, 1, sizeof(Id), 7);
260 pd->delta.bevr[pd->delta.nbevr++] = makeevr_atts(pool, pd, atts);
263 if ((str = find_attr("xml:base", atts)))
264 pd->delta.locbase = solv_strdup(str);
267 pd->delta.location = solv_strdup(find_attr("href", atts));
268 if ((str = find_attr("xml:base", atts)))
269 pd->delta.locbase = solv_strdup(str);
274 pd->delta.filechecksum = 0;
275 pd->delta.filechecksumtype = REPOKEY_TYPE_SHA1;
276 if ((str = find_attr("type", atts)) != 0)
278 pd->delta.filechecksumtype = solv_chksum_str2type(str);
279 if (!pd->delta.filechecksumtype)
280 pool_debug(pool, SOLV_ERROR, "unknown checksum type: '%s'\n", str);
292 endElement(void *userData, const char *name)
294 struct parsedata *pd = userData;
295 Pool *pool = pd->pool;
299 fprintf(stderr, "end: %s\n", name);
301 if (pd->depth != pd->statedepth)
305 fprintf(stderr, "back from unknown %d %d %d\n", pd->state, pd->depth, pd->statedepth);
316 case STATE_NEWPACKAGE:
320 /* read all data for a deltarpm. commit into attributes */
322 struct deltarpm *d = &pd->delta;
324 handle = repodata_new_handle(pd->data);
325 /* we commit all handles later on in one go so that the
326 * repodata code doesn't need to realloc every time */
327 pd->handles = solv_extend(pd->handles, pd->nhandles, 1, sizeof(Id), 63);
328 pd->handles[pd->nhandles++] = handle;
329 repodata_set_id(pd->data, handle, DELTA_PACKAGE_NAME, pd->newpkgname);
330 repodata_set_id(pd->data, handle, DELTA_PACKAGE_EVR, pd->newpkgevr);
331 repodata_set_id(pd->data, handle, DELTA_PACKAGE_ARCH, pd->newpkgarch);
334 repodata_set_deltalocation(pd->data, handle, 0, 0, d->location);
336 repodata_set_poolstr(pd->data, handle, DELTA_LOCATION_BASE, d->locbase);
339 repodata_set_num(pd->data, handle, DELTA_DOWNLOADSIZE, d->downloadsize);
341 repodata_set_checksum(pd->data, handle, DELTA_CHECKSUM, d->filechecksumtype, d->filechecksum);
344 repodata_set_id(pd->data, handle, DELTA_BASE_EVR, d->bevr[0]);
345 repodata_set_id(pd->data, handle, DELTA_SEQ_NAME, d->seqname);
346 repodata_set_id(pd->data, handle, DELTA_SEQ_EVR, d->seqevr);
347 /* should store as binary blob! */
348 repodata_set_str(pd->data, handle, DELTA_SEQ_NUM, d->seqnum);
351 pd->delta.filechecksum = solv_free(pd->delta.filechecksum);
352 pd->delta.bevr = solv_free(pd->delta.bevr);
354 pd->delta.seqnum = solv_free(pd->delta.seqnum);
355 pd->delta.location = solv_free(pd->delta.location);
356 pd->delta.locbase = solv_free(pd->delta.locbase);
359 pd->delta.location = solv_strdup(pd->content);
362 pd->delta.filechecksum = solv_strdup(pd->content);
365 pd->delta.downloadsize = strtoull(pd->content, 0, 10);
368 if ((str = pd->content))
371 s1 = strrchr(str, '-');
374 for (s2 = s1 - 1; s2 > str; s2--)
379 for (s2 = s2 - 1; s2 > str; s2--)
384 pd->delta.seqevr = pool_strn2id(pool, s2 + 1, s1 - s2 - 1, 1);
385 pd->delta.seqname = pool_strn2id(pool, str, s2 - str, 1);
390 pd->delta.seqnum = solv_strdup(str);
396 pd->state = pd->sbtab[pd->state];
402 characterData(void *userData, const XML_Char *s, int len)
404 struct parsedata *pd = userData;
409 l = pd->lcontent + len + 1;
410 if (l > pd->acontent)
412 pd->content = solv_realloc(pd->content, l + 256);
413 pd->acontent = l + 256;
415 c = pd->content + pd->lcontent;
422 #define BUFF_SIZE 8192
425 repo_add_deltainfoxml(Repo *repo, FILE *fp, int flags)
427 Pool *pool = repo->pool;
431 struct stateswitch *sw;
435 data = repo_add_repodata(repo, flags);
437 memset(&pd, 0, sizeof(pd));
438 for (i = 0, sw = stateswitches; sw->from != NUMSTATES; i++, sw++)
440 if (!pd.swtab[sw->from])
441 pd.swtab[sw->from] = sw;
442 pd.sbtab[sw->to] = sw->from;
448 pd.content = solv_malloc(256);
452 parser = XML_ParserCreate(NULL);
453 XML_SetUserData(parser, &pd);
454 XML_SetElementHandler(parser, startElement, endElement);
455 XML_SetCharacterDataHandler(parser, characterData);
458 l = fread(buf, 1, sizeof(buf), fp);
459 if (XML_Parse(parser, buf, l, l == 0) == XML_STATUS_ERROR)
461 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));
467 XML_ParserFree(parser);
468 solv_free(pd.content);
470 /* now commit all handles */
472 for (i = 0; i < pd.nhandles; i++)
473 repodata_add_flexarray(pd.data, SOLVID_META, REPOSITORY_DELTAINFO, pd.handles[i]);
474 solv_free(pd.handles);
476 if (!(flags & REPO_NO_INTERNALIZE))
477 repodata_internalize(data);