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