print error stack on failure of 'apt' solver
[ntk/apt.git] / cmdline / apt-internal-solver.cc
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 /* #####################################################################
4
5 cover around the internal solver to be able to run it like an external
6
7 ##################################################################### */
8 /*}}}*/
9 // Include Files /*{{{*/
10 #include <config.h>
11
12 #include <apt-pkg/error.h>
13 #include <apt-pkg/cmndline.h>
14 #include <apt-pkg/init.h>
15 #include <apt-pkg/cachefile.h>
16 #include <apt-pkg/cacheset.h>
17 #include <apt-pkg/strutl.h>
18 #include <apt-pkg/edsp.h>
19 #include <apt-pkg/algorithms.h>
20 #include <apt-pkg/fileutl.h>
21 #include <apt-pkg/pkgsystem.h>
22 #include <apt-pkg/upgrade.h>
23 #include <apt-pkg/configuration.h>
24 #include <apt-pkg/depcache.h>
25 #include <apt-pkg/pkgcache.h>
26 #include <apt-pkg/cacheiterators.h>
27
28 #include <string.h>
29 #include <iostream>
30 #include <list>
31 #include <string>
32 #include <unistd.h>
33 #include <cstdio>
34 #include <stdlib.h>
35
36 #include <apti18n.h>
37 /*}}}*/
38
39 // ShowHelp - Show a help screen /*{{{*/
40 // ---------------------------------------------------------------------
41 /* */
42 static bool ShowHelp(CommandLine &) {
43 ioprintf(std::cout,_("%s %s for %s compiled on %s %s\n"),PACKAGE,PACKAGE_VERSION,
44 COMMON_ARCH,__DATE__,__TIME__);
45
46 std::cout <<
47 _("Usage: apt-internal-solver\n"
48 "\n"
49 "apt-internal-solver is an interface to use the current internal\n"
50 "like an external resolver for the APT family for debugging or alike\n"
51 "\n"
52 "Options:\n"
53 " -h This help text.\n"
54 " -q Loggable output - no progress indicator\n"
55 " -c=? Read this configuration file\n"
56 " -o=? Set an arbitrary configuration option, eg -o dir::cache=/tmp\n");
57 return true;
58 }
59 /*}}}*/
60 APT_NORETURN static void DIE(std::string const &message) { /*{{{*/
61 std::cerr << "ERROR: " << message << std::endl;
62 _error->DumpErrors(std::cerr);
63 exit(EXIT_FAILURE);
64 }
65 /*}}}*/
66 int main(int argc,const char *argv[]) /*{{{*/
67 {
68 CommandLine::Args Args[] = {
69 {'h',"help","help",0},
70 {'v',"version","version",0},
71 {'q',"quiet","quiet",CommandLine::IntLevel},
72 {'q',"silent","quiet",CommandLine::IntLevel},
73 {'c',"config-file",0,CommandLine::ConfigFile},
74 {'o',"option",0,CommandLine::ArbItem},
75 {0,0,0,0}};
76
77 CommandLine CmdL(Args,_config);
78 if (pkgInitConfig(*_config) == false ||
79 CmdL.Parse(argc,argv) == false) {
80 _error->DumpErrors();
81 return 2;
82 }
83
84 // See if the help should be shown
85 if (_config->FindB("help") == true ||
86 _config->FindB("version") == true) {
87 ShowHelp(CmdL);
88 return 1;
89 }
90
91 if (CmdL.FileList[0] != 0 && strcmp(CmdL.FileList[0], "scenario") == 0)
92 {
93 if (pkgInitSystem(*_config,_system) == false) {
94 std::cerr << "System could not be initialized!" << std::endl;
95 return 1;
96 }
97 pkgCacheFile CacheFile;
98 CacheFile.Open(NULL, false);
99 APT::PackageSet pkgset = APT::PackageSet::FromCommandLine(CacheFile, CmdL.FileList + 1);
100 FILE* output = stdout;
101 if (pkgset.empty() == true)
102 EDSP::WriteScenario(CacheFile, output);
103 else
104 EDSP::WriteLimitedScenario(CacheFile, output, pkgset);
105 fclose(output);
106 _error->DumpErrors(std::cerr);
107 return 0;
108 }
109
110 // Deal with stdout not being a tty
111 if (!isatty(STDOUT_FILENO) && _config->FindI("quiet", -1) == -1)
112 _config->Set("quiet","1");
113
114 if (_config->FindI("quiet", 0) < 1)
115 _config->Set("Debug::EDSP::WriteSolution", true);
116
117 _config->Set("APT::Solver", "internal");
118 _config->Set("edsp::scenario", "stdin");
119 int input = STDIN_FILENO;
120 FILE* output = stdout;
121 SetNonBlock(input, false);
122
123 EDSP::WriteProgress(0, "Start up solver…", output);
124
125 if (pkgInitSystem(*_config,_system) == false)
126 DIE("System could not be initialized!");
127
128 EDSP::WriteProgress(1, "Read request…", output);
129
130 if (WaitFd(input, false, 5) == false)
131 DIE("WAIT timed out in the resolver");
132
133 std::list<std::string> install, remove;
134 bool upgrade, distUpgrade, autoRemove;
135 if (EDSP::ReadRequest(input, install, remove, upgrade, distUpgrade, autoRemove) == false)
136 DIE("Parsing the request failed!");
137
138 EDSP::WriteProgress(5, "Read scenario…", output);
139
140 pkgCacheFile CacheFile;
141 if (CacheFile.Open(NULL, false) == false)
142 DIE("Failed to open CacheFile!");
143
144 EDSP::WriteProgress(50, "Apply request on scenario…", output);
145
146 if (EDSP::ApplyRequest(install, remove, CacheFile) == false)
147 DIE("Failed to apply request to depcache!");
148
149 pkgProblemResolver Fix(CacheFile);
150 for (std::list<std::string>::const_iterator i = remove.begin();
151 i != remove.end(); ++i) {
152 pkgCache::PkgIterator P = CacheFile->FindPkg(*i);
153 Fix.Clear(P);
154 Fix.Protect(P);
155 Fix.Remove(P);
156 }
157
158 for (std::list<std::string>::const_iterator i = install.begin();
159 i != install.end(); ++i) {
160 pkgCache::PkgIterator P = CacheFile->FindPkg(*i);
161 Fix.Clear(P);
162 Fix.Protect(P);
163 }
164
165 for (std::list<std::string>::const_iterator i = install.begin();
166 i != install.end(); ++i)
167 CacheFile->MarkInstall(CacheFile->FindPkg(*i), true);
168
169 EDSP::WriteProgress(60, "Call problemresolver on current scenario…", output);
170
171 if (upgrade == true) {
172 if (pkgAllUpgrade(CacheFile) == false) {
173 EDSP::WriteError("ERR_UNSOLVABLE_UPGRADE", "An upgrade error occurred", output);
174 return 0;
175 }
176 } else if (distUpgrade == true) {
177 if (pkgDistUpgrade(CacheFile) == false) {
178 EDSP::WriteError("ERR_UNSOLVABLE_DIST_UPGRADE", "An dist-upgrade error occurred", output);
179 return 0;
180 }
181 } else if (Fix.Resolve() == false) {
182 EDSP::WriteError("ERR_UNSOLVABLE", "An error occurred", output);
183 return 0;
184 }
185
186 EDSP::WriteProgress(95, "Write solution…", output);
187
188 if (EDSP::WriteSolution(CacheFile, output) == false)
189 DIE("Failed to output the solution!");
190
191 EDSP::WriteProgress(100, "Done", output);
192
193 bool const Errors = _error->PendingError();
194 if (_config->FindI("quiet",0) > 0)
195 _error->DumpErrors(std::cerr);
196 else
197 _error->DumpErrors(std::cerr, GlobalError::DEBUG);
198 return Errors == true ? 100 : 0;
199 }
200 /*}}}*/