4 * Parses all files below 'proddir'
5 * See http://en.opensuse.org/Product_Management/Code11
8 * Copyright (c) 2008, Novell Inc.
10 * This program is licensed under the BSD license, read LICENSE.BSD
11 * for further information
14 #include <sys/types.h>
31 #include "tools_util.h"
32 #include "repo_content.h"
33 #include "repo_zyppdb.h"
34 #include "repo_releasefile_products.h"
48 STATE_DESCRIPTION, // 8
49 STATE_UPDATEREPOKEY, // 9 should go away
53 STATE_RUNTIMECONFIG, // 12
58 STATE_REGRELEASE, // 18
59 STATE_PRODUCTLINE, // 19
70 /* !! must be sorted by first column !! */
71 static struct stateswitch stateswitches[] = {
72 { STATE_START, "product", STATE_PRODUCT, 0 },
73 { STATE_PRODUCT, "vendor", STATE_VENDOR, 1 },
74 { STATE_PRODUCT, "name", STATE_NAME, 1 },
75 { STATE_PRODUCT, "version", STATE_VERSION, 1 },
76 { STATE_PRODUCT, "release", STATE_RELEASE, 1 },
77 { STATE_PRODUCT, "arch", STATE_ARCH, 1 },
78 { STATE_PRODUCT, "productline", STATE_PRODUCTLINE, 1 },
79 { STATE_PRODUCT, "summary", STATE_SUMMARY, 1 },
80 { STATE_PRODUCT, "description", STATE_DESCRIPTION, 1 },
81 { STATE_PRODUCT, "register", STATE_REGISTER, 0 },
82 { STATE_PRODUCT, "urls", STATE_URLS, 0 },
83 { STATE_PRODUCT, "runtimeconfig", STATE_RUNTIMECONFIG, 0 },
84 { STATE_PRODUCT, "linguas", STATE_LINGUAS, 0 },
85 { STATE_PRODUCT, "updaterepokey", STATE_UPDATEREPOKEY, 1 },
86 { STATE_PRODUCT, "cpeid", STATE_CPEID, 1 },
87 { STATE_URLS, "url", STATE_URL, 1 },
88 { STATE_LINGUAS, "lang", STATE_LANG, 0 },
89 { STATE_REGISTER, "target", STATE_TARGET, 1 },
90 { STATE_REGISTER, "release", STATE_REGRELEASE, 1 },
108 struct stateswitch *swtab[NUMSTATES];
109 enum state sbtab[NUMSTATES];
115 const char *tmpurltype;
123 ino_t currentproduct;
126 Id langcache[ID_NUM_INTERNAL];
132 * find value for xml attribute
133 * I: txt, name of attribute
134 * I: atts, list of key/value attributes
136 * O: pointer to value of matching key, or NULL
140 static inline const char *
141 find_attr(const char *txt, const char **atts, int dup)
143 for (; *atts; atts += 2)
145 if (!strcmp(*atts, txt))
146 return dup ? strdup(atts[1]) : atts[1];
153 * create localized tag
157 langtag(struct parsedata *pd, Id tag, const char *language)
159 if (language && !language[0])
161 if (!language || tag >= ID_NUM_INTERNAL)
162 return pool_id2langid(pd->repo->pool, tag, language, 1);
163 if (!pd->langcache[tag])
164 pd->langcache[tag] = pool_id2langid(pd->repo->pool, tag, language, 1);
165 return pd->langcache[tag];
170 * XML callback: startElement
174 startElement(void *userData, const char *name, const char **atts)
176 struct parsedata *pd = userData;
177 Pool *pool = pd->pool;
178 Solvable *s = pd->solvable;
179 struct stateswitch *sw;
182 fprintf(stderr, "start: [%d]%s\n", pd->state, name);
184 if (pd->depth != pd->statedepth)
191 if (!pd->swtab[pd->state]) /* no statetable -> no substates */
194 fprintf(stderr, "into unknown: %s (from: %d)\n", name, pd->state);
198 for (sw = pd->swtab[pd->state]; sw->from == pd->state; sw++) /* find name in statetable */
199 if (!strcmp(sw->ename, name))
202 if (sw->from != pd->state)
205 fprintf(stderr, "into unknown: %s (from: %d)\n", name, pd->state);
210 pd->docontent = sw->docontent;
211 pd->statedepth = pd->depth;
218 /* parse 'schemeversion' and store in global variable */
220 const char * scheme = find_attr("schemeversion", atts, 0);
221 pd->productscheme = (scheme && *scheme) ? atoi(scheme) : -1;
225 s = pd->solvable = pool_id2solvable(pool, repo_add_solvable(pd->repo));
226 pd->handle = s - pool->solvables;
230 /* <summary lang="xy">... */
232 pd->tmplang = find_attr("lang", atts, 1);
234 case STATE_DESCRIPTION:
235 pd->tmplang = find_attr("lang", atts, 1);
238 pd->tmpurltype = find_attr("name", atts, 1);
247 endElement(void *userData, const char *name)
249 struct parsedata *pd = userData;
250 Solvable *s = pd->solvable;
253 fprintf(stderr, "end: [%d]%s\n", pd->state, name);
255 if (pd->depth != pd->statedepth)
259 fprintf(stderr, "back from unknown %d %d %d\n", pd->state, pd->depth, pd->statedepth);
270 /* product done, finish solvable */
272 repodata_set_num(pd->data, pd->handle, SOLVABLE_INSTALLTIME, pd->ctime);
275 repodata_set_str(pd->data, pd->handle, PRODUCT_REFERENCEFILE, pd->basename);
277 /* this is where <productsdir>/baseproduct points to */
278 if (pd->currentproduct == pd->baseproduct)
279 repodata_set_str(pd->data, pd->handle, PRODUCT_TYPE, "base");
284 s->evr = makeevr(pd->pool, join2(pd->tmpvers, "-", pd->tmprel));
287 fprintf(stderr, "Seen <release> but no <version>\n");
290 else if (pd->tmpvers)
291 s->evr = makeevr(pd->pool, pd->tmpvers); /* just version, no release */
292 pd->tmpvers = sat_free((void *)pd->tmpvers);
293 pd->tmprel = sat_free((void *)pd->tmprel);
295 s->arch = ARCH_NOARCH;
298 if (s->name && s->arch != ARCH_SRC && s->arch != ARCH_NOSRC)
299 s->provides = repo_addid_dep(pd->repo, s->provides, rel2id(pd->pool, s->name, s->evr, REL_EQ, 1), 0);
303 s->vendor = str2id(pd->pool, pd->content, 1);
306 s->name = str2id(pd->pool, join2("product", ":", pd->content), 1);
309 pd->tmpvers = strdup(pd->content);
312 pd->tmprel = strdup(pd->content);
315 s->arch = str2id(pd->pool, pd->content, 1);
317 case STATE_PRODUCTLINE:
318 repodata_set_str(pd->data, pd->handle, PRODUCT_PRODUCTLINE, pd->content);
320 case STATE_UPDATEREPOKEY:
324 repodata_set_str(pd->data, pd->handle, langtag(pd, SOLVABLE_SUMMARY, pd->tmplang), pd->content);
325 pd->tmplang = sat_free((void *)pd->tmplang);
327 case STATE_DESCRIPTION:
328 repodata_set_str(pd->data, pd->handle, langtag(pd, SOLVABLE_DESCRIPTION, pd->tmplang), pd->content );
329 pd->tmplang = sat_free((void *)pd->tmplang);
334 repodata_add_poolstr_array(pd->data, pd->handle, PRODUCT_URL, pd->content);
335 repodata_add_idarray(pd->data, pd->handle, PRODUCT_URL_TYPE, str2id(pd->pool, pd->tmpurltype, 1));
337 pd->tmpurltype = sat_free((void *)pd->tmpurltype);
340 repodata_set_str(pd->data, pd->handle, PRODUCT_REGISTER_TARGET, pd->content);
342 case STATE_REGRELEASE:
343 repodata_set_str(pd->data, pd->handle, PRODUCT_REGISTER_RELEASE, pd->content);
347 repodata_set_str(pd->data, pd->handle, SOLVABLE_CPEID, pd->content);
352 pd->state = pd->sbtab[pd->state];
356 fprintf(stderr, "end: [%s] -> %d\n", name, pd->state);
362 characterData(void *userData, const XML_Char *s, int len)
364 struct parsedata *pd = userData;
369 l = pd->lcontent + len + 1;
370 if (l > pd->acontent)
372 pd->content = sat_realloc(pd->content, l + 256);
373 pd->acontent = l + 256;
375 c = pd->content + pd->lcontent;
382 #define BUFF_SIZE 8192
386 * add single product to repo
391 add_code11_product(struct parsedata *pd, FILE *fp)
397 if (!fstat(fileno(fp), &st))
399 pd->currentproduct = st.st_ino;
400 pd->ctime = (unsigned int)st.st_ctime;
404 pd->currentproduct = pd->baseproduct + 1; /* make it != baseproduct if stat fails */
409 XML_Parser parser = XML_ParserCreate(NULL);
410 XML_SetUserData(parser, pd);
411 XML_SetElementHandler(parser, startElement, endElement);
412 XML_SetCharacterDataHandler(parser, characterData);
416 l = fread(buf, 1, sizeof(buf), fp);
417 if (XML_Parse(parser, buf, l, l == 0) == XML_STATUS_ERROR)
419 pool_debug(pd->pool, SAT_ERROR, "%s: %s at line %u:%u\n", pd->filename, XML_ErrorString(XML_GetErrorCode(parser)), (unsigned int)XML_GetCurrentLineNumber(parser), (unsigned int)XML_GetCurrentColumnNumber(parser));
420 pool_debug(pd->pool, SAT_ERROR, "skipping this product\n");
421 XML_ParserFree(parser);
427 XML_ParserFree(parser);
432 repo_add_code11_products(Repo *repo, const char *dirpath, int flags)
436 struct stateswitch *sw;
440 data = repo_add_repodata(repo, flags);
442 memset(&pd, 0, sizeof(pd));
444 pd.pool = repo->pool;
447 pd.content = sat_malloc(256);
450 for (i = 0, sw = stateswitches; sw->from != NUMSTATES; i++, sw++)
452 if (!pd.swtab[sw->from])
453 pd.swtab[sw->from] = sw;
454 pd.sbtab[sw->to] = sw->from;
457 dir = opendir(dirpath);
460 struct dirent *entry;
464 /* check for <productsdir>/baseproduct on code11 and remember its target inode */
465 if (stat(join2(dirpath, "/", "baseproduct"), &st) == 0) /* follow symlink */
466 pd.baseproduct = st.st_ino;
470 while ((entry = readdir(dir)))
472 int len = strlen(entry->d_name);
473 if (len <= 5 || strcmp(entry->d_name + len - 5, ".prod") != 0)
475 fullpath = join2(dirpath, "/", entry->d_name);
476 FILE *fp = fopen(fullpath, "r");
482 pd.filename = fullpath;
483 pd.basename = entry->d_name;
484 add_code11_product(&pd, fp);
489 sat_free((void *)pd.tmplang);
490 sat_free(pd.content);
493 if (!(flags & REPO_NO_INTERNALIZE))
494 repodata_internalize(data);
498 /******************************************************************************************/
502 * read all installed products
504 * try proddir (reading all .xml files from this directory) first
505 * if not available, assume non-code11 layout and parse /etc/xyz-release
507 * parse each one as a product
510 /* Oh joy! Three parsers for the price of one! */
513 repo_add_products(Repo *repo, const char *proddir, const char *root, int flags)
515 const char *fullpath;
518 dir = opendir(proddir);
521 /* assume code11 stype products */
523 repo_add_code11_products(repo, proddir, flags);
527 /* code11 didn't work, try old zyppdb */
528 fullpath = root ? join2(root, "", "/var/lib/zypp/db/products") : "/var/lib/zypp/db/products";
529 dir = opendir(fullpath);
533 /* assume code10 style products */
534 repo_add_zyppdb_products(repo, fullpath, flags);
539 /* code11 didn't work, try -release files parsing */
540 fullpath = root ? join2(root, "", "/etc") : "/etc";
541 dir = opendir(fullpath);
545 repo_add_releasefile_products(repo, fullpath, flags);
550 /* no luck. print an error message in case the root argument is wrong */
554 /* the least we can do... */
555 if (!(flags & REPO_NO_INTERNALIZE) && (flags & REPO_REUSE_REPODATA) != 0)
556 repodata_internalize(repo_last_repodata(repo));