]> git.saurik.com Git - apt.git/blob - cmdline/apt-internal-solver.cc
remove duplicated 'the' from guides doc-base abstract
[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 #include <apt-private/private-main.h>
31
32 #include <string.h>
33 #include <iostream>
34 #include <sstream>
35 #include <list>
36 #include <string>
37 #include <unistd.h>
38 #include <cstdio>
39 #include <stdlib.h>
40
41 #include <apti18n.h>
42 /*}}}*/
43
44 static bool ShowHelp(CommandLine &) /*{{{*/
45 {
46 std::cout <<
47 _("Usage: apt-internal-solver\n"
48 "\n"
49 "apt-internal-solver is an interface to use the current internal\n"
50 "resolver for the APT family like an external one, for debugging or\n"
51 "the like.\n");
52 return true;
53 }
54 /*}}}*/
55 APT_NORETURN static void DIE(std::string const &message) { /*{{{*/
56 std::cerr << "ERROR: " << message << std::endl;
57 _error->DumpErrors(std::cerr);
58 exit(EXIT_FAILURE);
59 }
60 /*}}}*/
61 static std::vector<aptDispatchWithHelp> GetCommands() /*{{{*/
62 {
63 return {};
64 }
65 /*}}}*/
66 int main(int argc,const char *argv[]) /*{{{*/
67 {
68 InitLocale();
69
70 // we really don't need anything
71 DropPrivileges();
72
73 CommandLine CmdL;
74 ParseCommandLine(CmdL, APT_CMD::APT_INTERNAL_SOLVER, &_config, NULL, argc, argv, &ShowHelp, &GetCommands);
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 FileFd output;
86 if (output.OpenDescriptor(STDOUT_FILENO, FileFd::WriteOnly | FileFd::BufferedWrite, true) == false)
87 return 2;
88 if (pkgset.empty() == true)
89 EDSP::WriteScenario(CacheFile, output);
90 else
91 {
92 std::vector<bool> pkgvec(CacheFile->Head().PackageCount, false);
93 for (auto const &p: pkgset)
94 pkgvec[p->ID] = true;
95 EDSP::WriteLimitedScenario(CacheFile, output, pkgvec);
96 }
97 output.Close();
98 _error->DumpErrors(std::cerr);
99 return 0;
100 }
101
102 // Deal with stdout not being a tty
103 if (!isatty(STDOUT_FILENO) && _config->FindI("quiet", -1) == -1)
104 _config->Set("quiet","1");
105
106 if (_config->FindI("quiet", 0) < 1)
107 _config->Set("Debug::EDSP::WriteSolution", true);
108
109 _config->Set("APT::System", "Debian APT solver interface");
110 _config->Set("APT::Solver", "internal");
111 _config->Set("edsp::scenario", "/nonexistent/stdin");
112 FileFd output;
113 if (output.OpenDescriptor(STDOUT_FILENO, FileFd::WriteOnly | FileFd::BufferedWrite, true) == false)
114 DIE("stdout couldn't be opened");
115 int const input = STDIN_FILENO;
116 SetNonBlock(input, false);
117
118 EDSP::WriteProgress(0, "Start up solver…", output);
119
120 if (pkgInitSystem(*_config,_system) == false)
121 DIE("System could not be initialized!");
122
123 EDSP::WriteProgress(1, "Read request…", output);
124
125 if (WaitFd(input, false, 5) == false)
126 DIE("WAIT timed out in the resolver");
127
128 std::list<std::string> install, remove;
129 unsigned int flags;
130 if (EDSP::ReadRequest(input, install, remove, flags) == false)
131 DIE("Parsing the request failed!");
132
133 EDSP::WriteProgress(5, "Read scenario…", output);
134
135 pkgCacheFile CacheFile;
136 if (CacheFile.Open(NULL, false) == false)
137 DIE("Failed to open CacheFile!");
138
139 EDSP::WriteProgress(50, "Apply request on scenario…", output);
140
141 if (EDSP::ApplyRequest(install, remove, CacheFile) == false)
142 DIE("Failed to apply request to depcache!");
143
144 pkgProblemResolver Fix(CacheFile);
145 for (std::list<std::string>::const_iterator i = remove.begin();
146 i != remove.end(); ++i) {
147 pkgCache::PkgIterator P = CacheFile->FindPkg(*i);
148 Fix.Clear(P);
149 Fix.Protect(P);
150 Fix.Remove(P);
151 }
152
153 for (std::list<std::string>::const_iterator i = install.begin();
154 i != install.end(); ++i) {
155 pkgCache::PkgIterator P = CacheFile->FindPkg(*i);
156 Fix.Clear(P);
157 Fix.Protect(P);
158 }
159
160 for (std::list<std::string>::const_iterator i = install.begin();
161 i != install.end(); ++i)
162 CacheFile->MarkInstall(CacheFile->FindPkg(*i), true);
163
164 EDSP::WriteProgress(60, "Call problemresolver on current scenario…", output);
165
166 std::string failure;
167 if (flags & EDSP::Request::UPGRADE_ALL) {
168 int upgrade_flags = APT::Upgrade::ALLOW_EVERYTHING;
169 if (flags & EDSP::Request::FORBID_NEW_INSTALL)
170 upgrade_flags |= APT::Upgrade::FORBID_INSTALL_NEW_PACKAGES;
171 if (flags & EDSP::Request::FORBID_REMOVE)
172 upgrade_flags |= APT::Upgrade::FORBID_REMOVE_PACKAGES;
173
174 if (APT::Upgrade::Upgrade(CacheFile, upgrade_flags))
175 ;
176 else if (upgrade_flags == APT::Upgrade::ALLOW_EVERYTHING)
177 failure = "ERR_UNSOLVABLE_FULL_UPGRADE";
178 else
179 failure = "ERR_UNSOLVABLE_UPGRADE";
180 } else if (Fix.Resolve() == false)
181 failure = "ERR_UNSOLVABLE";
182
183 if (failure.empty() == false) {
184 std::ostringstream broken;
185 ShowBroken(broken, CacheFile, false);
186 EDSP::WriteError(failure.c_str(), broken.str(), output);
187 return 0;
188 }
189
190 EDSP::WriteProgress(95, "Write solution…", output);
191
192 if (EDSP::WriteSolution(CacheFile, output) == false)
193 DIE("Failed to output the solution!");
194
195 EDSP::WriteProgress(100, "Done", output);
196
197 return DispatchCommandLine(CmdL, {});
198 }
199 /*}}}*/