- also disable update rules for "keep installed" jobs.
[platform/upstream/libsolv.git] / tools / 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
35
36 //#define DUMPOUT 0
37
38 enum state {
39   STATE_START,           // 0
40   STATE_PRODUCT,         // 1
41   STATE_VENDOR,          // 2
42   STATE_NAME,            // 3
43   STATE_VERSION,         // 4
44   STATE_RELEASE,         // 5
45   STATE_ARCH,            // 6
46   STATE_SUMMARY,         // 7
47   STATE_DESCRIPTION,     // 8
48   STATE_UPDATEREPOKEY,   // 9 should go away
49   STATE_CPEID,         // 9
50   STATE_URLS,            // 10
51   STATE_URL,             // 11
52   STATE_RUNTIMECONFIG,   // 12
53   STATE_LINGUAS,         // 13
54   STATE_LANG,            // 14
55   STATE_REGISTER,        // 15
56   STATE_TARGET,          // 16
57   STATE_REGRELEASE,      // 18
58   STATE_PRODUCTLINE,     // 19
59   NUMSTATES              // 0
60 };
61
62 struct stateswitch {
63   enum state from;
64   char *ename;
65   enum state to;
66   int docontent;
67 };
68
69 /* !! must be sorted by first column !! */
70 static struct stateswitch stateswitches[] = {
71   { STATE_START,     "product",       STATE_PRODUCT,       0 },
72   { STATE_PRODUCT,   "vendor",        STATE_VENDOR,        1 },
73   { STATE_PRODUCT,   "name",          STATE_NAME,          1 },
74   { STATE_PRODUCT,   "version",       STATE_VERSION,       1 },
75   { STATE_PRODUCT,   "release",       STATE_RELEASE,       1 },
76   { STATE_PRODUCT,   "arch",          STATE_ARCH,          1 },
77   { STATE_PRODUCT,   "productline",   STATE_PRODUCTLINE,   1 },
78   { STATE_PRODUCT,   "summary",       STATE_SUMMARY,       1 },
79   { STATE_PRODUCT,   "description",   STATE_DESCRIPTION,   1 },
80   { STATE_PRODUCT,   "register",      STATE_REGISTER,      0 },
81   { STATE_PRODUCT,   "urls",          STATE_URLS,          0 },
82   { STATE_PRODUCT,   "runtimeconfig", STATE_RUNTIMECONFIG, 0 },
83   { STATE_PRODUCT,   "linguas",       STATE_LINGUAS,       0 },
84   { STATE_PRODUCT,   "updaterepokey", STATE_UPDATEREPOKEY, 1 },
85   { STATE_PRODUCT,   "cpename",       STATE_CPEID,         1 },
86   { STATE_URLS,      "url",           STATE_URL,           1 },
87   { STATE_LINGUAS,   "lang",          STATE_LANG,          0 },
88   { STATE_REGISTER,  "target",        STATE_TARGET,        1 },
89   { STATE_REGISTER,  "release",       STATE_REGRELEASE,    1 },
90   { NUMSTATES }
91 };
92
93 struct parsedata {
94   const char *filename;
95   const char *basename;
96   int depth;
97   enum state state;
98   int statedepth;
99   char *content;
100   int lcontent;
101   int acontent;
102   int docontent;
103   Pool *pool;
104   Repo *repo;
105   Repodata *data;
106
107   struct stateswitch *swtab[NUMSTATES];
108   enum state sbtab[NUMSTATES];
109   const char *attribute; /* only print this attribute, if currentproduct == baseproduct */
110
111   const char *tmplang;
112
113   const char *tmpvers;
114   const char *tmprel;
115   const char *tmpurltype;
116
117   unsigned int ctime;
118
119   Solvable *solvable;
120   Id handle;
121
122   ino_t baseproduct;
123   ino_t currentproduct;
124   int productscheme;
125
126   Id langcache[ID_NUM_INTERNAL];
127 };
128
129
130 /*
131  * find_attr
132  * find value for xml attribute
133  * I: txt, name of attribute
134  * I: atts, list of key/value attributes
135  * I: dup, strdup it
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, int dup)
142 {
143   for (; *atts; atts += 2)
144     {
145       if (!strcmp(*atts, txt))
146         return dup ? strdup(atts[1]) : atts[1];
147     }
148   return 0;
149 }
150
151
152 /*
153  * create localized tag
154  */
155
156 static Id
157 langtag(struct parsedata *pd, Id tag, const char *language)
158 {
159   if (language && !language[0])
160     language = 0;
161   if (!language || tag >= ID_NUM_INTERNAL)
162     return pool_id2langid(pd->repo->pool, tag, language, 1);
163   if (!pd->langcache[tag])
164     pd->langcache[tag] = pool_id2langid(pd->repo->pool, tag, language, 1);
165   return pd->langcache[tag];
166 }
167
168
169 /*
170  * XML callback: startElement
171  */
172
173 static void XMLCALL
174 startElement(void *userData, const char *name, const char **atts)
175 {
176   struct parsedata *pd = userData;
177   Pool *pool = pd->pool;
178   Solvable *s = pd->solvable;
179   struct stateswitch *sw;
180
181 #if 0
182       fprintf(stderr, "start: [%d]%s\n", pd->state, name);
183 #endif
184   if (pd->depth != pd->statedepth)
185     {
186       pd->depth++;
187       return;
188     }
189
190   pd->depth++;
191   if (!pd->swtab[pd->state])    /* no statetable -> no substates */
192     {
193 #if 0
194       fprintf(stderr, "into unknown: %s (from: %d)\n", name, pd->state);
195 #endif
196       return;
197     }
198   for (sw = pd->swtab[pd->state]; sw->from == pd->state; sw++)  /* find name in statetable */
199     if (!strcmp(sw->ename, name))
200       break;
201
202   if (sw->from != pd->state)
203     {
204 #if 0
205       fprintf(stderr, "into unknown: %s (from: %d)\n", name, pd->state);
206 #endif
207       return;
208     }
209   pd->state = sw->to;
210   pd->docontent = sw->docontent;
211   pd->statedepth = pd->depth;
212   pd->lcontent = 0;
213   *pd->content = 0;
214
215   switch(pd->state)
216     {
217     case STATE_PRODUCT:
218       /* parse 'schemeversion' and store in global variable */
219       {
220         const char * scheme = find_attr("schemeversion", atts, 0);
221         pd->productscheme = (scheme && *scheme) ? atoi(scheme) : -1;
222       }
223       if (!s)
224         {
225           s = pd->solvable = pool_id2solvable(pool, repo_add_solvable(pd->repo));
226           pd->handle = s - pool->solvables;
227         }
228       break;
229
230       /* <summary lang="xy">... */
231     case STATE_SUMMARY:
232       pd->tmplang = find_attr("lang", atts, 1);
233       break;
234     case STATE_DESCRIPTION:
235       pd->tmplang = find_attr("lang", atts, 1);
236       break;
237     case STATE_URL:
238       pd->tmpurltype = find_attr("name", atts, 1);
239       break;
240     default:
241       break;
242     }
243 }
244
245
246 static void XMLCALL
247 endElement(void *userData, const char *name)
248 {
249   struct parsedata *pd = userData;
250   Solvable *s = pd->solvable;
251
252 #if 0
253       fprintf(stderr, "end: [%d]%s\n", pd->state, name);
254 #endif
255   if (pd->depth != pd->statedepth)
256     {
257       pd->depth--;
258 #if 0
259       fprintf(stderr, "back from unknown %d %d %d\n", pd->state, pd->depth, pd->statedepth);
260 #endif
261       return;
262     }
263
264   pd->depth--;
265   pd->statedepth--;
266
267   switch (pd->state)
268     {
269     case STATE_PRODUCT:
270       /* product done, finish solvable */
271       if (pd->ctime)
272         repodata_set_num(pd->data, pd->handle, SOLVABLE_INSTALLTIME, pd->ctime);
273
274       if (pd->basename)
275         repodata_set_str(pd->data, pd->handle, PRODUCT_REFERENCEFILE, pd->basename);
276
277       /* this is where <productsdir>/baseproduct points to */
278       if (pd->currentproduct == pd->baseproduct)
279         repodata_set_str(pd->data, pd->handle, PRODUCT_TYPE, "base");
280
281       // output distver if requested, only if the product is
282       // a base product
283       // this is yum $releasever variable
284       if (pd->currentproduct == pd->baseproduct
285           && pd->attribute
286           && !strcmp(pd->attribute, "releasever"))
287         {
288           printf("%s\n", pd->tmpvers);
289         }
290
291       if (pd->tmprel)
292         {
293           if (pd->tmpvers)
294             s->evr = makeevr(pd->pool, join2(pd->tmpvers, "-", pd->tmprel));
295           else
296             {
297               fprintf(stderr, "Seen <release> but no <version>\n");
298             }
299         }
300       else if (pd->tmpvers)
301         s->evr = makeevr(pd->pool, pd->tmpvers); /* just version, no release */
302       pd->tmpvers = sat_free((void *)pd->tmpvers);
303       pd->tmprel = sat_free((void *)pd->tmprel);
304       if (!s->arch)
305         s->arch = ARCH_NOARCH;
306       if (s->arch != ARCH_SRC && s->arch != ARCH_NOSRC)
307         s->provides = repo_addid_dep(pd->repo, s->provides, rel2id(pd->pool, s->name, s->evr, REL_EQ, 1), 0);
308       pd->solvable = 0;
309       break;
310     case STATE_VENDOR:
311       s->vendor = str2id(pd->pool, pd->content, 1);
312       break;
313     case STATE_NAME:
314       s->name = str2id(pd->pool, join2("product", ":", pd->content), 1);
315       break;
316     case STATE_VERSION:
317       pd->tmpvers = strdup(pd->content);
318       break;
319     case STATE_RELEASE:
320       pd->tmprel = strdup(pd->content);
321       break;
322     case STATE_ARCH:
323       s->arch = str2id(pd->pool, pd->content, 1);
324       break;
325     case STATE_PRODUCTLINE:
326       repodata_set_str(pd->data, pd->handle, PRODUCT_PRODUCTLINE, pd->content);
327     break;
328     case STATE_UPDATEREPOKEY:
329       repodata_set_str(pd->data, pd->handle, langtag(pd, PRODUCT_UPDATEREPOKEY, pd->tmplang), pd->content);
330       break;
331     case STATE_SUMMARY:
332       repodata_set_str(pd->data, pd->handle, langtag(pd, SOLVABLE_SUMMARY, pd->tmplang), pd->content);
333       pd->tmplang = sat_free((void *)pd->tmplang);
334       break;
335     case STATE_DESCRIPTION:
336       repodata_set_str(pd->data, pd->handle, langtag(pd, SOLVABLE_DESCRIPTION, pd->tmplang), pd->content );
337       pd->tmplang = sat_free((void *)pd->tmplang);
338       break;
339     case STATE_URL:
340       if (pd->tmpurltype)
341         {
342           repodata_add_poolstr_array(pd->data, pd->handle, PRODUCT_URL, pd->content);
343           repodata_add_idarray(pd->data, pd->handle, PRODUCT_URL_TYPE, str2id(pd->pool, pd->tmpurltype, 1));
344         }
345       pd->tmpurltype = sat_free((void *)pd->tmpurltype);
346       break;
347     case STATE_TARGET:
348       repodata_set_str(pd->data, pd->handle, PRODUCT_REGISTER_TARGET, pd->content);
349       if (pd->currentproduct == pd->baseproduct
350           && pd->attribute
351           && !strcmp(pd->attribute, "register.target"))
352         {
353           printf("%s\n", pd->content);
354         }
355     break;
356     case STATE_REGRELEASE:
357       repodata_set_str(pd->data, pd->handle, PRODUCT_REGISTER_RELEASE, pd->content);
358       if (pd->currentproduct == pd->baseproduct
359           && pd->attribute
360           && !strcmp(pd->attribute, "register.release"))
361         {
362           printf("%s\n", pd->content);
363         }
364     break;
365     case STATE_CPEID:
366       if (pd->content)
367         repodata_set_str(pd->data, pd->handle, SOLVABLE_CPE_ID, pd->content);
368     default:
369       break;
370     }
371
372   pd->state = pd->sbtab[pd->state];
373   pd->docontent = 0;
374
375 #if 0
376       fprintf(stderr, "end: [%s] -> %d\n", name, pd->state);
377 #endif
378 }
379
380
381 static void XMLCALL
382 characterData(void *userData, const XML_Char *s, int len)
383 {
384   struct parsedata *pd = userData;
385   int l;
386   char *c;
387   if (!pd->docontent)
388     return;
389   l = pd->lcontent + len + 1;
390   if (l > pd->acontent)
391     {
392       pd->content = sat_realloc(pd->content, l + 256);
393       pd->acontent = l + 256;
394     }
395   c = pd->content + pd->lcontent;
396   pd->lcontent += len;
397   while (len-- > 0)
398     *c++ = *s++;
399   *c = 0;
400 }
401
402 #define BUFF_SIZE 8192
403
404
405 /*
406  * add single product to repo
407  *
408  */
409
410 static void
411 repo_add_product(struct parsedata *pd, FILE *fp, int code11)
412 {
413   char buf[BUFF_SIZE];
414   int l;
415   struct stat st;
416
417   if (!fstat(fileno(fp), &st))
418     {
419       pd->currentproduct = st.st_ino;
420       pd->ctime = (unsigned int)st.st_ctime;
421     }
422   else
423     {
424       pd->currentproduct = pd->baseproduct + 1; /* make it != baseproduct if stat fails */
425       perror("fstat");
426       pd->ctime = 0;
427     }
428
429   if (code11)
430     {
431       XML_Parser parser = XML_ParserCreate(NULL);
432       XML_SetUserData(parser, pd);
433       XML_SetElementHandler(parser, startElement, endElement);
434       XML_SetCharacterDataHandler(parser, characterData);
435
436       for (;;)
437         {
438           l = fread(buf, 1, sizeof(buf), fp);
439           if (XML_Parse(parser, buf, l, l == 0) == XML_STATUS_ERROR)
440             {
441               fprintf(stderr, "%s: %s at line %u:%u\n", pd->filename, XML_ErrorString(XML_GetErrorCode(parser)), (unsigned int)XML_GetCurrentLineNumber(parser), (unsigned int)XML_GetCurrentColumnNumber(parser));
442               fprintf(stderr, "Skipping this product\n");
443               return;
444             }
445           if (l == 0)
446             break;
447         }
448       XML_ParserFree(parser);
449     }
450   else
451     {
452       Id name = 0;
453       Id arch = 0;
454       Id version = 0;
455       int lnum = 0; /* line number */
456       char *ptr, *ptr1;
457       /* parse /etc/<xyz>-release file */
458       while (fgets(buf, sizeof(buf), fp))
459         {
460           /* remove trailing \n */
461           int l = strlen(buf);
462           if (*(buf + l - 1) == '\n')
463             {
464               --l;
465               *(buf + l) = 0;
466             }
467           ++lnum;
468
469           if (lnum == 1)
470             {
471               /* 1st line, <name> [(<arch>)] */
472               ptr = strchr(buf, '(');
473               if (ptr)
474                 {
475                   ptr1 = ptr - 1;
476                   *ptr++ = 0;
477                 }
478               else
479                 ptr1 = buf + l - 1;
480
481               /* track back until non-blank, non-digit */
482               while (ptr1 > buf
483                      && (*ptr1 == ' ' || isdigit(*ptr1) || *ptr1 == '.'))
484                 --ptr1;
485               *(++ptr1) = 0;
486               name = str2id(pd->pool, join2("product", ":", buf), 1);
487
488               if (ptr)
489                 {
490                   /* have arch */
491                   char *ptr1 = strchr(ptr, ')');
492                   if (ptr1)
493                     {
494                       *ptr1 = 0;
495                       /* downcase arch */
496                       ptr1 = ptr;
497                       while (*ptr1)
498                         {
499                           if (isupper(*ptr1)) *ptr1 = tolower(*ptr1);
500                           ++ptr1;
501                         }
502                       arch = str2id(pd->pool, ptr, 1);
503                     }
504                 }
505             }
506           else if (strncmp(buf, "VERSION", 7) == 0)
507             {
508               ptr = strchr(buf+7, '=');
509               if (ptr)
510                 {
511                   while (*++ptr == ' ');
512                   version = makeevr(pd->pool, ptr);
513                 }
514             }
515         }
516       if (name)
517         {
518           Solvable *s = pd->solvable = pool_id2solvable(pd->pool, repo_add_solvable(pd->repo));
519           s->name = name;
520           if (version)
521             s->evr = version;
522           if (arch)
523             s->arch = arch;
524           if (s->arch != ARCH_SRC && s->arch != ARCH_NOSRC)
525             s->provides = repo_addid_dep(pd->repo, s->provides, rel2id(pd->pool, s->name, s->evr, REL_EQ, 1), 0);
526         }
527     }
528   return;
529 }
530
531
532
533 /*
534  * parse dir looking for files ending in suffix
535  */
536
537 static void
538 parse_dir(DIR *dir, const char *path, struct parsedata *pd, int code11)
539 {
540   struct dirent *entry;
541   char *suffix = code11 ? ".prod" : "-release";
542   int slen = code11 ? 5 : 8;  /* strlen(".prod") : strlen("-release") */
543   struct stat st;
544
545   /* check for <productsdir>/baseproduct on code11 and remember its target inode */
546   if (code11
547       && stat(join2(path, "/", "baseproduct"), &st) == 0) /* follow symlink */
548     {
549       pd->baseproduct = st.st_ino;
550     }
551   else
552     pd->baseproduct = 0;
553
554   while ((entry = readdir(dir)))
555     {
556       int len;
557       len = strlen(entry->d_name);
558
559       /* skip /etc/lsb-release, thats not a product per-se */
560       if (!code11
561           && strcmp(entry->d_name, "lsb-release") == 0)
562         {
563           continue;
564         }
565
566       if (len > slen
567           && strcmp(entry->d_name + len - slen, suffix) == 0)
568         {
569           char *fullpath = join2(path, "/", entry->d_name);
570           FILE *fp = fopen(fullpath, "r");
571           if (!fp)
572             {
573               perror(fullpath);
574               break;
575             }
576           pd->filename = fullpath;
577           pd->basename = entry->d_name;
578           repo_add_product(pd, fp, code11);
579           fclose(fp);
580         }
581     }
582 }
583
584
585 /*
586  * read all installed products
587  *
588  * try proddir (reading all .xml files from this directory) first
589  * if not available, assume non-code11 layout and parse /etc/xyz-release
590  *
591  * parse each one as a product
592  */
593
594 /* Oh joy! Three parsers for the price of one! */
595
596 void
597 repo_add_products(Repo *repo, const char *proddir, const char *root, const char *attribute, int flags)
598 {
599   const char *fullpath = proddir;
600   DIR *dir;
601   int i;
602   struct parsedata pd;
603   struct stateswitch *sw;
604   Repodata *data;
605
606   if (!(flags & REPO_REUSE_REPODATA))
607     data = repo_add_repodata(repo, 0);
608   else
609     data = repo_last_repodata(repo);
610
611   memset(&pd, 0, sizeof(pd));
612   pd.repo = repo;
613   pd.pool = repo->pool;
614   pd.data = data;
615
616   pd.content = sat_malloc(256);
617   pd.acontent = 256;
618
619   pd.attribute = attribute;
620
621   for (i = 0, sw = stateswitches; sw->from != NUMSTATES; i++, sw++)
622     {
623       if (!pd.swtab[sw->from])
624         pd.swtab[sw->from] = sw;
625       pd.sbtab[sw->to] = sw->from;
626     }
627
628   dir = opendir(fullpath);
629   if (dir)
630     {
631       parse_dir(dir, fullpath, &pd, 1); /* assume 'code11' products */
632       closedir(dir);
633     }
634   else
635     {
636       fullpath = root ? join2(root, "", "/var/lib/zypp/db/products") : "/var/lib/zypp/db/products";
637       dir = opendir(fullpath);
638       if (dir)
639         {
640           repo_add_zyppdb_products(repo, data, fullpath, dir);      /* assume 'code10' zypp-style products */
641           closedir(dir);
642         }
643       else
644         {
645           fullpath = root ? join2(root, "", "/etc") : "/etc";
646           dir = opendir(fullpath);
647           if (dir)
648             {
649               parse_dir(dir, fullpath, &pd, 0); /* fall back to /etc/<xyz>-release parsing */
650               closedir(dir);
651             }
652           else
653             {
654               perror(fullpath);
655             }
656         }
657     }
658
659   sat_free((void *)pd.tmplang);
660   sat_free(pd.content);
661   join_freemem();
662
663   if (!(flags & REPO_NO_INTERNALIZE))
664     repodata_internalize(data);
665 }
666
667 /* EOF */