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