7e0a2969dacaa3064120dc50da8013d554876534
[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 (!(sw = pd->swtab[pd->state])) /* no statetable -> no substates */
187     {
188 #if 0
189       fprintf(stderr, "into unknown: [?]%s (from: ?, state %d)\n", name, pd->state);
190 #endif
191       return;
192     }
193   for (; 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: [%d]%s (from: %d, state %d)\n", sw->to, name, sw->from, pd->state);
201       exit( 1 );
202 #endif
203       return;
204     }
205   pd->state = sw->to;
206   pd->docontent = sw->docontent;
207   pd->statedepth = pd->depth;
208   pd->lcontent = 0;
209   *pd->content = 0;
210
211   switch(pd->state)
212     {
213     case STATE_PRODUCT:
214       /* parse 'schemeversion' and store in global variable */
215       {
216         const char * scheme = find_attr("schemeversion", atts, 0);
217         pd->productscheme = (scheme && *scheme) ? atoi(scheme) : -1;
218       }
219       if (!s)
220         {
221           s = pd->solvable = pool_id2solvable(pool, repo_add_solvable(pd->repo));
222           repodata_extend(pd->data, s - pool->solvables);
223           pd->handle = repodata_get_handle(pd->data, (s - pool->solvables) - pd->repo->start);
224         }
225       break;
226
227       /* <summary lang="xy">... */
228     case STATE_SUMMARY:
229       pd->tmplang = find_attr("lang", atts, 1);
230       break;
231     case STATE_DESCRIPTION:
232       pd->tmplang = find_attr("lang", atts, 1);
233       break;
234     case STATE_URL:
235       pd->tmpurltype = find_attr("name", atts, 1);
236       break;
237     default:
238       break;
239     }
240 }
241
242
243 static void XMLCALL
244 endElement(void *userData, const char *name)
245 {
246   struct parsedata *pd = userData;
247   Solvable *s = pd->solvable;
248
249 #if 0
250       fprintf(stderr, "end: [%d]%s\n", pd->state, name);
251 #endif
252   if (pd->depth != pd->statedepth)
253     {
254       pd->depth--;
255 #if 0
256       fprintf(stderr, "back from unknown %d %d %d\n", pd->state, pd->depth, pd->statedepth);
257 #endif
258       return;
259     }
260
261   pd->depth--;
262   pd->statedepth--;
263
264   switch (pd->state)
265     {
266     case STATE_PRODUCT:
267       /* product done, finish solvable */
268       if (pd->ctime)
269         repodata_set_num(pd->data, pd->handle, SOLVABLE_INSTALLTIME, pd->ctime);
270
271       /* this is where <productsdir>/baseproduct points to */
272       if (pd->currentproduct == pd->baseproduct)
273         repodata_set_str(pd->data, pd->handle, PRODUCT_TYPE, "base");
274
275       if (pd->tmprel)
276         {
277           if (pd->tmpvers)
278             s->evr = makeevr(pd->pool, join2(pd->tmpvers, "-", pd->tmprel));
279           else
280             {
281               fprintf(stderr, "Seen <release> but no <version>\n");
282             }
283         }
284       else if (pd->tmpvers)
285         s->evr = makeevr(pd->pool, pd->tmpvers); /* just version, no release */
286       pd->tmpvers = sat_free((void *)pd->tmpvers);
287       pd->tmprel = sat_free((void *)pd->tmprel);
288       if (!s->arch)
289         s->arch = ARCH_NOARCH;
290       if (s->arch != ARCH_SRC && s->arch != ARCH_NOSRC)
291         s->provides = repo_addid_dep(pd->repo, s->provides, rel2id(pd->pool, s->name, s->evr, REL_EQ, 1), 0);
292       pd->solvable = 0;
293       break;
294     case STATE_VENDOR:
295       s->vendor = str2id(pd->pool, pd->content, 1);
296       break;
297     case STATE_NAME:
298       s->name = str2id(pd->pool, join2("product", ":", pd->content), 1);
299       break;
300     case STATE_VERSION:
301       pd->tmpvers = strdup(pd->content);
302       break;
303     case STATE_RELEASE:
304       pd->tmprel = strdup(pd->content);
305       break;
306     case STATE_ARCH:
307       s->arch = str2id(pd->pool, pd->content, 1);
308       break;
309     case STATE_UPDATEREPOKEY:
310       repodata_set_str(pd->data, pd->handle, langtag(pd, PRODUCT_UPDATEREPOKEY, pd->tmplang), pd->content);
311       break;
312     case STATE_SUMMARY:
313       repodata_set_str(pd->data, pd->handle, langtag(pd, SOLVABLE_SUMMARY, pd->tmplang), pd->content);
314       pd->tmplang = sat_free((void *)pd->tmplang);
315       break;
316     case STATE_DESCRIPTION:
317       repodata_set_str(pd->data, pd->handle, langtag(pd, SOLVABLE_DESCRIPTION, pd->tmplang), pd->content );
318       pd->tmplang = sat_free((void *)pd->tmplang);
319       break;
320     case STATE_URL:
321       if (pd->tmpurltype)
322         {
323           repodata_add_poolstr_array(pd->data, pd->handle, PRODUCT_URL, pd->content);
324           repodata_add_idarray(pd->data, pd->handle, PRODUCT_URL_TYPE, str2id(pd->pool, pd->tmpurltype, 1));
325         }
326       pd->tmpurltype = sat_free((void *)pd->tmpurltype);
327       break;
328     case STATE_TARGET:
329       repodata_set_str(pd->data, pd->handle, PRODUCT_REGISTER_TARGET, pd->content);
330       if (pd->currentproduct == pd->baseproduct
331           && pd->attribute
332           && !strcmp(pd->attribute, "register.target"))
333         {
334           printf("%s\n", pd->content);
335         }
336     break;
337     case STATE_FLAVOR:
338       repodata_set_str(pd->data, pd->handle, PRODUCT_FLAVOR, pd->content);
339       if (pd->currentproduct == pd->baseproduct
340           && pd->attribute
341           && !strcmp(pd->attribute, "register.flavor"))
342         {
343           printf("%s\n", pd->content);
344         }
345     break;
346     case STATE_REGRELEASE:
347       repodata_set_str(pd->data, pd->handle, PRODUCT_REGISTER_RELEASE, pd->content);
348       if (pd->currentproduct == pd->baseproduct
349           && pd->attribute
350           && !strcmp(pd->attribute, "register.release"))
351         {
352           printf("%s\n", pd->content);
353         }
354     break;
355     default:
356       break;
357     }
358
359   pd->state = pd->sbtab[pd->state];
360   pd->docontent = 0;
361
362 #if 0
363       fprintf(stderr, "end: [%s] -> %d\n", name, pd->state);
364 #endif
365 }
366
367
368 static void XMLCALL
369 characterData(void *userData, const XML_Char *s, int len)
370 {
371   struct parsedata *pd = userData;
372   int l;
373   char *c;
374   if (!pd->docontent) {
375 #if 0
376     char *dup = strndup( s, len );
377   fprintf(stderr, "Content: [%d]'%s'\n", pd->state, dup );
378   free( dup );
379 #endif
380     return;
381   }
382   l = pd->lcontent + len + 1;
383   if (l > pd->acontent)
384     {
385       pd->content = realloc(pd->content, l + 256);
386       pd->acontent = l + 256;
387     }
388   c = pd->content + pd->lcontent;
389   pd->lcontent += len;
390   while (len-- > 0)
391     *c++ = *s++;
392   *c = 0;
393 }
394
395 #define BUFF_SIZE 8192
396
397
398 /*
399  * add single product to repo
400  *
401  */
402
403 static void
404 repo_add_product(struct parsedata *pd, Repodata *data, FILE *fp, int code11)
405 {
406   char buf[BUFF_SIZE];
407   int l;
408   struct stat st;
409
410   if (!fstat(fileno(fp), &st))
411     {
412       pd->currentproduct = st.st_ino;
413       pd->ctime = (unsigned int)st.st_ctime;
414     }
415   else
416     {
417       pd->currentproduct = pd->baseproduct + 1; /* make it != baseproduct if stat fails */
418       perror("fstat");
419       pd->ctime = 0;
420     }
421
422   XML_Parser parser = XML_ParserCreate(NULL);
423   XML_SetUserData(parser, pd);
424   XML_SetElementHandler(parser, startElement, endElement);
425   XML_SetCharacterDataHandler(parser, characterData);
426
427   for (;;)
428     {
429       l = fread(buf, 1, sizeof(buf), fp);
430       if (XML_Parse(parser, buf, l, l == 0) == XML_STATUS_ERROR)
431         {
432           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));
433           exit(1);
434         }
435       if (l == 0)
436         break;
437     }
438   XML_ParserFree(parser);
439 }
440
441
442 /*
443  * parse dir looking for files ending in suffix
444  */
445
446 static void
447 parse_dir(DIR *dir, const char *path, struct parsedata *pd, Repodata *repodata, int code11)
448 {
449   struct dirent *entry;
450   char *suffix = code11 ? ".prod" : "-release";
451   int slen = code11 ? 5 : 8;  /* strlen(".prod") : strlen("-release") */
452   struct stat st;
453
454   /* check for <productsdir>/baseproduct on code11 and remember its target inode */
455   if (code11
456       && stat(join2(path, "/", "baseproduct"), &st) == 0) /* follow symlink */
457     {
458       pd->baseproduct = st.st_ino;
459     }
460   else
461     pd->baseproduct = 0;
462
463   while ((entry = readdir(dir)))
464     {
465       int len;
466       len = strlen(entry->d_name);
467
468       /* skip /etc/lsb-release, thats not a product per-se */
469       if (!code11
470           && strcmp(entry->d_name, "lsb-release") == 0)
471         {
472           continue;
473         }
474
475       if (len > slen
476           && strcmp(entry->d_name + len - slen, suffix) == 0)
477         {
478           char *fullpath = join2(path, "/", entry->d_name);
479           FILE *fp = fopen(fullpath, "r");
480           if (!fp)
481             {
482               perror(fullpath);
483               break;
484             }
485           repo_add_product(pd, repodata, fp, code11);
486           fclose(fp);
487         }
488     }
489 }
490
491
492 /*
493  * read all installed products
494  *
495  * try proddir (reading all .xml files from this directory) first
496  * if not available, assume non-code11 layout and parse /etc/xyz-release
497  *
498  * parse each one as a product
499  */
500
501 void
502 repo_add_products(Repo *repo, Repodata *repodata, const char *proddir, const char *root, const char *attribute)
503 {
504   const char *fullpath = proddir;
505   int code11;
506   DIR *dir;
507   int i;
508   struct parsedata pd;
509   struct stateswitch *sw;
510
511   memset(&pd, 0, sizeof(pd));
512   pd.repo = repo;
513   pd.pool = repo->pool;
514   pd.data = repodata;
515
516   pd.content = malloc(256);
517   pd.acontent = 256;
518
519   pd.attribute = attribute;
520
521   for (i = 0, sw = stateswitches; sw->from != NUMSTATES; i++, sw++)
522     {
523       if (!pd.swtab[sw->from])
524         pd.swtab[sw->from] = sw;
525       pd.sbtab[sw->to] = sw->from;
526     }
527
528   code11 = 1;
529   dir = opendir(fullpath);
530   if (!dir)
531     {
532       fullpath = root ? join2(root, "", "/etc") : "/etc";
533       dir = opendir(fullpath);
534       code11 = 0;
535     }
536   if (!dir)
537     {
538       perror(fullpath);
539     }
540   else
541     {
542       parse_dir(dir, fullpath, &pd, repodata, code11);
543     }
544
545   sat_free((void *)pd.tmplang);
546   free(pd.content);
547   join_freemem();
548   closedir(dir);
549 }
550
551 /* EOF */