- bring solvable_trivial_installable functions back in sync with pool_trivial_installable
[platform/upstream/libsolv.git] / src / solvable.c
1 /*
2  * Copyright (c) 2008, Novell Inc.
3  *
4  * This program is licensed under the BSD license, read LICENSE.BSD
5  * for further information
6  */
7
8 /*
9  * solvable.c
10  *
11  * set/retrieve data from solvables
12  */
13
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <stdarg.h>
17 #include <unistd.h>
18 #include <string.h>
19
20 #include "pool.h"
21 #include "repo.h"
22 #include "util.h"
23 #include "chksum.h"
24
25 const char *
26 pool_solvable2str(Pool *pool, Solvable *s)
27 {
28   const char *n, *e, *a;
29   char *p;
30   n = pool_id2str(pool, s->name);
31   e = pool_id2str(pool, s->evr);
32   a = pool_id2str(pool, s->arch);
33   p = pool_alloctmpspace(pool, strlen(n) + strlen(e) + strlen(a) + 3);
34   sprintf(p, "%s-%s.%s", n, e, a);
35   return p;
36 }
37
38 Id
39 solvable_lookup_type(Solvable *s, Id keyname)
40 {
41   if (!s->repo)
42     return 0;
43   return repo_lookup_type(s->repo, s - s->repo->pool->solvables, keyname);
44 }
45
46 Id
47 solvable_lookup_id(Solvable *s, Id keyname)
48 {
49   if (!s->repo)
50     return 0;
51   return repo_lookup_id(s->repo, s - s->repo->pool->solvables, keyname);
52 }
53
54 int
55 solvable_lookup_idarray(Solvable *s, Id keyname, Queue *q)
56 {
57   if (!s->repo)
58     {
59       queue_empty(q);
60       return 0;
61     }
62   return repo_lookup_idarray(s->repo, s - s->repo->pool->solvables, keyname, q);
63 }
64
65 int
66 solvable_lookup_deparray(Solvable *s, Id keyname, Queue *q, Id marker)
67 {
68   if (!s->repo)
69     {
70       queue_empty(q);
71       return 0;
72     }
73   return repo_lookup_deparray(s->repo, s - s->repo->pool->solvables, keyname, q, marker);
74 }
75
76 const char *
77 solvable_lookup_str(Solvable *s, Id keyname)
78 {
79   if (!s->repo)
80     return 0;
81   return repo_lookup_str(s->repo, s - s->repo->pool->solvables, keyname);
82 }
83
84 static const char *
85 solvable_lookup_str_base(Solvable *s, Id keyname, Id basekeyname, int usebase)
86 {
87   Pool *pool;
88   const char *str, *basestr;
89   Id p, pp;
90   Solvable *s2;
91   int pass;
92
93   if (!s->repo)
94     return 0;
95   pool = s->repo->pool;
96   str = solvable_lookup_str(s, keyname);
97   if (str || keyname == basekeyname)
98     return str;
99   basestr = solvable_lookup_str(s, basekeyname);
100   if (!basestr)
101     return 0;
102   /* search for a solvable with same name and same base that has the
103    * translation */
104   if (!pool->whatprovides)
105     return usebase ? basestr : 0;
106   /* we do this in two passes, first same vendor, then all other vendors */
107   for (pass = 0; pass < 2; pass++)
108     {
109       FOR_PROVIDES(p, pp, s->name)
110         {
111           s2 = pool->solvables + p;
112           if (s2->name != s->name)
113             continue;
114           if ((s->vendor == s2->vendor) != (pass == 0))
115             continue;
116           str = solvable_lookup_str(s2, basekeyname);
117           if (!str || strcmp(str, basestr))
118             continue;
119           str = solvable_lookup_str(s2, keyname);
120           if (str)
121             return str;
122         }
123     }
124   return usebase ? basestr : 0;
125 }
126
127 const char *
128 solvable_lookup_str_poollang(Solvable *s, Id keyname)
129 {
130   Pool *pool;
131   int i, cols;
132   const char *str;
133   Id *row;
134
135   if (!s->repo)
136     return 0;
137   pool = s->repo->pool;
138   if (!pool->nlanguages)
139     return solvable_lookup_str(s, keyname);
140   cols = pool->nlanguages + 1;
141   if (!pool->languagecache)
142     {
143       pool->languagecache = solv_calloc(cols * ID_NUM_INTERNAL, sizeof(Id));
144       pool->languagecacheother = 0;
145     }
146   if (keyname >= ID_NUM_INTERNAL)
147     {
148       row = pool->languagecache + ID_NUM_INTERNAL * cols;
149       for (i = 0; i < pool->languagecacheother; i++, row += cols)
150         if (*row == keyname)
151           break;
152       if (i >= pool->languagecacheother)
153         {
154           pool->languagecache = solv_realloc2(pool->languagecache, pool->languagecacheother + 1, cols * sizeof(Id));
155           row = pool->languagecache + cols * (ID_NUM_INTERNAL + pool->languagecacheother++);
156           *row = keyname;
157         }
158     }
159   else
160     row = pool->languagecache + keyname * cols;
161   row++;        /* skip keyname */
162   for (i = 0; i < pool->nlanguages; i++, row++)
163     {
164       if (!*row)
165         *row = pool_id2langid(pool, keyname, pool->languages[i], 1);
166       str = solvable_lookup_str_base(s, *row, keyname, 0);
167       if (str)
168         return str;
169     }
170   return solvable_lookup_str(s, keyname);
171 }
172
173 const char *
174 solvable_lookup_str_lang(Solvable *s, Id keyname, const char *lang, int usebase)
175 {
176   if (s->repo)
177     {
178       Id id = pool_id2langid(s->repo->pool, keyname, lang, 0);
179       if (id)
180         return solvable_lookup_str_base(s, id, keyname, usebase);
181       if (!usebase)
182         return 0;
183     }
184   return solvable_lookup_str(s, keyname);
185 }
186
187 unsigned int
188 solvable_lookup_num(Solvable *s, Id keyname, unsigned int notfound)
189 {
190   if (!s->repo)
191     return notfound;
192   return repo_lookup_num(s->repo, s - s->repo->pool->solvables, keyname, notfound);
193 }
194
195 int
196 solvable_lookup_void(Solvable *s, Id keyname)
197 {
198   if (!s->repo)
199     return 0;
200   return repo_lookup_void(s->repo, s - s->repo->pool->solvables, keyname);
201 }
202
203 int
204 solvable_lookup_bool(Solvable *s, Id keyname)
205 {
206   if (!s->repo)
207     return 0;
208   /* historic nonsense: there are two ways of storing a bool, as num == 1 or void. test both. */
209   if (repo_lookup_type(s->repo, s - s->repo->pool->solvables, keyname) == REPOKEY_TYPE_VOID)
210     return 1;
211   return repo_lookup_num(s->repo, s - s->repo->pool->solvables, keyname, 0) == 1;
212 }
213
214 const unsigned char *
215 solvable_lookup_bin_checksum(Solvable *s, Id keyname, Id *typep)
216 {
217   Repo *repo = s->repo;
218
219   if (!repo)
220     {
221       *typep = 0;
222       return 0;
223     }
224   return repo_lookup_bin_checksum(repo, s - repo->pool->solvables, keyname, typep);
225 }
226
227 const char *
228 solvable_lookup_checksum(Solvable *s, Id keyname, Id *typep)
229 {
230   const unsigned char *chk = solvable_lookup_bin_checksum(s, keyname, typep);
231   return chk ? pool_bin2hex(s->repo->pool, chk, solv_chksum_len(*typep)) : 0;
232 }
233
234 static inline const char *
235 evrid2vrstr(Pool *pool, Id evrid)
236 {
237   const char *p, *evr = pool_id2str(pool, evrid);
238   if (!evr)
239     return evr;
240   for (p = evr; *p >= '0' && *p <= '9'; p++)
241     ;
242   return p != evr && *p == ':' ? p + 1 : evr;
243 }
244
245 char *
246 solvable_get_location(Solvable *s, unsigned int *medianrp)
247 {
248   Pool *pool;
249   int l = 0;
250   char *loc;
251   const char *mediadir, *mediafile;
252
253   if (medianrp)
254     *medianrp = 0;
255   if (!s->repo)
256     return 0;
257   pool = s->repo->pool;
258   if (medianrp)
259     *medianrp = solvable_lookup_num(s, SOLVABLE_MEDIANR, 1);
260   if (solvable_lookup_void(s, SOLVABLE_MEDIADIR))
261     mediadir = pool_id2str(pool, s->arch);
262   else
263     mediadir = solvable_lookup_str(s, SOLVABLE_MEDIADIR);
264   if (mediadir)
265     l = strlen(mediadir) + 1;
266   if (solvable_lookup_void(s, SOLVABLE_MEDIAFILE))
267     {
268       const char *name, *evr, *arch;
269       name = pool_id2str(pool, s->name);
270       evr = evrid2vrstr(pool, s->evr);
271       arch = pool_id2str(pool, s->arch);
272       /* name-vr.arch.rpm */
273       loc = pool_alloctmpspace(pool, l + strlen(name) + strlen(evr) + strlen(arch) + 7);
274       if (mediadir)
275         sprintf(loc, "%s/%s-%s.%s.rpm", mediadir, name, evr, arch);
276       else
277         sprintf(loc, "%s-%s.%s.rpm", name, evr, arch);
278     }
279   else
280     {
281       mediafile = solvable_lookup_str(s, SOLVABLE_MEDIAFILE);
282       if (!mediafile)
283         return 0;
284       loc = pool_alloctmpspace(pool, l + strlen(mediafile) + 1);
285       if (mediadir)
286         sprintf(loc, "%s/%s", mediadir, mediafile);
287       else
288         strcpy(loc, mediafile);
289     }
290   return loc;
291 }
292
293 /*****************************************************************************/
294
295 static inline Id dep2name(Pool *pool, Id dep)
296 {
297   while (ISRELDEP(dep))
298     {
299       Reldep *rd = rd = GETRELDEP(pool, dep);
300       dep = rd->name;
301     }
302   return dep;
303 }
304
305 static int providedbyinstalled_multiversion(Pool *pool, Map *installed, Id n, Id con) 
306 {
307   Id p, pp;
308   Solvable *sn = pool->solvables + n; 
309
310   FOR_PROVIDES(p, pp, sn->name)
311     {    
312       Solvable *s = pool->solvables + p; 
313       if (s->name != sn->name || s->arch != sn->arch)
314         continue;
315       if (!MAPTST(installed, p))
316         continue;
317       if (pool_match_nevr(pool, pool->solvables + p, con))
318         continue;
319       return 1;         /* found installed package that doesn't conflict */
320     }    
321   return 0;
322 }
323
324 static inline int providedbyinstalled(Pool *pool, Map *installed, Id dep, int ispatch, Map *noobsoletesmap)
325 {
326   Id p, pp;
327   FOR_PROVIDES(p, pp, dep)
328     {
329       if (p == SYSTEMSOLVABLE)
330         return -1;
331       if (ispatch && !pool_match_nevr(pool, pool->solvables + p, dep))
332         continue;
333       if (ispatch && noobsoletesmap && noobsoletesmap->size && MAPTST(noobsoletesmap, p) && ISRELDEP(dep))
334         if (providedbyinstalled_multiversion(pool, installed, p, dep))
335           continue;
336       if (MAPTST(installed, p))
337         return 1;
338     }
339   return 0;
340 }
341
342 /*
343  * solvable_trivial_installable_map - anwers is a solvable is installable
344  * without any other installs/deinstalls.
345  * The packages considered to be installed are provided via the
346  * installedmap bitmap. A additional "conflictsmap" bitmap providing
347  * information about the conflicts of the installed packages can be
348  * used for extra speed up. Provide a NULL pointer if you do not
349  * have this information.
350  * Both maps can be created with pool_create_state_maps() or
351  * solver_create_state_maps().
352  *
353  * returns:
354  * 1:  solvable is installable without any other package changes
355  * 0:  solvable is not installable
356  * -1: solvable is installable, but doesn't constrain any installed packages
357  */
358 int
359 solvable_trivial_installable_map(Solvable *s, Map *installedmap, Map *conflictsmap, Map *noobsoletesmap)
360 {
361   Pool *pool = s->repo->pool;
362   Solvable *s2;
363   Id p, *dp;
364   Id *reqp, req;
365   Id *conp, con;
366   int r, interesting = 0;
367
368   if (conflictsmap && MAPTST(conflictsmap, s - pool->solvables))
369     return 0;
370   if (s->requires)
371     {
372       reqp = s->repo->idarraydata + s->requires;
373       while ((req = *reqp++) != 0)
374         {
375           if (req == SOLVABLE_PREREQMARKER)
376             continue;
377           r = providedbyinstalled(pool, installedmap, req, 0, 0);
378           if (!r)
379             return 0;
380           if (r > 0)
381             interesting = 1;
382         }
383     }
384   if (s->conflicts)
385     {
386       int ispatch = 0;
387
388       if (!strncmp("patch:", pool_id2str(pool, s->name), 6))
389         ispatch = 1;
390       conp = s->repo->idarraydata + s->conflicts;
391       while ((con = *conp++) != 0)
392         {
393           if (providedbyinstalled(pool, installedmap, con, ispatch, noobsoletesmap))
394             return 0;
395           if (!interesting && ISRELDEP(con))
396             {
397               con = dep2name(pool, con);
398               if (providedbyinstalled(pool, installedmap, con, ispatch, noobsoletesmap))
399                 interesting = 1;
400             }
401         }
402     }
403 #if 0
404   if (s->repo)
405     {
406       Id *obsp, obs;
407       Repo *installed = 0;
408       if (s->obsoletes && s->repo != installed)
409         {
410           obsp = s->repo->idarraydata + s->obsoletes;
411           while ((obs = *obsp++) != 0)
412             {
413               if (providedbyinstalled(pool, installedmap, obs, 0, 0))
414                 return 0;
415             }
416         }
417       if (s->repo != installed)
418         {
419           Id pp;
420           FOR_PROVIDES(p, pp, s->name)
421             {
422               s2 = pool->solvables + p;
423               if (s2->repo == installed && s2->name == s->name)
424                 return 0;
425             }
426         }
427     }
428 #endif
429   if (!conflictsmap)
430     {
431       int i;
432
433       p = s - pool->solvables;
434       for (i = 1; i < pool->nsolvables; i++)
435         {
436           if (!MAPTST(installedmap, i))
437             continue;
438           s2 = pool->solvables + i;
439           if (!s2->conflicts)
440             continue;
441           conp = s2->repo->idarraydata + s2->conflicts;
442           while ((con = *conp++) != 0)
443             {
444               dp = pool_whatprovides_ptr(pool, con);
445               for (; *dp; dp++)
446                 if (*dp == p)
447                   return 0;
448             }
449         }
450      }
451   return interesting ? 1 : -1;
452 }
453
454 /*
455  * different interface for solvable_trivial_installable_map, where
456  * the information about the installed packages is provided
457  * by a queue.
458  */
459 int
460 solvable_trivial_installable_queue(Solvable *s, Queue *installed, Map *noobsoletesmap)
461 {
462   Pool *pool = s->repo->pool;
463   int i;
464   Id p;
465   Map installedmap;
466   int r;
467
468   map_init(&installedmap, pool->nsolvables);
469   for (i = 0; i < installed->count; i++)
470     {
471       p = installed->elements[i];
472       if (p > 0)                /* makes it work with decisionq */
473         MAPSET(&installedmap, p);
474     }
475   r = solvable_trivial_installable_map(s, &installedmap, 0, noobsoletesmap);
476   map_free(&installedmap);
477   return r;
478 }
479
480 /*
481  * different interface for solvable_trivial_installable_map, where
482  * the information about the installed packages is provided
483  * by a repo containing the installed solvables.
484  */
485 int
486 solvable_trivial_installable_repo(Solvable *s, Repo *installed, Map *noobsoletesmap)
487 {
488   Pool *pool = s->repo->pool;
489   Id p;
490   Solvable *s2;
491   Map installedmap;
492   int r;
493
494   map_init(&installedmap, pool->nsolvables);
495   FOR_REPO_SOLVABLES(installed, p, s2)
496     MAPSET(&installedmap, p);
497   r = solvable_trivial_installable_map(s, &installedmap, 0, noobsoletesmap);
498   map_free(&installedmap);
499   return r;
500 }
501
502
503 /*****************************************************************************/
504
505 /*
506  * Create maps containing the state of each solvable. Input is a "installed" queue,
507  * it contains all solvable ids that are considered to be installed.
508  * 
509  * The created maps can be used for solvable_trivial_installable_map(),
510  * pool_calc_duchanges(), pool_calc_installsizechange().
511  *
512  */
513 void
514 pool_create_state_maps(Pool *pool, Queue *installed, Map *installedmap, Map *conflictsmap)
515 {
516   int i;
517   Solvable *s;
518   Id p, *dp;
519   Id *conp, con;
520
521   map_init(installedmap, pool->nsolvables);
522   if (conflictsmap)
523     map_init(conflictsmap, pool->nsolvables);
524   for (i = 0; i < installed->count; i++)
525     {
526       p = installed->elements[i];
527       if (p <= 0)       /* makes it work with decisionq */
528         continue;
529       MAPSET(installedmap, p);
530       if (!conflictsmap)
531         continue;
532       s = pool->solvables + p;
533       if (!s->conflicts)
534         continue;
535       conp = s->repo->idarraydata + s->conflicts;
536       while ((con = *conp++) != 0)
537         {
538           dp = pool_whatprovides_ptr(pool, con);
539           for (; *dp; dp++)
540             MAPSET(conflictsmap, *dp);
541         }
542     }
543 }
544
545 /* Tests if two solvables have identical content. Currently
546  * both solvables need to come from the same pool
547  */
548 int
549 solvable_identical(Solvable *s1, Solvable *s2)
550 {
551   unsigned int bt1, bt2;
552   Id rq1, rq2;
553   Id *reqp;
554
555   if (s1->name != s2->name)
556     return 0;
557   if (s1->arch != s2->arch)
558     return 0;
559   if (s1->evr != s2->evr)
560     return 0;
561   /* map missing vendor to empty string */
562   if ((s1->vendor ? s1->vendor : 1) != (s2->vendor ? s2->vendor : 1))
563     return 0;
564
565   /* looking good, try some fancier stuff */
566   /* might also look up the package checksum here */
567   bt1 = solvable_lookup_num(s1, SOLVABLE_BUILDTIME, 0);
568   bt2 = solvable_lookup_num(s2, SOLVABLE_BUILDTIME, 0);
569   if (bt1 && bt2)
570     {
571       if (bt1 != bt2)
572         return 0;
573     }
574   else
575     {
576       /* look at requires in a last attempt to find recompiled packages */
577       rq1 = rq2 = 0;
578       if (s1->requires)
579         for (reqp = s1->repo->idarraydata + s1->requires; *reqp; reqp++)
580           rq1 ^= *reqp;
581       if (s2->requires)
582         for (reqp = s2->repo->idarraydata + s2->requires; *reqp; reqp++)
583           rq2 ^= *reqp;
584       if (rq1 != rq2)
585          return 0;
586     }
587   return 1;
588 }
589
590 /* return the self provide dependency of a solvable */
591 Id
592 solvable_selfprovidedep(Solvable *s)
593 {
594   Pool *pool;
595   Reldep *rd;
596   Id prov, *provp;
597
598   if (!s->repo)
599     return s->name;
600   pool = s->repo->pool;
601   if (s->provides)
602     {
603       provp = s->repo->idarraydata + s->provides;
604       while ((prov = *provp++) != 0)
605         {
606           if (!ISRELDEP(prov))
607             continue;
608           rd = GETRELDEP(pool, prov);
609           if (rd->name == s->name && rd->evr == s->evr && rd->flags == REL_EQ)
610             return prov;
611         }
612     }
613   return pool_rel2id(pool, s->name, s->evr, REL_EQ, 1);
614 }
615
616 /* setter functions, simply call the repo variants */
617 void
618 solvable_set_id(Solvable *s, Id keyname, Id id)
619 {
620   repo_set_num(s->repo, s - s->repo->pool->solvables, keyname, id);
621 }
622
623 void
624 solvable_set_num(Solvable *s, Id keyname, unsigned int num)
625 {
626   repo_set_num(s->repo, s - s->repo->pool->solvables, keyname, num);
627 }
628
629 void
630 solvable_set_str(Solvable *s, Id keyname, const char *str)
631 {
632   repo_set_str(s->repo, s - s->repo->pool->solvables, keyname, str);
633 }
634
635 void
636 solvable_set_poolstr(Solvable *s, Id keyname, const char *str)
637 {
638   repo_set_poolstr(s->repo, s - s->repo->pool->solvables, keyname, str);
639 }
640
641 void
642 solvable_add_poolstr_array(Solvable *s, Id keyname, const char *str)
643 {
644   repo_add_poolstr_array(s->repo, s - s->repo->pool->solvables, keyname, str);
645 }
646
647 void
648 solvable_add_idarray(Solvable *s, Id keyname, Id id)
649 {
650   repo_add_idarray(s->repo, s - s->repo->pool->solvables, keyname, id);
651 }
652
653 void
654 solvable_add_deparray(Solvable *s, Id keyname, Id dep, Id marker)
655 {
656   repo_add_deparray(s->repo, s - s->repo->pool->solvables, keyname, dep, marker);
657 }
658
659 void
660 solvable_set_idarray(Solvable *s, Id keyname, Queue *q)
661 {
662   repo_set_idarray(s->repo, s - s->repo->pool->solvables, keyname, q);
663 }
664
665 void
666 solvable_set_deparray(Solvable *s, Id keyname, Queue *q, Id marker)
667 {
668   repo_set_deparray(s->repo, s - s->repo->pool->solvables, keyname, q, marker);
669 }
670