]> git.saurik.com Git - apt.git/blob - cmdline/apt-internal-solver.cc
8296e8d01514f2633298337caa18dcad755dd654
[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 static 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 "resolver for the APT family like an external one, for debugging or\n"
51 "the like.\n");
52 return true;
53 }
54 /*}}}*/
55 APT_NORETURN static void DIE(std::string const &message) { /*{{{*/
56 std::cerr << "ERROR: " << message << std::endl;
57 _error->DumpErrors(std::cerr);
58 exit(EXIT_FAILURE);
59 }
60 /*}}}*/
61 static std::vector<aptDispatchWithHelp> GetCommands() /*{{{*/
62 {
63 return {};
64 }
65 /*}}}*/
66 static bool WriteSolution(pkgDepCache &Cache, FileFd &output) /*{{{*/
67 {
68 bool Okay = output.Failed() == false;
69 for (pkgCache::PkgIterator Pkg = Cache.PkgBegin(); Pkg.end() == false && likely(Okay); ++Pkg)
70 {
71 std::string action;
72 if (Cache[Pkg].Delete() == true)
73 Okay &= EDSP::WriteSolutionStanza(output, "Remove", Pkg.CurrentVer());
74 else if (Cache[Pkg].NewInstall() == true || Cache[Pkg].Upgrade() == true)
75 Okay &= EDSP::WriteSolutionStanza(output, "Install", Cache.GetCandidateVersion(Pkg));
76 else if (Cache[Pkg].Garbage == true)
77 Okay &= EDSP::WriteSolutionStanza(output, "Autoremove", Pkg.CurrentVer());
78 }
79 return Okay;
80 }
81 /*}}}*/
82 int main(int argc,const char *argv[]) /*{{{*/
83 {
84 // we really don't need anything
85 DropPrivileges();
86
87 CommandLine CmdL;
88 ParseCommandLine(CmdL, APT_CMD::APT_INTERNAL_SOLVER, &_config, NULL, argc, argv, &ShowHelp, &GetCommands);
89
90 if (CmdL.FileList[0] != 0 && strcmp(CmdL.FileList[0], "scenario") == 0)
91 {
92 if (pkgInitSystem(*_config,_system) == false) {
93 std::cerr << "System could not be initialized!" << std::endl;
94 return 1;
95 }
96 pkgCacheFile CacheFile;
97 CacheFile.Open(NULL, false);
98 APT::PackageSet pkgset = APT::PackageSet::FromCommandLine(CacheFile, CmdL.FileList + 1);
99 FileFd output;
100 if (output.OpenDescriptor(STDOUT_FILENO, FileFd::WriteOnly | FileFd::BufferedWrite, true) == false)
101 return 2;
102 if (pkgset.empty() == true)
103 EDSP::WriteScenario(CacheFile, output);
104 else
105 {
106 std::vector<bool> pkgvec(CacheFile->Head().PackageCount, false);
107 for (auto const &p: pkgset)
108 pkgvec[p->ID] = true;
109 EDSP::WriteLimitedScenario(CacheFile, output, pkgvec);
110 }
111 output.Close();
112 _error->DumpErrors(std::cerr);
113 return 0;
114 }
115
116 // Deal with stdout not being a tty
117 if (!isatty(STDOUT_FILENO) && _config->FindI("quiet", -1) == -1)
118 _config->Set("quiet","1");
119
120 if (_config->FindI("quiet", 0) < 1)
121 _config->Set("Debug::EDSP::WriteSolution", true);
122
123 _config->Set("APT::System", "Debian APT solver interface");
124 _config->Set("APT::Solver", "internal");
125 _config->Set("edsp::scenario", "/nonexistent/stdin");
126 FileFd output;
127 if (output.OpenDescriptor(STDOUT_FILENO, FileFd::WriteOnly | FileFd::BufferedWrite, true) == false)
128 DIE("stdout couldn't be opened");
129 int const input = STDIN_FILENO;
130 SetNonBlock(input, false);
131
132 EDSP::WriteProgress(0, "Start up solver…", output);
133
134 if (pkgInitSystem(*_config,_system) == false)
135 DIE("System could not be initialized!");
136
137 EDSP::WriteProgress(1, "Read request…", output);
138
139 if (WaitFd(input, false, 5) == false)
140 DIE("WAIT timed out in the resolver");
141
142 std::list<std::string> install, remove;
143 unsigned int flags;
144 if (EDSP::ReadRequest(input, install, remove, flags) == false)
145 DIE("Parsing the request failed!");
146
147 EDSP::WriteProgress(5, "Read scenario…", output);
148
149 pkgCacheFile CacheFile;
150 if (CacheFile.Open(NULL, false) == false)
151 DIE("Failed to open CacheFile!");
152
153 EDSP::WriteProgress(50, "Apply request on scenario…", output);
154
155 if (EDSP::ApplyRequest(install, remove, CacheFile) == false)
156 DIE("Failed to apply request to depcache!");
157
158 pkgProblemResolver Fix(CacheFile);
159 for (std::list<std::string>::const_iterator i = remove.begin();
160 i != remove.end(); ++i) {
161 pkgCache::PkgIterator P = CacheFile->FindPkg(*i);
162 Fix.Clear(P);
163 Fix.Protect(P);
164 Fix.Remove(P);
165 }
166
167 for (std::list<std::string>::const_iterator i = install.begin();
168 i != install.end(); ++i) {
169 pkgCache::PkgIterator P = CacheFile->FindPkg(*i);
170 Fix.Clear(P);
171 Fix.Protect(P);
172 }
173
174 for (std::list<std::string>::const_iterator i = install.begin();
175 i != install.end(); ++i)
176 CacheFile->MarkInstall(CacheFile->FindPkg(*i), true);
177
178 EDSP::WriteProgress(60, "Call problemresolver on current scenario…", output);
179
180 std::string failure;
181 if (flags & EDSP::Request::UPGRADE_ALL) {
182 int upgrade_flags = APT::Upgrade::ALLOW_EVERYTHING;
183 if (flags & EDSP::Request::FORBID_NEW_INSTALL)
184 upgrade_flags |= APT::Upgrade::FORBID_INSTALL_NEW_PACKAGES;
185 if (flags & EDSP::Request::FORBID_REMOVE)
186 upgrade_flags |= APT::Upgrade::FORBID_REMOVE_PACKAGES;
187
188 if (APT::Upgrade::Upgrade(CacheFile, upgrade_flags))
189 ;
190 else if (upgrade_flags == APT::Upgrade::ALLOW_EVERYTHING)
191 failure = "ERR_UNSOLVABLE_FULL_UPGRADE";
192 else
193 failure = "ERR_UNSOLVABLE_UPGRADE";
194 } else if (Fix.Resolve() == false)
195 failure = "ERR_UNSOLVABLE";
196
197 if (failure.empty() == false) {
198 std::ostringstream broken;
199 ShowBroken(broken, CacheFile, false);
200 EDSP::WriteError(failure.c_str(), broken.str(), output);
201 return 0;
202 }
203
204 EDSP::WriteProgress(95, "Write solution…", output);
205
206 if (WriteSolution(CacheFile, output) == false)
207 DIE("Failed to output the solution!");
208
209 EDSP::WriteProgress(100, "Done", output);
210
211 return DispatchCommandLine(CmdL, {});
212 }
213 /*}}}*/