refactor a bit, add some comments
[platform/upstream/libsolv.git] / ext / repo_products.c
1 /*
2  * repo_products.c
3  *
4  * Parses all files below 'proddir'
5  * See http://en.opensuse.org/Product_Management/Code11
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 <errno.h>
18 #include <limits.h>
19 #include <fcntl.h>
20 #include <ctype.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <assert.h>
25 #include <dirent.h>
26 #include <expat.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_content.h"
34 #include "repo_zyppdb.h"
35 #include "repo_products.h"
36 #include "repo_releasefile_products.h"
37
38
39 enum state {
40   STATE_START,
41   STATE_PRODUCT,
42   STATE_VENDOR,
43   STATE_NAME,
44   STATE_VERSION,
45   STATE_RELEASE,
46   STATE_ARCH,
47   STATE_SUMMARY,
48   STATE_SHORTSUMMARY,
49   STATE_DESCRIPTION,
50   STATE_UPDATEREPOKEY,
51   STATE_CPEID,
52   STATE_URLS,
53   STATE_URL,
54   STATE_RUNTIMECONFIG,
55   STATE_LINGUAS,
56   STATE_LANG,
57   STATE_REGISTER,
58   STATE_TARGET,
59   STATE_REGRELEASE,
60   STATE_PRODUCTLINE,
61   NUMSTATES
62 };
63
64 struct stateswitch {
65   enum state from;
66   char *ename;
67   enum state to;
68   int docontent;
69 };
70
71 /* !! must be sorted by first column !! */
72 static struct stateswitch stateswitches[] = {
73   { STATE_START,     "product",       STATE_PRODUCT,       0 },
74   { STATE_PRODUCT,   "vendor",        STATE_VENDOR,        1 },
75   { STATE_PRODUCT,   "name",          STATE_NAME,          1 },
76   { STATE_PRODUCT,   "version",       STATE_VERSION,       1 },
77   { STATE_PRODUCT,   "release",       STATE_RELEASE,       1 },
78   { STATE_PRODUCT,   "arch",          STATE_ARCH,          1 },
79   { STATE_PRODUCT,   "productline",   STATE_PRODUCTLINE,   1 },
80   { STATE_PRODUCT,   "summary",       STATE_SUMMARY,       1 },
81   { STATE_PRODUCT,   "shortsummary",  STATE_SHORTSUMMARY,  1 },
82   { STATE_PRODUCT,   "description",   STATE_DESCRIPTION,   1 },
83   { STATE_PRODUCT,   "register",      STATE_REGISTER,      0 },
84   { STATE_PRODUCT,   "urls",          STATE_URLS,          0 },
85   { STATE_PRODUCT,   "runtimeconfig", STATE_RUNTIMECONFIG, 0 },
86   { STATE_PRODUCT,   "linguas",       STATE_LINGUAS,       0 },
87   { STATE_PRODUCT,   "updaterepokey", STATE_UPDATEREPOKEY, 1 },
88   { STATE_PRODUCT,   "cpeid",         STATE_CPEID,         1 },
89   { STATE_URLS,      "url",           STATE_URL,           1 },
90   { STATE_LINGUAS,   "lang",          STATE_LANG,          0 },
91   { STATE_REGISTER,  "target",        STATE_TARGET,        1 },
92   { STATE_REGISTER,  "release",       STATE_REGRELEASE,    1 },
93   { NUMSTATES }
94 };
95
96 struct parsedata {
97   const char *filename;
98   const char *basename;
99   int depth;
100   enum state state;
101   int statedepth;
102   char *content;
103   int lcontent;
104   int acontent;
105   int docontent;
106   Pool *pool;
107   Repo *repo;
108   Repodata *data;
109
110   struct stateswitch *swtab[NUMSTATES];
111   enum state sbtab[NUMSTATES];
112   struct joindata jd;
113
114   const char *tmplang;
115
116   const char *tmpvers;
117   const char *tmprel;
118   Id urltype;
119
120   unsigned int ctime;
121
122   Solvable *solvable;
123   Id handle;
124
125   ino_t baseproduct;
126   ino_t currentproduct;
127   int productscheme;
128 };
129
130
131 /*
132  * find_attr
133  * find value for xml attribute
134  * I: txt, name of attribute
135  * I: atts, list of key/value attributes
136  * O: pointer to value of matching key, or NULL
137  *
138  */
139
140 static inline const char *
141 find_attr(const char *txt, const char **atts)
142 {
143   for (; *atts; atts += 2)
144     {
145       if (!strcmp(*atts, txt))
146         return atts[1];
147     }
148   return 0;
149 }
150
151
152 /*
153  * XML callback: startElement
154  */
155
156 static void XMLCALL
157 startElement(void *userData, const char *name, const char **atts)
158 {
159   struct parsedata *pd = userData;
160   Pool *pool = pd->pool;
161   Solvable *s = pd->solvable;
162   struct stateswitch *sw;
163
164 #if 0
165   fprintf(stderr, "start: [%d]%s\n", pd->state, name);
166 #endif
167   if (pd->depth != pd->statedepth)
168     {
169       pd->depth++;
170       return;
171     }
172
173   pd->depth++;
174   if (!pd->swtab[pd->state])    /* no statetable -> no substates */
175     {
176 #if 0
177       fprintf(stderr, "into unknown: %s (from: %d)\n", name, pd->state);
178 #endif
179       return;
180     }
181   for (sw = pd->swtab[pd->state]; sw->from == pd->state; sw++)  /* find name in statetable */
182     if (!strcmp(sw->ename, name))
183       break;
184
185   if (sw->from != pd->state)
186     {
187 #if 0
188       fprintf(stderr, "into unknown: %s (from: %d)\n", name, pd->state);
189 #endif
190       return;
191     }
192   pd->state = sw->to;
193   pd->docontent = sw->docontent;
194   pd->statedepth = pd->depth;
195   pd->lcontent = 0;
196   *pd->content = 0;
197
198   switch(pd->state)
199     {
200     case STATE_PRODUCT:
201       /* parse 'schemeversion' and store in global variable */
202       {
203         const char * scheme = find_attr("schemeversion", atts);
204         pd->productscheme = (scheme && *scheme) ? atoi(scheme) : -1;
205       }
206       if (!s)
207         {
208           s = pd->solvable = pool_id2solvable(pool, repo_add_solvable(pd->repo));
209           pd->handle = s - pool->solvables;
210         }
211       break;
212
213       /* <summary lang="xy">... */
214     case STATE_SUMMARY:
215     case STATE_DESCRIPTION:
216       pd->tmplang = join_dup(&pd->jd, find_attr("lang", atts));
217       break;
218     case STATE_URL:
219       pd->urltype = pool_str2id(pd->pool, find_attr("name", atts), 1);
220       break;
221     default:
222       break;
223     }
224 }
225
226
227 static void XMLCALL
228 endElement(void *userData, const char *name)
229 {
230   struct parsedata *pd = userData;
231   Solvable *s = pd->solvable;
232
233 #if 0
234   fprintf(stderr, "end: [%d]%s\n", pd->state, name);
235 #endif
236   if (pd->depth != pd->statedepth)
237     {
238       pd->depth--;
239 #if 0
240       fprintf(stderr, "back from unknown %d %d %d\n", pd->state, pd->depth, pd->statedepth);
241 #endif
242       return;
243     }
244
245   pd->depth--;
246   pd->statedepth--;
247
248   switch (pd->state)
249     {
250     case STATE_PRODUCT:
251       /* product done, finish solvable */
252       if (pd->ctime)
253         repodata_set_num(pd->data, pd->handle, SOLVABLE_INSTALLTIME, pd->ctime);
254
255       if (pd->basename)
256         repodata_set_str(pd->data, pd->handle, PRODUCT_REFERENCEFILE, pd->basename);
257
258       /* this is where <productsdir>/baseproduct points to */
259       if (pd->currentproduct == pd->baseproduct)
260         repodata_set_str(pd->data, pd->handle, PRODUCT_TYPE, "base");
261
262       if (pd->tmprel)
263         {
264           if (pd->tmpvers)
265             s->evr = makeevr(pd->pool, join2(&pd->jd, pd->tmpvers, "-", pd->tmprel));
266           else
267             {
268               fprintf(stderr, "Seen <release> but no <version>\n");
269             }
270         }
271       else if (pd->tmpvers)
272         s->evr = makeevr(pd->pool, pd->tmpvers); /* just version, no release */
273       pd->tmpvers = solv_free((void *)pd->tmpvers);
274       pd->tmprel = solv_free((void *)pd->tmprel);
275       if (!s->arch)
276         s->arch = ARCH_NOARCH;
277       if (!s->evr)
278         s->evr = ID_EMPTY;
279       if (s->name && s->arch != ARCH_SRC && s->arch != ARCH_NOSRC)
280         s->provides = repo_addid_dep(pd->repo, s->provides, pool_rel2id(pd->pool, s->name, s->evr, REL_EQ, 1), 0);
281       pd->solvable = 0;
282       break;
283     case STATE_VENDOR:
284       s->vendor = pool_str2id(pd->pool, pd->content, 1);
285       break;
286     case STATE_NAME:
287       s->name = pool_str2id(pd->pool, join2(&pd->jd, "product", ":", pd->content), 1);
288       break;
289     case STATE_VERSION:
290       pd->tmpvers = solv_strdup(pd->content);
291       break;
292     case STATE_RELEASE:
293       pd->tmprel = solv_strdup(pd->content);
294       break;
295     case STATE_ARCH:
296       s->arch = pool_str2id(pd->pool, pd->content, 1);
297       break;
298     case STATE_PRODUCTLINE:
299       repodata_set_str(pd->data, pd->handle, PRODUCT_PRODUCTLINE, pd->content);
300     break;
301     case STATE_UPDATEREPOKEY:
302       /** obsolete **/
303       break;
304     case STATE_SUMMARY:
305       repodata_set_str(pd->data, pd->handle, pool_id2langid(pd->pool, SOLVABLE_SUMMARY, pd->tmplang, 1), pd->content);
306       break;
307     case STATE_SHORTSUMMARY:
308       repodata_set_str(pd->data, pd->handle, PRODUCT_SHORTLABEL, pd->content);
309       break;
310     case STATE_DESCRIPTION:
311       repodata_set_str(pd->data, pd->handle, pool_id2langid(pd->pool, SOLVABLE_DESCRIPTION, pd->tmplang, 1), pd->content);
312       break;
313     case STATE_URL:
314       if (pd->urltype)
315         {
316           repodata_add_poolstr_array(pd->data, pd->handle, PRODUCT_URL, pd->content);
317           repodata_add_idarray(pd->data, pd->handle, PRODUCT_URL_TYPE, pd->urltype);
318         }
319       break;
320     case STATE_TARGET:
321       repodata_set_str(pd->data, pd->handle, PRODUCT_REGISTER_TARGET, pd->content);
322       break;
323     case STATE_REGRELEASE:
324       repodata_set_str(pd->data, pd->handle, PRODUCT_REGISTER_RELEASE, pd->content);
325       break;
326     case STATE_CPEID:
327       if (*pd->content)
328         repodata_set_str(pd->data, pd->handle, SOLVABLE_CPEID, pd->content);
329     default:
330       break;
331     }
332
333   pd->state = pd->sbtab[pd->state];
334   pd->docontent = 0;
335
336 #if 0
337       fprintf(stderr, "end: [%s] -> %d\n", name, pd->state);
338 #endif
339 }
340
341
342 static void XMLCALL
343 characterData(void *userData, const XML_Char *s, int len)
344 {
345   struct parsedata *pd = userData;
346   int l;
347   char *c;
348   if (!pd->docontent)
349     return;
350   l = pd->lcontent + len + 1;
351   if (l > pd->acontent)
352     {
353       pd->content = solv_realloc(pd->content, l + 256);
354       pd->acontent = l + 256;
355     }
356   c = pd->content + pd->lcontent;
357   pd->lcontent += len;
358   while (len-- > 0)
359     *c++ = *s++;
360   *c = 0;
361 }
362
363 #define BUFF_SIZE 8192
364
365
366 /*
367  * add single product to repo
368  *
369  */
370
371 static void
372 add_code11_product(struct parsedata *pd, FILE *fp)
373 {
374   char buf[BUFF_SIZE];
375   int l;
376   struct stat st;
377   XML_Parser parser;
378
379   if (!fstat(fileno(fp), &st))
380     {
381       pd->currentproduct = st.st_ino;
382       pd->ctime = (unsigned int)st.st_ctime;
383     }
384   else
385     {
386       pd->currentproduct = pd->baseproduct + 1; /* make it != baseproduct if stat fails */
387       pool_error(pd->pool, 0, "fstat: %s", strerror(errno));
388       pd->ctime = 0;
389     }
390
391   parser = XML_ParserCreate(NULL);
392   XML_SetUserData(parser, pd);
393   XML_SetElementHandler(parser, startElement, endElement);
394   XML_SetCharacterDataHandler(parser, characterData);
395
396   for (;;)
397     {
398       l = fread(buf, 1, sizeof(buf), fp);
399       if (XML_Parse(parser, buf, l, l == 0) == XML_STATUS_ERROR)
400         {
401           pool_debug(pd->pool, SOLV_ERROR, "%s: %s at line %u:%u\n", pd->filename, XML_ErrorString(XML_GetErrorCode(parser)), (unsigned int)XML_GetCurrentLineNumber(parser), (unsigned int)XML_GetCurrentColumnNumber(parser));
402           pool_debug(pd->pool, SOLV_ERROR, "skipping this product\n");
403           XML_ParserFree(parser);
404           return;
405         }
406       if (l == 0)
407         break;
408     }
409   XML_ParserFree(parser);
410 }
411
412
413 int
414 repo_add_code11_products(Repo *repo, const char *dirpath, int flags)
415 {
416   Repodata *data;
417   struct parsedata pd;
418   struct stateswitch *sw;
419   DIR *dir;
420   int i;
421
422   data = repo_add_repodata(repo, flags);
423
424   memset(&pd, 0, sizeof(pd));
425   pd.repo = repo;
426   pd.pool = repo->pool;
427   pd.data = data;
428
429   pd.content = solv_malloc(256);
430   pd.acontent = 256;
431
432   for (i = 0, sw = stateswitches; sw->from != NUMSTATES; i++, sw++)
433     {
434       if (!pd.swtab[sw->from])
435         pd.swtab[sw->from] = sw;
436       pd.sbtab[sw->to] = sw->from;
437     }
438
439   if (flags & REPO_USE_ROOTDIR)
440     dirpath = pool_prepend_rootdir(repo->pool, dirpath);
441   dir = opendir(dirpath);
442   if (dir)
443     {
444       struct dirent *entry;
445       struct stat st;
446       char *fullpath;
447
448       /* check for <productsdir>/baseproduct on code11 and remember its target inode */
449       if (stat(join2(&pd.jd, dirpath, "/", "baseproduct"), &st) == 0) /* follow symlink */
450         pd.baseproduct = st.st_ino;
451       else
452         pd.baseproduct = 0;
453
454       while ((entry = readdir(dir)))
455         {
456           int len = strlen(entry->d_name);
457           FILE *fp;
458           if (len <= 5 || strcmp(entry->d_name + len - 5, ".prod") != 0)
459             continue;
460           fullpath = join2(&pd.jd, dirpath, "/", entry->d_name);
461           fp = fopen(fullpath, "r");
462           if (!fp)
463             {
464               pool_error(repo->pool, 0, "%s: %s", fullpath, strerror(errno));
465               continue;
466             }
467           pd.filename = fullpath;
468           pd.basename = entry->d_name;
469           add_code11_product(&pd, fp);
470           fclose(fp);
471         }
472       closedir(dir);
473     }
474   solv_free(pd.content);
475   join_freemem(&pd.jd);
476   if (flags & REPO_USE_ROOTDIR)
477     solv_free((char *)dirpath);
478
479   if (!(flags & REPO_NO_INTERNALIZE))
480     repodata_internalize(data);
481   return 0;
482 }
483
484
485 /******************************************************************************************/
486
487
488 /*
489  * read all installed products
490  *
491  * try proddir (reading all .xml files from this directory) first
492  * if not available, assume non-code11 layout and parse /etc/xyz-release
493  *
494  * parse each one as a product
495  */
496
497 /* Oh joy! Three parsers for the price of one! */
498
499 int
500 repo_add_products(Repo *repo, const char *proddir, int flags)
501 {
502   const char *fullpath;
503   DIR *dir;
504
505   if (proddir)
506     {
507       dir = opendir(flags & REPO_USE_ROOTDIR ? pool_prepend_rootdir_tmp(repo->pool, proddir) : proddir);
508       if (dir)
509         {
510           /* assume code11 stype products */
511           closedir(dir);
512           return repo_add_code11_products(repo, proddir, flags);
513         }
514     }
515
516   /* code11 didn't work, try old code10 zyppdb */
517   fullpath = "/var/lib/zypp/db/products";
518   if (flags & REPO_USE_ROOTDIR)
519     fullpath = pool_prepend_rootdir_tmp(repo->pool, fullpath);
520   dir = opendir(fullpath);
521   if (dir)
522     {
523       closedir(dir);
524       /* assume code10 style products */
525       return repo_add_zyppdb_products(repo, "/var/lib/zypp/db/products", flags);
526     }
527
528   /* code10/11 didn't work, try -release files parsing */
529   fullpath = "/etc";
530   if (flags & REPO_USE_ROOTDIR)
531     fullpath = pool_prepend_rootdir_tmp(repo->pool, fullpath);
532   dir = opendir(fullpath);
533   if (dir)
534     {
535       closedir(dir);
536       return repo_add_releasefile_products(repo, "/etc", flags);
537     }
538
539   /* no luck. check if the rootdir exists */
540   fullpath = pool_get_rootdir(repo->pool);
541   if (fullpath && *fullpath)
542     {
543       dir = opendir(fullpath);
544       if (!dir)
545         return pool_error(repo->pool, -1, "%s: %s", fullpath, strerror(errno));
546       closedir(dir);
547     }
548
549   /* the least we can do... */
550   if (!(flags & REPO_NO_INTERNALIZE) && (flags & REPO_REUSE_REPODATA) != 0)
551     repodata_internalize(repo_last_repodata(repo));
552   return 0;
553 }
554
555 /* EOF */