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