]> git.saurik.com Git - apt.git/blame - cmdline/apt-internal-solver.cc
avoid triggering gcc's -Wunsafe-loop-optimizations in EDSP
[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 InitLocale();
69
70 // we really don't need anything
71 DropPrivileges();
fc1a78d8 72
ad7e0941 73 CommandLine CmdL;
90986d4d 74 ParseCommandLine(CmdL, APT_CMD::APT_INTERNAL_SOLVER, &_config, NULL, argc, argv, &ShowHelp, &GetCommands);
4128c846 75
904be352
DK
76 if (CmdL.FileList[0] != 0 && strcmp(CmdL.FileList[0], "scenario") == 0)
77 {
78 if (pkgInitSystem(*_config,_system) == false) {
79 std::cerr << "System could not be initialized!" << std::endl;
80 return 1;
81 }
82 pkgCacheFile CacheFile;
83 CacheFile.Open(NULL, false);
84 APT::PackageSet pkgset = APT::PackageSet::FromCommandLine(CacheFile, CmdL.FileList + 1);
ef00bd7a
DK
85 FileFd output;
86 if (output.OpenDescriptor(STDOUT_FILENO, FileFd::WriteOnly | FileFd::BufferedWrite, true) == false)
87 return 2;
904be352
DK
88 if (pkgset.empty() == true)
89 EDSP::WriteScenario(CacheFile, output);
90 else
91 EDSP::WriteLimitedScenario(CacheFile, output, pkgset);
ef00bd7a 92 output.Close();
904be352
DK
93 _error->DumpErrors(std::cerr);
94 return 0;
95 }
96
4128c846
DK
97 // Deal with stdout not being a tty
98 if (!isatty(STDOUT_FILENO) && _config->FindI("quiet", -1) == -1)
99 _config->Set("quiet","1");
100
101 if (_config->FindI("quiet", 0) < 1)
102 _config->Set("Debug::EDSP::WriteSolution", true);
103
c9443c01 104 _config->Set("APT::System", "Debian APT solver interface");
98278a81 105 _config->Set("APT::Solver", "internal");
7f58427b 106 _config->Set("edsp::scenario", "/nonexistent/stdin");
ef00bd7a
DK
107 FileFd output;
108 if (output.OpenDescriptor(STDOUT_FILENO, FileFd::WriteOnly | FileFd::BufferedWrite, true) == false)
109 DIE("stdout couldn't be opened");
110 int const input = STDIN_FILENO;
4128c846
DK
111 SetNonBlock(input, false);
112
e876223c
DK
113 EDSP::WriteProgress(0, "Start up solver…", output);
114
81b3540d
DK
115 if (pkgInitSystem(*_config,_system) == false)
116 DIE("System could not be initialized!");
4128c846 117
e876223c
DK
118 EDSP::WriteProgress(1, "Read request…", output);
119
4128c846 120 if (WaitFd(input, false, 5) == false)
81b3540d 121 DIE("WAIT timed out in the resolver");
4128c846
DK
122
123 std::list<std::string> install, remove;
124 bool upgrade, distUpgrade, autoRemove;
81b3540d
DK
125 if (EDSP::ReadRequest(input, install, remove, upgrade, distUpgrade, autoRemove) == false)
126 DIE("Parsing the request failed!");
4128c846 127
e876223c
DK
128 EDSP::WriteProgress(5, "Read scenario…", output);
129
4128c846 130 pkgCacheFile CacheFile;
81b3540d
DK
131 if (CacheFile.Open(NULL, false) == false)
132 DIE("Failed to open CacheFile!");
4128c846 133
e876223c
DK
134 EDSP::WriteProgress(50, "Apply request on scenario…", output);
135
81b3540d
DK
136 if (EDSP::ApplyRequest(install, remove, CacheFile) == false)
137 DIE("Failed to apply request to depcache!");
d9933172
DK
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
4128c846
DK
155 for (std::list<std::string>::const_iterator i = install.begin();
156 i != install.end(); ++i)
157 CacheFile->MarkInstall(CacheFile->FindPkg(*i), true);
158
e876223c 159 EDSP::WriteProgress(60, "Call problemresolver on current scenario…", output);
d9933172 160
d39d7f88 161 std::string failure;
80699703 162 if (upgrade == true) {
67caa2e6 163 if (APT::Upgrade::Upgrade(CacheFile, APT::Upgrade::FORBID_REMOVE_PACKAGES | APT::Upgrade::FORBID_INSTALL_NEW_PACKAGES) == false)
d39d7f88 164 failure = "ERR_UNSOLVABLE_UPGRADE";
80699703 165 } else if (distUpgrade == true) {
67caa2e6 166 if (APT::Upgrade::Upgrade(CacheFile, APT::Upgrade::ALLOW_EVERYTHING) == false)
d39d7f88
DK
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);
4128c846
DK
175 return 0;
176 }
177
e876223c
DK
178 EDSP::WriteProgress(95, "Write solution…", output);
179
81b3540d
DK
180 if (EDSP::WriteSolution(CacheFile, output) == false)
181 DIE("Failed to output the solution!");
4128c846 182
e876223c
DK
183 EDSP::WriteProgress(100, "Done", output);
184
011188e3 185 return DispatchCommandLine(CmdL, {});
4128c846
DK
186}
187 /*}}}*/