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