acff8cd57e666cc66aeccde391ddd530887fb7ab
[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 <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 #include "repo_zyppdb.h"
34 #include "repo_releasefile_products.h"
35
36
37 //#define DUMPOUT 0
38
39 enum state {
40   STATE_START,           // 0
41   STATE_PRODUCT,         // 1
42   STATE_VENDOR,          // 2
43   STATE_NAME,            // 3
44   STATE_VERSION,         // 4
45   STATE_RELEASE,         // 5
46   STATE_ARCH,            // 6
47   STATE_SUMMARY,         // 7
48   STATE_SHORTSUMMARY,
49   STATE_DESCRIPTION,     // 8
50   STATE_UPDATEREPOKEY,   // 9 should go away
51   STATE_CPEID,         // 9
52   STATE_URLS,            // 10
53   STATE_URL,             // 11
54   STATE_RUNTIMECONFIG,   // 12
55   STATE_LINGUAS,         // 13
56   STATE_LANG,            // 14
57   STATE_REGISTER,        // 15
58   STATE_TARGET,          // 16
59   STATE_REGRELEASE,      // 18
60   STATE_PRODUCTLINE,     // 19
61   NUMSTATES              // 0
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       {
217         const char *lang = find_attr("lang", atts);
218         pd->tmplang = lang ? join2(&pd->jd, lang, 0, 0) : 0;
219         break;
220       }
221     case STATE_URL:
222       pd->urltype = pool_str2id(pd->pool, find_attr("name", atts), 1);
223       break;
224     default:
225       break;
226     }
227 }
228
229
230 static void XMLCALL
231 endElement(void *userData, const char *name)
232 {
233   struct parsedata *pd = userData;
234   Solvable *s = pd->solvable;
235
236 #if 0
237       fprintf(stderr, "end: [%d]%s\n", pd->state, name);
238 #endif
239   if (pd->depth != pd->statedepth)
240     {
241       pd->depth--;
242 #if 0
243       fprintf(stderr, "back from unknown %d %d %d\n", pd->state, pd->depth, pd->statedepth);
244 #endif
245       return;
246     }
247
248   pd->depth--;
249   pd->statedepth--;
250
251   switch (pd->state)
252     {
253     case STATE_PRODUCT:
254       /* product done, finish solvable */
255       if (pd->ctime)
256         repodata_set_num(pd->data, pd->handle, SOLVABLE_INSTALLTIME, pd->ctime);
257
258       if (pd->basename)
259         repodata_set_str(pd->data, pd->handle, PRODUCT_REFERENCEFILE, pd->basename);
260
261       /* this is where <productsdir>/baseproduct points to */
262       if (pd->currentproduct == pd->baseproduct)
263         repodata_set_str(pd->data, pd->handle, PRODUCT_TYPE, "base");
264
265       if (pd->tmprel)
266         {
267           if (pd->tmpvers)
268             s->evr = makeevr(pd->pool, join2(&pd->jd, pd->tmpvers, "-", pd->tmprel));
269           else
270             {
271               fprintf(stderr, "Seen <release> but no <version>\n");
272             }
273         }
274       else if (pd->tmpvers)
275         s->evr = makeevr(pd->pool, pd->tmpvers); /* just version, no release */
276       pd->tmpvers = solv_free((void *)pd->tmpvers);
277       pd->tmprel = solv_free((void *)pd->tmprel);
278       if (!s->arch)
279         s->arch = ARCH_NOARCH;
280       if (!s->evr)
281         s->evr = ID_EMPTY;
282       if (s->name && s->arch != ARCH_SRC && s->arch != ARCH_NOSRC)
283         s->provides = repo_addid_dep(pd->repo, s->provides, pool_rel2id(pd->pool, s->name, s->evr, REL_EQ, 1), 0);
284       pd->solvable = 0;
285       break;
286     case STATE_VENDOR:
287       s->vendor = pool_str2id(pd->pool, pd->content, 1);
288       break;
289     case STATE_NAME:
290       s->name = pool_str2id(pd->pool, join2(&pd->jd, "product", ":", pd->content), 1);
291       break;
292     case STATE_VERSION:
293       pd->tmpvers = solv_strdup(pd->content);
294       break;
295     case STATE_RELEASE:
296       pd->tmprel = solv_strdup(pd->content);
297       break;
298     case STATE_ARCH:
299       s->arch = pool_str2id(pd->pool, pd->content, 1);
300       break;
301     case STATE_PRODUCTLINE:
302       repodata_set_str(pd->data, pd->handle, PRODUCT_PRODUCTLINE, pd->content);
303     break;
304     case STATE_UPDATEREPOKEY:
305       /** obsolete **/
306       break;
307     case STATE_SUMMARY:
308       repodata_set_str(pd->data, pd->handle, pool_id2langid(pd->pool, SOLVABLE_SUMMARY, pd->tmplang, 1), pd->content);
309       break;
310     case STATE_SHORTSUMMARY:
311       repodata_set_str(pd->data, pd->handle, PRODUCT_SHORTLABEL, pd->content);
312       break;
313     case STATE_DESCRIPTION:
314       repodata_set_str(pd->data, pd->handle, pool_id2langid(pd->pool, SOLVABLE_DESCRIPTION, pd->tmplang, 1), pd->content);
315       break;
316     case STATE_URL:
317       if (pd->urltype)
318         {
319           repodata_add_poolstr_array(pd->data, pd->handle, PRODUCT_URL, pd->content);
320           repodata_add_idarray(pd->data, pd->handle, PRODUCT_URL_TYPE, pd->urltype);
321         }
322       break;
323     case STATE_TARGET:
324       repodata_set_str(pd->data, pd->handle, PRODUCT_REGISTER_TARGET, pd->content);
325       break;
326     case STATE_REGRELEASE:
327       repodata_set_str(pd->data, pd->handle, PRODUCT_REGISTER_RELEASE, pd->content);
328       break;
329     case STATE_CPEID:
330       if (pd->content)
331         repodata_set_str(pd->data, pd->handle, SOLVABLE_CPEID, pd->content);
332     default:
333       break;
334     }
335
336   pd->state = pd->sbtab[pd->state];
337   pd->docontent = 0;
338
339 #if 0
340       fprintf(stderr, "end: [%s] -> %d\n", name, pd->state);
341 #endif
342 }
343
344
345 static void XMLCALL
346 characterData(void *userData, const XML_Char *s, int len)
347 {
348   struct parsedata *pd = userData;
349   int l;
350   char *c;
351   if (!pd->docontent)
352     return;
353   l = pd->lcontent + len + 1;
354   if (l > pd->acontent)
355     {
356       pd->content = solv_realloc(pd->content, l + 256);
357       pd->acontent = l + 256;
358     }
359   c = pd->content + pd->lcontent;
360   pd->lcontent += len;
361   while (len-- > 0)
362     *c++ = *s++;
363   *c = 0;
364 }
365
366 #define BUFF_SIZE 8192
367
368
369 /*
370  * add single product to repo
371  *
372  */
373
374 static void
375 add_code11_product(struct parsedata *pd, FILE *fp)
376 {
377   char buf[BUFF_SIZE];
378   int l;
379   struct stat st;
380   XML_Parser parser;
381
382   if (!fstat(fileno(fp), &st))
383     {
384       pd->currentproduct = st.st_ino;
385       pd->ctime = (unsigned int)st.st_ctime;
386     }
387   else
388     {
389       pd->currentproduct = pd->baseproduct + 1; /* make it != baseproduct if stat fails */
390       perror("fstat");
391       pd->ctime = 0;
392     }
393
394   parser = XML_ParserCreate(NULL);
395   XML_SetUserData(parser, pd);
396   XML_SetElementHandler(parser, startElement, endElement);
397   XML_SetCharacterDataHandler(parser, characterData);
398
399   for (;;)
400     {
401       l = fread(buf, 1, sizeof(buf), fp);
402       if (XML_Parse(parser, buf, l, l == 0) == XML_STATUS_ERROR)
403         {
404           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));
405           pool_debug(pd->pool, SOLV_ERROR, "skipping this product\n");
406           XML_ParserFree(parser);
407           return;
408         }
409       if (l == 0)
410         break;
411     }
412   XML_ParserFree(parser);
413 }
414
415
416 void
417 repo_add_code11_products(Repo *repo, const char *dirpath, int flags)
418 {
419   Repodata *data;
420   struct parsedata pd;
421   struct stateswitch *sw;
422   DIR *dir;
423   int i;
424
425   data = repo_add_repodata(repo, flags);
426
427   memset(&pd, 0, sizeof(pd));
428   pd.repo = repo;
429   pd.pool = repo->pool;
430   pd.data = data;
431
432   pd.content = solv_malloc(256);
433   pd.acontent = 256;
434
435   for (i = 0, sw = stateswitches; sw->from != NUMSTATES; i++, sw++)
436     {
437       if (!pd.swtab[sw->from])
438         pd.swtab[sw->from] = sw;
439       pd.sbtab[sw->to] = sw->from;
440     }
441
442   dir = opendir(dirpath);
443   if (dir)
444     {
445       struct dirent *entry;
446       struct stat st;
447       char *fullpath;
448
449       /* check for <productsdir>/baseproduct on code11 and remember its target inode */
450       if (stat(join2(&pd.jd, dirpath, "/", "baseproduct"), &st) == 0) /* follow symlink */
451         pd.baseproduct = st.st_ino;
452       else
453         pd.baseproduct = 0;
454
455       while ((entry = readdir(dir)))
456         {
457           int len = strlen(entry->d_name);
458           FILE *fp;
459           if (len <= 5 || strcmp(entry->d_name + len - 5, ".prod") != 0)
460             continue;
461           fullpath = join2(&pd.jd, dirpath, "/", entry->d_name);
462           fp = fopen(fullpath, "r");
463           if (!fp)
464             {
465               perror(fullpath);
466               continue;
467             }
468           pd.filename = fullpath;
469           pd.basename = entry->d_name;
470           add_code11_product(&pd, fp);
471           fclose(fp);
472         }
473       closedir(dir);
474     }
475   solv_free(pd.content);
476   join_freemem(&pd.jd);
477
478   if (!(flags & REPO_NO_INTERNALIZE))
479     repodata_internalize(data);
480 }
481
482
483 /******************************************************************************************/
484
485
486 /*
487  * read all installed products
488  *
489  * try proddir (reading all .xml files from this directory) first
490  * if not available, assume non-code11 layout and parse /etc/xyz-release
491  *
492  * parse each one as a product
493  */
494
495 /* Oh joy! Three parsers for the price of one! */
496
497 void
498 repo_add_products(Repo *repo, const char *proddir, const char *root, int flags)
499 {
500   char *fullpath;
501   DIR *dir;
502
503   if (proddir)
504     {
505       dir = opendir(proddir);
506       if (dir)
507         {
508           /* assume code11 stype products */
509           closedir(dir);
510           repo_add_code11_products(repo, proddir, flags);
511           return;
512         }
513     }
514
515   /* code11 didn't work, try old code10 zyppdb */
516   fullpath = solv_dupjoin(root ? root : 0, "/var/lib/zypp/db/products", 0);
517   dir = opendir(fullpath);
518   if (dir)
519     {
520       closedir(dir);
521       /* assume code10 style products */
522       repo_add_zyppdb_products(repo, fullpath, flags);
523       solv_free(fullpath);
524       return;
525     }
526   solv_free(fullpath);
527
528   /* code10/11 didn't work, try -release files parsing */
529   fullpath = solv_dupjoin(root ? root : 0, "/etc", 0);
530   dir = opendir(fullpath);
531   if (dir)
532     {
533       closedir(dir);
534       repo_add_releasefile_products(repo, fullpath, flags);
535       solv_free(fullpath);
536       return;
537     }
538
539   /* no luck. print an error message in case the root argument is wrong */
540   perror(fullpath);
541   solv_free(fullpath);
542
543   /* the least we can do... */
544   if (!(flags & REPO_NO_INTERNALIZE) && (flags & REPO_REUSE_REPODATA) != 0)
545     repodata_internalize(repo_last_repodata(repo));
546 }
547
548 /* EOF */