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