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>
32 #include "tools_util.h"
33 #include "repo_zyppdb.h"
55 /* !! must be sorted by first column !! */
56 static struct stateswitch stateswitches[] = {
57 { STATE_START, "product", STATE_PRODUCT, 0 },
58 { STATE_PRODUCT, "name", STATE_NAME, 1 },
59 { STATE_PRODUCT, "version", STATE_VERSION, 0 },
60 { STATE_PRODUCT, "arch", STATE_ARCH, 1 },
61 { STATE_PRODUCT, "summary", STATE_SUMMARY, 1 },
62 { STATE_PRODUCT, "install-time", STATE_INSTALLTIME, 1 },
63 { STATE_PRODUCT, "vendor", STATE_VENDOR, 1 },
79 struct stateswitch *swtab[NUMSTATES];
80 enum state sbtab[NUMSTATES];
92 * find value for xml attribute
93 * I: txt, name of attribute
94 * I: atts, list of key/value attributes
95 * O: pointer to value of matching key, or NULL
99 static inline const char *
100 find_attr(const char *txt, const char **atts)
102 for (; *atts; atts += 2)
104 if (!strcmp(*atts, txt))
112 * XML callback: startElement
116 startElement(void *userData, const char *name, const char **atts)
118 struct parsedata *pd = userData;
119 Pool *pool = pd->pool;
120 Solvable *s = pd->solvable;
121 struct stateswitch *sw;
124 fprintf(stderr, "start: [%d]%s\n", pd->state, name);
126 if (pd->depth != pd->statedepth)
133 if (!pd->swtab[pd->state]) /* no statetable -> no substates */
136 fprintf(stderr, "into unknown: %s (from: %d)\n", name, pd->state);
140 for (sw = pd->swtab[pd->state]; sw->from == pd->state; sw++) /* find name in statetable */
141 if (!strcmp(sw->ename, name))
144 if (sw->from != pd->state)
147 fprintf(stderr, "into unknown: %s (from: %d)\n", name, pd->state);
152 pd->docontent = sw->docontent;
153 pd->statedepth = pd->depth;
162 const char *type = find_attr("type", atts);
163 s = pd->solvable = pool_id2solvable(pool, repo_add_solvable(pd->repo));
164 repodata_extend(pd->data, s - pool->solvables);
165 pd->handle = s - pool->solvables;
167 repodata_set_str(pd->data, pd->handle, PRODUCT_TYPE, type);
172 const char *ver = find_attr("ver", atts);
173 const char *rel = find_attr("rel", atts);
174 /* const char *epoch = find_attr("epoch", atts); ignored */
175 s->evr = makeevr(pd->pool, join2(&pd->jd, ver, "-", rel));
178 /* <summary lang="xy">... */
180 pd->tmplang = join_dup(&pd->jd, find_attr("lang", atts));
189 endElement(void *userData, const char *name)
191 struct parsedata *pd = userData;
192 Solvable *s = pd->solvable;
195 fprintf(stderr, "end: [%d]%s\n", pd->state, name);
197 if (pd->depth != pd->statedepth)
201 fprintf(stderr, "back from unknown %d %d %d\n", pd->state, pd->depth, pd->statedepth);
213 s->arch = ARCH_NOARCH;
216 if (s->name && s->arch != ARCH_SRC && s->arch != ARCH_NOSRC)
217 s->provides = repo_addid_dep(pd->repo, s->provides, pool_rel2id(pd->pool, s->name, s->evr, REL_EQ, 1), 0);
221 s->name = pool_str2id(pd->pool, join2(&pd->jd, "product", ":", pd->content), 1);
224 s->arch = pool_str2id(pd->pool, pd->content, 1);
227 repodata_set_str(pd->data, pd->handle, pool_id2langid(pd->pool, SOLVABLE_SUMMARY, pd->tmplang, 1), pd->content);
230 s->vendor = pool_str2id(pd->pool, pd->content, 1);
232 case STATE_INSTALLTIME:
233 repodata_set_num(pd->data, pd->handle, SOLVABLE_INSTALLTIME, atol(pd->content));
238 pd->state = pd->sbtab[pd->state];
242 fprintf(stderr, "end: [%s] -> %d\n", name, pd->state);
248 characterData(void *userData, const XML_Char *s, int len)
250 struct parsedata *pd = userData;
255 l = pd->lcontent + len + 1;
256 if (l > pd->acontent)
258 pd->content = realloc(pd->content, l + 256);
259 pd->acontent = l + 256;
261 c = pd->content + pd->lcontent;
268 #define BUFF_SIZE 8192
272 * add single product to repo
277 add_zyppdb_product(struct parsedata *pd, FILE *fp)
282 XML_Parser parser = XML_ParserCreate(NULL);
283 XML_SetUserData(parser, pd);
284 XML_SetElementHandler(parser, startElement, endElement);
285 XML_SetCharacterDataHandler(parser, characterData);
289 l = fread(buf, 1, sizeof(buf), fp);
290 if (XML_Parse(parser, buf, l, l == 0) == XML_STATUS_ERROR)
292 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));
295 repo_free_solvable(pd->repo, pd->solvable - pd->pool->solvables, 1);
303 XML_ParserFree(parser);
308 * read all installed products
310 * parse each one as a product
314 repo_add_zyppdb_products(Repo *repo, const char *dirpath, int flags)
318 struct stateswitch *sw;
319 struct dirent *entry;
325 data = repo_add_repodata(repo, flags);
326 memset(&pd, 0, sizeof(pd));
328 pd.pool = repo->pool;
331 pd.content = malloc(256);
334 for (i = 0, sw = stateswitches; sw->from != NUMSTATES; i++, sw++)
336 if (!pd.swtab[sw->from])
337 pd.swtab[sw->from] = sw;
338 pd.sbtab[sw->to] = sw->from;
341 if (flags & REPO_USE_ROOTDIR)
342 dirpath = pool_prepend_rootdir(repo->pool, dirpath);
343 dir = opendir(dirpath);
346 while ((entry = readdir(dir)))
348 if (strlen(entry->d_name) < 3)
349 continue; /* skip '.' and '..' */
350 fullpath = join2(&pd.jd, dirpath, "/", entry->d_name);
351 if ((fp = fopen(fullpath, "r")) == 0)
353 pool_error(repo->pool, 0, "%s: %s", fullpath, strerror(errno));
356 add_zyppdb_product(&pd, fp);
363 join_freemem(&pd.jd);
364 if (flags & REPO_USE_ROOTDIR)
365 solv_free((char *)dirpath);
366 if (!(flags & REPO_NO_INTERNALIZE))
367 repodata_internalize(data);