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