]>
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> |
e7e10e47 | 30 | #include <apt-private/private-main.h> |
4128c846 | 31 | |
453b82a3 DK |
32 | #include <string.h> |
33 | #include <iostream> | |
d39d7f88 | 34 | #include <sstream> |
453b82a3 DK |
35 | #include <list> |
36 | #include <string> | |
4128c846 DK |
37 | #include <unistd.h> |
38 | #include <cstdio> | |
81b3540d | 39 | #include <stdlib.h> |
a00a9b44 DK |
40 | |
41 | #include <apti18n.h> | |
4128c846 DK |
42 | /*}}}*/ |
43 | ||
6079b276 DK |
44 | bool ShowHelp(CommandLine &, aptDispatchWithHelp const *) /*{{{*/ |
45 | { | |
4128c846 | 46 | std::cout << |
3999d158 | 47 | _("Usage: apt-internal-solver\n" |
4128c846 | 48 | "\n" |
3999d158 | 49 | "apt-internal-solver is an interface to use the current internal\n" |
4128c846 DK |
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" | |
3999d158 | 56 | " -o=? Set an arbitrary configuration option, eg -o dir::cache=/tmp\n"); |
4128c846 DK |
57 | return true; |
58 | } | |
59 | /*}}}*/ | |
81b3540d DK |
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 | /*}}}*/ | |
6079b276 | 66 | std::vector<aptDispatchWithHelp> GetCommands() /*{{{*/ |
011188e3 DK |
67 | { |
68 | return {}; | |
69 | } | |
70 | /*}}}*/ | |
4128c846 DK |
71 | int main(int argc,const char *argv[]) /*{{{*/ |
72 | { | |
e7e10e47 DK |
73 | InitLocale(); |
74 | ||
75 | // we really don't need anything | |
76 | DropPrivileges(); | |
fc1a78d8 | 77 | |
ad7e0941 | 78 | CommandLine CmdL; |
011188e3 | 79 | ParseCommandLine(CmdL, APT_CMD::APT_INTERNAL_SOLVER, &_config, NULL, argc, argv); |
4128c846 | 80 | |
904be352 DK |
81 | if (CmdL.FileList[0] != 0 && strcmp(CmdL.FileList[0], "scenario") == 0) |
82 | { | |
83 | if (pkgInitSystem(*_config,_system) == false) { | |
84 | std::cerr << "System could not be initialized!" << std::endl; | |
85 | return 1; | |
86 | } | |
87 | pkgCacheFile CacheFile; | |
88 | CacheFile.Open(NULL, false); | |
89 | APT::PackageSet pkgset = APT::PackageSet::FromCommandLine(CacheFile, CmdL.FileList + 1); | |
90 | FILE* output = stdout; | |
91 | if (pkgset.empty() == true) | |
92 | EDSP::WriteScenario(CacheFile, output); | |
93 | else | |
94 | EDSP::WriteLimitedScenario(CacheFile, output, pkgset); | |
95 | fclose(output); | |
96 | _error->DumpErrors(std::cerr); | |
97 | return 0; | |
98 | } | |
99 | ||
4128c846 DK |
100 | // Deal with stdout not being a tty |
101 | if (!isatty(STDOUT_FILENO) && _config->FindI("quiet", -1) == -1) | |
102 | _config->Set("quiet","1"); | |
103 | ||
104 | if (_config->FindI("quiet", 0) < 1) | |
105 | _config->Set("Debug::EDSP::WriteSolution", true); | |
106 | ||
c9443c01 | 107 | _config->Set("APT::System", "Debian APT solver interface"); |
98278a81 | 108 | _config->Set("APT::Solver", "internal"); |
7f58427b | 109 | _config->Set("edsp::scenario", "/nonexistent/stdin"); |
4128c846 DK |
110 | int input = STDIN_FILENO; |
111 | FILE* output = stdout; | |
112 | SetNonBlock(input, false); | |
113 | ||
e876223c DK |
114 | EDSP::WriteProgress(0, "Start up solver…", output); |
115 | ||
81b3540d DK |
116 | if (pkgInitSystem(*_config,_system) == false) |
117 | DIE("System could not be initialized!"); | |
4128c846 | 118 | |
e876223c DK |
119 | EDSP::WriteProgress(1, "Read request…", output); |
120 | ||
4128c846 | 121 | if (WaitFd(input, false, 5) == false) |
81b3540d | 122 | DIE("WAIT timed out in the resolver"); |
4128c846 DK |
123 | |
124 | std::list<std::string> install, remove; | |
125 | bool upgrade, distUpgrade, autoRemove; | |
81b3540d DK |
126 | if (EDSP::ReadRequest(input, install, remove, upgrade, distUpgrade, autoRemove) == false) |
127 | DIE("Parsing the request failed!"); | |
4128c846 | 128 | |
e876223c DK |
129 | EDSP::WriteProgress(5, "Read scenario…", output); |
130 | ||
4128c846 | 131 | pkgCacheFile CacheFile; |
81b3540d DK |
132 | if (CacheFile.Open(NULL, false) == false) |
133 | DIE("Failed to open CacheFile!"); | |
4128c846 | 134 | |
e876223c DK |
135 | EDSP::WriteProgress(50, "Apply request on scenario…", output); |
136 | ||
81b3540d DK |
137 | if (EDSP::ApplyRequest(install, remove, CacheFile) == false) |
138 | DIE("Failed to apply request to depcache!"); | |
d9933172 DK |
139 | |
140 | pkgProblemResolver Fix(CacheFile); | |
141 | for (std::list<std::string>::const_iterator i = remove.begin(); | |
142 | i != remove.end(); ++i) { | |
143 | pkgCache::PkgIterator P = CacheFile->FindPkg(*i); | |
144 | Fix.Clear(P); | |
145 | Fix.Protect(P); | |
146 | Fix.Remove(P); | |
147 | } | |
148 | ||
149 | for (std::list<std::string>::const_iterator i = install.begin(); | |
150 | i != install.end(); ++i) { | |
151 | pkgCache::PkgIterator P = CacheFile->FindPkg(*i); | |
152 | Fix.Clear(P); | |
153 | Fix.Protect(P); | |
154 | } | |
155 | ||
4128c846 DK |
156 | for (std::list<std::string>::const_iterator i = install.begin(); |
157 | i != install.end(); ++i) | |
158 | CacheFile->MarkInstall(CacheFile->FindPkg(*i), true); | |
159 | ||
e876223c | 160 | EDSP::WriteProgress(60, "Call problemresolver on current scenario…", output); |
d9933172 | 161 | |
d39d7f88 | 162 | std::string failure; |
80699703 | 163 | if (upgrade == true) { |
67caa2e6 | 164 | if (APT::Upgrade::Upgrade(CacheFile, APT::Upgrade::FORBID_REMOVE_PACKAGES | APT::Upgrade::FORBID_INSTALL_NEW_PACKAGES) == false) |
d39d7f88 | 165 | failure = "ERR_UNSOLVABLE_UPGRADE"; |
80699703 | 166 | } else if (distUpgrade == true) { |
67caa2e6 | 167 | if (APT::Upgrade::Upgrade(CacheFile, APT::Upgrade::ALLOW_EVERYTHING) == false) |
d39d7f88 DK |
168 | failure = "ERR_UNSOLVABLE_DIST_UPGRADE"; |
169 | } else if (Fix.Resolve() == false) | |
170 | failure = "ERR_UNSOLVABLE"; | |
171 | ||
172 | if (failure.empty() == false) { | |
173 | std::ostringstream broken; | |
174 | ShowBroken(broken, CacheFile, false); | |
175 | EDSP::WriteError(failure.c_str(), broken.str(), output); | |
4128c846 DK |
176 | return 0; |
177 | } | |
178 | ||
e876223c DK |
179 | EDSP::WriteProgress(95, "Write solution…", output); |
180 | ||
81b3540d DK |
181 | if (EDSP::WriteSolution(CacheFile, output) == false) |
182 | DIE("Failed to output the solution!"); | |
4128c846 | 183 | |
e876223c DK |
184 | EDSP::WriteProgress(100, "Done", output); |
185 | ||
011188e3 | 186 | return DispatchCommandLine(CmdL, {}); |
4128c846 DK |
187 | } |
188 | /*}}}*/ |