improve guessing of appdata.xml file name
[platform/upstream/libsolv.git] / ext / repo_appdata.c
1 /*
2  * repo_appdatadb.c
3  *
4  * Parses AppSteam Data files.
5  * See http://people.freedesktop.org/~hughsient/appdata/
6  *
7  *
8  * Copyright (c) 2013, 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 #include <errno.h>
27
28 #include "pool.h"
29 #include "repo.h"
30 #include "util.h"
31 #include "repo_appdata.h"
32
33
34 enum state {
35   STATE_START,
36   STATE_APPLICATION,
37   STATE_ID,
38   STATE_LICENCE,
39   STATE_NAME,
40   STATE_SUMMARY,
41   STATE_DESCRIPTION,
42   STATE_P,
43   STATE_UL,
44   STATE_UL_LI,
45   STATE_OL,
46   STATE_OL_LI,
47   STATE_URL,
48   STATE_GROUP,
49   NUMSTATES
50 };
51
52 struct stateswitch {
53   enum state from;
54   char *ename;
55   enum state to;
56   int docontent;
57 };
58
59 /* !! must be sorted by first column !! */
60 static struct stateswitch stateswitches[] = {
61   { STATE_START,       "applications",  STATE_START,   0 },
62   { STATE_START,       "application",   STATE_APPLICATION,   0 },
63   { STATE_APPLICATION, "id",            STATE_ID,            1 },
64   { STATE_APPLICATION, "licence",       STATE_LICENCE,       1 },
65   { STATE_APPLICATION, "name",          STATE_NAME,          1 },
66   { STATE_APPLICATION, "summary",       STATE_SUMMARY,       1 },
67   { STATE_APPLICATION, "description",   STATE_DESCRIPTION,   0 },
68   { STATE_APPLICATION, "url",           STATE_URL,           1 },
69   { STATE_APPLICATION, "project_group", STATE_GROUP,         1 },
70   { STATE_DESCRIPTION, "p",             STATE_P,             1 },
71   { STATE_DESCRIPTION, "ul",            STATE_UL,            0 },
72   { STATE_DESCRIPTION, "ol",            STATE_OL,            0 },
73   { STATE_UL,          "li",            STATE_UL_LI,         1 },
74   { STATE_OL,          "li",            STATE_OL_LI,         1 },
75   { NUMSTATES }
76 };
77
78 struct parsedata {
79   int depth;
80   enum state state;
81   int statedepth;
82   char *content;
83   int lcontent;
84   int acontent;
85   int docontent;
86   Pool *pool;
87   Repo *repo;
88   Repodata *data;
89
90   struct stateswitch *swtab[NUMSTATES];
91   enum state sbtab[NUMSTATES];
92
93   Solvable *solvable;
94   Id handle;
95
96   char *description;
97   int licnt;
98   int skip_tag;
99   int skip_tag_d;
100   int skip_tag_li;
101
102   int flags;
103   char *desktop_file;
104   int havesummary;
105 };
106
107
108 static inline const char *
109 find_attr(const char *txt, const char **atts)
110 {
111   for (; *atts; atts += 2)
112     if (!strcmp(*atts, txt))
113       return atts[1];
114   return 0;
115 }
116
117
118 static void XMLCALL
119 startElement(void *userData, const char *name, const char **atts)
120 {
121   struct parsedata *pd = userData;
122   Pool *pool = pd->pool;
123   Solvable *s = pd->solvable;
124   struct stateswitch *sw;
125
126 #if 0
127   fprintf(stderr, "start: [%d]%s\n", pd->state, name);
128 #endif
129   if (pd->depth != pd->statedepth)
130     {
131       pd->depth++;
132       return;
133     }
134
135   pd->depth++;
136   if (!pd->swtab[pd->state])    /* no statetable -> no substates */
137     {
138 #if 0
139       fprintf(stderr, "into unknown: %s (from: %d)\n", name, pd->state);
140 #endif
141       return;
142     }
143   for (sw = pd->swtab[pd->state]; sw->from == pd->state; sw++)  /* find name in statetable */
144     if (!strcmp(sw->ename, name))
145       break;
146
147   if (sw->from != pd->state)
148     {
149 #if 0
150       fprintf(stderr, "into unknown: %s (from: %d)\n", name, pd->state);
151 #endif
152       return;
153     }
154   pd->state = sw->to;
155   pd->docontent = sw->docontent;
156   pd->statedepth = pd->depth;
157   pd->lcontent = 0;
158   *pd->content = 0;
159
160   switch(pd->state)
161     {
162     case STATE_APPLICATION:
163       s = pd->solvable = pool_id2solvable(pool, repo_add_solvable(pd->repo));
164       pd->handle = s - pool->solvables;
165       pd->havesummary = 0;
166       break;
167     case STATE_NAME:
168     case STATE_SUMMARY:
169       pd->skip_tag = 0;
170       if (find_attr("xml:lang", atts))
171         pd->skip_tag = 1;
172       break;
173     case STATE_DESCRIPTION:
174       pd->skip_tag_d = 0;
175       if (find_attr("xml:lang", atts))
176         pd->skip_tag_d = 1;
177       pd->description = solv_free(pd->description);
178       break;
179     case STATE_OL:
180     case STATE_UL:
181       pd->skip_tag = 0;
182       if (find_attr("xml:lang", atts))
183         pd->skip_tag = 1;
184       pd->licnt = 0;
185       break;
186     case STATE_P:
187       pd->skip_tag = 0;
188       if (find_attr("xml:lang", atts))
189         pd->skip_tag = 1;
190       break;
191     case STATE_UL_LI:
192     case STATE_OL_LI:
193       pd->skip_tag_li = 0;
194       if (find_attr("xml:lang", atts))
195         pd->skip_tag_li = 1;
196       break;
197     default:
198       break;
199     }
200 }
201
202 /* replace whitespace with one space/newline */
203 /* also strip starting/ending whitespace */
204 static void
205 wsstrip(struct parsedata *pd)
206 {
207   int i, j;
208   int ws = 0;
209   for (i = j = 0; pd->content[i]; i++)
210     {
211       if (pd->content[i] == ' ' || pd->content[i] == '\t' || pd->content[i] == '\n')
212         {
213           ws |= pd->content[i] == '\n' ? 2 : 1;
214           continue;
215         }
216       if (ws && j)
217         pd->content[j++] = (ws & 2) ? '\n' : ' ';
218       ws = 0;
219       pd->content[j++] = pd->content[i];
220     }
221   pd->content[j] = 0;
222   pd->lcontent = j;
223 }
224
225 /* indent all lines */
226 static void
227 indent(struct parsedata *pd, int il)
228 {
229   int i, l;
230   for (l = 0; pd->content[l]; )
231     {
232       if (pd->content[l] == '\n')
233         {
234           l++;
235           continue;
236         }
237       if (pd->lcontent + il + 1 > pd->acontent)
238         {
239           pd->acontent = pd->lcontent + il + 256;
240           pd->content = realloc(pd->content, pd->acontent);
241         }
242       memmove(pd->content + l + il, pd->content + l, pd->lcontent - l + 1);
243       for (i = 0; i < il; i++)
244         pd->content[l + i] = ' ';
245       pd->lcontent += il;
246       while (pd->content[l] && pd->content[l] != '\n')
247         l++;
248     }
249 }
250
251 static void
252 add_missing_tags_from_desktop_file(struct parsedata *pd, Solvable *s, const char *desktop_file)
253 {
254   Pool *pool = pd->pool;
255   FILE *fp;
256   const char *filepath;
257   char buf[1024];
258   char *p, *p2, *p3;
259   int inde = 0;
260
261   filepath = pool_tmpjoin(pool, "/usr/share/applications/", desktop_file, 0);
262   if (pd->flags & REPO_USE_ROOTDIR)
263     filepath = pool_prepend_rootdir_tmp(pool, filepath);
264   if (!(fp = fopen(filepath, "r")))
265     return;
266   while (fgets(buf, sizeof(buf), fp) > 0)
267     {
268       int c, l = strlen(buf);
269       if (!l)
270         continue;
271       if (buf[l - 1] != '\n')
272         {
273           /* ignore overlong lines */
274           while ((c = getc(fp)) != EOF)
275             if (c == '\n')
276               break;
277           if (c == EOF)
278             break;
279           continue;
280         }
281       buf[--l] = 0;
282       while (l && (buf[l - 1] == ' ' || buf[l - 1] == '\t'))
283         buf[--l] = 0;
284       p = buf;
285       while (*p == ' ' || *p == '\t')
286         p++;
287       if (!*p || *p == '#')
288         continue;
289       if (*p == '[')
290         inde = 0;
291       if (!strcmp(p, "[Desktop Entry]"))
292         {
293           inde = 1;
294           continue;
295         }
296       if (!inde)
297         continue;
298       p2 = strchr(p, '=');
299       if (!p2 || p2 == p)
300         continue;
301       *p2 = 0;
302       for (p3 = p2 - 1; *p3 == ' ' || *p3 == '\t'; p3--)
303         *p3 = 0;
304       p2++;
305       while (*p2 == ' ' || *p2 == '\t')
306         p2++;
307       if (!*p2)
308         continue;
309       if (!s->name && !strcmp(p, "Name"))
310         s->name = pool_str2id(pool, pool_tmpjoin(pool, "application:", p2, 0), 1);
311       else if (!pd->havesummary && !strcmp(p, "Comment"))
312         {
313           pd->havesummary = 1;
314           repodata_set_str(pd->data, pd->handle, SOLVABLE_SUMMARY, p2);
315         }
316       else
317         continue;
318       if (s->name && pd->havesummary)
319         break;  /* our work is done */
320     }
321   fclose(fp);
322 }
323
324 static void XMLCALL
325 endElement(void *userData, const char *name)
326 {
327   struct parsedata *pd = userData;
328   Pool *pool = pd->pool;
329   Solvable *s = pd->solvable;
330   Id id;
331
332 #if 0
333   fprintf(stderr, "end: [%d]%s\n", pd->state, name);
334 #endif
335   if (pd->depth != pd->statedepth)
336     {
337       pd->depth--;
338 #if 0
339       fprintf(stderr, "back from unknown %d %d %d\n", pd->state, pd->depth, pd->statedepth);
340 #endif
341       return;
342     }
343
344   pd->depth--;
345   pd->statedepth--;
346
347   switch (pd->state)
348     {
349     case STATE_APPLICATION:
350       if (!s->arch)
351         s->arch = ARCH_NOARCH;
352       if (!s->evr)
353         s->evr = ID_EMPTY;
354       if ((!s->name || !pd->havesummary) && (pd->flags & APPDATA_CHECK_DESKTOP_FILE) != 0 && pd->desktop_file)
355         add_missing_tags_from_desktop_file(pd, s, pd->desktop_file);
356       if (!s->name && pd->desktop_file)
357         {
358           char *name = pool_tmpjoin(pool, "application:", pd->desktop_file, 0);
359           int l = strlen(name);
360           if (l > 8 && !strcmp(".desktop", name + l - 8))
361             l -= 8;
362           s->name = pool_strn2id(pool, name, l, 1);
363         }
364       if (s->name && s->arch != ARCH_SRC && s->arch != ARCH_NOSRC)
365         s->provides = repo_addid_dep(pd->repo, s->provides, pool_rel2id(pd->pool, s->name, s->evr, REL_EQ, 1), 0);
366       pd->solvable = 0;
367       pd->desktop_file = solv_free(pd->desktop_file);
368       break;
369     case STATE_ID:
370       pd->desktop_file = solv_strdup(pd->content);
371       /* guess the appdata.xml file name from the id element */
372       if (pd->lcontent > 8 && !strcmp(".desktop", pd->content + pd->lcontent - 8))
373         pd->content[pd->lcontent - 8] = 0;
374       else if (pd->lcontent > 4 && !strcmp(".ttf", pd->content + pd->lcontent - 4))
375         pd->content[pd->lcontent - 4] = 0;
376       else if (pd->lcontent > 4 && !strcmp(".otf", pd->content + pd->lcontent - 4))
377         pd->content[pd->lcontent - 4] = 0;
378       else if (pd->lcontent > 4 && !strcmp(".xml", pd->content + pd->lcontent - 4))
379         pd->content[pd->lcontent - 4] = 0;
380       else if (pd->lcontent > 3 && !strcmp(".db", pd->content + pd->lcontent - 3))
381         pd->content[pd->lcontent - 3] = 0;
382       id = pool_str2id(pd->pool, pool_tmpjoin(pool, "appdata(", pd->content, ".appdata.xml)"), 1);
383       s->requires = repo_addid_dep(pd->repo, s->requires, id, 0);
384       id = pool_str2id(pd->pool, pool_tmpjoin(pool, "application-appdata(", pd->content, ".appdata.xml)"), 1);
385       s->provides = repo_addid_dep(pd->repo, s->provides, id, 0);
386       break;
387     case STATE_NAME:
388       if (pd->skip_tag)
389         break;
390       s->name = pool_str2id(pd->pool, pool_tmpjoin(pool, "application:", pd->content, 0), 1);
391       break;
392     case STATE_LICENCE:
393       repodata_add_poolstr_array(pd->data, pd->handle, SOLVABLE_LICENSE, pd->content);
394       break;
395     case STATE_SUMMARY:
396       if (pd->skip_tag)
397         break;
398       pd->havesummary = 1;
399       repodata_set_str(pd->data, pd->handle, SOLVABLE_SUMMARY, pd->content);
400       break;
401     case STATE_URL:
402       repodata_set_str(pd->data, pd->handle, SOLVABLE_URL, pd->content);
403       break;
404     case STATE_GROUP:
405       repodata_add_poolstr_array(pd->data, pd->handle, SOLVABLE_GROUP, pd->content);
406       break;
407     case STATE_DESCRIPTION:
408       if (pd->description && !pd->skip_tag_d)
409         {
410           /* strip trailing newlines */
411           int l = strlen(pd->description);
412           while (l && pd->description[l - 1] == '\n')
413             pd->description[--l] = 0;
414           repodata_set_str(pd->data, pd->handle, SOLVABLE_DESCRIPTION, pd->description);
415         }
416       break;
417     case STATE_P:
418       if (pd->skip_tag)
419         break;
420       wsstrip(pd);
421       pd->description = solv_dupappend(pd->description, pd->content, "\n\n");
422       break;
423     case STATE_UL_LI:
424       if (pd->skip_tag || pd->skip_tag_li)
425         break;
426       wsstrip(pd);
427       indent(pd, 4);
428       pd->content[2] = '-';
429       pd->description = solv_dupappend(pd->description, pd->content, "\n");
430       break;
431     case STATE_OL_LI:
432       if (pd->skip_tag || pd->skip_tag_li)
433         break;
434       wsstrip(pd);
435       indent(pd, 4);
436       if (++pd->licnt >= 10)
437         pd->content[0] = '0' + (pd->licnt / 10) % 10;
438       pd->content[1] = '0' + pd->licnt  % 10;
439       pd->content[2] = '.';
440       pd->description = solv_dupappend(pd->description, pd->content, "\n");
441       break;
442     case STATE_UL:
443     case STATE_OL:
444       if (pd->skip_tag)
445         break;
446       pd->description = solv_dupappend(pd->description, "\n", 0);
447       break;
448     default:
449       break;
450     }
451
452   pd->state = pd->sbtab[pd->state];
453   pd->docontent = 0;
454
455 #if 0
456   fprintf(stderr, "end: [%s] -> %d\n", name, pd->state);
457 #endif
458 }
459
460
461 static void XMLCALL
462 characterData(void *userData, const XML_Char *s, int len)
463 {
464   struct parsedata *pd = userData;
465   int l;
466   char *c;
467   if (!pd->docontent)
468     return;
469   l = pd->lcontent + len + 1;
470   if (l > pd->acontent)
471     {
472       pd->acontent = l + 256;
473       pd->content = realloc(pd->content, pd->acontent);
474     }
475   c = pd->content + pd->lcontent;
476   pd->lcontent += len;
477   while (len-- > 0)
478     *c++ = *s++;
479   *c = 0;
480 }
481
482 #define BUFF_SIZE 8192
483
484 int
485 repo_add_appdata(Repo *repo, FILE *fp, int flags)
486 {
487   Pool *pool = repo->pool;
488   struct parsedata pd;
489   struct stateswitch *sw;
490   Repodata *data;
491   char buf[BUFF_SIZE];
492   int i, l;
493   int ret = 0;
494
495   data = repo_add_repodata(repo, flags);
496   memset(&pd, 0, sizeof(pd));
497   pd.repo = repo;
498   pd.pool = repo->pool;
499   pd.data = data;
500   pd.flags = flags;
501
502   pd.content = malloc(256);
503   pd.acontent = 256;
504
505   for (i = 0, sw = stateswitches; sw->from != NUMSTATES; i++, sw++)
506     {
507       if (!pd.swtab[sw->from])
508         pd.swtab[sw->from] = sw;
509       pd.sbtab[sw->to] = sw->from;
510     }
511
512   XML_Parser parser = XML_ParserCreate(NULL);
513   XML_SetUserData(parser, &pd);
514   XML_SetElementHandler(parser, startElement, endElement);
515   XML_SetCharacterDataHandler(parser, characterData);
516
517   for (;;)
518     {
519       l = fread(buf, 1, sizeof(buf), fp);
520       if (XML_Parse(parser, buf, l, l == 0) == XML_STATUS_ERROR)
521         {
522           pool_error(pool, -1, "repo_appdata: %s at line %u:%u\n", XML_ErrorString(XML_GetErrorCode(parser)), (unsigned int)XML_GetCurrentLineNumber(parser), (unsigned int)XML_GetCurrentColumnNumber(parser));
523           ret = -1;
524           break;
525         }
526       if (l == 0)
527         break;
528     }
529   XML_ParserFree(parser);
530
531   if (!(flags & REPO_NO_INTERNALIZE))
532     repodata_internalize(data);
533
534   solv_free(pd.content);
535   solv_free(pd.desktop_file);
536   solv_free(pd.description);
537   return ret;
538 }
539
540 /* add all files ending in .appdata.xml */
541 int
542 repo_add_appdata_dir(Repo *repo, const char *appdatadir, int flags)
543 {
544   DIR *dir;
545   char *dirpath;
546   Repodata *data;
547
548   data = repo_add_repodata(repo, flags);
549   if (flags & REPO_USE_ROOTDIR)
550     dirpath = pool_prepend_rootdir(repo->pool, appdatadir);
551   else
552     dirpath = solv_strdup(appdatadir);
553   if ((dir = opendir(dirpath)) != 0)
554     {
555       struct dirent *entry;
556       while ((entry = readdir(dir)))
557         {
558           const char *n;
559           FILE *fp;
560           int len = strlen(entry->d_name);
561           if (len <= 12 || strcmp(entry->d_name + len - 12, ".appdata.xml") != 0)
562             continue;
563           if (entry->d_name[0] == '.')
564             continue;
565           n = pool_tmpjoin(repo->pool, dirpath, "/", entry->d_name);
566           fp = fopen(n, "r");
567           if (!fp)
568             {
569               pool_error(repo->pool, 0, "%s: %s", n, strerror(errno));
570               continue;
571             }
572           repo_add_appdata(repo, fp, flags | REPO_NO_INTERNALIZE | REPO_REUSE_REPODATA | APPDATA_CHECK_DESKTOP_FILE);
573           fclose(fp);
574         }
575       closedir(dir);
576     }
577   solv_free(dirpath);
578   if (!(flags & REPO_NO_INTERNALIZE))
579     repodata_internalize(data);
580   return 0;
581 }