]> git.saurik.com Git - apt.git/blame - cmdline/apt-internal-solver.cc
allow user@host (aka: no password) in URI parsing
[apt.git] / cmdline / apt-internal-solver.cc
CommitLineData
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>
ad7e0941 27
d39d7f88 28#include <apt-private/private-output.h>
ad7e0941 29#include <apt-private/private-cmndline.h>
e7e10e47 30#include <apt-private/private-main.h>
4128c846 31
453b82a3
DK
32#include <string.h>
33#include <iostream>
d39d7f88 34#include <sstream>
453b82a3
DK
35#include <list>
36#include <string>
4128c846
DK
37#include <unistd.h>
38#include <cstdio>
81b3540d 39#include <stdlib.h>
a00a9b44
DK
40
41#include <apti18n.h>
4128c846
DK
42 /*}}}*/
43
9596cb94 44static bool ShowHelp(CommandLine &) /*{{{*/
6079b276 45{
4128c846 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"
d04e44ac
JR
50 "resolver for the APT family like an external one, for debugging or\n"
51 "the like.\n");
4128c846
DK
52 return true;
53}
54 /*}}}*/
81b3540d
DK
55APT_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 /*}}}*/
9596cb94 61static std::vector<aptDispatchWithHelp> GetCommands() /*{{{*/
011188e3
DK
62{
63 return {};
64}
65 /*}}}*/
71608330
DK
66static 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 /*}}}*/
4128c846
DK
82int main(int argc,const char *argv[]) /*{{{*/
83{
e7e10e47
DK
84 // we really don't need anything
85 DropPrivileges();
fc1a78d8 86
ad7e0941 87 CommandLine CmdL;
90986d4d 88 ParseCommandLine(CmdL, APT_CMD::APT_INTERNAL_SOLVER, &_config, NULL, argc, argv, &ShowHelp, &GetCommands);
4128c846 89
904be352
DK
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);
ef00bd7a
DK
99 FileFd output;
100 if (output.OpenDescriptor(STDOUT_FILENO, FileFd::WriteOnly | FileFd::BufferedWrite, true) == false)
101 return 2;
904be352
DK
102 if (pkgset.empty() == true)
103 EDSP::WriteScenario(CacheFile, output);
104 else
6dcae298
DK
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 }
ef00bd7a 111 output.Close();
904be352
DK
112 _error->DumpErrors(std::cerr);
113 return 0;
114 }
115
4128c846
DK
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
c9443c01 123 _config->Set("APT::System", "Debian APT solver interface");
98278a81 124 _config->Set("APT::Solver", "internal");
7f58427b 125 _config->Set("edsp::scenario", "/nonexistent/stdin");
385d9f2f 126 _config->Clear("Dir::Log");
ef00bd7a
DK
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;
4128c846
DK
131 SetNonBlock(input, false);
132
e876223c
DK
133 EDSP::WriteProgress(0, "Start up solver…", output);
134
81b3540d
DK
135 if (pkgInitSystem(*_config,_system) == false)
136 DIE("System could not be initialized!");
4128c846 137
e876223c
DK
138 EDSP::WriteProgress(1, "Read request…", output);
139
4128c846 140 if (WaitFd(input, false, 5) == false)
81b3540d 141 DIE("WAIT timed out in the resolver");
4128c846
DK
142
143 std::list<std::string> install, remove;
43c71fad
DK
144 unsigned int flags;
145 if (EDSP::ReadRequest(input, install, remove, flags) == false)
81b3540d 146 DIE("Parsing the request failed!");
4128c846 147
e876223c
DK
148 EDSP::WriteProgress(5, "Read scenario…", output);
149
4128c846 150 pkgCacheFile CacheFile;
81b3540d
DK
151 if (CacheFile.Open(NULL, false) == false)
152 DIE("Failed to open CacheFile!");
4128c846 153
e876223c
DK
154 EDSP::WriteProgress(50, "Apply request on scenario…", output);
155
81b3540d
DK
156 if (EDSP::ApplyRequest(install, remove, CacheFile) == false)
157 DIE("Failed to apply request to depcache!");
d9933172
DK
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
4128c846
DK
175 for (std::list<std::string>::const_iterator i = install.begin();
176 i != install.end(); ++i)
177 CacheFile->MarkInstall(CacheFile->FindPkg(*i), true);
178
e876223c 179 EDSP::WriteProgress(60, "Call problemresolver on current scenario…", output);
d9933172 180
d39d7f88 181 std::string failure;
43c71fad
DK
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
d39d7f88 194 failure = "ERR_UNSOLVABLE_UPGRADE";
d39d7f88
DK
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);
4128c846
DK
202 return 0;
203 }
204
e876223c
DK
205 EDSP::WriteProgress(95, "Write solution…", output);
206
71608330 207 if (WriteSolution(CacheFile, output) == false)
81b3540d 208 DIE("Failed to output the solution!");
4128c846 209
e876223c
DK
210 EDSP::WriteProgress(100, "Done", output);
211
011188e3 212 return DispatchCommandLine(CmdL, {});
4128c846
DK
213}
214 /*}}}*/