4 * Parses AppSteam Data files.
5 * See http://people.freedesktop.org/~hughsient/appdata/
8 * Copyright (c) 2013, Novell Inc.
10 * This program is licensed under the BSD license, read LICENSE.BSD
11 * for further information
14 #include <sys/types.h>
27 #include "solv_xmlparser.h"
28 #include "repo_appdata.h"
54 static struct solv_xmlparser_element stateswitches[] = {
55 { STATE_START, "applications", STATE_START, 0 },
56 { STATE_START, "components", STATE_START, 0 },
57 { STATE_START, "application", STATE_APPLICATION, 0 },
58 { STATE_START, "component", STATE_APPLICATION, 0 },
59 { STATE_APPLICATION, "id", STATE_ID, 1 },
60 { STATE_APPLICATION, "pkgname", STATE_PKGNAME, 1 },
61 { STATE_APPLICATION, "product_license", STATE_LICENCE, 1 },
62 { STATE_APPLICATION, "name", STATE_NAME, 1 },
63 { STATE_APPLICATION, "summary", STATE_SUMMARY, 1 },
64 { STATE_APPLICATION, "description", STATE_DESCRIPTION, 0 },
65 { STATE_APPLICATION, "url", STATE_URL, 1 },
66 { STATE_APPLICATION, "project_group", STATE_GROUP, 1 },
67 { STATE_APPLICATION, "keywords", STATE_KEYWORDS, 0 },
68 { STATE_APPLICATION, "extends", STATE_EXTENDS, 1 },
69 { STATE_DESCRIPTION, "p", STATE_P, 1 },
70 { STATE_DESCRIPTION, "ul", STATE_UL, 0 },
71 { STATE_DESCRIPTION, "ol", STATE_OL, 0 },
72 { STATE_UL, "li", STATE_UL_LI, 1 },
73 { STATE_OL, "li", STATE_OL_LI, 1 },
74 { STATE_KEYWORDS, "keyword", STATE_KEYWORD, 1 },
97 struct solv_xmlparser xmlp;
102 startElement(struct solv_xmlparser *xmlp, int state, const char *name, const char **atts)
104 struct parsedata *pd = xmlp->userdata;
105 Pool *pool = pd->pool;
109 /* ignore all language tags */
110 if (pd->skiplang || solv_xmlparser_find_attr("xml:lang", atts))
118 case STATE_APPLICATION:
119 type = solv_xmlparser_find_attr("type", atts);
122 if (strcmp(type, "desktop") != 0)
128 s = pd->solvable = pool_id2solvable(pool, repo_add_solvable(pd->repo));
129 pd->handle = s - pool->solvables;
131 repodata_set_poolstr(pd->data, pd->handle, SOLVABLE_CATEGORY, type);
133 case STATE_DESCRIPTION:
134 pd->description = solv_free(pd->description);
145 /* replace whitespace with one space/newline */
146 /* also strip starting/ending whitespace */
148 wsstrip(struct parsedata *pd)
150 struct solv_xmlparser *xmlp = &pd->xmlp;
153 for (i = j = 0; xmlp->content[i]; i++)
155 if (xmlp->content[i] == ' ' || xmlp->content[i] == '\t' || xmlp->content[i] == '\n')
157 ws |= xmlp->content[i] == '\n' ? 2 : 1;
161 xmlp->content[j++] = (ws & 2) ? '\n' : ' ';
163 xmlp->content[j++] = xmlp->content[i];
165 xmlp->content[j] = 0;
167 return xmlp->content;
170 /* indent all lines */
172 indent(struct parsedata *pd, int il)
174 struct solv_xmlparser *xmlp = &pd->xmlp;
176 for (l = 0; xmlp->content[l]; )
178 if (xmlp->content[l] == '\n')
183 if (xmlp->lcontent + il + 1 > xmlp->acontent)
185 xmlp->acontent = xmlp->lcontent + il + 256;
186 xmlp->content = solv_realloc(xmlp->content, xmlp->acontent);
188 memmove(xmlp->content + l + il, xmlp->content + l, xmlp->lcontent - l + 1);
189 for (i = 0; i < il; i++)
190 xmlp->content[l + i] = ' ';
191 xmlp->lcontent += il;
192 while (xmlp->content[l] && xmlp->content[l] != '\n')
195 return xmlp->content;
199 add_missing_tags_from_desktop_file(struct parsedata *pd, Solvable *s, const char *desktop_file)
201 Pool *pool = pd->pool;
203 const char *filepath;
208 filepath = pool_tmpjoin(pool, "/usr/share/applications/", desktop_file, 0);
209 if (pd->flags & REPO_USE_ROOTDIR)
210 filepath = pool_prepend_rootdir_tmp(pool, filepath);
211 if (!(fp = fopen(filepath, "r")))
213 while (fgets(buf, sizeof(buf), fp) > 0)
215 int c, l = strlen(buf);
218 if (buf[l - 1] != '\n')
220 /* ignore overlong lines */
221 while ((c = getc(fp)) != EOF)
229 while (l && (buf[l - 1] == ' ' || buf[l - 1] == '\t'))
232 while (*p == ' ' || *p == '\t')
234 if (!*p || *p == '#')
238 if (!strcmp(p, "[Desktop Entry]"))
249 for (p3 = p2 - 1; *p3 == ' ' || *p3 == '\t'; p3--)
252 while (*p2 == ' ' || *p2 == '\t')
256 if (!s->name && !strcmp(p, "Name"))
257 s->name = pool_str2id(pool, pool_tmpjoin(pool, "application:", p2, 0), 1);
258 else if (!pd->havesummary && !strcmp(p, "Comment"))
261 repodata_set_str(pd->data, pd->handle, SOLVABLE_SUMMARY, p2);
265 if (s->name && pd->havesummary)
266 break; /* our work is done */
272 guess_filename_from_id(Pool *pool, const char *id)
275 char *r = pool_tmpjoin(pool, id, ".metainfo.xml", 0);
276 if (l > 8 && !strcmp(".desktop", id + l - 8))
277 strcpy(r + l - 8, ".appdata.xml");
278 else if (l > 4 && !strcmp(".ttf", id + l - 4))
279 strcpy(r + l - 4, ".metainfo.xml");
280 else if (l > 4 && !strcmp(".otf", id + l - 4))
281 strcpy(r + l - 4, ".metainfo.xml");
282 else if (l > 4 && !strcmp(".xml", id + l - 4))
283 strcpy(r + l - 4, ".metainfo.xml");
284 else if (l > 3 && !strcmp(".db", id + l - 3))
285 strcpy(r + l - 3, ".metainfo.xml");
292 endElement(struct solv_xmlparser *xmlp, int state, char *content)
294 struct parsedata *pd = xmlp->userdata;
295 Pool *pool = pd->pool;
296 Solvable *s = pd->solvable;
309 case STATE_APPLICATION:
311 s->arch = ARCH_NOARCH;
314 if ((!s->name || !pd->havesummary) && (pd->flags & APPDATA_CHECK_DESKTOP_FILE) != 0 && pd->desktop_file)
315 add_missing_tags_from_desktop_file(pd, s, pd->desktop_file);
316 if (!s->name && pd->desktop_file)
318 char *name = pool_tmpjoin(pool, "application:", pd->desktop_file, 0);
319 int l = strlen(name);
320 if (l > 8 && !strcmp(".desktop", name + l - 8))
322 s->name = pool_strn2id(pool, name, l, 1);
324 if (!s->requires && pd->owners)
328 for (i = 0; i < pd->owners->count; i++)
330 Solvable *os = pd->pool->solvables + pd->owners->elements[i];
331 s->requires = repo_addid_dep(pd->repo, s->requires, os->name, 0);
332 id = pool_str2id(pd->pool, pool_tmpjoin(pd->pool, "application-appdata(", pool_id2str(pd->pool, os->name), ")"), 1);
333 s->provides = repo_addid_dep(pd->repo, s->provides, id, 0);
336 if (!s->requires && (pd->desktop_file || pd->filename))
338 /* add appdata() link requires/provides */
339 const char *filename = pd->filename;
341 filename = guess_filename_from_id(pool, pd->desktop_file);
344 filename = pool_tmpjoin(pool, "application-appdata(", filename, ")");
345 s->requires = repo_addid_dep(pd->repo, s->requires, pool_str2id(pd->pool, filename + 12, 1), 0);
346 s->provides = repo_addid_dep(pd->repo, s->provides, pool_str2id(pd->pool, filename, 1), 0);
349 if (s->name && s->arch != ARCH_SRC && s->arch != ARCH_NOSRC)
350 s->provides = repo_addid_dep(pd->repo, s->provides, pool_rel2id(pd->pool, s->name, s->evr, REL_EQ, 1), 0);
352 pd->desktop_file = solv_free(pd->desktop_file);
355 pd->desktop_file = solv_strdup(content);
358 s->name = pool_str2id(pd->pool, pool_tmpjoin(pool, "application:", content, 0), 1);
361 repodata_add_poolstr_array(pd->data, pd->handle, SOLVABLE_LICENSE, content);
365 repodata_set_str(pd->data, pd->handle, SOLVABLE_SUMMARY, content);
368 repodata_set_str(pd->data, pd->handle, SOLVABLE_URL, content);
371 repodata_add_poolstr_array(pd->data, pd->handle, SOLVABLE_GROUP, content);
374 repodata_add_poolstr_array(pd->data, pd->handle, SOLVABLE_EXTENDS, content);
376 case STATE_DESCRIPTION:
379 /* strip trailing newlines */
380 int l = strlen(pd->description);
381 while (l && pd->description[l - 1] == '\n')
382 pd->description[--l] = 0;
383 repodata_set_str(pd->data, pd->handle, SOLVABLE_DESCRIPTION, pd->description);
387 content = wsstrip(pd);
388 pd->description = solv_dupappend(pd->description, content, "\n\n");
392 content = indent(pd, 4);
394 pd->description = solv_dupappend(pd->description, content, "\n");
398 content = indent(pd, 4);
399 if (++pd->licnt >= 10)
400 content[0] = '0' + (pd->licnt / 10) % 10;
401 content[1] = '0' + pd->licnt % 10;
403 pd->description = solv_dupappend(pd->description, content, "\n");
407 pd->description = solv_dupappend(pd->description, "\n", 0);
410 id = pool_str2id(pd->pool, content, 1);
411 s->requires = repo_addid_dep(pd->repo, s->requires, id, 0);
412 id = pool_str2id(pd->pool, pool_tmpjoin(pd->pool, "application-appdata(", content, ")"), 1);
413 s->provides = repo_addid_dep(pd->repo, s->provides, id, 0);
416 repodata_add_poolstr_array(pd->data, pd->handle, SOLVABLE_KEYWORDS, content);
424 repo_add_appdata_fn(Repo *repo, FILE *fp, int flags, const char *filename, Queue *owners)
429 data = repo_add_repodata(repo, flags);
430 memset(&pd, 0, sizeof(pd));
432 pd.pool = repo->pool;
435 pd.filename = filename;
438 solv_xmlparser_init(&pd.xmlp, stateswitches, &pd, startElement, endElement);
439 if (solv_xmlparser_parse(&pd.xmlp, fp) != SOLV_XMLPARSER_OK)
441 pool_debug(pd.pool, SOLV_ERROR, "repo_appdata: %s at line %u:%u\n", pd.xmlp.errstr, pd.xmlp.line, pd.xmlp.column);
443 pd.solvable = solvable_free(pd.solvable, 1);
445 solv_xmlparser_free(&pd.xmlp);
447 solv_free(pd.desktop_file);
448 solv_free(pd.description);
450 if (!(flags & REPO_NO_INTERNALIZE))
451 repodata_internalize(data);
457 repo_add_appdata(Repo *repo, FILE *fp, int flags)
459 return repo_add_appdata_fn(repo, fp, flags, 0, 0);
462 struct uninternalized_filelist_data {
468 search_uninternalized_filelist_cb(void *cbdata, Solvable *s, Repodata *data, Repokey *key, KeyValue *kv)
470 struct uninternalized_filelist_data *uf = cbdata;
474 if (key->type != REPOKEY_TYPE_DIRSTRARRAY || kv->id != uf->did)
478 if (l > 12 && strncmp(str + l - 12, ".appdata.xml", 12))
479 id = pool_str2id(data->repo->pool, str, 1);
480 else if (l > 13 && strncmp(str + l - 13, ".metainfo.xml", 13))
481 id = pool_str2id(data->repo->pool, str, 1);
484 queue_push2(uf->res, s - data->repo->pool->solvables, id);
489 search_uninternalized_filelist(Repo *repo, const char *dir, Queue *res)
491 Pool *pool = repo->pool;
493 struct uninternalized_filelist_data uf;
496 for (rdid = 1; rdid < repo->nrepodata; rdid++)
498 Repodata *data = repo_id2repodata(repo, rdid);
501 if (data->state == REPODATA_STUB)
503 if (!repodata_has_keyname(data, SOLVABLE_FILELIST))
505 did = repodata_str2dir(data, dir, 0);
509 for (p = data->start; p < data->end; p++)
511 if (p >= pool->nsolvables || pool->solvables[p].repo != repo)
513 repodata_search_uninternalized(data, p, SOLVABLE_FILELIST, 0, search_uninternalized_filelist_cb, &uf);
518 /* add all files ending in .appdata.xml */
520 repo_add_appdata_dir(Repo *repo, const char *appdatadir, int flags)
530 if (flags & APPDATA_SEARCH_UNINTERNALIZED_FILELIST)
531 search_uninternalized_filelist(repo, appdatadir, &flq);
532 data = repo_add_repodata(repo, flags);
533 if (flags & REPO_USE_ROOTDIR)
534 dirpath = pool_prepend_rootdir(repo->pool, appdatadir);
536 dirpath = solv_strdup(appdatadir);
537 if ((dir = opendir(dirpath)) != 0)
539 struct dirent *entry;
540 while ((entry = readdir(dir)))
544 int len = strlen(entry->d_name);
545 if (entry->d_name[0] == '.')
547 if (!(len > 12 && !strcmp(entry->d_name + len - 12, ".appdata.xml")) &&
548 !(len > 13 && !strcmp(entry->d_name + len - 13, ".metainfo.xml")))
550 n = pool_tmpjoin(repo->pool, dirpath, "/", entry->d_name);
554 pool_error(repo->pool, 0, "%s: %s", n, strerror(errno));
557 if (flags & APPDATA_SEARCH_UNINTERNALIZED_FILELIST)
559 Id id = pool_str2id(repo->pool, entry->d_name, 0);
564 for (i = 0; i < flq.count; i += 2)
565 if (flq.elements[i + 1] == id)
566 queue_push(&oq, flq.elements[i]);
569 repo_add_appdata_fn(repo, fp, flags | REPO_NO_INTERNALIZE | REPO_REUSE_REPODATA | APPDATA_CHECK_DESKTOP_FILE, entry->d_name, oq.count ? &oq : 0);
575 if (!(flags & REPO_NO_INTERNALIZE))
576 repodata_internalize(data);