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"
31 parse_deps(struct parsedata *pd, struct solv_jsonparser *jp, Offset *depp)
34 while (type > 0 && (type = jsonparser_parse(jp)) > 0 && type != JP_ARRAY_END)
36 if (type == JP_STRING)
38 Id id = pool_conda_matchspec(pd->pool, jp->value);
40 *depp = repo_addid_dep(pd->repo, *depp, id, 0);
43 type = jsonparser_skip(jp, type);
49 parse_otherdeps(struct parsedata *pd, struct solv_jsonparser *jp, Id handle, Id keyname)
52 while (type > 0 && (type = jsonparser_parse(jp)) > 0 && type != JP_ARRAY_END)
54 if (type == JP_STRING)
56 Id id = pool_conda_matchspec(pd->pool, jp->value);
58 repodata_add_idarray(pd->data, handle, keyname, id);
61 type = jsonparser_skip(jp, type);
67 parse_trackfeatures(struct parsedata *pd, struct solv_jsonparser *jp, Id handle)
70 while (type > 0 && (type = jsonparser_parse(jp)) > 0 && type != JP_ARRAY_END)
72 if (type == JP_STRING)
74 char *p = jp->value, *pe;
75 while (*p == ' ' || *p == '\t')
79 for (pe = p + strlen(p) - 1; pe > p; pe--)
80 if (*pe != ' ' && *pe != '\t')
82 repodata_add_idarray(pd->data, handle, SOLVABLE_TRACK_FEATURES, pool_strn2id(pd->pool, p, pe - p + 1, 1));
85 type = jsonparser_skip(jp, type);
91 swap_solvables(Repo *repo, Repodata *data, Id pa, Id pb)
93 Pool *pool = repo->pool;
96 tmp = pool->solvables[pa];
97 pool->solvables[pa] = pool->solvables[pb];
98 pool->solvables[pb] = tmp;
99 repodata_swap_attrs(data, pa, pb);
103 fn2data(struct parsedata *pd, const char *fn, Id *fntypep, int create)
105 size_t l = strlen(fn), extl = 0;
107 if (l > 6 && !strcmp(fn + l - 6, ".conda"))
109 else if (l > 8 && !strcmp(fn + l - 8, ".tar.bz2"))
113 fnid = stringpool_strn2id(&pd->fnpool, fn, l - extl, create);
116 if (fnid * 2 + 2 > pd->fndata.count)
117 queue_insertn(&pd->fndata, pd->fndata.count, fnid * 2 + 2 - pd->fndata.count, 0);
119 *fntypep = extl == 8 ? 1 : 2; /* 1: legacy .tar.bz2 2: .conda */
120 return pd->fndata.elements + 2 * fnid;
124 parse_package(struct parsedata *pd, struct solv_jsonparser *jp, char *kfn)
126 int type = JP_OBJECT;
127 Pool *pool= pd->pool;
128 Repodata *data = pd->data;
133 Id *fndata = 0, fntype = 0;
135 handle = repo_add_solvable(pd->repo);
136 s = pool_id2solvable(pool, handle);
137 while (type > 0 && (type = jsonparser_parse(jp)) > 0 && type != JP_OBJECT_END)
139 if (type == JP_STRING && !strcmp(jp->key, "build"))
140 repodata_add_poolstr_array(data, handle, SOLVABLE_BUILDFLAVOR, jp->value);
141 else if (type == JP_NUMBER && !strcmp(jp->key, "build_number"))
142 repodata_set_str(data, handle, SOLVABLE_BUILDVERSION, jp->value);
143 else if (type == JP_ARRAY && !strcmp(jp->key, "depends"))
144 type = parse_deps(pd, jp, &s->requires);
145 else if (type == JP_ARRAY && !strcmp(jp->key, "requires"))
146 type = parse_deps(pd, jp, &s->requires);
147 else if (type == JP_ARRAY && !strcmp(jp->key, "constrains"))
148 type = parse_otherdeps(pd, jp, handle, SOLVABLE_CONSTRAINS);
149 else if (type == JP_STRING && !strcmp(jp->key, "license"))
150 repodata_add_poolstr_array(data, handle, SOLVABLE_LICENSE, jp->value);
151 else if (type == JP_STRING && !strcmp(jp->key, "md5"))
152 repodata_set_checksum(data, handle, SOLVABLE_PKGID, REPOKEY_TYPE_MD5, jp->value);
153 else if (type == JP_STRING && !strcmp(jp->key, "sha256"))
154 repodata_set_checksum(data, handle, SOLVABLE_CHECKSUM, REPOKEY_TYPE_SHA256, jp->value);
155 else if (type == JP_STRING && !strcmp(jp->key, "name"))
156 s->name = pool_str2id(pool, jp->value, 1);
157 else if (type == JP_STRING && !strcmp(jp->key, "version"))
158 s->evr= pool_str2id(pool, jp->value, 1);
159 else if (type == JP_STRING && !strcmp(jp->key, "fn") && !fn)
160 fn = solv_strdup(jp->value);
161 else if (type == JP_STRING && !strcmp(jp->key, "subdir") && !subdir)
162 subdir = solv_strdup(jp->value);
163 else if (type == JP_NUMBER && !strcmp(jp->key, "size"))
164 repodata_set_num(data, handle, SOLVABLE_DOWNLOADSIZE, strtoull(jp->value, 0, 10));
165 else if (type == JP_NUMBER && !strcmp(jp->key, "timestamp"))
167 unsigned long long ts = strtoull(jp->value, 0, 10);
168 if (ts > 253402300799ULL)
170 repodata_set_num(data, handle, SOLVABLE_BUILDTIME, ts);
172 else if (type == JP_STRING && !strcmp(jp->key, "track_features"))
174 char *p = jp->value, *pe;
177 if (*p == ' ' || *p == '\t' || *p == ',')
180 while (*pe && *pe != ' ' && *pe != '\t' && *pe != ',')
182 repodata_add_idarray(data, handle, SOLVABLE_TRACK_FEATURES, pool_strn2id(pool, p, pe - p, 1));
186 else if (type == JP_ARRAY && !strcmp(jp->key, "track_features"))
187 type = parse_trackfeatures(pd, jp, handle);
189 type = jsonparser_skip(jp, type);
193 repodata_set_location(data, handle, 0, subdir, fn ? fn : kfn);
194 fndata = fn2data(pd, fn ? fn : kfn, &fntype, 1);
201 s->provides = repo_addid_dep(pd->repo, s->provides, pool_rel2id(pool, s->name, s->evr, REL_EQ, 1), 0);
205 /* deal with legacy package entries */
206 if (fndata[0] && fndata[0] > fntype)
208 /* ignore this package */
209 repo_free_solvable(pd->repo, handle, 1);
212 if (fndata[0] && fndata[0] < fntype)
214 /* replace old package */
215 swap_solvables(pd->repo, data, handle, fndata[1]);
216 repo_free_solvable(pd->repo, handle, 1);
226 parse_packages(struct parsedata *pd, struct solv_jsonparser *jp)
228 int type = JP_OBJECT;
229 while (type > 0 && (type = jsonparser_parse(jp)) > 0 && type != JP_OBJECT_END)
231 if (type == JP_OBJECT)
233 char *fn = solv_strdup(jp->key);
234 type = parse_package(pd, jp, fn);
238 type = jsonparser_skip(jp, type);
244 parse_packages2(struct parsedata *pd, struct solv_jsonparser *jp)
247 while (type > 0 && (type = jsonparser_parse(jp)) > 0 && type != JP_ARRAY_END)
249 if (type == JP_OBJECT)
250 type = parse_package(pd, jp, 0);
252 type = jsonparser_skip(jp, type);
258 parse_main(struct parsedata *pd, struct solv_jsonparser *jp)
260 int type = JP_OBJECT;
261 while (type > 0 && (type = jsonparser_parse(jp)) > 0 && type != JP_OBJECT_END)
263 if (type == JP_OBJECT && !strcmp("packages", jp->key))
264 type = parse_packages(pd, jp);
265 else if (type == JP_ARRAY && !strcmp("packages", jp->key))
266 type = parse_packages2(pd, jp);
267 else if (type == JP_OBJECT && !strcmp("packages.conda", jp->key))
268 type = parse_packages(pd, jp);
269 else if (type == JP_ARRAY && !strcmp("packages.conda", jp->key))
270 type = parse_packages2(pd, jp);
272 type = jsonparser_skip(jp, type);
278 repo_add_conda(Repo *repo, FILE *fp, int flags)
280 Pool *pool = repo->pool;
281 struct solv_jsonparser jp;
286 data = repo_add_repodata(repo, flags);
288 memset(&pd, 0, sizeof(pd));
292 stringpool_init_empty(&pd.fnpool);
293 queue_init(&pd.fndata);
295 jsonparser_init(&jp, fp);
296 if ((type = jsonparser_parse(&jp)) != JP_OBJECT)
297 ret = pool_error(pool, -1, "repository does not start with an object");
298 else if ((type = parse_main(&pd, &jp)) != JP_OBJECT_END)
299 ret = pool_error(pool, -1, "parse error line %d", jp.line);
300 jsonparser_free(&jp);
302 queue_free(&pd.fndata);
303 stringpool_free(&pd.fnpool);
304 if (!(flags & REPO_NO_INTERNALIZE))
305 repodata_internalize(data);