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)
90 for (p = 2; p < pool->nsolvables; p++)
91 if (pool->solvables[p].repo)
94 if (select == SOLVER_SOLVABLE_REPO)
97 Repo *repo = pool_id2repo(pool, selection->elements[i + 1]);
99 FOR_REPO_SOLVABLES(repo, p, s)
104 FOR_JOB_SELECT(p, pp, select, selection->elements[i + 1])
111 solv_sort(pkgs->elements, pkgs->count, sizeof(Id), selection_solvables_sortcmp, NULL);
112 lastid = pkgs->elements[0];
113 for (i = j = 1; i < pkgs->count; i++)
114 if (pkgs->elements[i] != lastid)
115 pkgs->elements[j++] = lastid = pkgs->elements[i];
116 queue_truncate(pkgs, j);
120 selection_flatten(Pool *pool, Queue *selection)
124 if (selection->count <= 1)
126 for (i = 0; i < selection->count; i += 2)
127 if ((selection->elements[i] & SOLVER_SELECTMASK) == SOLVER_SOLVABLE_ALL)
129 selection->elements[0] = selection->elements[i];
130 selection->elements[1] = selection->elements[i + 1];
131 queue_truncate(selection, 2);
135 selection_solvables(pool, selection, &q);
138 queue_empty(selection);
141 queue_truncate(selection, 2);
144 selection->elements[0] = SOLVER_SOLVABLE_ONE_OF;
145 selection->elements[1] = pool_queuetowhatprovides(pool, &q);
149 selection->elements[0] = SOLVER_SOLVABLE | SOLVER_NOAUTOSET;
150 selection->elements[1] = q.elements[0];
155 selection_limit_rel(Pool *pool, Queue *selection, Id flags, Id evr)
158 for (i = j = 0; i < selection->count; i += 2)
160 Id select = selection->elements[i] & SOLVER_SELECTMASK;
161 if (select != SOLVER_SOLVABLE_NAME && select != SOLVER_SOLVABLE_PROVIDES)
162 continue; /* actually internal error */
163 selection->elements[i + 1] = pool_rel2id(pool, selection->elements[i + 1], evr, flags, 1);
164 if (flags == REL_ARCH)
165 selection->elements[i] |= SOLVER_SETARCH;
166 if (flags == REL_EQ && select == SOLVER_SOLVABLE_NAME && selection->elements[i])
168 if (pool->disttype == DISTTYPE_DEB)
169 selection->elements[i] |= SOLVER_SETEVR; /* debian can't match version only like rpm */
171 selection->elements[i] |= strchr(pool_id2str(pool, evr), '-') != 0 ? SOLVER_SETEVR : SOLVER_SETEV;
174 selection_prune(pool, selection);
178 selection_depglob(Pool *pool, Queue *selection, const char *name, int flags)
185 if (!(flags & (SELECTION_NAME|SELECTION_PROVIDES)))
188 if ((flags & SELECTION_INSTALLED_ONLY) != 0 && !pool->installed)
191 if (!(flags & SELECTION_NOCASE))
193 id = pool_str2id(pool, name, 0);
196 FOR_PROVIDES(p, pp, id)
198 Solvable *s = pool->solvables + p;
199 if ((flags & SELECTION_INSTALLED_ONLY) != 0 && s->repo != pool->installed)
202 if (s->name == id && (flags & SELECTION_NAME) != 0)
204 queue_push2(selection, SOLVER_SOLVABLE_NAME, id);
205 return SELECTION_NAME;
208 if (match && (flags & SELECTION_PROVIDES) != 0)
210 queue_push2(selection, SOLVER_SOLVABLE_PROVIDES, id);
211 return SELECTION_PROVIDES;
216 if ((flags & SELECTION_GLOB) != 0 && strpbrk(name, "[*?") != 0)
219 if (!doglob && !(flags & SELECTION_NOCASE))
222 if (doglob && (flags & SELECTION_NOCASE) != 0)
223 globflags = FNM_CASEFOLD;
225 #if 0 /* doesn't work with selection_limit_rel yet */
226 if (doglob && !strcmp(name, "*") && (flags & SELECTION_FLAT) != 0)
228 /* can't do this for SELECTION_PROVIDES, as src rpms don't provide anything */
229 if ((flags & SELECTION_NAME) != 0)
231 queue_push2(selection, SOLVER_SOLVABLE_ALL, 0);
232 return SELECTION_NAME;
237 if ((flags & SELECTION_NAME) != 0)
239 /* looks like a name glob. hard work. */
240 for (p = 1; p < pool->nsolvables; p++)
242 Solvable *s = pool->solvables + p;
243 if (!s->repo || !pool_installable(pool, s))
245 if ((flags & SELECTION_INSTALLED_ONLY) != 0 && s->repo != pool->installed)
248 if ((doglob ? fnmatch(name, pool_id2str(pool, id), globflags) : strcasecmp(name, pool_id2str(pool, id))) == 0)
250 for (i = 0; i < selection->count; i += 2)
251 if (selection->elements[i] == SOLVER_SOLVABLE_NAME && selection->elements[i + 1] == id)
253 if (i == selection->count)
254 queue_push2(selection, SOLVER_SOLVABLE_NAME, id);
259 return SELECTION_NAME;
261 if ((flags & SELECTION_PROVIDES))
263 /* looks like a dep glob. really hard work. */
264 for (id = 1; id < pool->ss.nstrings; id++)
266 if (!pool->whatprovides[id])
268 if ((doglob ? fnmatch(name, pool_id2str(pool, id), globflags) : strcasecmp(name, pool_id2str(pool, id))) == 0)
270 if ((flags & SELECTION_INSTALLED_ONLY) != 0)
272 FOR_PROVIDES(p, pp, id)
273 if (pool->solvables[p].repo == pool->installed)
278 queue_push2(selection, SOLVER_SOLVABLE_PROVIDES, id);
283 return SELECTION_PROVIDES;
289 selection_depglob_arch(Pool *pool, Queue *selection, const char *name, int flags)
295 if ((ret = selection_depglob(pool, selection, name, flags)) != 0)
297 /* check if theres an .arch suffix */
298 if ((r = strrchr(name, '.')) != 0 && r[1] && (archid = str2archid(pool, r + 1)) != 0)
300 char *rname = solv_strdup(name);
302 if ((ret = selection_depglob(pool, selection, rname, flags)) != 0)
304 selection_limit_rel(pool, selection, REL_ARCH, archid);
314 selection_filelist(Pool *pool, Queue *selection, const char *name, int flags)
320 type = !(flags & SELECTION_GLOB) || strpbrk(name, "[*?") == 0 ? SEARCH_STRING : SEARCH_GLOB;
321 if ((flags & SELECTION_NOCASE) != 0)
322 type |= SEARCH_NOCASE;
324 dataiterator_init(&di, pool, flags & SELECTION_INSTALLED_ONLY ? pool->installed : 0, 0, SOLVABLE_FILELIST, name, type|SEARCH_FILES|SEARCH_COMPLETE_FILELIST);
325 while (dataiterator_step(&di))
327 Solvable *s = pool->solvables + di.solvid;
328 if (!s->repo || !pool_installable(pool, s))
330 queue_push(&q, di.solvid);
331 dataiterator_skip_solvable(&di);
333 dataiterator_free(&di);
337 queue_push2(selection, SOLVER_SOLVABLE_ONE_OF, pool_queuetowhatprovides(pool, &q));
339 queue_push2(selection, SOLVER_SOLVABLE | SOLVER_NOAUTOSET, q.elements[0]);
341 return SELECTION_FILELIST;
345 selection_rel(Pool *pool, Queue *selection, const char *name, int flags)
350 /* relation case, support:
354 rname = solv_strdup(name);
355 if ((r = strpbrk(rname, "<=>")) != 0)
357 int nend = r - rname;
369 while (*r && *r == ' ' && *r == '\t')
371 while (nend && (rname[nend - 1] == ' ' || rname[nend -1 ] == '\t'))
380 if ((ret = selection_depglob_arch(pool, selection, rname, flags)) != 0)
383 selection_limit_rel(pool, selection, rflags, pool_str2id(pool, r, 1));
392 selection_nevra(Pool *pool, Queue *selection, const char *name, int flags)
394 char *rname, *r, *r2;
400 * nameglob-version.arch
401 * nameglob-version-release
402 * nameglob-version-release.arch
404 flags |= SELECTION_NAME;
405 flags &= ~SELECTION_PROVIDES;
407 if (pool->disttype == DISTTYPE_DEB)
409 if ((r = strchr(name, '_')) == 0)
411 rname = solv_strdup(name); /* so we can modify it */
412 r = rname + (r - name);
414 if ((ret = selection_depglob(pool, selection, rname, flags)) == 0)
419 /* is there a vaild arch? */
420 if ((r2 = strchr(r, '_')) != 0 && r[1] && (archid = str2archid(pool, r + 1)) != 0)
422 *r2 = 0; /* split off */
423 selection_limit_rel(pool, selection, REL_ARCH, archid);
425 selection_limit_rel(pool, selection, flags, pool_str2id(pool, r, 1));
430 if ((r = strrchr(name, '-')) == 0)
432 rname = solv_strdup(name); /* so we can modify it */
433 r = rname + (r - name);
435 /* try with just the version */
436 if ((ret = selection_depglob(pool, selection, rname, flags)) == 0)
438 /* no luck, try with version-release */
439 if ((r2 = strrchr(rname, '-')) == 0)
447 if ((ret = selection_depglob(pool, selection, rname, flags)) == 0)
453 /* we now know the name, check if we need to split of the arch */
455 if ((r2 = strrchr(r, '.')) != 0 && r2[1] && (archid = str2archid(pool, r2 + 1)) != 0)
457 /* found valid arch, split it off */
459 selection_limit_rel(pool, selection, REL_ARCH, archid);
461 selection_limit_rel(pool, selection, REL_EQ, pool_str2id(pool, r, 1));
467 selection_make(Pool *pool, Queue *selection, const char *name, int flags)
472 queue_empty(selection);
473 if (*name == '/' && (flags & SELECTION_FILELIST))
474 ret = selection_filelist(pool, selection, name, flags);
475 if (!ret && (r = strpbrk(name, "<=>")) != 0)
476 ret = selection_rel(pool, selection, name, flags);
478 ret = selection_depglob_arch(pool, selection, name, flags);
479 if (!ret && (flags & SELECTION_NAME) != 0)
480 ret = selection_nevra(pool, selection, name, flags);
481 if (ret && (flags & SELECTION_FLAT) != 0)
482 selection_flatten(pool, selection);
487 selection_limit(Pool *pool, Queue *sel1, Queue *sel2)
495 if (!sel1->count || !sel2->count)
500 if (sel1->count == 2 && (sel1->elements[0] & SOLVER_SELECTMASK) == SOLVER_SOLVABLE_ALL)
502 /* XXX: not 100% correct, but very useful */
504 queue_init_clone(sel1, sel2);
508 map_init(&m2, pool->nsolvables);
509 for (i = 0; i < sel2->count; i += 2)
511 Id select = sel2->elements[i] & SOLVER_SELECTMASK;
512 if (select == SOLVER_SOLVABLE_ALL)
514 if (select == SOLVER_SOLVABLE_REPO)
517 Repo *repo = pool_id2repo(pool, sel2->elements[i + 1]);
519 FOR_REPO_SOLVABLES(repo, p, s)
524 FOR_JOB_SELECT(p, pp, select, sel2->elements[i + 1])
528 if (sel2->count == 2) /* XXX: AND all setmasks instead? */
529 setflags = sel2->elements[0] & SOLVER_SETMASK & ~SOLVER_NOAUTOSET;
530 for (i = j = 0; i < sel1->count; i += 2)
532 Id select = sel1->elements[i] & SOLVER_SELECTMASK;
535 if (select == SOLVER_SOLVABLE_ALL)
537 for (p = 2; p < pool->nsolvables; p++)
543 else if (select == SOLVER_SOLVABLE_REPO)
546 Repo *repo = pool_id2repo(pool, sel1->elements[i + 1]);
548 FOR_REPO_SOLVABLES(repo, p, s)
558 FOR_JOB_SELECT(p, pp, select, sel1->elements[i + 1])
561 queue_pushunique(&q1, p);
570 sel1->elements[j] = sel1->elements[i] | setflags;
571 sel1->elements[j + 1] = sel1->elements[i + 1];
573 else if (q1.count > 1)
575 sel1->elements[j] = (sel1->elements[i] & ~SOLVER_SELECTMASK) | SOLVER_SOLVABLE_ONE_OF | setflags;
576 sel1->elements[j + 1] = pool_queuetowhatprovides(pool, &q1);
580 sel1->elements[j] = (sel1->elements[i] & ~SOLVER_SELECTMASK) | SOLVER_SOLVABLE | SOLVER_NOAUTOSET | setflags;
581 sel1->elements[j + 1] = q1.elements[0];
585 queue_truncate(sel1, j);
589 selection_add(Pool *pool, Queue *sel1, Queue *sel2)
592 for (i = 0; i < sel2->count; i++)
593 queue_push(sel1, sel2->elements[i]);