rattler_libsolv_c 1.2.3

Bindings for libsolv
Documentation
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
/*
 * Copyright (c) 2017, SUSE Inc.
 *
 * This program is licensed under the BSD license, read LICENSE.BSD
 * for further information
 */

/*
 * solver_util.c
 *
 * Dependency solver helper functions
 */

#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 "poolarch.h"
#include "util.h"
#include "evr.h"


/*-------------------------------------------------------------------
 * check if a installed package p is being updated
 */
static int
solver_is_updating(Solver *solv, Id p)
{
  /* check if the update rule is true */
  Pool *pool = solv->pool;
  Rule *r;
  Id l, pp;
  if (solv->decisionmap[p] >= 0)
    return 0;	/* old package stayed */
  r = solv->rules + solv->featurerules + (p - solv->installed->start);
  if (!r->p)
    r = solv->rules + solv->updaterules + (p - solv->installed->start);
  FOR_RULELITERALS(l, pp, r)
    if (l > 0 && l != p && solv->decisionmap[l] > 0)
      {
	/* check that this is really an upgrade */
	Solvable *si = pool->solvables + p;
	Solvable *s = pool->solvables + l;
	if (s->name != si->name || pool_evrcmp(pool, s->evr, si->evr, EVRCMP_COMPARE) > 0)
          return 1;
      }
  return 0;
}

/*-------------------------------------------------------------------
 * handle split provides
 *
 * a splitprovides dep looks like
 *     namespace:splitprovides(pkg REL_WITH path)
 * and is only true if pkg is installed and contains the specified path.
 * we also make sure that pkg is selected for an update, otherwise the
 * update would always be forced onto the user.
 * Map m is the map used when called from dep_possible.
 */
int
solver_splitprovides(Solver *solv, Id dep, Map *m)
{
  Pool *pool = solv->pool;
  Id p, pp;
  Reldep *rd;
  Solvable *s;

  if (!solv->dosplitprovides || !solv->installed)
    return 0;
  if (!ISRELDEP(dep))
    return 0;
  rd = GETRELDEP(pool, dep);
  if (rd->flags != REL_WITH)
    return 0;
  /*
   * things are a bit tricky here if pool->addedprovides == 1, because most split-provides are in
   * a non-standard location. If we simply call pool_whatprovides, we'll drag in the complete
   * file list. Instead we rely on pool_addfileprovides ignoring the addfileprovidesfiltered flag
   * for installed packages and check the lazywhatprovidesq (ignoring the REL_WITH part, but
   * we filter the package name further down anyway).
   */
  if (pool->addedfileprovides == 1 && !ISRELDEP(rd->evr) && !pool->whatprovides[rd->evr])
    pp = pool_searchlazywhatprovidesq(pool, rd->evr);
  else
    pp = pool_whatprovides(pool, dep);
  while ((p = pool->whatprovidesdata[pp++]) != 0)
    {
      /* here we have packages that provide the correct name and contain the path,
       * now do extra filtering */
      s = pool->solvables + p;
      if (s->repo != solv->installed || s->name != rd->name)
	continue;
      /* check if the package is updated. if m is set, we're called from dep_possible */
      if (m || solver_is_updating(solv, p))
	return 1;
    }
  return 0;
}

int
solver_dep_possible_slow(Solver *solv, Id dep, Map *m)
{
  Pool *pool = solv->pool;
  Id p, pp;

  if (ISRELDEP(dep))
    {
      Reldep *rd = GETRELDEP(pool, dep);
      if (rd->flags >= 8)
         {
          if (rd->flags == REL_COND || rd->flags == REL_UNLESS)
            return 1;
          if (rd->flags == REL_AND)
            {
              if (!solver_dep_possible_slow(solv, rd->name, m))
                return 0;
              return solver_dep_possible_slow(solv, rd->evr, m);
            }
          if (rd->flags == REL_OR)
            {
              if (solver_dep_possible_slow(solv, rd->name, m))
                return 1;
              return solver_dep_possible_slow(solv, rd->evr, m);
            }
          if (rd->flags == REL_NAMESPACE && rd->name == NAMESPACE_SPLITPROVIDES)
            return solver_splitprovides(solv, rd->evr, m);
        }
    }
  FOR_PROVIDES(p, pp, dep)
    {
      if (MAPTST(m, p))
        return 1;
    }
  return 0;
}

int
solver_dep_fulfilled_cplx(Solver *solv, Reldep *rd)
{
  Pool *pool = solv->pool;
  if (rd->flags == REL_COND)
    {
      if (ISRELDEP(rd->evr))
	{
	  Reldep *rd2 = GETRELDEP(pool, rd->evr);
	  if (rd2->flags == REL_ELSE)
	    {
	      if (solver_dep_fulfilled(solv, rd2->name))
		return solver_dep_fulfilled(solv, rd->name);
	      return solver_dep_fulfilled(solv, rd2->evr);
	    }
	}
      if (solver_dep_fulfilled(solv, rd->name))
	return 1;
      return !solver_dep_fulfilled(solv, rd->evr);
    }
  if (rd->flags == REL_UNLESS)
    {
      if (ISRELDEP(rd->evr))
	{
	  Reldep *rd2 = GETRELDEP(pool, rd->evr);
	  if (rd2->flags == REL_ELSE)
	    {
	      if (!solver_dep_fulfilled(solv, rd2->name))
		return solver_dep_fulfilled(solv, rd->name);
	      return solver_dep_fulfilled(solv, rd2->evr);
	    }
	}
      if (!solver_dep_fulfilled(solv, rd->name))
	return 0;
      return !solver_dep_fulfilled(solv, rd->evr);
    }
  if (rd->flags == REL_AND)
    {
      if (!solver_dep_fulfilled(solv, rd->name))
	return 0;
      return solver_dep_fulfilled(solv, rd->evr);
    }
  if (rd->flags == REL_OR)
    {
      if (solver_dep_fulfilled(solv, rd->name))
	return 1;
      return solver_dep_fulfilled(solv, rd->evr);
    }
  return 0;
}

static int
solver_dep_fulfilled_complex_func(Solver *solv, Reldep *rd, int (*dep_fullfilled)(Solver *, Id))
{
  Pool *pool = solv->pool;
  int r1, r2;
  if (rd->flags == REL_COND)
    {
      if (ISRELDEP(rd->evr))
	{
	  Reldep *rd2 = GETRELDEP(pool, rd->evr);
	  if (rd2->flags == REL_ELSE)
	    {
	      r1 = dep_fullfilled(solv, rd2->name);
	      if (r1)
		{
		  r2 = dep_fullfilled(solv, rd->name);
		  return r2 && r1 == 2 ? 2 : r2;
		}
	      return dep_fullfilled(solv, rd2->evr);
	    }
	}
      r1 = dep_fullfilled(solv, rd->name);
      r2 = !dep_fullfilled(solv, rd->evr);
      if (!r1 && !r2)
	return 0;
      return r1 == 2 ? 2 : 1;
    }
  if (rd->flags == REL_UNLESS)
    {
      if (ISRELDEP(rd->evr))
	{
	  Reldep *rd2 = GETRELDEP(pool, rd->evr);
	  if (rd2->flags == REL_ELSE)
	    {
	      r1 = dep_fullfilled(solv, rd2->name);
	      if (r1)
		{
		  r2 = dep_fullfilled(solv, rd2->evr);
		  return r2 && r1 == 2 ? 2 : r2;
		}
	      return dep_fullfilled(solv, rd->name);
	    }
	}
      /* A AND NOT(B) */
      r1 = dep_fullfilled(solv, rd->name);
      r2 = !dep_fullfilled(solv, rd->evr);
      if (!r1 || !r2)
	return 0;
      return r1 == 2 ? 2 : 1;
    }
  if (rd->flags == REL_AND)
    {
      r1 = dep_fullfilled(solv, rd->name);
      if (!r1)
	return 0;
      r2 = dep_fullfilled(solv, rd->evr);
      if (!r2)
	return 0;
      return r1 == 2 || r2 == 2 ? 2 : 1;
    }
  if (rd->flags == REL_OR)
    {
      r1 = dep_fullfilled(solv, rd->name);
      r2 = dep_fullfilled(solv, rd->evr);
      if (!r1 && !r2)
	return 0;
      return r1 == 2 || r2 == 2 ? 2 : 1;
    }
  return 0;
}

/* mirrors solver_dep_fulfilled, but returns 2 if a new package
 * was involved */
static int
solver_dep_fulfilled_alreadyinstalled(Solver *solv, Id dep)
{
  Pool *pool = solv->pool;
  Id p, pp;
  int r;

  if (ISRELDEP(dep))
    {
      Reldep *rd = GETRELDEP(pool, dep);
      if (rd->flags == REL_COND || rd->flags == REL_UNLESS || rd->flags == REL_AND || rd->flags == REL_OR)
	return solver_dep_fulfilled_complex_func(solv, rd, solver_dep_fulfilled_alreadyinstalled);
      if (rd->flags == REL_NAMESPACE && rd->name == NAMESPACE_SPLITPROVIDES)
        return solver_splitprovides(solv, rd->evr, 0) ? 2 : 0;
      if (rd->flags == REL_NAMESPACE && solv->installsuppdepq)
	{
	  Queue *q = solv->installsuppdepq;
	  int i;
	  for (i = 0; i < q->count; i++)
	    if (q->elements[i] == dep || q->elements[i] == rd->name)
	      return 2;
	}
    }
  r = 0;
  FOR_PROVIDES(p, pp, dep)
    if (solv->decisionmap[p] > 0)
      {
	Solvable *s = pool->solvables + p;
	if (s->repo && s->repo != solv->installed)
	  return 2;
        r = 1;
      }
  return r;
}

static int
solver_dep_fulfilled_namespace(Solver *solv, Id dep)
{
  Pool *pool = solv->pool;
  Id p, pp;
  int r = 1;

  if (ISRELDEP(dep))
    {
      Reldep *rd = GETRELDEP(pool, dep);
      if (rd->flags == REL_COND || rd->flags == REL_UNLESS || rd->flags == REL_AND || rd->flags == REL_OR)
	return solver_dep_fulfilled_complex_func(solv, rd, solver_dep_fulfilled_namespace);
      if (rd->flags == REL_NAMESPACE && rd->name == NAMESPACE_SPLITPROVIDES)
        return solver_splitprovides(solv, rd->evr, 0) ? 2 : 0;
      if (rd->flags == REL_NAMESPACE)
	r = 2;
    }
  FOR_PROVIDES(p, pp, dep)
    if (solv->decisionmap[p] > 0)
      return r;
  return 0;
}

int
solver_is_supplementing_alreadyinstalled(Solver *solv, Solvable *s)
{
  Id sup, *supp;
  supp = s->repo->idarraydata + s->supplements;
  while ((sup = *supp++) != 0)
    {
      if (!solv->addalreadyrecommended && solver_dep_fulfilled_alreadyinstalled(solv, sup) != 2)
	continue;
      if (solv->only_namespace_recommended && solver_dep_fulfilled_namespace(solv, sup) != 2)
	continue;
      return 1;
    }
  return 0;
}

int
solver_is_namespace_dep_slow(Solver *solv, Reldep *rd)
{
  Pool *pool = solv->pool;
  for (;;)
    {
      if (rd->flags == REL_NAMESPACE)
	return 1;
      if (ISRELDEP(rd->name) && solver_is_namespace_dep_slow(solv, GETRELDEP(pool, rd->name)))
	return 1;
      if (!ISRELDEP(rd->evr))
	return 0;
      rd = GETRELDEP(pool, rd->evr);
    }
}

/*
 * add all installed packages that package p obsoletes to Queue q.
 * Package p is not installed. Also, we know that if
 * solv->keepexplicitobsoletes is not set, p is not in the multiversion map.
 * Entries may get added multiple times.
 */
static void
solver_add_obsoleted(Solver *solv, Id p, Queue *q)
{
  Pool *pool = solv->pool;
  Repo *installed = solv->installed;
  Id p2, pp2;
  Solvable *s = pool->solvables + p;
  Id obs, *obsp;
  Id lastp2 = 0;

  if (!solv->keepexplicitobsoletes || !(solv->multiversion.size && MAPTST(&solv->multiversion, p)))
    {
      FOR_PROVIDES(p2, pp2, s->name)
        {
          Solvable *ps = pool->solvables + p2;
          if (ps->repo != installed)
            continue;
          if (!pool->implicitobsoleteusesprovides && ps->name != s->name)
            continue;
          if (pool->implicitobsoleteusescolors && !pool_colormatch(pool, s, ps))
            continue;
          queue_push(q, p2);
          lastp2 = p2;
        }
    }
  if (!s->obsoletes)
    return;
  obsp = s->repo->idarraydata + s->obsoletes;
  while ((obs = *obsp++) != 0)
    FOR_PROVIDES(p2, pp2, obs)
      {
        Solvable *ps = pool->solvables + p2;
        if (ps->repo != installed)
          continue;
        if (!pool->obsoleteusesprovides && !pool_match_nevr(pool, ps, obs))
          continue;
        if (pool->obsoleteusescolors && !pool_colormatch(pool, s, ps))
          continue;
        if (p2 == lastp2)
          continue;
        queue_push(q, p2);
        lastp2 = p2;
      }
}

/*
 * Call solver_add_obsoleted and intersect the result with the
 * elements in Queue q starting at qstart.
 * Assumes that it's the first call if qstart == q->count.
 * May use auxiliary map m for the intersection process, all
 * elements of q starting at qstart must have their bit cleared.
 * (This is also true after the function returns.)
 * (See solver_add_obsoleted for limitations of the package p)
 */
void
solver_intersect_obsoleted(Solver *solv, Id p, Queue *q, int qstart, Map *m)
{
  int i, j;
  int qcount = q->count;

  solver_add_obsoleted(solv, p, q);
  if (qcount == qstart)
    return;     /* first call */
  if (qcount == q->count)
    j = qstart;
  else if (qcount == qstart + 1)
    {
      /* easy if there's just one element */
      j = qstart;
      for (i = qcount; i < q->count; i++)
        if (q->elements[i] == q->elements[qstart])
          {
            j++;        /* keep the element */
            break;
          }
    }
  else if (!m || (!m->size && q->count - qstart <= 8))
    {
      /* faster than a map most of the time */
      int k;
      for (i = j = qstart; i < qcount; i++)
        {
          Id ip = q->elements[i];
          for (k = qcount; k < q->count; k++)
            if (q->elements[k] == ip)
              {
                q->elements[j++] = ip;
                break;
              }
        }
    }
  else
    {
      /* for the really pathologic cases we use the map */
      Repo *installed = solv->installed;
      if (!m->size)
        map_init(m, installed->end - installed->start);
      for (i = qcount; i < q->count; i++)
        MAPSET(m, q->elements[i] - installed->start);
      for (i = j = qstart; i < qcount; i++)
        if (MAPTST(m, q->elements[i] - installed->start))
          {
            MAPCLR(m, q->elements[i] - installed->start);
            q->elements[j++] = q->elements[i];
          }
    }
  queue_truncate(q, j);
}