f6eaa11f7023a92f0ce6d6453f7a8ff0bed8ae18
[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
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 // ShowHelp - Show a help screen /*{{{*/
45 bool ShowHelp(CommandLine &, CommandLine::DispatchWithHelp const *) {
46 ioprintf(std::cout, "%s %s (%s)\n", PACKAGE, PACKAGE_VERSION, COMMON_ARCH);
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 std::vector<CommandLine::DispatchWithHelp> GetCommands() /*{{{*/
69 {
70 return {};
71 }
72 /*}}}*/
73 int main(int argc,const char *argv[]) /*{{{*/
74 {
75 InitLocale();
76
77 // we really don't need anything
78 DropPrivileges();
79
80 CommandLine CmdL;
81 ParseCommandLine(CmdL, APT_CMD::APT_INTERNAL_SOLVER, &_config, NULL, argc, argv);
82
83 if (CmdL.FileList[0] != 0 && strcmp(CmdL.FileList[0], "scenario") == 0)
84 {
85 if (pkgInitSystem(*_config,_system) == false) {
86 std::cerr << "System could not be initialized!" << std::endl;
87 return 1;
88 }
89 pkgCacheFile CacheFile;
90 CacheFile.Open(NULL, false);
91 APT::PackageSet pkgset = APT::PackageSet::FromCommandLine(CacheFile, CmdL.FileList + 1);
92 FILE* output = stdout;
93 if (pkgset.empty() == true)
94 EDSP::WriteScenario(CacheFile, output);
95 else
96 EDSP::WriteLimitedScenario(CacheFile, output, pkgset);
97 fclose(output);
98 _error->DumpErrors(std::cerr);
99 return 0;
100 }
101
102 // Deal with stdout not being a tty
103 if (!isatty(STDOUT_FILENO) && _config->FindI("quiet", -1) == -1)
104 _config->Set("quiet","1");
105
106 if (_config->FindI("quiet", 0) < 1)
107 _config->Set("Debug::EDSP::WriteSolution", true);
108
109 _config->Set("APT::System", "Debian APT solver interface");
110 _config->Set("APT::Solver", "internal");
111 _config->Set("edsp::scenario", "/nonexistent/stdin");
112 int input = STDIN_FILENO;
113 FILE* output = stdout;
114 SetNonBlock(input, false);
115
116 EDSP::WriteProgress(0, "Start up solver…", output);
117
118 if (pkgInitSystem(*_config,_system) == false)
119 DIE("System could not be initialized!");
120
121 EDSP::WriteProgress(1, "Read request…", output);
122
123 if (WaitFd(input, false, 5) == false)
124 DIE("WAIT timed out in the resolver");
125
126 std::list<std::string> install, remove;
127 bool upgrade, distUpgrade, autoRemove;
128 if (EDSP::ReadRequest(input, install, remove, upgrade, distUpgrade, autoRemove) == false)
129 DIE("Parsing the request failed!");
130
131 EDSP::WriteProgress(5, "Read scenario…", output);
132
133 pkgCacheFile CacheFile;
134 if (CacheFile.Open(NULL, false) == false)
135 DIE("Failed to open CacheFile!");
136
137 EDSP::WriteProgress(50, "Apply request on scenario…", output);
138
139 if (EDSP::ApplyRequest(install, remove, CacheFile) == false)
140 DIE("Failed to apply request to depcache!");
141
142 pkgProblemResolver Fix(CacheFile);
143 for (std::list<std::string>::const_iterator i = remove.begin();
144 i != remove.end(); ++i) {
145 pkgCache::PkgIterator P = CacheFile->FindPkg(*i);
146 Fix.Clear(P);
147 Fix.Protect(P);
148 Fix.Remove(P);
149 }
150
151 for (std::list<std::string>::const_iterator i = install.begin();
152 i != install.end(); ++i) {
153 pkgCache::PkgIterator P = CacheFile->FindPkg(*i);
154 Fix.Clear(P);
155 Fix.Protect(P);
156 }
157
158 for (std::list<std::string>::const_iterator i = install.begin();
159 i != install.end(); ++i)
160 CacheFile->MarkInstall(CacheFile->FindPkg(*i), true);
161
162 EDSP::WriteProgress(60, "Call problemresolver on current scenario…", output);
163
164 std::string failure;
165 if (upgrade == true) {
166 if (APT::Upgrade::Upgrade(CacheFile, APT::Upgrade::FORBID_REMOVE_PACKAGES | APT::Upgrade::FORBID_INSTALL_NEW_PACKAGES) == false)
167 failure = "ERR_UNSOLVABLE_UPGRADE";
168 } else if (distUpgrade == true) {
169 if (APT::Upgrade::Upgrade(CacheFile, APT::Upgrade::ALLOW_EVERYTHING) == false)
170 failure = "ERR_UNSOLVABLE_DIST_UPGRADE";
171 } else if (Fix.Resolve() == false)
172 failure = "ERR_UNSOLVABLE";
173
174 if (failure.empty() == false) {
175 std::ostringstream broken;
176 ShowBroken(broken, CacheFile, false);
177 EDSP::WriteError(failure.c_str(), broken.str(), output);
178 return 0;
179 }
180
181 EDSP::WriteProgress(95, "Write solution…", output);
182
183 if (EDSP::WriteSolution(CacheFile, output) == false)
184 DIE("Failed to output the solution!");
185
186 EDSP::WriteProgress(100, "Done", output);
187
188 return DispatchCommandLine(CmdL, {});
189 }
190 /*}}}*/