]> git.saurik.com Git - apt.git/blob - cmdline/apt-internal-solver.cc
* lp:~mvo/apt/config-clear:
[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-solver\n"
38 "\n"
39 "apt-internal-solver 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 return true;
48 }
49 /*}}}*/
50 int main(int argc,const char *argv[]) /*{{{*/
51 {
52 CommandLine::Args Args[] = {
53 {'h',"help","help",0},
54 {'v',"version","version",0},
55 {'q',"quiet","quiet",CommandLine::IntLevel},
56 {'q',"silent","quiet",CommandLine::IntLevel},
57 {'c',"config-file",0,CommandLine::ConfigFile},
58 {'o',"option",0,CommandLine::ArbItem},
59 {0,0,0,0}};
60
61 CommandLine CmdL(Args,_config);
62 if (pkgInitConfig(*_config) == false ||
63 CmdL.Parse(argc,argv) == false) {
64 _error->DumpErrors();
65 return 2;
66 }
67
68 // See if the help should be shown
69 if (_config->FindB("help") == true ||
70 _config->FindB("version") == true) {
71 ShowHelp(CmdL);
72 return 1;
73 }
74
75 if (CmdL.FileList[0] != 0 && strcmp(CmdL.FileList[0], "scenario") == 0)
76 {
77 if (pkgInitSystem(*_config,_system) == false) {
78 std::cerr << "System could not be initialized!" << std::endl;
79 return 1;
80 }
81 pkgCacheFile CacheFile;
82 CacheFile.Open(NULL, false);
83 APT::PackageSet pkgset = APT::PackageSet::FromCommandLine(CacheFile, CmdL.FileList + 1);
84 FILE* output = stdout;
85 if (pkgset.empty() == true)
86 EDSP::WriteScenario(CacheFile, output);
87 else
88 EDSP::WriteLimitedScenario(CacheFile, output, pkgset);
89 fclose(output);
90 _error->DumpErrors(std::cerr);
91 return 0;
92 }
93
94 // Deal with stdout not being a tty
95 if (!isatty(STDOUT_FILENO) && _config->FindI("quiet", -1) == -1)
96 _config->Set("quiet","1");
97
98 if (_config->FindI("quiet", 0) < 1)
99 _config->Set("Debug::EDSP::WriteSolution", true);
100
101 _config->Set("APT::Solver", "internal");
102 _config->Set("edsp::scenario", "stdin");
103 int input = STDIN_FILENO;
104 FILE* output = stdout;
105 SetNonBlock(input, false);
106
107 EDSP::WriteProgress(0, "Start up solver…", output);
108
109 if (pkgInitSystem(*_config,_system) == false) {
110 std::cerr << "System could not be initialized!" << std::endl;
111 return 1;
112 }
113
114 EDSP::WriteProgress(1, "Read request…", output);
115
116 if (WaitFd(input, false, 5) == false)
117 std::cerr << "WAIT timed out in the resolver" << std::endl;
118
119 std::list<std::string> install, remove;
120 bool upgrade, distUpgrade, autoRemove;
121 if (EDSP::ReadRequest(input, install, remove, upgrade, distUpgrade, autoRemove) == false) {
122 std::cerr << "Parsing the request failed!" << std::endl;
123 return 2;
124 }
125
126 EDSP::WriteProgress(5, "Read scenario…", output);
127
128 pkgCacheFile CacheFile;
129 CacheFile.Open(NULL, false);
130
131 EDSP::WriteProgress(50, "Apply request on scenario…", output);
132
133 if (EDSP::ApplyRequest(install, remove, CacheFile) == false) {
134 std::cerr << "Failed to apply request to depcache!" << std::endl;
135 return 3;
136 }
137
138 pkgProblemResolver Fix(CacheFile);
139 for (std::list<std::string>::const_iterator i = remove.begin();
140 i != remove.end(); ++i) {
141 pkgCache::PkgIterator P = CacheFile->FindPkg(*i);
142 Fix.Clear(P);
143 Fix.Protect(P);
144 Fix.Remove(P);
145 }
146
147 for (std::list<std::string>::const_iterator i = install.begin();
148 i != install.end(); ++i) {
149 pkgCache::PkgIterator P = CacheFile->FindPkg(*i);
150 Fix.Clear(P);
151 Fix.Protect(P);
152 }
153
154 for (std::list<std::string>::const_iterator i = install.begin();
155 i != install.end(); ++i)
156 CacheFile->MarkInstall(CacheFile->FindPkg(*i), true);
157
158 EDSP::WriteProgress(60, "Call problemresolver on current scenario…", output);
159
160 if (upgrade == true) {
161 if (pkgAllUpgrade(CacheFile) == false) {
162 EDSP::WriteError("ERR_UNSOLVABLE_UPGRADE", "An upgrade error occured", output);
163 return 0;
164 }
165 } else if (distUpgrade == true) {
166 if (pkgDistUpgrade(CacheFile) == false) {
167 EDSP::WriteError("ERR_UNSOLVABLE_DIST_UPGRADE", "An dist-upgrade error occured", output);
168 return 0;
169 }
170 } else if (Fix.Resolve() == false) {
171 EDSP::WriteError("ERR_UNSOLVABLE", "An error occured", output);
172 return 0;
173 }
174
175 EDSP::WriteProgress(95, "Write solution…", output);
176
177 if (EDSP::WriteSolution(CacheFile, output) == false) {
178 std::cerr << "Failed to output the solution!" << std::endl;
179 return 4;
180 }
181
182 EDSP::WriteProgress(100, "Done", output);
183
184 bool const Errors = _error->PendingError();
185 if (_config->FindI("quiet",0) > 0)
186 _error->DumpErrors(std::cerr);
187 else
188 _error->DumpErrors(std::cerr, GlobalError::DEBUG);
189 return Errors == true ? 100 : 0;
190 }
191 /*}}}*/