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"
47 STATE_DESCRIPTION, // 8
48 STATE_UPDATEREPOKEY, // 9 should go away
52 STATE_RUNTIMECONFIG, // 12
57 STATE_REGRELEASE, // 18
58 STATE_PRODUCTLINE, // 19
69 /* !! must be sorted by first column !! */
70 static struct stateswitch stateswitches[] = {
71 { STATE_START, "product", STATE_PRODUCT, 0 },
72 { STATE_PRODUCT, "vendor", STATE_VENDOR, 1 },
73 { STATE_PRODUCT, "name", STATE_NAME, 1 },
74 { STATE_PRODUCT, "version", STATE_VERSION, 1 },
75 { STATE_PRODUCT, "release", STATE_RELEASE, 1 },
76 { STATE_PRODUCT, "arch", STATE_ARCH, 1 },
77 { STATE_PRODUCT, "productline", STATE_PRODUCTLINE, 1 },
78 { STATE_PRODUCT, "summary", STATE_SUMMARY, 1 },
79 { STATE_PRODUCT, "description", STATE_DESCRIPTION, 1 },
80 { STATE_PRODUCT, "register", STATE_REGISTER, 0 },
81 { STATE_PRODUCT, "urls", STATE_URLS, 0 },
82 { STATE_PRODUCT, "runtimeconfig", STATE_RUNTIMECONFIG, 0 },
83 { STATE_PRODUCT, "linguas", STATE_LINGUAS, 0 },
84 { STATE_PRODUCT, "updaterepokey", STATE_UPDATEREPOKEY, 1 },
85 { STATE_PRODUCT, "cpename", STATE_CPEID, 1 },
86 { STATE_URLS, "url", STATE_URL, 1 },
87 { STATE_LINGUAS, "lang", STATE_LANG, 0 },
88 { STATE_REGISTER, "target", STATE_TARGET, 1 },
89 { STATE_REGISTER, "release", STATE_REGRELEASE, 1 },
105 struct stateswitch *swtab[NUMSTATES];
106 enum state sbtab[NUMSTATES];
107 const char *attribute; /* only print this attribute, if currentproduct == baseproduct */
113 const char *tmpurltype;
121 ino_t currentproduct;
124 Id langcache[ID_NUM_INTERNAL];
130 * find value for xml attribute
131 * I: txt, name of attribute
132 * I: atts, list of key/value attributes
134 * O: pointer to value of matching key, or NULL
138 static inline const char *
139 find_attr(const char *txt, const char **atts, int dup)
141 for (; *atts; atts += 2)
143 if (!strcmp(*atts, txt))
144 return dup ? strdup(atts[1]) : atts[1];
151 * create localized tag
155 langtag(struct parsedata *pd, Id tag, const char *language)
157 if (language && !language[0])
159 if (!language || tag >= ID_NUM_INTERNAL)
160 return pool_id2langid(pd->repo->pool, tag, language, 1);
161 if (!pd->langcache[tag])
162 pd->langcache[tag] = pool_id2langid(pd->repo->pool, tag, language, 1);
163 return pd->langcache[tag];
168 * XML callback: startElement
172 startElement(void *userData, const char *name, const char **atts)
174 struct parsedata *pd = userData;
175 Pool *pool = pd->pool;
176 Solvable *s = pd->solvable;
177 struct stateswitch *sw;
180 fprintf(stderr, "start: [%d]%s\n", pd->state, name);
182 if (pd->depth != pd->statedepth)
189 if (!pd->swtab[pd->state]) /* no statetable -> no substates */
192 fprintf(stderr, "into unknown: %s (from: %d)\n", name, pd->state);
196 for (sw = pd->swtab[pd->state]; sw->from == pd->state; sw++) /* find name in statetable */
197 if (!strcmp(sw->ename, name))
200 if (sw->from != pd->state)
203 fprintf(stderr, "into unknown: %s (from: %d)\n", name, pd->state);
208 pd->docontent = sw->docontent;
209 pd->statedepth = pd->depth;
216 /* parse 'schemeversion' and store in global variable */
218 const char * scheme = find_attr("schemeversion", atts, 0);
219 pd->productscheme = (scheme && *scheme) ? atoi(scheme) : -1;
223 s = pd->solvable = pool_id2solvable(pool, repo_add_solvable(pd->repo));
224 pd->handle = s - pool->solvables;
228 /* <summary lang="xy">... */
230 pd->tmplang = find_attr("lang", atts, 1);
232 case STATE_DESCRIPTION:
233 pd->tmplang = find_attr("lang", atts, 1);
236 pd->tmpurltype = find_attr("name", atts, 1);
245 endElement(void *userData, const char *name)
247 struct parsedata *pd = userData;
248 Solvable *s = pd->solvable;
251 fprintf(stderr, "end: [%d]%s\n", pd->state, name);
253 if (pd->depth != pd->statedepth)
257 fprintf(stderr, "back from unknown %d %d %d\n", pd->state, pd->depth, pd->statedepth);
268 /* product done, finish solvable */
270 repodata_set_num(pd->data, pd->handle, SOLVABLE_INSTALLTIME, pd->ctime);
272 /* this is where <productsdir>/baseproduct points to */
273 if (pd->currentproduct == pd->baseproduct)
274 repodata_set_str(pd->data, pd->handle, PRODUCT_TYPE, "base");
276 // output distver if requested, only if the product is
278 if (pd->currentproduct == pd->baseproduct
280 && !strcmp(pd->attribute, "distver"))
282 printf("%s\n", pd->tmpvers);
288 s->evr = makeevr(pd->pool, join2(pd->tmpvers, "-", pd->tmprel));
291 fprintf(stderr, "Seen <release> but no <version>\n");
294 else if (pd->tmpvers)
295 s->evr = makeevr(pd->pool, pd->tmpvers); /* just version, no release */
296 pd->tmpvers = sat_free((void *)pd->tmpvers);
297 pd->tmprel = sat_free((void *)pd->tmprel);
299 s->arch = ARCH_NOARCH;
300 if (s->arch != ARCH_SRC && s->arch != ARCH_NOSRC)
301 s->provides = repo_addid_dep(pd->repo, s->provides, rel2id(pd->pool, s->name, s->evr, REL_EQ, 1), 0);
305 s->vendor = str2id(pd->pool, pd->content, 1);
308 s->name = str2id(pd->pool, join2("product", ":", pd->content), 1);
311 pd->tmpvers = strdup(pd->content);
314 pd->tmprel = strdup(pd->content);
317 s->arch = 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:
323 repodata_set_str(pd->data, pd->handle, langtag(pd, PRODUCT_UPDATEREPOKEY, pd->tmplang), pd->content);
326 repodata_set_str(pd->data, pd->handle, langtag(pd, SOLVABLE_SUMMARY, pd->tmplang), pd->content);
327 pd->tmplang = sat_free((void *)pd->tmplang);
329 case STATE_DESCRIPTION:
330 repodata_set_str(pd->data, pd->handle, langtag(pd, SOLVABLE_DESCRIPTION, pd->tmplang), pd->content );
331 pd->tmplang = sat_free((void *)pd->tmplang);
336 repodata_add_poolstr_array(pd->data, pd->handle, PRODUCT_URL, pd->content);
337 repodata_add_idarray(pd->data, pd->handle, PRODUCT_URL_TYPE, str2id(pd->pool, pd->tmpurltype, 1));
339 pd->tmpurltype = sat_free((void *)pd->tmpurltype);
342 repodata_set_str(pd->data, pd->handle, PRODUCT_REGISTER_TARGET, pd->content);
343 if (pd->currentproduct == pd->baseproduct
345 && !strcmp(pd->attribute, "register.target"))
347 printf("%s\n", pd->content);
350 case STATE_REGRELEASE:
351 repodata_set_str(pd->data, pd->handle, PRODUCT_REGISTER_RELEASE, pd->content);
352 if (pd->currentproduct == pd->baseproduct
354 && !strcmp(pd->attribute, "register.release"))
356 printf("%s\n", pd->content);
361 repodata_set_str(pd->data, pd->handle, SOLVABLE_CPE_ID, pd->content);
366 pd->state = pd->sbtab[pd->state];
370 fprintf(stderr, "end: [%s] -> %d\n", name, pd->state);
376 characterData(void *userData, const XML_Char *s, int len)
378 struct parsedata *pd = userData;
383 l = pd->lcontent + len + 1;
384 if (l > pd->acontent)
386 pd->content = sat_realloc(pd->content, l + 256);
387 pd->acontent = l + 256;
389 c = pd->content + pd->lcontent;
396 #define BUFF_SIZE 8192
400 * add single product to repo
405 repo_add_product(struct parsedata *pd, FILE *fp, int code11)
411 if (!fstat(fileno(fp), &st))
413 pd->currentproduct = st.st_ino;
414 pd->ctime = (unsigned int)st.st_ctime;
418 pd->currentproduct = pd->baseproduct + 1; /* make it != baseproduct if stat fails */
425 XML_Parser parser = XML_ParserCreate(NULL);
426 XML_SetUserData(parser, pd);
427 XML_SetElementHandler(parser, startElement, endElement);
428 XML_SetCharacterDataHandler(parser, characterData);
432 l = fread(buf, 1, sizeof(buf), fp);
433 if (XML_Parse(parser, buf, l, l == 0) == XML_STATUS_ERROR)
435 fprintf(stderr, "repo_products: %s at line %u:%u\n", XML_ErrorString(XML_GetErrorCode(parser)), (unsigned int)XML_GetCurrentLineNumber(parser), (unsigned int)XML_GetCurrentColumnNumber(parser));
441 XML_ParserFree(parser);
448 int lnum = 0; /* line number */
450 /* parse /etc/<xyz>-release file */
451 while (fgets(buf, sizeof(buf), fp))
453 /* remove trailing \n */
455 if (*(buf + l - 1) == '\n')
464 /* 1st line, <name> [(<arch>)] */
465 ptr = strchr(buf, '(');
474 /* track back until non-blank, non-digit */
476 && (*ptr1 == ' ' || isdigit(*ptr1) || *ptr1 == '.'))
479 name = str2id(pd->pool, join2("product", ":", buf), 1);
484 char *ptr1 = strchr(ptr, ')');
492 if (isupper(*ptr1)) *ptr1 = tolower(*ptr1);
495 arch = str2id(pd->pool, ptr, 1);
499 else if (strncmp(buf, "VERSION", 7) == 0)
501 ptr = strchr(buf+7, '=');
504 while (*++ptr == ' ');
505 version = makeevr(pd->pool, ptr);
511 Solvable *s = pd->solvable = pool_id2solvable(pd->pool, repo_add_solvable(pd->repo));
517 if (s->arch != ARCH_SRC && s->arch != ARCH_NOSRC)
518 s->provides = repo_addid_dep(pd->repo, s->provides, rel2id(pd->pool, s->name, s->evr, REL_EQ, 1), 0);
527 * parse dir looking for files ending in suffix
531 parse_dir(DIR *dir, const char *path, struct parsedata *pd, int code11)
533 struct dirent *entry;
534 char *suffix = code11 ? ".prod" : "-release";
535 int slen = code11 ? 5 : 8; /* strlen(".prod") : strlen("-release") */
538 /* check for <productsdir>/baseproduct on code11 and remember its target inode */
540 && stat(join2(path, "/", "baseproduct"), &st) == 0) /* follow symlink */
542 pd->baseproduct = st.st_ino;
547 while ((entry = readdir(dir)))
550 len = strlen(entry->d_name);
552 /* skip /etc/lsb-release, thats not a product per-se */
554 && strcmp(entry->d_name, "lsb-release") == 0)
560 && strcmp(entry->d_name + len - slen, suffix) == 0)
562 char *fullpath = join2(path, "/", entry->d_name);
563 FILE *fp = fopen(fullpath, "r");
569 repo_add_product(pd, fp, code11);
577 * read all installed products
579 * try proddir (reading all .xml files from this directory) first
580 * if not available, assume non-code11 layout and parse /etc/xyz-release
582 * parse each one as a product
585 /* Oh joy! Three parsers for the price of one! */
588 repo_add_products(Repo *repo, const char *proddir, const char *root, const char *attribute, int flags)
590 const char *fullpath = proddir;
594 struct stateswitch *sw;
597 if (!(flags & REPO_REUSE_REPODATA))
598 data = repo_add_repodata(repo, 0);
600 data = repo_last_repodata(repo);
602 memset(&pd, 0, sizeof(pd));
604 pd.pool = repo->pool;
607 pd.content = sat_malloc(256);
610 pd.attribute = attribute;
612 for (i = 0, sw = stateswitches; sw->from != NUMSTATES; i++, sw++)
614 if (!pd.swtab[sw->from])
615 pd.swtab[sw->from] = sw;
616 pd.sbtab[sw->to] = sw->from;
619 dir = opendir(fullpath);
622 parse_dir(dir, fullpath, &pd, 1); /* assume 'code11' products */
627 fullpath = root ? join2(root, "", "/var/lib/zypp/db/products") : "/var/lib/zypp/db/products";
628 dir = opendir(fullpath);
631 repo_add_zyppdb_products(repo, data, fullpath, dir); /* assume 'code10' zypp-style products */
636 fullpath = root ? join2(root, "", "/etc") : "/etc";
637 dir = opendir(fullpath);
640 parse_dir(dir, fullpath, &pd, 0); /* fall back to /etc/<xyz>-release parsing */
650 sat_free((void *)pd.tmplang);
651 sat_free(pd.content);
655 if (!(flags & REPO_NO_INTERNALIZE))
656 repodata_internalize(data);