2 * Copyright (c) 2007, Novell Inc.
4 * This program is licensed under the BSD license, read LICENSE.BSD
5 * for further information
21 #include "selection.h"
26 str2archid(Pool *pool, const char *arch)
31 id = pool_str2id(pool, arch, 0);
32 if (!id || id == ARCH_SRC || id == ARCH_NOSRC || id == ARCH_NOARCH)
34 if (pool->id2arch && (id > pool->lastarch || !pool->id2arch[id]))
40 selection_prune(Pool *pool, Queue *selection)
44 for (i = j = 0; i < selection->count; i += 2)
46 Id select = selection->elements[i] & SOLVER_SELECTMASK;
48 if (select == SOLVER_SOLVABLE_ALL)
50 else if (select == SOLVER_SOLVABLE_REPO)
53 Repo *repo = pool_id2repo(pool, selection->elements[i + 1]);
55 FOR_REPO_SOLVABLES(repo, p, s)
60 FOR_JOB_SELECT(p, pp, select, selection->elements[i + 1])
65 selection->elements[j] = selection->elements[i];
66 selection->elements[j + 1] = selection->elements[i + 1];
69 queue_truncate(selection, j);
74 selection_solvables_sortcmp(const void *ap, const void *bp, void *dp)
76 return *(const Id *)ap - *(const Id *)bp;
80 selection_solvables(Pool *pool, Queue *selection, Queue *pkgs)
85 for (i = 0; i < selection->count; i += 2)
87 Id select = selection->elements[i] & SOLVER_SELECTMASK;
88 if (select == SOLVER_SOLVABLE_ALL)
93 if (select == SOLVER_SOLVABLE_REPO)
96 Repo *repo = pool_id2repo(pool, selection->elements[i + 1]);
98 FOR_REPO_SOLVABLES(repo, p, s)
103 FOR_JOB_SELECT(p, pp, select, selection->elements[i + 1])
110 solv_sort(pkgs->elements, pkgs->count, sizeof(Id), selection_solvables_sortcmp, NULL);
111 lastid = pkgs->elements[0];
112 for (i = j = 1; i < pkgs->count; i++)
113 if (pkgs->elements[i] != lastid)
114 pkgs->elements[j++] = lastid = pkgs->elements[i];
115 queue_truncate(pkgs, j);
119 selection_flatten(Pool *pool, Queue *selection)
123 if (selection->count <= 1)
125 for (i = 0; i < selection->count; i += 2)
126 if ((selection->elements[i] & SOLVER_SELECTMASK) == SOLVER_SOLVABLE_ALL)
128 selection->elements[0] = selection->elements[i];
129 selection->elements[1] = selection->elements[i + 1];
130 queue_truncate(selection, 2);
134 selection_solvables(pool, selection, &q);
137 queue_empty(selection);
140 queue_truncate(selection, 2);
143 selection->elements[0] = SOLVER_SOLVABLE_ONE_OF;
144 selection->elements[1] = pool_queuetowhatprovides(pool, &q);
148 selection->elements[0] = SOLVER_SOLVABLE | SOLVER_NOAUTOSET;
149 selection->elements[1] = q.elements[0];
154 selection_limit_rel(Pool *pool, Queue *selection, Id relflags, Id relevr)
157 for (i = j = 0; i < selection->count; i += 2)
159 Id select = selection->elements[i] & SOLVER_SELECTMASK;
160 if (select != SOLVER_SOLVABLE_NAME && select != SOLVER_SOLVABLE_PROVIDES)
161 continue; /* actually internal error */
162 selection->elements[i + 1] = pool_rel2id(pool, selection->elements[i + 1], relevr, relflags, 1);
163 if (relflags == REL_ARCH)
164 selection->elements[i] |= SOLVER_SETARCH;
165 if (relflags == REL_EQ && select == SOLVER_SOLVABLE_NAME && selection->elements[i])
167 if (pool->disttype == DISTTYPE_DEB)
168 selection->elements[i] |= SOLVER_SETEVR; /* debian can't match version only like rpm */
170 selection->elements[i] |= strchr(pool_id2str(pool, relevr), '-') != 0 ? SOLVER_SETEVR : SOLVER_SETEV;
173 selection_prune(pool, selection);
177 selection_depglob(Pool *pool, Queue *selection, const char *name, int flags)
184 if (!(flags & (SELECTION_NAME|SELECTION_PROVIDES)))
187 if ((flags & SELECTION_INSTALLED_ONLY) != 0 && !pool->installed)
190 if (!(flags & SELECTION_NOCASE))
192 id = pool_str2id(pool, name, 0);
195 FOR_PROVIDES(p, pp, id)
197 Solvable *s = pool->solvables + p;
198 if ((flags & SELECTION_INSTALLED_ONLY) != 0 && s->repo != pool->installed)
201 if (s->name == id && (flags & SELECTION_NAME) != 0)
203 queue_push2(selection, SOLVER_SOLVABLE_NAME, id);
204 return SELECTION_NAME;
207 if (match && (flags & SELECTION_PROVIDES) != 0)
209 queue_push2(selection, SOLVER_SOLVABLE_PROVIDES, id);
210 return SELECTION_PROVIDES;
215 if ((flags & SELECTION_GLOB) != 0 && strpbrk(name, "[*?") != 0)
218 if (!doglob && !(flags & SELECTION_NOCASE))
221 if (doglob && (flags & SELECTION_NOCASE) != 0)
222 globflags = FNM_CASEFOLD;
224 #if 0 /* doesn't work with selection_limit_rel yet */
225 if (doglob && !strcmp(name, "*") && (flags & SELECTION_FLAT) != 0)
227 /* can't do this for SELECTION_PROVIDES, as src rpms don't provide anything */
228 if ((flags & SELECTION_NAME) != 0)
230 queue_push2(selection, SOLVER_SOLVABLE_ALL, 0);
231 return SELECTION_NAME;
236 if ((flags & SELECTION_NAME) != 0)
238 /* looks like a name glob. hard work. */
239 for (p = 1; p < pool->nsolvables; p++)
241 Solvable *s = pool->solvables + p;
242 if (!s->repo || !pool_installable(pool, s))
244 if ((flags & SELECTION_INSTALLED_ONLY) != 0 && s->repo != pool->installed)
247 if ((doglob ? fnmatch(name, pool_id2str(pool, id), globflags) : strcasecmp(name, pool_id2str(pool, id))) == 0)
249 for (i = 0; i < selection->count; i += 2)
250 if (selection->elements[i] == SOLVER_SOLVABLE_NAME && selection->elements[i + 1] == id)
252 if (i == selection->count)
253 queue_push2(selection, SOLVER_SOLVABLE_NAME, id);
258 return SELECTION_NAME;
260 if ((flags & SELECTION_PROVIDES))
262 /* looks like a dep glob. really hard work. */
263 for (id = 1; id < pool->ss.nstrings; id++)
265 if (!pool->whatprovides[id])
267 if ((doglob ? fnmatch(name, pool_id2str(pool, id), globflags) : strcasecmp(name, pool_id2str(pool, id))) == 0)
269 if ((flags & SELECTION_INSTALLED_ONLY) != 0)
271 FOR_PROVIDES(p, pp, id)
272 if (pool->solvables[p].repo == pool->installed)
277 queue_push2(selection, SOLVER_SOLVABLE_PROVIDES, id);
282 return SELECTION_PROVIDES;
288 selection_depglob_arch(Pool *pool, Queue *selection, const char *name, int flags)
294 if ((ret = selection_depglob(pool, selection, name, flags)) != 0)
296 /* check if theres an .arch suffix */
297 if ((r = strrchr(name, '.')) != 0 && r[1] && (archid = str2archid(pool, r + 1)) != 0)
299 char *rname = solv_strdup(name);
301 if ((ret = selection_depglob(pool, selection, rname, flags)) != 0)
303 selection_limit_rel(pool, selection, REL_ARCH, archid);
313 selection_filelist(Pool *pool, Queue *selection, const char *name, int flags)
319 type = !(flags & SELECTION_GLOB) || strpbrk(name, "[*?") == 0 ? SEARCH_STRING : SEARCH_GLOB;
320 if ((flags & SELECTION_NOCASE) != 0)
321 type |= SEARCH_NOCASE;
323 dataiterator_init(&di, pool, flags & SELECTION_INSTALLED_ONLY ? pool->installed : 0, 0, SOLVABLE_FILELIST, name, type|SEARCH_FILES|SEARCH_COMPLETE_FILELIST);
324 while (dataiterator_step(&di))
326 Solvable *s = pool->solvables + di.solvid;
327 if (!s->repo || !pool_installable(pool, s))
329 queue_push(&q, di.solvid);
330 dataiterator_skip_solvable(&di);
332 dataiterator_free(&di);
336 queue_push2(selection, SOLVER_SOLVABLE_ONE_OF, pool_queuetowhatprovides(pool, &q));
338 queue_push2(selection, SOLVER_SOLVABLE | SOLVER_NOAUTOSET, q.elements[0]);
340 return SELECTION_FILELIST;
344 selection_rel(Pool *pool, Queue *selection, const char *name, int flags)
349 /* relation case, support:
353 rname = solv_strdup(name);
354 if ((r = strpbrk(rname, "<=>")) != 0)
356 int nend = r - rname;
368 while (*r && *r == ' ' && *r == '\t')
370 while (nend && (rname[nend - 1] == ' ' || rname[nend -1 ] == '\t'))
379 if ((ret = selection_depglob_arch(pool, selection, rname, flags)) != 0)
382 selection_limit_rel(pool, selection, rflags, pool_str2id(pool, r, 1));
391 selection_nevra(Pool *pool, Queue *selection, const char *name, int flags)
393 char *rname, *r, *r2;
399 * nameglob-version.arch
400 * nameglob-version-release
401 * nameglob-version-release.arch
403 flags |= SELECTION_NAME;
404 flags &= ~SELECTION_PROVIDES;
406 if (pool->disttype == DISTTYPE_DEB)
408 if ((r = strchr(name, '_')) == 0)
410 rname = solv_strdup(name); /* so we can modify it */
411 r = rname + (r - name);
413 if ((ret = selection_depglob(pool, selection, rname, flags)) == 0)
418 /* is there a vaild arch? */
419 if ((r2 = strchr(r, '_')) != 0 && r[1] && (archid = str2archid(pool, r + 1)) != 0)
421 *r2 = 0; /* split off */
422 selection_limit_rel(pool, selection, REL_ARCH, archid);
424 selection_limit_rel(pool, selection, REL_EQ, pool_str2id(pool, r, 1));
429 if ((r = strrchr(name, '-')) == 0)
431 rname = solv_strdup(name); /* so we can modify it */
432 r = rname + (r - name);
434 /* try with just the version */
435 if ((ret = selection_depglob(pool, selection, rname, flags)) == 0)
437 /* no luck, try with version-release */
438 if ((r2 = strrchr(rname, '-')) == 0)
446 if ((ret = selection_depglob(pool, selection, rname, flags)) == 0)
452 /* we now know the name, check if we need to split of the arch */
454 if ((r2 = strrchr(r, '.')) != 0 && r2[1] && (archid = str2archid(pool, r2 + 1)) != 0)
456 /* found valid arch, split it off */
458 selection_limit_rel(pool, selection, REL_ARCH, archid);
460 selection_limit_rel(pool, selection, REL_EQ, pool_str2id(pool, r, 1));
466 selection_make(Pool *pool, Queue *selection, const char *name, int flags)
471 queue_empty(selection);
472 if (*name == '/' && (flags & SELECTION_FILELIST))
473 ret = selection_filelist(pool, selection, name, flags);
474 if (!ret && (r = strpbrk(name, "<=>")) != 0)
475 ret = selection_rel(pool, selection, name, flags);
477 ret = selection_depglob_arch(pool, selection, name, flags);
478 if (!ret && (flags & SELECTION_NAME) != 0)
479 ret = selection_nevra(pool, selection, name, flags);
480 if (ret && (flags & SELECTION_FLAT) != 0)
481 selection_flatten(pool, selection);
486 selection_limit(Pool *pool, Queue *sel1, Queue *sel2)
494 if (!sel1->count || !sel2->count)
499 if (sel1->count == 2 && (sel1->elements[0] & SOLVER_SELECTMASK) == SOLVER_SOLVABLE_ALL)
501 /* XXX: not 100% correct, but very useful */
503 queue_init_clone(sel1, sel2);
507 map_init(&m2, pool->nsolvables);
508 for (i = 0; i < sel2->count; i += 2)
510 Id select = sel2->elements[i] & SOLVER_SELECTMASK;
511 if (select == SOLVER_SOLVABLE_ALL)
513 if (select == SOLVER_SOLVABLE_REPO)
516 Repo *repo = pool_id2repo(pool, sel2->elements[i + 1]);
518 FOR_REPO_SOLVABLES(repo, p, s)
523 FOR_JOB_SELECT(p, pp, select, sel2->elements[i + 1])
527 if (sel2->count == 2) /* XXX: AND all setmasks instead? */
528 setflags = sel2->elements[0] & SOLVER_SETMASK & ~SOLVER_NOAUTOSET;
529 for (i = j = 0; i < sel1->count; i += 2)
531 Id select = sel1->elements[i] & SOLVER_SELECTMASK;
534 if (select == SOLVER_SOLVABLE_ALL)
536 FOR_POOL_SOLVABLES(p)
544 else if (select == SOLVER_SOLVABLE_REPO)
547 Repo *repo = pool_id2repo(pool, sel1->elements[i + 1]);
549 FOR_REPO_SOLVABLES(repo, p, s)
559 FOR_JOB_SELECT(p, pp, select, sel1->elements[i + 1])
562 queue_pushunique(&q1, p);
571 sel1->elements[j] = sel1->elements[i] | setflags;
572 sel1->elements[j + 1] = sel1->elements[i + 1];
574 else if (q1.count > 1)
576 sel1->elements[j] = (sel1->elements[i] & ~SOLVER_SELECTMASK) | SOLVER_SOLVABLE_ONE_OF | setflags;
577 sel1->elements[j + 1] = pool_queuetowhatprovides(pool, &q1);
581 sel1->elements[j] = (sel1->elements[i] & ~SOLVER_SELECTMASK) | SOLVER_SOLVABLE | SOLVER_NOAUTOSET | setflags;
582 sel1->elements[j + 1] = q1.elements[0];
586 queue_truncate(sel1, j);
590 selection_add(Pool *pool, Queue *sel1, Queue *sel2)
593 for (i = 0; i < sel2->count; i++)
594 queue_push(sel1, sel2->elements[i]);