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