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"
27 str2archid(Pool *pool, const char *arch)
32 id = pool_str2id(pool, arch, 0);
33 if (!id || id == ARCH_SRC || id == ARCH_NOSRC || id == ARCH_NOARCH)
35 if (pool->id2arch && (id > pool->lastarch || !pool->id2arch[id]))
41 selection_prune(Pool *pool, Queue *selection)
45 for (i = j = 0; i < selection->count; i += 2)
47 Id select = selection->elements[i] & SOLVER_SELECTMASK;
49 if (select == SOLVER_SOLVABLE_ALL)
51 else if (select == SOLVER_SOLVABLE_REPO)
54 Repo *repo = pool_id2repo(pool, selection->elements[i + 1]);
56 FOR_REPO_SOLVABLES(repo, p, s)
61 FOR_JOB_SELECT(p, pp, select, selection->elements[i + 1])
66 selection->elements[j] = selection->elements[i];
67 selection->elements[j + 1] = selection->elements[i + 1];
70 queue_truncate(selection, j);
75 selection_solvables_sortcmp(const void *ap, const void *bp, void *dp)
77 return *(const Id *)ap - *(const Id *)bp;
81 selection_solvables(Pool *pool, Queue *selection, Queue *pkgs)
86 for (i = 0; i < selection->count; i += 2)
88 Id select = selection->elements[i] & SOLVER_SELECTMASK;
89 if (select == SOLVER_SOLVABLE_ALL)
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 relflags, Id relevr)
158 for (i = 0; i < selection->count; i += 2)
160 Id select = selection->elements[i] & SOLVER_SELECTMASK;
161 Id id = selection->elements[i + 1];
162 if (select == SOLVER_SOLVABLE || select == SOLVER_SOLVABLE_ONE_OF)
164 /* done by selection_addsrc */
167 Id rel = 0, relname = 0;
171 FOR_JOB_SELECT(p, pp, select, id)
173 Solvable *s = pool->solvables + p;
174 if (!rel || s->name != relname)
177 rel = pool_rel2id(pool, relname, relevr, relflags, 1);
179 if (pool_match_nevr(pool, s, rel))
188 selection->elements[i] = SOLVER_SOLVABLE | SOLVER_NOAUTOSET;
189 selection->elements[i + 1] = q.elements[0];
193 selection->elements[i] = SOLVER_SOLVABLE_ONE_OF;
194 selection->elements[i + 1] = pool_queuetowhatprovides(pool, &q);
199 else if (select == SOLVER_SOLVABLE_NAME && select == SOLVER_SOLVABLE_PROVIDES)
201 /* don't stack src reldeps */
202 if (relflags == REL_ARCH && (relevr == ARCH_SRC || relevr == ARCH_NOSRC) && ISRELDEP(id))
204 Reldep *rd = GETRELDEP(pool, id);
205 if (rd->flags == REL_ARCH && rd->evr == ARCH_SRC)
208 selection->elements[i + 1] = pool_rel2id(pool, id, relevr, relflags, 1);
211 continue; /* actually internal error */
212 if (relflags == REL_ARCH)
213 selection->elements[i] |= SOLVER_SETARCH;
214 if (relflags == REL_EQ && select != SOLVER_SOLVABLE_PROVIDES)
216 if (pool->disttype == DISTTYPE_DEB)
217 selection->elements[i] |= SOLVER_SETEVR; /* debian can't match version only like rpm */
219 selection->elements[i] |= strchr(pool_id2str(pool, relevr), '-') != 0 ? SOLVER_SETEVR : SOLVER_SETEV;
222 selection_prune(pool, selection);
226 selection_addsrc(Pool *pool, Queue *selection, int flags)
232 if ((flags & SELECTION_INSTALLED_ONLY) != 0)
233 return; /* sources can't be installed */
235 for (i = 0; i < selection->count; i += 2)
237 if (selection->elements[i] != SOLVER_SOLVABLE_NAME)
239 name = selection->elements[i + 1];
242 FOR_POOL_SOLVABLES(p)
244 Solvable *s = pool->solvables + p;
247 if (s->arch == ARCH_SRC || s->arch == ARCH_NOSRC)
249 else if (s->repo != pool->installed && !pool_installable(pool, s))
253 if (!havesrc || !q.count)
257 selection->elements[i] = SOLVER_SOLVABLE | SOLVER_NOAUTOSET;
258 selection->elements[i + 1] = q.elements[0];
262 selection->elements[i] = SOLVER_SOLVABLE_ONE_OF;
263 selection->elements[i + 1] = pool_queuetowhatprovides(pool, &q);
270 selection_depglob(Pool *pool, Queue *selection, const char *name, int flags)
277 if ((flags & SELECTION_SOURCE_ONLY) != 0)
279 flags &= ~SELECTION_PROVIDES; /* sources don't provide anything */
280 flags &= ~SELECTION_WITH_SOURCE;
283 if (!(flags & (SELECTION_NAME|SELECTION_PROVIDES)))
286 if ((flags & SELECTION_INSTALLED_ONLY) != 0 && !pool->installed)
289 if (!(flags & SELECTION_NOCASE))
291 id = pool_str2id(pool, name, 0);
294 if ((flags & (SELECTION_SOURCE_ONLY | SELECTION_WITH_SOURCE)) != 0 && (flags & SELECTION_NAME) != 0)
296 /* src rpms don't have provides, so we must check every solvable */
297 FOR_PROVIDES(p, pp, id) /* try fast path first */
299 Solvable *s = pool->solvables + p;
302 if ((flags & SELECTION_INSTALLED_ONLY) != 0 && s->repo != pool->installed)
304 if ((flags & SELECTION_SOURCE_ONLY) != 0)
305 id = pool_rel2id(pool, id, ARCH_SRC, REL_ARCH, 1);
306 queue_push2(selection, SOLVER_SOLVABLE_NAME, id);
307 if ((flags & SELECTION_WITH_SOURCE) != 0)
308 selection_addsrc(pool, selection, flags);
309 return SELECTION_NAME;
312 FOR_POOL_SOLVABLES(p) /* slow path */
314 Solvable *s = pool->solvables + p;
315 if (s->name == id && (s->arch == ARCH_SRC || s->arch == ARCH_NOSRC))
317 if ((flags & SELECTION_INSTALLED_ONLY) != 0 && s->repo != pool->installed)
318 continue; /* just in case... src rpms can't be installed */
319 if ((flags & SELECTION_SOURCE_ONLY) != 0)
320 id = pool_rel2id(pool, id, ARCH_SRC, REL_ARCH, 1);
321 queue_push2(selection, SOLVER_SOLVABLE_NAME, id);
322 if ((flags & SELECTION_WITH_SOURCE) != 0)
323 selection_addsrc(pool, selection, flags);
324 return SELECTION_NAME;
328 FOR_PROVIDES(p, pp, id)
330 Solvable *s = pool->solvables + p;
331 if ((flags & SELECTION_INSTALLED_ONLY) != 0 && s->repo != pool->installed)
334 if (s->name == id && (flags & SELECTION_NAME) != 0)
336 if ((flags & SELECTION_SOURCE_ONLY) != 0)
337 id = pool_rel2id(pool, id, ARCH_SRC, REL_ARCH, 1);
338 queue_push2(selection, SOLVER_SOLVABLE_NAME, id);
339 if ((flags & SELECTION_WITH_SOURCE) != 0)
340 selection_addsrc(pool, selection, flags);
341 return SELECTION_NAME;
344 if (match && (flags & SELECTION_PROVIDES) != 0)
346 queue_push2(selection, SOLVER_SOLVABLE_PROVIDES, id);
347 return SELECTION_PROVIDES;
352 if ((flags & SELECTION_GLOB) != 0 && strpbrk(name, "[*?") != 0)
355 if (!doglob && !(flags & SELECTION_NOCASE))
358 if (doglob && (flags & SELECTION_NOCASE) != 0)
359 globflags = FNM_CASEFOLD;
361 #if 0 /* doesn't work with selection_limit_rel yet */
362 if (doglob && !strcmp(name, "*") && (flags & SELECTION_FLAT) != 0)
364 /* can't do this for SELECTION_PROVIDES, as src rpms don't provide anything */
365 if ((flags & SELECTION_NAME) != 0)
367 queue_push2(selection, SOLVER_SOLVABLE_ALL, 0);
368 return SELECTION_NAME;
373 if ((flags & SELECTION_NAME) != 0)
375 /* looks like a name glob. hard work. */
376 FOR_POOL_SOLVABLES(p)
378 Solvable *s = pool->solvables + p;
379 if (s->repo != pool->installed && !pool_installable(pool, s))
380 if (!(flags & SELECTION_SOURCE_ONLY) || (s->arch != ARCH_SRC && s->arch != ARCH_NOSRC))
382 if ((flags & SELECTION_INSTALLED_ONLY) != 0 && s->repo != pool->installed)
385 if ((doglob ? fnmatch(name, pool_id2str(pool, id), globflags) : strcasecmp(name, pool_id2str(pool, id))) == 0)
387 if ((flags & SELECTION_SOURCE_ONLY) != 0)
388 id = pool_rel2id(pool, id, ARCH_SRC, REL_ARCH, 1);
389 /* queue_pushunique2 */
390 for (i = 0; i < selection->count; i += 2)
391 if (selection->elements[i] == SOLVER_SOLVABLE_NAME && selection->elements[i + 1] == id)
393 if (i == selection->count)
394 queue_push2(selection, SOLVER_SOLVABLE_NAME, id);
400 if ((flags & SELECTION_WITH_SOURCE) != 0)
401 selection_addsrc(pool, selection, flags);
402 return SELECTION_NAME;
405 if ((flags & SELECTION_PROVIDES))
407 /* looks like a dep glob. really hard work. */
408 for (id = 1; id < pool->ss.nstrings; id++)
410 if (!pool->whatprovides[id])
412 if ((doglob ? fnmatch(name, pool_id2str(pool, id), globflags) : strcasecmp(name, pool_id2str(pool, id))) == 0)
414 if ((flags & SELECTION_INSTALLED_ONLY) != 0)
416 FOR_PROVIDES(p, pp, id)
417 if (pool->solvables[p].repo == pool->installed)
422 queue_push2(selection, SOLVER_SOLVABLE_PROVIDES, id);
427 return SELECTION_PROVIDES;
433 selection_depglob_arch(Pool *pool, Queue *selection, const char *name, int flags)
439 if ((ret = selection_depglob(pool, selection, name, flags)) != 0)
441 /* check if there is an .arch suffix */
442 if ((r = strrchr(name, '.')) != 0 && r[1] && (archid = str2archid(pool, r + 1)) != 0)
444 char *rname = solv_strdup(name);
446 if (archid == ARCH_SRC || archid == ARCH_NOSRC)
447 flags |= SELECTION_SOURCE_ONLY;
448 if ((ret = selection_depglob(pool, selection, rname, flags)) != 0)
450 selection_limit_rel(pool, selection, REL_ARCH, archid);
460 selection_filelist(Pool *pool, Queue *selection, const char *name, int flags)
466 type = !(flags & SELECTION_GLOB) || strpbrk(name, "[*?") == 0 ? SEARCH_STRING : SEARCH_GLOB;
467 if ((flags & SELECTION_NOCASE) != 0)
468 type |= SEARCH_NOCASE;
470 dataiterator_init(&di, pool, flags & SELECTION_INSTALLED_ONLY ? pool->installed : 0, 0, SOLVABLE_FILELIST, name, type|SEARCH_FILES|SEARCH_COMPLETE_FILELIST);
471 while (dataiterator_step(&di))
473 Solvable *s = pool->solvables + di.solvid;
476 if (s->repo != pool->installed && !pool_installable(pool, s))
477 if (!(flags & SELECTION_SOURCE_ONLY) || (s->arch != ARCH_SRC && s->arch != ARCH_NOSRC))
479 if ((flags & SELECTION_INSTALLED_ONLY) != 0 && s->repo != pool->installed)
481 queue_push(&q, di.solvid);
482 dataiterator_skip_solvable(&di);
484 dataiterator_free(&di);
488 queue_push2(selection, SOLVER_SOLVABLE_ONE_OF, pool_queuetowhatprovides(pool, &q));
490 queue_push2(selection, SOLVER_SOLVABLE | SOLVER_NOAUTOSET, q.elements[0]);
492 return SELECTION_FILELIST;
496 selection_rel(Pool *pool, Queue *selection, const char *name, int flags)
501 /* relation case, support:
505 rname = solv_strdup(name);
506 if ((r = strpbrk(rname, "<=>")) != 0)
508 int nend = r - rname;
520 while (*r && *r == ' ' && *r == '\t')
522 while (nend && (rname[nend - 1] == ' ' || rname[nend -1 ] == '\t'))
531 if ((ret = selection_depglob_arch(pool, selection, rname, flags)) != 0)
534 selection_limit_rel(pool, selection, rflags, pool_str2id(pool, r, 1));
542 #if defined(MULTI_SEMANTICS)
543 # define EVRCMP_DEPCMP (pool->disttype == DISTTYPE_DEB ? EVRCMP_COMPARE : EVRCMP_MATCH_RELEASE)
544 #elif defined(DEBIAN)
545 # define EVRCMP_DEPCMP EVRCMP_COMPARE
547 # define EVRCMP_DEPCMP EVRCMP_MATCH_RELEASE
550 /* magic epoch promotion code, works only for SELECTION_NAME selections */
552 selection_limit_evr(Pool *pool, Queue *selection, char *evr)
559 queue_init_buffer(&q, qbuf, sizeof(qbuf)/sizeof(*qbuf));
560 for (i = j = 0; i < selection->count; i += 2)
562 Id select = selection->elements[i] & SOLVER_SELECTMASK;
563 Id id = selection->elements[i + 1];
565 const char *lastepoch = 0;
566 int lastepochlen = 0;
569 FOR_JOB_SELECT(p, pp, select, id)
571 Solvable *s = pool->solvables + p;
572 const char *sevr = pool_id2str(pool, s->evr);
574 for (sp = sevr; *sp >= '0' && *sp <= '9'; sp++)
578 /* compare vr part */
579 if (strcmp(evr, sp != sevr ? sp + 1 : sevr) != 0)
581 int r = pool_evrcmp_str(pool, sp != sevr ? sp + 1 : sevr, evr, EVRCMP_DEPCMP);
582 if (r == -1 || r == 1)
583 continue; /* solvable does not match vr */
588 while (sevr < sp && *sevr == '0') /* normalize */
594 lastepochlen = sp - sevr;
596 else if (lastepochlen != sp - sevr || strncmp(lastepoch, sevr, lastepochlen) != 0)
597 lastepochlen = -1; /* multiple different epochs */
599 if (!lastepoch || lastepochlen == 0)
600 id = pool_str2id(pool, evr, 1); /* no match at all or zero epoch */
601 else if (lastepochlen >= 0)
603 /* found exactly one epoch, simlpy prepend */
604 char *evrx = solv_malloc(strlen(evr) + lastepochlen + 2);
605 strncpy(evrx, lastepoch, lastepochlen + 1);
606 strcpy(evrx + lastepochlen + 1, evr);
607 id = pool_str2id(pool, evrx, 1);
612 /* multiple epochs in multiple solvables, convert to listy of solvables */
613 selection->elements[j] = (selection->elements[i] & ~SOLVER_SELECTMASK) | SOLVER_SOLVABLE_ONE_OF;
614 selection->elements[j + 1] = pool_queuetowhatprovides(pool, &q);
619 queue_push2(&q, selection->elements[i], selection->elements[i + 1]);
620 selection_limit_rel(pool, &q, REL_EQ, id);
624 continue; /* oops, no match */
626 selection->elements[j] = q.elements[0];
627 selection->elements[j + 1] = q.elements[1];
630 queue_truncate(selection, j);
635 selection_nevra(Pool *pool, Queue *selection, const char *name, int flags)
637 char *rname, *r, *r2;
643 * nameglob-version.arch
644 * nameglob-version-release
645 * nameglob-version-release.arch
647 flags |= SELECTION_NAME;
648 flags &= ~SELECTION_PROVIDES;
650 if (pool->disttype == DISTTYPE_DEB)
652 if ((r = strchr(name, '_')) == 0)
654 rname = solv_strdup(name); /* so we can modify it */
655 r = rname + (r - name);
657 if ((ret = selection_depglob(pool, selection, rname, flags)) == 0)
662 /* is there a vaild arch? */
663 if ((r2 = strchr(r, '_')) != 0 && r[1] && (archid = str2archid(pool, r + 1)) != 0)
665 *r2 = 0; /* split off */
666 selection_limit_rel(pool, selection, REL_ARCH, archid);
668 selection_limit_rel(pool, selection, REL_EQ, pool_str2id(pool, r, 1));
673 if ((r = strrchr(name, '-')) == 0)
675 rname = solv_strdup(name); /* so we can modify it */
676 r = rname + (r - name);
679 /* split off potential arch part from version */
680 if ((r2 = strrchr(r + 1, '.')) != 0 && r2[1] && (archid = str2archid(pool, r2 + 1)) != 0)
681 *r2 = 0; /* found valid arch, split it off */
682 if (archid == ARCH_SRC || archid == ARCH_NOSRC)
683 flags |= SELECTION_SOURCE_ONLY;
685 /* try with just the version */
686 if ((ret = selection_depglob(pool, selection, rname, flags)) == 0)
688 /* no luck, try with version-release */
689 if ((r2 = strrchr(rname, '-')) == 0)
697 if ((ret = selection_depglob(pool, selection, rname, flags)) == 0)
704 selection_limit_rel(pool, selection, REL_ARCH, archid);
705 selection_limit_evr(pool, selection, r + 1); /* magic epoch promotion */
711 selection_make(Pool *pool, Queue *selection, const char *name, int flags)
716 queue_empty(selection);
717 if (*name == '/' && (flags & SELECTION_FILELIST))
718 ret = selection_filelist(pool, selection, name, flags);
719 if (!ret && (r = strpbrk(name, "<=>")) != 0)
720 ret = selection_rel(pool, selection, name, flags);
722 ret = selection_depglob_arch(pool, selection, name, flags);
723 if (!ret && (flags & SELECTION_NAME) != 0)
724 ret = selection_nevra(pool, selection, name, flags);
725 if (ret && (flags & SELECTION_FLAT) != 0)
726 selection_flatten(pool, selection);
731 selection_limit(Pool *pool, Queue *sel1, Queue *sel2)
739 if (!sel1->count || !sel2->count)
744 if (sel1->count == 2 && (sel1->elements[0] & SOLVER_SELECTMASK) == SOLVER_SOLVABLE_ALL)
746 /* XXX: not 100% correct, but very useful */
748 queue_init_clone(sel1, sel2);
752 map_init(&m2, pool->nsolvables);
753 for (i = 0; i < sel2->count; i += 2)
755 Id select = sel2->elements[i] & SOLVER_SELECTMASK;
756 if (select == SOLVER_SOLVABLE_ALL)
758 if (select == SOLVER_SOLVABLE_REPO)
761 Repo *repo = pool_id2repo(pool, sel2->elements[i + 1]);
763 FOR_REPO_SOLVABLES(repo, p, s)
768 FOR_JOB_SELECT(p, pp, select, sel2->elements[i + 1])
772 if (sel2->count == 2) /* XXX: AND all setmasks instead? */
773 setflags = sel2->elements[0] & SOLVER_SETMASK & ~SOLVER_NOAUTOSET;
774 for (i = j = 0; i < sel1->count; i += 2)
776 Id select = sel1->elements[i] & SOLVER_SELECTMASK;
779 if (select == SOLVER_SOLVABLE_ALL)
781 FOR_POOL_SOLVABLES(p)
789 else if (select == SOLVER_SOLVABLE_REPO)
792 Repo *repo = pool_id2repo(pool, sel1->elements[i + 1]);
794 FOR_REPO_SOLVABLES(repo, p, s)
804 FOR_JOB_SELECT(p, pp, select, sel1->elements[i + 1])
807 queue_pushunique(&q1, p);
816 sel1->elements[j] = sel1->elements[i] | setflags;
817 sel1->elements[j + 1] = sel1->elements[i + 1];
819 else if (q1.count > 1)
821 sel1->elements[j] = (sel1->elements[i] & ~SOLVER_SELECTMASK) | SOLVER_SOLVABLE_ONE_OF | setflags;
822 sel1->elements[j + 1] = pool_queuetowhatprovides(pool, &q1);
826 sel1->elements[j] = (sel1->elements[i] & ~SOLVER_SELECTMASK) | SOLVER_SOLVABLE | SOLVER_NOAUTOSET | setflags;
827 sel1->elements[j + 1] = q1.elements[0];
831 queue_truncate(sel1, j);
835 selection_add(Pool *pool, Queue *sel1, Queue *sel2)
838 for (i = 0; i < sel2->count; i++)
839 queue_push(sel1, sel2->elements[i]);