2 * Copyright (c) 2013, SUSE Inc.
4 * This program is licensed under the BSD license, read LICENSE.BSD
5 * for further information
11 #include <sys/types.h>
22 #include "repo_autopattern.h"
30 if (*p == '%' && p[1] && p[2])
32 int d1 = p[1], d2 = p[2];
33 if (d1 >= '0' && d1 <= '9')
35 else if (d1 >= 'a' && d1 <= 'f')
37 else if (d1 >= 'A' && d1 <= 'F')
41 if (d2 >= '0' && d2 <= '9')
43 else if (d2 >= 'a' && d2 <= 'f')
45 else if (d2 >= 'A' && d2 <= 'F')
49 if (d1 != -1 && d2 != -1)
62 datestr2timestamp(const char *date)
69 for (p = date; *p >= '0' && *p <= '9'; p++)
73 memset(&tm, 0, sizeof(tm));
74 p = strptime(date, "%F%T", &tm);
77 memset(&tm, 0, sizeof(tm));
78 p = strptime(date, "%F", &tm);
86 repo_add_autopattern(Repo *repo, int flags)
88 Pool *pool = repo->pool;
94 Id pattern_id, product_id;
95 Id autopattern_id = 0, autoproduct_id = 0;
103 if (repo == pool->installed)
104 flags |= ADD_NO_AUTOPRODUCTS; /* no auto products for installed repos */
106 pattern_id = pool_str2id(pool, "pattern()", 9);
107 product_id = pool_str2id(pool, "product()", 9);
108 FOR_REPO_SOLVABLES(repo, p, s)
110 const char *n = pool_id2str(pool, s->name);
113 if (!strncmp("pattern:", n, 8))
115 queue_push(&patq, p);
118 else if (!strncmp("product:", n, 8))
120 queue_push(&prdq, p);
126 Id prv, *prvp = repo->idarraydata + s->provides;
127 while ((prv = *prvp++) != 0) /* go through all provides */
130 Reldep *rd = GETRELDEP(pool, prv);
131 if (rd->flags != REL_EQ)
133 if (rd->name == pattern_id)
135 const char *evrstr = pool_id2str(pool, rd->evr);
136 if (evrstr[0] == '.')
138 queue_push2(&patq2, p, rd->evr);
141 if (rd->name == product_id)
143 queue_push2(&prdq2, p, rd->evr);
149 for (i = 0; i < patq2.count; i += 2)
155 unsigned long long num;
157 s = pool->solvables + patq2.elements[i];
158 /* construct new name */
159 newname = pool_tmpjoin(pool, "pattern:", pool_id2str(pool, patq2.elements[i + 1]), 0);
161 name = pool_str2id(pool, newname, 0);
164 /* check if we already have that pattern */
165 for (j = 0; j < patq.count; j++)
167 s2 = pool->solvables + patq.elements[j];
168 if (s2->name == name && s2->arch == s->arch && s2->evr == s->evr)
172 continue; /* yes, do not add again */
176 name = pool_str2id(pool, newname, 1);
179 repo_internalize(repo); /* to make that the lookups work */
180 data = repo_add_repodata(repo, flags);
182 s2 = pool_id2solvable(pool, repo_add_solvable(repo));
183 s = pool->solvables + patq2.elements[i]; /* re-calc pointer */
187 s2->vendor = s->vendor;
188 /* add link requires */
189 s2->requires = repo_addid_dep(repo, s2->requires, pool_rel2id(pool, s->name, s->evr, REL_EQ, 1) , 0);
190 /* add autopattern provides */
192 autopattern_id = pool_str2id(pool, "autopattern()", 1);
193 s2->provides = repo_addid_dep(repo, s2->provides, pool_rel2id(pool, autopattern_id, s->name, REL_EQ, 1), 0);
194 /* add self provides */
195 s2->provides = repo_addid_dep(repo, s2->provides, pool_rel2id(pool, s2->name, s2->evr, REL_EQ, 1), 0);
196 if ((num = solvable_lookup_num(s, SOLVABLE_INSTALLTIME, 0)) != 0)
197 repodata_set_num(data, s2 - pool->solvables, SOLVABLE_INSTALLTIME, num);
198 if ((num = solvable_lookup_num(s, SOLVABLE_BUILDTIME, 0)) != 0)
199 repodata_set_num(data, s2 - pool->solvables, SOLVABLE_BUILDTIME, num);
200 if ((str = solvable_lookup_str(s, SOLVABLE_SUMMARY)) != 0)
201 repodata_set_str(data, s2 - pool->solvables, SOLVABLE_SUMMARY, str);
202 if ((str = solvable_lookup_str(s, SOLVABLE_DESCRIPTION)) != 0)
203 repodata_set_str(data, s2 - pool->solvables, SOLVABLE_DESCRIPTION, str);
204 /* fill in stuff from provides */
205 prvp = repo->idarraydata + s->provides;
206 while ((prv = *prvp++) != 0) /* go through all provides */
211 Reldep *rd = GETRELDEP(pool, prv);
212 if (rd->flags != REL_EQ)
217 pn = pool_id2str(pool, prv);
218 if (strncmp("pattern-", pn, 8) != 0)
223 newname = pool_tmpjoin(pool, pool_id2str(pool, evr), 0, 0);
226 if (!strncmp(pn, "pattern-category(", 17) && evr)
231 if (l > 17 + 9 || pn[l - 1] != ')')
233 strncpy(lang, pn + 17, l - 17 - 1);
234 lang[l - 17 - 1] = 0;
235 langtag = SOLVABLE_CATEGORY;
236 if (*lang && strcmp(lang, "en") != 0)
237 langtag = pool_id2langid(pool, SOLVABLE_CATEGORY, lang, 1);
238 if (newname[solv_validutf8(newname)] == 0)
239 repodata_set_str(data, s2 - pool->solvables, langtag, newname);
242 char *ustr = solv_latin1toutf8(newname);
243 repodata_set_str(data, s2 - pool->solvables, langtag, ustr);
247 else if (!strcmp(pn, "pattern-includes()") && evr)
248 repodata_add_poolstr_array(data, s2 - pool->solvables, SOLVABLE_INCLUDES, pool_tmpjoin(pool, "pattern:", newname, 0));
249 else if (!strcmp(pn, "pattern-extends()") && evr)
250 repodata_add_poolstr_array(data, s2 - pool->solvables, SOLVABLE_EXTENDS, pool_tmpjoin(pool, "pattern:", newname, 0));
251 else if (!strcmp(pn, "pattern-icon()") && evr)
252 repodata_set_str(data, s2 - pool->solvables, SOLVABLE_ICON, newname);
253 else if (!strcmp(pn, "pattern-order()") && evr)
254 repodata_set_str(data, s2 - pool->solvables, SOLVABLE_ORDER, newname);
255 else if (!strcmp(pn, "pattern-visible()") && !evr)
256 repodata_set_void(data, s2 - pool->solvables, SOLVABLE_ISVISIBLE);
262 if ((flags & ADD_NO_AUTOPRODUCTS) != 0)
265 for (i = 0; i < prdq2.count; i += 2)
269 Id name, evr = 0, prv, *prvp;
271 unsigned long long num;
273 s = pool->solvables + prdq2.elements[i];
274 /* construct new name */
275 newname = pool_tmpjoin(pool, "product(", pool_id2str(pool, prdq2.elements[i + 1]), ")");
277 name = pool_str2id(pool, newname, 0);
279 continue; /* must have it in provides! */
280 prvp = repo->idarraydata + s->provides;
281 while ((prv = *prvp++) != 0) /* go through all provides */
285 Reldep *rd = GETRELDEP(pool, prv);
286 if (rd->name == name && rd->flags == REL_EQ)
294 continue; /* not found in provides */
295 newname = pool_tmpjoin(pool, "product:", pool_id2str(pool, prdq2.elements[i + 1]), 0);
297 name = pool_str2id(pool, newname, 0);
300 /* check if we already have that product */
301 for (j = 0; j < prdq.count; j++)
303 s2 = pool->solvables + prdq.elements[j];
304 if (s2->name == name && s2->arch == s->arch && s2->evr == evr)
308 continue; /* yes, do not add again */
312 name = pool_str2id(pool, newname, 1);
315 repo_internalize(repo); /* to make that the lookups work */
316 data = repo_add_repodata(repo, flags);
318 if ((num = solvable_lookup_num(s, SOLVABLE_INSTALLTIME, 0)) != 0)
319 continue; /* eek, not for installed packages, please! */
320 s2 = pool_id2solvable(pool, repo_add_solvable(repo));
321 s = pool->solvables + prdq2.elements[i]; /* re-calc pointer */
325 s2->vendor = s->vendor;
326 /* add link requires */
327 s2->requires = repo_addid_dep(repo, s2->requires, prv, 0);
329 autoproduct_id = pool_str2id(pool, "autoproduct()", 1);
330 s2->provides = repo_addid_dep(repo, s2->provides, pool_rel2id(pool, autoproduct_id, s->name, REL_EQ, 1), 0);
331 /* add self provides */
332 s2->provides = repo_addid_dep(repo, s2->provides, pool_rel2id(pool, s2->name, s2->evr, REL_EQ, 1), 0);
333 if ((num = solvable_lookup_num(s, SOLVABLE_BUILDTIME, 0)) != 0)
334 repodata_set_num(data, s2 - pool->solvables, SOLVABLE_BUILDTIME, num);
335 if ((str = solvable_lookup_str(s, SOLVABLE_SUMMARY)) != 0)
336 repodata_set_str(data, s2 - pool->solvables, SOLVABLE_SUMMARY, str);
337 if ((str = solvable_lookup_str(s, SOLVABLE_DESCRIPTION)) != 0)
338 repodata_set_str(data, s2 - pool->solvables, SOLVABLE_DESCRIPTION, str);
339 if ((str = solvable_lookup_str(s, SOLVABLE_DISTRIBUTION)) != 0)
340 repodata_set_str(data, s2 - pool->solvables, SOLVABLE_DISTRIBUTION, str);
341 /* fill in stuff from provides */
342 prvp = repo->idarraydata + s->provides;
343 while ((prv = *prvp++) != 0) /* go through all provides */
348 Reldep *rd = GETRELDEP(pool, prv);
349 if (rd->flags != REL_EQ)
354 pn = pool_id2str(pool, prv);
355 if (strncmp("product-", pn, 8) != 0)
360 newname = pool_tmpjoin(pool, pool_id2str(pool, evr), 0, 0);
363 if (!strcmp(pn, "product-label()") && evr)
364 repodata_set_str(data, s2 - pool->solvables, PRODUCT_SHORTLABEL, newname);
365 else if (!strcmp(pn, "product-type()") && evr)
366 repodata_set_str(data, s2 - pool->solvables, PRODUCT_TYPE, newname);
367 else if (!strcmp(pn, "product-cpeid()") && evr)
368 repodata_set_str(data, s2 - pool->solvables, SOLVABLE_CPEID, newname);
369 else if (!strcmp(pn, "product-flags()") && evr)
370 repodata_add_poolstr_array(data, s2 - pool->solvables, PRODUCT_FLAGS, newname);
371 else if (!strcmp(pn, "product-updates-repoid()") && evr)
373 Id h = repodata_new_handle(data);
374 repodata_set_str(data, h, PRODUCT_UPDATES_REPOID, newname);
375 repodata_add_flexarray(data, s2 - pool->solvables, PRODUCT_UPDATES, h);
377 else if (!strcmp(pn, "product-endoflife()") && evr)
379 time_t t = datestr2timestamp(newname);
381 repodata_set_num(data, s2 - pool->solvables, PRODUCT_ENDOFLIFE, t);
383 else if (!strncmp(pn, "product-url(", 12) && evr && pn[12] && pn[13] && strlen(pn + 12) < 32)
386 strcpy(type, pn + 12);
387 type[strlen(type) - 1] = 0; /* closing ) */
388 repodata_add_poolstr_array(data, s2 - pool->solvables, PRODUCT_URL_TYPE, type);
389 repodata_add_poolstr_array(data, s2 - pool->solvables, PRODUCT_URL, newname);
396 if (data && !(flags & REPO_NO_INTERNALIZE))
397 repodata_internalize(data);
398 else if (!data && !(flags & REPO_NO_INTERNALIZE))
399 repo_internalize(repo);