4 * Parses /var/lib/zypp/db/products/...
5 * The are old (pre Code11) products. See bnc#429177
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"
45 STATE_INSTALLTIME, // 7
56 /* !! must be sorted by first column !! */
57 static struct stateswitch stateswitches[] = {
58 { STATE_START, "product", STATE_PRODUCT, 0 },
59 { STATE_PRODUCT, "name", STATE_NAME, 1 },
60 { STATE_PRODUCT, "version", STATE_VERSION, 0 },
61 { STATE_PRODUCT, "arch", STATE_ARCH, 1 },
62 { STATE_PRODUCT, "summary", STATE_SUMMARY, 1 },
63 { STATE_PRODUCT, "install-time", STATE_INSTALLTIME, 1 },
64 { STATE_PRODUCT, "vendor", STATE_VENDOR, 1 },
80 struct stateswitch *swtab[NUMSTATES];
81 enum state sbtab[NUMSTATES];
88 Id langcache[ID_NUM_INTERNAL];
94 * find value for xml attribute
95 * I: txt, name of attribute
96 * I: atts, list of key/value attributes
98 * O: pointer to value of matching key, or NULL
102 static inline const char *
103 find_attr(const char *txt, const char **atts, int dup)
105 for (; *atts; atts += 2)
107 if (!strcmp(*atts, txt))
108 return dup ? strdup(atts[1]) : atts[1];
115 * create localized tag
119 langtag(struct parsedata *pd, Id tag, const char *language)
121 if (language && !language[0])
123 if (!language || tag >= ID_NUM_INTERNAL)
124 return pool_id2langid(pd->repo->pool, tag, language, 1);
125 if (!pd->langcache[tag])
126 pd->langcache[tag] = pool_id2langid(pd->repo->pool, tag, language, 1);
127 return pd->langcache[tag];
132 * XML callback: startElement
136 startElement(void *userData, const char *name, const char **atts)
138 struct parsedata *pd = userData;
139 Pool *pool = pd->pool;
140 Solvable *s = pd->solvable;
141 struct stateswitch *sw;
144 fprintf(stderr, "start: [%d]%s\n", pd->state, name);
146 if (pd->depth != pd->statedepth)
153 if (!pd->swtab[pd->state]) /* no statetable -> no substates */
156 fprintf(stderr, "into unknown: %s (from: %d)\n", name, pd->state);
160 for (sw = pd->swtab[pd->state]; sw->from == pd->state; sw++) /* find name in statetable */
161 if (!strcmp(sw->ename, name))
164 if (sw->from != pd->state)
167 fprintf(stderr, "into unknown: %s (from: %d)\n", name, pd->state);
172 pd->docontent = sw->docontent;
173 pd->statedepth = pd->depth;
182 const char *type = find_attr("type", atts, 0);
183 s = pd->solvable = pool_id2solvable(pool, repo_add_solvable(pd->repo));
184 repodata_extend(pd->data, s - pool->solvables);
185 pd->handle = s - pool->solvables;
188 repodata_set_str(pd->data, pd->handle, PRODUCT_TYPE, type);
194 const char *ver = find_attr("ver", atts, 0);
195 const char *rel = find_attr("rel", atts, 0);
196 /* const char *epoch = find_attr("epoch", atts, 1); ignored */
197 s->evr = makeevr(pd->pool, join2(ver, "-", rel));
200 /* <summary lang="xy">... */
202 pd->tmplang = find_attr("lang", atts, 1);
211 endElement(void *userData, const char *name)
213 struct parsedata *pd = userData;
214 Solvable *s = pd->solvable;
217 fprintf(stderr, "end: [%d]%s\n", pd->state, name);
219 if (pd->depth != pd->statedepth)
223 fprintf(stderr, "back from unknown %d %d %d\n", pd->state, pd->depth, pd->statedepth);
236 s->arch = ARCH_NOARCH;
237 if (s->arch != ARCH_SRC && s->arch != ARCH_NOSRC)
238 s->provides = repo_addid_dep(pd->repo, s->provides, rel2id(pd->pool, s->name, s->evr, REL_EQ, 1), 0);
242 s->name = str2id(pd->pool, join2("product", ":", pd->content), 1);
245 s->arch = str2id(pd->pool, pd->content, 1);
248 repodata_set_str(pd->data, pd->handle, langtag(pd, SOLVABLE_SUMMARY, pd->tmplang), pd->content);
249 pd->tmplang = sat_free((void *)pd->tmplang);
252 s->vendor = str2id(pd->pool, pd->content, 1);
254 case STATE_INSTALLTIME:
255 repodata_set_num(pd->data, pd->handle, SOLVABLE_INSTALLTIME, atol(pd->content));
260 pd->state = pd->sbtab[pd->state];
264 fprintf(stderr, "end: [%s] -> %d\n", name, pd->state);
270 characterData(void *userData, const XML_Char *s, int len)
272 struct parsedata *pd = userData;
275 if (!pd->docontent) {
277 char *dup = strndup( s, len );
278 fprintf(stderr, "Content: [%d]'%s'\n", pd->state, dup );
283 l = pd->lcontent + len + 1;
284 if (l > pd->acontent)
286 pd->content = realloc(pd->content, l + 256);
287 pd->acontent = l + 256;
289 c = pd->content + pd->lcontent;
296 #define BUFF_SIZE 8192
300 * add single product to repo
305 repo_add_product(struct parsedata *pd, Repodata *data, FILE *fp)
310 XML_Parser parser = XML_ParserCreate(NULL);
311 XML_SetUserData(parser, pd);
312 XML_SetElementHandler(parser, startElement, endElement);
313 XML_SetCharacterDataHandler(parser, characterData);
317 l = fread(buf, 1, sizeof(buf), fp);
318 if (XML_Parse(parser, buf, l, l == 0) == XML_STATUS_ERROR)
320 fprintf(stderr, "repo_zyppdb: %s at line %u:%u\n", XML_ErrorString(XML_GetErrorCode(parser)), (unsigned int)XML_GetCurrentLineNumber(parser), (unsigned int)XML_GetCurrentColumnNumber(parser));
326 XML_ParserFree(parser);
333 * parse dir for products
337 parse_dir(DIR *dir, const char *path, struct parsedata *pd, Repodata *repodata)
339 struct dirent *entry;
341 while ((entry = readdir(dir)))
343 int len = strlen(entry->d_name);
344 if (len < 3) /* skip '.' and '..' */
346 char *fullpath = join2(path, "/", entry->d_name);
347 FILE *fp = fopen(fullpath, "r");
353 repo_add_product(pd, repodata, fp);
360 * read all installed products
362 * parse each one as a product
366 repo_add_zyppdb_products(Repo *repo, Repodata *repodata, const char *fullpath, DIR *dir)
370 struct stateswitch *sw;
372 memset(&pd, 0, sizeof(pd));
374 pd.pool = repo->pool;
377 pd.content = malloc(256);
380 for (i = 0, sw = stateswitches; sw->from != NUMSTATES; i++, sw++)
382 if (!pd.swtab[sw->from])
383 pd.swtab[sw->from] = sw;
384 pd.sbtab[sw->to] = sw->from;
387 parse_dir(dir, fullpath, &pd, repodata);
389 sat_free((void *)pd.tmplang);