- only add selfprovides if name is set
[platform/upstream/libsolv.git] / ext / 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 #include "repo_releasefile_products.h"
35
36
37 //#define DUMPOUT 0
38
39 enum state {
40   STATE_START,           // 0
41   STATE_PRODUCT,         // 1
42   STATE_VENDOR,          // 2
43   STATE_NAME,            // 3
44   STATE_VERSION,         // 4
45   STATE_RELEASE,         // 5
46   STATE_ARCH,            // 6
47   STATE_SUMMARY,         // 7
48   STATE_DESCRIPTION,     // 8
49   STATE_UPDATEREPOKEY,   // 9 should go away
50   STATE_CPEID,         // 9
51   STATE_URLS,            // 10
52   STATE_URL,             // 11
53   STATE_RUNTIMECONFIG,   // 12
54   STATE_LINGUAS,         // 13
55   STATE_LANG,            // 14
56   STATE_REGISTER,        // 15
57   STATE_TARGET,          // 16
58   STATE_REGRELEASE,      // 18
59   STATE_PRODUCTLINE,     // 19
60   NUMSTATES              // 0
61 };
62
63 struct stateswitch {
64   enum state from;
65   char *ename;
66   enum state to;
67   int docontent;
68 };
69
70 /* !! must be sorted by first column !! */
71 static struct stateswitch stateswitches[] = {
72   { STATE_START,     "product",       STATE_PRODUCT,       0 },
73   { STATE_PRODUCT,   "vendor",        STATE_VENDOR,        1 },
74   { STATE_PRODUCT,   "name",          STATE_NAME,          1 },
75   { STATE_PRODUCT,   "version",       STATE_VERSION,       1 },
76   { STATE_PRODUCT,   "release",       STATE_RELEASE,       1 },
77   { STATE_PRODUCT,   "arch",          STATE_ARCH,          1 },
78   { STATE_PRODUCT,   "productline",   STATE_PRODUCTLINE,   1 },
79   { STATE_PRODUCT,   "summary",       STATE_SUMMARY,       1 },
80   { STATE_PRODUCT,   "description",   STATE_DESCRIPTION,   1 },
81   { STATE_PRODUCT,   "register",      STATE_REGISTER,      0 },
82   { STATE_PRODUCT,   "urls",          STATE_URLS,          0 },
83   { STATE_PRODUCT,   "runtimeconfig", STATE_RUNTIMECONFIG, 0 },
84   { STATE_PRODUCT,   "linguas",       STATE_LINGUAS,       0 },
85   { STATE_PRODUCT,   "updaterepokey", STATE_UPDATEREPOKEY, 1 },
86   { STATE_PRODUCT,   "cpeid",         STATE_CPEID,         1 },
87   { STATE_URLS,      "url",           STATE_URL,           1 },
88   { STATE_LINGUAS,   "lang",          STATE_LANG,          0 },
89   { STATE_REGISTER,  "target",        STATE_TARGET,        1 },
90   { STATE_REGISTER,  "release",       STATE_REGRELEASE,    1 },
91   { NUMSTATES }
92 };
93
94 struct parsedata {
95   const char *filename;
96   const char *basename;
97   int depth;
98   enum state state;
99   int statedepth;
100   char *content;
101   int lcontent;
102   int acontent;
103   int docontent;
104   Pool *pool;
105   Repo *repo;
106   Repodata *data;
107
108   struct stateswitch *swtab[NUMSTATES];
109   enum state sbtab[NUMSTATES];
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       if (pd->tmprel)
282         {
283           if (pd->tmpvers)
284             s->evr = makeevr(pd->pool, join2(pd->tmpvers, "-", pd->tmprel));
285           else
286             {
287               fprintf(stderr, "Seen <release> but no <version>\n");
288             }
289         }
290       else if (pd->tmpvers)
291         s->evr = makeevr(pd->pool, pd->tmpvers); /* just version, no release */
292       pd->tmpvers = sat_free((void *)pd->tmpvers);
293       pd->tmprel = sat_free((void *)pd->tmprel);
294       if (!s->arch)
295         s->arch = ARCH_NOARCH;
296       if (!s->evr)
297         s->evr = ID_EMPTY;
298       if (s->name && s->arch != ARCH_SRC && s->arch != ARCH_NOSRC)
299         s->provides = repo_addid_dep(pd->repo, s->provides, rel2id(pd->pool, s->name, s->evr, REL_EQ, 1), 0);
300       pd->solvable = 0;
301       break;
302     case STATE_VENDOR:
303       s->vendor = str2id(pd->pool, pd->content, 1);
304       break;
305     case STATE_NAME:
306       s->name = str2id(pd->pool, join2("product", ":", pd->content), 1);
307       break;
308     case STATE_VERSION:
309       pd->tmpvers = strdup(pd->content);
310       break;
311     case STATE_RELEASE:
312       pd->tmprel = strdup(pd->content);
313       break;
314     case STATE_ARCH:
315       s->arch = str2id(pd->pool, pd->content, 1);
316       break;
317     case STATE_PRODUCTLINE:
318       repodata_set_str(pd->data, pd->handle, PRODUCT_PRODUCTLINE, pd->content);
319     break;
320     case STATE_UPDATEREPOKEY:
321       /** obsolete **/
322       break;
323     case STATE_SUMMARY:
324       repodata_set_str(pd->data, pd->handle, langtag(pd, SOLVABLE_SUMMARY, pd->tmplang), pd->content);
325       pd->tmplang = sat_free((void *)pd->tmplang);
326       break;
327     case STATE_DESCRIPTION:
328       repodata_set_str(pd->data, pd->handle, langtag(pd, SOLVABLE_DESCRIPTION, pd->tmplang), pd->content );
329       pd->tmplang = sat_free((void *)pd->tmplang);
330       break;
331     case STATE_URL:
332       if (pd->tmpurltype)
333         {
334           repodata_add_poolstr_array(pd->data, pd->handle, PRODUCT_URL, pd->content);
335           repodata_add_idarray(pd->data, pd->handle, PRODUCT_URL_TYPE, str2id(pd->pool, pd->tmpurltype, 1));
336         }
337       pd->tmpurltype = sat_free((void *)pd->tmpurltype);
338       break;
339     case STATE_TARGET:
340       repodata_set_str(pd->data, pd->handle, PRODUCT_REGISTER_TARGET, pd->content);
341       break;
342     case STATE_REGRELEASE:
343       repodata_set_str(pd->data, pd->handle, PRODUCT_REGISTER_RELEASE, pd->content);
344       break;
345     case STATE_CPEID:
346       if (pd->content)
347         repodata_set_str(pd->data, pd->handle, SOLVABLE_CPE_ID, pd->content);
348     default:
349       break;
350     }
351
352   pd->state = pd->sbtab[pd->state];
353   pd->docontent = 0;
354
355 #if 0
356       fprintf(stderr, "end: [%s] -> %d\n", name, pd->state);
357 #endif
358 }
359
360
361 static void XMLCALL
362 characterData(void *userData, const XML_Char *s, int len)
363 {
364   struct parsedata *pd = userData;
365   int l;
366   char *c;
367   if (!pd->docontent)
368     return;
369   l = pd->lcontent + len + 1;
370   if (l > pd->acontent)
371     {
372       pd->content = sat_realloc(pd->content, l + 256);
373       pd->acontent = l + 256;
374     }
375   c = pd->content + pd->lcontent;
376   pd->lcontent += len;
377   while (len-- > 0)
378     *c++ = *s++;
379   *c = 0;
380 }
381
382 #define BUFF_SIZE 8192
383
384
385 /*
386  * add single product to repo
387  *
388  */
389
390 static void
391 add_code11_product(struct parsedata *pd, FILE *fp)
392 {
393   char buf[BUFF_SIZE];
394   int l;
395   struct stat st;
396
397   if (!fstat(fileno(fp), &st))
398     {
399       pd->currentproduct = st.st_ino;
400       pd->ctime = (unsigned int)st.st_ctime;
401     }
402   else
403     {
404       pd->currentproduct = pd->baseproduct + 1; /* make it != baseproduct if stat fails */
405       perror("fstat");
406       pd->ctime = 0;
407     }
408
409   XML_Parser parser = XML_ParserCreate(NULL);
410   XML_SetUserData(parser, pd);
411   XML_SetElementHandler(parser, startElement, endElement);
412   XML_SetCharacterDataHandler(parser, characterData);
413
414   for (;;)
415     {
416       l = fread(buf, 1, sizeof(buf), fp);
417       if (XML_Parse(parser, buf, l, l == 0) == XML_STATUS_ERROR)
418         {
419           pool_debug(pd->pool, SAT_ERROR, "%s: %s at line %u:%u\n", pd->filename, XML_ErrorString(XML_GetErrorCode(parser)), (unsigned int)XML_GetCurrentLineNumber(parser), (unsigned int)XML_GetCurrentColumnNumber(parser));
420           pool_debug(pd->pool, SAT_ERROR, "skipping this product\n");
421           XML_ParserFree(parser);
422           return;
423         }
424       if (l == 0)
425         break;
426     }
427   XML_ParserFree(parser);
428 }
429
430
431 void
432 repo_add_code11_products(Repo *repo, const char *dirpath, int flags)
433 {
434   Repodata *data;
435   struct parsedata pd;
436   struct stateswitch *sw;
437   DIR *dir;
438   int i;
439
440   if (!(flags & REPO_REUSE_REPODATA))
441     data = repo_add_repodata(repo, 0);
442   else
443     data = repo_last_repodata(repo);
444
445   memset(&pd, 0, sizeof(pd));
446   pd.repo = repo;
447   pd.pool = repo->pool;
448   pd.data = data;
449
450   pd.content = sat_malloc(256);
451   pd.acontent = 256;
452
453   for (i = 0, sw = stateswitches; sw->from != NUMSTATES; i++, sw++)
454     {
455       if (!pd.swtab[sw->from])
456         pd.swtab[sw->from] = sw;
457       pd.sbtab[sw->to] = sw->from;
458     }
459
460   dir = opendir(dirpath);
461   if (dir)
462     {
463       struct dirent *entry;
464       struct stat st;
465       char *fullpath;
466
467       /* check for <productsdir>/baseproduct on code11 and remember its target inode */
468       if (stat(join2(dirpath, "/", "baseproduct"), &st) == 0) /* follow symlink */
469         pd.baseproduct = st.st_ino;
470       else
471         pd.baseproduct = 0;
472
473       while ((entry = readdir(dir)))
474         {
475           int len = strlen(entry->d_name);
476           if (len <= 5 || strcmp(entry->d_name + len - 5, ".prod") != 0)
477             continue;
478           fullpath = join2(dirpath, "/", entry->d_name);
479           FILE *fp = fopen(fullpath, "r");
480           if (!fp)
481             {
482               perror(fullpath);
483               continue;
484             }
485           pd.filename = fullpath;
486           pd.basename = entry->d_name;
487           add_code11_product(&pd, fp);
488           fclose(fp);
489         }
490       closedir(dir);
491     }
492   sat_free((void *)pd.tmplang);
493   sat_free(pd.content);
494   join_freemem();
495
496   if (!(flags & REPO_NO_INTERNALIZE))
497     repodata_internalize(data);
498 }
499
500
501 /******************************************************************************************/
502
503
504 /*
505  * read all installed products
506  *
507  * try proddir (reading all .xml files from this directory) first
508  * if not available, assume non-code11 layout and parse /etc/xyz-release
509  *
510  * parse each one as a product
511  */
512
513 /* Oh joy! Three parsers for the price of one! */
514
515 void
516 repo_add_products(Repo *repo, const char *proddir, const char *root, int flags)
517 {
518   const char *fullpath;
519   DIR *dir;
520
521   dir = opendir(proddir);
522   if (dir)
523     {
524       /* assume code11 stype products */
525       closedir(dir);
526       repo_add_code11_products(repo, proddir, flags);
527       return;
528     }
529
530   /* code11 didn't work, try old zyppdb */
531   fullpath = root ? join2(root, "", "/var/lib/zypp/db/products") : "/var/lib/zypp/db/products";
532   dir = opendir(fullpath);
533   if (dir)
534     {
535       closedir(dir);
536       /* assume code10 style products */
537       repo_add_zyppdb_products(repo, fullpath, flags);
538       join_freemem();
539       return;
540     }
541   
542   /* code11 didn't work, try -release files parsing */
543   fullpath = root ? join2(root, "", "/etc") : "/etc";
544   dir = opendir(fullpath);
545   if (dir)
546     {
547       closedir(dir);
548       repo_add_releasefile_products(repo, fullpath, flags);
549       join_freemem();
550       return;
551     }
552
553   /* no luck. print an error message in case the root argument is wrong */
554   perror(fullpath);
555   join_freemem();
556
557   /* the least we can do... */
558   if (!(flags & REPO_NO_INTERNALIZE) && (flags & REPO_REUSE_REPODATA) != 0)
559     repodata_internalize(repo_last_repodata(repo));
560 }
561
562 /* EOF */