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;
65 /* !! must be sorted by first column !! */
66 static struct stateswitch stateswitches[] = {
67 { STATE_START, "product", STATE_PRODUCT, 0 },
68 { STATE_PRODUCT, "general", STATE_GENERAL, 0 },
69 { STATE_GENERAL, "vendor", STATE_VENDOR, 1 },
70 { STATE_GENERAL, "name", STATE_NAME, 1 },
71 { STATE_GENERAL, "version", STATE_VERSION, 1 },
72 { STATE_GENERAL, "release", STATE_RELEASE, 1 },
73 { STATE_GENERAL, "summary", STATE_SUMMARY, 1 },
74 { STATE_GENERAL, "description", STATE_DESCRIPTION, 1 },
75 { STATE_GENERAL, "distribution", STATE_DISTRIBUTION, 0 },
76 { STATE_GENERAL, "urls", STATE_URLS, 0 },
77 { STATE_GENERAL, "runtimeconfig", STATE_RUNTIMECONFIG, 0 },
78 { STATE_GENERAL, "linguas", STATE_LINGUAS, 0 },
79 { STATE_URLS, "url", STATE_URL, 0 },
80 { STATE_LINGUAS, "lang", STATE_LANG, 0 },
97 struct stateswitch *swtab[NUMSTATES];
98 enum state sbtab[NUMSTATES];
100 const char *attribute; /* only print this attribute */
109 Id langcache[ID_NUM_INTERNAL];
115 * find value for xml attribute
116 * I: txt, name of attribute
117 * I: atts, list of key/value attributes
119 * O: pointer to value of matching key, or NULL
123 static inline const char *
124 find_attr(const char *txt, const char **atts, int dup)
126 for (; *atts; atts += 2)
128 if (!strcmp(*atts, txt))
129 return dup ? strdup(atts[1]) : atts[1];
136 * create localized tag
140 langtag(struct parsedata *pd, Id tag, const char *language)
142 if (language && !language[0])
144 if (!language || tag >= ID_NUM_INTERNAL)
145 return pool_id2langid(pd->repo->pool, tag, language, 1);
146 if (!pd->langcache[tag])
147 pd->langcache[tag] = pool_id2langid(pd->repo->pool, tag, language, 1);
148 return pd->langcache[tag];
153 * XML callback: startElement
157 startElement(void *userData, const char *name, const char **atts)
159 struct parsedata *pd = userData;
160 Pool *pool = pd->pool;
161 struct stateswitch *sw;
164 fprintf(stderr, "start: [%d]%s\n", pd->state, name);
166 if (pd->depth != pd->statedepth)
173 for (sw = pd->swtab[pd->state]; sw->from == pd->state; sw++) /* find name in statetable */
174 if (!strcmp(sw->ename, name))
177 if (sw->from != pd->state)
180 fprintf(stderr, "into unknown: [%d]%s (from: %d)\n", sw->to, name, sw->from);
186 pd->docontent = sw->docontent;
187 pd->statedepth = pd->depth;
199 pd->s = pool_id2solvable(pool, repo_add_solvable(pd->repo));
200 repodata_extend(pd->data, pd->s - pool->solvables);
201 pd->handle = repodata_get_handle(pd->data, pd->s - pool->solvables - pd->repo->start);
205 /* <summary lang="xy">... */
207 pd->tmplang = find_attr("lang", atts, 1);
209 case STATE_DESCRIPTION:
210 pd->tmplang = find_attr("lang", atts, 1);
212 case STATE_DISTRIBUTION:
215 if ((str = find_attr("flavor", atts, 0)))
216 repo_set_str(pd->repo, pd->s - pool->solvables, PRODUCT_FLAVOR, str);
217 if ((str = find_attr("target", atts, 0)))
219 if (pd->attribute && !strcmp(pd->attribute, "distribution.target"))
222 repo_set_str(pd->repo, pd->s - pool->solvables, SOLVABLE_DISTRIBUTION, str);
228 case STATE_RUNTIMECONFIG:
237 endElement(void *userData, const char *name)
239 struct parsedata *pd = userData;
242 fprintf(stderr, "end: %s\n", name);
244 if (pd->depth != pd->statedepth)
248 fprintf(stderr, "back from unknown %d %d %d\n", pd->state, pd->depth, pd->statedepth);
259 pd->s->vendor = str2id(pd->pool, pd->content, 1);
262 pd->s->name = str2id(pd->pool, join2("product", ":", pd->content), 1);
265 pd->tmpvers = strdup(pd->content);
268 pd->tmprel = strdup(pd->content);
271 repodata_set_str(pd->data, pd->handle, langtag(pd, SOLVABLE_SUMMARY, pd->tmplang), pd->content);
274 free( (char *)pd->tmplang );
278 case STATE_DESCRIPTION:
279 repodata_set_str(pd->data, pd->handle, langtag(pd, SOLVABLE_DESCRIPTION, pd->tmplang), pd->content );
282 free( (char *)pd->tmplang );
286 case STATE_DISTRIBUTION:
290 case STATE_RUNTIMECONFIG:
296 pd->state = pd->sbtab[pd->state];
304 characterData(void *userData, const XML_Char *s, int len)
306 struct parsedata *pd = userData;
309 if (!pd->docontent) {
311 char *dup = strndup( s, len );
312 fprintf(stderr, "Content: [%d]'%s'\n", pd->state, dup );
317 l = pd->lcontent + len + 1;
318 if (l > pd->acontent)
320 pd->content = realloc(pd->content, l + 256);
321 pd->acontent = l + 256;
323 c = pd->content + pd->lcontent;
330 #define BUFF_SIZE 8192
334 * add single product to repo
339 repo_add_product(struct parsedata *pd, Repodata *data, FILE *fp, int code11)
341 Pool *pool = pd->pool;
344 struct stateswitch *sw;
346 for (i = 0, sw = stateswitches; sw->from != NUMSTATES; i++, sw++)
348 if (!pd->swtab[sw->from])
349 pd->swtab[sw->from] = sw;
350 pd->sbtab[sw->to] = sw->from;
352 XML_Parser parser = XML_ParserCreate(NULL);
353 XML_SetUserData(parser, pd);
354 XML_SetElementHandler(parser, startElement, endElement);
355 XML_SetCharacterDataHandler(parser, characterData);
359 l = fread(buf, 1, sizeof(buf), fp);
360 if (XML_Parse(parser, buf, l, l == 0) == XML_STATUS_ERROR)
362 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));
368 XML_ParserFree(parser);
374 if (!fstat(fileno(fp), &st))
376 repodata_set_num(pd->data, pd->handle, SOLVABLE_INSTALLTIME, st.st_ctime);
377 /* this is where <productsdir>/baseproduct points to */
378 if (st.st_ino == baseproduct)
379 repodata_set_str(pd->data, pd->handle, PRODUCT_TYPE, "base");
383 perror("Can't stat()");
390 s->evr = makeevr(pool, join2(pd->tmpvers, "-", pd->tmprel));
391 free((char *)pd->tmpvers);
396 fprintf(stderr, "Seen <release> but no <version>\n");
398 free((char *)pd->tmprel);
401 else if (pd->tmpvers)
403 s->evr = makeevr(pool, pd->tmpvers); /* just version, no release */
404 free((char *)pd->tmpvers);
408 s->arch = ARCH_NOARCH;
409 if (s->arch != ARCH_SRC && s->arch != ARCH_NOSRC)
411 s->provides = repo_addid_dep(pd->repo, s->provides, rel2id(pool, s->name, s->evr, REL_EQ, 1), 0);
419 * parse dir looking for files ending in suffix
423 parse_dir(DIR *dir, const char *path, struct parsedata *pd, Repodata *repodata, int code11)
425 struct dirent *entry;
426 char *suffix = code11 ? ".xml" : "-release";
427 int slen = code11 ? 4 : 8; /* strlen(".xml") : strlen("-release") */
430 /* check for <productsdir>/baseproduct on code11 and remember its target inode */
432 && stat(join2(path, "/", "baseproduct"), &st) == 0) /* follow symlink */
434 baseproduct = st.st_ino;
437 while ((entry = readdir(dir)))
440 len = strlen(entry->d_name);
442 /* skip /etc/lsb-release, thats not a product per-se */
444 && strcmp(entry->d_name, "lsb-release") == 0)
450 && strcmp(entry->d_name+len-slen, suffix) == 0)
452 char *fullpath = join2(path, "/", entry->d_name);
453 FILE *fp = fopen(fullpath, "r");
459 repo_add_product(pd, repodata, fp, code11);
467 * read all installed products
469 * try proddir (reading all .xml files from this directory) first
470 * if not available, assume non-code11 layout and parse /etc/xyz-release
472 * parse each one as a product
476 repo_add_products(Repo *repo, Repodata *repodata, const char *proddir, const char *root, const char *attribute)
478 const char *fullpath = proddir;
480 DIR *dir = opendir(fullpath);
483 memset(&pd, 0, sizeof(pd));
485 pd.pool = repo->pool;
486 pd.data = repo_add_repodata(pd.repo, 0);
488 pd.content = malloc(256);
491 pd.attribute = attribute;
495 fullpath = root ? join2(root, "", "/etc") : "/etc";
496 dir = opendir(fullpath);
505 parse_dir(dir, fullpath, &pd, repodata, code11);
509 repodata_internalize(pd.data);