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;
66 /* !! must be sorted by first column !! */
67 static struct stateswitch stateswitches[] = {
68 { STATE_START, "product", STATE_PRODUCT, 0 },
69 { STATE_PRODUCT, "general", STATE_GENERAL, 0 },
70 { STATE_GENERAL, "vendor", STATE_VENDOR, 1 },
71 { STATE_GENERAL, "name", STATE_NAME, 1 },
72 { STATE_GENERAL, "version", STATE_VERSION, 1 },
73 { STATE_GENERAL, "release", STATE_RELEASE, 1 },
74 { STATE_GENERAL, "summary", STATE_SUMMARY, 1 },
75 { STATE_GENERAL, "description", STATE_DESCRIPTION, 1 },
76 { STATE_GENERAL, "distribution", STATE_DISTRIBUTION, 0 },
77 { STATE_GENERAL, "urls", STATE_URLS, 0 },
78 { STATE_GENERAL, "runtimeconfig", STATE_RUNTIMECONFIG, 0 },
79 { STATE_GENERAL, "linguas", STATE_LINGUAS, 0 },
80 { STATE_URLS, "url", STATE_URL, 0 },
81 { STATE_LINGUAS, "lang", STATE_LANG, 0 },
98 struct stateswitch *swtab[NUMSTATES];
99 enum state sbtab[NUMSTATES];
101 const char *attribute; /* only print this attribute, if currentproduct == baseproduct */
110 Id langcache[ID_NUM_INTERNAL];
116 * find value for xml attribute
117 * I: txt, name of attribute
118 * I: atts, list of key/value attributes
120 * O: pointer to value of matching key, or NULL
124 static inline const char *
125 find_attr(const char *txt, const char **atts, int dup)
127 for (; *atts; atts += 2)
129 if (!strcmp(*atts, txt))
130 return dup ? strdup(atts[1]) : atts[1];
137 * create localized tag
141 langtag(struct parsedata *pd, Id tag, const char *language)
143 if (language && !language[0])
145 if (!language || tag >= ID_NUM_INTERNAL)
146 return pool_id2langid(pd->repo->pool, tag, language, 1);
147 if (!pd->langcache[tag])
148 pd->langcache[tag] = pool_id2langid(pd->repo->pool, tag, language, 1);
149 return pd->langcache[tag];
154 * XML callback: startElement
158 startElement(void *userData, const char *name, const char **atts)
160 struct parsedata *pd = userData;
161 Pool *pool = pd->pool;
162 struct stateswitch *sw;
165 fprintf(stderr, "start: [%d]%s\n", pd->state, name);
167 if (pd->depth != pd->statedepth)
174 for (sw = pd->swtab[pd->state]; sw->from == pd->state; sw++) /* find name in statetable */
175 if (!strcmp(sw->ename, name))
178 if (sw->from != pd->state)
181 fprintf(stderr, "into unknown: [%d]%s (from: %d)\n", sw->to, name, sw->from);
187 pd->docontent = sw->docontent;
188 pd->statedepth = pd->depth;
200 pd->s = pool_id2solvable(pool, repo_add_solvable(pd->repo));
201 repodata_extend(pd->data, pd->s - pool->solvables);
202 pd->handle = repodata_get_handle(pd->data, pd->s - pool->solvables - pd->repo->start);
206 /* <summary lang="xy">... */
208 pd->tmplang = find_attr("lang", atts, 1);
210 case STATE_DESCRIPTION:
211 pd->tmplang = find_attr("lang", atts, 1);
213 case STATE_DISTRIBUTION:
216 if ((str = find_attr("flavor", atts, 0)))
217 repo_set_str(pd->repo, pd->s - pool->solvables, PRODUCT_FLAVOR, str);
218 if ((str = find_attr("target", atts, 0)))
220 if (currentproduct == baseproduct
222 && !strcmp(pd->attribute, "distribution.target"))
225 repo_set_str(pd->repo, pd->s - pool->solvables, SOLVABLE_DISTRIBUTION, str);
231 case STATE_RUNTIMECONFIG:
240 endElement(void *userData, const char *name)
242 struct parsedata *pd = userData;
245 fprintf(stderr, "end: %s\n", name);
247 if (pd->depth != pd->statedepth)
251 fprintf(stderr, "back from unknown %d %d %d\n", pd->state, pd->depth, pd->statedepth);
262 pd->s->vendor = str2id(pd->pool, pd->content, 1);
265 pd->s->name = str2id(pd->pool, join2("product", ":", pd->content), 1);
268 pd->tmpvers = strdup(pd->content);
271 pd->tmprel = strdup(pd->content);
274 repodata_set_str(pd->data, pd->handle, langtag(pd, SOLVABLE_SUMMARY, pd->tmplang), pd->content);
277 free( (char *)pd->tmplang );
281 case STATE_DESCRIPTION:
282 repodata_set_str(pd->data, pd->handle, langtag(pd, SOLVABLE_DESCRIPTION, pd->tmplang), pd->content );
285 free( (char *)pd->tmplang );
289 case STATE_DISTRIBUTION:
293 case STATE_RUNTIMECONFIG:
299 pd->state = pd->sbtab[pd->state];
307 characterData(void *userData, const XML_Char *s, int len)
309 struct parsedata *pd = userData;
312 if (!pd->docontent) {
314 char *dup = strndup( s, len );
315 fprintf(stderr, "Content: [%d]'%s'\n", pd->state, dup );
320 l = pd->lcontent + len + 1;
321 if (l > pd->acontent)
323 pd->content = realloc(pd->content, l + 256);
324 pd->acontent = l + 256;
326 c = pd->content + pd->lcontent;
333 #define BUFF_SIZE 8192
337 * add single product to repo
342 repo_add_product(struct parsedata *pd, Repodata *data, FILE *fp, int code11)
344 Pool *pool = pd->pool;
347 struct stateswitch *sw;
350 if (!fstat(fileno(fp), &st))
351 currentproduct = st.st_ino;
354 currentproduct = baseproduct+1; /* make it != baseproduct if stat fails */
356 perror("Can't stat()");
359 for (i = 0, sw = stateswitches; sw->from != NUMSTATES; i++, sw++)
361 if (!pd->swtab[sw->from])
362 pd->swtab[sw->from] = sw;
363 pd->sbtab[sw->to] = sw->from;
365 XML_Parser parser = XML_ParserCreate(NULL);
366 XML_SetUserData(parser, pd);
367 XML_SetElementHandler(parser, startElement, endElement);
368 XML_SetCharacterDataHandler(parser, characterData);
372 l = fread(buf, 1, sizeof(buf), fp);
373 if (XML_Parse(parser, buf, l, l == 0) == XML_STATUS_ERROR)
375 fprintf(stderr, "repo_diskusagexml: %s at line %u:%u\n", XML_ErrorString(XML_GetErrorCode(parser)), (unsigned int)XML_GetCurrentLineNumber(parser), (unsigned int)XML_GetCurrentColumnNumber(parser));
381 XML_ParserFree(parser);
387 repodata_set_num(pd->data, pd->handle, SOLVABLE_INSTALLTIME, st.st_ctime);
388 /* this is where <productsdir>/baseproduct points to */
389 if (currentproduct == baseproduct)
390 repodata_set_str(pd->data, pd->handle, PRODUCT_TYPE, "base");
396 s->evr = makeevr(pool, join2(pd->tmpvers, "-", pd->tmprel));
397 free((char *)pd->tmpvers);
402 fprintf(stderr, "Seen <release> but no <version>\n");
404 free((char *)pd->tmprel);
407 else if (pd->tmpvers)
409 s->evr = makeevr(pool, pd->tmpvers); /* just version, no release */
410 free((char *)pd->tmpvers);
414 s->arch = ARCH_NOARCH;
415 if (s->arch != ARCH_SRC && s->arch != ARCH_NOSRC)
417 s->provides = repo_addid_dep(pd->repo, s->provides, rel2id(pool, s->name, s->evr, REL_EQ, 1), 0);
425 * parse dir looking for files ending in suffix
429 parse_dir(DIR *dir, const char *path, struct parsedata *pd, Repodata *repodata, int code11)
431 struct dirent *entry;
432 char *suffix = code11 ? ".xml" : "-release";
433 int slen = code11 ? 4 : 8; /* strlen(".xml") : strlen("-release") */
436 /* check for <productsdir>/baseproduct on code11 and remember its target inode */
438 && stat(join2(path, "/", "baseproduct"), &st) == 0) /* follow symlink */
440 baseproduct = st.st_ino;
445 while ((entry = readdir(dir)))
448 len = strlen(entry->d_name);
450 /* skip /etc/lsb-release, thats not a product per-se */
452 && strcmp(entry->d_name, "lsb-release") == 0)
458 && strcmp(entry->d_name+len-slen, suffix) == 0)
460 char *fullpath = join2(path, "/", entry->d_name);
461 FILE *fp = fopen(fullpath, "r");
467 repo_add_product(pd, repodata, fp, code11);
475 * read all installed products
477 * try proddir (reading all .xml files from this directory) first
478 * if not available, assume non-code11 layout and parse /etc/xyz-release
480 * parse each one as a product
484 repo_add_products(Repo *repo, Repodata *repodata, const char *proddir, const char *root, const char *attribute)
486 const char *fullpath = proddir;
488 DIR *dir = opendir(fullpath);
491 memset(&pd, 0, sizeof(pd));
493 pd.pool = repo->pool;
494 pd.data = repo_add_repodata(pd.repo, 0);
496 pd.content = malloc(256);
499 pd.attribute = attribute;
503 fullpath = root ? join2(root, "", "/etc") : "/etc";
504 dir = opendir(fullpath);
513 parse_dir(dir, fullpath, &pd, repodata, code11);
517 repodata_internalize(pd.data);