d73a900236dfc766b9ab40e66ffa0e0a3d1a657e
[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  * Copyright (c) 2008, Novell Inc.
8  *
9  * This program is licensed under the BSD license, read LICENSE.BSD
10  * for further information
11  */
12
13 #include <sys/types.h>
14 #include <sys/stat.h>
15 #include <unistd.h>
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <string.h>
19 #include <assert.h>
20 #include <dirent.h>
21 #include <errno.h>
22
23 #include "pool.h"
24 #include "repo.h"
25 #include "util.h"
26 #include "solv_xmlparser.h"
27 #define DISABLE_SPLIT
28 #include "tools_util.h"
29 #include "repo_zyppdb.h"
30
31
32 enum state {
33   STATE_START,
34   STATE_PRODUCT,
35   STATE_NAME,
36   STATE_VERSION,
37   STATE_ARCH,
38   STATE_SUMMARY,
39   STATE_VENDOR,
40   STATE_INSTALLTIME,
41   NUMSTATES
42 };
43
44 static struct solv_xmlparser_element stateswitches[] = {
45   { STATE_START,     "product",       STATE_PRODUCT,       0 },
46   { STATE_PRODUCT,   "name",          STATE_NAME,          1 },
47   { STATE_PRODUCT,   "version",       STATE_VERSION,       0 },
48   { STATE_PRODUCT,   "arch",          STATE_ARCH,          1 },
49   { STATE_PRODUCT,   "summary",       STATE_SUMMARY,       1 },
50   { STATE_PRODUCT,   "install-time",  STATE_INSTALLTIME,   1 },
51   { STATE_PRODUCT,   "vendor",        STATE_VENDOR,        1 },
52   { NUMSTATES }
53 };
54
55 struct parsedata {
56   Pool *pool;
57   Repo *repo;
58   Repodata *data;
59   const char *filename;
60   const char *tmplang;
61   Solvable *solvable;
62   Id handle;
63   struct solv_xmlparser xmlp;
64   struct joindata jd;
65 };
66
67
68
69 static void
70 startElement(struct solv_xmlparser *xmlp, int state, const char *name, const char **atts)
71 {
72   struct parsedata *pd = xmlp->userdata;
73   Pool *pool = pd->pool;
74   Solvable *s = pd->solvable;
75
76   switch(state)
77     {
78     case STATE_PRODUCT:
79       {
80         /* parse 'type' */
81         const char *type = solv_xmlparser_find_attr("type", atts);
82         s = pd->solvable = pool_id2solvable(pool, repo_add_solvable(pd->repo));
83         pd->handle = s - pool->solvables;
84         if (type)
85           repodata_set_str(pd->data, pd->handle, PRODUCT_TYPE, type);
86       }
87       break;
88     case STATE_VERSION:
89       {
90         const char *ver = solv_xmlparser_find_attr("ver", atts);
91         const char *rel = solv_xmlparser_find_attr("rel", atts);
92         /* const char *epoch = solv_xmlparser_find_attr("epoch", atts); ignored */
93         s->evr = makeevr(pd->pool, join2(&pd->jd, ver, "-", rel));
94       }
95       break;
96     case STATE_SUMMARY:         /* <summary lang="xy">... */
97       pd->tmplang = join_dup(&pd->jd, solv_xmlparser_find_attr("lang", atts));
98       break;
99     default:
100       break;
101     }
102 }
103
104
105 static void
106 endElement(struct solv_xmlparser *xmlp, int state, char *content)
107 {
108   struct parsedata *pd = xmlp->userdata;
109   Solvable *s = pd->solvable;
110
111   switch (state)
112     {
113     case STATE_PRODUCT:
114       if (!s->arch)
115         s->arch = ARCH_NOARCH;
116       if (!s->evr)
117         s->evr = ID_EMPTY;
118       if (s->name && s->arch != ARCH_SRC && s->arch != ARCH_NOSRC)
119         s->provides = repo_addid_dep(pd->repo, s->provides, pool_rel2id(pd->pool, s->name, s->evr, REL_EQ, 1), 0);
120       pd->solvable = 0;
121       break;
122     case STATE_NAME:
123       s->name = pool_str2id(pd->pool, join2(&pd->jd, "product", ":", content), 1);
124       break;
125     case STATE_ARCH:
126       s->arch = pool_str2id(pd->pool, content, 1);
127       break;
128     case STATE_SUMMARY:
129       repodata_set_str(pd->data, pd->handle, pool_id2langid(pd->pool, SOLVABLE_SUMMARY, pd->tmplang, 1), content);
130       break;
131     case STATE_VENDOR:
132       s->vendor = pool_str2id(pd->pool, content, 1);
133       break;
134     case STATE_INSTALLTIME:
135       repodata_set_num(pd->data, pd->handle, SOLVABLE_INSTALLTIME, atol(content));
136     default:
137       break;
138     }
139 }
140
141 static void
142 errorCallback(struct solv_xmlparser *xmlp, const char *errstr, unsigned int line, unsigned int column)
143 {
144   struct parsedata *pd = xmlp->userdata;
145   pool_debug(pd->pool, SOLV_ERROR, "repo_zyppdb: %s: %s at line %u:%u\n", pd->filename, errstr, line, column);
146   if (pd->solvable)
147     {
148       repo_free_solvable(pd->repo, pd->solvable - pd->pool->solvables, 1);
149       pd->solvable = 0;
150     }
151 }
152
153
154 /*
155  * read all installed products
156  *
157  * parse each one as a product
158  */
159
160 int
161 repo_add_zyppdb_products(Repo *repo, const char *dirpath, int flags)
162 {
163   struct parsedata pd;
164   struct dirent *entry;
165   char *fullpath;
166   DIR *dir;
167   FILE *fp;
168   Repodata *data;
169
170   data = repo_add_repodata(repo, flags);
171   memset(&pd, 0, sizeof(pd));
172   pd.repo = repo;
173   pd.pool = repo->pool;
174   pd.data = data;
175   solv_xmlparser_init(&pd.xmlp, stateswitches, &pd, startElement, endElement, errorCallback);
176
177   if (flags & REPO_USE_ROOTDIR)
178     dirpath = pool_prepend_rootdir(repo->pool, dirpath);
179   dir = opendir(dirpath);
180   if (dir)
181     {
182       while ((entry = readdir(dir)))
183         {
184           if (entry->d_name[0] == '.')
185             continue;   /* skip dot files */
186           fullpath = join2(&pd.jd, dirpath, "/", entry->d_name);
187           if ((fp = fopen(fullpath, "r")) == 0)
188             {
189               pool_error(repo->pool, 0, "%s: %s", fullpath, strerror(errno));
190               continue;
191             }
192           pd.filename = entry->d_name;
193           solv_xmlparser_parse(&pd.xmlp, fp);
194           fclose(fp);
195         }
196     }
197   closedir(dir);
198
199   solv_xmlparser_free(&pd.xmlp);
200   join_freemem(&pd.jd);
201   if (flags & REPO_USE_ROOTDIR)
202     solv_free((char *)dirpath);
203   if (!(flags & REPO_NO_INTERNALIZE))
204     repodata_internalize(data);
205   return 0;
206 }
207
208 /* EOF */