]> git.saurik.com Git - apt.git/blob - cmdline/apt-internal-solver.cc
if file is inaccessible for _apt, disable privilege drop in acquire
[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 <apt-private/private-output.h>
29 #include <apt-private/private-cmndline.h>
30
31 #include <string.h>
32 #include <iostream>
33 #include <sstream>
34 #include <list>
35 #include <string>
36 #include <unistd.h>
37 #include <cstdio>
38 #include <stdlib.h>
39
40 #include <apti18n.h>
41 /*}}}*/
42
43 // ShowHelp - Show a help screen /*{{{*/
44 // ---------------------------------------------------------------------
45 /* */
46 static bool ShowHelp(CommandLine &) {
47 ioprintf(std::cout, "%s %s (%s)\n", PACKAGE, PACKAGE_VERSION, COMMON_ARCH);
48
49 std::cout <<
50 _("Usage: apt-internal-solver\n"
51 "\n"
52 "apt-internal-solver is an interface to use the current internal\n"
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"
59 " -o=? Set an arbitrary configuration option, eg -o dir::cache=/tmp\n");
60 return true;
61 }
62 /*}}}*/
63 APT_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 /*}}}*/
69 int 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},
76 {'c',"config-file",0,CommandLine::ConfigFile},
77 {'o',"option",0,CommandLine::ArbItem},
78 {0,0,0,0}};
79
80 // we really don't need anything
81 DropPrivileges();
82
83 CommandLine CmdL;
84 ParseCommandLine(CmdL, NULL, Args, &_config, NULL, argc, argv, ShowHelp);
85
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
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
112 _config->Set("APT::System", "Debian APT solver interface");
113 _config->Set("APT::Solver", "internal");
114 _config->Set("edsp::scenario", "stdin");
115 int input = STDIN_FILENO;
116 FILE* output = stdout;
117 SetNonBlock(input, false);
118
119 EDSP::WriteProgress(0, "Start up solver…", output);
120
121 if (pkgInitSystem(*_config,_system) == false)
122 DIE("System could not be initialized!");
123
124 EDSP::WriteProgress(1, "Read request…", output);
125
126 if (WaitFd(input, false, 5) == false)
127 DIE("WAIT timed out in the resolver");
128
129 std::list<std::string> install, remove;
130 bool upgrade, distUpgrade, autoRemove;
131 if (EDSP::ReadRequest(input, install, remove, upgrade, distUpgrade, autoRemove) == false)
132 DIE("Parsing the request failed!");
133
134 EDSP::WriteProgress(5, "Read scenario…", output);
135
136 pkgCacheFile CacheFile;
137 if (CacheFile.Open(NULL, false) == false)
138 DIE("Failed to open CacheFile!");
139
140 EDSP::WriteProgress(50, "Apply request on scenario…", output);
141
142 if (EDSP::ApplyRequest(install, remove, CacheFile) == false)
143 DIE("Failed to apply request to depcache!");
144
145 pkgProblemResolver Fix(CacheFile);
146 for (std::list<std::string>::const_iterator i = remove.begin();
147 i != remove.end(); ++i) {
148 pkgCache::PkgIterator P = CacheFile->FindPkg(*i);
149 Fix.Clear(P);
150 Fix.Protect(P);
151 Fix.Remove(P);
152 }
153
154 for (std::list<std::string>::const_iterator i = install.begin();
155 i != install.end(); ++i) {
156 pkgCache::PkgIterator P = CacheFile->FindPkg(*i);
157 Fix.Clear(P);
158 Fix.Protect(P);
159 }
160
161 for (std::list<std::string>::const_iterator i = install.begin();
162 i != install.end(); ++i)
163 CacheFile->MarkInstall(CacheFile->FindPkg(*i), true);
164
165 EDSP::WriteProgress(60, "Call problemresolver on current scenario…", output);
166
167 std::string failure;
168 if (upgrade == true) {
169 if (APT::Upgrade::Upgrade(CacheFile, APT::Upgrade::FORBID_REMOVE_PACKAGES | APT::Upgrade::FORBID_INSTALL_NEW_PACKAGES) == false)
170 failure = "ERR_UNSOLVABLE_UPGRADE";
171 } else if (distUpgrade == true) {
172 if (APT::Upgrade::Upgrade(CacheFile, APT::Upgrade::ALLOW_EVERYTHING) == false)
173 failure = "ERR_UNSOLVABLE_DIST_UPGRADE";
174 } else if (Fix.Resolve() == false)
175 failure = "ERR_UNSOLVABLE";
176
177 if (failure.empty() == false) {
178 std::ostringstream broken;
179 ShowBroken(broken, CacheFile, false);
180 EDSP::WriteError(failure.c_str(), broken.str(), output);
181 return 0;
182 }
183
184 EDSP::WriteProgress(95, "Write solution…", output);
185
186 if (EDSP::WriteSolution(CacheFile, output) == false)
187 DIE("Failed to output the solution!");
188
189 EDSP::WriteProgress(100, "Done", output);
190
191 bool const Errors = _error->PendingError();
192 if (_config->FindI("quiet",0) > 0)
193 _error->DumpErrors(std::cerr);
194 else
195 _error->DumpErrors(std::cerr, GlobalError::DEBUG);
196 return Errors == true ? 100 : 0;
197 }
198 /*}}}*/