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
58 STATE_REGRELEASE, // 18
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, "summary", STATE_SUMMARY, 1 },
78 { STATE_PRODUCT, "description", STATE_DESCRIPTION, 1 },
79 { STATE_PRODUCT, "register", STATE_REGISTER, 0 },
80 { STATE_PRODUCT, "urls", STATE_URLS, 0 },
81 { STATE_PRODUCT, "runtimeconfig", STATE_RUNTIMECONFIG, 0 },
82 { STATE_PRODUCT, "linguas", STATE_LINGUAS, 0 },
83 { STATE_PRODUCT, "updaterepokey", STATE_UPDATEREPOKEY, 1 },
84 { STATE_PRODUCT, "cpename", STATE_CPENAME, 1 },
85 { STATE_URLS, "url", STATE_URL, 1 },
86 { STATE_LINGUAS, "lang", STATE_LANG, 0 },
87 { STATE_REGISTER, "flavor", STATE_FLAVOR, 1 },
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 repodata_extend(pd->data, s - pool->solvables);
225 pd->handle = repodata_get_handle(pd->data, (s - pool->solvables) - pd->repo->start);
229 /* <summary lang="xy">... */
231 pd->tmplang = find_attr("lang", atts, 1);
233 case STATE_DESCRIPTION:
234 pd->tmplang = find_attr("lang", atts, 1);
237 pd->tmpurltype = find_attr("name", atts, 1);
246 endElement(void *userData, const char *name)
248 struct parsedata *pd = userData;
249 Solvable *s = pd->solvable;
252 fprintf(stderr, "end: [%d]%s\n", pd->state, name);
254 if (pd->depth != pd->statedepth)
258 fprintf(stderr, "back from unknown %d %d %d\n", pd->state, pd->depth, pd->statedepth);
269 /* product done, finish solvable */
271 repodata_set_num(pd->data, pd->handle, SOLVABLE_INSTALLTIME, pd->ctime);
273 /* this is where <productsdir>/baseproduct points to */
274 if (pd->currentproduct == pd->baseproduct)
275 repodata_set_str(pd->data, pd->handle, PRODUCT_TYPE, "base");
280 s->evr = makeevr(pd->pool, join2(pd->tmpvers, "-", pd->tmprel));
283 fprintf(stderr, "Seen <release> but no <version>\n");
286 else if (pd->tmpvers)
287 s->evr = makeevr(pd->pool, pd->tmpvers); /* just version, no release */
288 pd->tmpvers = sat_free((void *)pd->tmpvers);
289 pd->tmprel = sat_free((void *)pd->tmprel);
291 s->arch = ARCH_NOARCH;
292 if (s->arch != ARCH_SRC && s->arch != ARCH_NOSRC)
293 s->provides = repo_addid_dep(pd->repo, s->provides, rel2id(pd->pool, s->name, s->evr, REL_EQ, 1), 0);
297 s->vendor = str2id(pd->pool, pd->content, 1);
300 s->name = str2id(pd->pool, join2("product", ":", pd->content), 1);
303 pd->tmpvers = strdup(pd->content);
306 pd->tmprel = strdup(pd->content);
309 s->arch = str2id(pd->pool, pd->content, 1);
311 case STATE_UPDATEREPOKEY:
312 repodata_set_str(pd->data, pd->handle, langtag(pd, PRODUCT_UPDATEREPOKEY, pd->tmplang), pd->content);
315 repodata_set_str(pd->data, pd->handle, langtag(pd, SOLVABLE_SUMMARY, pd->tmplang), pd->content);
316 pd->tmplang = sat_free((void *)pd->tmplang);
318 case STATE_DESCRIPTION:
319 repodata_set_str(pd->data, pd->handle, langtag(pd, SOLVABLE_DESCRIPTION, pd->tmplang), pd->content );
320 pd->tmplang = sat_free((void *)pd->tmplang);
325 repodata_add_poolstr_array(pd->data, pd->handle, PRODUCT_URL, pd->content);
326 repodata_add_idarray(pd->data, pd->handle, PRODUCT_URL_TYPE, str2id(pd->pool, pd->tmpurltype, 1));
328 pd->tmpurltype = sat_free((void *)pd->tmpurltype);
331 repodata_set_str(pd->data, pd->handle, PRODUCT_REGISTER_TARGET, pd->content);
332 if (pd->currentproduct == pd->baseproduct
334 && !strcmp(pd->attribute, "register.target"))
336 printf("%s\n", pd->content);
340 repodata_set_str(pd->data, pd->handle, PRODUCT_FLAVOR, pd->content);
341 if (pd->currentproduct == pd->baseproduct
343 && !strcmp(pd->attribute, "register.flavor"))
345 printf("%s\n", pd->content);
348 case STATE_REGRELEASE:
349 repodata_set_str(pd->data, pd->handle, PRODUCT_REGISTER_RELEASE, pd->content);
350 if (pd->currentproduct == pd->baseproduct
352 && !strcmp(pd->attribute, "register.release"))
354 printf("%s\n", pd->content);
359 repodata_set_str(pd->data, pd->handle, SOLVABLE_CPE_NAME, pd->content);
364 pd->state = pd->sbtab[pd->state];
368 fprintf(stderr, "end: [%s] -> %d\n", name, pd->state);
374 characterData(void *userData, const XML_Char *s, int len)
376 struct parsedata *pd = userData;
379 if (!pd->docontent) {
381 char *dup = strndup( s, len );
382 fprintf(stderr, "Content: [%d]'%s'\n", pd->state, dup );
387 l = pd->lcontent + len + 1;
388 if (l > pd->acontent)
390 pd->content = realloc(pd->content, l + 256);
391 pd->acontent = l + 256;
393 c = pd->content + pd->lcontent;
400 #define BUFF_SIZE 8192
404 * add single product to repo
409 repo_add_product(struct parsedata *pd, Repodata *data, FILE *fp, int code11)
415 if (!fstat(fileno(fp), &st))
417 pd->currentproduct = st.st_ino;
418 pd->ctime = (unsigned int)st.st_ctime;
422 pd->currentproduct = pd->baseproduct + 1; /* make it != baseproduct if stat fails */
429 XML_Parser parser = XML_ParserCreate(NULL);
430 XML_SetUserData(parser, pd);
431 XML_SetElementHandler(parser, startElement, endElement);
432 XML_SetCharacterDataHandler(parser, characterData);
436 l = fread(buf, 1, sizeof(buf), fp);
437 if (XML_Parse(parser, buf, l, l == 0) == XML_STATUS_ERROR)
439 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));
445 XML_ParserFree(parser);
452 int lnum = 0; /* line number */
454 /* parse /etc/<xyz>-release file */
455 while (fgets(buf, sizeof(buf), fp))
457 /* remove trailing \n */
459 if (*(buf + l - 1) == '\n')
468 /* 1st line, <name> [(<arch>)] */
469 ptr = strchr(buf, '(');
478 /* track back until non-blank, non-digit */
480 && (*ptr1 == ' ' || isdigit(*ptr1) || *ptr1 == '.'))
483 name = str2id(pd->pool, join2("product", ":", buf), 1);
488 char *ptr1 = strchr(ptr, ')');
496 if (isupper(*ptr1)) *ptr1 = tolower(*ptr1);
499 arch = str2id(pd->pool, ptr, 1);
503 else if (strncmp(buf, "VERSION", 7) == 0)
505 ptr = strchr(buf+7, '=');
508 while (*++ptr == ' ');
509 version = makeevr(pd->pool, ptr);
515 Solvable *s = pd->solvable = pool_id2solvable(pd->pool, repo_add_solvable(pd->repo));
521 if (s->arch != ARCH_SRC && s->arch != ARCH_NOSRC)
522 s->provides = repo_addid_dep(pd->repo, s->provides, rel2id(pd->pool, s->name, s->evr, REL_EQ, 1), 0);
531 * parse dir looking for files ending in suffix
535 parse_dir(DIR *dir, const char *path, struct parsedata *pd, Repodata *repodata, int code11)
537 struct dirent *entry;
538 char *suffix = code11 ? ".prod" : "-release";
539 int slen = code11 ? 5 : 8; /* strlen(".prod") : strlen("-release") */
542 /* check for <productsdir>/baseproduct on code11 and remember its target inode */
544 && stat(join2(path, "/", "baseproduct"), &st) == 0) /* follow symlink */
546 pd->baseproduct = st.st_ino;
551 while ((entry = readdir(dir)))
554 len = strlen(entry->d_name);
556 /* skip /etc/lsb-release, thats not a product per-se */
558 && strcmp(entry->d_name, "lsb-release") == 0)
564 && strcmp(entry->d_name + len - slen, suffix) == 0)
566 char *fullpath = join2(path, "/", entry->d_name);
567 FILE *fp = fopen(fullpath, "r");
573 repo_add_product(pd, repodata, fp, code11);
581 * read all installed products
583 * try proddir (reading all .xml files from this directory) first
584 * if not available, assume non-code11 layout and parse /etc/xyz-release
586 * parse each one as a product
590 repo_add_products(Repo *repo, Repodata *repodata, const char *proddir, const char *root, const char *attribute)
592 const char *fullpath = proddir;
596 struct stateswitch *sw;
598 memset(&pd, 0, sizeof(pd));
600 pd.pool = repo->pool;
603 pd.content = malloc(256);
606 pd.attribute = attribute;
608 for (i = 0, sw = stateswitches; sw->from != NUMSTATES; i++, sw++)
610 if (!pd.swtab[sw->from])
611 pd.swtab[sw->from] = sw;
612 pd.sbtab[sw->to] = sw->from;
615 dir = opendir(fullpath);
618 parse_dir(dir, fullpath, &pd, repodata, 1); /* assume 'code11' products */
623 fullpath = root ? join2(root, "", "/var/lib/zypp/db/products") : "/var/lib/zypp/db/products";
624 dir = opendir(fullpath);
627 repo_add_zyppdb_products(repo, repodata, fullpath, dir); /* assume 'code10' zypp-style products */
632 fullpath = root ? join2(root, "", "/etc") : "/etc";
633 dir = opendir(fullpath);
636 parse_dir(dir, fullpath, &pd, repodata, 0); /* fall back to /etc/<xyz>-release parsing */
646 sat_free((void *)pd.tmplang);