5200c2930c278403e1f8ecb3daeeb714590357e8
[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 #include <errno.h>
27
28 #include "pool.h"
29 #include "repo.h"
30 #include "util.h"
31 #define DISABLE_SPLIT
32 #include "tools_util.h"
33 #include "repo_zyppdb.h"
34
35
36 enum state {
37   STATE_START,
38   STATE_PRODUCT,
39   STATE_NAME,
40   STATE_VERSION,
41   STATE_ARCH,
42   STATE_SUMMARY,
43   STATE_VENDOR,
44   STATE_INSTALLTIME,
45   NUMSTATES
46 };
47
48 struct stateswitch {
49   enum state from;
50   char *ename;
51   enum state to;
52   int docontent;
53 };
54
55 /* !! must be sorted by first column !! */
56 static struct stateswitch stateswitches[] = {
57   { STATE_START,     "product",       STATE_PRODUCT,       0 },
58   { STATE_PRODUCT,   "name",          STATE_NAME,          1 },
59   { STATE_PRODUCT,   "version",       STATE_VERSION,       0 },
60   { STATE_PRODUCT,   "arch",          STATE_ARCH,          1 },
61   { STATE_PRODUCT,   "summary",       STATE_SUMMARY,       1 },
62   { STATE_PRODUCT,   "install-time",  STATE_INSTALLTIME,   1 },
63   { STATE_PRODUCT,   "vendor",        STATE_VENDOR,        1 },
64   { NUMSTATES }
65 };
66
67 struct parsedata {
68   int depth;
69   enum state state;
70   int statedepth;
71   char *content;
72   int lcontent;
73   int acontent;
74   int docontent;
75   Pool *pool;
76   Repo *repo;
77   Repodata *data;
78
79   struct stateswitch *swtab[NUMSTATES];
80   enum state sbtab[NUMSTATES];
81   struct joindata jd;
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  * O: pointer to value of matching key, or NULL
96  *
97  */
98
99 static inline const char *
100 find_attr(const char *txt, const char **atts)
101 {
102   for (; *atts; atts += 2)
103     {
104       if (!strcmp(*atts, txt))
105         return atts[1];
106     }
107   return 0;
108 }
109
110
111 /*
112  * XML callback: startElement
113  */
114
115 static void XMLCALL
116 startElement(void *userData, const char *name, const char **atts)
117 {
118   struct parsedata *pd = userData;
119   Pool *pool = pd->pool;
120   Solvable *s = pd->solvable;
121   struct stateswitch *sw;
122
123 #if 0
124   fprintf(stderr, "start: [%d]%s\n", pd->state, name);
125 #endif
126   if (pd->depth != pd->statedepth)
127     {
128       pd->depth++;
129       return;
130     }
131
132   pd->depth++;
133   if (!pd->swtab[pd->state])    /* no statetable -> no substates */
134     {
135 #if 0
136       fprintf(stderr, "into unknown: %s (from: %d)\n", name, pd->state);
137 #endif
138       return;
139     }
140   for (sw = pd->swtab[pd->state]; sw->from == pd->state; sw++)  /* find name in statetable */
141     if (!strcmp(sw->ename, name))
142       break;
143
144   if (sw->from != pd->state)
145     {
146 #if 0
147       fprintf(stderr, "into unknown: %s (from: %d)\n", name, pd->state);
148 #endif
149       return;
150     }
151   pd->state = sw->to;
152   pd->docontent = sw->docontent;
153   pd->statedepth = pd->depth;
154   pd->lcontent = 0;
155   *pd->content = 0;
156
157   switch(pd->state)
158     {
159     case STATE_PRODUCT:
160       {
161         /* parse 'type' */
162         const char *type = find_attr("type", atts);
163         s = pd->solvable = pool_id2solvable(pool, repo_add_solvable(pd->repo));
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           if (pd->solvable)
293             {
294               repo_free_solvable(pd->repo, pd->solvable - pd->pool->solvables, 1);
295               pd->solvable = 0;
296             }
297           return;
298         }
299       if (l == 0)
300         break;
301     }
302   XML_ParserFree(parser);
303 }
304
305
306 /*
307  * read all installed products
308  *
309  * parse each one as a product
310  */
311
312 int
313 repo_add_zyppdb_products(Repo *repo, const char *dirpath, int flags)
314 {
315   int i;
316   struct parsedata pd;
317   struct stateswitch *sw;
318   struct dirent *entry;
319   char *fullpath;
320   DIR *dir;
321   FILE *fp;
322   Repodata *data;
323
324   data = repo_add_repodata(repo, flags);
325   memset(&pd, 0, sizeof(pd));
326   pd.repo = repo;
327   pd.pool = repo->pool;
328   pd.data = data;
329
330   pd.content = malloc(256);
331   pd.acontent = 256;
332
333   for (i = 0, sw = stateswitches; sw->from != NUMSTATES; i++, sw++)
334     {
335       if (!pd.swtab[sw->from])
336         pd.swtab[sw->from] = sw;
337       pd.sbtab[sw->to] = sw->from;
338     }
339
340   if (flags & REPO_USE_ROOTDIR)
341     dirpath = pool_prepend_rootdir(repo->pool, dirpath);
342   dir = opendir(dirpath);
343   if (dir)
344     {
345       while ((entry = readdir(dir)))
346         {
347           if (strlen(entry->d_name) < 3)
348             continue;   /* skip '.' and '..' */
349           fullpath = join2(&pd.jd, dirpath, "/", entry->d_name);
350           if ((fp = fopen(fullpath, "r")) == 0)
351             {
352               pool_error(repo->pool, 0, "%s: %s", fullpath, strerror(errno));
353               continue;
354             }
355           add_zyppdb_product(&pd, fp);
356           fclose(fp);
357         }
358     }
359   closedir(dir);
360
361   free(pd.content);
362   join_freemem(&pd.jd);
363   if (flags & REPO_USE_ROOTDIR)
364     solv_free((char *)dirpath);
365   if (!(flags & REPO_NO_INTERNALIZE))
366     repodata_internalize(data);
367   return 0;
368 }
369
370 /* EOF */