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