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