]> git.saurik.com Git - apt.git/blob - cmdline/apt-internal-solver.cc
merged from donkults experimental branch
[apt.git] / cmdline / apt-internal-solver.cc
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 /*{{{*/
10 #include <apt-pkg/error.h>
11 #include <apt-pkg/cmndline.h>
12 #include <apt-pkg/init.h>
13 #include <apt-pkg/cachefile.h>
14 #include <apt-pkg/cacheset.h>
15 #include <apt-pkg/strutl.h>
16 #include <apt-pkg/edsp.h>
17 #include <apt-pkg/algorithms.h>
18 #include <apt-pkg/fileutl.h>
19 #include <apt-pkg/pkgsystem.h>
20
21 #include <config.h>
22 #include <apti18n.h>
23
24 #include <unistd.h>
25 #include <cstdio>
26 /*}}}*/
27
28 // ShowHelp - Show a help screen /*{{{*/
29 // ---------------------------------------------------------------------
30 /* */
31 bool ShowHelp(CommandLine &CmdL) {
32 ioprintf(std::cout,_("%s %s for %s compiled on %s %s\n"),PACKAGE,PACKAGE_VERSION,
33 COMMON_ARCH,__DATE__,__TIME__);
34
35 std::cout <<
36 _("Usage: apt-internal-resolver\n"
37 "\n"
38 "apt-internal-resolver is an interface to use the current internal\n"
39 "like an external resolver for the APT family for debugging or alike\n"
40 "\n"
41 "Options:\n"
42 " -h This help text.\n"
43 " -q Loggable output - no progress indicator\n"
44 " -c=? Read this configuration file\n"
45 " -o=? Set an arbitrary configuration option, eg -o dir::cache=/tmp\n"
46 "apt.conf(5) manual pages for more information and options.\n"
47 " This APT has Super Cow Powers.\n");
48 return true;
49 }
50 /*}}}*/
51 int main(int argc,const char *argv[]) /*{{{*/
52 {
53 CommandLine::Args Args[] = {
54 {'h',"help","help",0},
55 {'v',"version","version",0},
56 {'q',"quiet","quiet",CommandLine::IntLevel},
57 {'q',"silent","quiet",CommandLine::IntLevel},
58 {'c',"config-file",0,CommandLine::ConfigFile},
59 {'o',"option",0,CommandLine::ArbItem},
60 {0,0,0,0}};
61
62 CommandLine CmdL(Args,_config);
63 if (pkgInitConfig(*_config) == false ||
64 CmdL.Parse(argc,argv) == false) {
65 _error->DumpErrors();
66 return 2;
67 }
68
69 // See if the help should be shown
70 if (_config->FindB("help") == true ||
71 _config->FindB("version") == true) {
72 ShowHelp(CmdL);
73 return 1;
74 }
75
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);
85 FILE* output = stdout;
86 if (pkgset.empty() == true)
87 EDSP::WriteScenario(CacheFile, output);
88 else
89 EDSP::WriteLimitedScenario(CacheFile, output, pkgset);
90 fclose(output);
91 _error->DumpErrors(std::cerr);
92 return 0;
93 }
94
95 // Deal with stdout not being a tty
96 if (!isatty(STDOUT_FILENO) && _config->FindI("quiet", -1) == -1)
97 _config->Set("quiet","1");
98
99 if (_config->FindI("quiet", 0) < 1)
100 _config->Set("Debug::EDSP::WriteSolution", true);
101
102 _config->Set("APT::Solver", "internal");
103 _config->Set("edsp::scenario", "stdin");
104 int input = STDIN_FILENO;
105 FILE* output = stdout;
106 SetNonBlock(input, false);
107
108 EDSP::WriteProgress(0, "Start up solver…", output);
109
110 if (pkgInitSystem(*_config,_system) == false) {
111 std::cerr << "System could not be initialized!" << std::endl;
112 return 1;
113 }
114
115 EDSP::WriteProgress(1, "Read request…", output);
116
117 if (WaitFd(input, false, 5) == false)
118 std::cerr << "WAIT timed out in the resolver" << std::endl;
119
120 std::list<std::string> install, remove;
121 bool upgrade, distUpgrade, autoRemove;
122 if (EDSP::ReadRequest(input, install, remove, upgrade, distUpgrade, autoRemove) == false) {
123 std::cerr << "Parsing the request failed!" << std::endl;
124 return 2;
125 }
126
127 EDSP::WriteProgress(5, "Read scenario…", output);
128
129 pkgCacheFile CacheFile;
130 CacheFile.Open(NULL, false);
131
132 EDSP::WriteProgress(50, "Apply request on scenario…", output);
133
134 if (EDSP::ApplyRequest(install, remove, CacheFile) == false) {
135 std::cerr << "Failed to apply request to depcache!" << std::endl;
136 return 3;
137 }
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
155 for (std::list<std::string>::const_iterator i = install.begin();
156 i != install.end(); ++i)
157 CacheFile->MarkInstall(CacheFile->FindPkg(*i), true);
158
159 EDSP::WriteProgress(60, "Call problemresolver on current scenario…", output);
160
161 if (upgrade == true) {
162 if (pkgAllUpgrade(CacheFile) == false) {
163 EDSP::WriteError("ERR_UNSOLVABLE_UPGRADE", "An upgrade error occured", output);
164 return 0;
165 }
166 } else if (distUpgrade == true) {
167 if (pkgDistUpgrade(CacheFile) == false) {
168 EDSP::WriteError("ERR_UNSOLVABLE_DIST_UPGRADE", "An dist-upgrade error occured", output);
169 return 0;
170 }
171 } else if (Fix.Resolve() == false) {
172 EDSP::WriteError("ERR_UNSOLVABLE", "An error occured", output);
173 return 0;
174 }
175
176 EDSP::WriteProgress(95, "Write solution…", output);
177
178 if (EDSP::WriteSolution(CacheFile, output) == false) {
179 std::cerr << "Failed to output the solution!" << std::endl;
180 return 4;
181 }
182
183 EDSP::WriteProgress(100, "Done", output);
184
185 bool const Errors = _error->PendingError();
186 if (_config->FindI("quiet",0) > 0)
187 _error->DumpErrors(std::cerr);
188 else
189 _error->DumpErrors(std::cerr, GlobalError::DEBUG);
190 return Errors == true ? 100 : 0;
191 }
192 /*}}}*/