]> git.saurik.com Git - apt.git/blame - cmdline/apt-internal-solver.cc
Get rid of .bzrignore
[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 /*}}}*/
4128c846
DK
66int main(int argc,const char *argv[]) /*{{{*/
67{
e7e10e47
DK
68 // we really don't need anything
69 DropPrivileges();
fc1a78d8 70
ad7e0941 71 CommandLine CmdL;
90986d4d 72 ParseCommandLine(CmdL, APT_CMD::APT_INTERNAL_SOLVER, &_config, NULL, argc, argv, &ShowHelp, &GetCommands);
4128c846 73
904be352
DK
74 if (CmdL.FileList[0] != 0 && strcmp(CmdL.FileList[0], "scenario") == 0)
75 {
76 if (pkgInitSystem(*_config,_system) == false) {
77 std::cerr << "System could not be initialized!" << std::endl;
78 return 1;
79 }
80 pkgCacheFile CacheFile;
81 CacheFile.Open(NULL, false);
82 APT::PackageSet pkgset = APT::PackageSet::FromCommandLine(CacheFile, CmdL.FileList + 1);
ef00bd7a
DK
83 FileFd output;
84 if (output.OpenDescriptor(STDOUT_FILENO, FileFd::WriteOnly | FileFd::BufferedWrite, true) == false)
85 return 2;
904be352
DK
86 if (pkgset.empty() == true)
87 EDSP::WriteScenario(CacheFile, output);
88 else
6dcae298
DK
89 {
90 std::vector<bool> pkgvec(CacheFile->Head().PackageCount, false);
91 for (auto const &p: pkgset)
92 pkgvec[p->ID] = true;
93 EDSP::WriteLimitedScenario(CacheFile, output, pkgvec);
94 }
ef00bd7a 95 output.Close();
904be352
DK
96 _error->DumpErrors(std::cerr);
97 return 0;
98 }
99
4128c846
DK
100 // Deal with stdout not being a tty
101 if (!isatty(STDOUT_FILENO) && _config->FindI("quiet", -1) == -1)
102 _config->Set("quiet","1");
103
104 if (_config->FindI("quiet", 0) < 1)
105 _config->Set("Debug::EDSP::WriteSolution", true);
106
c9443c01 107 _config->Set("APT::System", "Debian APT solver interface");
98278a81 108 _config->Set("APT::Solver", "internal");
7f58427b 109 _config->Set("edsp::scenario", "/nonexistent/stdin");
ef00bd7a
DK
110 FileFd output;
111 if (output.OpenDescriptor(STDOUT_FILENO, FileFd::WriteOnly | FileFd::BufferedWrite, true) == false)
112 DIE("stdout couldn't be opened");
113 int const input = STDIN_FILENO;
4128c846
DK
114 SetNonBlock(input, false);
115
e876223c
DK
116 EDSP::WriteProgress(0, "Start up solver…", output);
117
81b3540d
DK
118 if (pkgInitSystem(*_config,_system) == false)
119 DIE("System could not be initialized!");
4128c846 120
e876223c
DK
121 EDSP::WriteProgress(1, "Read request…", output);
122
4128c846 123 if (WaitFd(input, false, 5) == false)
81b3540d 124 DIE("WAIT timed out in the resolver");
4128c846
DK
125
126 std::list<std::string> install, remove;
43c71fad
DK
127 unsigned int flags;
128 if (EDSP::ReadRequest(input, install, remove, flags) == false)
81b3540d 129 DIE("Parsing the request failed!");
4128c846 130
e876223c
DK
131 EDSP::WriteProgress(5, "Read scenario…", output);
132
4128c846 133 pkgCacheFile CacheFile;
81b3540d
DK
134 if (CacheFile.Open(NULL, false) == false)
135 DIE("Failed to open CacheFile!");
4128c846 136
e876223c
DK
137 EDSP::WriteProgress(50, "Apply request on scenario…", output);
138
81b3540d
DK
139 if (EDSP::ApplyRequest(install, remove, CacheFile) == false)
140 DIE("Failed to apply request to depcache!");
d9933172
DK
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
4128c846
DK
158 for (std::list<std::string>::const_iterator i = install.begin();
159 i != install.end(); ++i)
160 CacheFile->MarkInstall(CacheFile->FindPkg(*i), true);
161
e876223c 162 EDSP::WriteProgress(60, "Call problemresolver on current scenario…", output);
d9933172 163
d39d7f88 164 std::string failure;
43c71fad
DK
165 if (flags & EDSP::Request::UPGRADE_ALL) {
166 int upgrade_flags = APT::Upgrade::ALLOW_EVERYTHING;
167 if (flags & EDSP::Request::FORBID_NEW_INSTALL)
168 upgrade_flags |= APT::Upgrade::FORBID_INSTALL_NEW_PACKAGES;
169 if (flags & EDSP::Request::FORBID_REMOVE)
170 upgrade_flags |= APT::Upgrade::FORBID_REMOVE_PACKAGES;
171
172 if (APT::Upgrade::Upgrade(CacheFile, upgrade_flags))
173 ;
174 else if (upgrade_flags == APT::Upgrade::ALLOW_EVERYTHING)
175 failure = "ERR_UNSOLVABLE_FULL_UPGRADE";
176 else
d39d7f88 177 failure = "ERR_UNSOLVABLE_UPGRADE";
d39d7f88
DK
178 } else if (Fix.Resolve() == false)
179 failure = "ERR_UNSOLVABLE";
180
181 if (failure.empty() == false) {
182 std::ostringstream broken;
183 ShowBroken(broken, CacheFile, false);
184 EDSP::WriteError(failure.c_str(), broken.str(), output);
4128c846
DK
185 return 0;
186 }
187
e876223c
DK
188 EDSP::WriteProgress(95, "Write solution…", output);
189
81b3540d
DK
190 if (EDSP::WriteSolution(CacheFile, output) == false)
191 DIE("Failed to output the solution!");
4128c846 192
e876223c
DK
193 EDSP::WriteProgress(100, "Done", output);
194
011188e3 195 return DispatchCommandLine(CmdL, {});
4128c846
DK
196}
197 /*}}}*/