- also parse cudf provides ;)
[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  *
8  * Copyright (c) 2008, Novell Inc.
9  *
10  * This program is licensed under the BSD license, read LICENSE.BSD
11  * for further information
12  */
13
14 #include <sys/types.h>
15 #include <sys/stat.h>
16 #include <unistd.h>
17 #include <limits.h>
18 #include <fcntl.h>
19 #include <ctype.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <assert.h>
24 #include <dirent.h>
25 #include <expat.h>
26
27 #include "pool.h"
28 #include "repo.h"
29 #include "util.h"
30 #define DISABLE_SPLIT
31 #include "tools_util.h"
32 #include "repo_zyppdb.h"
33
34
35 enum state {
36   STATE_START,
37   STATE_PRODUCT,
38   STATE_NAME,
39   STATE_VERSION,
40   STATE_ARCH,
41   STATE_SUMMARY,
42   STATE_VENDOR,
43   STATE_INSTALLTIME,
44   NUMSTATES
45 };
46
47 struct stateswitch {
48   enum state from;
49   char *ename;
50   enum state to;
51   int docontent;
52 };
53
54 /* !! must be sorted by first column !! */
55 static struct stateswitch stateswitches[] = {
56   { STATE_START,     "product",       STATE_PRODUCT,       0 },
57   { STATE_PRODUCT,   "name",          STATE_NAME,          1 },
58   { STATE_PRODUCT,   "version",       STATE_VERSION,       0 },
59   { STATE_PRODUCT,   "arch",          STATE_ARCH,          1 },
60   { STATE_PRODUCT,   "summary",       STATE_SUMMARY,       1 },
61   { STATE_PRODUCT,   "install-time",  STATE_INSTALLTIME,   1 },
62   { STATE_PRODUCT,   "vendor",        STATE_VENDOR,        1 },
63   { NUMSTATES }
64 };
65
66 struct parsedata {
67   int depth;
68   enum state state;
69   int statedepth;
70   char *content;
71   int lcontent;
72   int acontent;
73   int docontent;
74   Pool *pool;
75   Repo *repo;
76   Repodata *data;
77
78   struct stateswitch *swtab[NUMSTATES];
79   enum state sbtab[NUMSTATES];
80   struct joindata jd;
81
82   const char *tmplang;
83
84   Solvable *solvable;
85   Id handle;
86 };
87
88
89 /*
90  * find_attr
91  * find value for xml attribute
92  * I: txt, name of attribute
93  * I: atts, list of key/value attributes
94  * O: pointer to value of matching key, or NULL
95  *
96  */
97
98 static inline const char *
99 find_attr(const char *txt, const char **atts)
100 {
101   for (; *atts; atts += 2)
102     {
103       if (!strcmp(*atts, txt))
104         return atts[1];
105     }
106   return 0;
107 }
108
109
110 /*
111  * XML callback: startElement
112  */
113
114 static void XMLCALL
115 startElement(void *userData, const char *name, const char **atts)
116 {
117   struct parsedata *pd = userData;
118   Pool *pool = pd->pool;
119   Solvable *s = pd->solvable;
120   struct stateswitch *sw;
121
122 #if 0
123   fprintf(stderr, "start: [%d]%s\n", pd->state, name);
124 #endif
125   if (pd->depth != pd->statedepth)
126     {
127       pd->depth++;
128       return;
129     }
130
131   pd->depth++;
132   if (!pd->swtab[pd->state])    /* no statetable -> no substates */
133     {
134 #if 0
135       fprintf(stderr, "into unknown: %s (from: %d)\n", name, pd->state);
136 #endif
137       return;
138     }
139   for (sw = pd->swtab[pd->state]; sw->from == pd->state; sw++)  /* find name in statetable */
140     if (!strcmp(sw->ename, name))
141       break;
142
143   if (sw->from != pd->state)
144     {
145 #if 0
146       fprintf(stderr, "into unknown: %s (from: %d)\n", name, pd->state);
147 #endif
148       return;
149     }
150   pd->state = sw->to;
151   pd->docontent = sw->docontent;
152   pd->statedepth = pd->depth;
153   pd->lcontent = 0;
154   *pd->content = 0;
155
156   switch(pd->state)
157     {
158     case STATE_PRODUCT:
159       {
160         /* parse 'type' */
161         const char *type = find_attr("type", atts);
162         s = pd->solvable = pool_id2solvable(pool, repo_add_solvable(pd->repo));
163         repodata_extend(pd->data, s - pool->solvables);
164         pd->handle = s - pool->solvables;
165         if (type)
166           repodata_set_str(pd->data, pd->handle, PRODUCT_TYPE, type);
167       }
168       break;
169     case STATE_VERSION:
170       {
171         const char *ver = find_attr("ver", atts);
172         const char *rel = find_attr("rel", atts);
173         /* const char *epoch = find_attr("epoch", atts); ignored */
174         s->evr = makeevr(pd->pool, join2(&pd->jd, ver, "-", rel));
175       }
176       break;
177       /* <summary lang="xy">... */
178     case STATE_SUMMARY:
179       pd->tmplang = join_dup(&pd->jd, find_attr("lang", atts));
180       break;
181     default:
182       break;
183     }
184 }
185
186
187 static void XMLCALL
188 endElement(void *userData, const char *name)
189 {
190   struct parsedata *pd = userData;
191   Solvable *s = pd->solvable;
192
193 #if 0
194   fprintf(stderr, "end: [%d]%s\n", pd->state, name);
195 #endif
196   if (pd->depth != pd->statedepth)
197     {
198       pd->depth--;
199 #if 0
200       fprintf(stderr, "back from unknown %d %d %d\n", pd->state, pd->depth, pd->statedepth);
201 #endif
202       return;
203     }
204
205   pd->depth--;
206   pd->statedepth--;
207
208   switch (pd->state)
209     {
210     case STATE_PRODUCT:
211       if (!s->arch)
212         s->arch = ARCH_NOARCH;
213       if (!s->evr)
214         s->evr = ID_EMPTY;
215       if (s->name && s->arch != ARCH_SRC && s->arch != ARCH_NOSRC)
216         s->provides = repo_addid_dep(pd->repo, s->provides, pool_rel2id(pd->pool, s->name, s->evr, REL_EQ, 1), 0);
217       pd->solvable = 0;
218       break;
219     case STATE_NAME:
220       s->name = pool_str2id(pd->pool, join2(&pd->jd, "product", ":", pd->content), 1);
221       break;
222     case STATE_ARCH:
223       s->arch = pool_str2id(pd->pool, pd->content, 1);
224       break;
225     case STATE_SUMMARY:
226       repodata_set_str(pd->data, pd->handle, pool_id2langid(pd->pool, SOLVABLE_SUMMARY, pd->tmplang, 1), pd->content);
227       break;
228     case STATE_VENDOR:
229       s->vendor = pool_str2id(pd->pool, pd->content, 1);
230       break;
231     case STATE_INSTALLTIME:
232       repodata_set_num(pd->data, pd->handle, SOLVABLE_INSTALLTIME, atol(pd->content));
233     default:
234       break;
235     }
236
237   pd->state = pd->sbtab[pd->state];
238   pd->docontent = 0;
239
240 #if 0
241   fprintf(stderr, "end: [%s] -> %d\n", name, pd->state);
242 #endif
243 }
244
245
246 static void XMLCALL
247 characterData(void *userData, const XML_Char *s, int len)
248 {
249   struct parsedata *pd = userData;
250   int l;
251   char *c;
252   if (!pd->docontent)
253     return;
254   l = pd->lcontent + len + 1;
255   if (l > pd->acontent)
256     {
257       pd->content = realloc(pd->content, l + 256);
258       pd->acontent = l + 256;
259     }
260   c = pd->content + pd->lcontent;
261   pd->lcontent += len;
262   while (len-- > 0)
263     *c++ = *s++;
264   *c = 0;
265 }
266
267 #define BUFF_SIZE 8192
268
269
270 /*
271  * add single product to repo
272  *
273  */
274
275 static void
276 add_zyppdb_product(struct parsedata *pd, FILE *fp)
277 {
278   char buf[BUFF_SIZE];
279   int l;
280
281   XML_Parser parser = XML_ParserCreate(NULL);
282   XML_SetUserData(parser, pd);
283   XML_SetElementHandler(parser, startElement, endElement);
284   XML_SetCharacterDataHandler(parser, characterData);
285
286   for (;;)
287     {
288       l = fread(buf, 1, sizeof(buf), fp);
289       if (XML_Parse(parser, buf, l, l == 0) == XML_STATUS_ERROR)
290         {
291           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));
292           return;
293         }
294       if (l == 0)
295         break;
296     }
297   XML_ParserFree(parser);
298 }
299
300
301 /*
302  * read all installed products
303  *
304  * parse each one as a product
305  */
306
307 int
308 repo_add_zyppdb_products(Repo *repo, const char *dirpath, int flags)
309 {
310   int i;
311   struct parsedata pd;
312   struct stateswitch *sw;
313   struct dirent *entry;
314   char *fullpath;
315   DIR *dir;
316   FILE *fp;
317   Repodata *data;
318   
319   data = repo_add_repodata(repo, flags);
320   memset(&pd, 0, sizeof(pd));
321   pd.repo = repo;
322   pd.pool = repo->pool;
323   pd.data = data;
324
325   pd.content = malloc(256);
326   pd.acontent = 256;
327
328   for (i = 0, sw = stateswitches; sw->from != NUMSTATES; i++, sw++)
329     {
330       if (!pd.swtab[sw->from])
331         pd.swtab[sw->from] = sw;
332       pd.sbtab[sw->to] = sw->from;
333     }
334
335   dir = opendir(dirpath);
336   if (dir)
337     {
338       while ((entry = readdir(dir)))
339         {
340           if (strlen(entry->d_name) < 3)
341             continue;   /* skip '.' and '..' */
342           fullpath = join2(&pd.jd, dirpath, "/", entry->d_name);
343           if ((fp = fopen(fullpath, "r")) == 0)
344             {
345               perror(fullpath);
346               continue;
347             }
348           add_zyppdb_product(&pd, fp);
349           fclose(fp);
350         }
351     }
352   closedir(dir);
353
354   free(pd.content);
355   join_freemem(&pd.jd);
356   if (!(flags & REPO_NO_INTERNALIZE))
357     repodata_internalize(data);
358   return 0;
359 }
360
361 /* EOF */