]> git.saurik.com Git - apt.git/blame - apt-pkg/cacheset.cc
Add GetPriority(VerIterator) to pkgDepCache::Policy
[apt.git] / apt-pkg / cacheset.cc
CommitLineData
ffee1c2b
DK
1// -*- mode: cpp; mode: fold -*-
2// Description /*{{{*/
3/* ######################################################################
4
5 Simple wrapper around a std::set to provide a similar interface to
7959c5ed
DK
6 a set of cache structures as to the complete set of all structures
7 in the pkgCache. Currently only Package is supported.
ffee1c2b
DK
8
9 ##################################################################### */
10 /*}}}*/
11// Include Files /*{{{*/
ea542140
DK
12#include <config.h>
13
78c32596 14#include <apt-pkg/aptconfiguration.h>
472ff00e 15#include <apt-pkg/cachefile.h>
9ba5aa3b 16#include <apt-pkg/cachefilter.h>
8fde7239 17#include <apt-pkg/cacheset.h>
ffee1c2b 18#include <apt-pkg/error.h>
856d3b06 19#include <apt-pkg/versionmatch.h>
472ff00e
DK
20#include <apt-pkg/pkgrecords.h>
21#include <apt-pkg/policy.h>
453b82a3
DK
22#include <apt-pkg/cacheiterators.h>
23#include <apt-pkg/configuration.h>
24#include <apt-pkg/depcache.h>
25#include <apt-pkg/macros.h>
26#include <apt-pkg/pkgcache.h>
fdff5b03 27#include <apt-pkg/fileutl.h>
ffee1c2b 28
453b82a3
DK
29#include <stddef.h>
30#include <stdio.h>
31#include <string.h>
ffee1c2b 32#include <regex.h>
453b82a3
DK
33#include <list>
34#include <string>
35#include <vector>
ea542140
DK
36
37#include <apti18n.h>
ffee1c2b
DK
38 /*}}}*/
39namespace APT {
1e064088
DK
40// PackageFrom - selecting the appropriate method for package selection /*{{{*/
41bool CacheSetHelper::PackageFrom(enum PkgSelector const select, PackageContainerInterface * const pci,
42 pkgCacheFile &Cache, std::string const &pattern) {
43 switch (select) {
44 case UNKNOWN: return false;
45 case REGEX: return PackageFromRegEx(pci, Cache, pattern);
46 case TASK: return PackageFromTask(pci, Cache, pattern);
47 case FNMATCH: return PackageFromFnmatch(pci, Cache, pattern);
48 case PACKAGENAME: return PackageFromPackageName(pci, Cache, pattern);
49 case STRING: return PackageFromString(pci, Cache, pattern);
50 }
51 return false;
52}
53 /*}}}*/
54// PackageFromTask - Return all packages in the cache from a specific task /*{{{*/
55bool CacheSetHelper::PackageFromTask(PackageContainerInterface * const pci, pkgCacheFile &Cache, std::string pattern) {
bd631595 56 size_t const archfound = pattern.find_last_of(':');
dc0f01f7
DK
57 std::string arch = "native";
58 if (archfound != std::string::npos) {
59 arch = pattern.substr(archfound+1);
60 pattern.erase(archfound);
61 }
62
63 if (pattern[pattern.length() -1] != '^')
15fc8636 64 return false;
dc0f01f7
DK
65 pattern.erase(pattern.length()-1);
66
bd631595 67 if (unlikely(Cache.GetPkgCache() == 0 || Cache.GetDepCache() == 0))
15fc8636
DK
68 return false;
69
70 bool const wasEmpty = pci->empty();
71 if (wasEmpty == true)
fdba4d53 72 pci->setConstructor(CacheSetHelper::TASK);
bd631595 73
dc0f01f7
DK
74 // get the records
75 pkgRecords Recs(Cache);
76
77 // build regexp for the task
78 regex_t Pattern;
79 char S[300];
80 snprintf(S, sizeof(S), "^Task:.*[, ]%s([, ]|$)", pattern.c_str());
81 if(regcomp(&Pattern,S, REG_EXTENDED | REG_NOSUB | REG_NEWLINE) != 0) {
82 _error->Error("Failed to compile task regexp");
15fc8636 83 return false;
dc0f01f7
DK
84 }
85
15fc8636 86 bool found = false;
dc0f01f7
DK
87 for (pkgCache::GrpIterator Grp = Cache->GrpBegin(); Grp.end() == false; ++Grp) {
88 pkgCache::PkgIterator Pkg = Grp.FindPkg(arch);
89 if (Pkg.end() == true)
90 continue;
91 pkgCache::VerIterator ver = Cache[Pkg].CandidateVerIter(Cache);
92 if(ver.end() == true)
93 continue;
94
95 pkgRecords::Parser &parser = Recs.Lookup(ver.FileList());
96 const char *start, *end;
97 parser.GetRec(start,end);
98 unsigned int const length = end - start;
62d8a765
DK
99 if (unlikely(length == 0))
100 continue;
dc0f01f7
DK
101 char buf[length];
102 strncpy(buf, start, length);
103 buf[length-1] = '\0';
70e706ad
DK
104 if (regexec(&Pattern, buf, 0, 0, 0) != 0)
105 continue;
106
15fc8636 107 pci->insert(Pkg);
1e064088 108 showPackageSelection(Pkg, CacheSetHelper::TASK, pattern);
15fc8636 109 found = true;
dc0f01f7 110 }
70e706ad 111 regfree(&Pattern);
dc0f01f7 112
15fc8636 113 if (found == false) {
1e064088 114 canNotFindPackage(CacheSetHelper::TASK, pci, Cache, pattern);
fdba4d53 115 pci->setConstructor(CacheSetHelper::UNKNOWN);
15fc8636
DK
116 return false;
117 }
118
fdba4d53
DK
119 if (wasEmpty == false && pci->getConstructor() != CacheSetHelper::UNKNOWN)
120 pci->setConstructor(CacheSetHelper::UNKNOWN);
dc0f01f7 121
15fc8636 122 return true;
dc0f01f7
DK
123}
124 /*}}}*/
1e064088
DK
125// PackageFromRegEx - Return all packages in the cache matching a pattern /*{{{*/
126bool CacheSetHelper::PackageFromRegEx(PackageContainerInterface * const pci, pkgCacheFile &Cache, std::string pattern) {
6e235c66 127 static const char * const isregex = ".?+*|[^$";
6e235c66 128 if (pattern.find_first_of(isregex) == std::string::npos)
15fc8636
DK
129 return false;
130
131 bool const wasEmpty = pci->empty();
132 if (wasEmpty == true)
fdba4d53 133 pci->setConstructor(CacheSetHelper::REGEX);
ffee1c2b 134
6e235c66 135 size_t archfound = pattern.find_last_of(':');
dc0f01f7 136 std::string arch = "native";
6e235c66
DK
137 if (archfound != std::string::npos) {
138 arch = pattern.substr(archfound+1);
139 if (arch.find_first_of(isregex) == std::string::npos)
140 pattern.erase(archfound);
141 else
142 arch = "native";
143 }
144
bd631595 145 if (unlikely(Cache.GetPkgCache() == 0))
15fc8636 146 return false;
bd631595 147
9ba5aa3b
DK
148 APT::CacheFilter::PackageNameMatchesRegEx regexfilter(pattern);
149
15fc8636 150 bool found = false;
9ba5aa3b
DK
151 for (pkgCache::GrpIterator Grp = Cache.GetPkgCache()->GrpBegin(); Grp.end() == false; ++Grp) {
152 if (regexfilter(Grp) == false)
ffee1c2b 153 continue;
6e235c66 154 pkgCache::PkgIterator Pkg = Grp.FindPkg(arch);
78c32596 155 if (Pkg.end() == true) {
6e235c66
DK
156 if (archfound == std::string::npos) {
157 std::vector<std::string> archs = APT::Configuration::getArchitectures();
158 for (std::vector<std::string>::const_iterator a = archs.begin();
159 a != archs.end() && Pkg.end() != true; ++a)
160 Pkg = Grp.FindPkg(*a);
78c32596
DK
161 }
162 if (Pkg.end() == true)
163 continue;
164 }
ffee1c2b 165
15fc8636 166 pci->insert(Pkg);
1e064088 167 showPackageSelection(Pkg, CacheSetHelper::REGEX, pattern);
15fc8636 168 found = true;
ffee1c2b 169 }
ffee1c2b 170
15fc8636 171 if (found == false) {
1e064088 172 canNotFindPackage(CacheSetHelper::REGEX, pci, Cache, pattern);
fdba4d53 173 pci->setConstructor(CacheSetHelper::UNKNOWN);
15fc8636
DK
174 return false;
175 }
176
fdba4d53
DK
177 if (wasEmpty == false && pci->getConstructor() != CacheSetHelper::UNKNOWN)
178 pci->setConstructor(CacheSetHelper::UNKNOWN);
b9179170
MV
179
180 return true;
181}
182 /*}}}*/
1e064088
DK
183// PackageFromFnmatch - Returns the package defined by this fnmatch /*{{{*/
184bool CacheSetHelper::PackageFromFnmatch(PackageContainerInterface * const pci,
185 pkgCacheFile &Cache, std::string pattern)
b9179170
MV
186{
187 static const char * const isfnmatch = ".?*[]!";
188 if (pattern.find_first_of(isfnmatch) == std::string::npos)
189 return false;
190
191 bool const wasEmpty = pci->empty();
192 if (wasEmpty == true)
fdba4d53 193 pci->setConstructor(CacheSetHelper::FNMATCH);
b9179170
MV
194
195 size_t archfound = pattern.find_last_of(':');
196 std::string arch = "native";
197 if (archfound != std::string::npos) {
198 arch = pattern.substr(archfound+1);
199 if (arch.find_first_of(isfnmatch) == std::string::npos)
200 pattern.erase(archfound);
201 else
202 arch = "native";
203 }
204
205 if (unlikely(Cache.GetPkgCache() == 0))
206 return false;
207
208 APT::CacheFilter::PackageNameMatchesFnmatch filter(pattern);
209
210 bool found = false;
211 for (pkgCache::GrpIterator Grp = Cache.GetPkgCache()->GrpBegin(); Grp.end() == false; ++Grp) {
212 if (filter(Grp) == false)
213 continue;
214 pkgCache::PkgIterator Pkg = Grp.FindPkg(arch);
215 if (Pkg.end() == true) {
216 if (archfound == std::string::npos) {
217 std::vector<std::string> archs = APT::Configuration::getArchitectures();
218 for (std::vector<std::string>::const_iterator a = archs.begin();
219 a != archs.end() && Pkg.end() != true; ++a)
220 Pkg = Grp.FindPkg(*a);
221 }
222 if (Pkg.end() == true)
223 continue;
224 }
225
226 pci->insert(Pkg);
1e064088 227 showPackageSelection(Pkg, CacheSetHelper::FNMATCH, pattern);
b9179170
MV
228 found = true;
229 }
230
231 if (found == false) {
1e064088 232 canNotFindPackage(CacheSetHelper::FNMATCH, pci, Cache, pattern);
fdba4d53 233 pci->setConstructor(CacheSetHelper::UNKNOWN);
b9179170
MV
234 return false;
235 }
236
fdba4d53
DK
237 if (wasEmpty == false && pci->getConstructor() != CacheSetHelper::UNKNOWN)
238 pci->setConstructor(CacheSetHelper::UNKNOWN);
70e706ad 239
15fc8636 240 return true;
78c32596
DK
241}
242 /*}}}*/
1e064088
DK
243// PackageFromName - Returns the package defined by this string /*{{{*/
244pkgCache::PkgIterator CacheSetHelper::PackageFromName(pkgCacheFile &Cache,
245 std::string const &str) {
bd631595
DK
246 std::string pkg = str;
247 size_t archfound = pkg.find_last_of(':');
248 std::string arch;
249 if (archfound != std::string::npos) {
250 arch = pkg.substr(archfound+1);
251 pkg.erase(archfound);
252 }
253
254 if (Cache.GetPkgCache() == 0)
255 return pkgCache::PkgIterator(Cache, 0);
256
257 pkgCache::PkgIterator Pkg(Cache, 0);
258 if (arch.empty() == true) {
259 pkgCache::GrpIterator Grp = Cache.GetPkgCache()->FindGrp(pkg);
260 if (Grp.end() == false)
261 Pkg = Grp.FindPreferredPkg();
262 } else
263 Pkg = Cache.GetPkgCache()->FindPkg(pkg, arch);
264
265 if (Pkg.end() == true)
1e064088 266 return canNotFindPkgName(Cache, str);
bd631595
DK
267 return Pkg;
268}
269 /*}}}*/
1e064088
DK
270// PackageFromPackageName - Returns the package defined by this string /*{{{*/
271bool CacheSetHelper::PackageFromPackageName(PackageContainerInterface * const pci, pkgCacheFile &Cache,
272 std::string pkg) {
2f0d4029
DK
273 if (unlikely(Cache.GetPkgCache() == 0))
274 return false;
275
276 size_t const archfound = pkg.find_last_of(':');
277 std::string arch;
278 if (archfound != std::string::npos) {
279 arch = pkg.substr(archfound+1);
280 pkg.erase(archfound);
f1d86c0e
DK
281 if (arch == "all" || arch == "native")
282 arch = _config->Find("APT::Architecture");
2f0d4029
DK
283 }
284
285 pkgCache::GrpIterator Grp = Cache.GetPkgCache()->FindGrp(pkg);
286 if (Grp.end() == false) {
287 if (arch.empty() == true) {
288 pkgCache::PkgIterator Pkg = Grp.FindPreferredPkg();
289 if (Pkg.end() == false)
290 {
291 pci->insert(Pkg);
292 return true;
293 }
294 } else {
295 bool found = false;
296 // for 'linux-any' return the first package matching, for 'linux-*' return all matches
297 bool const isGlobal = arch.find('*') != std::string::npos;
298 APT::CacheFilter::PackageArchitectureMatchesSpecification pams(arch);
299 for (pkgCache::PkgIterator Pkg = Grp.PackageList(); Pkg.end() == false; Pkg = Grp.NextPkg(Pkg)) {
300 if (pams(Pkg) == false)
301 continue;
302 pci->insert(Pkg);
303 found = true;
304 if (isGlobal == false)
305 break;
306 }
307 if (found == true)
308 return true;
309 }
310 }
311
1e064088 312 pkgCache::PkgIterator Pkg = canNotFindPkgName(Cache, pkg);
2f0d4029
DK
313 if (Pkg.end() == true)
314 return false;
315
316 pci->insert(Pkg);
317 return true;
318}
319 /*}}}*/
1e064088
DK
320// PackageFromString - Return all packages matching a specific string /*{{{*/
321bool CacheSetHelper::PackageFromString(PackageContainerInterface * const pci, pkgCacheFile &Cache, std::string const &str) {
15fc8636 322 bool found = true;
48c39e32
DK
323 _error->PushToStack();
324
1e064088
DK
325 if (PackageFrom(CacheSetHelper::PACKAGENAME, pci, Cache, str) == false &&
326 PackageFrom(CacheSetHelper::TASK, pci, Cache, str) == false &&
fdba4d53 327 // FIXME: hm, hm, regexp/fnmatch incompatible?
1e064088
DK
328 PackageFrom(CacheSetHelper::FNMATCH, pci, Cache, str) == false &&
329 PackageFrom(CacheSetHelper::REGEX, pci, Cache, str) == false)
15fc8636 330 {
1e064088 331 canNotFindPackage(CacheSetHelper::PACKAGENAME, pci, Cache, str);
15fc8636 332 found = false;
48c39e32 333 }
dc0f01f7 334
15fc8636 335 if (found == true)
48c39e32
DK
336 _error->RevertToStack();
337 else
338 _error->MergeWithStack();
15fc8636 339 return found;
856d3b06
DK
340}
341 /*}}}*/
1e064088
DK
342// PackageFromCommandLine - Return all packages specified on commandline /*{{{*/
343bool CacheSetHelper::PackageFromCommandLine(PackageContainerInterface * const pci, pkgCacheFile &Cache, const char **cmdline) {
15fc8636
DK
344 bool found = false;
345 for (const char **I = cmdline; *I != 0; ++I)
1e064088 346 found |= PackageFrom(CacheSetHelper::PACKAGENAME, pci, Cache, *I);
15fc8636
DK
347 return found;
348}
349 /*}}}*/
350// FromModifierCommandLine - helper doing the work for PKG:GroupedFromCommandLine /*{{{*/
1e064088 351bool CacheSetHelper::PackageFromModifierCommandLine(unsigned short &modID, PackageContainerInterface * const pci,
15fc8636 352 pkgCacheFile &Cache, const char * cmdline,
1e064088 353 std::list<PkgModifier> const &mods) {
15fc8636 354 std::string str = cmdline;
e6a12579 355 unsigned short fallback = modID;
15fc8636 356 bool modifierPresent = false;
1e064088 357 for (std::list<PkgModifier>::const_iterator mod = mods.begin();
15fc8636
DK
358 mod != mods.end(); ++mod) {
359 size_t const alength = strlen(mod->Alias);
360 switch(mod->Pos) {
1e064088 361 case PkgModifier::POSTFIX:
15fc8636
DK
362 if (str.compare(str.length() - alength, alength,
363 mod->Alias, 0, alength) != 0)
55c59998 364 continue;
15fc8636
DK
365 str.erase(str.length() - alength);
366 modID = mod->ID;
55c59998 367 break;
1e064088 368 case PkgModifier::PREFIX:
15fc8636 369 continue;
1e064088 370 case PkgModifier::NONE:
15fc8636 371 continue;
55c59998 372 }
15fc8636
DK
373 modifierPresent = true;
374 break;
375 }
376 if (modifierPresent == true) {
1e064088
DK
377 bool const errors = showErrors(false);
378 bool const found = PackageFrom(PACKAGENAME, pci, Cache, cmdline);
379 showErrors(errors);
380 if (found == true) {
e6a12579 381 modID = fallback;
15fc8636
DK
382 return true;
383 }
384 }
1e064088 385 return PackageFrom(CacheSetHelper::PACKAGENAME, pci, Cache, str);
15fc8636
DK
386}
387 /*}}}*/
388// FromModifierCommandLine - helper doing the work for VER:GroupedFromCommandLine /*{{{*/
389bool VersionContainerInterface::FromModifierCommandLine(unsigned short &modID,
390 VersionContainerInterface * const vci,
391 pkgCacheFile &Cache, const char * cmdline,
392 std::list<Modifier> const &mods,
393 CacheSetHelper &helper) {
fdba4d53 394 CacheSetHelper::VerSelector select = CacheSetHelper::NEWEST;
15fc8636 395 std::string str = cmdline;
d99854ca
DK
396 if (unlikely(str.empty() == true))
397 return false;
15fc8636
DK
398 bool modifierPresent = false;
399 unsigned short fallback = modID;
400 for (std::list<Modifier>::const_iterator mod = mods.begin();
401 mod != mods.end(); ++mod) {
402 if (modID == fallback && mod->ID == fallback)
403 select = mod->SelectVersion;
404 size_t const alength = strlen(mod->Alias);
405 switch(mod->Pos) {
406 case Modifier::POSTFIX:
d99854ca
DK
407 if (str.length() <= alength ||
408 str.compare(str.length() - alength, alength, mod->Alias, 0, alength) != 0)
bd631595 409 continue;
15fc8636
DK
410 str.erase(str.length() - alength);
411 modID = mod->ID;
412 select = mod->SelectVersion;
413 break;
414 case Modifier::PREFIX:
415 continue;
416 case Modifier::NONE:
417 continue;
bd631595 418 }
15fc8636
DK
419 modifierPresent = true;
420 break;
55c59998 421 }
15fc8636
DK
422 if (modifierPresent == true) {
423 bool const errors = helper.showErrors(false);
424 bool const found = VersionContainerInterface::FromString(vci, Cache, cmdline, select, helper, true);
425 helper.showErrors(errors);
e6a12579
DK
426 if (found == true) {
427 modID = fallback;
15fc8636 428 return true;
e6a12579 429 }
15fc8636
DK
430 }
431 return FromString(vci, Cache, str, select, helper);
55c59998
DK
432}
433 /*}}}*/
856d3b06 434// FromCommandLine - Return all versions specified on commandline /*{{{*/
15fc8636
DK
435bool VersionContainerInterface::FromCommandLine(VersionContainerInterface * const vci,
436 pkgCacheFile &Cache, const char **cmdline,
fdba4d53
DK
437 CacheSetHelper::VerSelector const fallback,
438 CacheSetHelper &helper) {
15fc8636 439 bool found = false;
bd631595 440 for (const char **I = cmdline; *I != 0; ++I)
15fc8636
DK
441 found |= VersionContainerInterface::FromString(vci, Cache, *I, fallback, helper);
442 return found;
55c59998
DK
443}
444 /*}}}*/
445// FromString - Returns all versions spedcified by a string /*{{{*/
15fc8636
DK
446bool VersionContainerInterface::FromString(VersionContainerInterface * const vci,
447 pkgCacheFile &Cache, std::string pkg,
fdba4d53
DK
448 CacheSetHelper::VerSelector const fallback,
449 CacheSetHelper &helper,
15fc8636 450 bool const onlyFromName) {
1e064088
DK
451 PackageSet pkgset;
452 if(FileExists(pkg)) {
453 helper.PackageFrom(CacheSetHelper::STRING, &pkgset, Cache, pkg);
454 if(pkgset.empty() == true)
455 return false;
456 return VersionContainerInterface::FromPackage(vci, Cache, pkgset.begin(), fallback, helper);
457 }
fdff5b03 458
55c59998
DK
459 std::string ver;
460 bool verIsRel = false;
461 size_t const vertag = pkg.find_last_of("/=");
472ff00e 462 if (vertag != std::string::npos) {
55c59998
DK
463 ver = pkg.substr(vertag+1);
464 verIsRel = (pkg[vertag] == '/');
465 pkg.erase(vertag);
466 }
bd631595 467 if (onlyFromName == false)
1e064088 468 helper.PackageFrom(CacheSetHelper::STRING, &pkgset, Cache, pkg);
bd631595 469 else {
1e064088 470 helper.PackageFrom(CacheSetHelper::PACKAGENAME, &pkgset, Cache, pkg);
bd631595
DK
471 }
472
c8db3fff 473 bool errors = true;
fdba4d53 474 if (pkgset.getConstructor() != CacheSetHelper::UNKNOWN)
c8db3fff 475 errors = helper.showErrors(false);
15fc8636
DK
476
477 bool found = false;
55c59998
DK
478 for (PackageSet::const_iterator P = pkgset.begin();
479 P != pkgset.end(); ++P) {
472ff00e 480 if (vertag == std::string::npos) {
15fc8636 481 found |= VersionContainerInterface::FromPackage(vci, Cache, P, fallback, helper);
55c59998 482 continue;
856d3b06 483 }
55c59998
DK
484 pkgCache::VerIterator V;
485 if (ver == "installed")
70e706ad 486 V = getInstalledVer(Cache, P, helper);
55c59998 487 else if (ver == "candidate")
70e706ad 488 V = getCandidateVer(Cache, P, helper);
f1a58ff8
DK
489 else if (ver == "newest") {
490 if (P->VersionList != 0)
491 V = P.VersionList();
492 else
fdba4d53 493 V = helper.canNotGetVersion(CacheSetHelper::NEWEST, Cache, P);
f1a58ff8 494 } else {
55c59998
DK
495 pkgVersionMatch Match(ver, (verIsRel == true ? pkgVersionMatch::Release :
496 pkgVersionMatch::Version));
497 V = Match.Find(P);
498 if (V.end() == true) {
499 if (verIsRel == true)
500 _error->Error(_("Release '%s' for '%s' was not found"),
501 ver.c_str(), P.FullName(true).c_str());
502 else
503 _error->Error(_("Version '%s' for '%s' was not found"),
504 ver.c_str(), P.FullName(true).c_str());
84910ad5
DK
505 continue;
506 }
78c32596 507 }
55c59998
DK
508 if (V.end() == true)
509 continue;
fdba4d53
DK
510 if (verIsRel == true)
511 helper.showVersionSelection(P, V, CacheSetHelper::RELEASE, ver);
512 else
513 helper.showVersionSelection(P, V, CacheSetHelper::VERSIONNUMBER, ver);
15fc8636
DK
514 vci->insert(V);
515 found = true;
78c32596 516 }
fdba4d53 517 if (pkgset.getConstructor() != CacheSetHelper::UNKNOWN)
c8db3fff 518 helper.showErrors(errors);
15fc8636 519 return found;
856d3b06
DK
520}
521 /*}}}*/
fb83c1d0 522// FromPackage - versions from package based on fallback /*{{{*/
15fc8636
DK
523bool VersionContainerInterface::FromPackage(VersionContainerInterface * const vci,
524 pkgCacheFile &Cache,
525 pkgCache::PkgIterator const &P,
fdba4d53 526 CacheSetHelper::VerSelector const fallback,
15fc8636 527 CacheSetHelper &helper) {
84910ad5 528 pkgCache::VerIterator V;
70e706ad 529 bool showErrors;
15fc8636 530 bool found = false;
84910ad5 531 switch(fallback) {
fdba4d53 532 case CacheSetHelper::ALL:
84910ad5
DK
533 if (P->VersionList != 0)
534 for (V = P.VersionList(); V.end() != true; ++V)
15fc8636 535 found |= vci->insert(V);
84910ad5 536 else
fdba4d53 537 helper.canNotFindVersion(CacheSetHelper::ALL, vci, Cache, P);
84910ad5 538 break;
fdba4d53 539 case CacheSetHelper::CANDANDINST:
15fc8636
DK
540 found |= vci->insert(getInstalledVer(Cache, P, helper));
541 found |= vci->insert(getCandidateVer(Cache, P, helper));
84910ad5 542 break;
fdba4d53 543 case CacheSetHelper::CANDIDATE:
15fc8636 544 found |= vci->insert(getCandidateVer(Cache, P, helper));
84910ad5 545 break;
fdba4d53 546 case CacheSetHelper::INSTALLED:
15fc8636 547 found |= vci->insert(getInstalledVer(Cache, P, helper));
84910ad5 548 break;
fdba4d53 549 case CacheSetHelper::CANDINST:
70e706ad
DK
550 showErrors = helper.showErrors(false);
551 V = getCandidateVer(Cache, P, helper);
84910ad5 552 if (V.end() == true)
70e706ad
DK
553 V = getInstalledVer(Cache, P, helper);
554 helper.showErrors(showErrors);
84910ad5 555 if (V.end() == false)
15fc8636 556 found |= vci->insert(V);
84910ad5 557 else
fdba4d53 558 helper.canNotFindVersion(CacheSetHelper::CANDINST, vci, Cache, P);
84910ad5 559 break;
fdba4d53 560 case CacheSetHelper::INSTCAND:
70e706ad
DK
561 showErrors = helper.showErrors(false);
562 V = getInstalledVer(Cache, P, helper);
84910ad5 563 if (V.end() == true)
70e706ad
DK
564 V = getCandidateVer(Cache, P, helper);
565 helper.showErrors(showErrors);
84910ad5 566 if (V.end() == false)
15fc8636 567 found |= vci->insert(V);
84910ad5 568 else
fdba4d53 569 helper.canNotFindVersion(CacheSetHelper::INSTCAND, vci, Cache, P);
84910ad5 570 break;
fdba4d53 571 case CacheSetHelper::NEWEST:
84910ad5 572 if (P->VersionList != 0)
15fc8636 573 found |= vci->insert(P.VersionList());
84910ad5 574 else
fdba4d53 575 helper.canNotFindVersion(CacheSetHelper::NEWEST, vci, Cache, P);
84910ad5 576 break;
fdba4d53
DK
577 case CacheSetHelper::RELEASE:
578 case CacheSetHelper::VERSIONNUMBER:
579 // both make no sense here, so always false
580 return false;
84910ad5 581 }
15fc8636 582 return found;
84910ad5
DK
583}
584 /*}}}*/
9112f777
DK
585// FromDependency - versions satisfying a given dependency /*{{{*/
586bool VersionContainerInterface::FromDependency(VersionContainerInterface * const vci,
587 pkgCacheFile &Cache,
588 pkgCache::DepIterator const &D,
589 CacheSetHelper::VerSelector const selector,
590 CacheSetHelper &helper)
591{
592 bool found = false;
593 switch(selector) {
594 case CacheSetHelper::ALL:
595 {
596 pkgCache::PkgIterator const T = D.TargetPkg();
597 for (pkgCache::VerIterator Ver = T.VersionList(); Ver.end() == false; ++Ver)
598 {
599 if (D.IsSatisfied(Ver) == true)
600 {
601 vci->insert(Ver);
602 found = true;
603 }
604 for (pkgCache::PrvIterator Prv = T.ProvidesList(); Prv.end() != true; ++Prv)
605 {
606 pkgCache::VerIterator const V = Prv.OwnerVer();
607 if (unlikely(V.end() == true) || D.IsSatisfied(Prv) == false)
608 continue;
609 vci->insert(V);
610 found = true;
611 }
612 }
613 return found;
614 }
615 case CacheSetHelper::CANDANDINST:
616 {
617 found = FromDependency(vci, Cache, D, CacheSetHelper::CANDIDATE, helper);
618 found &= FromDependency(vci, Cache, D, CacheSetHelper::INSTALLED, helper);
619 return found;
620 }
621 case CacheSetHelper::CANDIDATE:
622 {
623 pkgCache::PkgIterator const T = D.TargetPkg();
624 pkgCache::VerIterator const Cand = Cache[T].CandidateVerIter(Cache);
625 if (Cand.end() == false && D.IsSatisfied(Cand) == true)
626 {
627 vci->insert(Cand);
628 found = true;
629 }
630 for (pkgCache::PrvIterator Prv = T.ProvidesList(); Prv.end() != true; ++Prv)
631 {
632 pkgCache::VerIterator const V = Prv.OwnerVer();
633 pkgCache::VerIterator const Cand = Cache[Prv.OwnerPkg()].CandidateVerIter(Cache);
634 if (Cand.end() == true || V != Cand || D.IsSatisfied(Prv) == false)
635 continue;
636 vci->insert(Cand);
637 found = true;
638 }
639 return found;
640 }
641 case CacheSetHelper::INSTALLED:
642 {
643 pkgCache::PkgIterator const T = D.TargetPkg();
644 pkgCache::VerIterator const Cand = T.CurrentVer();
645 if (Cand.end() == false && D.IsSatisfied(Cand) == true)
646 {
647 vci->insert(Cand);
648 found = true;
649 }
650 for (pkgCache::PrvIterator Prv = T.ProvidesList(); Prv.end() != true; ++Prv)
651 {
652 pkgCache::VerIterator const V = Prv.OwnerVer();
653 pkgCache::VerIterator const Cand = Prv.OwnerPkg().CurrentVer();
654 if (Cand.end() == true || V != Cand || D.IsSatisfied(Prv) == false)
655 continue;
656 vci->insert(Cand);
657 found = true;
658 }
659 return found;
660 }
661 case CacheSetHelper::CANDINST:
662 return FromDependency(vci, Cache, D, CacheSetHelper::CANDIDATE, helper) ||
663 FromDependency(vci, Cache, D, CacheSetHelper::INSTALLED, helper);
664 case CacheSetHelper::INSTCAND:
665 return FromDependency(vci, Cache, D, CacheSetHelper::INSTALLED, helper) ||
666 FromDependency(vci, Cache, D, CacheSetHelper::CANDIDATE, helper);
667 case CacheSetHelper::NEWEST:
668 {
669 pkgCache::PkgIterator const T = D.TargetPkg();
670 pkgCache::VerIterator const Cand = T.VersionList();
671 if (Cand.end() == false && D.IsSatisfied(Cand) == true)
672 {
673 vci->insert(Cand);
674 found = true;
675 }
676 for (pkgCache::PrvIterator Prv = T.ProvidesList(); Prv.end() != true; ++Prv)
677 {
678 pkgCache::VerIterator const V = Prv.OwnerVer();
679 pkgCache::VerIterator const Cand = Prv.OwnerPkg().VersionList();
680 if (Cand.end() == true || V != Cand || D.IsSatisfied(Prv) == false)
681 continue;
682 vci->insert(Cand);
683 found = true;
684 }
685 return found;
686 }
687 case CacheSetHelper::RELEASE:
688 case CacheSetHelper::VERSIONNUMBER:
689 // both make no sense here, so always false
690 return false;
691 }
692 return found;
693}
694 /*}}}*/
856d3b06 695// getCandidateVer - Returns the candidate version of the given package /*{{{*/
15fc8636 696pkgCache::VerIterator VersionContainerInterface::getCandidateVer(pkgCacheFile &Cache,
70e706ad 697 pkgCache::PkgIterator const &Pkg, CacheSetHelper &helper) {
a8ef7efd 698 pkgCache::VerIterator Cand;
15fc8636 699 if (Cache.IsPolicyBuilt() == true || Cache.IsDepCacheBuilt() == false) {
bd631595
DK
700 if (unlikely(Cache.GetPolicy() == 0))
701 return pkgCache::VerIterator(Cache);
a8ef7efd 702 Cand = Cache.GetPolicy()->GetCandidateVer(Pkg);
2fbfb111
DK
703 } else {
704 Cand = Cache[Pkg].CandidateVerIter(Cache);
a8ef7efd 705 }
70e706ad 706 if (Cand.end() == true)
fdba4d53 707 return helper.canNotGetVersion(CacheSetHelper::CANDIDATE, Cache, Pkg);
856d3b06
DK
708 return Cand;
709}
710 /*}}}*/
711// getInstalledVer - Returns the installed version of the given package /*{{{*/
15fc8636 712pkgCache::VerIterator VersionContainerInterface::getInstalledVer(pkgCacheFile &Cache,
70e706ad
DK
713 pkgCache::PkgIterator const &Pkg, CacheSetHelper &helper) {
714 if (Pkg->CurrentVer == 0)
fdba4d53 715 return helper.canNotGetVersion(CacheSetHelper::INSTALLED, Cache, Pkg);
856d3b06 716 return Pkg.CurrentVer();
ffee1c2b
DK
717}
718 /*}}}*/
15fc8636 719
fdba4d53
DK
720// canNotFindPackage - with the given selector and pattern /*{{{*/
721void CacheSetHelper::canNotFindPackage(enum PkgSelector const select,
722 PackageContainerInterface * const pci, pkgCacheFile &Cache,
723 std::string const &pattern) {
724 switch (select) {
586d8704 725APT_IGNORE_DEPRECATED_PUSH
fdba4d53
DK
726 case REGEX: canNotFindRegEx(pci, Cache, pattern); break;
727 case TASK: canNotFindTask(pci, Cache, pattern); break;
728 case FNMATCH: canNotFindFnmatch(pci, Cache, pattern); break;
729 case PACKAGENAME: canNotFindPackage(pci, Cache, pattern); break;
1e064088 730 case STRING: canNotFindPackage(pci, Cache, pattern); break;
fdba4d53 731 case UNKNOWN: break;
586d8704 732APT_IGNORE_DEPRECATED_POP
fdba4d53 733 }
bd631595 734}
70e706ad 735// canNotFindTask - handle the case no package is found for a task /*{{{*/
65512241 736void CacheSetHelper::canNotFindTask(PackageContainerInterface * const /*pci*/, pkgCacheFile &/*Cache*/, std::string pattern) {
70e706ad 737 if (ShowError == true)
cd7bbc47 738 _error->Insert(ErrorType, _("Couldn't find task '%s'"), pattern.c_str());
70e706ad
DK
739}
740 /*}}}*/
741// canNotFindRegEx - handle the case no package is found by a regex /*{{{*/
65512241 742void CacheSetHelper::canNotFindRegEx(PackageContainerInterface * const /*pci*/, pkgCacheFile &/*Cache*/, std::string pattern) {
70e706ad 743 if (ShowError == true)
cd7bbc47 744 _error->Insert(ErrorType, _("Couldn't find any package by regex '%s'"), pattern.c_str());
70e706ad 745}
fdba4d53 746 /*}}}*/
16724b66 747// canNotFindFnmatch - handle the case no package is found by a fnmatch /*{{{*/
b58f28d4 748 void CacheSetHelper::canNotFindFnmatch(PackageContainerInterface * const /*pci*/, pkgCacheFile &/*Cache*/, std::string pattern) {
16724b66
MV
749 if (ShowError == true)
750 _error->Insert(ErrorType, _("Couldn't find any package by glob '%s'"), pattern.c_str());
751}
fdba4d53 752 /*}}}*/
70e706ad 753// canNotFindPackage - handle the case no package is found from a string/*{{{*/
a02db58f 754APT_CONST void CacheSetHelper::canNotFindPackage(PackageContainerInterface * const /*pci*/, pkgCacheFile &/*Cache*/, std::string const &/*str*/) {
70e706ad
DK
755}
756 /*}}}*/
fdba4d53
DK
757 /*}}}*/
758// canNotFindPkgName - handle the case no package has this name /*{{{*/
759pkgCache::PkgIterator CacheSetHelper::canNotFindPkgName(pkgCacheFile &Cache,
760 std::string const &str) {
761 if (ShowError == true)
762 _error->Insert(ErrorType, _("Unable to locate package %s"), str.c_str());
763 return pkgCache::PkgIterator(Cache, 0);
764}
765 /*}}}*/
766// canNotFindVersion - for package by selector /*{{{*/
767void CacheSetHelper::canNotFindVersion(enum VerSelector const select, VersionContainerInterface * const vci, pkgCacheFile &Cache, pkgCache::PkgIterator const &Pkg)
768{
769 switch (select) {
586d8704 770APT_IGNORE_DEPRECATED_PUSH
fdba4d53
DK
771 case ALL: canNotFindAllVer(vci, Cache, Pkg); break;
772 case INSTCAND: canNotFindInstCandVer(vci, Cache, Pkg); break;
773 case CANDINST: canNotFindCandInstVer(vci, Cache, Pkg); break;
774 case NEWEST: canNotFindNewestVer(Cache, Pkg); break;
775 case CANDIDATE: canNotFindCandidateVer(Cache, Pkg); break;
776 case INSTALLED: canNotFindInstalledVer(Cache, Pkg); break;
586d8704 777APT_IGNORE_DEPRECATED_POP
fdba4d53
DK
778 case CANDANDINST: canNotGetCandInstVer(Cache, Pkg); break;
779 case RELEASE:
780 case VERSIONNUMBER:
781 // invalid in this branch
782 break;
783 }
784}
70e706ad 785// canNotFindAllVer /*{{{*/
65512241 786void CacheSetHelper::canNotFindAllVer(VersionContainerInterface * const /*vci*/, pkgCacheFile &/*Cache*/,
70e706ad
DK
787 pkgCache::PkgIterator const &Pkg) {
788 if (ShowError == true)
edc0ef10 789 _error->Insert(ErrorType, _("Can't select versions from package '%s' as it is purely virtual"), Pkg.FullName(true).c_str());
70e706ad
DK
790}
791 /*}}}*/
792// canNotFindInstCandVer /*{{{*/
fdba4d53 793void CacheSetHelper::canNotFindInstCandVer(VersionContainerInterface * const /*vci*/, pkgCacheFile &Cache,
70e706ad 794 pkgCache::PkgIterator const &Pkg) {
fdba4d53 795 canNotGetInstCandVer(Cache, Pkg);
70e706ad
DK
796}
797 /*}}}*/
cf28bcad 798// canNotFindInstCandVer /*{{{*/
fdba4d53 799void CacheSetHelper::canNotFindCandInstVer(VersionContainerInterface * const /*vci*/, pkgCacheFile &Cache,
cf28bcad 800 pkgCache::PkgIterator const &Pkg) {
fdba4d53 801 canNotGetCandInstVer(Cache, Pkg);
cf28bcad
DK
802}
803 /*}}}*/
fdba4d53
DK
804 /*}}}*/
805// canNotGetVersion - for package by selector /*{{{*/
806pkgCache::VerIterator CacheSetHelper::canNotGetVersion(enum VerSelector const select, pkgCacheFile &Cache, pkgCache::PkgIterator const &Pkg) {
807 switch (select) {
586d8704 808APT_IGNORE_DEPRECATED_PUSH
fdba4d53
DK
809 case NEWEST: return canNotFindNewestVer(Cache, Pkg);
810 case CANDIDATE: return canNotFindCandidateVer(Cache, Pkg);
811 case INSTALLED: return canNotFindInstalledVer(Cache, Pkg);
586d8704 812APT_IGNORE_DEPRECATED_POP
fdba4d53
DK
813 case CANDINST: return canNotGetCandInstVer(Cache, Pkg);
814 case INSTCAND: return canNotGetInstCandVer(Cache, Pkg);
815 case ALL:
816 case CANDANDINST:
817 case RELEASE:
818 case VERSIONNUMBER:
819 // invalid in this branch
820 return pkgCache::VerIterator(Cache, 0);
821 }
822 return pkgCache::VerIterator(Cache, 0);
823}
70e706ad
DK
824// canNotFindNewestVer /*{{{*/
825pkgCache::VerIterator CacheSetHelper::canNotFindNewestVer(pkgCacheFile &Cache,
826 pkgCache::PkgIterator const &Pkg) {
827 if (ShowError == true)
cd7bbc47 828 _error->Insert(ErrorType, _("Can't select newest version from package '%s' as it is purely virtual"), Pkg.FullName(true).c_str());
c8db3fff 829 return pkgCache::VerIterator(Cache, 0);
70e706ad
DK
830}
831 /*}}}*/
832// canNotFindCandidateVer /*{{{*/
833pkgCache::VerIterator CacheSetHelper::canNotFindCandidateVer(pkgCacheFile &Cache,
834 pkgCache::PkgIterator const &Pkg) {
835 if (ShowError == true)
cd7bbc47 836 _error->Insert(ErrorType, _("Can't select candidate version from package %s as it has no candidate"), Pkg.FullName(true).c_str());
c8db3fff 837 return pkgCache::VerIterator(Cache, 0);
70e706ad
DK
838}
839 /*}}}*/
840// canNotFindInstalledVer /*{{{*/
841pkgCache::VerIterator CacheSetHelper::canNotFindInstalledVer(pkgCacheFile &Cache,
842 pkgCache::PkgIterator const &Pkg) {
843 if (ShowError == true)
cd7bbc47 844 _error->Insert(ErrorType, _("Can't select installed version from package %s as it is not installed"), Pkg.FullName(true).c_str());
c8db3fff 845 return pkgCache::VerIterator(Cache, 0);
70e706ad
DK
846}
847 /*}}}*/
fdba4d53
DK
848// canNotFindInstCandVer /*{{{*/
849pkgCache::VerIterator CacheSetHelper::canNotGetInstCandVer(pkgCacheFile &Cache,
850 pkgCache::PkgIterator const &Pkg) {
851 if (ShowError == true)
852 _error->Insert(ErrorType, _("Can't select installed nor candidate version from package '%s' as it has neither of them"), Pkg.FullName(true).c_str());
853 return pkgCache::VerIterator(Cache, 0);
854}
855 /*}}}*/
856// canNotFindInstCandVer /*{{{*/
857pkgCache::VerIterator CacheSetHelper::canNotGetCandInstVer(pkgCacheFile &Cache,
858 pkgCache::PkgIterator const &Pkg) {
859 if (ShowError == true)
860 _error->Insert(ErrorType, _("Can't select installed nor candidate version from package '%s' as it has neither of them"), Pkg.FullName(true).c_str());
861 return pkgCache::VerIterator(Cache, 0);
862}
863 /*}}}*/
864 /*}}}*/
865// showPackageSelection - by selector and given pattern /*{{{*/
2b4cead3 866void CacheSetHelper::showPackageSelection(pkgCache::PkgIterator const &pkg, enum PkgSelector const select,
fdba4d53
DK
867 std::string const &pattern) {
868 switch (select) {
586d8704 869APT_IGNORE_DEPRECATED_PUSH
fdba4d53
DK
870 case REGEX: showRegExSelection(pkg, pattern); break;
871 case TASK: showTaskSelection(pkg, pattern); break;
872 case FNMATCH: showFnmatchSelection(pkg, pattern); break;
586d8704 873APT_IGNORE_DEPRECATED_POP
fdba4d53 874 case PACKAGENAME: /* no suprises here */ break;
1e064088 875 case STRING: /* handled by the special cases */ break;
fdba4d53
DK
876 case UNKNOWN: break;
877 }
878}
15fc8636 879// showTaskSelection /*{{{*/
a02db58f 880APT_CONST void CacheSetHelper::showTaskSelection(pkgCache::PkgIterator const &/*pkg*/,
65512241 881 std::string const &/*pattern*/) {
15fc8636
DK
882}
883 /*}}}*/
884// showRegExSelection /*{{{*/
a02db58f 885APT_CONST void CacheSetHelper::showRegExSelection(pkgCache::PkgIterator const &/*pkg*/,
65512241 886 std::string const &/*pattern*/) {
15fc8636
DK
887}
888 /*}}}*/
16724b66 889// showFnmatchSelection /*{{{*/
b58f28d4
MV
890APT_CONST void CacheSetHelper::showFnmatchSelection(pkgCache::PkgIterator const &/*pkg*/,
891 std::string const &/*pattern*/) {
16724b66
MV
892}
893 /*}}}*/
fdba4d53
DK
894 /*}}}*/
895// showVersionSelection /*{{{*/
2b4cead3 896void CacheSetHelper::showVersionSelection(pkgCache::PkgIterator const &Pkg,
fdba4d53
DK
897 pkgCache::VerIterator const &Ver, enum VerSelector const select, std::string const &pattern) {
898 switch (select) {
586d8704 899APT_IGNORE_DEPRECATED_PUSH
fdba4d53
DK
900 case RELEASE:
901 showSelectedVersion(Pkg, Ver, pattern, true);
902 break;
903 case VERSIONNUMBER:
904 showSelectedVersion(Pkg, Ver, pattern, false);
905 break;
586d8704 906APT_IGNORE_DEPRECATED_POP
fdba4d53
DK
907 case NEWEST:
908 case CANDIDATE:
909 case INSTALLED:
910 case CANDINST:
911 case INSTCAND:
912 case ALL:
913 case CANDANDINST:
914 // not really suprises, but in fact: just not implemented
915 break;
916 }
917}
a02db58f 918APT_CONST void CacheSetHelper::showSelectedVersion(pkgCache::PkgIterator const &/*Pkg*/,
65512241
DK
919 pkgCache::VerIterator const /*Ver*/,
920 std::string const &/*ver*/,
921 bool const /*verIsRel*/) {
15fc8636
DK
922}
923 /*}}}*/
c8a4ce6c
DK
924
925CacheSetHelper::CacheSetHelper(bool const ShowError, GlobalError::MsgType ErrorType) :
6c55f07a 926 ShowError(ShowError), ErrorType(ErrorType), d(NULL) {}
c8a4ce6c
DK
927CacheSetHelper::~CacheSetHelper() {}
928
6c55f07a
DK
929PackageContainerInterface::PackageContainerInterface() : ConstructedBy(CacheSetHelper::UNKNOWN), d(NULL) {}
930PackageContainerInterface::PackageContainerInterface(CacheSetHelper::PkgSelector const by) : ConstructedBy(by), d(NULL) {}
931PackageContainerInterface& PackageContainerInterface::operator=(PackageContainerInterface const &other) {
932 if (this != &other)
933 this->ConstructedBy = other.ConstructedBy;
934 return *this;
935}
c8a4ce6c
DK
936PackageContainerInterface::~PackageContainerInterface() {}
937
3707fd4f
DK
938PackageUniverse::PackageUniverse(pkgCache * const Owner) : _cont(Owner), d(NULL) {}
939PackageUniverse::PackageUniverse(pkgCacheFile * const Owner) : _cont(Owner->GetPkgCache()), d(NULL) {}
c8a4ce6c
DK
940PackageUniverse::~PackageUniverse() {}
941
6c55f07a
DK
942VersionContainerInterface::VersionContainerInterface() : d(NULL) {}
943VersionContainerInterface& VersionContainerInterface::operator=(VersionContainerInterface const &) {
944 return *this;
945}
946
c8a4ce6c 947VersionContainerInterface::~VersionContainerInterface() {}
ffee1c2b 948}