- strip distepoch in pool_solvable2str()
[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 int
204 solvable_lookup_num(Solvable *s, Id keyname, unsigned int 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 int
212 solvable_lookup_void(Solvable *s, Id keyname)
213 {
214   if (!s->repo)
215     return 0;
216   return repo_lookup_void(s->repo, s - s->repo->pool->solvables, keyname);
217 }
218
219 int
220 solvable_lookup_bool(Solvable *s, Id keyname)
221 {
222   if (!s->repo)
223     return 0;
224   /* historic nonsense: there are two ways of storing a bool, as num == 1 or void. test both. */
225   if (repo_lookup_type(s->repo, s - s->repo->pool->solvables, keyname) == REPOKEY_TYPE_VOID)
226     return 1;
227   return repo_lookup_num(s->repo, s - s->repo->pool->solvables, keyname, 0) == 1;
228 }
229
230 const unsigned char *
231 solvable_lookup_bin_checksum(Solvable *s, Id keyname, Id *typep)
232 {
233   Repo *repo = s->repo;
234
235   if (!repo)
236     {
237       *typep = 0;
238       return 0;
239     }
240   return repo_lookup_bin_checksum(repo, s - repo->pool->solvables, keyname, typep);
241 }
242
243 const char *
244 solvable_lookup_checksum(Solvable *s, Id keyname, Id *typep)
245 {
246   const unsigned char *chk = solvable_lookup_bin_checksum(s, keyname, typep);
247   return chk ? pool_bin2hex(s->repo->pool, chk, solv_chksum_len(*typep)) : 0;
248 }
249
250 static inline const char *
251 evrid2vrstr(Pool *pool, Id evrid)
252 {
253   const char *p, *evr = pool_id2str(pool, evrid);
254   if (!evr)
255     return evr;
256   for (p = evr; *p >= '0' && *p <= '9'; p++)
257     ;
258   return p != evr && *p == ':' ? p + 1 : evr;
259 }
260
261 char *
262 solvable_get_location(Solvable *s, unsigned int *medianrp)
263 {
264   Pool *pool;
265   int l = 0;
266   char *loc;
267   const char *mediadir, *mediafile;
268
269   if (medianrp)
270     *medianrp = 0;
271   if (!s->repo)
272     return 0;
273   pool = s->repo->pool;
274   if (medianrp)
275     *medianrp = solvable_lookup_num(s, SOLVABLE_MEDIANR, 1);
276   if (solvable_lookup_void(s, SOLVABLE_MEDIADIR))
277     mediadir = pool_id2str(pool, s->arch);
278   else
279     mediadir = solvable_lookup_str(s, SOLVABLE_MEDIADIR);
280   if (mediadir)
281     l = strlen(mediadir) + 1;
282   if (solvable_lookup_void(s, SOLVABLE_MEDIAFILE))
283     {
284       const char *name, *evr, *arch;
285       name = pool_id2str(pool, s->name);
286       evr = evrid2vrstr(pool, s->evr);
287       arch = pool_id2str(pool, s->arch);
288       /* name-vr.arch.rpm */
289       loc = pool_alloctmpspace(pool, l + strlen(name) + strlen(evr) + strlen(arch) + 7);
290       if (mediadir)
291         sprintf(loc, "%s/%s-%s.%s.rpm", mediadir, name, evr, arch);
292       else
293         sprintf(loc, "%s-%s.%s.rpm", name, evr, arch);
294     }
295   else
296     {
297       mediafile = solvable_lookup_str(s, SOLVABLE_MEDIAFILE);
298       if (!mediafile)
299         return 0;
300       loc = pool_alloctmpspace(pool, l + strlen(mediafile) + 1);
301       if (mediadir)
302         sprintf(loc, "%s/%s", mediadir, mediafile);
303       else
304         strcpy(loc, mediafile);
305     }
306   return loc;
307 }
308
309 /*****************************************************************************/
310
311 static inline Id dep2name(Pool *pool, Id dep)
312 {
313   while (ISRELDEP(dep))
314     {
315       Reldep *rd = rd = GETRELDEP(pool, dep);
316       dep = rd->name;
317     }
318   return dep;
319 }
320
321 static int providedbyinstalled_multiversion(Pool *pool, Map *installed, Id n, Id con) 
322 {
323   Id p, pp;
324   Solvable *sn = pool->solvables + n; 
325
326   FOR_PROVIDES(p, pp, sn->name)
327     {    
328       Solvable *s = pool->solvables + p; 
329       if (s->name != sn->name || s->arch != sn->arch)
330         continue;
331       if (!MAPTST(installed, p))
332         continue;
333       if (pool_match_nevr(pool, pool->solvables + p, con))
334         continue;
335       return 1;         /* found installed package that doesn't conflict */
336     }    
337   return 0;
338 }
339
340 static inline int providedbyinstalled(Pool *pool, Map *installed, Id dep, int ispatch, Map *noobsoletesmap)
341 {
342   Id p, pp;
343   FOR_PROVIDES(p, pp, dep)
344     {
345       if (p == SYSTEMSOLVABLE)
346         return -1;
347       if (ispatch && !pool_match_nevr(pool, pool->solvables + p, dep))
348         continue;
349       if (ispatch && noobsoletesmap && noobsoletesmap->size && MAPTST(noobsoletesmap, p) && ISRELDEP(dep))
350         if (providedbyinstalled_multiversion(pool, installed, p, dep))
351           continue;
352       if (MAPTST(installed, p))
353         return 1;
354     }
355   return 0;
356 }
357
358 /*
359  * solvable_trivial_installable_map - anwers is a solvable is installable
360  * without any other installs/deinstalls.
361  * The packages considered to be installed are provided via the
362  * installedmap bitmap. A additional "conflictsmap" bitmap providing
363  * information about the conflicts of the installed packages can be
364  * used for extra speed up. Provide a NULL pointer if you do not
365  * have this information.
366  * Both maps can be created with pool_create_state_maps() or
367  * solver_create_state_maps().
368  *
369  * returns:
370  * 1:  solvable is installable without any other package changes
371  * 0:  solvable is not installable
372  * -1: solvable is installable, but doesn't constrain any installed packages
373  */
374 int
375 solvable_trivial_installable_map(Solvable *s, Map *installedmap, Map *conflictsmap, Map *noobsoletesmap)
376 {
377   Pool *pool = s->repo->pool;
378   Solvable *s2;
379   Id p, *dp;
380   Id *reqp, req;
381   Id *conp, con;
382   int r, interesting = 0;
383
384   if (conflictsmap && MAPTST(conflictsmap, s - pool->solvables))
385     return 0;
386   if (s->requires)
387     {
388       reqp = s->repo->idarraydata + s->requires;
389       while ((req = *reqp++) != 0)
390         {
391           if (req == SOLVABLE_PREREQMARKER)
392             continue;
393           r = providedbyinstalled(pool, installedmap, req, 0, 0);
394           if (!r)
395             return 0;
396           if (r > 0)
397             interesting = 1;
398         }
399     }
400   if (s->conflicts)
401     {
402       int ispatch = 0;
403
404       if (!strncmp("patch:", pool_id2str(pool, s->name), 6))
405         ispatch = 1;
406       conp = s->repo->idarraydata + s->conflicts;
407       while ((con = *conp++) != 0)
408         {
409           if (providedbyinstalled(pool, installedmap, con, ispatch, noobsoletesmap))
410             return 0;
411           if (!interesting && ISRELDEP(con))
412             {
413               con = dep2name(pool, con);
414               if (providedbyinstalled(pool, installedmap, con, ispatch, noobsoletesmap))
415                 interesting = 1;
416             }
417         }
418     }
419 #if 0
420   if (s->repo)
421     {
422       Id *obsp, obs;
423       Repo *installed = 0;
424       if (s->obsoletes && s->repo != installed)
425         {
426           obsp = s->repo->idarraydata + s->obsoletes;
427           while ((obs = *obsp++) != 0)
428             {
429               if (providedbyinstalled(pool, installedmap, obs, 0, 0))
430                 return 0;
431             }
432         }
433       if (s->repo != installed)
434         {
435           Id pp;
436           FOR_PROVIDES(p, pp, s->name)
437             {
438               s2 = pool->solvables + p;
439               if (s2->repo == installed && s2->name == s->name)
440                 return 0;
441             }
442         }
443     }
444 #endif
445   if (!conflictsmap)
446     {
447       int i;
448
449       p = s - pool->solvables;
450       for (i = 1; i < pool->nsolvables; i++)
451         {
452           if (!MAPTST(installedmap, i))
453             continue;
454           s2 = pool->solvables + i;
455           if (!s2->conflicts)
456             continue;
457           conp = s2->repo->idarraydata + s2->conflicts;
458           while ((con = *conp++) != 0)
459             {
460               dp = pool_whatprovides_ptr(pool, con);
461               for (; *dp; dp++)
462                 if (*dp == p)
463                   return 0;
464             }
465         }
466      }
467   return interesting ? 1 : -1;
468 }
469
470 /*
471  * different interface for solvable_trivial_installable_map, where
472  * the information about the installed packages is provided
473  * by a queue.
474  */
475 int
476 solvable_trivial_installable_queue(Solvable *s, Queue *installed, Map *noobsoletesmap)
477 {
478   Pool *pool = s->repo->pool;
479   int i;
480   Id p;
481   Map installedmap;
482   int r;
483
484   map_init(&installedmap, pool->nsolvables);
485   for (i = 0; i < installed->count; i++)
486     {
487       p = installed->elements[i];
488       if (p > 0)                /* makes it work with decisionq */
489         MAPSET(&installedmap, p);
490     }
491   r = solvable_trivial_installable_map(s, &installedmap, 0, noobsoletesmap);
492   map_free(&installedmap);
493   return r;
494 }
495
496 /*
497  * different interface for solvable_trivial_installable_map, where
498  * the information about the installed packages is provided
499  * by a repo containing the installed solvables.
500  */
501 int
502 solvable_trivial_installable_repo(Solvable *s, Repo *installed, Map *noobsoletesmap)
503 {
504   Pool *pool = s->repo->pool;
505   Id p;
506   Solvable *s2;
507   Map installedmap;
508   int r;
509
510   map_init(&installedmap, pool->nsolvables);
511   FOR_REPO_SOLVABLES(installed, p, s2)
512     MAPSET(&installedmap, p);
513   r = solvable_trivial_installable_map(s, &installedmap, 0, noobsoletesmap);
514   map_free(&installedmap);
515   return r;
516 }
517
518
519 /*****************************************************************************/
520
521 /*
522  * Create maps containing the state of each solvable. Input is a "installed" queue,
523  * it contains all solvable ids that are considered to be installed.
524  * 
525  * The created maps can be used for solvable_trivial_installable_map(),
526  * pool_calc_duchanges(), pool_calc_installsizechange().
527  *
528  */
529 void
530 pool_create_state_maps(Pool *pool, Queue *installed, Map *installedmap, Map *conflictsmap)
531 {
532   int i;
533   Solvable *s;
534   Id p, *dp;
535   Id *conp, con;
536
537   map_init(installedmap, pool->nsolvables);
538   if (conflictsmap)
539     map_init(conflictsmap, pool->nsolvables);
540   for (i = 0; i < installed->count; i++)
541     {
542       p = installed->elements[i];
543       if (p <= 0)       /* makes it work with decisionq */
544         continue;
545       MAPSET(installedmap, p);
546       if (!conflictsmap)
547         continue;
548       s = pool->solvables + p;
549       if (!s->conflicts)
550         continue;
551       conp = s->repo->idarraydata + s->conflicts;
552       while ((con = *conp++) != 0)
553         {
554           dp = pool_whatprovides_ptr(pool, con);
555           for (; *dp; dp++)
556             MAPSET(conflictsmap, *dp);
557         }
558     }
559 }
560
561 /* Tests if two solvables have identical content. Currently
562  * both solvables need to come from the same pool
563  */
564 int
565 solvable_identical(Solvable *s1, Solvable *s2)
566 {
567   unsigned int bt1, bt2;
568   Id rq1, rq2;
569   Id *reqp;
570
571   if (s1->name != s2->name)
572     return 0;
573   if (s1->arch != s2->arch)
574     return 0;
575   if (s1->evr != s2->evr)
576     return 0;
577   /* map missing vendor to empty string */
578   if ((s1->vendor ? s1->vendor : 1) != (s2->vendor ? s2->vendor : 1))
579     return 0;
580
581   /* looking good, try some fancier stuff */
582   /* might also look up the package checksum here */
583   bt1 = solvable_lookup_num(s1, SOLVABLE_BUILDTIME, 0);
584   bt2 = solvable_lookup_num(s2, SOLVABLE_BUILDTIME, 0);
585   if (bt1 && bt2)
586     {
587       if (bt1 != bt2)
588         return 0;
589     }
590   else
591     {
592       /* look at requires in a last attempt to find recompiled packages */
593       rq1 = rq2 = 0;
594       if (s1->requires)
595         for (reqp = s1->repo->idarraydata + s1->requires; *reqp; reqp++)
596           rq1 ^= *reqp;
597       if (s2->requires)
598         for (reqp = s2->repo->idarraydata + s2->requires; *reqp; reqp++)
599           rq2 ^= *reqp;
600       if (rq1 != rq2)
601          return 0;
602     }
603   return 1;
604 }
605
606 /* return the self provide dependency of a solvable */
607 Id
608 solvable_selfprovidedep(Solvable *s)
609 {
610   Pool *pool;
611   Reldep *rd;
612   Id prov, *provp;
613
614   if (!s->repo)
615     return s->name;
616   pool = s->repo->pool;
617   if (s->provides)
618     {
619       provp = s->repo->idarraydata + s->provides;
620       while ((prov = *provp++) != 0)
621         {
622           if (!ISRELDEP(prov))
623             continue;
624           rd = GETRELDEP(pool, prov);
625           if (rd->name == s->name && rd->evr == s->evr && rd->flags == REL_EQ)
626             return prov;
627         }
628     }
629   return pool_rel2id(pool, s->name, s->evr, REL_EQ, 1);
630 }
631
632 /* setter functions, simply call the repo variants */
633 void
634 solvable_set_id(Solvable *s, Id keyname, Id id)
635 {
636   repo_set_num(s->repo, s - s->repo->pool->solvables, keyname, id);
637 }
638
639 void
640 solvable_set_num(Solvable *s, Id keyname, unsigned int num)
641 {
642   repo_set_num(s->repo, s - s->repo->pool->solvables, keyname, num);
643 }
644
645 void
646 solvable_set_str(Solvable *s, Id keyname, const char *str)
647 {
648   repo_set_str(s->repo, s - s->repo->pool->solvables, keyname, str);
649 }
650
651 void
652 solvable_set_poolstr(Solvable *s, Id keyname, const char *str)
653 {
654   repo_set_poolstr(s->repo, s - s->repo->pool->solvables, keyname, str);
655 }
656
657 void
658 solvable_add_poolstr_array(Solvable *s, Id keyname, const char *str)
659 {
660   repo_add_poolstr_array(s->repo, s - s->repo->pool->solvables, keyname, str);
661 }
662
663 void
664 solvable_add_idarray(Solvable *s, Id keyname, Id id)
665 {
666   repo_add_idarray(s->repo, s - s->repo->pool->solvables, keyname, id);
667 }
668
669 void
670 solvable_add_deparray(Solvable *s, Id keyname, Id dep, Id marker)
671 {
672   repo_add_deparray(s->repo, s - s->repo->pool->solvables, keyname, dep, marker);
673 }
674
675 void
676 solvable_set_idarray(Solvable *s, Id keyname, Queue *q)
677 {
678   repo_set_idarray(s->repo, s - s->repo->pool->solvables, keyname, q);
679 }
680
681 void
682 solvable_set_deparray(Solvable *s, Id keyname, Queue *q, Id marker)
683 {
684   repo_set_deparray(s->repo, s - s->repo->pool->solvables, keyname, q, marker);
685 }
686