4 * Parses legacy /var/lib/zypp/db/products/... files.
5 * They are old (pre Code11) product descriptions. 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];
93 * find value for xml attribute
94 * I: txt, name of attribute
95 * I: atts, list of key/value attributes
96 * O: pointer to value of matching key, or NULL
100 static inline const char *
101 find_attr(const char *txt, const char **atts)
103 for (; *atts; atts += 2)
105 if (!strcmp(*atts, txt))
113 * XML callback: startElement
117 startElement(void *userData, const char *name, const char **atts)
119 struct parsedata *pd = userData;
120 Pool *pool = pd->pool;
121 Solvable *s = pd->solvable;
122 struct stateswitch *sw;
125 fprintf(stderr, "start: [%d]%s\n", pd->state, name);
127 if (pd->depth != pd->statedepth)
134 if (!pd->swtab[pd->state]) /* no statetable -> no substates */
137 fprintf(stderr, "into unknown: %s (from: %d)\n", name, pd->state);
141 for (sw = pd->swtab[pd->state]; sw->from == pd->state; sw++) /* find name in statetable */
142 if (!strcmp(sw->ename, name))
145 if (sw->from != pd->state)
148 fprintf(stderr, "into unknown: %s (from: %d)\n", name, pd->state);
153 pd->docontent = sw->docontent;
154 pd->statedepth = pd->depth;
163 const char *type = find_attr("type", atts);
164 s = pd->solvable = pool_id2solvable(pool, repo_add_solvable(pd->repo));
165 repodata_extend(pd->data, s - pool->solvables);
166 pd->handle = s - pool->solvables;
168 repodata_set_str(pd->data, pd->handle, PRODUCT_TYPE, type);
173 const char *ver = find_attr("ver", atts);
174 const char *rel = find_attr("rel", atts);
175 /* const char *epoch = find_attr("epoch", atts); ignored */
176 s->evr = makeevr(pd->pool, join2(&pd->jd, ver, "-", rel));
179 /* <summary lang="xy">... */
181 pd->tmplang = join_dup(&pd->jd, find_attr("lang", atts));
190 endElement(void *userData, const char *name)
192 struct parsedata *pd = userData;
193 Solvable *s = pd->solvable;
196 fprintf(stderr, "end: [%d]%s\n", pd->state, name);
198 if (pd->depth != pd->statedepth)
202 fprintf(stderr, "back from unknown %d %d %d\n", pd->state, pd->depth, pd->statedepth);
214 s->arch = ARCH_NOARCH;
217 if (s->name && s->arch != ARCH_SRC && s->arch != ARCH_NOSRC)
218 s->provides = repo_addid_dep(pd->repo, s->provides, pool_rel2id(pd->pool, s->name, s->evr, REL_EQ, 1), 0);
222 s->name = pool_str2id(pd->pool, join2(&pd->jd, "product", ":", pd->content), 1);
225 s->arch = pool_str2id(pd->pool, pd->content, 1);
228 repodata_set_str(pd->data, pd->handle, pool_id2langid(pd->pool, SOLVABLE_SUMMARY, pd->tmplang, 1), pd->content);
231 s->vendor = pool_str2id(pd->pool, pd->content, 1);
233 case STATE_INSTALLTIME:
234 repodata_set_num(pd->data, pd->handle, SOLVABLE_INSTALLTIME, atol(pd->content));
239 pd->state = pd->sbtab[pd->state];
243 fprintf(stderr, "end: [%s] -> %d\n", name, pd->state);
249 characterData(void *userData, const XML_Char *s, int len)
251 struct parsedata *pd = userData;
256 l = pd->lcontent + len + 1;
257 if (l > pd->acontent)
259 pd->content = realloc(pd->content, l + 256);
260 pd->acontent = l + 256;
262 c = pd->content + pd->lcontent;
269 #define BUFF_SIZE 8192
273 * add single product to repo
278 add_zyppdb_product(struct parsedata *pd, FILE *fp)
283 XML_Parser parser = XML_ParserCreate(NULL);
284 XML_SetUserData(parser, pd);
285 XML_SetElementHandler(parser, startElement, endElement);
286 XML_SetCharacterDataHandler(parser, characterData);
290 l = fread(buf, 1, sizeof(buf), fp);
291 if (XML_Parse(parser, buf, l, l == 0) == XML_STATUS_ERROR)
293 pool_debug(pd->pool, SOLV_ERROR, "repo_zyppdb: %s at line %u:%u\n", XML_ErrorString(XML_GetErrorCode(parser)), (unsigned int)XML_GetCurrentLineNumber(parser), (unsigned int)XML_GetCurrentColumnNumber(parser));
299 XML_ParserFree(parser);
304 * read all installed products
306 * parse each one as a product
310 repo_add_zyppdb_products(Repo *repo, const char *dirpath, int flags)
314 struct stateswitch *sw;
315 struct dirent *entry;
321 data = repo_add_repodata(repo, flags);
322 memset(&pd, 0, sizeof(pd));
324 pd.pool = repo->pool;
327 pd.content = malloc(256);
330 for (i = 0, sw = stateswitches; sw->from != NUMSTATES; i++, sw++)
332 if (!pd.swtab[sw->from])
333 pd.swtab[sw->from] = sw;
334 pd.sbtab[sw->to] = sw->from;
337 dir = opendir(dirpath);
340 while ((entry = readdir(dir)))
342 if (strlen(entry->d_name) < 3)
343 continue; /* skip '.' and '..' */
344 fullpath = join2(&pd.jd, dirpath, "/", entry->d_name);
345 if ((fp = fopen(fullpath, "r")) == 0)
350 add_zyppdb_product(&pd, fp);
357 join_freemem(&pd.jd);
358 if (!(flags & REPO_NO_INTERNALIZE))
359 repodata_internalize(data);