5ee42979ee5f397e3a7b6e558aa76bbca8fc3832
[platform/upstream/libsolv.git] / examples / solv / solv.c
1 /*
2  * Copyright (c) 2009-2015, SUSE LLC.
3  *
4  * This program is licensed under the BSD license, read LICENSE.BSD
5  * for further information
6  */
7
8 /* solv, a little software installer demoing the sat solver library */
9
10 /* things it does:
11  * - understands globs for package names / dependencies
12  * - understands .arch suffix
13  * - installation of commandline packages
14  * - repository data caching
15  * - on demand loading of secondary repository data
16  * - gpg and checksum verification
17  * - file conflicts
18  * - deltarpm support
19  * - fastestmirror implementation
20  *
21  * things available in the library but missing from solv:
22  * - vendor policy loading
23  * - multi version handling
24  */
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <unistd.h>
29 #include <sys/utsname.h>
30
31 #include "pool.h"
32 #include "poolarch.h"
33 #include "evr.h"
34 #include "selection.h"
35 #include "repo.h"
36 #include "solver.h"
37 #include "solverdebug.h"
38 #include "transaction.h"
39 #include "testcase.h"
40 #ifdef SUSE
41 #include "repo_autopattern.h"
42 #endif
43
44 #include "repoinfo.h"
45 #include "repoinfo_cache.h"
46 #include "repoinfo_download.h"
47
48 #if defined(ENABLE_RPMDB)
49 #include "fileprovides.h"
50 #include "fileconflicts.h"
51 #include "deltarpm.h"
52 #endif
53 #if defined(SUSE) || defined(FEDORA) || defined(MAGEIA)
54 #include "patchjobs.h"
55 #endif
56
57 void
58 setarch(Pool *pool)
59 {
60   struct utsname un;
61   if (uname(&un))
62     {
63       perror("uname");
64       exit(1);
65     }
66   pool_setarch(pool, un.machine);
67 }
68
69
70 int
71 yesno(const char *str, int other)
72 {
73   char inbuf[128], *ip;
74
75   for (;;)
76     {
77       printf("%s", str);
78       fflush(stdout);
79       *inbuf = 0;
80       if (!(ip = fgets(inbuf, sizeof(inbuf), stdin)))
81         {
82           printf("Abort.\n");
83           exit(1);
84         }
85       while (*ip == ' ' || *ip == '\t')
86         ip++;
87       if (*ip == 'q')
88         {
89           printf("Abort.\n");
90           exit(1);
91         }
92       if (*ip == 'y' || *ip == 'n' || *ip == other)
93         return *ip == 'n' ? 0 : *ip;
94     }
95 }
96
97 #ifdef SUSE
98 static Id
99 nscallback(Pool *pool, void *data, Id name, Id evr)
100 {
101 #if 0
102   if (name == NAMESPACE_LANGUAGE)
103     {
104       if (!strcmp(pool_id2str(pool, evr), "ja"))
105         return 1;
106       if (!strcmp(pool_id2str(pool, evr), "de"))
107         return 1;
108       if (!strcmp(pool_id2str(pool, evr), "en"))
109         return 1;
110       if (!strcmp(pool_id2str(pool, evr), "en_US"))
111         return 1;
112     }
113 #endif
114   return 0;
115 }
116 #endif
117
118 #ifdef SUSE
119 static void
120 showdiskusagechanges(Transaction *trans)
121 {
122   DUChanges duc[4];
123   int i;
124
125   /* XXX: use mountpoints here */
126   memset(duc, 0, sizeof(duc));
127   duc[0].path = "/";
128   duc[1].path = "/usr/share/man";
129   duc[2].path = "/sbin";
130   duc[3].path = "/etc";
131   transaction_calc_duchanges(trans, duc, 4);
132   for (i = 0; i < 4; i++)
133     printf("duchanges %s: %d K  %d inodes\n", duc[i].path, duc[i].kbytes, duc[i].files);
134 }
135 #endif
136
137 static Id
138 find_repo(const char *name, Pool *pool, struct repoinfo *repoinfos, int nrepoinfos)
139 {
140   const char *rp;
141   int i;
142
143   for (rp = name; *rp; rp++)
144     if (*rp <= '0' || *rp >= '9')
145       break;
146   if (!*rp)
147     {
148       /* repo specified by number */
149       int rnum = atoi(name);
150       for (i = 0; i < nrepoinfos; i++)
151         {
152           struct repoinfo *cinfo = repoinfos + i;
153           if (!cinfo->enabled || !cinfo->repo)
154             continue;
155           if (--rnum == 0)
156             return cinfo->repo->repoid;
157         }
158     }
159   else
160     {
161       /* repo specified by alias */
162       Repo *repo;
163       FOR_REPOS(i, repo)
164         {
165           if (!strcasecmp(name, repo->name))
166             return repo->repoid;
167         }
168     }
169   return 0;
170 }
171
172
173 #define MODE_LIST        0
174 #define MODE_INSTALL     1
175 #define MODE_ERASE       2
176 #define MODE_UPDATE      3
177 #define MODE_DISTUPGRADE 4
178 #define MODE_VERIFY      5
179 #define MODE_PATCH       6
180 #define MODE_INFO        7
181 #define MODE_REPOLIST    8
182 #define MODE_SEARCH      9
183
184 void
185 usage(int r)
186 {
187   fprintf(stderr, "Usage: solv COMMAND <select>\n");
188   fprintf(stderr, "\n");
189   fprintf(stderr, "    dist-upgrade: replace installed packages with\n");
190   fprintf(stderr, "                  versions from the repositories\n");
191   fprintf(stderr, "    erase:        erase installed packages\n");
192   fprintf(stderr, "    info:         display package information\n");
193   fprintf(stderr, "    install:      install packages\n");
194   fprintf(stderr, "    list:         list packages\n");
195   fprintf(stderr, "    repos:        list enabled repositories\n");
196   fprintf(stderr, "    search:       search name/summary/description\n");
197   fprintf(stderr, "    update:       update installed packages\n");
198   fprintf(stderr, "    verify:       check dependencies of installed packages\n");
199 #if defined(SUSE) || defined(FEDORA) || defined(MAGEIA)
200   fprintf(stderr, "    patch:        install newest maintenance updates\n");
201 #endif
202   fprintf(stderr, "\n");
203   exit(r);
204 }
205
206 int
207 main(int argc, char **argv)
208 {
209   Pool *pool;
210   Repo *commandlinerepo = 0;
211   Id *commandlinepkgs = 0;
212   Id p;
213   struct repoinfo *repoinfos, installedrepoinfo;
214   int nrepoinfos = 0;
215   int mainmode = 0, mode = 0;
216   int i, newpkgs;
217   Queue job, checkq;
218   Solver *solv = 0;
219   Transaction *trans;
220   FILE **newpkgsfps;
221   Queue repofilter;
222   Queue kindfilter;
223   Queue archfilter;
224   int archfilter_src = 0;
225   int cleandeps = 0;
226   int forcebest = 0;
227   char *rootdir = 0;
228   char *keyname = 0;
229   int keyname_depstr = 0;
230   int debuglevel = 0;
231   int answer, acnt = 0;
232   char *testcase = 0;
233
234   argc--;
235   argv++;
236   while (argc && !strcmp(argv[0], "-d"))
237     {
238       debuglevel++;
239       argc--;
240       argv++;
241     }
242   if (!argv[0])
243     usage(1);
244   if (!strcmp(argv[0], "install") || !strcmp(argv[0], "in"))
245     {
246       mainmode = MODE_INSTALL;
247       mode = SOLVER_INSTALL;
248     }
249 #if defined(SUSE) || defined(FEDORA) || defined(MAGEIA)
250   else if (!strcmp(argv[0], "patch"))
251     {
252       mainmode = MODE_PATCH;
253       mode = SOLVER_INSTALL;
254     }
255 #endif
256   else if (!strcmp(argv[0], "erase") || !strcmp(argv[0], "rm"))
257     {
258       mainmode = MODE_ERASE;
259       mode = SOLVER_ERASE;
260     }
261   else if (!strcmp(argv[0], "list") || !strcmp(argv[0], "ls"))
262     {
263       mainmode = MODE_LIST;
264       mode = 0;
265     }
266   else if (!strcmp(argv[0], "info"))
267     {
268       mainmode = MODE_INFO;
269       mode = 0;
270     }
271   else if (!strcmp(argv[0], "search") || !strcmp(argv[0], "se"))
272     {
273       mainmode = MODE_SEARCH;
274       mode = 0;
275     }
276   else if (!strcmp(argv[0], "verify"))
277     {
278       mainmode = MODE_VERIFY;
279       mode = SOLVER_VERIFY;
280     }
281   else if (!strcmp(argv[0], "update") || !strcmp(argv[0], "up"))
282     {
283       mainmode = MODE_UPDATE;
284       mode = SOLVER_UPDATE;
285     }
286   else if (!strcmp(argv[0], "dist-upgrade") || !strcmp(argv[0], "dup"))
287     {
288       mainmode = MODE_DISTUPGRADE;
289       mode = SOLVER_DISTUPGRADE;
290     }
291   else if (!strcmp(argv[0], "repos") || !strcmp(argv[0], "repolist") || !strcmp(argv[0], "lr"))
292     {
293       mainmode = MODE_REPOLIST;
294       mode = 0;
295     }
296   else
297     usage(1);
298
299   for (;;)
300     {
301       if (argc > 2 && !strcmp(argv[1], "--root"))
302         {
303           rootdir = argv[2];
304           argc -= 2;
305           argv += 2;
306         }
307       else if (argc > 1 && !strcmp(argv[1], "--clean"))
308         {
309           cleandeps = 1;
310           argc--;
311           argv++;
312         }
313       else if (argc > 1 && !strcmp(argv[1], "--best"))
314         {
315           forcebest = 1;
316           argc--;
317           argv++;
318         }
319       else if (argc > 1 && !strcmp(argv[1], "--depstr"))
320         {
321           keyname_depstr = 1;
322           argc--;
323           argv++;
324         }
325       else if (argc > 2 && !strcmp(argv[1], "--keyname"))
326         {
327           keyname = argv[2];
328           argc -= 2;
329           argv += 2;
330         }
331       else if (argc > 2 && !strcmp(argv[1], "--testcase"))
332         {
333           testcase = argv[2];
334           argc -= 2;
335           argv += 2;
336         }
337       else
338         break;
339     }
340
341   set_userhome();
342   pool = pool_create();
343   pool_set_rootdir(pool, rootdir);
344
345 #if 0
346   {
347     const char *langs[] = {"de_DE", "de", "en"};
348     pool_set_languages(pool, langs, sizeof(langs)/sizeof(*langs));
349   }
350 #endif
351
352   pool_setloadcallback(pool, load_stub, 0);
353 #ifdef SUSE
354   pool->nscallback = nscallback;
355 #endif
356   if (debuglevel)
357     pool_setdebuglevel(pool, debuglevel);
358   setarch(pool);
359   pool_set_flag(pool, POOL_FLAG_ADDFILEPROVIDESFILTERED, 1);
360   repoinfos = read_repoinfos(pool, &nrepoinfos);
361   sort_repoinfos(repoinfos, nrepoinfos);
362
363   if (mainmode == MODE_REPOLIST)
364     {
365       int j = 1;
366       for (i = 0; i < nrepoinfos; i++)
367         {
368           struct repoinfo *cinfo = repoinfos + i;
369           if (!cinfo->enabled)
370             continue;
371           printf("%d: %-20s %s (prio %d)\n", j++, cinfo->alias, cinfo->name, cinfo->priority);
372         }
373       exit(0);
374     }
375   memset(&installedrepoinfo, 0, sizeof(installedrepoinfo));
376   if (!read_installed_repo(&installedrepoinfo, pool))
377     exit(1);
378   read_repos(pool, repoinfos, nrepoinfos);
379
380   /* setup filters */
381   queue_init(&repofilter);
382   queue_init(&kindfilter);
383   queue_init(&archfilter);
384   while (argc > 1)
385     {
386       if (!strcmp(argv[1], "-i"))
387         {
388           queue_push2(&repofilter, SOLVER_SOLVABLE_REPO | SOLVER_SETREPO, pool->installed->repoid);
389           argc--;
390           argv++;
391         }
392       else if (argc > 2 && (!strcmp(argv[1], "-r") || !strcmp(argv[1], "--repo")))
393         {
394           Id repoid = find_repo(argv[2], pool, repoinfos, nrepoinfos);
395           if (!repoid)
396             {
397               fprintf(stderr, "%s: no such repo\n", argv[2]);
398               exit(1);
399             }
400           /* SETVENDOR is actually wrong but useful */
401           queue_push2(&repofilter, SOLVER_SOLVABLE_REPO | SOLVER_SETREPO | SOLVER_SETVENDOR, repoid);
402           argc -= 2;
403           argv += 2;
404         }
405       else if (argc > 2 && !strcmp(argv[1], "--arch"))
406         {
407           if (!strcmp(argv[2], "src") || !strcmp(argv[2], "nosrc"))
408             archfilter_src = 1;
409           queue_push2(&archfilter, SOLVER_SOLVABLE_PROVIDES, pool_rel2id(pool, 0, pool_str2id(pool, argv[2], 1), REL_ARCH, 1));
410           argc -= 2;
411           argv += 2;
412         }
413       else if (argc > 2 && (!strcmp(argv[1], "-t") || !strcmp(argv[1], "--type")))
414         {
415           const char *kind = argv[2];
416           if (!strcmp(kind, "srcpackage"))
417             {
418               /* hey! should use --arch! */
419               queue_push2(&archfilter, SOLVER_SOLVABLE_PROVIDES, pool_rel2id(pool, 0, ARCH_SRC, REL_ARCH, 1));
420               archfilter_src = 1;
421               argc -= 2;
422               argv += 2;
423               continue;
424             }
425           if (!strcmp(kind, "package"))
426             kind = "";
427           if (!strcmp(kind, "all"))
428             queue_push2(&kindfilter, SOLVER_SOLVABLE_ALL, 0);
429           else
430             queue_push2(&kindfilter, SOLVER_SOLVABLE_PROVIDES, pool_rel2id(pool, 0, pool_str2id(pool, kind, 1), REL_KIND, 1));
431           argc -= 2;
432           argv += 2;
433         }
434       else
435         break;
436     }
437
438   if (mainmode == MODE_SEARCH)
439     {
440       Queue sel, q;
441       Dataiterator di;
442       if (argc != 2)
443         usage(1);
444       pool_createwhatprovides(pool);
445       queue_init(&sel);
446       dataiterator_init(&di, pool, 0, 0, 0, argv[1], SEARCH_SUBSTRING|SEARCH_NOCASE);
447       dataiterator_set_keyname(&di, SOLVABLE_NAME);
448       dataiterator_set_search(&di, 0, 0);
449       while (dataiterator_step(&di))
450         queue_push2(&sel, SOLVER_SOLVABLE, di.solvid);
451       dataiterator_set_keyname(&di, SOLVABLE_SUMMARY);
452       dataiterator_set_search(&di, 0, 0);
453       while (dataiterator_step(&di))
454         queue_push2(&sel, SOLVER_SOLVABLE, di.solvid);
455       dataiterator_set_keyname(&di, SOLVABLE_DESCRIPTION);
456       dataiterator_set_search(&di, 0, 0);
457       while (dataiterator_step(&di))
458         queue_push2(&sel, SOLVER_SOLVABLE, di.solvid);
459       dataiterator_free(&di);
460       if (repofilter.count)
461         selection_filter(pool, &sel, &repofilter);
462         
463       queue_init(&q);
464       selection_solvables(pool, &sel, &q);
465       queue_free(&sel);
466       for (i = 0; i < q.count; i++)
467         {
468           Solvable *s = pool_id2solvable(pool, q.elements[i]);
469           printf("  - %s [%s]: %s\n", pool_solvable2str(pool, s), s->repo->name, solvable_lookup_str(s, SOLVABLE_SUMMARY));
470         }
471       queue_free(&q);
472       exit(0);
473     }
474
475   /* process command line packages */
476   if (mainmode == MODE_LIST || mainmode == MODE_INFO || mainmode == MODE_INSTALL)
477     {
478       for (i = 1; i < argc; i++)
479         {
480           if (!is_cmdline_package((const char *)argv[i]))
481             continue;
482           if (access(argv[i], R_OK))
483             {
484               perror(argv[i]);
485               exit(1);
486             }
487           if (!commandlinepkgs)
488             commandlinepkgs = solv_calloc(argc, sizeof(Id));
489           if (!commandlinerepo)
490             commandlinerepo = repo_create(pool, "@commandline");
491           p = add_cmdline_package(commandlinerepo, (const char *)argv[i]);
492           if (!p)
493             {
494               fprintf(stderr, "could not add '%s'\n", argv[i]);
495               exit(1);
496             }
497           commandlinepkgs[i] = p;
498         }
499       if (commandlinerepo)
500         {
501           repo_internalize(commandlinerepo);
502 #ifdef SUSE
503           repo_add_autopattern(commandlinerepo, 0);
504 #endif
505         }
506     }
507
508 #if defined(ENABLE_RPMDB)
509   if (pool->disttype == DISTTYPE_RPM)
510     addfileprovides(pool);
511 #endif
512   pool_createwhatprovides(pool);
513
514   if (keyname)
515     keyname = solv_dupjoin("solvable:", keyname, 0);
516   queue_init(&job);
517   for (i = 1; i < argc; i++)
518     {
519       Queue job2;
520       int flags, rflags;
521
522       if (commandlinepkgs && commandlinepkgs[i])
523         {
524           queue_push2(&job, SOLVER_SOLVABLE, commandlinepkgs[i]);
525           continue;
526         }
527       queue_init(&job2);
528       flags = SELECTION_NAME|SELECTION_PROVIDES|SELECTION_GLOB;
529       flags |= SELECTION_CANON|SELECTION_DOTARCH|SELECTION_REL;
530       if (kindfilter.count)
531         flags |= SELECTION_SKIP_KIND;
532       if (mode == MODE_LIST || archfilter_src)
533         flags |= SELECTION_WITH_SOURCE;
534       if (argv[i][0] == '/')
535         flags |= SELECTION_FILELIST | (mode == MODE_ERASE ? SELECTION_INSTALLED_ONLY : 0);
536       if (!keyname)
537         rflags = selection_make(pool, &job2, argv[i], flags);
538       else
539         {
540           if (keyname_depstr)
541             flags |= SELECTION_MATCH_DEPSTR;
542           rflags = selection_make_matchdeps(pool, &job2, argv[i], flags, pool_str2id(pool, keyname, 1), 0);
543         }
544       if (repofilter.count)
545         selection_filter(pool, &job2, &repofilter);
546       if (archfilter.count)
547         selection_filter(pool, &job2, &archfilter);
548       if (kindfilter.count)
549         selection_filter(pool, &job2, &kindfilter);
550       if (!job2.count)
551         {
552           flags |= SELECTION_NOCASE;
553           if (!keyname)
554             rflags = selection_make(pool, &job2, argv[i], flags);
555           else
556             rflags = selection_make_matchdeps(pool, &job2, argv[i], flags, pool_str2id(pool, keyname, 1), 0);
557           if (repofilter.count)
558             selection_filter(pool, &job2, &repofilter);
559           if (archfilter.count)
560             selection_filter(pool, &job2, &archfilter);
561           if (kindfilter.count)
562             selection_filter(pool, &job2, &kindfilter);
563           if (job2.count)
564             printf("[ignoring case for '%s']\n", argv[i]);
565         }
566       if (!job2.count)
567         {
568           fprintf(stderr, "nothing matches '%s'\n", argv[i]);
569           exit(1);
570         }
571       if (rflags & SELECTION_FILELIST)
572         printf("[using file list match for '%s']\n", argv[i]);
573       if (rflags & SELECTION_PROVIDES)
574         printf("[using capability match for '%s']\n", argv[i]);
575       queue_insertn(&job, job.count, job2.count, job2.elements);
576       queue_free(&job2);
577     }
578   keyname = solv_free(keyname);
579
580   if (!job.count && (mainmode == MODE_UPDATE || mainmode == MODE_DISTUPGRADE || mainmode == MODE_VERIFY || repofilter.count || archfilter.count || kindfilter.count))
581     {
582       queue_push2(&job, SOLVER_SOLVABLE_ALL, 0);
583       if (repofilter.count)
584         selection_filter(pool, &job, &repofilter);
585       if (archfilter.count)
586         selection_filter(pool, &job, &archfilter);
587       if (kindfilter.count)
588         selection_filter(pool, &job, &kindfilter);
589     }
590   queue_free(&repofilter);
591   queue_free(&archfilter);
592   queue_free(&kindfilter);
593
594   if (!job.count && mainmode != MODE_PATCH)
595     {
596       printf("no package matched\n");
597       exit(1);
598     }
599
600   if (mainmode == MODE_LIST || mainmode == MODE_INFO)
601     {
602       /* list mode, no solver needed */
603       Queue q;
604       queue_init(&q);
605       for (i = 0; i < job.count; i += 2)
606         {
607           int j;
608           queue_empty(&q);
609           pool_job2solvables(pool, &q, job.elements[i], job.elements[i + 1]);
610           for (j = 0; j < q.count; j++)
611             {
612               Solvable *s = pool_id2solvable(pool, q.elements[j]);
613               if (mainmode == MODE_INFO)
614                 {
615                   const char *str;
616                   printf("Name:        %s\n", pool_solvable2str(pool, s));
617                   printf("Repo:        %s\n", s->repo->name);
618                   printf("Summary:     %s\n", solvable_lookup_str(s, SOLVABLE_SUMMARY));
619                   str = solvable_lookup_str(s, SOLVABLE_URL);
620                   if (str)
621                     printf("Url:         %s\n", str);
622                   str = solvable_lookup_str(s, SOLVABLE_LICENSE);
623                   if (str)
624                     printf("License:     %s\n", str);
625                   printf("Description:\n%s\n", solvable_lookup_str(s, SOLVABLE_DESCRIPTION));
626                   printf("\n");
627                 }
628               else
629                 {
630 #if 1
631                   const char *sum = solvable_lookup_str_lang(s, SOLVABLE_SUMMARY, "de", 1);
632 #else
633                   const char *sum = solvable_lookup_str_poollang(s, SOLVABLE_SUMMARY);
634 #endif
635                   printf("  - %s [%s]\n", pool_solvable2str(pool, s), s->repo->name);
636                   if (sum)
637                     printf("    %s\n", sum);
638                 }
639             }
640         }
641       queue_free(&q);
642       queue_free(&job);
643       pool_free(pool);
644       free_repoinfos(repoinfos, nrepoinfos);
645       solv_free(commandlinepkgs);
646       exit(0);
647     }
648
649 #if defined(SUSE) || defined(FEDORA) || defined(MAGEIA)
650   if (mainmode == MODE_PATCH)
651     add_patchjobs(pool, &job);
652 #endif
653
654   // add mode
655   for (i = 0; i < job.count; i += 2)
656     {
657       job.elements[i] |= mode;
658       if (mode == SOLVER_UPDATE && pool_isemptyupdatejob(pool, job.elements[i], job.elements[i + 1]))
659         job.elements[i] ^= SOLVER_UPDATE ^ SOLVER_INSTALL;
660       if (cleandeps)
661         job.elements[i] |= SOLVER_CLEANDEPS;
662       if (forcebest)
663         job.elements[i] |= SOLVER_FORCEBEST;
664     }
665
666   // multiversion test
667   // queue_push2(&job, SOLVER_MULTIVERSION|SOLVER_SOLVABLE_NAME, pool_str2id(pool, "kernel-pae", 1));
668   // queue_push2(&job, SOLVER_MULTIVERSION|SOLVER_SOLVABLE_NAME, pool_str2id(pool, "kernel-pae-base", 1));
669   // queue_push2(&job, SOLVER_MULTIVERSION|SOLVER_SOLVABLE_NAME, pool_str2id(pool, "kernel-pae-extra", 1));
670 #if 0
671   queue_push2(&job, SOLVER_INSTALL|SOLVER_SOLVABLE_PROVIDES, pool_rel2id(pool, NAMESPACE_LANGUAGE, 0, REL_NAMESPACE, 1));
672   queue_push2(&job, SOLVER_ERASE|SOLVER_CLEANDEPS|SOLVER_SOLVABLE_PROVIDES, pool_rel2id(pool, NAMESPACE_LANGUAGE, 0, REL_NAMESPACE, 1));
673 #endif
674
675 rerunsolver:
676   solv = solver_create(pool);
677   solver_set_flag(solv, SOLVER_FLAG_SPLITPROVIDES, 1);
678 #if defined(FEDORA) || defined(MAGEIA)
679   solver_set_flag(solv, SOLVER_FLAG_ALLOW_VENDORCHANGE, 1);
680 #endif
681   if (mainmode == MODE_ERASE)
682     solver_set_flag(solv, SOLVER_FLAG_ALLOW_UNINSTALL, 1);      /* don't nag */
683   solver_set_flag(solv, SOLVER_FLAG_BEST_OBEY_POLICY, 1);
684
685   for (;;)
686     {
687       Id problem, solution;
688       int pcnt, scnt;
689
690       pcnt = solver_solve(solv, &job);
691       if (testcase)
692         {
693           printf("Writing solver testcase:\n");
694           if (!testcase_write(solv, testcase, TESTCASE_RESULT_TRANSACTION | TESTCASE_RESULT_PROBLEMS, 0, 0))
695             printf("%s\n", pool_errstr(pool));
696           testcase = 0;
697         }
698       if (!pcnt)
699         break;
700       pcnt = solver_problem_count(solv);
701       printf("Found %d problems:\n", pcnt);
702       for (problem = 1; problem <= pcnt; problem++)
703         {
704           int take = 0;
705           printf("Problem %d/%d:\n", problem, pcnt);
706           solver_printprobleminfo(solv, problem);
707           printf("\n");
708           scnt = solver_solution_count(solv, problem);
709           for (solution = 1; solution <= scnt; solution++)
710             {
711               printf("Solution %d:\n", solution);
712               solver_printsolution(solv, problem, solution);
713               printf("\n");
714             }
715           for (;;)
716             {
717               char inbuf[128], *ip;
718               printf("Please choose a solution: ");
719               fflush(stdout);
720               *inbuf = 0;
721               if (!(ip = fgets(inbuf, sizeof(inbuf), stdin)))
722                 {
723                   printf("Abort.\n");
724                   exit(1);
725                 }
726               while (*ip == ' ' || *ip == '\t')
727                 ip++;
728               if (*ip >= '0' && *ip <= '9')
729                 {
730                   take = atoi(ip);
731                   if (take >= 1 && take <= scnt)
732                     break;
733                 }
734               if (*ip == 's')
735                 {
736                   take = 0;
737                   break;
738                 }
739               if (*ip == 'q')
740                 {
741                   printf("Abort.\n");
742                   exit(1);
743                 }
744             }
745           if (!take)
746             continue;
747           solver_take_solution(solv, problem, take, &job);
748         }
749     }
750
751   trans = solver_create_transaction(solv);
752   if (!trans->steps.count)
753     {
754       printf("Nothing to do.\n");
755       transaction_free(trans);
756       solver_free(solv);
757       queue_free(&job);
758       pool_free(pool);
759       free_repoinfos(repoinfos, nrepoinfos);
760       solv_free(commandlinepkgs);
761       exit(1);
762     }
763
764   /* display transaction to the user and ask for confirmation */
765   printf("\n");
766   printf("Transaction summary:\n\n");
767   transaction_print(trans);
768 #if defined(SUSE)
769   showdiskusagechanges(trans);
770 #endif
771   printf("install size change: %d K\n", transaction_calc_installsizechange(trans));
772   printf("\n");
773
774   acnt = solver_alternatives_count(solv);
775   if (acnt)
776     {
777       if (acnt == 1)
778         printf("Have one alternative:\n");
779       else
780         printf("Have %d alternatives:\n", acnt);
781       for (i = 1; i <= acnt; i++)
782         {
783           Id id, from;
784           int atype = solver_get_alternative(solv, i, &id, &from, 0, 0, 0);
785           printf("  - %s\n", solver_alternative2str(solv, atype, id, from));
786         }
787       printf("\n");
788       answer = yesno("OK to continue (y/n/a)? ", 'a');
789     }
790   else
791     answer = yesno("OK to continue (y/n)? ", 0);
792   if (answer == 'a')
793     {
794       Queue choicesq;
795       Queue answerq;
796       Id id, from, chosen;
797       int j;
798
799       queue_init(&choicesq);
800       queue_init(&answerq);
801       for (i = 1; i <= acnt; i++)
802         {
803           int atype = solver_get_alternative(solv, i, &id, &from, &chosen, &choicesq, 0);
804           printf("\n%s\n", solver_alternative2str(solv, atype, id, from));
805           for (j = 0; j < choicesq.count; j++)
806             {
807               Id p = choicesq.elements[j];
808               if (p < 0)
809                 p = -p;
810               queue_push(&answerq, p);
811               printf("%6d: %s\n", answerq.count, pool_solvid2str(pool, p));
812             }
813         }
814       queue_free(&choicesq);
815       printf("\n");
816       for (;;)
817         {
818           char inbuf[128], *ip;
819           int neg = 0;
820           printf("OK to continue (y/n), or number to change alternative: ");
821           fflush(stdout);
822           *inbuf = 0;
823           if (!(ip = fgets(inbuf, sizeof(inbuf), stdin)))
824             {
825               printf("Abort.\n");
826               exit(1);
827             }
828           while (*ip == ' ' || *ip == '\t')
829             ip++;
830           if (*ip == '-' && ip[1] >= '0' && ip[1] <= '9')
831             {
832               neg = 1;
833               ip++;
834             }
835           if (*ip >= '0' && *ip <= '9')
836             {
837               int take = atoi(ip);
838               if (take > 0 && take <= answerq.count)
839                 {
840                   Id p = answerq.elements[take - 1];
841                   queue_free(&answerq);
842                   queue_push2(&job, (neg ? SOLVER_DISFAVOR : SOLVER_FAVOR) | SOLVER_SOLVABLE_NAME, pool->solvables[p].name);
843                   solver_free(solv);
844                   solv = 0;
845                   goto rerunsolver;
846                   break;
847                 }
848             }
849           if (*ip == 'n' || *ip == 'y')
850             {
851               answer = *ip == 'n' ? 0 : *ip;
852               break;
853             }
854         }
855       queue_free(&answerq);
856     }
857   if (!answer)
858     {
859       printf("Abort.\n");
860       transaction_free(trans);
861       solver_free(solv);
862       queue_free(&job);
863       pool_free(pool);
864       free_repoinfos(repoinfos, nrepoinfos);
865       solv_free(commandlinepkgs);
866       exit(1);
867     }
868
869   /* download all new packages */
870   queue_init(&checkq);
871   newpkgs = transaction_installedresult(trans, &checkq);
872   newpkgsfps = 0;
873   if (newpkgs)
874     {
875       int downloadsize = 0;
876       for (i = 0; i < newpkgs; i++)
877         {
878           Solvable *s;
879
880           p = checkq.elements[i];
881           s = pool_id2solvable(pool, p);
882           downloadsize += solvable_lookup_sizek(s, SOLVABLE_DOWNLOADSIZE, 0);
883         }
884       printf("Downloading %d packages, %d K\n", newpkgs, downloadsize);
885       newpkgsfps = solv_calloc(newpkgs, sizeof(*newpkgsfps));
886       for (i = 0; i < newpkgs; i++)
887         {
888           const char *loc;
889           Solvable *s;
890           struct repoinfo *cinfo;
891
892           p = checkq.elements[i];
893           s = pool_id2solvable(pool, p);
894           if (s->repo == commandlinerepo)
895             {
896               loc = solvable_lookup_location(s, 0);
897               if (!loc)
898                 continue;
899               if (!(newpkgsfps[i] = fopen(loc, "r")))
900                 {
901                   perror(loc);
902                   exit(1);
903                 }
904               putchar('.');
905               continue;
906             }
907           cinfo = s->repo->appdata;
908           if (!cinfo || cinfo->type == TYPE_INSTALLED)
909             {
910               printf("%s: no repository information\n", s->repo->name);
911               exit(1);
912             }
913           loc = solvable_lookup_location(s, 0);
914           if (!loc)
915              continue;  /* pseudo package? */
916 #if defined(ENABLE_RPMDB)
917           if (pool->installed && pool->installed->nsolvables)
918             {
919               if ((newpkgsfps[i] = trydeltadownload(s, loc)) != 0)
920                 {
921                   putchar('d');
922                   fflush(stdout);
923                   continue;             /* delta worked! */
924                 }
925             }
926 #endif
927           if ((newpkgsfps[i] = downloadpackage(s, loc)) == 0)
928             {
929               printf("\n%s: %s not found in repository\n", s->repo->name, loc);
930               exit(1);
931             }
932           putchar('.');
933           fflush(stdout);
934         }
935       putchar('\n');
936     }
937
938 #if defined(ENABLE_RPMDB) && (defined(SUSE) || defined(FEDORA) || defined(MANDRIVA) || defined(MAGEIA))
939   /* check for file conflicts */
940   if (newpkgs)
941     {
942       Queue conflicts;
943       queue_init(&conflicts);
944       if (checkfileconflicts(pool, &checkq, newpkgs, newpkgsfps, &conflicts))
945         {
946           if (yesno("Re-run solver (y/n/q)? ", 0))
947             {
948               for (i = 0; i < newpkgs; i++)
949                 if (newpkgsfps[i])
950                   fclose(newpkgsfps[i]);
951               newpkgsfps = solv_free(newpkgsfps);
952               solver_free(solv);
953               solv = 0;
954               pool_add_fileconflicts_deps(pool, &conflicts);
955               queue_free(&conflicts);
956               goto rerunsolver;
957             }
958         }
959       queue_free(&conflicts);
960     }
961 #endif
962
963   /* and finally commit the transaction */
964   printf("Committing transaction:\n\n");
965   transaction_order(trans, 0);
966   for (i = 0; i < trans->steps.count; i++)
967     {
968       int j;
969       FILE *fp;
970       Id type;
971
972       p = trans->steps.elements[i];
973       type = transaction_type(trans, p, SOLVER_TRANSACTION_RPM_ONLY);
974       switch(type)
975         {
976         case SOLVER_TRANSACTION_ERASE:
977           printf("erase %s\n", pool_solvid2str(pool, p));
978           commit_transactionelement(pool, type, p, 0);
979           break;
980         case SOLVER_TRANSACTION_INSTALL:
981         case SOLVER_TRANSACTION_MULTIINSTALL:
982           printf("install %s\n", pool_solvid2str(pool, p));
983           for (j = 0; j < newpkgs; j++)
984             if (checkq.elements[j] == p)
985               break;
986           fp = j < newpkgs ? newpkgsfps[j] : 0;
987           if (!fp)
988             continue;
989           commit_transactionelement(pool, type, p, fp);
990           fclose(fp);
991           newpkgsfps[j] = 0;
992           break;
993         default:
994           break;
995         }
996     }
997
998   for (i = 0; i < newpkgs; i++)
999     if (newpkgsfps[i])
1000       fclose(newpkgsfps[i]);
1001   solv_free(newpkgsfps);
1002   queue_free(&checkq);
1003   transaction_free(trans);
1004   solver_free(solv);
1005   queue_free(&job);
1006   pool_free(pool);
1007   free_repoinfos(repoinfos, nrepoinfos);
1008   solv_free(commandlinepkgs);
1009   exit(0);
1010 }