Interpret Remove and Install lines in Responses correctly
[ntk/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: %lu\n", Ver.Index());
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 size_t const index = section.FindULL(type.c_str(), 0);
160 if (index == 0) {
161 _error->Warning("Unable to parse %s request with id value '%s'!", type.c_str(), section.FindS(type.c_str()).c_str());
162 continue;
163 }
164
165 pkgCache::VerIterator Ver(Cache.GetCache(), Cache.GetCache().VerP + index);
166 Cache.SetCandidateVersion(Ver);
167 if (type == "Install")
168 Cache.MarkInstall(Ver.ParentPkg(), false, false);
169 else if (type == "Remove")
170 Cache.MarkDelete(Ver.ParentPkg(), false);
171 }
172 return true;
173 }
174 /*}}}*/
175 // EDSP::ReadLine - first line from the given file descriptor /*{{{*/
176 // ---------------------------------------------------------------------
177 /* Little helper method to read a complete line into a string. Similar to
178 fgets but we need to use the low-level read() here as otherwise the
179 listparser will be confused later on as mixing of fgets and read isn't
180 a supported action according to the manpages and results are undefined */
181 bool EDSP::ReadLine(int const input, std::string &line) {
182 char one;
183 ssize_t data = 0;
184 line.erase();
185 line.reserve(100);
186 while ((data = read(input, &one, sizeof(one))) != -1) {
187 if (data != 1)
188 continue;
189 if (one == '\n')
190 return true;
191 if (one == '\r')
192 continue;
193 if (line.empty() == true && isblank(one) != 0)
194 continue;
195 line += one;
196 }
197 return false;
198 }
199 /*}}}*/
200 // EDSP::StringToBool - convert yes/no to bool /*{{{*/
201 // ---------------------------------------------------------------------
202 /* we are not as lazy as we are in the global StringToBool as we really
203 only accept yes/no here - but we will ignore leading spaces */
204 bool EDSP::StringToBool(char const *answer, bool const defValue) {
205 for (; isspace(*answer) != 0; ++answer);
206 if (strncasecmp(answer, "yes", 3) == 0)
207 return true;
208 else if (strncasecmp(answer, "no", 2) == 0)
209 return false;
210 else
211 _error->Warning("Value '%s' is not a boolean 'yes' or 'no'!", answer);
212 return defValue;
213 }
214 /*}}}*/
215 // EDSP::ReadRequest - first stanza from the given file descriptor /*{{{*/
216 bool EDSP::ReadRequest(int const input, std::list<std::string> &install,
217 std::list<std::string> &remove, bool &upgrade,
218 bool &distUpgrade, bool &autoRemove)
219 {
220 install.clear();
221 remove.clear();
222 upgrade = false;
223 distUpgrade = false;
224 autoRemove = false;
225 std::string line;
226 while (ReadLine(input, line) == true)
227 {
228 // Skip empty lines before request
229 if (line.empty() == true)
230 continue;
231 // The first Tag must be a request, so search for it
232 if (line.compare(0, 8, "Request:") != 0)
233 continue;
234
235 while (ReadLine(input, line) == true)
236 {
237 // empty lines are the end of the request
238 if (line.empty() == true)
239 return true;
240
241 std::list<std::string> *request = NULL;
242 if (line.compare(0, 8, "Install:") == 0)
243 {
244 line.erase(0, 8);
245 request = &install;
246 }
247 else if (line.compare(0, 7, "Remove:") == 0)
248 {
249 line.erase(0, 7);
250 request = &remove;
251 }
252 else if (line.compare(0, 8, "Upgrade:") == 0)
253 upgrade = EDSP::StringToBool(line.c_str() + 9, false);
254 else if (line.compare(0, 13, "Dist-Upgrade:") == 0)
255 distUpgrade = EDSP::StringToBool(line.c_str() + 14, false);
256 else if (line.compare(0, 11, "Autoremove:") == 0)
257 autoRemove = EDSP::StringToBool(line.c_str() + 12, false);
258 else
259 _error->Warning("Unknown line in EDSP Request stanza: %s", line.c_str());
260
261 if (request == NULL)
262 continue;
263 size_t end = line.length();
264 do {
265 size_t begin = line.rfind(' ');
266 if (begin == std::string::npos)
267 {
268 request->push_back(line.substr(0, end));
269 break;
270 }
271 else if (begin < end)
272 request->push_back(line.substr(begin + 1, end));
273 line.erase(begin);
274 end = line.find_last_not_of(' ');
275 } while (end != std::string::npos);
276 }
277 }
278 return false;
279 }
280 /*}}}*/
281 // EDSP::ApplyRequest - first stanza from the given file descriptor /*{{{*/
282 bool EDSP::ApplyRequest(std::list<std::string> const &install,
283 std::list<std::string> const &remove,
284 pkgDepCache &Cache)
285 {
286 for (std::list<std::string>::const_iterator i = install.begin();
287 i != install.end(); ++i)
288 Cache.MarkInstall(Cache.FindPkg(*i), false);
289
290 for (std::list<std::string>::const_iterator i = remove.begin();
291 i != remove.end(); ++i)
292 Cache.MarkDelete(Cache.FindPkg(*i));
293 return true;
294 }
295 /*}}}*/
296 // EDSP::WriteSolution - to the given file descriptor /*{{{*/
297 bool EDSP::WriteSolution(pkgDepCache &Cache, FILE* output)
298 {
299 bool const Debug = _config->FindB("Debug::EDSP::WriteSolution", false);
300 for (pkgCache::PkgIterator Pkg = Cache.PkgBegin(); Pkg.end() == false; ++Pkg)
301 {
302 if (Cache[Pkg].Delete() == true)
303 fprintf(output, "Remove: %d\n", Cache.GetCandidateVer(Pkg)->ID);
304 else if (Cache[Pkg].NewInstall() == true || Cache[Pkg].Upgrade() == true)
305 fprintf(output, "Install: %d\n", Cache.GetCandidateVer(Pkg)->ID);
306 else
307 continue;
308 if (Debug == true)
309 fprintf(output, "Package: %s\nVersion: %s\n", Pkg.FullName().c_str(), Cache.GetCandidateVer(Pkg).VerStr());
310 fprintf(output, "\n");
311 }
312
313 return true;
314 }
315 /*}}}*/
316 bool EDSP::WriteError(std::string const &message, FILE* output) { return false; }