1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
|
/*
* Copyright (c) 2017, SUSE LLC.
*
* This program is licensed under the BSD license, read LICENSE.BSD
* for further information
*/
/* Functions that help getting/setting userinstalled packages. */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include "solver.h"
#include "solver_private.h"
#include "bitmap.h"
#include "pool.h"
#include "util.h"
#include "poolarch.h"
#include "linkedpkg.h"
static int
get_userinstalled_cmp(const void *ap, const void *bp, void *dp)
{
return *(Id *)ap - *(Id *)bp;
}
static int
get_userinstalled_cmp_names(const void *ap, const void *bp, void *dp)
{
Pool *pool = dp;
return strcmp(pool_id2str(pool, *(Id *)ap), pool_id2str(pool, *(Id *)bp));
}
static int
get_userinstalled_cmp_namearch(const void *ap, const void *bp, void *dp)
{
Pool *pool = dp;
int r;
r = strcmp(pool_id2str(pool, ((Id *)ap)[0]), pool_id2str(pool, ((Id *)bp)[0]));
if (r)
return r;
return strcmp(pool_id2str(pool, ((Id *)ap)[1]), pool_id2str(pool, ((Id *)bp)[1]));
}
static void
get_userinstalled_sort_uniq(Pool *pool, Queue *q, int flags)
{
Id lastp = -1, lasta = -1;
int i, j;
if (q->count < ((flags & GET_USERINSTALLED_NAMEARCH) ? 4 : 2))
return;
if ((flags & GET_USERINSTALLED_NAMEARCH) != 0)
solv_sort(q->elements, q->count / 2, 2 * sizeof(Id), get_userinstalled_cmp_namearch, pool);
else if ((flags & GET_USERINSTALLED_NAMES) != 0)
solv_sort(q->elements, q->count, sizeof(Id), get_userinstalled_cmp_names, pool);
else
solv_sort(q->elements, q->count, sizeof(Id), get_userinstalled_cmp, 0);
if ((flags & GET_USERINSTALLED_NAMEARCH) != 0)
{
for (i = j = 0; i < q->count; i += 2)
if (q->elements[i] != lastp || q->elements[i + 1] != lasta)
{
q->elements[j++] = lastp = q->elements[i];
q->elements[j++] = lasta = q->elements[i + 1];
}
}
else
{
for (i = j = 0; i < q->count; i++)
if (q->elements[i] != lastp)
q->elements[j++] = lastp = q->elements[i];
}
queue_truncate(q, j);
}
static void
namearch2solvables(Pool *pool, Queue *q, Queue *qout, int job)
{
int i;
if (!pool->installed)
return;
for (i = 0; i < q->count; i += 2)
{
Id p, pp, name = q->elements[i], arch = q->elements[i + 1];
FOR_PROVIDES(p, pp, name)
{
Solvable *s = pool->solvables + p;
if (s->repo != pool->installed || s->name != name || (arch && s->arch != arch))
continue;
if (job)
queue_push(qout, job);
queue_push(qout, p);
}
}
}
void
solver_get_userinstalled(Solver *solv, Queue *q, int flags)
{
Pool *pool = solv->pool;
Id p, p2, pp;
Solvable *s;
Repo *installed = solv->installed;
int i, j;
Map userinstalled;
map_init(&userinstalled, 0);
queue_empty(q);
/* first process jobs */
for (i = 0; i < solv->job.count; i += 2)
{
Id how = solv->job.elements[i];
Id what, select;
if (installed && (how & SOLVER_JOBMASK) == SOLVER_USERINSTALLED)
{
if (!userinstalled.size)
map_grow(&userinstalled, installed->end - installed->start);
what = solv->job.elements[i + 1];
select = how & SOLVER_SELECTMASK;
if (select == SOLVER_SOLVABLE_ALL || (select == SOLVER_SOLVABLE_REPO && what == installed->repoid))
{
FOR_REPO_SOLVABLES(installed, p, s)
MAPSET(&userinstalled, p - installed->start);
}
FOR_JOB_SELECT(p, pp, select, what)
if (pool->solvables[p].repo == installed)
MAPSET(&userinstalled, p - installed->start);
continue;
}
if ((how & SOLVER_JOBMASK) != SOLVER_INSTALL)
continue;
if ((how & SOLVER_NOTBYUSER) != 0)
continue;
what = solv->job.elements[i + 1];
select = how & SOLVER_SELECTMASK;
FOR_JOB_SELECT(p, pp, select, what)
if (solv->decisionmap[p] > 0)
{
queue_push(q, p);
#ifdef ENABLE_LINKED_PKGS
if (has_package_link(pool, pool->solvables + p))
{
int j;
Queue lq;
queue_init(&lq);
find_package_link(pool, pool->solvables + p, 0, &lq, 0, 0);
for (j = 0; j < lq.count; j++)
if (solv->decisionmap[lq.elements[j]] > 0)
queue_push(q, lq.elements[j]);
}
#endif
}
}
/* now process updates of userinstalled packages */
if (installed && userinstalled.size)
{
for (i = 1; i < solv->decisionq.count; i++)
{
p = solv->decisionq.elements[i];
if (p <= 0)
continue;
s = pool->solvables + p;
if (!s->repo)
continue;
if (s->repo == installed)
{
if (MAPTST(&userinstalled, p - installed->start))
queue_push(q, p);
continue;
}
/* new package, check if we replace a userinstalled one */
FOR_PROVIDES(p2, pp, s->name)
{
Solvable *ps = pool->solvables + p2;
if (p2 == p || ps->repo != installed || !MAPTST(&userinstalled, p2 - installed->start))
continue;
if (!pool->implicitobsoleteusesprovides && s->name != ps->name)
continue;
if (pool->implicitobsoleteusescolors && !pool_colormatch(pool, s, ps))
continue;
queue_push(q, p);
break;
}
if (!p2 && s->repo != installed && s->obsoletes)
{
Id obs, *obsp = s->repo->idarraydata + s->obsoletes;
while ((obs = *obsp++) != 0)
{
FOR_PROVIDES(p2, pp, obs)
{
Solvable *ps = pool->solvables + p2;
if (p2 == p || ps->repo != installed || !MAPTST(&userinstalled, p2 - installed->start))
continue;
if (!pool->obsoleteusesprovides && !pool_match_nevr(pool, ps, obs))
continue;
if (pool->obsoleteusescolors && !pool_colormatch(pool, s, ps))
continue;
queue_push(q, p);
break;
}
if (p2)
break;
}
}
}
}
map_free(&userinstalled);
/* convert to desired output format */
if ((flags & GET_USERINSTALLED_NAMEARCH) != 0)
{
int qcount = q->count;
queue_insertn(q, 0, qcount, 0);
for (i = j = 0; i < qcount; i++)
{
s = pool->solvables + q->elements[i + qcount];
q->elements[j++] = s->name;
q->elements[j++] = s->arch;
}
}
else if ((flags & GET_USERINSTALLED_NAMES) != 0)
{
for (i = 0; i < q->count; i++)
{
s = pool->solvables + q->elements[i];
q->elements[i] = s->name;
}
}
/* sort and unify */
get_userinstalled_sort_uniq(pool, q, flags);
/* invert if asked for */
if ((flags & GET_USERINSTALLED_INVERTED) != 0)
{
/* first generate queue with all installed packages */
Queue invq;
queue_init(&invq);
for (i = 1; i < solv->decisionq.count; i++)
{
p = solv->decisionq.elements[i];
if (p <= 0)
continue;
s = pool->solvables + p;
if (!s->repo)
continue;
if ((flags & GET_USERINSTALLED_NAMEARCH) != 0)
queue_push2(&invq, s->name, s->arch);
else if ((flags & GET_USERINSTALLED_NAMES) != 0)
queue_push(&invq, s->name);
else
queue_push(&invq, p);
}
/* push q on invq, just in case... */
queue_insertn(&invq, invq.count, q->count, q->elements);
get_userinstalled_sort_uniq(pool, &invq, flags);
/* subtract queues (easy as they are sorted and invq is a superset of q) */
if ((flags & GET_USERINSTALLED_NAMEARCH) != 0)
{
if (q->count)
{
for (i = j = 0; i < invq.count; i += 2)
if (invq.elements[i] == q->elements[j] && invq.elements[i + 1] == q->elements[j + 1])
{
invq.elements[i] = invq.elements[i + 1] = 0;
j += 2;
if (j >= q->count)
break;
}
queue_empty(q);
}
for (i = 0; i < invq.count; i += 2)
if (invq.elements[i])
queue_push2(q, invq.elements[i], invq.elements[i + 1]);
}
else
{
if (q->count)
{
for (i = j = 0; i < invq.count; i++)
if (invq.elements[i] == q->elements[j])
{
invq.elements[i] = 0;
if (++j >= q->count)
break;
}
queue_empty(q);
}
for (i = 0; i < invq.count; i++)
if (invq.elements[i])
queue_push(q, invq.elements[i]);
}
queue_free(&invq);
}
}
void
pool_add_userinstalled_jobs(Pool *pool, Queue *q, Queue *job, int flags)
{
int i;
if ((flags & GET_USERINSTALLED_INVERTED) != 0)
{
Queue invq;
Id p, lastid;
Solvable *s;
int bad;
if (!pool->installed)
return;
queue_init(&invq);
if ((flags & GET_USERINSTALLED_NAMEARCH) != 0)
flags &= ~GET_USERINSTALLED_NAMES; /* just in case */
FOR_REPO_SOLVABLES(pool->installed, p, s)
queue_push(&invq, flags & GET_USERINSTALLED_NAMES ? s->name : p);
if ((flags & GET_USERINSTALLED_NAMEARCH) != 0)
{
/* for namearch we convert to packages */
namearch2solvables(pool, q, &invq, 0);
get_userinstalled_sort_uniq(pool, &invq, flags);
namearch2solvables(pool, q, &invq, 0);
flags = 0;
}
else
{
queue_insertn(&invq, invq.count, q->count, q->elements);
get_userinstalled_sort_uniq(pool, &invq, flags);
/* now the fun part, add q again, sort, and remove all dups */
queue_insertn(&invq, invq.count, q->count, q->elements);
}
if (invq.count > 1)
{
if ((flags & GET_USERINSTALLED_NAMES) != 0)
solv_sort(invq.elements, invq.count, sizeof(Id), get_userinstalled_cmp_names, pool);
else
solv_sort(invq.elements, invq.count, sizeof(Id), get_userinstalled_cmp, 0);
}
lastid = -1;
bad = 1;
for (i = 0; i < invq.count; i++)
{
if (invq.elements[i] == lastid)
{
bad = 1;
continue;
}
if (!bad)
queue_push2(job, SOLVER_USERINSTALLED | (flags & GET_USERINSTALLED_NAMES ? SOLVER_SOLVABLE_NAME : SOLVER_SOLVABLE), lastid);
bad = 0;
lastid = invq.elements[i];
}
if (!bad)
queue_push2(job, SOLVER_USERINSTALLED | (flags & GET_USERINSTALLED_NAMES ? SOLVER_SOLVABLE_NAME : SOLVER_SOLVABLE), lastid);
queue_free(&invq);
}
else
{
if (flags & GET_USERINSTALLED_NAMEARCH)
namearch2solvables(pool, q, job, SOLVER_USERINSTALLED | SOLVER_SOLVABLE);
else
{
for (i = 0; i < q->count; i++)
queue_push2(job, SOLVER_USERINSTALLED | (flags & GET_USERINSTALLED_NAMES ? SOLVER_SOLVABLE_NAME : SOLVER_SOLVABLE), q->elements[i]);
}
}
}
|