]> git.saurik.com Git - apt.git/blob - cmdline/cacheset.cc
reorder classes a bit and make TryToInstallBuildDep use them
[apt.git] / cmdline / cacheset.cc
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 /* ######################################################################
4
5 Simple wrapper around a std::set to provide a similar interface to
6 a set of cache structures as to the complete set of all structures
7 in the pkgCache. Currently only Package is supported.
8
9 ##################################################################### */
10 /*}}}*/
11 // Include Files /*{{{*/
12 #include <apt-pkg/aptconfiguration.h>
13 #include <apt-pkg/error.h>
14 #include <apt-pkg/strutl.h>
15 #include <apt-pkg/versionmatch.h>
16
17 #include <apti18n.h>
18
19 #include "cacheset.h"
20
21 #include <vector>
22
23 #include <regex.h>
24 /*}}}*/
25 namespace APT {
26 // FromTask - Return all packages in the cache from a specific task /*{{{*/
27 PackageSet PackageSet::FromTask(pkgCacheFile &Cache, std::string pattern, CacheSetHelper &helper) {
28 size_t const archfound = pattern.find_last_of(':');
29 std::string arch = "native";
30 if (archfound != std::string::npos) {
31 arch = pattern.substr(archfound+1);
32 pattern.erase(archfound);
33 }
34
35 if (pattern[pattern.length() -1] != '^')
36 return APT::PackageSet(TASK);
37 pattern.erase(pattern.length()-1);
38
39 if (unlikely(Cache.GetPkgCache() == 0 || Cache.GetDepCache() == 0))
40 return APT::PackageSet(TASK);
41
42 PackageSet pkgset(TASK);
43 // get the records
44 pkgRecords Recs(Cache);
45
46 // build regexp for the task
47 regex_t Pattern;
48 char S[300];
49 snprintf(S, sizeof(S), "^Task:.*[, ]%s([, ]|$)", pattern.c_str());
50 if(regcomp(&Pattern,S, REG_EXTENDED | REG_NOSUB | REG_NEWLINE) != 0) {
51 _error->Error("Failed to compile task regexp");
52 return pkgset;
53 }
54
55 for (pkgCache::GrpIterator Grp = Cache->GrpBegin(); Grp.end() == false; ++Grp) {
56 pkgCache::PkgIterator Pkg = Grp.FindPkg(arch);
57 if (Pkg.end() == true)
58 continue;
59 pkgCache::VerIterator ver = Cache[Pkg].CandidateVerIter(Cache);
60 if(ver.end() == true)
61 continue;
62
63 pkgRecords::Parser &parser = Recs.Lookup(ver.FileList());
64 const char *start, *end;
65 parser.GetRec(start,end);
66 unsigned int const length = end - start;
67 char buf[length];
68 strncpy(buf, start, length);
69 buf[length-1] = '\0';
70 if (regexec(&Pattern, buf, 0, 0, 0) != 0)
71 continue;
72
73 pkgset.insert(Pkg);
74 }
75 regfree(&Pattern);
76
77 if (pkgset.empty() == true)
78 return helper.canNotFindTask(Cache, pattern);
79
80 helper.showTaskSelection(pkgset, pattern);
81 return pkgset;
82 }
83 /*}}}*/
84 // FromRegEx - Return all packages in the cache matching a pattern /*{{{*/
85 PackageSet PackageSet::FromRegEx(pkgCacheFile &Cache, std::string pattern, CacheSetHelper &helper) {
86 static const char * const isregex = ".?+*|[^$";
87 if (pattern.find_first_of(isregex) == std::string::npos)
88 return PackageSet(REGEX);
89
90 size_t archfound = pattern.find_last_of(':');
91 std::string arch = "native";
92 if (archfound != std::string::npos) {
93 arch = pattern.substr(archfound+1);
94 if (arch.find_first_of(isregex) == std::string::npos)
95 pattern.erase(archfound);
96 else
97 arch = "native";
98 }
99
100 regex_t Pattern;
101 int Res;
102 if ((Res = regcomp(&Pattern, pattern.c_str() , REG_EXTENDED | REG_ICASE | REG_NOSUB)) != 0) {
103 char Error[300];
104 regerror(Res, &Pattern, Error, sizeof(Error));
105 _error->Error(_("Regex compilation error - %s"), Error);
106 return PackageSet(REGEX);
107 }
108
109 if (unlikely(Cache.GetPkgCache() == 0))
110 return PackageSet(REGEX);
111
112 PackageSet pkgset(REGEX);
113 for (pkgCache::GrpIterator Grp = Cache.GetPkgCache()->GrpBegin(); Grp.end() == false; ++Grp)
114 {
115 if (regexec(&Pattern, Grp.Name(), 0, 0, 0) != 0)
116 continue;
117 pkgCache::PkgIterator Pkg = Grp.FindPkg(arch);
118 if (Pkg.end() == true) {
119 if (archfound == std::string::npos) {
120 std::vector<std::string> archs = APT::Configuration::getArchitectures();
121 for (std::vector<std::string>::const_iterator a = archs.begin();
122 a != archs.end() && Pkg.end() != true; ++a)
123 Pkg = Grp.FindPkg(*a);
124 }
125 if (Pkg.end() == true)
126 continue;
127 }
128
129 pkgset.insert(Pkg);
130 }
131 regfree(&Pattern);
132
133 if (pkgset.empty() == true)
134 return helper.canNotFindRegEx(Cache, pattern);
135
136 helper.showRegExSelection(pkgset, pattern);
137 return pkgset;
138 }
139 /*}}}*/
140 // FromName - Returns the package defined by this string /*{{{*/
141 pkgCache::PkgIterator PackageSet::FromName(pkgCacheFile &Cache,
142 std::string const &str, CacheSetHelper &helper) {
143 std::string pkg = str;
144 size_t archfound = pkg.find_last_of(':');
145 std::string arch;
146 if (archfound != std::string::npos) {
147 arch = pkg.substr(archfound+1);
148 pkg.erase(archfound);
149 }
150
151 if (Cache.GetPkgCache() == 0)
152 return pkgCache::PkgIterator(Cache, 0);
153
154 pkgCache::PkgIterator Pkg(Cache, 0);
155 if (arch.empty() == true) {
156 pkgCache::GrpIterator Grp = Cache.GetPkgCache()->FindGrp(pkg);
157 if (Grp.end() == false)
158 Pkg = Grp.FindPreferredPkg();
159 } else
160 Pkg = Cache.GetPkgCache()->FindPkg(pkg, arch);
161
162 if (Pkg.end() == true)
163 return helper.canNotFindPkgName(Cache, str);
164 return Pkg;
165 }
166 /*}}}*/
167 // GroupedFromCommandLine - Return all versions specified on commandline/*{{{*/
168 std::map<unsigned short, PackageSet> PackageSet::GroupedFromCommandLine(
169 pkgCacheFile &Cache, const char **cmdline,
170 std::list<PackageSet::Modifier> const &mods,
171 unsigned short const &fallback, CacheSetHelper &helper) {
172 std::map<unsigned short, PackageSet> pkgsets;
173 for (const char **I = cmdline; *I != 0; ++I) {
174 unsigned short modID = fallback;
175 std::string str = *I;
176 bool modifierPresent = false;
177 for (std::list<PackageSet::Modifier>::const_iterator mod = mods.begin();
178 mod != mods.end(); ++mod) {
179 size_t const alength = strlen(mod->Alias);
180 switch(mod->Pos) {
181 case PackageSet::Modifier::POSTFIX:
182 if (str.compare(str.length() - alength, alength,
183 mod->Alias, 0, alength) != 0)
184 continue;
185 str.erase(str.length() - alength);
186 modID = mod->ID;
187 break;
188 case PackageSet::Modifier::PREFIX:
189 continue;
190 case PackageSet::Modifier::NONE:
191 continue;
192 }
193 modifierPresent = true;
194 break;
195 }
196 if (modifierPresent == true) {
197 bool const errors = helper.showErrors(false);
198 pkgCache::PkgIterator Pkg = FromName(Cache, *I, helper);
199 helper.showErrors(errors);
200 if (Pkg.end() == false) {
201 pkgsets[fallback].insert(Pkg);
202 continue;
203 }
204 }
205 pkgsets[modID].insert(PackageSet::FromString(Cache, str, helper));
206 }
207 return pkgsets;
208 }
209 /*}}}*/
210 // FromCommandLine - Return all packages specified on commandline /*{{{*/
211 PackageSet PackageSet::FromCommandLine(pkgCacheFile &Cache, const char **cmdline, CacheSetHelper &helper) {
212 PackageSet pkgset;
213 for (const char **I = cmdline; *I != 0; ++I) {
214 PackageSet pset = FromString(Cache, *I, helper);
215 pkgset.insert(pset.begin(), pset.end());
216 }
217 return pkgset;
218 }
219 /*}}}*/
220 // FromString - Return all packages matching a specific string /*{{{*/
221 PackageSet PackageSet::FromString(pkgCacheFile &Cache, std::string const &str, CacheSetHelper &helper) {
222 _error->PushToStack();
223
224 PackageSet pkgset;
225 pkgCache::PkgIterator Pkg = FromName(Cache, str, helper);
226 if (Pkg.end() == false)
227 pkgset.insert(Pkg);
228 else {
229 pkgset = FromTask(Cache, str, helper);
230 if (pkgset.empty() == true) {
231 pkgset = FromRegEx(Cache, str, helper);
232 if (pkgset.empty() == true)
233 pkgset = helper.canNotFindPackage(Cache, str);
234 }
235 }
236
237 if (pkgset.empty() == false)
238 _error->RevertToStack();
239 else
240 _error->MergeWithStack();
241 return pkgset;
242 }
243 /*}}}*/
244 // GroupedFromCommandLine - Return all versions specified on commandline/*{{{*/
245 std::map<unsigned short, VersionSet> VersionSet::GroupedFromCommandLine(
246 pkgCacheFile &Cache, const char **cmdline,
247 std::list<VersionSet::Modifier> const &mods,
248 unsigned short const &fallback, CacheSetHelper &helper) {
249 std::map<unsigned short, VersionSet> versets;
250 for (const char **I = cmdline; *I != 0; ++I) {
251 unsigned short modID = fallback;
252 VersionSet::Version select = VersionSet::NEWEST;
253 std::string str = *I;
254 bool modifierPresent = false;
255 for (std::list<VersionSet::Modifier>::const_iterator mod = mods.begin();
256 mod != mods.end(); ++mod) {
257 if (modID == fallback && mod->ID == fallback)
258 select = mod->SelectVersion;
259 size_t const alength = strlen(mod->Alias);
260 switch(mod->Pos) {
261 case VersionSet::Modifier::POSTFIX:
262 if (str.compare(str.length() - alength, alength,
263 mod->Alias, 0, alength) != 0)
264 continue;
265 str.erase(str.length() - alength);
266 modID = mod->ID;
267 select = mod->SelectVersion;
268 break;
269 case VersionSet::Modifier::PREFIX:
270 continue;
271 case VersionSet::Modifier::NONE:
272 continue;
273 }
274 modifierPresent = true;
275 break;
276 }
277
278 if (modifierPresent == true) {
279 bool const errors = helper.showErrors(false);
280 VersionSet const vset = VersionSet::FromString(Cache, std::string(*I), select, helper, true);
281 helper.showErrors(errors);
282 if (vset.empty() == false) {
283 versets[fallback].insert(vset);
284 continue;
285 }
286 }
287 versets[modID].insert(VersionSet::FromString(Cache, str, select , helper));
288 }
289 return versets;
290 }
291 /*}}}*/
292 // FromCommandLine - Return all versions specified on commandline /*{{{*/
293 APT::VersionSet VersionSet::FromCommandLine(pkgCacheFile &Cache, const char **cmdline,
294 APT::VersionSet::Version const &fallback, CacheSetHelper &helper) {
295 VersionSet verset;
296 for (const char **I = cmdline; *I != 0; ++I)
297 verset.insert(VersionSet::FromString(Cache, *I, fallback, helper));
298 return verset;
299 }
300 /*}}}*/
301 // FromString - Returns all versions spedcified by a string /*{{{*/
302 APT::VersionSet VersionSet::FromString(pkgCacheFile &Cache, std::string pkg,
303 APT::VersionSet::Version const &fallback, CacheSetHelper &helper,
304 bool const &onlyFromName) {
305 std::string ver;
306 bool verIsRel = false;
307 size_t const vertag = pkg.find_last_of("/=");
308 if (vertag != string::npos) {
309 ver = pkg.substr(vertag+1);
310 verIsRel = (pkg[vertag] == '/');
311 pkg.erase(vertag);
312 }
313 PackageSet pkgset;
314 if (onlyFromName == false)
315 pkgset = PackageSet::FromString(Cache, pkg, helper);
316 else {
317 pkgset.insert(PackageSet::FromName(Cache, pkg, helper));
318 }
319
320 VersionSet verset;
321 bool errors = true;
322 if (pkgset.getConstructor() != PackageSet::UNKNOWN)
323 errors = helper.showErrors(false);
324 for (PackageSet::const_iterator P = pkgset.begin();
325 P != pkgset.end(); ++P) {
326 if (vertag == string::npos) {
327 verset.insert(VersionSet::FromPackage(Cache, P, fallback, helper));
328 continue;
329 }
330 pkgCache::VerIterator V;
331 if (ver == "installed")
332 V = getInstalledVer(Cache, P, helper);
333 else if (ver == "candidate")
334 V = getCandidateVer(Cache, P, helper);
335 else {
336 pkgVersionMatch Match(ver, (verIsRel == true ? pkgVersionMatch::Release :
337 pkgVersionMatch::Version));
338 V = Match.Find(P);
339 if (V.end() == true) {
340 if (verIsRel == true)
341 _error->Error(_("Release '%s' for '%s' was not found"),
342 ver.c_str(), P.FullName(true).c_str());
343 else
344 _error->Error(_("Version '%s' for '%s' was not found"),
345 ver.c_str(), P.FullName(true).c_str());
346 continue;
347 }
348 }
349 if (V.end() == true)
350 continue;
351 helper.showSelectedVersion(P, V, ver, verIsRel);
352 verset.insert(V);
353 }
354 if (pkgset.getConstructor() != PackageSet::UNKNOWN)
355 helper.showErrors(errors);
356 return verset;
357 }
358 /*}}}*/
359 // FromPackage - versions from package based on fallback /*{{{*/
360 VersionSet VersionSet::FromPackage(pkgCacheFile &Cache, pkgCache::PkgIterator const &P,
361 VersionSet::Version const &fallback, CacheSetHelper &helper) {
362 VersionSet verset;
363 pkgCache::VerIterator V;
364 bool showErrors;
365 switch(fallback) {
366 case VersionSet::ALL:
367 if (P->VersionList != 0)
368 for (V = P.VersionList(); V.end() != true; ++V)
369 verset.insert(V);
370 else
371 verset.insert(helper.canNotFindAllVer(Cache, P));
372 break;
373 case VersionSet::CANDANDINST:
374 verset.insert(getInstalledVer(Cache, P, helper));
375 verset.insert(getCandidateVer(Cache, P, helper));
376 break;
377 case VersionSet::CANDIDATE:
378 verset.insert(getCandidateVer(Cache, P, helper));
379 break;
380 case VersionSet::INSTALLED:
381 verset.insert(getInstalledVer(Cache, P, helper));
382 break;
383 case VersionSet::CANDINST:
384 showErrors = helper.showErrors(false);
385 V = getCandidateVer(Cache, P, helper);
386 if (V.end() == true)
387 V = getInstalledVer(Cache, P, helper);
388 helper.showErrors(showErrors);
389 if (V.end() == false)
390 verset.insert(V);
391 else
392 verset.insert(helper.canNotFindInstCandVer(Cache, P));
393 break;
394 case VersionSet::INSTCAND:
395 showErrors = helper.showErrors(false);
396 V = getInstalledVer(Cache, P, helper);
397 if (V.end() == true)
398 V = getCandidateVer(Cache, P, helper);
399 helper.showErrors(showErrors);
400 if (V.end() == false)
401 verset.insert(V);
402 else
403 verset.insert(helper.canNotFindInstCandVer(Cache, P));
404 break;
405 case VersionSet::NEWEST:
406 if (P->VersionList != 0)
407 verset.insert(P.VersionList());
408 else
409 verset.insert(helper.canNotFindNewestVer(Cache, P));
410 break;
411 }
412 return verset;
413 }
414 /*}}}*/
415 // getCandidateVer - Returns the candidate version of the given package /*{{{*/
416 pkgCache::VerIterator VersionSet::getCandidateVer(pkgCacheFile &Cache,
417 pkgCache::PkgIterator const &Pkg, CacheSetHelper &helper) {
418 pkgCache::VerIterator Cand;
419 if (Cache.IsDepCacheBuilt() == true)
420 Cand = Cache[Pkg].CandidateVerIter(Cache);
421 else {
422 if (unlikely(Cache.GetPolicy() == 0))
423 return pkgCache::VerIterator(Cache);
424 Cand = Cache.GetPolicy()->GetCandidateVer(Pkg);
425 }
426 if (Cand.end() == true)
427 return helper.canNotFindCandidateVer(Cache, Pkg);
428 return Cand;
429 }
430 /*}}}*/
431 // getInstalledVer - Returns the installed version of the given package /*{{{*/
432 pkgCache::VerIterator VersionSet::getInstalledVer(pkgCacheFile &Cache,
433 pkgCache::PkgIterator const &Pkg, CacheSetHelper &helper) {
434 if (Pkg->CurrentVer == 0)
435 return helper.canNotFindInstalledVer(Cache, Pkg);
436 return Pkg.CurrentVer();
437 }
438 /*}}}*/
439 // canNotFindPkgName - handle the case no package has this name /*{{{*/
440 pkgCache::PkgIterator CacheSetHelper::canNotFindPkgName(pkgCacheFile &Cache,
441 std::string const &str) {
442 if (ShowError == true)
443 _error->Error(_("Unable to locate package %s"), str.c_str());
444 return pkgCache::PkgIterator(Cache, 0);
445 }
446 /*}}}*/
447 // canNotFindTask - handle the case no package is found for a task /*{{{*/
448 PackageSet CacheSetHelper::canNotFindTask(pkgCacheFile &Cache, std::string pattern) {
449 if (ShowError == true)
450 _error->Error(_("Couldn't find task '%s'"), pattern.c_str());
451 return PackageSet();
452 }
453 /*}}}*/
454 // canNotFindRegEx - handle the case no package is found by a regex /*{{{*/
455 PackageSet CacheSetHelper::canNotFindRegEx(pkgCacheFile &Cache, std::string pattern) {
456 if (ShowError == true)
457 _error->Error(_("Couldn't find any package by regex '%s'"), pattern.c_str());
458 return PackageSet();
459 }
460 /*}}}*/
461 // canNotFindPackage - handle the case no package is found from a string/*{{{*/
462 PackageSet CacheSetHelper::canNotFindPackage(pkgCacheFile &Cache, std::string const &str) {
463 return PackageSet();
464 }
465 /*}}}*/
466 // canNotFindAllVer /*{{{*/
467 VersionSet CacheSetHelper::canNotFindAllVer(pkgCacheFile &Cache,
468 pkgCache::PkgIterator const &Pkg) {
469 if (ShowError == true)
470 _error->Error(_("Can't select versions from package '%s' as it purely virtual"), Pkg.FullName(true).c_str());
471 return VersionSet();
472 }
473 /*}}}*/
474 // canNotFindInstCandVer /*{{{*/
475 VersionSet CacheSetHelper::canNotFindInstCandVer(pkgCacheFile &Cache,
476 pkgCache::PkgIterator const &Pkg) {
477 if (ShowError == true)
478 _error->Error(_("Can't select installed nor candidate version from package '%s' as it has neither of them"), Pkg.FullName(true).c_str());
479 return VersionSet();
480 }
481 /*}}}*/
482 // canNotFindInstCandVer /*{{{*/
483 VersionSet CacheSetHelper::canNotFindCandInstVer(pkgCacheFile &Cache,
484 pkgCache::PkgIterator const &Pkg) {
485 if (ShowError == true)
486 _error->Error(_("Can't select installed nor candidate version from package '%s' as it has neither of them"), Pkg.FullName(true).c_str());
487 return VersionSet();
488 }
489 /*}}}*/
490 // canNotFindNewestVer /*{{{*/
491 pkgCache::VerIterator CacheSetHelper::canNotFindNewestVer(pkgCacheFile &Cache,
492 pkgCache::PkgIterator const &Pkg) {
493 if (ShowError == true)
494 _error->Error(_("Can't select newest version from package '%s' as it is purely virtual"), Pkg.FullName(true).c_str());
495 return pkgCache::VerIterator(Cache, 0);
496 }
497 /*}}}*/
498 // canNotFindCandidateVer /*{{{*/
499 pkgCache::VerIterator CacheSetHelper::canNotFindCandidateVer(pkgCacheFile &Cache,
500 pkgCache::PkgIterator const &Pkg) {
501 if (ShowError == true)
502 _error->Error(_("Can't select candidate version from package %s as it has no candidate"), Pkg.FullName(true).c_str());
503 return pkgCache::VerIterator(Cache, 0);
504 }
505 /*}}}*/
506 // canNotFindInstalledVer /*{{{*/
507 pkgCache::VerIterator CacheSetHelper::canNotFindInstalledVer(pkgCacheFile &Cache,
508 pkgCache::PkgIterator const &Pkg) {
509 if (ShowError == true)
510 _error->Error(_("Can't select installed version from package %s as it is not installed"), Pkg.FullName(true).c_str());
511 return pkgCache::VerIterator(Cache, 0);
512 }
513 /*}}}*/
514 }