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