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