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