add a small wrapper to use the internal apt solver as an external one
[ntk/apt.git] / apt-pkg / edsp.cc
CommitLineData
6d38011b
DK
1// -*- mode: cpp; mode: fold -*-
2// Description /*{{{*/
3/* ######################################################################
4 Set of methods to help writing and reading everything needed for EDSP
5 ##################################################################### */
6 /*}}}*/
7// Include Files /*{{{*/
c3b85126 8#include <apt-pkg/edsp.h>
6d38011b
DK
9#include <apt-pkg/error.h>
10#include <apt-pkg/configuration.h>
11#include <apt-pkg/version.h>
12#include <apt-pkg/policy.h>
13
14#include <apti18n.h>
15#include <limits>
16
17#include <stdio.h>
18 /*}}}*/
19
c3b85126
DK
20// EDSP::WriteScenario - to the given file descriptor /*{{{*/
21bool EDSP::WriteScenario(pkgDepCache &Cache, FILE* output)
6d38011b
DK
22{
23 // we could use pkgCache::DepType and ::Priority, but these would be lokalized stringsā€¦
24 const char * const PrioMap[] = {0, "important", "required", "standard",
25 "optional", "extra"};
26 const char * const DepMap[] = {"", "Depends", "PreDepends", "Suggests",
27 "Recommends" , "Conflicts", "Replaces",
28 "Obsoletes", "Breaks", "Enhances"};
29
30 for (pkgCache::PkgIterator Pkg = Cache.PkgBegin(); Pkg.end() == false; ++Pkg)
31 {
32 for (pkgCache::VerIterator Ver = Pkg.VersionList(); Ver.end() == false; ++Ver)
33 {
34 fprintf(output, "Package: %s\n", Pkg.Name());
35 fprintf(output, "Architecture: %s\n", Ver.Arch());
36 fprintf(output, "Version: %s\n", Ver.VerStr());
37 if (Pkg.CurrentVer() == Ver)
38 fprintf(output, "Installed: yes\n");
39 if (Pkg->SelectedState == pkgCache::State::Hold)
40 fprintf(output, "Hold: yes\n");
41 fprintf(output, "APT-ID: %u\n", Ver->ID);
42 fprintf(output, "Priority: %s\n", PrioMap[Ver->Priority]);
43 if ((Pkg->Flags & pkgCache::Flag::Essential) == pkgCache::Flag::Essential)
44 fprintf(output, "Essential: yes\n");
45 fprintf(output, "Section: %s\n", Ver.Section());
46 if (Ver->MultiArch == pkgCache::Version::Allowed || Ver->MultiArch == pkgCache::Version::AllAllowed)
47 fprintf(output, "Multi-Arch: allowed\n");
48 else if (Ver->MultiArch == pkgCache::Version::Foreign || Ver->MultiArch == pkgCache::Version::AllForeign)
49 fprintf(output, "Multi-Arch: foreign\n");
50 else if (Ver->MultiArch == pkgCache::Version::Same)
51 fprintf(output, "Multi-Arch: same\n");
52 signed short Pin = std::numeric_limits<signed short>::min();
53 for (pkgCache::VerFileIterator File = Ver.FileList(); File.end() == false; ++File) {
54 signed short const p = Cache.GetPolicy().GetPriority(File.File());
55 if (Pin < p)
56 Pin = p;
57 }
58 fprintf(output, "APT-Pin: %d\n", Pin);
59 if (Cache.GetCandidateVer(Pkg) == Ver)
60 fprintf(output, "APT-Candidate: yes\n");
61 if ((Cache[Pkg].Flags & pkgCache::Flag::Auto) == pkgCache::Flag::Auto)
62 fprintf(output, "APT-Automatic: yes\n");
63 std::string dependencies[pkgCache::Dep::Enhances + 1];
64 bool orGroup = false;
65 for (pkgCache::DepIterator Dep = Ver.DependsList(); Dep.end() == false; ++Dep)
66 {
67 // Ignore implicit dependencies for multiarch here
68 if (strcmp(Pkg.Arch(), Dep.TargetPkg().Arch()) != 0)
69 continue;
70 if (orGroup == false)
71 dependencies[Dep->Type].append(", ");
72 dependencies[Dep->Type].append(Dep.TargetPkg().Name());
73 if (Dep->Version != 0)
74 dependencies[Dep->Type].append(" (").append(pkgCache::CompTypeDeb(Dep->CompareOp)).append(" ").append(Dep.TargetVer()).append(")");
75 if ((Dep->CompareOp & pkgCache::Dep::Or) == pkgCache::Dep::Or)
76 {
77 dependencies[Dep->Type].append(" | ");
78 orGroup = true;
79 }
80 else
81 orGroup = false;
82 }
83 for (int i = 1; i < pkgCache::Dep::Enhances + 1; ++i)
84 if (dependencies[i].empty() == false)
85 fprintf(output, "%s: %s\n", DepMap[i], dependencies[i].c_str()+2);
86 string provides;
87 for (pkgCache::PrvIterator Prv = Ver.ProvidesList(); Prv.end() == false; ++Prv)
88 {
89 // Ignore implicit provides for multiarch here
90 if (strcmp(Pkg.Arch(), Prv.ParentPkg().Arch()) != 0 || strcmp(Pkg.Name(),Prv.Name()) == 0)
91 continue;
92 provides.append(", ").append(Prv.Name());
93 }
94 if (provides.empty() == false)
95 fprintf(output, "Provides: %s\n", provides.c_str()+2);
96
97
98 fprintf(output, "\n");
99 }
100 }
101 return true;
102}
103 /*}}}*/
c3b85126 104// EDSP::WriteRequest - to the given file descriptor /*{{{*/
93794bc9
DK
105bool EDSP::WriteRequest(pkgDepCache &Cache, FILE* output, bool const Upgrade,
106 bool const DistUpgrade, bool const AutoRemove)
6d38011b 107{
93794bc9 108 string del, inst;
6d38011b
DK
109 for (pkgCache::PkgIterator Pkg = Cache.PkgBegin(); Pkg.end() == false; ++Pkg)
110 {
111 string* req;
112 if (Cache[Pkg].Delete() == true)
113 req = &del;
93794bc9 114 else if (Cache[Pkg].NewInstall() == true || Cache[Pkg].Upgrade() == true)
6d38011b 115 req = &inst;
6d38011b
DK
116 else
117 continue;
6d5bd614 118 req->append(" ").append(Pkg.FullName());
6d38011b 119 }
93794bc9 120 fprintf(output, "Request: EDSP 0.2\n");
6d38011b 121 if (del.empty() == false)
6d5bd614 122 fprintf(output, "Remove: %s\n", del.c_str()+1);
6d38011b 123 if (inst.empty() == false)
6d5bd614 124 fprintf(output, "Install: %s\n", inst.c_str()+1);
93794bc9
DK
125 if (Upgrade == true)
126 fprintf(output, "Upgrade: yes\n");
127 if (DistUpgrade == true)
128 fprintf(output, "Dist-Upgrade: yes\n");
129 if (AutoRemove == true)
130 fprintf(output, "Autoremove: yes\n");
131 if (_config->FindB("APT::Solver::Strict-Pinning", true) == false)
132 fprintf(output, "Strict-Pinning: no\n");
133 string solverpref("APT::Solver::");
134 solverpref.append(_config->Find("APT::Solver::Name", "internal")).append("::Preferences");
6d5bd614 135 if (_config->Exists(solverpref) == true)
93794bc9 136 fprintf(output, "Preferences: %s\n", _config->Find(solverpref,"").c_str());
6d5bd614 137 fprintf(output, "\n");
6d38011b 138
e3674d91
DK
139 return true;
140}
141 /*}}}*/
c3b85126 142bool EDSP::ReadResponse(FILE* input, pkgDepCache &Cache) { return false; }
29099cb6 143
6d5bd614
DK
144// EDSP::ReadLine - first line from the given file descriptor /*{{{*/
145// ---------------------------------------------------------------------
146/* Little helper method to read a complete line into a string. Similar to
147 fgets but we need to use the low-level read() here as otherwise the
148 listparser will be confused later on as mixing of fgets and read isn't
149 a supported action according to the manpages and result are undefined */
150bool EDSP::ReadLine(int const input, std::string &line) {
151 char one;
152 ssize_t data = 0;
153 line.erase();
154 line.reserve(100);
155 while ((data = read(input, &one, sizeof(one))) != -1) {
156 if (data != 1)
157 continue;
158 if (one == '\n')
159 return true;
160 if (one == '\r')
161 continue;
162 if (line.empty() == true && isblank(one) != 0)
163 continue;
164 line += one;
165 }
166 return false;
167}
168 /*}}}*/
40795fca
DK
169// EDSP::StringToBool - convert yes/no to bool /*{{{*/
170// ---------------------------------------------------------------------
171/* we are not as lazy as we are in the global StringToBool as we really
172 only accept yes/no here - but we will ignore leading spaces */
173bool EDSP::StringToBool(char const *answer, bool const defValue) {
174 for (; isspace(*answer) != 0; ++answer);
175 if (strncasecmp(answer, "yes", 3) == 0)
176 return true;
177 else if (strncasecmp(answer, "no", 2) == 0)
178 return false;
179 else
180 _error->Warning("Value '%s' is not a boolean 'yes' or 'no'!", answer);
181 return defValue;
182}
183 /*}}}*/
6d5bd614
DK
184// EDSP::ReadRequest - first stanza from the given file descriptor /*{{{*/
185bool EDSP::ReadRequest(int const input, std::list<std::string> &install,
40795fca
DK
186 std::list<std::string> &remove, bool &upgrade,
187 bool &distUpgrade, bool &autoRemove)
6d5bd614 188{
40795fca
DK
189 install.clear();
190 remove.clear();
191 upgrade = false;
192 distUpgrade = false;
193 autoRemove = false;
6d5bd614
DK
194 std::string line;
195 while (ReadLine(input, line) == true)
196 {
197 // Skip empty lines before request
198 if (line.empty() == true)
199 continue;
200 // The first Tag must be a request, so search for it
40795fca 201 if (line.compare(0, 8, "Request:") != 0)
6d5bd614
DK
202 continue;
203
204 while (ReadLine(input, line) == true)
205 {
206 // empty lines are the end of the request
207 if (line.empty() == true)
208 return true;
209
210 std::list<std::string> *request = NULL;
40795fca 211 if (line.compare(0, 8, "Install:") == 0)
6d5bd614 212 {
40795fca 213 line.erase(0, 8);
6d5bd614
DK
214 request = &install;
215 }
40795fca 216 else if (line.compare(0, 7, "Remove:") == 0)
6d5bd614 217 {
40795fca 218 line.erase(0, 7);
6d5bd614
DK
219 request = &remove;
220 }
40795fca
DK
221 else if (line.compare(0, 8, "Upgrade:") == 0)
222 upgrade = EDSP::StringToBool(line.c_str() + 9, false);
223 else if (line.compare(0, 13, "Dist-Upgrade:") == 0)
224 distUpgrade = EDSP::StringToBool(line.c_str() + 14, false);
225 else if (line.compare(0, 11, "Autoremove:") == 0)
226 autoRemove = EDSP::StringToBool(line.c_str() + 12, false);
227 else
228 _error->Warning("Unknown line in EDSP Request stanza: %s", line.c_str());
229
6d5bd614
DK
230 if (request == NULL)
231 continue;
232 size_t end = line.length();
233 do {
234 size_t begin = line.rfind(' ');
235 if (begin == std::string::npos)
236 {
40795fca 237 request->push_back(line.substr(0, end));
6d5bd614
DK
238 break;
239 }
240 else if (begin < end)
241 request->push_back(line.substr(begin + 1, end));
242 line.erase(begin);
243 end = line.find_last_not_of(' ');
244 } while (end != std::string::npos);
245 }
246 }
247 return false;
248}
249 /*}}}*/
250// EDSP::ApplyRequest - first stanza from the given file descriptor /*{{{*/
c3b85126 251bool EDSP::ApplyRequest(std::list<std::string> const &install,
29099cb6
DK
252 std::list<std::string> const &remove,
253 pkgDepCache &Cache)
6d5bd614
DK
254{
255 for (std::list<std::string>::const_iterator i = install.begin();
256 i != install.end(); ++i)
257 Cache.MarkInstall(Cache.FindPkg(*i), false);
258
259 for (std::list<std::string>::const_iterator i = remove.begin();
260 i != remove.end(); ++i)
261 Cache.MarkDelete(Cache.FindPkg(*i));
262 return true;
263}
264 /*}}}*/
c3b85126
DK
265// EDSP::WriteSolution - to the given file descriptor /*{{{*/
266bool EDSP::WriteSolution(pkgDepCache &Cache, FILE* output)
e3674d91 267{
6d5bd614 268 bool const Debug = _config->FindB("Debug::EDSP::WriteSolution", false);
e3674d91
DK
269 for (pkgCache::PkgIterator Pkg = Cache.PkgBegin(); Pkg.end() == false; ++Pkg)
270 {
271 if (Cache[Pkg].Delete() == true)
272 fprintf(output, "Remove: %d\n", Cache.GetCandidateVer(Pkg)->ID);
273 else if (Cache[Pkg].NewInstall() == true || Cache[Pkg].Upgrade() == true)
274 fprintf(output, "Install: %d\n", Cache.GetCandidateVer(Pkg)->ID);
275 else
276 continue;
277 if (Debug == true)
278 fprintf(output, "Package: %s\nVersion: %s\n", Pkg.FullName().c_str(), Cache.GetCandidateVer(Pkg).VerStr());
279 fprintf(output, "\n");
280 }
281
6d38011b
DK
282 return true;
283}
284 /*}}}*/
c3b85126 285bool EDSP::WriteError(std::string const &message, FILE* output) { return false; }