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