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
17 #include <sys/types.h>
35 #include "tools_util.h"
36 #include "repo_content.h"
37 #include "repo_zyppdb.h"
38 #include "repo_products.h"
39 #include "repo_releasefile_products.h"
77 /* !! must be sorted by first column !! */
78 static struct stateswitch stateswitches[] = {
79 { STATE_START, "product", STATE_PRODUCT, 0 },
80 { STATE_PRODUCT, "vendor", STATE_VENDOR, 1 },
81 { STATE_PRODUCT, "name", STATE_NAME, 1 },
82 { STATE_PRODUCT, "version", STATE_VERSION, 1 },
83 { STATE_PRODUCT, "release", STATE_RELEASE, 1 },
84 { STATE_PRODUCT, "arch", STATE_ARCH, 1 },
85 { STATE_PRODUCT, "productline", STATE_PRODUCTLINE, 1 },
86 { STATE_PRODUCT, "summary", STATE_SUMMARY, 1 },
87 { STATE_PRODUCT, "shortsummary", STATE_SHORTSUMMARY, 1 },
88 { STATE_PRODUCT, "description", STATE_DESCRIPTION, 1 },
89 { STATE_PRODUCT, "register", STATE_REGISTER, 0 },
90 { STATE_PRODUCT, "urls", STATE_URLS, 0 },
91 { STATE_PRODUCT, "runtimeconfig", STATE_RUNTIMECONFIG, 0 },
92 { STATE_PRODUCT, "linguas", STATE_LINGUAS, 0 },
93 { STATE_PRODUCT, "updaterepokey", STATE_UPDATEREPOKEY, 1 },
94 { STATE_PRODUCT, "cpeid", STATE_CPEID, 1 },
95 { STATE_PRODUCT, "endoflife", STATE_ENDOFLIFE, 1 },
96 { STATE_URLS, "url", STATE_URL, 1 },
97 { STATE_LINGUAS, "lang", STATE_LANG, 0 },
98 { STATE_REGISTER, "target", STATE_TARGET, 1 },
99 { STATE_REGISTER, "release", STATE_REGRELEASE, 1 },
100 { STATE_REGISTER, "updates", STATE_REGUPDATES, 0 },
101 { STATE_REGUPDATES, "repository", STATE_REGUPDREPO, 0 },
106 const char *filename;
107 const char *basename;
119 struct stateswitch *swtab[NUMSTATES];
120 enum state sbtab[NUMSTATES];
135 ino_t currentproduct;
142 * find value for xml attribute
143 * I: txt, name of attribute
144 * I: atts, list of key/value attributes
145 * O: pointer to value of matching key, or NULL
149 static inline const char *
150 find_attr(const char *txt, const char **atts)
152 for (; *atts; atts += 2)
154 if (!strcmp(*atts, txt))
161 datestr2timestamp(const char *date)
168 for (p = date; *p >= '0' && *p <= '9'; p++)
172 memset(&tm, 0, sizeof(tm));
173 p = strptime(date, "%F%T", &tm);
176 memset(&tm, 0, sizeof(tm));
177 p = strptime(date, "%F", &tm);
185 * XML callback: startElement
189 startElement(void *userData, const char *name, const char **atts)
191 struct parsedata *pd = userData;
192 Pool *pool = pd->pool;
193 Solvable *s = pd->solvable;
194 struct stateswitch *sw;
197 fprintf(stderr, "start: [%d]%s\n", pd->state, name);
199 if (pd->depth != pd->statedepth)
206 if (!pd->swtab[pd->state]) /* no statetable -> no substates */
209 fprintf(stderr, "into unknown: %s (from: %d)\n", name, pd->state);
213 for (sw = pd->swtab[pd->state]; sw->from == pd->state; sw++) /* find name in statetable */
214 if (!strcmp(sw->ename, name))
217 if (sw->from != pd->state)
220 fprintf(stderr, "into unknown: %s (from: %d)\n", name, pd->state);
225 pd->docontent = sw->docontent;
226 pd->statedepth = pd->depth;
233 /* parse 'schemeversion' and store in global variable */
235 const char * scheme = find_attr("schemeversion", atts);
236 pd->productscheme = (scheme && *scheme) ? atoi(scheme) : -1;
240 s = pd->solvable = pool_id2solvable(pool, repo_add_solvable(pd->repo));
241 pd->handle = s - pool->solvables;
245 /* <summary lang="xy">... */
247 case STATE_DESCRIPTION:
248 pd->tmplang = join_dup(&pd->jd, find_attr("lang", atts));
251 pd->urltype = pool_str2id(pd->pool, find_attr("name", atts), 1);
253 case STATE_REGUPDREPO:
255 const char *repoid = find_attr("repoid", atts);
256 if (repoid && *repoid)
258 Id h = repodata_new_handle(pd->data);
259 repodata_set_str(pd->data, h, PRODUCT_UPDATES_REPOID, repoid);
260 repodata_add_flexarray(pd->data, pd->handle, PRODUCT_UPDATES, h);
271 endElement(void *userData, const char *name)
273 struct parsedata *pd = userData;
274 Solvable *s = pd->solvable;
277 fprintf(stderr, "end: [%d]%s\n", pd->state, name);
279 if (pd->depth != pd->statedepth)
283 fprintf(stderr, "back from unknown %d %d %d\n", pd->state, pd->depth, pd->statedepth);
294 /* product done, finish solvable */
296 repodata_set_num(pd->data, pd->handle, SOLVABLE_INSTALLTIME, pd->ctime);
299 repodata_set_str(pd->data, pd->handle, PRODUCT_REFERENCEFILE, pd->basename);
301 /* this is where <productsdir>/baseproduct points to */
302 if (pd->currentproduct == pd->baseproduct)
303 repodata_set_str(pd->data, pd->handle, PRODUCT_TYPE, "base");
308 s->evr = makeevr(pd->pool, join2(&pd->jd, pd->tmpvers, "-", pd->tmprel));
311 fprintf(stderr, "Seen <release> but no <version>\n");
314 else if (pd->tmpvers)
315 s->evr = makeevr(pd->pool, pd->tmpvers); /* just version, no release */
316 pd->tmpvers = solv_free((void *)pd->tmpvers);
317 pd->tmprel = solv_free((void *)pd->tmprel);
319 s->arch = ARCH_NOARCH;
322 if (s->name && s->arch != ARCH_SRC && s->arch != ARCH_NOSRC)
323 s->provides = repo_addid_dep(pd->repo, s->provides, pool_rel2id(pd->pool, s->name, s->evr, REL_EQ, 1), 0);
327 s->vendor = pool_str2id(pd->pool, pd->content, 1);
330 s->name = pool_str2id(pd->pool, join2(&pd->jd, "product", ":", pd->content), 1);
333 pd->tmpvers = solv_strdup(pd->content);
336 pd->tmprel = solv_strdup(pd->content);
339 s->arch = pool_str2id(pd->pool, pd->content, 1);
341 case STATE_PRODUCTLINE:
342 repodata_set_str(pd->data, pd->handle, PRODUCT_PRODUCTLINE, pd->content);
344 case STATE_UPDATEREPOKEY:
348 repodata_set_str(pd->data, pd->handle, pool_id2langid(pd->pool, SOLVABLE_SUMMARY, pd->tmplang, 1), pd->content);
350 case STATE_SHORTSUMMARY:
351 repodata_set_str(pd->data, pd->handle, PRODUCT_SHORTLABEL, pd->content);
353 case STATE_DESCRIPTION:
354 repodata_set_str(pd->data, pd->handle, pool_id2langid(pd->pool, SOLVABLE_DESCRIPTION, pd->tmplang, 1), pd->content);
359 repodata_add_poolstr_array(pd->data, pd->handle, PRODUCT_URL, pd->content);
360 repodata_add_idarray(pd->data, pd->handle, PRODUCT_URL_TYPE, pd->urltype);
364 repodata_set_str(pd->data, pd->handle, PRODUCT_REGISTER_TARGET, pd->content);
366 case STATE_REGRELEASE:
367 repodata_set_str(pd->data, pd->handle, PRODUCT_REGISTER_RELEASE, pd->content);
371 repodata_set_str(pd->data, pd->handle, SOLVABLE_CPEID, pd->content);
373 case STATE_ENDOFLIFE:
376 time_t t = datestr2timestamp(pd->content);
378 repodata_set_num(pd->data, pd->handle, PRODUCT_ENDOFLIFE, (unsigned long long)t);
385 pd->state = pd->sbtab[pd->state];
389 fprintf(stderr, "end: [%s] -> %d\n", name, pd->state);
395 characterData(void *userData, const XML_Char *s, int len)
397 struct parsedata *pd = userData;
402 l = pd->lcontent + len + 1;
403 if (l > pd->acontent)
405 pd->content = solv_realloc(pd->content, l + 256);
406 pd->acontent = l + 256;
408 c = pd->content + pd->lcontent;
415 #define BUFF_SIZE 8192
419 * add single product to repo
424 add_code11_product(struct parsedata *pd, FILE *fp)
431 if (!fstat(fileno(fp), &st))
433 pd->currentproduct = st.st_ino;
434 pd->ctime = (unsigned int)st.st_ctime;
438 pd->currentproduct = pd->baseproduct + 1; /* make it != baseproduct if stat fails */
439 pool_error(pd->pool, 0, "fstat: %s", strerror(errno));
443 parser = XML_ParserCreate(NULL);
444 XML_SetUserData(parser, pd);
445 XML_SetElementHandler(parser, startElement, endElement);
446 XML_SetCharacterDataHandler(parser, characterData);
450 l = fread(buf, 1, sizeof(buf), fp);
451 if (XML_Parse(parser, buf, l, l == 0) == XML_STATUS_ERROR)
453 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));
454 pool_debug(pd->pool, SOLV_ERROR, "skipping this product\n");
455 XML_ParserFree(parser);
461 XML_ParserFree(parser);
466 repo_add_code11_products(Repo *repo, const char *dirpath, int flags)
470 struct stateswitch *sw;
474 data = repo_add_repodata(repo, flags);
476 memset(&pd, 0, sizeof(pd));
478 pd.pool = repo->pool;
481 pd.content = solv_malloc(256);
484 for (i = 0, sw = stateswitches; sw->from != NUMSTATES; i++, sw++)
486 if (!pd.swtab[sw->from])
487 pd.swtab[sw->from] = sw;
488 pd.sbtab[sw->to] = sw->from;
491 if (flags & REPO_USE_ROOTDIR)
492 dirpath = pool_prepend_rootdir(repo->pool, dirpath);
493 dir = opendir(dirpath);
496 struct dirent *entry;
500 /* check for <productsdir>/baseproduct on code11 and remember its target inode */
501 if (stat(join2(&pd.jd, dirpath, "/", "baseproduct"), &st) == 0) /* follow symlink */
502 pd.baseproduct = st.st_ino;
506 while ((entry = readdir(dir)))
508 int len = strlen(entry->d_name);
510 if (len <= 5 || strcmp(entry->d_name + len - 5, ".prod") != 0)
512 fullpath = join2(&pd.jd, dirpath, "/", entry->d_name);
513 fp = fopen(fullpath, "r");
516 pool_error(repo->pool, 0, "%s: %s", fullpath, strerror(errno));
519 pd.filename = fullpath;
520 pd.basename = entry->d_name;
521 add_code11_product(&pd, fp);
526 solv_free(pd.content);
527 join_freemem(&pd.jd);
528 if (flags & REPO_USE_ROOTDIR)
529 solv_free((char *)dirpath);
531 if (!(flags & REPO_NO_INTERNALIZE))
532 repodata_internalize(data);
537 /******************************************************************************************/
541 * read all installed products
543 * try proddir (reading all .xml files from this directory) first
544 * if not available, assume non-code11 layout and parse /etc/xyz-release
546 * parse each one as a product
549 /* Oh joy! Three parsers for the price of one! */
552 repo_add_products(Repo *repo, const char *proddir, int flags)
554 const char *fullpath;
559 dir = opendir(flags & REPO_USE_ROOTDIR ? pool_prepend_rootdir_tmp(repo->pool, proddir) : proddir);
562 /* assume code11 stype products */
564 return repo_add_code11_products(repo, proddir, flags);
568 /* code11 didn't work, try old code10 zyppdb */
569 fullpath = "/var/lib/zypp/db/products";
570 if (flags & REPO_USE_ROOTDIR)
571 fullpath = pool_prepend_rootdir_tmp(repo->pool, fullpath);
572 dir = opendir(fullpath);
576 /* assume code10 style products */
577 return repo_add_zyppdb_products(repo, "/var/lib/zypp/db/products", flags);
580 /* code10/11 didn't work, try -release files parsing */
582 if (flags & REPO_USE_ROOTDIR)
583 fullpath = pool_prepend_rootdir_tmp(repo->pool, fullpath);
584 dir = opendir(fullpath);
588 return repo_add_releasefile_products(repo, "/etc", flags);
591 /* no luck. check if the rootdir exists */
592 fullpath = pool_get_rootdir(repo->pool);
593 if (fullpath && *fullpath)
595 dir = opendir(fullpath);
597 return pool_error(repo->pool, -1, "%s: %s", fullpath, strerror(errno));
601 /* the least we can do... */
602 if (!(flags & REPO_NO_INTERNALIZE) && (flags & REPO_REUSE_REPODATA) != 0)
603 repodata_internalize(repo_last_repodata(repo));