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 if ((flags & SELECTION_MAYBESRC) != 0 && (flags & SELECTION_NAME) != 0)
197 /* src rpms don't have provides, so we must check every solvable */
198 FOR_PROVIDES(p, pp, id) /* try fast path first */
200 Solvable *s = pool->solvables + p;
203 if ((flags & SELECTION_INSTALLED_ONLY) != 0 && s->repo != pool->installed)
205 queue_push2(selection, SOLVER_SOLVABLE_NAME, id);
206 return SELECTION_NAME;
209 FOR_POOL_SOLVABLES(p) /* slow path */
211 Solvable *s = pool->solvables + p;
212 if (s->name == id && (s->arch == ARCH_SRC || s->arch == ARCH_NOSRC))
214 if ((flags & SELECTION_INSTALLED_ONLY) != 0 && s->repo != pool->installed)
215 continue; /* just in case... src rpms can't be installed */
216 queue_push2(selection, SOLVER_SOLVABLE_NAME, id);
217 return SELECTION_NAME;
221 FOR_PROVIDES(p, pp, id)
223 Solvable *s = pool->solvables + p;
224 if ((flags & SELECTION_INSTALLED_ONLY) != 0 && s->repo != pool->installed)
227 if (s->name == id && (flags & SELECTION_NAME) != 0)
229 queue_push2(selection, SOLVER_SOLVABLE_NAME, id);
230 return SELECTION_NAME;
233 if (match && (flags & SELECTION_PROVIDES) != 0)
235 queue_push2(selection, SOLVER_SOLVABLE_PROVIDES, id);
236 return SELECTION_PROVIDES;
241 if ((flags & SELECTION_GLOB) != 0 && strpbrk(name, "[*?") != 0)
244 if (!doglob && !(flags & SELECTION_NOCASE))
247 if (doglob && (flags & SELECTION_NOCASE) != 0)
248 globflags = FNM_CASEFOLD;
250 #if 0 /* doesn't work with selection_limit_rel yet */
251 if (doglob && !strcmp(name, "*") && (flags & SELECTION_FLAT) != 0)
253 /* can't do this for SELECTION_PROVIDES, as src rpms don't provide anything */
254 if ((flags & SELECTION_NAME) != 0)
256 queue_push2(selection, SOLVER_SOLVABLE_ALL, 0);
257 return SELECTION_NAME;
262 if ((flags & SELECTION_NAME) != 0)
264 /* looks like a name glob. hard work. */
265 FOR_POOL_SOLVABLES(p)
267 Solvable *s = pool->solvables + p;
268 if (!pool_installable(pool, s))
269 if (!(flags & SELECTION_MAYBESRC) || (s->arch != ARCH_SRC && s->arch != ARCH_NOSRC))
271 if ((flags & SELECTION_INSTALLED_ONLY) != 0 && s->repo != pool->installed)
274 if ((doglob ? fnmatch(name, pool_id2str(pool, id), globflags) : strcasecmp(name, pool_id2str(pool, id))) == 0)
276 for (i = 0; i < selection->count; i += 2)
277 if (selection->elements[i] == SOLVER_SOLVABLE_NAME && selection->elements[i + 1] == id)
279 if (i == selection->count)
280 queue_push2(selection, SOLVER_SOLVABLE_NAME, id);
285 return SELECTION_NAME;
287 if ((flags & SELECTION_PROVIDES))
289 /* looks like a dep glob. really hard work. */
290 for (id = 1; id < pool->ss.nstrings; id++)
292 if (!pool->whatprovides[id])
294 if ((doglob ? fnmatch(name, pool_id2str(pool, id), globflags) : strcasecmp(name, pool_id2str(pool, id))) == 0)
296 if ((flags & SELECTION_INSTALLED_ONLY) != 0)
298 FOR_PROVIDES(p, pp, id)
299 if (pool->solvables[p].repo == pool->installed)
304 queue_push2(selection, SOLVER_SOLVABLE_PROVIDES, id);
309 return SELECTION_PROVIDES;
315 selection_depglob_arch(Pool *pool, Queue *selection, const char *name, int flags)
321 if ((ret = selection_depglob(pool, selection, name, flags)) != 0)
323 /* check if theres an .arch suffix */
324 if ((r = strrchr(name, '.')) != 0 && r[1] && (archid = str2archid(pool, r + 1)) != 0)
326 char *rname = solv_strdup(name);
328 if (archid == ARCH_SRC || archid == ARCH_NOSRC)
329 flags |= SELECTION_MAYBESRC;
330 if ((ret = selection_depglob(pool, selection, rname, flags)) != 0)
332 selection_limit_rel(pool, selection, REL_ARCH, archid);
342 selection_filelist(Pool *pool, Queue *selection, const char *name, int flags)
348 type = !(flags & SELECTION_GLOB) || strpbrk(name, "[*?") == 0 ? SEARCH_STRING : SEARCH_GLOB;
349 if ((flags & SELECTION_NOCASE) != 0)
350 type |= SEARCH_NOCASE;
352 dataiterator_init(&di, pool, flags & SELECTION_INSTALLED_ONLY ? pool->installed : 0, 0, SOLVABLE_FILELIST, name, type|SEARCH_FILES|SEARCH_COMPLETE_FILELIST);
353 while (dataiterator_step(&di))
355 Solvable *s = pool->solvables + di.solvid;
356 if (!s->repo || !pool_installable(pool, s))
358 queue_push(&q, di.solvid);
359 dataiterator_skip_solvable(&di);
361 dataiterator_free(&di);
365 queue_push2(selection, SOLVER_SOLVABLE_ONE_OF, pool_queuetowhatprovides(pool, &q));
367 queue_push2(selection, SOLVER_SOLVABLE | SOLVER_NOAUTOSET, q.elements[0]);
369 return SELECTION_FILELIST;
373 selection_rel(Pool *pool, Queue *selection, const char *name, int flags)
378 /* relation case, support:
382 rname = solv_strdup(name);
383 if ((r = strpbrk(rname, "<=>")) != 0)
385 int nend = r - rname;
397 while (*r && *r == ' ' && *r == '\t')
399 while (nend && (rname[nend - 1] == ' ' || rname[nend -1 ] == '\t'))
408 if ((ret = selection_depglob_arch(pool, selection, rname, flags)) != 0)
411 selection_limit_rel(pool, selection, rflags, pool_str2id(pool, r, 1));
420 selection_nevra(Pool *pool, Queue *selection, const char *name, int flags)
422 char *rname, *r, *r2;
428 * nameglob-version.arch
429 * nameglob-version-release
430 * nameglob-version-release.arch
432 flags |= SELECTION_NAME;
433 flags &= ~SELECTION_PROVIDES;
435 if (pool->disttype == DISTTYPE_DEB)
437 if ((r = strchr(name, '_')) == 0)
439 rname = solv_strdup(name); /* so we can modify it */
440 r = rname + (r - name);
442 if ((ret = selection_depglob(pool, selection, rname, flags)) == 0)
447 /* is there a vaild arch? */
448 if ((r2 = strchr(r, '_')) != 0 && r[1] && (archid = str2archid(pool, r + 1)) != 0)
450 *r2 = 0; /* split off */
451 selection_limit_rel(pool, selection, REL_ARCH, archid);
453 selection_limit_rel(pool, selection, REL_EQ, pool_str2id(pool, r, 1));
458 if ((r = strrchr(name, '-')) == 0)
460 rname = solv_strdup(name); /* so we can modify it */
461 r = rname + (r - name);
464 /* split off potential arch part from version */
465 if ((r2 = strrchr(r + 1, '.')) != 0 && r2[1] && (archid = str2archid(pool, r2 + 1)) != 0)
466 *r2 = 0; /* found valid arch, split it off */
467 if (archid == ARCH_SRC || archid == ARCH_NOSRC)
468 flags |= SELECTION_MAYBESRC;
470 /* try with just the version */
471 if ((ret = selection_depglob(pool, selection, rname, flags)) == 0)
473 /* no luck, try with version-release */
474 if ((r2 = strrchr(rname, '-')) == 0)
482 if ((ret = selection_depglob(pool, selection, rname, flags)) == 0)
489 selection_limit_rel(pool, selection, REL_ARCH, archid);
490 selection_limit_rel(pool, selection, REL_EQ, pool_str2id(pool, r + 1, 1));
496 selection_make(Pool *pool, Queue *selection, const char *name, int flags)
501 queue_empty(selection);
502 if (*name == '/' && (flags & SELECTION_FILELIST))
503 ret = selection_filelist(pool, selection, name, flags);
504 if (!ret && (r = strpbrk(name, "<=>")) != 0)
505 ret = selection_rel(pool, selection, name, flags);
507 ret = selection_depglob_arch(pool, selection, name, flags);
508 if (!ret && (flags & SELECTION_NAME) != 0)
509 ret = selection_nevra(pool, selection, name, flags);
510 if (ret && (flags & SELECTION_FLAT) != 0)
511 selection_flatten(pool, selection);
516 selection_limit(Pool *pool, Queue *sel1, Queue *sel2)
524 if (!sel1->count || !sel2->count)
529 if (sel1->count == 2 && (sel1->elements[0] & SOLVER_SELECTMASK) == SOLVER_SOLVABLE_ALL)
531 /* XXX: not 100% correct, but very useful */
533 queue_init_clone(sel1, sel2);
537 map_init(&m2, pool->nsolvables);
538 for (i = 0; i < sel2->count; i += 2)
540 Id select = sel2->elements[i] & SOLVER_SELECTMASK;
541 if (select == SOLVER_SOLVABLE_ALL)
543 if (select == SOLVER_SOLVABLE_REPO)
546 Repo *repo = pool_id2repo(pool, sel2->elements[i + 1]);
548 FOR_REPO_SOLVABLES(repo, p, s)
553 FOR_JOB_SELECT(p, pp, select, sel2->elements[i + 1])
557 if (sel2->count == 2) /* XXX: AND all setmasks instead? */
558 setflags = sel2->elements[0] & SOLVER_SETMASK & ~SOLVER_NOAUTOSET;
559 for (i = j = 0; i < sel1->count; i += 2)
561 Id select = sel1->elements[i] & SOLVER_SELECTMASK;
564 if (select == SOLVER_SOLVABLE_ALL)
566 FOR_POOL_SOLVABLES(p)
574 else if (select == SOLVER_SOLVABLE_REPO)
577 Repo *repo = pool_id2repo(pool, sel1->elements[i + 1]);
579 FOR_REPO_SOLVABLES(repo, p, s)
589 FOR_JOB_SELECT(p, pp, select, sel1->elements[i + 1])
592 queue_pushunique(&q1, p);
601 sel1->elements[j] = sel1->elements[i] | setflags;
602 sel1->elements[j + 1] = sel1->elements[i + 1];
604 else if (q1.count > 1)
606 sel1->elements[j] = (sel1->elements[i] & ~SOLVER_SELECTMASK) | SOLVER_SOLVABLE_ONE_OF | setflags;
607 sel1->elements[j + 1] = pool_queuetowhatprovides(pool, &q1);
611 sel1->elements[j] = (sel1->elements[i] & ~SOLVER_SELECTMASK) | SOLVER_SOLVABLE | SOLVER_NOAUTOSET | setflags;
612 sel1->elements[j + 1] = q1.elements[0];
616 queue_truncate(sel1, j);
620 selection_add(Pool *pool, Queue *sel1, Queue *sel2)
623 for (i = 0; i < sel2->count; i++)
624 queue_push(sel1, sel2->elements[i]);