e511872b81384881d7ceb597147be502575b2be5
[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       pd->tmplang = join_dup(&pd->jd, find_attr("lang", atts));
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(&pd->jd, "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       break;
230     case STATE_VENDOR:
231       s->vendor = pool_str2id(pd->pool, pd->content, 1);
232       break;
233     case STATE_INSTALLTIME:
234       repodata_set_num(pd->data, pd->handle, SOLVABLE_INSTALLTIME, atol(pd->content));
235     default:
236       break;
237     }
238
239   pd->state = pd->sbtab[pd->state];
240   pd->docontent = 0;
241
242 #if 0
243   fprintf(stderr, "end: [%s] -> %d\n", name, pd->state);
244 #endif
245 }
246
247
248 static void XMLCALL
249 characterData(void *userData, const XML_Char *s, int len)
250 {
251   struct parsedata *pd = userData;
252   int l;
253   char *c;
254   if (!pd->docontent)
255     return;
256   l = pd->lcontent + len + 1;
257   if (l > pd->acontent)
258     {
259       pd->content = realloc(pd->content, l + 256);
260       pd->acontent = l + 256;
261     }
262   c = pd->content + pd->lcontent;
263   pd->lcontent += len;
264   while (len-- > 0)
265     *c++ = *s++;
266   *c = 0;
267 }
268
269 #define BUFF_SIZE 8192
270
271
272 /*
273  * add single product to repo
274  *
275  */
276
277 static void
278 add_zyppdb_product(struct parsedata *pd, FILE *fp)
279 {
280   char buf[BUFF_SIZE];
281   int l;
282
283   XML_Parser parser = XML_ParserCreate(NULL);
284   XML_SetUserData(parser, pd);
285   XML_SetElementHandler(parser, startElement, endElement);
286   XML_SetCharacterDataHandler(parser, characterData);
287
288   for (;;)
289     {
290       l = fread(buf, 1, sizeof(buf), fp);
291       if (XML_Parse(parser, buf, l, l == 0) == XML_STATUS_ERROR)
292         {
293           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));
294           return;
295         }
296       if (l == 0)
297         break;
298     }
299   XML_ParserFree(parser);
300 }
301
302
303 /*
304  * read all installed products
305  *
306  * parse each one as a product
307  */
308
309 void
310 repo_add_zyppdb_products(Repo *repo, const char *dirpath, int flags)
311 {
312   int i;
313   struct parsedata pd;
314   struct stateswitch *sw;
315   struct dirent *entry;
316   char *fullpath;
317   DIR *dir;
318   FILE *fp;
319   Repodata *data;
320   
321   data = repo_add_repodata(repo, flags);
322   memset(&pd, 0, sizeof(pd));
323   pd.repo = repo;
324   pd.pool = repo->pool;
325   pd.data = data;
326
327   pd.content = malloc(256);
328   pd.acontent = 256;
329
330   for (i = 0, sw = stateswitches; sw->from != NUMSTATES; i++, sw++)
331     {
332       if (!pd.swtab[sw->from])
333         pd.swtab[sw->from] = sw;
334       pd.sbtab[sw->to] = sw->from;
335     }
336
337   dir = opendir(dirpath);
338   if (dir)
339     {
340       while ((entry = readdir(dir)))
341         {
342           if (strlen(entry->d_name) < 3)
343             continue;   /* skip '.' and '..' */
344           fullpath = join2(&pd.jd, dirpath, "/", entry->d_name);
345           if ((fp = fopen(fullpath, "r")) == 0)
346             {
347               perror(fullpath);
348               continue;
349             }
350           add_zyppdb_product(&pd, fp);
351           fclose(fp);
352         }
353     }
354   closedir(dir);
355
356   free(pd.content);
357   join_freemem(&pd.jd);
358   if (!(flags & REPO_NO_INTERNALIZE))
359     repodata_internalize(data);
360 }
361
362 /* EOF */