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