4 * Parses all files below 'proddir'
5 * See http://en.opensuse.org/Product_Management/Code11
8 * Copyright (c) 2008, Novell Inc.
10 * This program is licensed under the BSD license, read LICENSE.BSD
11 * for further information
14 #include <sys/types.h>
31 #include "tools_util.h"
32 #include "repo_content.h"
33 #include "repo_zyppdb.h"
34 #include "repo_products.h"
35 #include "repo_releasefile_products.h"
70 /* !! must be sorted by first column !! */
71 static struct stateswitch stateswitches[] = {
72 { STATE_START, "product", STATE_PRODUCT, 0 },
73 { STATE_PRODUCT, "vendor", STATE_VENDOR, 1 },
74 { STATE_PRODUCT, "name", STATE_NAME, 1 },
75 { STATE_PRODUCT, "version", STATE_VERSION, 1 },
76 { STATE_PRODUCT, "release", STATE_RELEASE, 1 },
77 { STATE_PRODUCT, "arch", STATE_ARCH, 1 },
78 { STATE_PRODUCT, "productline", STATE_PRODUCTLINE, 1 },
79 { STATE_PRODUCT, "summary", STATE_SUMMARY, 1 },
80 { STATE_PRODUCT, "shortsummary", STATE_SHORTSUMMARY, 1 },
81 { STATE_PRODUCT, "description", STATE_DESCRIPTION, 1 },
82 { STATE_PRODUCT, "register", STATE_REGISTER, 0 },
83 { STATE_PRODUCT, "urls", STATE_URLS, 0 },
84 { STATE_PRODUCT, "runtimeconfig", STATE_RUNTIMECONFIG, 0 },
85 { STATE_PRODUCT, "linguas", STATE_LINGUAS, 0 },
86 { STATE_PRODUCT, "updaterepokey", STATE_UPDATEREPOKEY, 1 },
87 { STATE_PRODUCT, "cpeid", STATE_CPEID, 1 },
88 { STATE_URLS, "url", STATE_URL, 1 },
89 { STATE_LINGUAS, "lang", STATE_LANG, 0 },
90 { STATE_REGISTER, "target", STATE_TARGET, 1 },
91 { STATE_REGISTER, "release", STATE_REGRELEASE, 1 },
109 struct stateswitch *swtab[NUMSTATES];
110 enum state sbtab[NUMSTATES];
125 ino_t currentproduct;
132 * find value for xml attribute
133 * I: txt, name of attribute
134 * I: atts, list of key/value attributes
135 * O: pointer to value of matching key, or NULL
139 static inline const char *
140 find_attr(const char *txt, const char **atts)
142 for (; *atts; atts += 2)
144 if (!strcmp(*atts, txt))
152 * XML callback: startElement
156 startElement(void *userData, const char *name, const char **atts)
158 struct parsedata *pd = userData;
159 Pool *pool = pd->pool;
160 Solvable *s = pd->solvable;
161 struct stateswitch *sw;
164 fprintf(stderr, "start: [%d]%s\n", pd->state, name);
166 if (pd->depth != pd->statedepth)
173 if (!pd->swtab[pd->state]) /* no statetable -> no substates */
176 fprintf(stderr, "into unknown: %s (from: %d)\n", name, pd->state);
180 for (sw = pd->swtab[pd->state]; sw->from == pd->state; sw++) /* find name in statetable */
181 if (!strcmp(sw->ename, name))
184 if (sw->from != pd->state)
187 fprintf(stderr, "into unknown: %s (from: %d)\n", name, pd->state);
192 pd->docontent = sw->docontent;
193 pd->statedepth = pd->depth;
200 /* parse 'schemeversion' and store in global variable */
202 const char * scheme = find_attr("schemeversion", atts);
203 pd->productscheme = (scheme && *scheme) ? atoi(scheme) : -1;
207 s = pd->solvable = pool_id2solvable(pool, repo_add_solvable(pd->repo));
208 pd->handle = s - pool->solvables;
212 /* <summary lang="xy">... */
214 case STATE_DESCRIPTION:
215 pd->tmplang = join_dup(&pd->jd, find_attr("lang", atts));
218 pd->urltype = pool_str2id(pd->pool, find_attr("name", atts), 1);
227 endElement(void *userData, const char *name)
229 struct parsedata *pd = userData;
230 Solvable *s = pd->solvable;
233 fprintf(stderr, "end: [%d]%s\n", pd->state, name);
235 if (pd->depth != pd->statedepth)
239 fprintf(stderr, "back from unknown %d %d %d\n", pd->state, pd->depth, pd->statedepth);
250 /* product done, finish solvable */
252 repodata_set_num(pd->data, pd->handle, SOLVABLE_INSTALLTIME, pd->ctime);
255 repodata_set_str(pd->data, pd->handle, PRODUCT_REFERENCEFILE, pd->basename);
257 /* this is where <productsdir>/baseproduct points to */
258 if (pd->currentproduct == pd->baseproduct)
259 repodata_set_str(pd->data, pd->handle, PRODUCT_TYPE, "base");
264 s->evr = makeevr(pd->pool, join2(&pd->jd, pd->tmpvers, "-", pd->tmprel));
267 fprintf(stderr, "Seen <release> but no <version>\n");
270 else if (pd->tmpvers)
271 s->evr = makeevr(pd->pool, pd->tmpvers); /* just version, no release */
272 pd->tmpvers = solv_free((void *)pd->tmpvers);
273 pd->tmprel = solv_free((void *)pd->tmprel);
275 s->arch = ARCH_NOARCH;
278 if (s->name && s->arch != ARCH_SRC && s->arch != ARCH_NOSRC)
279 s->provides = repo_addid_dep(pd->repo, s->provides, pool_rel2id(pd->pool, s->name, s->evr, REL_EQ, 1), 0);
283 s->vendor = pool_str2id(pd->pool, pd->content, 1);
286 s->name = pool_str2id(pd->pool, join2(&pd->jd, "product", ":", pd->content), 1);
289 pd->tmpvers = solv_strdup(pd->content);
292 pd->tmprel = solv_strdup(pd->content);
295 s->arch = pool_str2id(pd->pool, pd->content, 1);
297 case STATE_PRODUCTLINE:
298 repodata_set_str(pd->data, pd->handle, PRODUCT_PRODUCTLINE, pd->content);
300 case STATE_UPDATEREPOKEY:
304 repodata_set_str(pd->data, pd->handle, pool_id2langid(pd->pool, SOLVABLE_SUMMARY, pd->tmplang, 1), pd->content);
306 case STATE_SHORTSUMMARY:
307 repodata_set_str(pd->data, pd->handle, PRODUCT_SHORTLABEL, pd->content);
309 case STATE_DESCRIPTION:
310 repodata_set_str(pd->data, pd->handle, pool_id2langid(pd->pool, SOLVABLE_DESCRIPTION, pd->tmplang, 1), pd->content);
315 repodata_add_poolstr_array(pd->data, pd->handle, PRODUCT_URL, pd->content);
316 repodata_add_idarray(pd->data, pd->handle, PRODUCT_URL_TYPE, pd->urltype);
320 repodata_set_str(pd->data, pd->handle, PRODUCT_REGISTER_TARGET, pd->content);
322 case STATE_REGRELEASE:
323 repodata_set_str(pd->data, pd->handle, PRODUCT_REGISTER_RELEASE, pd->content);
327 repodata_set_str(pd->data, pd->handle, SOLVABLE_CPEID, pd->content);
332 pd->state = pd->sbtab[pd->state];
336 fprintf(stderr, "end: [%s] -> %d\n", name, pd->state);
342 characterData(void *userData, const XML_Char *s, int len)
344 struct parsedata *pd = userData;
349 l = pd->lcontent + len + 1;
350 if (l > pd->acontent)
352 pd->content = solv_realloc(pd->content, l + 256);
353 pd->acontent = l + 256;
355 c = pd->content + pd->lcontent;
362 #define BUFF_SIZE 8192
366 * add single product to repo
371 add_code11_product(struct parsedata *pd, FILE *fp)
378 if (!fstat(fileno(fp), &st))
380 pd->currentproduct = st.st_ino;
381 pd->ctime = (unsigned int)st.st_ctime;
385 pd->currentproduct = pd->baseproduct + 1; /* make it != baseproduct if stat fails */
390 parser = XML_ParserCreate(NULL);
391 XML_SetUserData(parser, pd);
392 XML_SetElementHandler(parser, startElement, endElement);
393 XML_SetCharacterDataHandler(parser, characterData);
397 l = fread(buf, 1, sizeof(buf), fp);
398 if (XML_Parse(parser, buf, l, l == 0) == XML_STATUS_ERROR)
400 pool_debug(pd->pool, SOLV_ERROR, "%s: %s at line %u:%u\n", pd->filename, XML_ErrorString(XML_GetErrorCode(parser)), (unsigned int)XML_GetCurrentLineNumber(parser), (unsigned int)XML_GetCurrentColumnNumber(parser));
401 pool_debug(pd->pool, SOLV_ERROR, "skipping this product\n");
402 XML_ParserFree(parser);
408 XML_ParserFree(parser);
413 repo_add_code11_products(Repo *repo, const char *dirpath, int flags)
417 struct stateswitch *sw;
421 data = repo_add_repodata(repo, flags);
423 memset(&pd, 0, sizeof(pd));
425 pd.pool = repo->pool;
428 pd.content = solv_malloc(256);
431 for (i = 0, sw = stateswitches; sw->from != NUMSTATES; i++, sw++)
433 if (!pd.swtab[sw->from])
434 pd.swtab[sw->from] = sw;
435 pd.sbtab[sw->to] = sw->from;
438 dir = opendir(dirpath);
441 struct dirent *entry;
445 /* check for <productsdir>/baseproduct on code11 and remember its target inode */
446 if (stat(join2(&pd.jd, dirpath, "/", "baseproduct"), &st) == 0) /* follow symlink */
447 pd.baseproduct = st.st_ino;
451 while ((entry = readdir(dir)))
453 int len = strlen(entry->d_name);
455 if (len <= 5 || strcmp(entry->d_name + len - 5, ".prod") != 0)
457 fullpath = join2(&pd.jd, dirpath, "/", entry->d_name);
458 fp = fopen(fullpath, "r");
464 pd.filename = fullpath;
465 pd.basename = entry->d_name;
466 add_code11_product(&pd, fp);
471 solv_free(pd.content);
472 join_freemem(&pd.jd);
474 if (!(flags & REPO_NO_INTERNALIZE))
475 repodata_internalize(data);
480 /******************************************************************************************/
484 * read all installed products
486 * try proddir (reading all .xml files from this directory) first
487 * if not available, assume non-code11 layout and parse /etc/xyz-release
489 * parse each one as a product
492 /* Oh joy! Three parsers for the price of one! */
495 repo_add_products(Repo *repo, const char *proddir, const char *root, int flags)
503 dir = opendir(proddir);
506 /* assume code11 stype products */
508 return repo_add_code11_products(repo, proddir, flags);
512 /* code11 didn't work, try old code10 zyppdb */
513 fullpath = solv_dupjoin(root ? root : 0, "/var/lib/zypp/db/products", 0);
514 dir = opendir(fullpath);
518 /* assume code10 style products */
519 ret = repo_add_zyppdb_products(repo, fullpath, flags);
525 /* code10/11 didn't work, try -release files parsing */
526 fullpath = solv_dupjoin(root ? root : 0, "/etc", 0);
527 dir = opendir(fullpath);
531 ret = repo_add_releasefile_products(repo, fullpath, flags);
536 /* no luck. print an error message in case the root argument is wrong */
540 /* the least we can do... */
541 if (!(flags & REPO_NO_INTERNALIZE) && (flags & REPO_REUSE_REPODATA) != 0)
542 repodata_internalize(repo_last_repodata(repo));