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">... */
182 const char *lang = find_attr("lang", atts);
183 pd->tmplang = lang ? join2(&pd->jd, lang, 0, 0) : 0;
193 endElement(void *userData, const char *name)
195 struct parsedata *pd = userData;
196 Solvable *s = pd->solvable;
199 fprintf(stderr, "end: [%d]%s\n", pd->state, name);
201 if (pd->depth != pd->statedepth)
205 fprintf(stderr, "back from unknown %d %d %d\n", pd->state, pd->depth, pd->statedepth);
217 s->arch = ARCH_NOARCH;
220 if (s->name && s->arch != ARCH_SRC && s->arch != ARCH_NOSRC)
221 s->provides = repo_addid_dep(pd->repo, s->provides, pool_rel2id(pd->pool, s->name, s->evr, REL_EQ, 1), 0);
225 s->name = pool_str2id(pd->pool, join2(&pd->jd, "product", ":", pd->content), 1);
228 s->arch = pool_str2id(pd->pool, pd->content, 1);
231 repodata_set_str(pd->data, pd->handle, pool_id2langid(pd->pool, SOLVABLE_SUMMARY, pd->tmplang, 1), pd->content);
234 s->vendor = pool_str2id(pd->pool, pd->content, 1);
236 case STATE_INSTALLTIME:
237 repodata_set_num(pd->data, pd->handle, SOLVABLE_INSTALLTIME, atol(pd->content));
242 pd->state = pd->sbtab[pd->state];
246 fprintf(stderr, "end: [%s] -> %d\n", name, pd->state);
252 characterData(void *userData, const XML_Char *s, int len)
254 struct parsedata *pd = userData;
259 l = pd->lcontent + len + 1;
260 if (l > pd->acontent)
262 pd->content = realloc(pd->content, l + 256);
263 pd->acontent = l + 256;
265 c = pd->content + pd->lcontent;
272 #define BUFF_SIZE 8192
276 * add single product to repo
281 add_zyppdb_product(struct parsedata *pd, FILE *fp)
286 XML_Parser parser = XML_ParserCreate(NULL);
287 XML_SetUserData(parser, pd);
288 XML_SetElementHandler(parser, startElement, endElement);
289 XML_SetCharacterDataHandler(parser, characterData);
293 l = fread(buf, 1, sizeof(buf), fp);
294 if (XML_Parse(parser, buf, l, l == 0) == XML_STATUS_ERROR)
296 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));
302 XML_ParserFree(parser);
307 * read all installed products
309 * parse each one as a product
313 repo_add_zyppdb_products(Repo *repo, const char *dirpath, int flags)
317 struct stateswitch *sw;
318 struct dirent *entry;
324 data = repo_add_repodata(repo, flags);
325 memset(&pd, 0, sizeof(pd));
327 pd.pool = repo->pool;
330 pd.content = malloc(256);
333 for (i = 0, sw = stateswitches; sw->from != NUMSTATES; i++, sw++)
335 if (!pd.swtab[sw->from])
336 pd.swtab[sw->from] = sw;
337 pd.sbtab[sw->to] = sw->from;
340 dir = opendir(dirpath);
343 while ((entry = readdir(dir)))
345 if (strlen(entry->d_name) < 3)
346 continue; /* skip '.' and '..' */
347 fullpath = join2(&pd.jd, dirpath, "/", entry->d_name);
348 if ((fp = fopen(fullpath, "r")) == 0)
353 add_zyppdb_product(&pd, fp);
360 join_freemem(&pd.jd);
361 if (!(flags & REPO_NO_INTERNALIZE))
362 repodata_internalize(data);