]>
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> | |
4128c846 | 27 | |
453b82a3 DK |
28 | #include <string.h> |
29 | #include <iostream> | |
30 | #include <list> | |
31 | #include <string> | |
4128c846 DK |
32 | #include <unistd.h> |
33 | #include <cstdio> | |
81b3540d | 34 | #include <stdlib.h> |
a00a9b44 DK |
35 | |
36 | #include <apti18n.h> | |
4128c846 DK |
37 | /*}}}*/ |
38 | ||
39 | // ShowHelp - Show a help screen /*{{{*/ | |
40 | // --------------------------------------------------------------------- | |
41 | /* */ | |
65512241 | 42 | static bool ShowHelp(CommandLine &) { |
9179f697 | 43 | ioprintf(std::cout,_("%s %s for %s compiled on %s %s\n"),PACKAGE,PACKAGE_VERSION, |
4128c846 DK |
44 | COMMON_ARCH,__DATE__,__TIME__); |
45 | ||
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 | /*}}}*/ | |
4128c846 DK |
66 | int main(int argc,const char *argv[]) /*{{{*/ |
67 | { | |
68 | CommandLine::Args Args[] = { | |
69 | {'h',"help","help",0}, | |
70 | {'v',"version","version",0}, | |
71 | {'q',"quiet","quiet",CommandLine::IntLevel}, | |
72 | {'q',"silent","quiet",CommandLine::IntLevel}, | |
904be352 DK |
73 | {'c',"config-file",0,CommandLine::ConfigFile}, |
74 | {'o',"option",0,CommandLine::ArbItem}, | |
4128c846 DK |
75 | {0,0,0,0}}; |
76 | ||
77 | CommandLine CmdL(Args,_config); | |
78 | if (pkgInitConfig(*_config) == false || | |
79 | CmdL.Parse(argc,argv) == false) { | |
80 | _error->DumpErrors(); | |
81 | return 2; | |
82 | } | |
83 | ||
84 | // See if the help should be shown | |
85 | if (_config->FindB("help") == true || | |
86 | _config->FindB("version") == true) { | |
87 | ShowHelp(CmdL); | |
88 | return 1; | |
89 | } | |
90 | ||
904be352 DK |
91 | if (CmdL.FileList[0] != 0 && strcmp(CmdL.FileList[0], "scenario") == 0) |
92 | { | |
93 | if (pkgInitSystem(*_config,_system) == false) { | |
94 | std::cerr << "System could not be initialized!" << std::endl; | |
95 | return 1; | |
96 | } | |
97 | pkgCacheFile CacheFile; | |
98 | CacheFile.Open(NULL, false); | |
99 | APT::PackageSet pkgset = APT::PackageSet::FromCommandLine(CacheFile, CmdL.FileList + 1); | |
100 | FILE* output = stdout; | |
101 | if (pkgset.empty() == true) | |
102 | EDSP::WriteScenario(CacheFile, output); | |
103 | else | |
104 | EDSP::WriteLimitedScenario(CacheFile, output, pkgset); | |
105 | fclose(output); | |
106 | _error->DumpErrors(std::cerr); | |
107 | return 0; | |
108 | } | |
109 | ||
4128c846 DK |
110 | // Deal with stdout not being a tty |
111 | if (!isatty(STDOUT_FILENO) && _config->FindI("quiet", -1) == -1) | |
112 | _config->Set("quiet","1"); | |
113 | ||
114 | if (_config->FindI("quiet", 0) < 1) | |
115 | _config->Set("Debug::EDSP::WriteSolution", true); | |
116 | ||
98278a81 | 117 | _config->Set("APT::Solver", "internal"); |
4128c846 DK |
118 | _config->Set("edsp::scenario", "stdin"); |
119 | int input = STDIN_FILENO; | |
120 | FILE* output = stdout; | |
121 | SetNonBlock(input, false); | |
122 | ||
e876223c DK |
123 | EDSP::WriteProgress(0, "Start up solver…", output); |
124 | ||
81b3540d DK |
125 | if (pkgInitSystem(*_config,_system) == false) |
126 | DIE("System could not be initialized!"); | |
4128c846 | 127 | |
e876223c DK |
128 | EDSP::WriteProgress(1, "Read request…", output); |
129 | ||
4128c846 | 130 | if (WaitFd(input, false, 5) == false) |
81b3540d | 131 | DIE("WAIT timed out in the resolver"); |
4128c846 DK |
132 | |
133 | std::list<std::string> install, remove; | |
134 | bool upgrade, distUpgrade, autoRemove; | |
81b3540d DK |
135 | if (EDSP::ReadRequest(input, install, remove, upgrade, distUpgrade, autoRemove) == false) |
136 | DIE("Parsing the request failed!"); | |
4128c846 | 137 | |
e876223c DK |
138 | EDSP::WriteProgress(5, "Read scenario…", output); |
139 | ||
4128c846 | 140 | pkgCacheFile CacheFile; |
81b3540d DK |
141 | if (CacheFile.Open(NULL, false) == false) |
142 | DIE("Failed to open CacheFile!"); | |
4128c846 | 143 | |
e876223c DK |
144 | EDSP::WriteProgress(50, "Apply request on scenario…", output); |
145 | ||
81b3540d DK |
146 | if (EDSP::ApplyRequest(install, remove, CacheFile) == false) |
147 | DIE("Failed to apply request to depcache!"); | |
d9933172 DK |
148 | |
149 | pkgProblemResolver Fix(CacheFile); | |
150 | for (std::list<std::string>::const_iterator i = remove.begin(); | |
151 | i != remove.end(); ++i) { | |
152 | pkgCache::PkgIterator P = CacheFile->FindPkg(*i); | |
153 | Fix.Clear(P); | |
154 | Fix.Protect(P); | |
155 | Fix.Remove(P); | |
156 | } | |
157 | ||
158 | for (std::list<std::string>::const_iterator i = install.begin(); | |
159 | i != install.end(); ++i) { | |
160 | pkgCache::PkgIterator P = CacheFile->FindPkg(*i); | |
161 | Fix.Clear(P); | |
162 | Fix.Protect(P); | |
163 | } | |
164 | ||
4128c846 DK |
165 | for (std::list<std::string>::const_iterator i = install.begin(); |
166 | i != install.end(); ++i) | |
167 | CacheFile->MarkInstall(CacheFile->FindPkg(*i), true); | |
168 | ||
e876223c | 169 | EDSP::WriteProgress(60, "Call problemresolver on current scenario…", output); |
d9933172 | 170 | |
80699703 DK |
171 | if (upgrade == true) { |
172 | if (pkgAllUpgrade(CacheFile) == false) { | |
1e3f4083 | 173 | EDSP::WriteError("ERR_UNSOLVABLE_UPGRADE", "An upgrade error occurred", output); |
80699703 DK |
174 | return 0; |
175 | } | |
176 | } else if (distUpgrade == true) { | |
177 | if (pkgDistUpgrade(CacheFile) == false) { | |
1e3f4083 | 178 | EDSP::WriteError("ERR_UNSOLVABLE_DIST_UPGRADE", "An dist-upgrade error occurred", output); |
80699703 DK |
179 | return 0; |
180 | } | |
181 | } else if (Fix.Resolve() == false) { | |
1e3f4083 | 182 | EDSP::WriteError("ERR_UNSOLVABLE", "An error occurred", output); |
4128c846 DK |
183 | return 0; |
184 | } | |
185 | ||
e876223c DK |
186 | EDSP::WriteProgress(95, "Write solution…", output); |
187 | ||
81b3540d DK |
188 | if (EDSP::WriteSolution(CacheFile, output) == false) |
189 | DIE("Failed to output the solution!"); | |
4128c846 | 190 | |
e876223c DK |
191 | EDSP::WriteProgress(100, "Done", output); |
192 | ||
4128c846 DK |
193 | bool const Errors = _error->PendingError(); |
194 | if (_config->FindI("quiet",0) > 0) | |
904be352 | 195 | _error->DumpErrors(std::cerr); |
4128c846 | 196 | else |
904be352 | 197 | _error->DumpErrors(std::cerr, GlobalError::DEBUG); |
4128c846 DK |
198 | return Errors == true ? 100 : 0; |
199 | } | |
200 | /*}}}*/ |