]>
Commit | Line | Data |
---|---|---|
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 | ||
28 | #include <apt-private/private-output.h> | |
29 | #include <apt-private/private-cmndline.h> | |
30 | #include <apt-private/private-main.h> | |
31 | ||
32 | #include <string.h> | |
33 | #include <iostream> | |
34 | #include <sstream> | |
35 | #include <list> | |
36 | #include <string> | |
37 | #include <unistd.h> | |
38 | #include <cstdio> | |
39 | #include <stdlib.h> | |
40 | ||
41 | #include <apti18n.h> | |
42 | /*}}}*/ | |
43 | ||
44 | bool ShowHelp(CommandLine &) /*{{{*/ | |
45 | { | |
46 | std::cout << | |
47 | _("Usage: apt-internal-solver\n" | |
48 | "\n" | |
49 | "apt-internal-solver is an interface to use the current internal\n" | |
50 | "like an external resolver for the APT family for debugging or alike.\n"); | |
51 | return true; | |
52 | } | |
53 | /*}}}*/ | |
54 | APT_NORETURN static void DIE(std::string const &message) { /*{{{*/ | |
55 | std::cerr << "ERROR: " << message << std::endl; | |
56 | _error->DumpErrors(std::cerr); | |
57 | exit(EXIT_FAILURE); | |
58 | } | |
59 | /*}}}*/ | |
60 | std::vector<aptDispatchWithHelp> GetCommands() /*{{{*/ | |
61 | { | |
62 | return {}; | |
63 | } | |
64 | /*}}}*/ | |
65 | int main(int argc,const char *argv[]) /*{{{*/ | |
66 | { | |
67 | InitLocale(); | |
68 | ||
69 | // we really don't need anything | |
70 | DropPrivileges(); | |
71 | ||
72 | CommandLine CmdL; | |
73 | ParseCommandLine(CmdL, APT_CMD::APT_INTERNAL_SOLVER, &_config, NULL, argc, argv); | |
74 | ||
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 | ||
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 | ||
101 | _config->Set("APT::System", "Debian APT solver interface"); | |
102 | _config->Set("APT::Solver", "internal"); | |
103 | _config->Set("edsp::scenario", "/nonexistent/stdin"); | |
104 | int input = STDIN_FILENO; | |
105 | FILE* output = stdout; | |
106 | SetNonBlock(input, false); | |
107 | ||
108 | EDSP::WriteProgress(0, "Start up solver…", output); | |
109 | ||
110 | if (pkgInitSystem(*_config,_system) == false) | |
111 | DIE("System could not be initialized!"); | |
112 | ||
113 | EDSP::WriteProgress(1, "Read request…", output); | |
114 | ||
115 | if (WaitFd(input, false, 5) == false) | |
116 | DIE("WAIT timed out in the resolver"); | |
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 | DIE("Parsing the request failed!"); | |
122 | ||
123 | EDSP::WriteProgress(5, "Read scenario…", output); | |
124 | ||
125 | pkgCacheFile CacheFile; | |
126 | if (CacheFile.Open(NULL, false) == false) | |
127 | DIE("Failed to open CacheFile!"); | |
128 | ||
129 | EDSP::WriteProgress(50, "Apply request on scenario…", output); | |
130 | ||
131 | if (EDSP::ApplyRequest(install, remove, CacheFile) == false) | |
132 | DIE("Failed to apply request to depcache!"); | |
133 | ||
134 | pkgProblemResolver Fix(CacheFile); | |
135 | for (std::list<std::string>::const_iterator i = remove.begin(); | |
136 | i != remove.end(); ++i) { | |
137 | pkgCache::PkgIterator P = CacheFile->FindPkg(*i); | |
138 | Fix.Clear(P); | |
139 | Fix.Protect(P); | |
140 | Fix.Remove(P); | |
141 | } | |
142 | ||
143 | for (std::list<std::string>::const_iterator i = install.begin(); | |
144 | i != install.end(); ++i) { | |
145 | pkgCache::PkgIterator P = CacheFile->FindPkg(*i); | |
146 | Fix.Clear(P); | |
147 | Fix.Protect(P); | |
148 | } | |
149 | ||
150 | for (std::list<std::string>::const_iterator i = install.begin(); | |
151 | i != install.end(); ++i) | |
152 | CacheFile->MarkInstall(CacheFile->FindPkg(*i), true); | |
153 | ||
154 | EDSP::WriteProgress(60, "Call problemresolver on current scenario…", output); | |
155 | ||
156 | std::string failure; | |
157 | if (upgrade == true) { | |
158 | if (APT::Upgrade::Upgrade(CacheFile, APT::Upgrade::FORBID_REMOVE_PACKAGES | APT::Upgrade::FORBID_INSTALL_NEW_PACKAGES) == false) | |
159 | failure = "ERR_UNSOLVABLE_UPGRADE"; | |
160 | } else if (distUpgrade == true) { | |
161 | if (APT::Upgrade::Upgrade(CacheFile, APT::Upgrade::ALLOW_EVERYTHING) == false) | |
162 | failure = "ERR_UNSOLVABLE_DIST_UPGRADE"; | |
163 | } else if (Fix.Resolve() == false) | |
164 | failure = "ERR_UNSOLVABLE"; | |
165 | ||
166 | if (failure.empty() == false) { | |
167 | std::ostringstream broken; | |
168 | ShowBroken(broken, CacheFile, false); | |
169 | EDSP::WriteError(failure.c_str(), broken.str(), output); | |
170 | return 0; | |
171 | } | |
172 | ||
173 | EDSP::WriteProgress(95, "Write solution…", output); | |
174 | ||
175 | if (EDSP::WriteSolution(CacheFile, output) == false) | |
176 | DIE("Failed to output the solution!"); | |
177 | ||
178 | EDSP::WriteProgress(100, "Done", output); | |
179 | ||
180 | return DispatchCommandLine(CmdL, {}); | |
181 | } | |
182 | /*}}}*/ |