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