4 * Parses legacy /var/lib/zypp/db/products/... files.
5 * They are old (pre Code11) product descriptions. See bnc#429177
7 * Copyright (c) 2008, Novell Inc.
9 * This program is licensed under the BSD license, read LICENSE.BSD
10 * for further information
13 #include <sys/types.h>
26 #include "solv_xmlparser.h"
28 #include "tools_util.h"
29 #include "repo_zyppdb.h"
44 static struct solv_xmlparser_element stateswitches[] = {
45 { STATE_START, "product", STATE_PRODUCT, 0 },
46 { STATE_PRODUCT, "name", STATE_NAME, 1 },
47 { STATE_PRODUCT, "version", STATE_VERSION, 0 },
48 { STATE_PRODUCT, "arch", STATE_ARCH, 1 },
49 { STATE_PRODUCT, "summary", STATE_SUMMARY, 1 },
50 { STATE_PRODUCT, "install-time", STATE_INSTALLTIME, 1 },
51 { STATE_PRODUCT, "vendor", STATE_VENDOR, 1 },
63 struct solv_xmlparser xmlp;
70 startElement(struct solv_xmlparser *xmlp, int state, const char *name, const char **atts)
72 struct parsedata *pd = xmlp->userdata;
73 Pool *pool = pd->pool;
74 Solvable *s = pd->solvable;
81 const char *type = solv_xmlparser_find_attr("type", atts);
82 s = pd->solvable = pool_id2solvable(pool, repo_add_solvable(pd->repo));
83 pd->handle = s - pool->solvables;
85 repodata_set_str(pd->data, pd->handle, PRODUCT_TYPE, type);
90 const char *ver = solv_xmlparser_find_attr("ver", atts);
91 const char *rel = solv_xmlparser_find_attr("rel", atts);
92 /* const char *epoch = solv_xmlparser_find_attr("epoch", atts); ignored */
93 s->evr = makeevr(pd->pool, join2(&pd->jd, ver, "-", rel));
96 case STATE_SUMMARY: /* <summary lang="xy">... */
97 pd->tmplang = join_dup(&pd->jd, solv_xmlparser_find_attr("lang", atts));
106 endElement(struct solv_xmlparser *xmlp, int state, char *content)
108 struct parsedata *pd = xmlp->userdata;
109 Solvable *s = pd->solvable;
115 s->arch = ARCH_NOARCH;
118 if (s->name && s->arch != ARCH_SRC && s->arch != ARCH_NOSRC)
119 s->provides = repo_addid_dep(pd->repo, s->provides, pool_rel2id(pd->pool, s->name, s->evr, REL_EQ, 1), 0);
123 s->name = pool_str2id(pd->pool, join2(&pd->jd, "product", ":", content), 1);
126 s->arch = pool_str2id(pd->pool, content, 1);
129 repodata_set_str(pd->data, pd->handle, pool_id2langid(pd->pool, SOLVABLE_SUMMARY, pd->tmplang, 1), content);
132 s->vendor = pool_str2id(pd->pool, content, 1);
134 case STATE_INSTALLTIME:
135 repodata_set_num(pd->data, pd->handle, SOLVABLE_INSTALLTIME, atol(content));
143 * read all installed products
145 * parse each one as a product
149 repo_add_zyppdb_products(Repo *repo, const char *dirpath, int flags)
152 struct dirent *entry;
158 data = repo_add_repodata(repo, flags);
159 memset(&pd, 0, sizeof(pd));
161 pd.pool = repo->pool;
163 solv_xmlparser_init(&pd.xmlp, stateswitches, &pd, startElement, endElement);
165 if (flags & REPO_USE_ROOTDIR)
166 dirpath = pool_prepend_rootdir(repo->pool, dirpath);
167 dir = opendir(dirpath);
170 while ((entry = readdir(dir)))
172 if (entry->d_name[0] == '.')
173 continue; /* skip dot files */
174 fullpath = join2(&pd.jd, dirpath, "/", entry->d_name);
175 if ((fp = fopen(fullpath, "r")) == 0)
177 pool_error(repo->pool, 0, "%s: %s", fullpath, strerror(errno));
180 pd.filename = entry->d_name;
181 if (solv_xmlparser_parse(&pd.xmlp, fp) != SOLV_XMLPARSER_OK)
183 pool_debug(pd.pool, SOLV_ERROR, "repo_zyppdb: %s: %s at line %u:%u\n", pd.filename, pd.xmlp.errstr, pd.xmlp.line, pd.xmlp.column);
184 pd.solvable = solvable_free(pd.solvable, 1);
191 solv_xmlparser_free(&pd.xmlp);
192 join_freemem(&pd.jd);
193 if (flags & REPO_USE_ROOTDIR)
194 solv_free((char *)dirpath);
195 if (!(flags & REPO_NO_INTERNALIZE))
196 repodata_internalize(data);