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 Id id = selection->elements[i + 1];
161 if (select != SOLVER_SOLVABLE_NAME && select != SOLVER_SOLVABLE_PROVIDES)
162 continue; /* actually internal error */
163 if (relflags == REL_ARCH && (relevr == ARCH_SRC || relevr == ARCH_NOSRC) && ISRELDEP(id))
165 Reldep *rd = GETRELDEP(pool, id);
166 if (rd->flags == REL_ARCH && rd->evr == ARCH_SRC)
169 selection->elements[i + 1] = pool_rel2id(pool, id, relevr, relflags, 1);
170 if (relflags == REL_ARCH)
171 selection->elements[i] |= SOLVER_SETARCH;
172 if (relflags == REL_EQ && select == SOLVER_SOLVABLE_NAME && selection->elements[i])
174 if (pool->disttype == DISTTYPE_DEB)
175 selection->elements[i] |= SOLVER_SETEVR; /* debian can't match version only like rpm */
177 selection->elements[i] |= strchr(pool_id2str(pool, relevr), '-') != 0 ? SOLVER_SETEVR : SOLVER_SETEV;
180 selection_prune(pool, selection);
184 selection_depglob(Pool *pool, Queue *selection, const char *name, int flags)
191 if ((flags & SELECTION_SOURCE) != 0)
192 flags &= ~SELECTION_PROVIDES; /* sources don't provide anything */
194 if (!(flags & (SELECTION_NAME|SELECTION_PROVIDES)))
197 if ((flags & SELECTION_INSTALLED_ONLY) != 0 && !pool->installed)
200 if (!(flags & SELECTION_NOCASE))
202 id = pool_str2id(pool, name, 0);
205 if ((flags & SELECTION_SOURCE) != 0 && (flags & SELECTION_NAME) != 0)
207 /* src rpms don't have provides, so we must check every solvable */
208 FOR_PROVIDES(p, pp, id) /* try fast path first */
210 Solvable *s = pool->solvables + p;
213 if ((flags & SELECTION_INSTALLED_ONLY) != 0 && s->repo != pool->installed)
215 id = pool_rel2id(pool, id, ARCH_SRC, REL_ARCH, 1);
216 queue_push2(selection, SOLVER_SOLVABLE_NAME, id);
217 return SELECTION_NAME;
220 FOR_POOL_SOLVABLES(p) /* slow path */
222 Solvable *s = pool->solvables + p;
223 if (s->name == id && (s->arch == ARCH_SRC || s->arch == ARCH_NOSRC))
225 if ((flags & SELECTION_INSTALLED_ONLY) != 0 && s->repo != pool->installed)
226 continue; /* just in case... src rpms can't be installed */
227 id = pool_rel2id(pool, id, ARCH_SRC, REL_ARCH, 1);
228 queue_push2(selection, SOLVER_SOLVABLE_NAME, id);
229 return SELECTION_NAME;
233 FOR_PROVIDES(p, pp, id)
235 Solvable *s = pool->solvables + p;
236 if ((flags & SELECTION_INSTALLED_ONLY) != 0 && s->repo != pool->installed)
239 if (s->name == id && (flags & SELECTION_NAME) != 0)
241 if ((flags & SELECTION_SOURCE) != 0)
242 id = pool_rel2id(pool, id, ARCH_SRC, REL_ARCH, 1);
243 queue_push2(selection, SOLVER_SOLVABLE_NAME, id);
244 return SELECTION_NAME;
247 if (match && (flags & SELECTION_PROVIDES) != 0)
249 queue_push2(selection, SOLVER_SOLVABLE_PROVIDES, id);
250 return SELECTION_PROVIDES;
255 if ((flags & SELECTION_GLOB) != 0 && strpbrk(name, "[*?") != 0)
258 if (!doglob && !(flags & SELECTION_NOCASE))
261 if (doglob && (flags & SELECTION_NOCASE) != 0)
262 globflags = FNM_CASEFOLD;
264 #if 0 /* doesn't work with selection_limit_rel yet */
265 if (doglob && !strcmp(name, "*") && (flags & SELECTION_FLAT) != 0)
267 /* can't do this for SELECTION_PROVIDES, as src rpms don't provide anything */
268 if ((flags & SELECTION_NAME) != 0)
270 queue_push2(selection, SOLVER_SOLVABLE_ALL, 0);
271 return SELECTION_NAME;
276 if ((flags & SELECTION_NAME) != 0)
278 /* looks like a name glob. hard work. */
279 FOR_POOL_SOLVABLES(p)
281 Solvable *s = pool->solvables + p;
282 if (!pool_installable(pool, s))
283 if (!(flags & SELECTION_SOURCE) || (s->arch != ARCH_SRC && s->arch != ARCH_NOSRC))
285 if ((flags & SELECTION_INSTALLED_ONLY) != 0 && s->repo != pool->installed)
288 if ((doglob ? fnmatch(name, pool_id2str(pool, id), globflags) : strcasecmp(name, pool_id2str(pool, id))) == 0)
290 if ((flags & SELECTION_SOURCE) != 0)
291 id = pool_rel2id(pool, id, ARCH_SRC, REL_ARCH, 1);
292 /* queue_pushunique2 */
293 for (i = 0; i < selection->count; i += 2)
294 if (selection->elements[i] == SOLVER_SOLVABLE_NAME && selection->elements[i + 1] == id)
296 if (i == selection->count)
297 queue_push2(selection, SOLVER_SOLVABLE_NAME, id);
302 return SELECTION_NAME;
304 if ((flags & SELECTION_PROVIDES))
306 /* looks like a dep glob. really hard work. */
307 for (id = 1; id < pool->ss.nstrings; id++)
309 if (!pool->whatprovides[id])
311 if ((doglob ? fnmatch(name, pool_id2str(pool, id), globflags) : strcasecmp(name, pool_id2str(pool, id))) == 0)
313 if ((flags & SELECTION_INSTALLED_ONLY) != 0)
315 FOR_PROVIDES(p, pp, id)
316 if (pool->solvables[p].repo == pool->installed)
321 queue_push2(selection, SOLVER_SOLVABLE_PROVIDES, id);
326 return SELECTION_PROVIDES;
332 selection_depglob_arch(Pool *pool, Queue *selection, const char *name, int flags)
338 if ((ret = selection_depglob(pool, selection, name, flags)) != 0)
340 /* check if theres an .arch suffix */
341 if ((r = strrchr(name, '.')) != 0 && r[1] && (archid = str2archid(pool, r + 1)) != 0)
343 char *rname = solv_strdup(name);
345 if (archid == ARCH_SRC || archid == ARCH_NOSRC)
346 flags |= SELECTION_SOURCE;
347 if ((ret = selection_depglob(pool, selection, rname, flags)) != 0)
349 selection_limit_rel(pool, selection, REL_ARCH, archid);
359 selection_filelist(Pool *pool, Queue *selection, const char *name, int flags)
365 type = !(flags & SELECTION_GLOB) || strpbrk(name, "[*?") == 0 ? SEARCH_STRING : SEARCH_GLOB;
366 if ((flags & SELECTION_NOCASE) != 0)
367 type |= SEARCH_NOCASE;
369 dataiterator_init(&di, pool, flags & SELECTION_INSTALLED_ONLY ? pool->installed : 0, 0, SOLVABLE_FILELIST, name, type|SEARCH_FILES|SEARCH_COMPLETE_FILELIST);
370 while (dataiterator_step(&di))
372 Solvable *s = pool->solvables + di.solvid;
375 if (!pool_installable(pool, s))
376 if (!(flags & SELECTION_SOURCE) || (s->arch != ARCH_SRC && s->arch != ARCH_NOSRC))
378 if ((flags & SELECTION_INSTALLED_ONLY) != 0 && s->repo != pool->installed)
380 queue_push(&q, di.solvid);
381 dataiterator_skip_solvable(&di);
383 dataiterator_free(&di);
387 queue_push2(selection, SOLVER_SOLVABLE_ONE_OF, pool_queuetowhatprovides(pool, &q));
389 queue_push2(selection, SOLVER_SOLVABLE | SOLVER_NOAUTOSET, q.elements[0]);
391 return SELECTION_FILELIST;
395 selection_rel(Pool *pool, Queue *selection, const char *name, int flags)
400 /* relation case, support:
404 rname = solv_strdup(name);
405 if ((r = strpbrk(rname, "<=>")) != 0)
407 int nend = r - rname;
419 while (*r && *r == ' ' && *r == '\t')
421 while (nend && (rname[nend - 1] == ' ' || rname[nend -1 ] == '\t'))
430 if ((ret = selection_depglob_arch(pool, selection, rname, flags)) != 0)
433 selection_limit_rel(pool, selection, rflags, pool_str2id(pool, r, 1));
442 selection_nevra(Pool *pool, Queue *selection, const char *name, int flags)
444 char *rname, *r, *r2;
450 * nameglob-version.arch
451 * nameglob-version-release
452 * nameglob-version-release.arch
454 flags |= SELECTION_NAME;
455 flags &= ~SELECTION_PROVIDES;
457 if (pool->disttype == DISTTYPE_DEB)
459 if ((r = strchr(name, '_')) == 0)
461 rname = solv_strdup(name); /* so we can modify it */
462 r = rname + (r - name);
464 if ((ret = selection_depglob(pool, selection, rname, flags)) == 0)
469 /* is there a vaild arch? */
470 if ((r2 = strchr(r, '_')) != 0 && r[1] && (archid = str2archid(pool, r + 1)) != 0)
472 *r2 = 0; /* split off */
473 selection_limit_rel(pool, selection, REL_ARCH, archid);
475 selection_limit_rel(pool, selection, REL_EQ, pool_str2id(pool, r, 1));
480 if ((r = strrchr(name, '-')) == 0)
482 rname = solv_strdup(name); /* so we can modify it */
483 r = rname + (r - name);
486 /* split off potential arch part from version */
487 if ((r2 = strrchr(r + 1, '.')) != 0 && r2[1] && (archid = str2archid(pool, r2 + 1)) != 0)
488 *r2 = 0; /* found valid arch, split it off */
489 if (archid == ARCH_SRC || archid == ARCH_NOSRC)
490 flags |= SELECTION_SOURCE;
492 /* try with just the version */
493 if ((ret = selection_depglob(pool, selection, rname, flags)) == 0)
495 /* no luck, try with version-release */
496 if ((r2 = strrchr(rname, '-')) == 0)
504 if ((ret = selection_depglob(pool, selection, rname, flags)) == 0)
511 selection_limit_rel(pool, selection, REL_ARCH, archid);
512 selection_limit_rel(pool, selection, REL_EQ, pool_str2id(pool, r + 1, 1));
518 selection_make(Pool *pool, Queue *selection, const char *name, int flags)
523 queue_empty(selection);
524 if (*name == '/' && (flags & SELECTION_FILELIST))
525 ret = selection_filelist(pool, selection, name, flags);
526 if (!ret && (r = strpbrk(name, "<=>")) != 0)
527 ret = selection_rel(pool, selection, name, flags);
529 ret = selection_depglob_arch(pool, selection, name, flags);
530 if (!ret && (flags & SELECTION_NAME) != 0)
531 ret = selection_nevra(pool, selection, name, flags);
532 if (ret && (flags & SELECTION_FLAT) != 0)
533 selection_flatten(pool, selection);
538 selection_limit(Pool *pool, Queue *sel1, Queue *sel2)
546 if (!sel1->count || !sel2->count)
551 if (sel1->count == 2 && (sel1->elements[0] & SOLVER_SELECTMASK) == SOLVER_SOLVABLE_ALL)
553 /* XXX: not 100% correct, but very useful */
555 queue_init_clone(sel1, sel2);
559 map_init(&m2, pool->nsolvables);
560 for (i = 0; i < sel2->count; i += 2)
562 Id select = sel2->elements[i] & SOLVER_SELECTMASK;
563 if (select == SOLVER_SOLVABLE_ALL)
565 if (select == SOLVER_SOLVABLE_REPO)
568 Repo *repo = pool_id2repo(pool, sel2->elements[i + 1]);
570 FOR_REPO_SOLVABLES(repo, p, s)
575 FOR_JOB_SELECT(p, pp, select, sel2->elements[i + 1])
579 if (sel2->count == 2) /* XXX: AND all setmasks instead? */
580 setflags = sel2->elements[0] & SOLVER_SETMASK & ~SOLVER_NOAUTOSET;
581 for (i = j = 0; i < sel1->count; i += 2)
583 Id select = sel1->elements[i] & SOLVER_SELECTMASK;
586 if (select == SOLVER_SOLVABLE_ALL)
588 FOR_POOL_SOLVABLES(p)
596 else if (select == SOLVER_SOLVABLE_REPO)
599 Repo *repo = pool_id2repo(pool, sel1->elements[i + 1]);
601 FOR_REPO_SOLVABLES(repo, p, s)
611 FOR_JOB_SELECT(p, pp, select, sel1->elements[i + 1])
614 queue_pushunique(&q1, p);
623 sel1->elements[j] = sel1->elements[i] | setflags;
624 sel1->elements[j + 1] = sel1->elements[i + 1];
626 else if (q1.count > 1)
628 sel1->elements[j] = (sel1->elements[i] & ~SOLVER_SELECTMASK) | SOLVER_SOLVABLE_ONE_OF | setflags;
629 sel1->elements[j + 1] = pool_queuetowhatprovides(pool, &q1);
633 sel1->elements[j] = (sel1->elements[i] & ~SOLVER_SELECTMASK) | SOLVER_SOLVABLE | SOLVER_NOAUTOSET | setflags;
634 sel1->elements[j + 1] = q1.elements[0];
638 queue_truncate(sel1, j);
642 selection_add(Pool *pool, Queue *sel1, Queue *sel2)
645 for (i = 0; i < sel2->count; i++)
646 queue_push(sel1, sel2->elements[i]);