2 * Copyright (c) 2012, 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 <= 2)
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_filter_rel(Pool *pool, Queue *selection, Id relflags, Id relevr)
159 for (i = 0; i < selection->count; i += 2)
161 Id select = selection->elements[i] & SOLVER_SELECTMASK;
162 Id id = selection->elements[i + 1];
163 if (select == SOLVER_SOLVABLE || select == SOLVER_SOLVABLE_ONE_OF)
165 /* done by selection_addsrc */
168 Id rel = 0, relname = 0;
172 FOR_JOB_SELECT(p, pp, select, id)
174 Solvable *s = pool->solvables + p;
175 if (!rel || s->name != relname)
178 rel = pool_rel2id(pool, relname, relevr, relflags, 1);
180 if (pool_match_nevr(pool, s, rel))
189 selection->elements[i] = SOLVER_SOLVABLE | SOLVER_NOAUTOSET;
190 selection->elements[i + 1] = q.elements[0];
194 selection->elements[i] = SOLVER_SOLVABLE_ONE_OF;
195 selection->elements[i + 1] = pool_queuetowhatprovides(pool, &q);
200 else if (select == SOLVER_SOLVABLE_NAME || select == SOLVER_SOLVABLE_PROVIDES)
202 /* don't stack src reldeps */
203 if (relflags == REL_ARCH && (relevr == ARCH_SRC || relevr == ARCH_NOSRC) && ISRELDEP(id))
205 Reldep *rd = GETRELDEP(pool, id);
206 if (rd->flags == REL_ARCH && rd->evr == ARCH_SRC)
209 selection->elements[i + 1] = pool_rel2id(pool, id, relevr, relflags, 1);
212 continue; /* actually internal error */
213 if (relflags == REL_ARCH)
214 selection->elements[i] |= SOLVER_SETARCH;
215 if (relflags == REL_EQ && select != SOLVER_SOLVABLE_PROVIDES)
217 if (pool->disttype == DISTTYPE_DEB)
218 selection->elements[i] |= SOLVER_SETEVR; /* debian can't match version only like rpm */
220 selection->elements[i] |= strchr(pool_id2str(pool, relevr), '-') != 0 ? SOLVER_SETEVR : SOLVER_SETEV;
223 selection_prune(pool, selection);
227 selection_addsrc(Pool *pool, Queue *selection, int flags)
233 if ((flags & SELECTION_INSTALLED_ONLY) != 0)
234 return; /* sources can't be installed */
236 for (i = 0; i < selection->count; i += 2)
238 if (selection->elements[i] != SOLVER_SOLVABLE_NAME)
240 name = selection->elements[i + 1];
243 FOR_POOL_SOLVABLES(p)
245 Solvable *s = pool->solvables + p;
248 if (s->arch == ARCH_SRC || s->arch == ARCH_NOSRC)
250 if (pool_disabled_solvable(pool, s))
254 else if (s->repo != pool->installed && !pool_installable(pool, s))
258 if (!havesrc || !q.count)
262 selection->elements[i] = SOLVER_SOLVABLE | SOLVER_NOAUTOSET;
263 selection->elements[i + 1] = q.elements[0];
267 selection->elements[i] = SOLVER_SOLVABLE_ONE_OF;
268 selection->elements[i + 1] = pool_queuetowhatprovides(pool, &q);
275 selection_depglob(Pool *pool, Queue *selection, const char *name, int flags)
282 if ((flags & SELECTION_SOURCE_ONLY) != 0)
284 flags &= ~SELECTION_PROVIDES; /* sources don't provide anything */
285 flags &= ~SELECTION_WITH_SOURCE;
288 if (!(flags & (SELECTION_NAME|SELECTION_PROVIDES)))
291 if ((flags & SELECTION_INSTALLED_ONLY) != 0 && !pool->installed)
294 if (!(flags & SELECTION_NOCASE))
296 id = pool_str2id(pool, name, 0);
299 if ((flags & (SELECTION_SOURCE_ONLY | SELECTION_WITH_SOURCE)) != 0 && (flags & SELECTION_NAME) != 0)
301 /* src rpms don't have provides, so we must check every solvable */
302 FOR_PROVIDES(p, pp, id) /* try fast path first */
304 Solvable *s = pool->solvables + p;
307 if ((flags & SELECTION_INSTALLED_ONLY) != 0 && s->repo != pool->installed)
309 if ((flags & SELECTION_SOURCE_ONLY) != 0)
310 id = pool_rel2id(pool, id, ARCH_SRC, REL_ARCH, 1);
311 queue_push2(selection, SOLVER_SOLVABLE_NAME, id);
312 if ((flags & SELECTION_WITH_SOURCE) != 0)
313 selection_addsrc(pool, selection, flags);
314 return SELECTION_NAME;
317 FOR_POOL_SOLVABLES(p) /* slow path */
319 Solvable *s = pool->solvables + p;
320 if (s->name == id && (s->arch == ARCH_SRC || s->arch == ARCH_NOSRC))
322 if ((flags & SELECTION_INSTALLED_ONLY) != 0 && s->repo != pool->installed)
323 continue; /* just in case... src rpms can't be installed */
324 if (pool_disabled_solvable(pool, s))
326 if ((flags & SELECTION_SOURCE_ONLY) != 0)
327 id = pool_rel2id(pool, id, ARCH_SRC, REL_ARCH, 1);
328 queue_push2(selection, SOLVER_SOLVABLE_NAME, id);
329 if ((flags & SELECTION_WITH_SOURCE) != 0)
330 selection_addsrc(pool, selection, flags);
331 return SELECTION_NAME;
335 FOR_PROVIDES(p, pp, id)
337 Solvable *s = pool->solvables + p;
338 if ((flags & SELECTION_INSTALLED_ONLY) != 0 && s->repo != pool->installed)
341 if (s->name == id && (flags & SELECTION_NAME) != 0)
343 if ((flags & SELECTION_SOURCE_ONLY) != 0)
344 id = pool_rel2id(pool, id, ARCH_SRC, REL_ARCH, 1);
345 queue_push2(selection, SOLVER_SOLVABLE_NAME, id);
346 if ((flags & SELECTION_WITH_SOURCE) != 0)
347 selection_addsrc(pool, selection, flags);
348 return SELECTION_NAME;
351 if (match && (flags & SELECTION_PROVIDES) != 0)
353 queue_push2(selection, SOLVER_SOLVABLE_PROVIDES, id);
354 return SELECTION_PROVIDES;
359 if ((flags & SELECTION_GLOB) != 0 && strpbrk(name, "[*?") != 0)
362 if (!doglob && !(flags & SELECTION_NOCASE))
365 if (doglob && (flags & SELECTION_NOCASE) != 0)
366 globflags = FNM_CASEFOLD;
368 #if 0 /* doesn't work with selection_filter_rel yet */
369 if (doglob && !strcmp(name, "*") && (flags & SELECTION_FLAT) != 0)
371 /* can't do this for SELECTION_PROVIDES, as src rpms don't provide anything */
372 if ((flags & SELECTION_NAME) != 0)
374 queue_push2(selection, SOLVER_SOLVABLE_ALL, 0);
375 return SELECTION_NAME;
380 if ((flags & SELECTION_NAME) != 0)
382 /* looks like a name glob. hard work. */
383 FOR_POOL_SOLVABLES(p)
385 Solvable *s = pool->solvables + p;
386 if (s->repo != pool->installed && !pool_installable(pool, s))
388 if (!(flags & SELECTION_SOURCE_ONLY) || (s->arch != ARCH_SRC && s->arch != ARCH_NOSRC))
390 if (pool_disabled_solvable(pool, s))
393 if ((flags & SELECTION_INSTALLED_ONLY) != 0 && s->repo != pool->installed)
396 if ((doglob ? fnmatch(name, pool_id2str(pool, id), globflags) : strcasecmp(name, pool_id2str(pool, id))) == 0)
398 if ((flags & SELECTION_SOURCE_ONLY) != 0)
399 id = pool_rel2id(pool, id, ARCH_SRC, REL_ARCH, 1);
400 /* queue_pushunique2 */
401 for (i = 0; i < selection->count; i += 2)
402 if (selection->elements[i] == SOLVER_SOLVABLE_NAME && selection->elements[i + 1] == id)
404 if (i == selection->count)
405 queue_push2(selection, SOLVER_SOLVABLE_NAME, id);
411 if ((flags & SELECTION_WITH_SOURCE) != 0)
412 selection_addsrc(pool, selection, flags);
413 return SELECTION_NAME;
416 if ((flags & SELECTION_PROVIDES))
418 /* looks like a dep glob. really hard work. */
419 for (id = 1; id < pool->ss.nstrings; id++)
421 if (!pool->whatprovides[id])
423 if ((doglob ? fnmatch(name, pool_id2str(pool, id), globflags) : strcasecmp(name, pool_id2str(pool, id))) == 0)
425 if ((flags & SELECTION_INSTALLED_ONLY) != 0)
427 FOR_PROVIDES(p, pp, id)
428 if (pool->solvables[p].repo == pool->installed)
433 queue_push2(selection, SOLVER_SOLVABLE_PROVIDES, id);
438 return SELECTION_PROVIDES;
444 selection_depglob_arch(Pool *pool, Queue *selection, const char *name, int flags)
450 if ((ret = selection_depglob(pool, selection, name, flags)) != 0)
452 if (!(flags & SELECTION_DOTARCH))
454 /* check if there is an .arch suffix */
455 if ((r = strrchr(name, '.')) != 0 && r[1] && (archid = str2archid(pool, r + 1)) != 0)
457 char *rname = solv_strdup(name);
459 if (archid == ARCH_SRC || archid == ARCH_NOSRC)
460 flags |= SELECTION_SOURCE_ONLY;
461 if ((ret = selection_depglob(pool, selection, rname, flags)) != 0)
463 selection_filter_rel(pool, selection, REL_ARCH, archid);
465 return ret | SELECTION_DOTARCH;
473 selection_filelist(Pool *pool, Queue *selection, const char *name, int flags)
479 type = !(flags & SELECTION_GLOB) || strpbrk(name, "[*?") == 0 ? SEARCH_STRING : SEARCH_GLOB;
480 if ((flags & SELECTION_NOCASE) != 0)
481 type |= SEARCH_NOCASE;
483 dataiterator_init(&di, pool, flags & SELECTION_INSTALLED_ONLY ? pool->installed : 0, 0, SOLVABLE_FILELIST, name, type|SEARCH_FILES|SEARCH_COMPLETE_FILELIST);
484 while (dataiterator_step(&di))
486 Solvable *s = pool->solvables + di.solvid;
489 if (s->repo != pool->installed && !pool_installable(pool, s))
491 if (!(flags & SELECTION_SOURCE_ONLY) || (s->arch != ARCH_SRC && s->arch != ARCH_NOSRC))
493 if (pool_disabled_solvable(pool, s))
496 if ((flags & SELECTION_INSTALLED_ONLY) != 0 && s->repo != pool->installed)
498 queue_push(&q, di.solvid);
499 dataiterator_skip_solvable(&di);
501 dataiterator_free(&di);
505 queue_push2(selection, SOLVER_SOLVABLE_ONE_OF, pool_queuetowhatprovides(pool, &q));
507 queue_push2(selection, SOLVER_SOLVABLE | SOLVER_NOAUTOSET, q.elements[0]);
509 return SELECTION_FILELIST;
513 selection_rel(Pool *pool, Queue *selection, const char *name, int flags)
518 /* relation case, support:
522 rname = solv_strdup(name);
523 if ((r = strpbrk(rname, "<=>")) != 0)
525 int nend = r - rname;
537 while (*r && *r == ' ' && *r == '\t')
539 while (nend && (rname[nend - 1] == ' ' || rname[nend -1 ] == '\t'))
548 if ((ret = selection_depglob_arch(pool, selection, rname, flags)) != 0)
551 selection_filter_rel(pool, selection, rflags, pool_str2id(pool, r, 1));
553 return ret | SELECTION_REL;
559 #if defined(MULTI_SEMANTICS)
560 # define EVRCMP_DEPCMP (pool->disttype == DISTTYPE_DEB ? EVRCMP_COMPARE : EVRCMP_MATCH_RELEASE)
561 #elif defined(DEBIAN)
562 # define EVRCMP_DEPCMP EVRCMP_COMPARE
564 # define EVRCMP_DEPCMP EVRCMP_MATCH_RELEASE
567 /* magic epoch promotion code, works only for SELECTION_NAME selections */
569 selection_filter_evr(Pool *pool, Queue *selection, char *evr)
576 queue_init_buffer(&q, qbuf, sizeof(qbuf)/sizeof(*qbuf));
577 for (i = j = 0; i < selection->count; i += 2)
579 Id select = selection->elements[i] & SOLVER_SELECTMASK;
580 Id id = selection->elements[i + 1];
582 const char *lastepoch = 0;
583 int lastepochlen = 0;
586 FOR_JOB_SELECT(p, pp, select, id)
588 Solvable *s = pool->solvables + p;
589 const char *sevr = pool_id2str(pool, s->evr);
591 for (sp = sevr; *sp >= '0' && *sp <= '9'; sp++)
595 /* compare vr part */
596 if (strcmp(evr, sp != sevr ? sp + 1 : sevr) != 0)
598 int r = pool_evrcmp_str(pool, sp != sevr ? sp + 1 : sevr, evr, EVRCMP_DEPCMP);
599 if (r == -1 || r == 1)
600 continue; /* solvable does not match vr */
605 while (sevr < sp && *sevr == '0') /* normalize epoch */
611 lastepochlen = sp - sevr;
613 else if (lastepochlen != sp - sevr || strncmp(lastepoch, sevr, lastepochlen) != 0)
614 lastepochlen = -1; /* multiple different epochs */
616 if (!lastepoch || lastepochlen == 0)
617 id = pool_str2id(pool, evr, 1); /* no match at all or zero epoch */
618 else if (lastepochlen >= 0)
620 /* found exactly one epoch, simply prepend */
621 char *evrx = solv_malloc(strlen(evr) + lastepochlen + 2);
622 strncpy(evrx, lastepoch, lastepochlen + 1);
623 strcpy(evrx + lastepochlen + 1, evr);
624 id = pool_str2id(pool, evrx, 1);
629 /* multiple epochs in multiple solvables, convert to list of solvables */
630 selection->elements[j] = (selection->elements[i] & ~SOLVER_SELECTMASK) | SOLVER_SOLVABLE_ONE_OF;
631 selection->elements[j + 1] = pool_queuetowhatprovides(pool, &q);
636 queue_push2(&q, selection->elements[i], selection->elements[i + 1]);
637 selection_filter_rel(pool, &q, REL_EQ, id);
639 continue; /* oops, no match */
640 selection->elements[j] = q.elements[0];
641 selection->elements[j + 1] = q.elements[1];
644 queue_truncate(selection, j);
648 /* match the "canonical" name of the package */
650 selection_canon(Pool *pool, Queue *selection, const char *name, int flags)
652 char *rname, *r, *r2;
658 * nameglob-version.arch
659 * nameglob-version-release
660 * nameglob-version-release.arch
662 flags |= SELECTION_NAME;
663 flags &= ~SELECTION_PROVIDES;
665 if (pool->disttype == DISTTYPE_DEB)
667 if ((r = strchr(name, '_')) == 0)
669 rname = solv_strdup(name); /* so we can modify it */
670 r = rname + (r - name);
672 if ((ret = selection_depglob(pool, selection, rname, flags)) == 0)
677 /* is there a vaild arch? */
678 if ((r2 = strchr(r, '_')) != 0 && r[1] && (archid = str2archid(pool, r + 1)) != 0)
680 *r2 = 0; /* split off */
681 selection_filter_rel(pool, selection, REL_ARCH, archid);
683 selection_filter_rel(pool, selection, REL_EQ, pool_str2id(pool, r, 1));
685 return ret | SELECTION_CANON;
688 if ((r = strrchr(name, '-')) == 0)
690 rname = solv_strdup(name); /* so we can modify it */
691 r = rname + (r - name);
694 /* split off potential arch part from version */
695 if ((r2 = strrchr(r + 1, '.')) != 0 && r2[1] && (archid = str2archid(pool, r2 + 1)) != 0)
696 *r2 = 0; /* found valid arch, split it off */
697 if (archid == ARCH_SRC || archid == ARCH_NOSRC)
698 flags |= SELECTION_SOURCE_ONLY;
700 /* try with just the version */
701 if ((ret = selection_depglob(pool, selection, rname, flags)) == 0)
703 /* no luck, try with version-release */
704 if ((r2 = strrchr(rname, '-')) == 0)
712 if ((ret = selection_depglob(pool, selection, rname, flags)) == 0)
719 selection_filter_rel(pool, selection, REL_ARCH, archid);
720 selection_filter_evr(pool, selection, r + 1); /* magic epoch promotion */
722 return ret | SELECTION_CANON;
726 selection_make(Pool *pool, Queue *selection, const char *name, int flags)
731 queue_empty(selection);
732 if (*name == '/' && (flags & SELECTION_FILELIST))
733 ret = selection_filelist(pool, selection, name, flags);
734 if (!ret && (flags & SELECTION_REL) != 0 && (r = strpbrk(name, "<=>")) != 0)
735 ret = selection_rel(pool, selection, name, flags);
737 ret = selection_depglob_arch(pool, selection, name, flags);
738 if (!ret && (flags & SELECTION_CANON) != 0)
739 ret = selection_canon(pool, selection, name, flags);
740 if (ret && !selection->count)
741 ret = 0; /* no match -> always return zero */
742 if (ret && (flags & SELECTION_FLAT) != 0)
743 selection_flatten(pool, selection);
748 selection_filter(Pool *pool, Queue *sel1, Queue *sel2)
756 if (!sel1->count || !sel2->count)
761 if (sel1->count == 2 && (sel1->elements[0] & SOLVER_SELECTMASK) == SOLVER_SOLVABLE_ALL)
763 /* XXX: not 100% correct, but very useful */
765 queue_init_clone(sel1, sel2);
769 map_init(&m2, pool->nsolvables);
770 for (i = 0; i < sel2->count; i += 2)
772 Id select = sel2->elements[i] & SOLVER_SELECTMASK;
773 if (select == SOLVER_SOLVABLE_ALL)
775 if (select == SOLVER_SOLVABLE_REPO)
778 Repo *repo = pool_id2repo(pool, sel2->elements[i + 1]);
780 FOR_REPO_SOLVABLES(repo, p, s)
785 FOR_JOB_SELECT(p, pp, select, sel2->elements[i + 1])
789 if (sel2->count == 2) /* XXX: AND all setmasks instead? */
790 setflags = sel2->elements[0] & SOLVER_SETMASK & ~SOLVER_NOAUTOSET;
791 for (i = j = 0; i < sel1->count; i += 2)
793 Id select = sel1->elements[i] & SOLVER_SELECTMASK;
796 if (select == SOLVER_SOLVABLE_ALL)
798 FOR_POOL_SOLVABLES(p)
806 else if (select == SOLVER_SOLVABLE_REPO)
809 Repo *repo = pool_id2repo(pool, sel1->elements[i + 1]);
811 FOR_REPO_SOLVABLES(repo, p, s)
821 FOR_JOB_SELECT(p, pp, select, sel1->elements[i + 1])
824 queue_pushunique(&q1, p);
833 sel1->elements[j] = sel1->elements[i] | setflags;
834 sel1->elements[j + 1] = sel1->elements[i + 1];
836 else if (q1.count > 1)
838 sel1->elements[j] = (sel1->elements[i] & ~SOLVER_SELECTMASK) | SOLVER_SOLVABLE_ONE_OF | setflags;
839 sel1->elements[j + 1] = pool_queuetowhatprovides(pool, &q1);
843 sel1->elements[j] = (sel1->elements[i] & ~SOLVER_SELECTMASK) | SOLVER_SOLVABLE | SOLVER_NOAUTOSET | setflags;
844 sel1->elements[j + 1] = q1.elements[0];
848 queue_truncate(sel1, j);
852 selection_add(Pool *pool, Queue *sel1, Queue *sel2)
855 for (i = 0; i < sel2->count; i++)
856 queue_push(sel1, sel2->elements[i]);