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;
48 STATE_DESCRIPTION, // 8
49 STATE_DISTRIBUTION, // 9
53 STATE_UPDATEREPOKEY, // 13
54 STATE_BUILDCONFIG, // 14
55 STATE_INSTALLCONFIG, // 15
56 STATE_RUNTIMECONFIG, // 16
69 /* !! must be sorted by first column !! */
70 static struct stateswitch stateswitches[] = {
71 { STATE_START, "product", STATE_PRODUCT, 0 },
72 { STATE_PRODUCT, "general", STATE_GENERAL, 0 },
73 { STATE_GENERAL, "vendor", STATE_VENDOR, 1 },
74 { STATE_GENERAL, "name", STATE_NAME, 1 },
75 { STATE_GENERAL, "version", STATE_VERSION, 1 },
76 { STATE_GENERAL, "release", STATE_RELEASE, 1 },
77 { STATE_GENERAL, "summary", STATE_SUMMARY, 1 },
78 { STATE_GENERAL, "description", STATE_DESCRIPTION, 1 },
79 { STATE_GENERAL, "distribution", STATE_DISTRIBUTION, 0 },
80 { STATE_GENERAL, "urls", STATE_URLS, 0 },
81 { STATE_GENERAL, "runtimeconfig", STATE_RUNTIMECONFIG, 0 },
82 { STATE_GENERAL, "installconfig", STATE_INSTALLCONFIG, 0 },
83 { STATE_GENERAL, "buildconfig", STATE_BUILDCONFIG, 0 },
84 { STATE_GENERAL, "linguas", STATE_LINGUAS, 0 },
85 { STATE_GENERAL, "update_repo_key", STATE_UPDATEREPOKEY, 0 },
86 { STATE_URLS, "url", STATE_URL, 0 },
87 /* { STATE_BUILDCONFIG,"linguas", STATE_LINGUAS, 0 }, */
88 { STATE_LINGUAS, "lang", STATE_LANG, 0 },
105 struct stateswitch *swtab[NUMSTATES];
106 enum state sbtab[NUMSTATES];
108 const char *attribute; /* only print this attribute, if currentproduct == baseproduct */
117 Id langcache[ID_NUM_INTERNAL];
123 * find value for xml attribute
124 * I: txt, name of attribute
125 * I: atts, list of key/value attributes
127 * O: pointer to value of matching key, or NULL
131 static inline const char *
132 find_attr(const char *txt, const char **atts, int dup)
134 for (; *atts; atts += 2)
136 if (!strcmp(*atts, txt))
137 return dup ? strdup(atts[1]) : atts[1];
144 * create localized tag
148 langtag(struct parsedata *pd, Id tag, const char *language)
150 if (language && !language[0])
152 if (!language || tag >= ID_NUM_INTERNAL)
153 return pool_id2langid(pd->repo->pool, tag, language, 1);
154 if (!pd->langcache[tag])
155 pd->langcache[tag] = pool_id2langid(pd->repo->pool, tag, language, 1);
156 return pd->langcache[tag];
161 * XML callback: startElement
165 startElement(void *userData, const char *name, const char **atts)
167 struct parsedata *pd = userData;
168 Pool *pool = pd->pool;
169 struct stateswitch *sw;
172 fprintf(stderr, "start: [%d]%s\n", pd->state, name);
174 if (pd->depth != pd->statedepth)
181 for (sw = pd->swtab[pd->state]; sw->from == pd->state; sw++) /* find name in statetable */
182 if (!strcmp(sw->ename, name))
185 if (sw->from != pd->state)
188 fprintf(stderr, "into unknown: [%d]%s (from: %d, state %d)\n", sw->to, name, sw->from, pd->state);
194 pd->docontent = sw->docontent;
195 pd->statedepth = pd->depth;
207 pd->s = pool_id2solvable(pool, repo_add_solvable(pd->repo));
208 repodata_extend(pd->data, pd->s - pool->solvables);
209 pd->handle = repodata_get_handle(pd->data, pd->s - pool->solvables - pd->repo->start);
213 /* <summary lang="xy">... */
215 pd->tmplang = find_attr("lang", atts, 1);
217 case STATE_DESCRIPTION:
218 pd->tmplang = find_attr("lang", atts, 1);
220 case STATE_DISTRIBUTION:
223 if ((str = find_attr("flavor", atts, 0)))
224 repo_set_str(pd->repo, pd->s - pool->solvables, PRODUCT_FLAVOR, str);
225 if ((str = find_attr("target", atts, 0)))
227 if (currentproduct == baseproduct
229 && !strcmp(pd->attribute, "distribution.target"))
232 repo_set_str(pd->repo, pd->s - pool->solvables, SOLVABLE_DISTRIBUTION, str);
238 case STATE_RUNTIMECONFIG:
247 endElement(void *userData, const char *name)
249 struct parsedata *pd = userData;
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 pd->s->vendor = str2id(pd->pool, pd->content, 1);
272 pd->s->name = str2id(pd->pool, join2("product", ":", pd->content), 1);
275 pd->tmpvers = strdup(pd->content);
278 pd->tmprel = strdup(pd->content);
281 repodata_set_str(pd->data, pd->handle, langtag(pd, SOLVABLE_SUMMARY, pd->tmplang), pd->content);
284 free( (char *)pd->tmplang );
288 case STATE_DESCRIPTION:
289 repodata_set_str(pd->data, pd->handle, langtag(pd, SOLVABLE_DESCRIPTION, pd->tmplang), pd->content );
292 free( (char *)pd->tmplang );
296 case STATE_DISTRIBUTION:
300 case STATE_RUNTIMECONFIG:
306 pd->state = pd->sbtab[pd->state];
310 fprintf(stderr, "end: [%s] -> %d\n", name, pd->state);
317 characterData(void *userData, const XML_Char *s, int len)
319 struct parsedata *pd = userData;
322 if (!pd->docontent) {
324 char *dup = strndup( s, len );
325 fprintf(stderr, "Content: [%d]'%s'\n", pd->state, dup );
330 l = pd->lcontent + len + 1;
331 if (l > pd->acontent)
333 pd->content = realloc(pd->content, l + 256);
334 pd->acontent = l + 256;
336 c = pd->content + pd->lcontent;
343 #define BUFF_SIZE 8192
347 * add single product to repo
352 repo_add_product(struct parsedata *pd, Repodata *data, FILE *fp, int code11)
354 Pool *pool = pd->pool;
357 struct stateswitch *sw;
360 if (!fstat(fileno(fp), &st))
361 currentproduct = st.st_ino;
364 currentproduct = baseproduct+1; /* make it != baseproduct if stat fails */
366 perror("Can't stat()");
369 for (i = 0, sw = stateswitches; sw->from != NUMSTATES; i++, sw++)
371 if (!pd->swtab[sw->from])
372 pd->swtab[sw->from] = sw;
373 pd->sbtab[sw->to] = sw->from;
375 XML_Parser parser = XML_ParserCreate(NULL);
376 XML_SetUserData(parser, pd);
377 XML_SetElementHandler(parser, startElement, endElement);
378 XML_SetCharacterDataHandler(parser, characterData);
382 l = fread(buf, 1, sizeof(buf), fp);
383 if (XML_Parse(parser, buf, l, l == 0) == XML_STATUS_ERROR)
385 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));
391 XML_ParserFree(parser);
398 repodata_set_num(pd->data, pd->handle, SOLVABLE_INSTALLTIME, st.st_ctime);
399 /* this is where <productsdir>/baseproduct points to */
400 if (currentproduct == baseproduct)
401 repodata_set_str(pd->data, pd->handle, PRODUCT_TYPE, "base");
407 s->evr = makeevr(pool, join2(pd->tmpvers, "-", pd->tmprel));
408 free((char *)pd->tmpvers);
413 fprintf(stderr, "Seen <release> but no <version>\n");
415 free((char *)pd->tmprel);
418 else if (pd->tmpvers)
420 s->evr = makeevr(pool, pd->tmpvers); /* just version, no release */
421 free((char *)pd->tmpvers);
425 s->arch = ARCH_NOARCH;
426 if (s->arch != ARCH_SRC && s->arch != ARCH_NOSRC)
428 s->provides = repo_addid_dep(pd->repo, s->provides, rel2id(pool, s->name, s->evr, REL_EQ, 1), 0);
436 * parse dir looking for files ending in suffix
440 parse_dir(DIR *dir, const char *path, struct parsedata *pd, Repodata *repodata, int code11)
442 struct dirent *entry;
443 char *suffix = code11 ? ".prod" : "-release";
444 int slen = code11 ? 5 : 8; /* strlen(".prod") : strlen("-release") */
447 /* check for <productsdir>/baseproduct on code11 and remember its target inode */
449 && stat(join2(path, "/", "baseproduct"), &st) == 0) /* follow symlink */
451 baseproduct = st.st_ino;
456 while ((entry = readdir(dir)))
459 len = strlen(entry->d_name);
461 /* skip /etc/lsb-release, thats not a product per-se */
463 && strcmp(entry->d_name, "lsb-release") == 0)
469 && strcmp(entry->d_name+len-slen, suffix) == 0)
471 char *fullpath = join2(path, "/", entry->d_name);
472 FILE *fp = fopen(fullpath, "r");
478 repo_add_product(pd, repodata, fp, code11);
486 * read all installed products
488 * try proddir (reading all .xml files from this directory) first
489 * if not available, assume non-code11 layout and parse /etc/xyz-release
491 * parse each one as a product
495 repo_add_products(Repo *repo, Repodata *repodata, const char *proddir, const char *root, const char *attribute)
497 const char *fullpath = proddir;
499 DIR *dir = opendir(fullpath);
502 memset(&pd, 0, sizeof(pd));
504 pd.pool = repo->pool;
507 pd.content = malloc(256);
510 pd.attribute = attribute;
514 fullpath = root ? join2(root, "", "/etc") : "/etc";
515 dir = opendir(fullpath);
524 parse_dir(dir, fullpath, &pd, repodata, code11);