73fa61c52fb214f72408c35c872f7b230169605e
[platform/upstream/libsolv.git] / tools / installcheck.c
1 /* vim: sw=2 et cino=>4,n-2,{1s
2  */
3
4 /*
5  * Copyright (c) 2009, Novell Inc.
6  *
7  * This program is licensed under the BSD license, read LICENSE.BSD
8  * for further information
9  */
10
11
12 #define _GNU_SOURCE
13
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <unistd.h>
17 #include <string.h>
18 #include <zlib.h>
19
20 #include "pool.h"
21 #include "poolarch.h"
22 #include "repo_solv.h"
23 #ifdef ENABLE_SUSEREPO
24 #include "repo_susetags.h"
25 #endif
26 #ifdef ENABLE_RPMMD
27 #include "repo_rpmmd.h"
28 #endif
29 #ifdef ENABLE_DEBIAN
30 #include "repo_deb.h"
31 #endif
32 #include "solver.h"
33 #include "solv_xfopen.h"
34
35
36 void
37 usage(char** argv)
38 {
39   printf("Usage:\n%s: <arch> [options..] repo [--nocheck repo]...\n"
40          "\t--exclude <pattern>\twhitespace-separated list of (sub-)"
41          "packagenames to ignore\n"
42          "\t--withobsoletes\t\tCheck for obsoletes on packages contained in repos\n"
43          "\t--nocheck\t\tDo not warn about all following repos (only use them to fulfill dependencies)\n"
44          "\t--withsrc\t\tAlso check dependencies of src.rpm\n\n"
45          , argv[0]);
46   exit(1);
47 }
48
49
50 int
51 main(int argc, char **argv)
52 {
53   Pool *pool;
54   Solver *solv;
55   Repo *repo;
56   Queue job;
57   Queue rids;
58   Queue cand;
59   char *arch, *exclude_pat;
60   int i, j;
61   Id p;
62   Id archid, noarchid;
63   Id rpmrel;
64 #ifndef DEBIAN
65   Id rpmid;
66 #endif
67   int status = 0;
68   int nocheck = 0;
69   int withsrc = 0;
70   int obsoletepkgcheck = 0;
71
72   exclude_pat = 0;
73   if (argc < 3)
74     usage(argv);
75
76   arch = argv[1];
77   pool = pool_create();
78   pool_setarch(pool, arch);
79   noarchid = pool->solvables[SYSTEMSOLVABLE].arch;
80   for (i = 2; i < argc; i++)
81     {
82       FILE *fp;
83       int r, l;
84
85       if (!strcmp(argv[i], "--withsrc"))
86         {
87           withsrc++;
88           continue;
89         }
90       if (!strcmp(argv[i], "--withobsoletes"))
91         {
92           obsoletepkgcheck++;
93           continue;
94         }
95       if (!strcmp(argv[i], "--nocheck"))
96         {
97           if (!nocheck)
98             nocheck = pool->nsolvables;
99           continue;
100         }
101       if (!strcmp(argv[i], "--exclude"))
102         {
103           if (i + 1 >= argc)
104             {
105               printf("--exclude needs a whitespace separated list of substrings as parameter\n");
106               exit(1);
107             }
108           exclude_pat = argv[i + 1];
109           ++i;
110           continue;
111         }
112       l = strlen(argv[i]);
113       if (!strcmp(argv[i], "-"))
114         fp = stdin;
115       else if ((fp = solv_xfopen(argv[i], 0)) == 0)
116         {
117           perror(argv[i]);
118           exit(1);
119         }
120       repo = repo_create(pool, argv[i]);
121       r = 0;
122       if (0)
123         {
124         }
125 #ifdef ENABLE_SUSEREPO
126       else if (l >= 8 && !strcmp(argv[i] + l - 8, "packages"))
127         {
128           r = repo_add_susetags(repo, fp, 0, 0, 0);
129         }
130       else if (l >= 11 && !strcmp(argv[i] + l - 11, "packages.gz"))
131         {
132           r = repo_add_susetags(repo, fp, 0, 0, 0);
133         }
134 #endif
135 #ifdef ENABLE_RPMMD
136       else if (l >= 14 && !strcmp(argv[i] + l - 14, "primary.xml.gz"))
137         {
138           r = repo_add_rpmmd(repo, fp, 0, 0);
139         }
140 #endif
141 #ifdef ENABLE_DEBIAN
142       else if (l >= 8 && !strcmp(argv[i] + l - 8, "Packages"))
143         {
144           r = repo_add_debpackages(repo, fp, 0);
145         }
146       else if (l >= 11 && !strcmp(argv[i] + l - 11, "Packages.gz"))
147         {
148           r = repo_add_debpackages(repo, fp, 0);
149         }
150 #endif
151       else
152         r = repo_add_solv(repo, fp, 0);
153       if (r)
154         {
155           fprintf(stderr, "could not add repo %s: %s\n", argv[i], pool_errstr(pool));
156           exit(1);
157         }
158       if (fp != stdin)
159         fclose(fp);
160     }
161   pool_addfileprovides(pool);
162   pool_createwhatprovides(pool);
163   archid = pool_str2id(pool, arch, 0);
164 #ifndef DEBIAN
165   rpmid = pool_str2id(pool, "rpm", 0);
166   rpmrel = 0;
167   if (rpmid && archid)
168     {
169       for (p = 1; p < pool->nsolvables; p++)
170         {
171           Solvable *s = pool->solvables + p;
172           if (s->name == rpmid && s->arch == archid && pool_installable(pool, s))
173             break;
174         }
175       if (p < pool->nsolvables)
176         rpmrel = pool_rel2id(pool, rpmid, archid, REL_ARCH, 1);
177     }
178 #else
179   rpmrel = 0;
180 #endif
181   
182   queue_init(&job);
183   queue_init(&rids);
184   queue_init(&cand);
185   for (p = 1; p < (nocheck ? nocheck : pool->nsolvables); p++)
186     {
187       Solvable *s = pool->solvables + p;
188       if (!s->repo)
189         continue;
190       if (withsrc && (s->arch == ARCH_SRC || s->arch == ARCH_NOSRC))
191         {
192           queue_push(&cand, p);
193           continue;
194         }
195       if (!pool_installable(pool, s))
196         continue;
197       if (archid && s->arch != archid && s->arch != noarchid)
198         {
199           /* check if we will conflict with a infarch rule, if yes,
200            * don't bother checking the package */
201           Id rp, rpp;
202           FOR_PROVIDES(rp, rpp, s->name)
203             {
204               if (pool->solvables[rp].name != s->name)
205                 continue;
206               if (pool->solvables[rp].arch == archid)
207                 break;
208             }
209           if (rp)
210             continue;
211         }
212       queue_push(&cand, p);
213     }
214   if (obsoletepkgcheck)
215     {
216       int obsoleteusesprovides = pool_get_flag(pool, POOL_FLAG_OBSOLETEUSESPROVIDES);
217       int obsoleteusescolors = pool_get_flag(pool, POOL_FLAG_OBSOLETEUSESCOLORS);
218
219       for (i = 0; i < cand.count; i++)
220         {
221           Solvable *s;
222           s = pool->solvables + cand.elements[i];
223
224           if (s->obsoletes)
225             {
226               Id obs, *obsp = s->repo->idarraydata + s->obsoletes;
227
228               while ((obs = *obsp++) != 0)
229                 {
230                   Id op, opp;
231                   FOR_PROVIDES(op, opp, obs)
232                     {
233                       Solvable *os = pool->solvables + op;
234                       if (nocheck && op >= nocheck)
235                         continue;
236                       if (solvable_identical(s, os))
237                         continue;
238                       if (!obsoleteusesprovides && !pool_match_nevr(pool, os, obs))
239                         continue;
240                       if (obsoleteusescolors && !pool_colormatch(pool, s, os))
241                         continue;
242                       status = 2;
243                       printf("can't install %s:\n", pool_solvid2str(pool, op));
244                       printf("  package is obsoleted by %s\n", pool_solvable2str(pool, s));
245                     }
246                 }
247             }
248         }
249     }
250
251   solv = solver_create(pool);
252
253   /* prune cand by doing weak installs */
254   while (cand.count)
255     {
256       queue_empty(&job);
257       for (i = 0; i < cand.count; i++)
258         {
259           p = cand.elements[i];
260           queue_push2(&job, SOLVER_INSTALL|SOLVER_SOLVABLE|SOLVER_WEAK, p);
261         }
262       if (rpmrel)
263         queue_push2(&job, SOLVER_INSTALL|SOLVER_SOLVABLE_NAME, rpmrel);
264       solver_set_flag(solv, SOLVER_FLAG_IGNORE_RECOMMENDED, 1);
265       solver_solve(solv, &job);
266       /* prune... */
267       for (i = j = 0; i < cand.count; i++)
268         {
269           p = cand.elements[i];
270           if (solver_get_decisionlevel(solv, p) <= 0)
271             {
272               cand.elements[j++] = p;
273               continue;
274             }
275 #if 0
276           Solvable *s = pool->solvables + p;
277           if (!strcmp(pool_id2str(pool, s->name), "libusb-compat-devel"))
278             {
279               cand.elements[j++] = p;
280               continue;
281             }
282 #endif
283         }
284       cand.count = j;
285       if (i == j)
286         break;
287     }
288
289   /* now check every candidate */
290   for (i = 0; i < cand.count; i++)
291     {
292       Solvable *s;
293       int problemcount;
294
295       p = cand.elements[i];
296       if (exclude_pat)
297         {
298           char *ptr, *save = 0, *pattern;
299           int match = 0;
300           pattern = solv_strdup(exclude_pat);
301
302           for (ptr = strtok_r(pattern, " ", &save);
303               ptr;
304               ptr = strtok_r(NULL, " ", &save))
305             {
306               if (*ptr && strstr(pool_solvid2str(pool, p), ptr))
307                 {
308                   match = 1;
309                   break;
310                 }
311             }
312           solv_free(pattern);
313           if (match)
314             continue;
315         }
316       s = pool->solvables + p;
317       queue_empty(&job);
318       queue_push2(&job, SOLVER_INSTALL|SOLVER_SOLVABLE, p);
319       if (rpmrel)
320         queue_push2(&job, SOLVER_INSTALL|SOLVER_SOLVABLE_NAME, rpmrel);
321       solver_set_flag(solv, SOLVER_FLAG_IGNORE_RECOMMENDED, 1);
322       problemcount = solver_solve(solv, &job);
323       if (problemcount)
324         {
325           Id problem = 0;
326           Solvable *s2;
327
328           status = 1;
329           printf("can't install %s:\n", pool_solvable2str(pool, s));
330           while ((problem = solver_next_problem(solv, problem)) != 0)
331             {
332               solver_findallproblemrules(solv, problem, &rids);
333               for (j = 0; j < rids.count; j++)
334                 {
335                   Id probr = rids.elements[j];
336                   int k;
337                   Queue rinfo;
338                   queue_init(&rinfo);
339
340                   solver_allruleinfos(solv, probr, &rinfo);
341                   for (k = 0; k < rinfo.count; k += 4)
342                     {
343                       Id dep, source, target;
344                       source = rinfo.elements[k + 1];
345                       target = rinfo.elements[k + 2];
346                       dep = rinfo.elements[k + 3];
347                       switch (rinfo.elements[k])
348                         {
349                         case SOLVER_RULE_DISTUPGRADE:
350                           break;
351                         case SOLVER_RULE_INFARCH:
352                           s = pool_id2solvable(pool, source);
353                           printf("  %s has inferior architecture\n", pool_solvable2str(pool, s));
354                           break;
355                         case SOLVER_RULE_UPDATE:
356                           s = pool_id2solvable(pool, source);
357                           printf("  %s can not be updated\n", pool_solvable2str(pool, s));
358                           break;
359                         case SOLVER_RULE_JOB:
360                         case SOLVER_RULE_JOB_PROVIDED_BY_SYSTEM:
361                         case SOLVER_RULE_JOB_UNKNOWN_PACKAGE:
362                         case SOLVER_RULE_JOB_UNSUPPORTED:
363                           break;
364                         case SOLVER_RULE_RPM:
365                           printf("  some dependency problem\n");
366                           break;
367                         case SOLVER_RULE_JOB_NOTHING_PROVIDES_DEP:
368                           printf("  nothing provides requested %s\n", pool_dep2str(pool, dep));
369                           break;
370                         case SOLVER_RULE_RPM_NOT_INSTALLABLE:
371                           s = pool_id2solvable(pool, source);
372                           printf("  package %s is not installable\n", pool_solvable2str(pool, s));
373                           break;
374                         case SOLVER_RULE_RPM_NOTHING_PROVIDES_DEP:
375                           s = pool_id2solvable(pool, source);
376                           printf("  nothing provides %s needed by %s\n", pool_dep2str(pool, dep), pool_solvable2str(pool, s));
377                           if (ISRELDEP(dep))
378                             {
379                               Reldep *rd = GETRELDEP(pool, dep);
380                               if (!ISRELDEP(rd->name))
381                                 {
382                                   Id rp, rpp;
383                                   FOR_PROVIDES(rp, rpp, rd->name)
384                                     printf("    (we have %s)\n", pool_solvable2str(pool, pool->solvables + rp));
385                                 }
386                             }
387                           break;
388                         case SOLVER_RULE_RPM_SAME_NAME:
389                           s = pool_id2solvable(pool, source);
390                           s2 = pool_id2solvable(pool, target);
391                           printf("  cannot install both %s and %s\n", pool_solvable2str(pool, s), pool_solvable2str(pool, s2));
392                           break;
393                         case SOLVER_RULE_RPM_PACKAGE_CONFLICT:
394                           s = pool_id2solvable(pool, source);
395                           s2 = pool_id2solvable(pool, target);
396                           printf("  package %s conflicts with %s provided by %s\n", pool_solvable2str(pool, s), pool_dep2str(pool, dep), pool_solvable2str(pool, s2));
397                           break;
398                         case SOLVER_RULE_RPM_PACKAGE_OBSOLETES:
399                           s = pool_id2solvable(pool, source);
400                           s2 = pool_id2solvable(pool, target);
401                           printf("  package %s obsoletes %s provided by %s\n", pool_solvable2str(pool, s), pool_dep2str(pool, dep), pool_solvable2str(pool, s2));
402                           break;
403                         case SOLVER_RULE_RPM_PACKAGE_REQUIRES:
404                           s = pool_id2solvable(pool, source);
405                           printf("  package %s requires %s, but none of the providers can be installed\n", pool_solvable2str(pool, s), pool_dep2str(pool, dep));
406                           break;
407                         case SOLVER_RULE_RPM_SELF_CONFLICT:
408                           s = pool_id2solvable(pool, source);
409                           printf("  package %s conflicts with %s provided by itself\n", pool_solvable2str(pool, s), pool_dep2str(pool, dep));
410                           break;
411                         }
412                     }
413                 }
414             }
415         }
416 #if 0
417       else
418         {
419           if (!strcmp(pool_id2str(pool, s->name), "libusb-compat-devel"))
420             {
421               solver_printdecisions(solv);
422             }
423         }
424 #endif
425     }
426   solver_free(solv);
427   exit(status);
428 }