]> git.saurik.com Git - apt.git/blame - cmdline/apt-internal-solver.cc
Merge branch 'debian/sid' into debian/experimental
[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>
d39d7f88 27#include <apt-private/private-output.h>
4128c846 28
453b82a3
DK
29#include <string.h>
30#include <iostream>
d39d7f88 31#include <sstream>
453b82a3
DK
32#include <list>
33#include <string>
4128c846
DK
34#include <unistd.h>
35#include <cstdio>
81b3540d 36#include <stdlib.h>
a00a9b44
DK
37
38#include <apti18n.h>
4128c846
DK
39 /*}}}*/
40
41// ShowHelp - Show a help screen /*{{{*/
42// ---------------------------------------------------------------------
43/* */
65512241 44static bool ShowHelp(CommandLine &) {
9179f697 45 ioprintf(std::cout,_("%s %s for %s compiled on %s %s\n"),PACKAGE,PACKAGE_VERSION,
4128c846
DK
46 COMMON_ARCH,__DATE__,__TIME__);
47
48 std::cout <<
3999d158 49 _("Usage: apt-internal-solver\n"
4128c846 50 "\n"
3999d158 51 "apt-internal-solver is an interface to use the current internal\n"
4128c846
DK
52 "like an external resolver for the APT family for debugging or alike\n"
53 "\n"
54 "Options:\n"
55 " -h This help text.\n"
56 " -q Loggable output - no progress indicator\n"
57 " -c=? Read this configuration file\n"
3999d158 58 " -o=? Set an arbitrary configuration option, eg -o dir::cache=/tmp\n");
4128c846
DK
59 return true;
60}
61 /*}}}*/
81b3540d
DK
62APT_NORETURN static void DIE(std::string const &message) { /*{{{*/
63 std::cerr << "ERROR: " << message << std::endl;
64 _error->DumpErrors(std::cerr);
65 exit(EXIT_FAILURE);
66}
67 /*}}}*/
4128c846
DK
68int main(int argc,const char *argv[]) /*{{{*/
69{
70 CommandLine::Args Args[] = {
71 {'h',"help","help",0},
72 {'v',"version","version",0},
73 {'q',"quiet","quiet",CommandLine::IntLevel},
74 {'q',"silent","quiet",CommandLine::IntLevel},
904be352
DK
75 {'c',"config-file",0,CommandLine::ConfigFile},
76 {'o',"option",0,CommandLine::ArbItem},
4128c846
DK
77 {0,0,0,0}};
78
fc1a78d8
MV
79 // we really don't need anything
80 DropPrivs();
81
4128c846
DK
82 CommandLine CmdL(Args,_config);
83 if (pkgInitConfig(*_config) == false ||
84 CmdL.Parse(argc,argv) == false) {
85 _error->DumpErrors();
86 return 2;
87 }
88
89 // See if the help should be shown
90 if (_config->FindB("help") == true ||
91 _config->FindB("version") == true) {
92 ShowHelp(CmdL);
93 return 1;
94 }
95
904be352
DK
96 if (CmdL.FileList[0] != 0 && strcmp(CmdL.FileList[0], "scenario") == 0)
97 {
98 if (pkgInitSystem(*_config,_system) == false) {
99 std::cerr << "System could not be initialized!" << std::endl;
100 return 1;
101 }
102 pkgCacheFile CacheFile;
103 CacheFile.Open(NULL, false);
104 APT::PackageSet pkgset = APT::PackageSet::FromCommandLine(CacheFile, CmdL.FileList + 1);
105 FILE* output = stdout;
106 if (pkgset.empty() == true)
107 EDSP::WriteScenario(CacheFile, output);
108 else
109 EDSP::WriteLimitedScenario(CacheFile, output, pkgset);
110 fclose(output);
111 _error->DumpErrors(std::cerr);
112 return 0;
113 }
114
4128c846
DK
115 // Deal with stdout not being a tty
116 if (!isatty(STDOUT_FILENO) && _config->FindI("quiet", -1) == -1)
117 _config->Set("quiet","1");
118
119 if (_config->FindI("quiet", 0) < 1)
120 _config->Set("Debug::EDSP::WriteSolution", true);
121
98278a81 122 _config->Set("APT::Solver", "internal");
4128c846
DK
123 _config->Set("edsp::scenario", "stdin");
124 int input = STDIN_FILENO;
125 FILE* output = stdout;
126 SetNonBlock(input, false);
127
e876223c
DK
128 EDSP::WriteProgress(0, "Start up solver…", output);
129
81b3540d
DK
130 if (pkgInitSystem(*_config,_system) == false)
131 DIE("System could not be initialized!");
4128c846 132
e876223c
DK
133 EDSP::WriteProgress(1, "Read request…", output);
134
4128c846 135 if (WaitFd(input, false, 5) == false)
81b3540d 136 DIE("WAIT timed out in the resolver");
4128c846
DK
137
138 std::list<std::string> install, remove;
139 bool upgrade, distUpgrade, autoRemove;
81b3540d
DK
140 if (EDSP::ReadRequest(input, install, remove, upgrade, distUpgrade, autoRemove) == false)
141 DIE("Parsing the request failed!");
4128c846 142
e876223c
DK
143 EDSP::WriteProgress(5, "Read scenario…", output);
144
4128c846 145 pkgCacheFile CacheFile;
81b3540d
DK
146 if (CacheFile.Open(NULL, false) == false)
147 DIE("Failed to open CacheFile!");
4128c846 148
e876223c
DK
149 EDSP::WriteProgress(50, "Apply request on scenario…", output);
150
81b3540d
DK
151 if (EDSP::ApplyRequest(install, remove, CacheFile) == false)
152 DIE("Failed to apply request to depcache!");
d9933172
DK
153
154 pkgProblemResolver Fix(CacheFile);
155 for (std::list<std::string>::const_iterator i = remove.begin();
156 i != remove.end(); ++i) {
157 pkgCache::PkgIterator P = CacheFile->FindPkg(*i);
158 Fix.Clear(P);
159 Fix.Protect(P);
160 Fix.Remove(P);
161 }
162
163 for (std::list<std::string>::const_iterator i = install.begin();
164 i != install.end(); ++i) {
165 pkgCache::PkgIterator P = CacheFile->FindPkg(*i);
166 Fix.Clear(P);
167 Fix.Protect(P);
168 }
169
4128c846
DK
170 for (std::list<std::string>::const_iterator i = install.begin();
171 i != install.end(); ++i)
172 CacheFile->MarkInstall(CacheFile->FindPkg(*i), true);
173
e876223c 174 EDSP::WriteProgress(60, "Call problemresolver on current scenario…", output);
d9933172 175
d39d7f88 176 std::string failure;
80699703 177 if (upgrade == true) {
67caa2e6 178 if (APT::Upgrade::Upgrade(CacheFile, APT::Upgrade::FORBID_REMOVE_PACKAGES | APT::Upgrade::FORBID_INSTALL_NEW_PACKAGES) == false)
d39d7f88 179 failure = "ERR_UNSOLVABLE_UPGRADE";
80699703 180 } else if (distUpgrade == true) {
67caa2e6 181 if (APT::Upgrade::Upgrade(CacheFile, APT::Upgrade::ALLOW_EVERYTHING) == false)
d39d7f88
DK
182 failure = "ERR_UNSOLVABLE_DIST_UPGRADE";
183 } else if (Fix.Resolve() == false)
184 failure = "ERR_UNSOLVABLE";
185
186 if (failure.empty() == false) {
187 std::ostringstream broken;
188 ShowBroken(broken, CacheFile, false);
189 EDSP::WriteError(failure.c_str(), broken.str(), output);
4128c846
DK
190 return 0;
191 }
192
e876223c
DK
193 EDSP::WriteProgress(95, "Write solution…", output);
194
81b3540d
DK
195 if (EDSP::WriteSolution(CacheFile, output) == false)
196 DIE("Failed to output the solution!");
4128c846 197
e876223c
DK
198 EDSP::WriteProgress(100, "Done", output);
199
4128c846
DK
200 bool const Errors = _error->PendingError();
201 if (_config->FindI("quiet",0) > 0)
904be352 202 _error->DumpErrors(std::cerr);
4128c846 203 else
904be352 204 _error->DumpErrors(std::cerr, GlobalError::DEBUG);
4128c846
DK
205 return Errors == true ? 100 : 0;
206}
207 /*}}}*/