- use join2 for temp store of the language instead of strdup
[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   struct joindata jd;
83
84   const char *tmplang;
85
86   Solvable *solvable;
87   Id handle;
88 };
89
90
91 /*
92  * find_attr
93  * find value for xml attribute
94  * I: txt, name of attribute
95  * I: atts, list of key/value attributes
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)
102 {
103   for (; *atts; atts += 2)
104     {
105       if (!strcmp(*atts, txt))
106         return 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);
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);
174         const char *rel = find_attr("rel", atts);
175         /* const char *epoch = find_attr("epoch", atts); ignored */
176         s->evr = makeevr(pd->pool, join2(&pd->jd, ver, "-", rel));
177       }
178       break;
179       /* <summary lang="xy">... */
180     case STATE_SUMMARY:
181       {
182         const char *lang = find_attr("lang", atts);
183         pd->tmplang = lang ? join2(&pd->jd, lang, 0, 0) : 0;
184       }
185       break;
186     default:
187       break;
188     }
189 }
190
191
192 static void XMLCALL
193 endElement(void *userData, const char *name)
194 {
195   struct parsedata *pd = userData;
196   Solvable *s = pd->solvable;
197
198 #if 0
199   fprintf(stderr, "end: [%d]%s\n", pd->state, name);
200 #endif
201   if (pd->depth != pd->statedepth)
202     {
203       pd->depth--;
204 #if 0
205       fprintf(stderr, "back from unknown %d %d %d\n", pd->state, pd->depth, pd->statedepth);
206 #endif
207       return;
208     }
209
210   pd->depth--;
211   pd->statedepth--;
212
213   switch (pd->state)
214     {
215     case STATE_PRODUCT:
216       if (!s->arch)
217         s->arch = ARCH_NOARCH;
218       if (!s->evr)
219         s->evr = ID_EMPTY;
220       if (s->name && s->arch != ARCH_SRC && s->arch != ARCH_NOSRC)
221         s->provides = repo_addid_dep(pd->repo, s->provides, pool_rel2id(pd->pool, s->name, s->evr, REL_EQ, 1), 0);
222       pd->solvable = 0;
223       break;
224     case STATE_NAME:
225       s->name = pool_str2id(pd->pool, join2(&pd->jd, "product", ":", pd->content), 1);
226       break;
227     case STATE_ARCH:
228       s->arch = pool_str2id(pd->pool, pd->content, 1);
229       break;
230     case STATE_SUMMARY:
231       repodata_set_str(pd->data, pd->handle, pool_id2langid(pd->pool, SOLVABLE_SUMMARY, pd->tmplang, 1), pd->content);
232       break;
233     case STATE_VENDOR:
234       s->vendor = pool_str2id(pd->pool, pd->content, 1);
235       break;
236     case STATE_INSTALLTIME:
237       repodata_set_num(pd->data, pd->handle, SOLVABLE_INSTALLTIME, atol(pd->content));
238     default:
239       break;
240     }
241
242   pd->state = pd->sbtab[pd->state];
243   pd->docontent = 0;
244
245 #if 0
246   fprintf(stderr, "end: [%s] -> %d\n", name, pd->state);
247 #endif
248 }
249
250
251 static void XMLCALL
252 characterData(void *userData, const XML_Char *s, int len)
253 {
254   struct parsedata *pd = userData;
255   int l;
256   char *c;
257   if (!pd->docontent)
258     return;
259   l = pd->lcontent + len + 1;
260   if (l > pd->acontent)
261     {
262       pd->content = realloc(pd->content, l + 256);
263       pd->acontent = l + 256;
264     }
265   c = pd->content + pd->lcontent;
266   pd->lcontent += len;
267   while (len-- > 0)
268     *c++ = *s++;
269   *c = 0;
270 }
271
272 #define BUFF_SIZE 8192
273
274
275 /*
276  * add single product to repo
277  *
278  */
279
280 static void
281 add_zyppdb_product(struct parsedata *pd, FILE *fp)
282 {
283   char buf[BUFF_SIZE];
284   int l;
285
286   XML_Parser parser = XML_ParserCreate(NULL);
287   XML_SetUserData(parser, pd);
288   XML_SetElementHandler(parser, startElement, endElement);
289   XML_SetCharacterDataHandler(parser, characterData);
290
291   for (;;)
292     {
293       l = fread(buf, 1, sizeof(buf), fp);
294       if (XML_Parse(parser, buf, l, l == 0) == XML_STATUS_ERROR)
295         {
296           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));
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 void
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   dir = opendir(dirpath);
341   if (dir)
342     {
343       while ((entry = readdir(dir)))
344         {
345           if (strlen(entry->d_name) < 3)
346             continue;   /* skip '.' and '..' */
347           fullpath = join2(&pd.jd, dirpath, "/", entry->d_name);
348           if ((fp = fopen(fullpath, "r")) == 0)
349             {
350               perror(fullpath);
351               continue;
352             }
353           add_zyppdb_product(&pd, fp);
354           fclose(fp);
355         }
356     }
357   closedir(dir);
358
359   free(pd.content);
360   join_freemem(&pd.jd);
361   if (!(flags & REPO_NO_INTERNALIZE))
362     repodata_internalize(data);
363 }
364
365 /* EOF */