2 * Copyright (c) 2017, SUSE LLC.
4 * This program is licensed under the BSD license, read LICENSE.BSD
5 * for further information
8 /* Functions that help getting/setting userinstalled packages. */
16 #include "solver_private.h"
21 #include "linkedpkg.h"
24 get_userinstalled_cmp(const void *ap, const void *bp, void *dp)
26 return *(Id *)ap - *(Id *)bp;
30 get_userinstalled_cmp_names(const void *ap, const void *bp, void *dp)
33 return strcmp(pool_id2str(pool, *(Id *)ap), pool_id2str(pool, *(Id *)bp));
37 get_userinstalled_cmp_namearch(const void *ap, const void *bp, void *dp)
41 r = strcmp(pool_id2str(pool, ((Id *)ap)[0]), pool_id2str(pool, ((Id *)bp)[0]));
44 return strcmp(pool_id2str(pool, ((Id *)ap)[1]), pool_id2str(pool, ((Id *)bp)[1]));
48 get_userinstalled_sort_uniq(Pool *pool, Queue *q, int flags)
50 Id lastp = -1, lasta = -1;
52 if (q->count < ((flags & GET_USERINSTALLED_NAMEARCH) ? 4 : 2))
54 if ((flags & GET_USERINSTALLED_NAMEARCH) != 0)
55 solv_sort(q->elements, q->count / 2, 2 * sizeof(Id), get_userinstalled_cmp_namearch, pool);
56 else if ((flags & GET_USERINSTALLED_NAMES) != 0)
57 solv_sort(q->elements, q->count, sizeof(Id), get_userinstalled_cmp_names, pool);
59 solv_sort(q->elements, q->count, sizeof(Id), get_userinstalled_cmp, 0);
60 if ((flags & GET_USERINSTALLED_NAMEARCH) != 0)
62 for (i = j = 0; i < q->count; i += 2)
63 if (q->elements[i] != lastp || q->elements[i + 1] != lasta)
65 q->elements[j++] = lastp = q->elements[i];
66 q->elements[j++] = lasta = q->elements[i + 1];
71 for (i = j = 0; i < q->count; i++)
72 if (q->elements[i] != lastp)
73 q->elements[j++] = lastp = q->elements[i];
79 namearch2solvables(Pool *pool, Queue *q, Queue *qout, int job)
84 for (i = 0; i < q->count; i += 2)
86 Id p, pp, name = q->elements[i], arch = q->elements[i + 1];
87 FOR_PROVIDES(p, pp, name)
89 Solvable *s = pool->solvables + p;
90 if (s->repo != pool->installed || s->name != name || (arch && s->arch != arch))
93 queue_push(qout, job);
100 solver_get_userinstalled(Solver *solv, Queue *q, int flags)
102 Pool *pool = solv->pool;
105 Repo *installed = solv->installed;
109 map_init(&userinstalled, 0);
111 /* first process jobs */
112 for (i = 0; i < solv->job.count; i += 2)
114 Id how = solv->job.elements[i];
116 if (installed && (how & SOLVER_JOBMASK) == SOLVER_USERINSTALLED)
118 if (!userinstalled.size)
119 map_grow(&userinstalled, installed->end - installed->start);
120 what = solv->job.elements[i + 1];
121 select = how & SOLVER_SELECTMASK;
122 if (select == SOLVER_SOLVABLE_ALL || (select == SOLVER_SOLVABLE_REPO && what == installed->repoid))
124 FOR_REPO_SOLVABLES(installed, p, s)
125 MAPSET(&userinstalled, p - installed->start);
127 FOR_JOB_SELECT(p, pp, select, what)
128 if (pool->solvables[p].repo == installed)
129 MAPSET(&userinstalled, p - installed->start);
132 if ((how & SOLVER_JOBMASK) != SOLVER_INSTALL)
134 if ((how & SOLVER_NOTBYUSER) != 0)
136 what = solv->job.elements[i + 1];
137 select = how & SOLVER_SELECTMASK;
138 FOR_JOB_SELECT(p, pp, select, what)
139 if (solv->decisionmap[p] > 0)
142 #ifdef ENABLE_LINKED_PKGS
143 if (has_package_link(pool, pool->solvables + p))
148 find_package_link(pool, pool->solvables + p, 0, &lq, 0, 0);
149 for (j = 0; j < lq.count; j++)
150 if (solv->decisionmap[lq.elements[j]] > 0)
151 queue_push(q, lq.elements[j]);
156 /* now process updates of userinstalled packages */
157 if (installed && userinstalled.size)
159 for (i = 1; i < solv->decisionq.count; i++)
161 p = solv->decisionq.elements[i];
164 s = pool->solvables + p;
167 if (s->repo == installed)
169 if (MAPTST(&userinstalled, p - installed->start))
173 /* new package, check if we replace a userinstalled one */
174 FOR_PROVIDES(p2, pp, s->name)
176 Solvable *ps = pool->solvables + p2;
177 if (p2 == p || ps->repo != installed || !MAPTST(&userinstalled, p2 - installed->start))
179 if (!pool->implicitobsoleteusesprovides && s->name != ps->name)
181 if (pool->implicitobsoleteusescolors && !pool_colormatch(pool, s, ps))
186 if (!p2 && s->repo != installed && s->obsoletes)
188 Id obs, *obsp = s->repo->idarraydata + s->obsoletes;
189 while ((obs = *obsp++) != 0)
191 FOR_PROVIDES(p2, pp, obs)
193 Solvable *ps = pool->solvables + p2;
194 if (p2 == p || ps->repo != installed || !MAPTST(&userinstalled, p2 - installed->start))
196 if (!pool->obsoleteusesprovides && !pool_match_nevr(pool, ps, obs))
198 if (pool->obsoleteusescolors && !pool_colormatch(pool, s, ps))
209 map_free(&userinstalled);
211 /* convert to desired output format */
212 if ((flags & GET_USERINSTALLED_NAMEARCH) != 0)
214 int qcount = q->count;
215 queue_insertn(q, 0, qcount, 0);
216 for (i = j = 0; i < qcount; i++)
218 s = pool->solvables + q->elements[i + qcount];
219 q->elements[j++] = s->name;
220 q->elements[j++] = s->arch;
223 else if ((flags & GET_USERINSTALLED_NAMES) != 0)
225 for (i = 0; i < q->count; i++)
227 s = pool->solvables + q->elements[i];
228 q->elements[i] = s->name;
232 get_userinstalled_sort_uniq(pool, q, flags);
234 /* invert if asked for */
235 if ((flags & GET_USERINSTALLED_INVERTED) != 0)
237 /* first generate queue with all installed packages */
240 for (i = 1; i < solv->decisionq.count; i++)
242 p = solv->decisionq.elements[i];
245 s = pool->solvables + p;
248 if ((flags & GET_USERINSTALLED_NAMEARCH) != 0)
249 queue_push2(&invq, s->name, s->arch);
250 else if ((flags & GET_USERINSTALLED_NAMES) != 0)
251 queue_push(&invq, s->name);
253 queue_push(&invq, p);
255 /* push q on invq, just in case... */
256 queue_insertn(&invq, invq.count, q->count, q->elements);
257 get_userinstalled_sort_uniq(pool, &invq, flags);
258 /* subtract queues (easy as they are sorted and invq is a superset of q) */
259 if ((flags & GET_USERINSTALLED_NAMEARCH) != 0)
263 for (i = j = 0; i < invq.count; i += 2)
264 if (invq.elements[i] == q->elements[j] && invq.elements[i + 1] == q->elements[j + 1])
266 invq.elements[i] = invq.elements[i + 1] = 0;
273 for (i = 0; i < invq.count; i += 2)
274 if (invq.elements[i])
275 queue_push2(q, invq.elements[i], invq.elements[i + 1]);
281 for (i = j = 0; i < invq.count; i++)
282 if (invq.elements[i] == q->elements[j])
284 invq.elements[i] = 0;
290 for (i = 0; i < invq.count; i++)
291 if (invq.elements[i])
292 queue_push(q, invq.elements[i]);
299 pool_add_userinstalled_jobs(Pool *pool, Queue *q, Queue *job, int flags)
303 if ((flags & GET_USERINSTALLED_INVERTED) != 0)
309 if (!pool->installed)
312 if ((flags & GET_USERINSTALLED_NAMEARCH) != 0)
313 flags &= ~GET_USERINSTALLED_NAMES; /* just in case */
314 FOR_REPO_SOLVABLES(pool->installed, p, s)
315 queue_push(&invq, flags & GET_USERINSTALLED_NAMES ? s->name : p);
316 if ((flags & GET_USERINSTALLED_NAMEARCH) != 0)
318 /* for namearch we convert to packages */
319 namearch2solvables(pool, q, &invq, 0);
320 get_userinstalled_sort_uniq(pool, &invq, flags);
321 namearch2solvables(pool, q, &invq, 0);
326 queue_insertn(&invq, invq.count, q->count, q->elements);
327 get_userinstalled_sort_uniq(pool, &invq, flags);
328 /* now the fun part, add q again, sort, and remove all dups */
329 queue_insertn(&invq, invq.count, q->count, q->elements);
333 if ((flags & GET_USERINSTALLED_NAMES) != 0)
334 solv_sort(invq.elements, invq.count, sizeof(Id), get_userinstalled_cmp_names, pool);
336 solv_sort(invq.elements, invq.count, sizeof(Id), get_userinstalled_cmp, 0);
340 for (i = 0; i < invq.count; i++)
342 if (invq.elements[i] == lastid)
348 queue_push2(job, SOLVER_USERINSTALLED | (flags & GET_USERINSTALLED_NAMES ? SOLVER_SOLVABLE_NAME : SOLVER_SOLVABLE), lastid);
350 lastid = invq.elements[i];
353 queue_push2(job, SOLVER_USERINSTALLED | (flags & GET_USERINSTALLED_NAMES ? SOLVER_SOLVABLE_NAME : SOLVER_SOLVABLE), lastid);
358 if (flags & GET_USERINSTALLED_NAMEARCH)
359 namearch2solvables(pool, q, job, SOLVER_USERINSTALLED | SOLVER_SOLVABLE);
362 for (i = 0; i < q->count; i++)
363 queue_push2(job, SOLVER_USERINSTALLED | (flags & GET_USERINSTALLED_NAMES ? SOLVER_SOLVABLE_NAME : SOLVER_SOLVABLE), q->elements[i]);