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>
30 #include "tools_util.h"
31 #include "repo_content.h"
45 STATE_DESCRIPTION, // 8
46 STATE_UPDATEREPOKEY, // 9 should go away
50 STATE_RUNTIMECONFIG, // 12
56 STATE_REGRELEASE, // 18
67 /* !! must be sorted by first column !! */
68 static struct stateswitch stateswitches[] = {
69 { STATE_START, "product", STATE_PRODUCT, 0 },
70 { STATE_PRODUCT, "vendor", STATE_VENDOR, 1 },
71 { STATE_PRODUCT, "name", STATE_NAME, 1 },
72 { STATE_PRODUCT, "version", STATE_VERSION, 1 },
73 { STATE_PRODUCT, "release", STATE_RELEASE, 1 },
74 { STATE_PRODUCT, "arch", STATE_ARCH, 1 },
75 { STATE_PRODUCT, "summary", STATE_SUMMARY, 1 },
76 { STATE_PRODUCT, "description", STATE_DESCRIPTION, 1 },
77 { STATE_PRODUCT, "register", STATE_REGISTER, 0 },
78 { STATE_PRODUCT, "urls", STATE_URLS, 0 },
79 { STATE_PRODUCT, "runtimeconfig", STATE_RUNTIMECONFIG, 0 },
80 { STATE_PRODUCT, "linguas", STATE_LINGUAS, 0 },
81 { STATE_PRODUCT, "updaterepokey", STATE_UPDATEREPOKEY, 1 },
82 { STATE_PRODUCT, "cpename", STATE_CPENAME, 1 },
83 { STATE_URLS, "url", STATE_URL, 1 },
84 { STATE_LINGUAS, "lang", STATE_LANG, 0 },
85 { STATE_REGISTER, "flavor", STATE_FLAVOR, 1 },
86 { STATE_REGISTER, "target", STATE_TARGET, 1 },
87 { STATE_REGISTER, "release", STATE_REGRELEASE, 1 },
104 struct stateswitch *swtab[NUMSTATES];
105 enum state sbtab[NUMSTATES];
106 const char *attribute; /* only print this attribute, if currentproduct == baseproduct */
112 const char *tmpurltype;
120 ino_t currentproduct;
123 Id langcache[ID_NUM_INTERNAL];
129 * find value for xml attribute
130 * I: txt, name of attribute
131 * I: atts, list of key/value attributes
133 * O: pointer to value of matching key, or NULL
137 static inline const char *
138 find_attr(const char *txt, const char **atts, int dup)
140 for (; *atts; atts += 2)
142 if (!strcmp(*atts, txt))
143 return dup ? strdup(atts[1]) : atts[1];
150 * create localized tag
154 langtag(struct parsedata *pd, Id tag, const char *language)
156 if (language && !language[0])
158 if (!language || tag >= ID_NUM_INTERNAL)
159 return pool_id2langid(pd->repo->pool, tag, language, 1);
160 if (!pd->langcache[tag])
161 pd->langcache[tag] = pool_id2langid(pd->repo->pool, tag, language, 1);
162 return pd->langcache[tag];
167 * XML callback: startElement
171 startElement(void *userData, const char *name, const char **atts)
173 struct parsedata *pd = userData;
174 Pool *pool = pd->pool;
175 Solvable *s = pd->solvable;
176 struct stateswitch *sw;
179 fprintf(stderr, "start: [%d]%s\n", pd->state, name);
181 if (pd->depth != pd->statedepth)
188 if (!pd->swtab[pd->state]) /* no statetable -> no substates */
191 fprintf(stderr, "into unknown: %s (from: %d)\n", name, pd->state);
195 for (sw = pd->swtab[pd->state]; sw->from == pd->state; sw++) /* find name in statetable */
196 if (!strcmp(sw->ename, name))
199 if (sw->from != pd->state)
202 fprintf(stderr, "into unknown: %s (from: %d)\n", name, pd->state);
207 pd->docontent = sw->docontent;
208 pd->statedepth = pd->depth;
215 /* parse 'schemeversion' and store in global variable */
217 const char * scheme = find_attr("schemeversion", atts, 0);
218 pd->productscheme = (scheme && *scheme) ? atoi(scheme) : -1;
222 s = pd->solvable = pool_id2solvable(pool, repo_add_solvable(pd->repo));
223 repodata_extend(pd->data, s - pool->solvables);
224 pd->handle = repodata_get_handle(pd->data, (s - pool->solvables) - pd->repo->start);
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");
279 s->evr = makeevr(pd->pool, join2(pd->tmpvers, "-", pd->tmprel));
282 fprintf(stderr, "Seen <release> but no <version>\n");
285 else if (pd->tmpvers)
286 s->evr = makeevr(pd->pool, pd->tmpvers); /* just version, no release */
287 pd->tmpvers = sat_free((void *)pd->tmpvers);
288 pd->tmprel = sat_free((void *)pd->tmprel);
290 s->arch = ARCH_NOARCH;
291 if (s->arch != ARCH_SRC && s->arch != ARCH_NOSRC)
292 s->provides = repo_addid_dep(pd->repo, s->provides, rel2id(pd->pool, s->name, s->evr, REL_EQ, 1), 0);
296 s->vendor = str2id(pd->pool, pd->content, 1);
299 s->name = str2id(pd->pool, join2("product", ":", pd->content), 1);
302 pd->tmpvers = strdup(pd->content);
305 pd->tmprel = strdup(pd->content);
308 s->arch = str2id(pd->pool, pd->content, 1);
310 case STATE_UPDATEREPOKEY:
311 repodata_set_str(pd->data, pd->handle, langtag(pd, PRODUCT_UPDATEREPOKEY, pd->tmplang), pd->content);
314 repodata_set_str(pd->data, pd->handle, langtag(pd, SOLVABLE_SUMMARY, pd->tmplang), pd->content);
315 pd->tmplang = sat_free((void *)pd->tmplang);
317 case STATE_DESCRIPTION:
318 repodata_set_str(pd->data, pd->handle, langtag(pd, SOLVABLE_DESCRIPTION, pd->tmplang), pd->content );
319 pd->tmplang = sat_free((void *)pd->tmplang);
324 repodata_add_poolstr_array(pd->data, pd->handle, PRODUCT_URL, pd->content);
325 repodata_add_idarray(pd->data, pd->handle, PRODUCT_URL_TYPE, str2id(pd->pool, pd->tmpurltype, 1));
327 pd->tmpurltype = sat_free((void *)pd->tmpurltype);
330 repodata_set_str(pd->data, pd->handle, PRODUCT_REGISTER_TARGET, pd->content);
331 if (pd->currentproduct == pd->baseproduct
333 && !strcmp(pd->attribute, "register.target"))
335 printf("%s\n", pd->content);
339 repodata_set_str(pd->data, pd->handle, PRODUCT_FLAVOR, pd->content);
340 if (pd->currentproduct == pd->baseproduct
342 && !strcmp(pd->attribute, "register.flavor"))
344 printf("%s\n", pd->content);
347 case STATE_REGRELEASE:
348 repodata_set_str(pd->data, pd->handle, PRODUCT_REGISTER_RELEASE, pd->content);
349 if (pd->currentproduct == pd->baseproduct
351 && !strcmp(pd->attribute, "register.release"))
353 printf("%s\n", pd->content);
358 repodata_set_str(pd->data, pd->handle, PRODUCT_CPE_NAME, pd->content);
363 pd->state = pd->sbtab[pd->state];
367 fprintf(stderr, "end: [%s] -> %d\n", name, pd->state);
373 characterData(void *userData, const XML_Char *s, int len)
375 struct parsedata *pd = userData;
378 if (!pd->docontent) {
380 char *dup = strndup( s, len );
381 fprintf(stderr, "Content: [%d]'%s'\n", pd->state, dup );
386 l = pd->lcontent + len + 1;
387 if (l > pd->acontent)
389 pd->content = realloc(pd->content, l + 256);
390 pd->acontent = l + 256;
392 c = pd->content + pd->lcontent;
399 #define BUFF_SIZE 8192
403 * add single product to repo
408 repo_add_product(struct parsedata *pd, Repodata *data, FILE *fp, int code11)
414 if (!fstat(fileno(fp), &st))
416 pd->currentproduct = st.st_ino;
417 pd->ctime = (unsigned int)st.st_ctime;
421 pd->currentproduct = pd->baseproduct + 1; /* make it != baseproduct if stat fails */
426 XML_Parser parser = XML_ParserCreate(NULL);
427 XML_SetUserData(parser, pd);
428 XML_SetElementHandler(parser, startElement, endElement);
429 XML_SetCharacterDataHandler(parser, characterData);
433 l = fread(buf, 1, sizeof(buf), fp);
434 if (XML_Parse(parser, buf, l, l == 0) == XML_STATUS_ERROR)
436 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));
442 XML_ParserFree(parser);
447 * parse dir looking for files ending in suffix
451 parse_dir(DIR *dir, const char *path, struct parsedata *pd, Repodata *repodata, int code11)
453 struct dirent *entry;
454 char *suffix = code11 ? ".prod" : "-release";
455 int slen = code11 ? 5 : 8; /* strlen(".prod") : strlen("-release") */
458 /* check for <productsdir>/baseproduct on code11 and remember its target inode */
460 && stat(join2(path, "/", "baseproduct"), &st) == 0) /* follow symlink */
462 pd->baseproduct = st.st_ino;
467 while ((entry = readdir(dir)))
470 len = strlen(entry->d_name);
472 /* skip /etc/lsb-release, thats not a product per-se */
474 && strcmp(entry->d_name, "lsb-release") == 0)
480 && strcmp(entry->d_name + len - slen, suffix) == 0)
482 char *fullpath = join2(path, "/", entry->d_name);
483 FILE *fp = fopen(fullpath, "r");
489 repo_add_product(pd, repodata, fp, code11);
497 * read all installed products
499 * try proddir (reading all .xml files from this directory) first
500 * if not available, assume non-code11 layout and parse /etc/xyz-release
502 * parse each one as a product
506 repo_add_products(Repo *repo, Repodata *repodata, const char *proddir, const char *root, const char *attribute)
508 const char *fullpath = proddir;
513 struct stateswitch *sw;
515 memset(&pd, 0, sizeof(pd));
517 pd.pool = repo->pool;
520 pd.content = malloc(256);
523 pd.attribute = attribute;
525 for (i = 0, sw = stateswitches; sw->from != NUMSTATES; i++, sw++)
527 if (!pd.swtab[sw->from])
528 pd.swtab[sw->from] = sw;
529 pd.sbtab[sw->to] = sw->from;
533 dir = opendir(fullpath);
536 fullpath = root ? join2(root, "", "/etc") : "/etc";
537 dir = opendir(fullpath);
546 parse_dir(dir, fullpath, &pd, repodata, code11);
549 sat_free((void *)pd.tmplang);