small optimization for pool_addfileprovides
[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         repodata_extend(pd->data, s - pool->solvables);
165         pd->handle = s - pool->solvables;
166         if (type)
167           repodata_set_str(pd->data, pd->handle, PRODUCT_TYPE, type);
168       }
169       break;
170     case STATE_VERSION:
171       {
172         const char *ver = find_attr("ver", atts);
173         const char *rel = find_attr("rel", atts);
174         /* const char *epoch = find_attr("epoch", atts); ignored */
175         s->evr = makeevr(pd->pool, join2(&pd->jd, ver, "-", rel));
176       }
177       break;
178       /* <summary lang="xy">... */
179     case STATE_SUMMARY:
180       pd->tmplang = join_dup(&pd->jd, find_attr("lang", atts));
181       break;
182     default:
183       break;
184     }
185 }
186
187
188 static void XMLCALL
189 endElement(void *userData, const char *name)
190 {
191   struct parsedata *pd = userData;
192   Solvable *s = pd->solvable;
193
194 #if 0
195   fprintf(stderr, "end: [%d]%s\n", pd->state, name);
196 #endif
197   if (pd->depth != pd->statedepth)
198     {
199       pd->depth--;
200 #if 0
201       fprintf(stderr, "back from unknown %d %d %d\n", pd->state, pd->depth, pd->statedepth);
202 #endif
203       return;
204     }
205
206   pd->depth--;
207   pd->statedepth--;
208
209   switch (pd->state)
210     {
211     case STATE_PRODUCT:
212       if (!s->arch)
213         s->arch = ARCH_NOARCH;
214       if (!s->evr)
215         s->evr = ID_EMPTY;
216       if (s->name && s->arch != ARCH_SRC && s->arch != ARCH_NOSRC)
217         s->provides = repo_addid_dep(pd->repo, s->provides, pool_rel2id(pd->pool, s->name, s->evr, REL_EQ, 1), 0);
218       pd->solvable = 0;
219       break;
220     case STATE_NAME:
221       s->name = pool_str2id(pd->pool, join2(&pd->jd, "product", ":", pd->content), 1);
222       break;
223     case STATE_ARCH:
224       s->arch = pool_str2id(pd->pool, pd->content, 1);
225       break;
226     case STATE_SUMMARY:
227       repodata_set_str(pd->data, pd->handle, pool_id2langid(pd->pool, SOLVABLE_SUMMARY, pd->tmplang, 1), pd->content);
228       break;
229     case STATE_VENDOR:
230       s->vendor = pool_str2id(pd->pool, pd->content, 1);
231       break;
232     case STATE_INSTALLTIME:
233       repodata_set_num(pd->data, pd->handle, SOLVABLE_INSTALLTIME, atol(pd->content));
234     default:
235       break;
236     }
237
238   pd->state = pd->sbtab[pd->state];
239   pd->docontent = 0;
240
241 #if 0
242   fprintf(stderr, "end: [%s] -> %d\n", name, pd->state);
243 #endif
244 }
245
246
247 static void XMLCALL
248 characterData(void *userData, const XML_Char *s, int len)
249 {
250   struct parsedata *pd = userData;
251   int l;
252   char *c;
253   if (!pd->docontent)
254     return;
255   l = pd->lcontent + len + 1;
256   if (l > pd->acontent)
257     {
258       pd->content = realloc(pd->content, l + 256);
259       pd->acontent = l + 256;
260     }
261   c = pd->content + pd->lcontent;
262   pd->lcontent += len;
263   while (len-- > 0)
264     *c++ = *s++;
265   *c = 0;
266 }
267
268 #define BUFF_SIZE 8192
269
270
271 /*
272  * add single product to repo
273  *
274  */
275
276 static void
277 add_zyppdb_product(struct parsedata *pd, FILE *fp)
278 {
279   char buf[BUFF_SIZE];
280   int l;
281
282   XML_Parser parser = XML_ParserCreate(NULL);
283   XML_SetUserData(parser, pd);
284   XML_SetElementHandler(parser, startElement, endElement);
285   XML_SetCharacterDataHandler(parser, characterData);
286
287   for (;;)
288     {
289       l = fread(buf, 1, sizeof(buf), fp);
290       if (XML_Parse(parser, buf, l, l == 0) == XML_STATUS_ERROR)
291         {
292           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));
293           if (pd->solvable)
294             {
295               repo_free_solvable(pd->repo, pd->solvable - pd->pool->solvables, 1);
296               pd->solvable = 0;
297             }
298           return;
299         }
300       if (l == 0)
301         break;
302     }
303   XML_ParserFree(parser);
304 }
305
306
307 /*
308  * read all installed products
309  *
310  * parse each one as a product
311  */
312
313 int
314 repo_add_zyppdb_products(Repo *repo, const char *dirpath, int flags)
315 {
316   int i;
317   struct parsedata pd;
318   struct stateswitch *sw;
319   struct dirent *entry;
320   char *fullpath;
321   DIR *dir;
322   FILE *fp;
323   Repodata *data;
324
325   data = repo_add_repodata(repo, flags);
326   memset(&pd, 0, sizeof(pd));
327   pd.repo = repo;
328   pd.pool = repo->pool;
329   pd.data = data;
330
331   pd.content = malloc(256);
332   pd.acontent = 256;
333
334   for (i = 0, sw = stateswitches; sw->from != NUMSTATES; i++, sw++)
335     {
336       if (!pd.swtab[sw->from])
337         pd.swtab[sw->from] = sw;
338       pd.sbtab[sw->to] = sw->from;
339     }
340
341   if (flags & REPO_USE_ROOTDIR)
342     dirpath = pool_prepend_rootdir(repo->pool, dirpath);
343   dir = opendir(dirpath);
344   if (dir)
345     {
346       while ((entry = readdir(dir)))
347         {
348           if (strlen(entry->d_name) < 3)
349             continue;   /* skip '.' and '..' */
350           fullpath = join2(&pd.jd, dirpath, "/", entry->d_name);
351           if ((fp = fopen(fullpath, "r")) == 0)
352             {
353               pool_error(repo->pool, 0, "%s: %s", fullpath, strerror(errno));
354               continue;
355             }
356           add_zyppdb_product(&pd, fp);
357           fclose(fp);
358         }
359     }
360   closedir(dir);
361
362   free(pd.content);
363   join_freemem(&pd.jd);
364   if (flags & REPO_USE_ROOTDIR)
365     solv_free((char *)dirpath);
366   if (!(flags & REPO_NO_INTERNALIZE))
367     repodata_internalize(data);
368   return 0;
369 }
370
371 /* EOF */