]> git.saurik.com Git - apt.git/blob - cmdline/apt-internal-solver.cc
CMake: Add unit tests
[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 _config->Clear("Dir::Log");
127 FileFd output;
128 if (output.OpenDescriptor(STDOUT_FILENO, FileFd::WriteOnly | FileFd::BufferedWrite, true) == false)
129 DIE("stdout couldn't be opened");
130 int const input = STDIN_FILENO;
131 SetNonBlock(input, false);
132
133 EDSP::WriteProgress(0, "Start up solver…", output);
134
135 if (pkgInitSystem(*_config,_system) == false)
136 DIE("System could not be initialized!");
137
138 EDSP::WriteProgress(1, "Read request…", output);
139
140 if (WaitFd(input, false, 5) == false)
141 DIE("WAIT timed out in the resolver");
142
143 std::list<std::string> install, remove;
144 unsigned int flags;
145 if (EDSP::ReadRequest(input, install, remove, flags) == false)
146 DIE("Parsing the request failed!");
147
148 EDSP::WriteProgress(5, "Read scenario…", output);
149
150 pkgCacheFile CacheFile;
151 if (CacheFile.Open(NULL, false) == false)
152 DIE("Failed to open CacheFile!");
153
154 EDSP::WriteProgress(50, "Apply request on scenario…", output);
155
156 if (EDSP::ApplyRequest(install, remove, CacheFile) == false)
157 DIE("Failed to apply request to depcache!");
158
159 pkgProblemResolver Fix(CacheFile);
160 for (std::list<std::string>::const_iterator i = remove.begin();
161 i != remove.end(); ++i) {
162 pkgCache::PkgIterator P = CacheFile->FindPkg(*i);
163 Fix.Clear(P);
164 Fix.Protect(P);
165 Fix.Remove(P);
166 }
167
168 for (std::list<std::string>::const_iterator i = install.begin();
169 i != install.end(); ++i) {
170 pkgCache::PkgIterator P = CacheFile->FindPkg(*i);
171 Fix.Clear(P);
172 Fix.Protect(P);
173 }
174
175 for (std::list<std::string>::const_iterator i = install.begin();
176 i != install.end(); ++i)
177 CacheFile->MarkInstall(CacheFile->FindPkg(*i), true);
178
179 EDSP::WriteProgress(60, "Call problemresolver on current scenario…", output);
180
181 std::string failure;
182 if (flags & EDSP::Request::UPGRADE_ALL) {
183 int upgrade_flags = APT::Upgrade::ALLOW_EVERYTHING;
184 if (flags & EDSP::Request::FORBID_NEW_INSTALL)
185 upgrade_flags |= APT::Upgrade::FORBID_INSTALL_NEW_PACKAGES;
186 if (flags & EDSP::Request::FORBID_REMOVE)
187 upgrade_flags |= APT::Upgrade::FORBID_REMOVE_PACKAGES;
188
189 if (APT::Upgrade::Upgrade(CacheFile, upgrade_flags))
190 ;
191 else if (upgrade_flags == APT::Upgrade::ALLOW_EVERYTHING)
192 failure = "ERR_UNSOLVABLE_FULL_UPGRADE";
193 else
194 failure = "ERR_UNSOLVABLE_UPGRADE";
195 } else if (Fix.Resolve() == false)
196 failure = "ERR_UNSOLVABLE";
197
198 if (failure.empty() == false) {
199 std::ostringstream broken;
200 ShowBroken(broken, CacheFile, false);
201 EDSP::WriteError(failure.c_str(), broken.str(), output);
202 return 0;
203 }
204
205 EDSP::WriteProgress(95, "Write solution…", output);
206
207 if (WriteSolution(CacheFile, output) == false)
208 DIE("Failed to output the solution!");
209
210 EDSP::WriteProgress(100, "Done", output);
211
212 return DispatchCommandLine(CmdL, {});
213 }
214 /*}}}*/