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_releasefile_products.h"
49 STATE_DESCRIPTION, // 8
50 STATE_UPDATEREPOKEY, // 9 should go away
54 STATE_RUNTIMECONFIG, // 12
59 STATE_REGRELEASE, // 18
60 STATE_PRODUCTLINE, // 19
71 /* !! must be sorted by first column !! */
72 static struct stateswitch stateswitches[] = {
73 { STATE_START, "product", STATE_PRODUCT, 0 },
74 { STATE_PRODUCT, "vendor", STATE_VENDOR, 1 },
75 { STATE_PRODUCT, "name", STATE_NAME, 1 },
76 { STATE_PRODUCT, "version", STATE_VERSION, 1 },
77 { STATE_PRODUCT, "release", STATE_RELEASE, 1 },
78 { STATE_PRODUCT, "arch", STATE_ARCH, 1 },
79 { STATE_PRODUCT, "productline", STATE_PRODUCTLINE, 1 },
80 { STATE_PRODUCT, "summary", STATE_SUMMARY, 1 },
81 { STATE_PRODUCT, "shortsummary", STATE_SHORTSUMMARY, 1 },
82 { STATE_PRODUCT, "description", STATE_DESCRIPTION, 1 },
83 { STATE_PRODUCT, "register", STATE_REGISTER, 0 },
84 { STATE_PRODUCT, "urls", STATE_URLS, 0 },
85 { STATE_PRODUCT, "runtimeconfig", STATE_RUNTIMECONFIG, 0 },
86 { STATE_PRODUCT, "linguas", STATE_LINGUAS, 0 },
87 { STATE_PRODUCT, "updaterepokey", STATE_UPDATEREPOKEY, 1 },
88 { STATE_PRODUCT, "cpeid", STATE_CPEID, 1 },
89 { STATE_URLS, "url", STATE_URL, 1 },
90 { STATE_LINGUAS, "lang", STATE_LANG, 0 },
91 { STATE_REGISTER, "target", STATE_TARGET, 1 },
92 { STATE_REGISTER, "release", STATE_REGRELEASE, 1 },
110 struct stateswitch *swtab[NUMSTATES];
111 enum state sbtab[NUMSTATES];
117 const char *tmpurltype;
125 ino_t currentproduct;
128 Id langcache[ID_NUM_INTERNAL];
134 * find value for xml attribute
135 * I: txt, name of attribute
136 * I: atts, list of key/value attributes
138 * O: pointer to value of matching key, or NULL
142 static inline const char *
143 find_attr(const char *txt, const char **atts, int dup)
145 for (; *atts; atts += 2)
147 if (!strcmp(*atts, txt))
148 return dup ? solv_strdup(atts[1]) : atts[1];
155 * create localized tag
159 langtag(struct parsedata *pd, Id tag, const char *language)
161 if (language && !language[0])
163 if (!language || tag >= ID_NUM_INTERNAL)
164 return pool_id2langid(pd->repo->pool, tag, language, 1);
165 if (!pd->langcache[tag])
166 pd->langcache[tag] = pool_id2langid(pd->repo->pool, tag, language, 1);
167 return pd->langcache[tag];
172 * XML callback: startElement
176 startElement(void *userData, const char *name, const char **atts)
178 struct parsedata *pd = userData;
179 Pool *pool = pd->pool;
180 Solvable *s = pd->solvable;
181 struct stateswitch *sw;
184 fprintf(stderr, "start: [%d]%s\n", pd->state, name);
186 if (pd->depth != pd->statedepth)
193 if (!pd->swtab[pd->state]) /* no statetable -> no substates */
196 fprintf(stderr, "into unknown: %s (from: %d)\n", name, pd->state);
200 for (sw = pd->swtab[pd->state]; sw->from == pd->state; sw++) /* find name in statetable */
201 if (!strcmp(sw->ename, name))
204 if (sw->from != pd->state)
207 fprintf(stderr, "into unknown: %s (from: %d)\n", name, pd->state);
212 pd->docontent = sw->docontent;
213 pd->statedepth = pd->depth;
220 /* parse 'schemeversion' and store in global variable */
222 const char * scheme = find_attr("schemeversion", atts, 0);
223 pd->productscheme = (scheme && *scheme) ? atoi(scheme) : -1;
227 s = pd->solvable = pool_id2solvable(pool, repo_add_solvable(pd->repo));
228 pd->handle = s - pool->solvables;
232 /* <summary lang="xy">... */
234 pd->tmplang = find_attr("lang", atts, 1);
236 case STATE_DESCRIPTION:
237 pd->tmplang = find_attr("lang", atts, 1);
240 pd->tmpurltype = find_attr("name", atts, 1);
249 endElement(void *userData, const char *name)
251 struct parsedata *pd = userData;
252 Solvable *s = pd->solvable;
255 fprintf(stderr, "end: [%d]%s\n", pd->state, name);
257 if (pd->depth != pd->statedepth)
261 fprintf(stderr, "back from unknown %d %d %d\n", pd->state, pd->depth, pd->statedepth);
272 /* product done, finish solvable */
274 repodata_set_num(pd->data, pd->handle, SOLVABLE_INSTALLTIME, pd->ctime);
277 repodata_set_str(pd->data, pd->handle, PRODUCT_REFERENCEFILE, pd->basename);
279 /* this is where <productsdir>/baseproduct points to */
280 if (pd->currentproduct == pd->baseproduct)
281 repodata_set_str(pd->data, pd->handle, PRODUCT_TYPE, "base");
286 s->evr = makeevr(pd->pool, join2(pd->tmpvers, "-", pd->tmprel));
289 fprintf(stderr, "Seen <release> but no <version>\n");
292 else if (pd->tmpvers)
293 s->evr = makeevr(pd->pool, pd->tmpvers); /* just version, no release */
294 pd->tmpvers = solv_free((void *)pd->tmpvers);
295 pd->tmprel = solv_free((void *)pd->tmprel);
297 s->arch = ARCH_NOARCH;
300 if (s->name && s->arch != ARCH_SRC && s->arch != ARCH_NOSRC)
301 s->provides = repo_addid_dep(pd->repo, s->provides, pool_rel2id(pd->pool, s->name, s->evr, REL_EQ, 1), 0);
305 s->vendor = pool_str2id(pd->pool, pd->content, 1);
308 s->name = pool_str2id(pd->pool, join2("product", ":", pd->content), 1);
311 pd->tmpvers = solv_strdup(pd->content);
314 pd->tmprel = solv_strdup(pd->content);
317 s->arch = pool_str2id(pd->pool, pd->content, 1);
319 case STATE_PRODUCTLINE:
320 repodata_set_str(pd->data, pd->handle, PRODUCT_PRODUCTLINE, pd->content);
322 case STATE_UPDATEREPOKEY:
326 repodata_set_str(pd->data, pd->handle, langtag(pd, SOLVABLE_SUMMARY, pd->tmplang), pd->content);
327 pd->tmplang = solv_free((void *)pd->tmplang);
329 case STATE_SHORTSUMMARY:
330 repodata_set_str(pd->data, pd->handle, PRODUCT_SHORTLABEL, pd->content);
332 case STATE_DESCRIPTION:
333 repodata_set_str(pd->data, pd->handle, langtag(pd, SOLVABLE_DESCRIPTION, pd->tmplang), pd->content );
334 pd->tmplang = solv_free((void *)pd->tmplang);
339 repodata_add_poolstr_array(pd->data, pd->handle, PRODUCT_URL, pd->content);
340 repodata_add_idarray(pd->data, pd->handle, PRODUCT_URL_TYPE, pool_str2id(pd->pool, pd->tmpurltype, 1));
342 pd->tmpurltype = solv_free((void *)pd->tmpurltype);
345 repodata_set_str(pd->data, pd->handle, PRODUCT_REGISTER_TARGET, pd->content);
347 case STATE_REGRELEASE:
348 repodata_set_str(pd->data, pd->handle, PRODUCT_REGISTER_RELEASE, pd->content);
352 repodata_set_str(pd->data, pd->handle, SOLVABLE_CPEID, pd->content);
357 pd->state = pd->sbtab[pd->state];
361 fprintf(stderr, "end: [%s] -> %d\n", name, pd->state);
367 characterData(void *userData, const XML_Char *s, int len)
369 struct parsedata *pd = userData;
374 l = pd->lcontent + len + 1;
375 if (l > pd->acontent)
377 pd->content = solv_realloc(pd->content, l + 256);
378 pd->acontent = l + 256;
380 c = pd->content + pd->lcontent;
387 #define BUFF_SIZE 8192
391 * add single product to repo
396 add_code11_product(struct parsedata *pd, FILE *fp)
402 if (!fstat(fileno(fp), &st))
404 pd->currentproduct = st.st_ino;
405 pd->ctime = (unsigned int)st.st_ctime;
409 pd->currentproduct = pd->baseproduct + 1; /* make it != baseproduct if stat fails */
414 XML_Parser parser = XML_ParserCreate(NULL);
415 XML_SetUserData(parser, pd);
416 XML_SetElementHandler(parser, startElement, endElement);
417 XML_SetCharacterDataHandler(parser, characterData);
421 l = fread(buf, 1, sizeof(buf), fp);
422 if (XML_Parse(parser, buf, l, l == 0) == XML_STATUS_ERROR)
424 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));
425 pool_debug(pd->pool, SOLV_ERROR, "skipping this product\n");
426 XML_ParserFree(parser);
432 XML_ParserFree(parser);
437 repo_add_code11_products(Repo *repo, const char *dirpath, int flags)
441 struct stateswitch *sw;
445 data = repo_add_repodata(repo, flags);
447 memset(&pd, 0, sizeof(pd));
449 pd.pool = repo->pool;
452 pd.content = solv_malloc(256);
455 for (i = 0, sw = stateswitches; sw->from != NUMSTATES; i++, sw++)
457 if (!pd.swtab[sw->from])
458 pd.swtab[sw->from] = sw;
459 pd.sbtab[sw->to] = sw->from;
462 dir = opendir(dirpath);
465 struct dirent *entry;
469 /* check for <productsdir>/baseproduct on code11 and remember its target inode */
470 if (stat(join2(dirpath, "/", "baseproduct"), &st) == 0) /* follow symlink */
471 pd.baseproduct = st.st_ino;
475 while ((entry = readdir(dir)))
477 int len = strlen(entry->d_name);
478 if (len <= 5 || strcmp(entry->d_name + len - 5, ".prod") != 0)
480 fullpath = join2(dirpath, "/", entry->d_name);
481 FILE *fp = fopen(fullpath, "r");
487 pd.filename = fullpath;
488 pd.basename = entry->d_name;
489 add_code11_product(&pd, fp);
494 solv_free((void *)pd.tmplang);
495 solv_free(pd.content);
498 if (!(flags & REPO_NO_INTERNALIZE))
499 repodata_internalize(data);
503 /******************************************************************************************/
507 * read all installed products
509 * try proddir (reading all .xml files from this directory) first
510 * if not available, assume non-code11 layout and parse /etc/xyz-release
512 * parse each one as a product
515 /* Oh joy! Three parsers for the price of one! */
518 repo_add_products(Repo *repo, const char *proddir, const char *root, int flags)
520 const char *fullpath;
523 dir = opendir(proddir);
526 /* assume code11 stype products */
528 repo_add_code11_products(repo, proddir, flags);
532 /* code11 didn't work, try old zyppdb */
533 fullpath = root ? join2(root, "", "/var/lib/zypp/db/products") : "/var/lib/zypp/db/products";
534 dir = opendir(fullpath);
538 /* assume code10 style products */
539 repo_add_zyppdb_products(repo, fullpath, flags);
544 /* code11 didn't work, try -release files parsing */
545 fullpath = root ? join2(root, "", "/etc") : "/etc";
546 dir = opendir(fullpath);
550 repo_add_releasefile_products(repo, fullpath, flags);
555 /* no luck. print an error message in case the root argument is wrong */
559 /* the least we can do... */
560 if (!(flags & REPO_NO_INTERNALIZE) && (flags & REPO_REUSE_REPODATA) != 0)
561 repodata_internalize(repo_last_repodata(repo));