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