2 * Copyright (c) 2019, SUSE LLC.
4 * This program is licensed under the BSD license, read LICENSE.BSD
5 * for further information
17 #include "solv_jsonparser.h"
19 #include "repo_conda.h"
28 parse_deps(struct parsedata *pd, struct solv_jsonparser *jp, Offset *depp)
31 while (type > 0 && (type = jsonparser_parse(jp)) > 0 && type != JP_ARRAY_END)
33 if (type == JP_STRING)
35 Id id = pool_conda_matchspec(pd->pool, jp->value);
37 *depp = repo_addid_dep(pd->repo, *depp, id, 0);
40 type = jsonparser_skip(jp, type);
46 parse_package(struct parsedata *pd, struct solv_jsonparser *jp, char *kfn)
50 Repodata *data = pd->data;
52 Id handle = repo_add_solvable(pd->repo);
53 s = pool_id2solvable(pool, handle);
57 while (type > 0 && (type = jsonparser_parse(jp)) > 0 && type != JP_OBJECT_END)
59 if (type == JP_STRING && !strcmp(jp->key, "build"))
60 repodata_add_poolstr_array(data, handle, SOLVABLE_BUILDFLAVOR, jp->value);
61 else if (type == JP_NUMBER && !strcmp(jp->key, "build_number"))
62 repodata_set_str(data, handle, SOLVABLE_BUILDVERSION, jp->value);
63 else if (type == JP_ARRAY && !strcmp(jp->key, "depends"))
64 type = parse_deps(pd, jp, &s->requires);
65 else if (type == JP_ARRAY && !strcmp(jp->key, "requires"))
66 type = parse_deps(pd, jp, &s->requires);
67 else if (type == JP_STRING && !strcmp(jp->key, "license"))
68 repodata_add_poolstr_array(data, handle, SOLVABLE_LICENSE, jp->value);
69 else if (type == JP_STRING && !strcmp(jp->key, "md5"))
70 repodata_set_checksum(data, handle, SOLVABLE_PKGID, REPOKEY_TYPE_MD5, jp->value);
71 else if (type == JP_STRING && !strcmp(jp->key, "sha256"))
72 repodata_set_checksum(data, handle, SOLVABLE_CHECKSUM, REPOKEY_TYPE_SHA256, jp->value);
73 else if (type == JP_STRING && !strcmp(jp->key, "name"))
74 s->name = pool_str2id(pool, jp->value, 1);
75 else if (type == JP_STRING && !strcmp(jp->key, "version"))
76 s->evr= pool_str2id(pool, jp->value, 1);
77 else if (type == JP_STRING && !strcmp(jp->key, "fn") && !fn)
78 fn = solv_strdup(jp->value);
79 else if (type == JP_STRING && !strcmp(jp->key, "subdir") && !subdir)
80 subdir = solv_strdup(jp->value);
81 else if (type == JP_NUMBER && !strcmp(jp->key, "size"))
82 repodata_set_num(data, handle, SOLVABLE_DOWNLOADSIZE, strtoull(jp->value, 0, 10));
83 else if (type == JP_NUMBER && !strcmp(jp->key, "timestamp"))
85 unsigned long long ts = strtoull(jp->value, 0, 10);
86 if (ts > 253402300799ULL)
88 repodata_set_num(data, handle, SOLVABLE_BUILDTIME, ts);
91 type = jsonparser_skip(jp, type);
94 repodata_set_location(data, handle, 0, subdir, fn ? fn : kfn);
100 s->provides = repo_addid_dep(pd->repo, s->provides, pool_rel2id(pool, s->name, s->evr, REL_EQ, 1), 0);
105 parse_packages(struct parsedata *pd, struct solv_jsonparser *jp)
107 int type = JP_OBJECT;
108 while (type > 0 && (type = jsonparser_parse(jp)) > 0 && type != JP_OBJECT_END)
110 if (type == JP_OBJECT)
112 char *fn = solv_strdup(jp->key);
113 type = parse_package(pd, jp, fn);
117 type = jsonparser_skip(jp, type);
123 parse_packages2(struct parsedata *pd, struct solv_jsonparser *jp)
126 while (type > 0 && (type = jsonparser_parse(jp)) > 0 && type != JP_ARRAY_END)
128 if (type == JP_OBJECT)
129 type = parse_package(pd, jp, 0);
131 type = jsonparser_skip(jp, type);
137 parse_main(struct parsedata *pd, struct solv_jsonparser *jp)
139 int type = JP_OBJECT;
140 while (type > 0 && (type = jsonparser_parse(jp)) > 0 && type != JP_OBJECT_END)
142 if (type == JP_OBJECT && !strcmp("packages", jp->key))
143 type = parse_packages(pd, jp);
144 if (type == JP_ARRAY && !strcmp("packages", jp->key))
145 type = parse_packages2(pd, jp);
147 type = jsonparser_skip(jp, type);
153 repo_add_conda(Repo *repo, FILE *fp, int flags)
155 Pool *pool = repo->pool;
156 struct solv_jsonparser jp;
161 data = repo_add_repodata(repo, flags);
163 memset(&pd, 0, sizeof(pd));
168 jsonparser_init(&jp, fp);
169 if ((type = jsonparser_parse(&jp)) != JP_OBJECT)
170 ret = pool_error(pool, -1, "repository does not start with an object");
171 else if ((type = parse_main(&pd, &jp)) != JP_OBJECT_END)
172 ret = pool_error(pool, -1, "parse error line %d", jp.line);
173 jsonparser_free(&jp);
175 if (!(flags & REPO_NO_INTERNALIZE))
176 repodata_internalize(data);