4 * Parses all files below 'proddir'
5 * See http://en.opensuse.org/Product_Management/Code11
8 * Copyright (c) 2007, 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"
34 static ino_t baseproduct = 0;
35 static ino_t currentproduct = 0;
37 static int productscheme = -1;
50 STATE_DESCRIPTION, // 8
51 STATE_UPDATEREPOKEY, // 9
54 #if 0 /* not needed */
55 STATE_BUILDCONFIG, // 12
56 STATE_INSTALLCONFIG, // 13
58 STATE_RUNTIMECONFIG, // 14
64 STATE_REGRELEASE, // 20
75 /* !! must be sorted by first column !! */
76 static struct stateswitch stateswitches[] = {
77 { STATE_START, "product", STATE_PRODUCT, 0 },
78 { STATE_PRODUCT, "vendor", STATE_VENDOR, 1 },
79 { STATE_PRODUCT, "name", STATE_NAME, 1 },
80 { STATE_PRODUCT, "version", STATE_VERSION, 1 },
81 { STATE_PRODUCT, "release", STATE_RELEASE, 1 },
82 { STATE_PRODUCT, "arch", STATE_ARCH, 1 },
83 { STATE_PRODUCT, "summary", STATE_SUMMARY, 1 },
84 { STATE_PRODUCT, "description", STATE_DESCRIPTION, 1 },
85 { STATE_PRODUCT, "register", STATE_REGISTER, 0 },
86 { STATE_PRODUCT, "urls", STATE_URLS, 0 },
87 { STATE_PRODUCT, "runtimeconfig", STATE_RUNTIMECONFIG, 0 },
88 #if 0 /* not needed */
89 { STATE_PRODUCT, "installconfig", STATE_INSTALLCONFIG, 0 },
90 { STATE_PRODUCT, "buildconfig", STATE_BUILDCONFIG, 0 },
92 { STATE_PRODUCT, "linguas", STATE_LINGUAS, 0 },
93 { STATE_PRODUCT, "updaterepokey", STATE_UPDATEREPOKEY, 1 },
94 { STATE_URLS, "url", STATE_URL, 1 },
95 /* { STATE_BUILDCONFIG,"linguas", STATE_LINGUAS, 0 }, */
96 { STATE_LINGUAS, "lang", STATE_LANG, 0 },
97 { STATE_REGISTER, "flavor", STATE_FLAVOR, 1 },
98 { STATE_REGISTER, "target", STATE_TARGET, 1 },
99 { STATE_REGISTER, "release", STATE_REGRELEASE, 1 },
116 struct stateswitch *swtab[NUMSTATES];
117 enum state sbtab[NUMSTATES];
118 const char *attribute; /* only print this attribute, if currentproduct == baseproduct */
123 const char *tmpurltype;
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 ? 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 struct stateswitch *sw;
183 fprintf(stderr, "start: [%d]%s\n", pd->state, name);
185 if (pd->depth != pd->statedepth)
192 if ( !(sw = pd->swtab[pd->state]) ) /* no statetable -> no substates */
195 fprintf(stderr, "into unknown: [?]%s (from: ?, state %d)\n", name, pd->state);
199 for (; sw->from == pd->state; sw++) /* find name in statetable */
200 if (!strcmp(sw->ename, name))
203 if (sw->from != pd->state)
206 fprintf(stderr, "into unknown: [%d]%s (from: %d, state %d)\n", sw->to, name, sw->from, 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 productscheme = (scheme && *scheme) ? atoi(scheme) : -1;
227 pd->s = pool_id2solvable(pool, repo_add_solvable(pd->repo));
228 repodata_extend(pd->data, pd->s - pool->solvables);
229 pd->handle = repodata_get_handle(pd->data, pd->s - pool->solvables - pd->repo->start);
233 /* <summary lang="xy">... */
235 pd->tmplang = find_attr("lang", atts, 1);
237 case STATE_DESCRIPTION:
238 pd->tmplang = find_attr("lang", atts, 1);
241 pd->tmpurltype = find_attr("name", atts, 1);
251 endElement(void *userData, const char *name)
253 struct parsedata *pd = userData;
256 fprintf(stderr, "end: [%d]%s\n", pd->state, name);
258 if (pd->depth != pd->statedepth)
262 fprintf(stderr, "back from unknown %d %d %d\n", pd->state, pd->depth, pd->statedepth);
273 pd->s->vendor = str2id(pd->pool, pd->content, 1);
276 pd->s->name = str2id(pd->pool, join2("product", ":", pd->content), 1);
279 pd->tmpvers = strdup(pd->content);
282 pd->tmprel = strdup(pd->content);
285 pd->s->arch = str2id(pd->pool, pd->content, 1);
287 case STATE_UPDATEREPOKEY:
288 repodata_set_str(pd->data, pd->handle, langtag(pd, PRODUCT_UPDATEREPOKEY, pd->tmplang), pd->content);
291 repodata_set_str(pd->data, pd->handle, langtag(pd, SOLVABLE_SUMMARY, pd->tmplang), pd->content);
294 free( (char *)pd->tmplang );
298 case STATE_DESCRIPTION:
299 repodata_set_str(pd->data, pd->handle, langtag(pd, SOLVABLE_DESCRIPTION, pd->tmplang), pd->content );
302 free( (char *)pd->tmplang );
309 repodata_add_poolstr_array(pd->data, pd->handle, PRODUCT_URL, pd->content);
311 repodata_add_idarray(pd->data, pd->handle, PRODUCT_URL_TYPE, str2id(pd->pool, pd->tmpurltype, 1));
315 if (currentproduct == baseproduct
317 && !strcmp(pd->attribute, "register.target"))
319 printf("%s\n", pd->content);
323 repodata_set_str(pd->data, pd->handle, PRODUCT_FLAVOR, pd->content );
324 if (currentproduct == baseproduct
326 && !strcmp(pd->attribute, "register.flavor"))
328 printf("%s\n", pd->content);
331 case STATE_REGRELEASE:
332 if (currentproduct == baseproduct
334 && !strcmp(pd->attribute, "register.release"))
336 printf("%s\n", pd->content);
343 pd->state = pd->sbtab[pd->state];
347 fprintf(stderr, "end: [%s] -> %d\n", name, pd->state);
354 characterData(void *userData, const XML_Char *s, int len)
356 struct parsedata *pd = userData;
359 if (!pd->docontent) {
361 char *dup = strndup( s, len );
362 fprintf(stderr, "Content: [%d]'%s'\n", pd->state, dup );
367 l = pd->lcontent + len + 1;
368 if (l > pd->acontent)
370 pd->content = realloc(pd->content, l + 256);
371 pd->acontent = l + 256;
373 c = pd->content + pd->lcontent;
380 #define BUFF_SIZE 8192
384 * add single product to repo
389 repo_add_product(struct parsedata *pd, Repodata *data, FILE *fp, int code11)
391 Pool *pool = pd->pool;
394 struct stateswitch *sw;
397 pd->s = NULL; /* enforce new solvable when coming here again */
399 if (!fstat(fileno(fp), &st))
400 currentproduct = st.st_ino;
403 currentproduct = baseproduct+1; /* make it != baseproduct if stat fails */
405 perror("Can't stat()");
408 for (i = 0, sw = stateswitches; sw->from != NUMSTATES; i++, sw++)
410 if (!pd->swtab[sw->from])
411 pd->swtab[sw->from] = sw;
412 pd->sbtab[sw->to] = sw->from;
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 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));
430 XML_ParserFree(parser);
437 repodata_set_num(pd->data, pd->handle, SOLVABLE_INSTALLTIME, st.st_ctime);
438 /* this is where <productsdir>/baseproduct points to */
439 if (currentproduct == baseproduct)
440 repodata_set_str(pd->data, pd->handle, PRODUCT_TYPE, "base");
446 s->evr = makeevr(pool, join2(pd->tmpvers, "-", pd->tmprel));
447 free((char *)pd->tmpvers);
452 fprintf(stderr, "Seen <release> but no <version>\n");
454 free((char *)pd->tmprel);
457 else if (pd->tmpvers)
459 s->evr = makeevr(pool, pd->tmpvers); /* just version, no release */
460 free((char *)pd->tmpvers);
464 s->arch = ARCH_NOARCH;
465 if (s->arch != ARCH_SRC && s->arch != ARCH_NOSRC)
467 s->provides = repo_addid_dep(pd->repo, s->provides, rel2id(pool, s->name, s->evr, REL_EQ, 1), 0);
476 * parse dir looking for files ending in suffix
480 parse_dir(DIR *dir, const char *path, struct parsedata *pd, Repodata *repodata, int code11)
482 struct dirent *entry;
483 char *suffix = code11 ? ".prod" : "-release";
484 int slen = code11 ? 5 : 8; /* strlen(".prod") : strlen("-release") */
487 /* check for <productsdir>/baseproduct on code11 and remember its target inode */
489 && stat(join2(path, "/", "baseproduct"), &st) == 0) /* follow symlink */
491 baseproduct = st.st_ino;
496 while ((entry = readdir(dir)))
499 len = strlen(entry->d_name);
501 /* skip /etc/lsb-release, thats not a product per-se */
503 && strcmp(entry->d_name, "lsb-release") == 0)
509 && strcmp(entry->d_name+len-slen, suffix) == 0)
511 char *fullpath = join2(path, "/", entry->d_name);
512 FILE *fp = fopen(fullpath, "r");
518 repo_add_product(pd, repodata, fp, code11);
526 * read all installed products
528 * try proddir (reading all .xml files from this directory) first
529 * if not available, assume non-code11 layout and parse /etc/xyz-release
531 * parse each one as a product
535 repo_add_products(Repo *repo, Repodata *repodata, const char *proddir, const char *root, const char *attribute)
537 const char *fullpath = proddir;
539 DIR *dir = opendir(fullpath);
542 memset(&pd, 0, sizeof(pd));
544 pd.pool = repo->pool;
547 pd.content = malloc(256);
550 pd.attribute = attribute;
554 fullpath = root ? join2(root, "", "/etc") : "/etc";
555 dir = opendir(fullpath);
564 parse_dir(dir, fullpath, &pd, repodata, code11);