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 );
310 repodata_add_poolstr_array(pd->data, pd->handle, PRODUCT_URL, pd->content);
312 if ( pd->tmpurltype[0] == 's' )
313 type = PRODUCT_URL_TYPE_SMOLT;
314 else if ( pd->tmpurltype[0] == 'u' )
315 type = PRODUCT_URL_TYPE_UPDATE;
316 else if ( pd->tmpurltype[0] == 'o' )
317 type = PRODUCT_URL_TYPE_OPTIONAL;
318 else if ( pd->tmpurltype[0] == 'r' )
319 type = PRODUCT_URL_TYPE_RELNOTES;
321 type = PRODUCT_URL_TYPE_EXTRA;
323 repodata_add_idarray(pd->data, pd->handle, PRODUCT_URL_TYPE, type);
327 if (currentproduct == baseproduct
329 && !strcmp(pd->attribute, "register.target"))
331 printf("%s\n", pd->content);
335 repodata_set_str(pd->data, pd->handle, PRODUCT_FLAVOR, pd->content );
336 if (currentproduct == baseproduct
338 && !strcmp(pd->attribute, "register.flavor"))
340 printf("%s\n", pd->content);
343 case STATE_REGRELEASE:
344 if (currentproduct == baseproduct
346 && !strcmp(pd->attribute, "register.release"))
348 printf("%s\n", pd->content);
355 pd->state = pd->sbtab[pd->state];
359 fprintf(stderr, "end: [%s] -> %d\n", name, pd->state);
366 characterData(void *userData, const XML_Char *s, int len)
368 struct parsedata *pd = userData;
371 if (!pd->docontent) {
373 char *dup = strndup( s, len );
374 fprintf(stderr, "Content: [%d]'%s'\n", pd->state, dup );
379 l = pd->lcontent + len + 1;
380 if (l > pd->acontent)
382 pd->content = realloc(pd->content, l + 256);
383 pd->acontent = l + 256;
385 c = pd->content + pd->lcontent;
392 #define BUFF_SIZE 8192
396 * add single product to repo
401 repo_add_product(struct parsedata *pd, Repodata *data, FILE *fp, int code11)
403 Pool *pool = pd->pool;
406 struct stateswitch *sw;
409 pd->s = NULL; /* enforce new solvable when coming here again */
411 if (!fstat(fileno(fp), &st))
412 currentproduct = st.st_ino;
415 currentproduct = baseproduct+1; /* make it != baseproduct if stat fails */
417 perror("Can't stat()");
420 for (i = 0, sw = stateswitches; sw->from != NUMSTATES; i++, sw++)
422 if (!pd->swtab[sw->from])
423 pd->swtab[sw->from] = sw;
424 pd->sbtab[sw->to] = sw->from;
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);
449 repodata_set_num(pd->data, pd->handle, SOLVABLE_INSTALLTIME, st.st_ctime);
450 /* this is where <productsdir>/baseproduct points to */
451 if (currentproduct == baseproduct)
452 repodata_set_str(pd->data, pd->handle, PRODUCT_TYPE, "base");
458 s->evr = makeevr(pool, join2(pd->tmpvers, "-", pd->tmprel));
459 free((char *)pd->tmpvers);
464 fprintf(stderr, "Seen <release> but no <version>\n");
466 free((char *)pd->tmprel);
469 else if (pd->tmpvers)
471 s->evr = makeevr(pool, pd->tmpvers); /* just version, no release */
472 free((char *)pd->tmpvers);
476 s->arch = ARCH_NOARCH;
477 if (s->arch != ARCH_SRC && s->arch != ARCH_NOSRC)
479 s->provides = repo_addid_dep(pd->repo, s->provides, rel2id(pool, s->name, s->evr, REL_EQ, 1), 0);
488 * parse dir looking for files ending in suffix
492 parse_dir(DIR *dir, const char *path, struct parsedata *pd, Repodata *repodata, int code11)
494 struct dirent *entry;
495 char *suffix = code11 ? ".prod" : "-release";
496 int slen = code11 ? 5 : 8; /* strlen(".prod") : strlen("-release") */
499 /* check for <productsdir>/baseproduct on code11 and remember its target inode */
501 && stat(join2(path, "/", "baseproduct"), &st) == 0) /* follow symlink */
503 baseproduct = st.st_ino;
508 while ((entry = readdir(dir)))
511 len = strlen(entry->d_name);
513 /* skip /etc/lsb-release, thats not a product per-se */
515 && strcmp(entry->d_name, "lsb-release") == 0)
521 && strcmp(entry->d_name+len-slen, suffix) == 0)
523 char *fullpath = join2(path, "/", entry->d_name);
524 FILE *fp = fopen(fullpath, "r");
530 repo_add_product(pd, repodata, fp, code11);
538 * read all installed products
540 * try proddir (reading all .xml files from this directory) first
541 * if not available, assume non-code11 layout and parse /etc/xyz-release
543 * parse each one as a product
547 repo_add_products(Repo *repo, Repodata *repodata, const char *proddir, const char *root, const char *attribute)
549 const char *fullpath = proddir;
551 DIR *dir = opendir(fullpath);
554 memset(&pd, 0, sizeof(pd));
556 pd.pool = repo->pool;
559 pd.content = malloc(256);
562 pd.attribute = attribute;
566 fullpath = root ? join2(root, "", "/etc") : "/etc";
567 dir = opendir(fullpath);
576 parse_dir(dir, fullpath, &pd, repodata, code11);