Imported Upstream version 0.7.2
[platform/upstream/libsolv.git] / ext / repo_zyppdb.c
1 /*
2  * repo_zyppdb.c
3  *
4  * Parses legacy /var/lib/zypp/db/products/... files.
5  * They are old (pre Code11) product descriptions. See bnc#429177
6  *
7  * Copyright (c) 2008, Novell Inc.
8  *
9  * This program is licensed under the BSD license, read LICENSE.BSD
10  * for further information
11  */
12
13 #include <sys/types.h>
14 #include <sys/stat.h>
15 #include <unistd.h>
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <string.h>
19 #include <assert.h>
20 #include <dirent.h>
21 #include <errno.h>
22
23 #include "pool.h"
24 #include "repo.h"
25 #include "util.h"
26 #include "solv_xmlparser.h"
27 #define DISABLE_SPLIT
28 #include "tools_util.h"
29 #include "repo_zyppdb.h"
30
31
32 enum state {
33   STATE_START,
34   STATE_PRODUCT,
35   STATE_NAME,
36   STATE_VERSION,
37   STATE_ARCH,
38   STATE_SUMMARY,
39   STATE_VENDOR,
40   STATE_INSTALLTIME,
41   NUMSTATES
42 };
43
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 },
52   { NUMSTATES }
53 };
54
55 struct parsedata {
56   Pool *pool;
57   Repo *repo;
58   Repodata *data;
59   const char *filename;
60   const char *tmplang;
61   Solvable *solvable;
62   Id handle;
63   struct solv_xmlparser xmlp;
64   struct joindata jd;
65 };
66
67
68
69 static void
70 startElement(struct solv_xmlparser *xmlp, int state, const char *name, const char **atts)
71 {
72   struct parsedata *pd = xmlp->userdata;
73   Pool *pool = pd->pool;
74   Solvable *s = pd->solvable;
75
76   switch(state)
77     {
78     case STATE_PRODUCT:
79       {
80         /* parse 'type' */
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;
84         if (type)
85           repodata_set_str(pd->data, pd->handle, PRODUCT_TYPE, type);
86       }
87       break;
88     case STATE_VERSION:
89       {
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));
94       }
95       break;
96     case STATE_SUMMARY:         /* <summary lang="xy">... */
97       pd->tmplang = join_dup(&pd->jd, solv_xmlparser_find_attr("lang", atts));
98       break;
99     default:
100       break;
101     }
102 }
103
104
105 static void
106 endElement(struct solv_xmlparser *xmlp, int state, char *content)
107 {
108   struct parsedata *pd = xmlp->userdata;
109   Solvable *s = pd->solvable;
110
111   switch (state)
112     {
113     case STATE_PRODUCT:
114       if (!s->arch)
115         s->arch = ARCH_NOARCH;
116       if (!s->evr)
117         s->evr = ID_EMPTY;
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);
120       pd->solvable = 0;
121       break;
122     case STATE_NAME:
123       s->name = pool_str2id(pd->pool, join2(&pd->jd, "product", ":", content), 1);
124       break;
125     case STATE_ARCH:
126       s->arch = pool_str2id(pd->pool, content, 1);
127       break;
128     case STATE_SUMMARY:
129       repodata_set_str(pd->data, pd->handle, pool_id2langid(pd->pool, SOLVABLE_SUMMARY, pd->tmplang, 1), content);
130       break;
131     case STATE_VENDOR:
132       s->vendor = pool_str2id(pd->pool, content, 1);
133       break;
134     case STATE_INSTALLTIME:
135       repodata_set_num(pd->data, pd->handle, SOLVABLE_INSTALLTIME, atol(content));
136     default:
137       break;
138     }
139 }
140
141
142 /*
143  * read all installed products
144  *
145  * parse each one as a product
146  */
147
148 int
149 repo_add_zyppdb_products(Repo *repo, const char *dirpath, int flags)
150 {
151   struct parsedata pd;
152   struct dirent *entry;
153   char *fullpath;
154   DIR *dir;
155   FILE *fp;
156   Repodata *data;
157
158   data = repo_add_repodata(repo, flags);
159   memset(&pd, 0, sizeof(pd));
160   pd.repo = repo;
161   pd.pool = repo->pool;
162   pd.data = data;
163   solv_xmlparser_init(&pd.xmlp, stateswitches, &pd, startElement, endElement);
164
165   if (flags & REPO_USE_ROOTDIR)
166     dirpath = pool_prepend_rootdir(repo->pool, dirpath);
167   dir = opendir(dirpath);
168   if (dir)
169     {
170       while ((entry = readdir(dir)))
171         {
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)
176             {
177               pool_error(repo->pool, 0, "%s: %s", fullpath, strerror(errno));
178               continue;
179             }
180           pd.filename = entry->d_name;
181           if (solv_xmlparser_parse(&pd.xmlp, fp) != SOLV_XMLPARSER_OK)
182             {
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);
185             }
186           fclose(fp);
187         }
188     }
189   closedir(dir);
190
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);
197   return 0;
198 }
199
200 /* EOF */