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];
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);
235 s->arch = ARCH_NOARCH;
238 if (s->name && s->arch != ARCH_SRC && s->arch != ARCH_NOSRC)
239 s->provides = repo_addid_dep(pd->repo, s->provides, rel2id(pd->pool, s->name, s->evr, REL_EQ, 1), 0);
243 s->name = str2id(pd->pool, join2("product", ":", pd->content), 1);
246 s->arch = str2id(pd->pool, pd->content, 1);
249 repodata_set_str(pd->data, pd->handle, langtag(pd, SOLVABLE_SUMMARY, pd->tmplang), pd->content);
250 pd->tmplang = sat_free((void *)pd->tmplang);
253 s->vendor = str2id(pd->pool, pd->content, 1);
255 case STATE_INSTALLTIME:
256 repodata_set_num(pd->data, pd->handle, SOLVABLE_INSTALLTIME, atol(pd->content));
261 pd->state = pd->sbtab[pd->state];
265 fprintf(stderr, "end: [%s] -> %d\n", name, pd->state);
271 characterData(void *userData, const XML_Char *s, int len)
273 struct parsedata *pd = userData;
276 if (!pd->docontent) {
278 char *dup = strndup( s, len );
279 fprintf(stderr, "Content: [%d]'%s'\n", pd->state, dup );
284 l = pd->lcontent + len + 1;
285 if (l > pd->acontent)
287 pd->content = realloc(pd->content, l + 256);
288 pd->acontent = l + 256;
290 c = pd->content + pd->lcontent;
297 #define BUFF_SIZE 8192
301 * add single product to repo
306 add_zyppdb_product(struct parsedata *pd, FILE *fp)
311 XML_Parser parser = XML_ParserCreate(NULL);
312 XML_SetUserData(parser, pd);
313 XML_SetElementHandler(parser, startElement, endElement);
314 XML_SetCharacterDataHandler(parser, characterData);
318 l = fread(buf, 1, sizeof(buf), fp);
319 if (XML_Parse(parser, buf, l, l == 0) == XML_STATUS_ERROR)
321 pool_debug(pd->pool, SAT_ERROR, "repo_zyppdb: %s at line %u:%u\n", XML_ErrorString(XML_GetErrorCode(parser)), (unsigned int)XML_GetCurrentLineNumber(parser), (unsigned int)XML_GetCurrentColumnNumber(parser));
327 XML_ParserFree(parser);
332 * read all installed products
334 * parse each one as a product
338 repo_add_zyppdb_products(Repo *repo, const char *dirpath, int flags)
342 struct stateswitch *sw;
343 struct dirent *entry;
349 data = repo_add_repodata(repo, flags);
350 memset(&pd, 0, sizeof(pd));
352 pd.pool = repo->pool;
355 pd.content = malloc(256);
358 for (i = 0, sw = stateswitches; sw->from != NUMSTATES; i++, sw++)
360 if (!pd.swtab[sw->from])
361 pd.swtab[sw->from] = sw;
362 pd.sbtab[sw->to] = sw->from;
365 dir = opendir(dirpath);
368 while ((entry = readdir(dir)))
370 if (strlen(entry->d_name) < 3)
371 continue; /* skip '.' and '..' */
372 fullpath = join2(dirpath, "/", entry->d_name);
373 if ((fp = fopen(fullpath, "r")) == 0)
378 add_zyppdb_product(&pd, fp);
384 sat_free((void *)pd.tmplang);
387 if (!(flags & REPO_NO_INTERNALIZE))
388 repodata_internalize(data);