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