]> git.saurik.com Git - apt.git/blame - apt-pkg/cacheset.cc
improve https method queue progress reporting
[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) {
586d8704 616APT_IGNORE_DEPRECATED_PUSH
fdba4d53
DK
617 case REGEX: canNotFindRegEx(pci, Cache, pattern); break;
618 case TASK: canNotFindTask(pci, Cache, pattern); break;
619 case FNMATCH: canNotFindFnmatch(pci, Cache, pattern); break;
620 case PACKAGENAME: canNotFindPackage(pci, Cache, pattern); break;
1e064088 621 case STRING: canNotFindPackage(pci, Cache, pattern); break;
fdba4d53 622 case UNKNOWN: break;
586d8704 623APT_IGNORE_DEPRECATED_POP
fdba4d53 624 }
bd631595 625}
70e706ad 626// canNotFindTask - handle the case no package is found for a task /*{{{*/
65512241 627void CacheSetHelper::canNotFindTask(PackageContainerInterface * const /*pci*/, pkgCacheFile &/*Cache*/, std::string pattern) {
70e706ad 628 if (ShowError == true)
cd7bbc47 629 _error->Insert(ErrorType, _("Couldn't find task '%s'"), pattern.c_str());
70e706ad
DK
630}
631 /*}}}*/
632// canNotFindRegEx - handle the case no package is found by a regex /*{{{*/
65512241 633void CacheSetHelper::canNotFindRegEx(PackageContainerInterface * const /*pci*/, pkgCacheFile &/*Cache*/, std::string pattern) {
70e706ad 634 if (ShowError == true)
cd7bbc47 635 _error->Insert(ErrorType, _("Couldn't find any package by regex '%s'"), pattern.c_str());
70e706ad 636}
fdba4d53 637 /*}}}*/
16724b66 638// canNotFindFnmatch - handle the case no package is found by a fnmatch /*{{{*/
b58f28d4 639 void CacheSetHelper::canNotFindFnmatch(PackageContainerInterface * const /*pci*/, pkgCacheFile &/*Cache*/, std::string pattern) {
16724b66
MV
640 if (ShowError == true)
641 _error->Insert(ErrorType, _("Couldn't find any package by glob '%s'"), pattern.c_str());
642}
fdba4d53 643 /*}}}*/
70e706ad 644// canNotFindPackage - handle the case no package is found from a string/*{{{*/
a02db58f 645APT_CONST void CacheSetHelper::canNotFindPackage(PackageContainerInterface * const /*pci*/, pkgCacheFile &/*Cache*/, std::string const &/*str*/) {
70e706ad
DK
646}
647 /*}}}*/
fdba4d53
DK
648 /*}}}*/
649// canNotFindPkgName - handle the case no package has this name /*{{{*/
650pkgCache::PkgIterator CacheSetHelper::canNotFindPkgName(pkgCacheFile &Cache,
651 std::string const &str) {
652 if (ShowError == true)
653 _error->Insert(ErrorType, _("Unable to locate package %s"), str.c_str());
654 return pkgCache::PkgIterator(Cache, 0);
655}
656 /*}}}*/
657// canNotFindVersion - for package by selector /*{{{*/
658void CacheSetHelper::canNotFindVersion(enum VerSelector const select, VersionContainerInterface * const vci, pkgCacheFile &Cache, pkgCache::PkgIterator const &Pkg)
659{
660 switch (select) {
586d8704 661APT_IGNORE_DEPRECATED_PUSH
fdba4d53
DK
662 case ALL: canNotFindAllVer(vci, Cache, Pkg); break;
663 case INSTCAND: canNotFindInstCandVer(vci, Cache, Pkg); break;
664 case CANDINST: canNotFindCandInstVer(vci, Cache, Pkg); break;
665 case NEWEST: canNotFindNewestVer(Cache, Pkg); break;
666 case CANDIDATE: canNotFindCandidateVer(Cache, Pkg); break;
667 case INSTALLED: canNotFindInstalledVer(Cache, Pkg); break;
586d8704 668APT_IGNORE_DEPRECATED_POP
fdba4d53
DK
669 case CANDANDINST: canNotGetCandInstVer(Cache, Pkg); break;
670 case RELEASE:
671 case VERSIONNUMBER:
672 // invalid in this branch
673 break;
674 }
675}
70e706ad 676// canNotFindAllVer /*{{{*/
65512241 677void CacheSetHelper::canNotFindAllVer(VersionContainerInterface * const /*vci*/, pkgCacheFile &/*Cache*/,
70e706ad
DK
678 pkgCache::PkgIterator const &Pkg) {
679 if (ShowError == true)
edc0ef10 680 _error->Insert(ErrorType, _("Can't select versions from package '%s' as it is purely virtual"), Pkg.FullName(true).c_str());
70e706ad
DK
681}
682 /*}}}*/
683// canNotFindInstCandVer /*{{{*/
fdba4d53 684void CacheSetHelper::canNotFindInstCandVer(VersionContainerInterface * const /*vci*/, pkgCacheFile &Cache,
70e706ad 685 pkgCache::PkgIterator const &Pkg) {
fdba4d53 686 canNotGetInstCandVer(Cache, Pkg);
70e706ad
DK
687}
688 /*}}}*/
cf28bcad 689// canNotFindInstCandVer /*{{{*/
fdba4d53 690void CacheSetHelper::canNotFindCandInstVer(VersionContainerInterface * const /*vci*/, pkgCacheFile &Cache,
cf28bcad 691 pkgCache::PkgIterator const &Pkg) {
fdba4d53 692 canNotGetCandInstVer(Cache, Pkg);
cf28bcad
DK
693}
694 /*}}}*/
fdba4d53
DK
695 /*}}}*/
696// canNotGetVersion - for package by selector /*{{{*/
697pkgCache::VerIterator CacheSetHelper::canNotGetVersion(enum VerSelector const select, pkgCacheFile &Cache, pkgCache::PkgIterator const &Pkg) {
698 switch (select) {
586d8704 699APT_IGNORE_DEPRECATED_PUSH
fdba4d53
DK
700 case NEWEST: return canNotFindNewestVer(Cache, Pkg);
701 case CANDIDATE: return canNotFindCandidateVer(Cache, Pkg);
702 case INSTALLED: return canNotFindInstalledVer(Cache, Pkg);
586d8704 703APT_IGNORE_DEPRECATED_POP
fdba4d53
DK
704 case CANDINST: return canNotGetCandInstVer(Cache, Pkg);
705 case INSTCAND: return canNotGetInstCandVer(Cache, Pkg);
706 case ALL:
707 case CANDANDINST:
708 case RELEASE:
709 case VERSIONNUMBER:
710 // invalid in this branch
711 return pkgCache::VerIterator(Cache, 0);
712 }
713 return pkgCache::VerIterator(Cache, 0);
714}
70e706ad
DK
715// canNotFindNewestVer /*{{{*/
716pkgCache::VerIterator CacheSetHelper::canNotFindNewestVer(pkgCacheFile &Cache,
717 pkgCache::PkgIterator const &Pkg) {
718 if (ShowError == true)
cd7bbc47 719 _error->Insert(ErrorType, _("Can't select newest version from package '%s' as it is purely virtual"), Pkg.FullName(true).c_str());
c8db3fff 720 return pkgCache::VerIterator(Cache, 0);
70e706ad
DK
721}
722 /*}}}*/
723// canNotFindCandidateVer /*{{{*/
724pkgCache::VerIterator CacheSetHelper::canNotFindCandidateVer(pkgCacheFile &Cache,
725 pkgCache::PkgIterator const &Pkg) {
726 if (ShowError == true)
cd7bbc47 727 _error->Insert(ErrorType, _("Can't select candidate version from package %s as it has no candidate"), Pkg.FullName(true).c_str());
c8db3fff 728 return pkgCache::VerIterator(Cache, 0);
70e706ad
DK
729}
730 /*}}}*/
731// canNotFindInstalledVer /*{{{*/
732pkgCache::VerIterator CacheSetHelper::canNotFindInstalledVer(pkgCacheFile &Cache,
733 pkgCache::PkgIterator const &Pkg) {
734 if (ShowError == true)
cd7bbc47 735 _error->Insert(ErrorType, _("Can't select installed version from package %s as it is not installed"), Pkg.FullName(true).c_str());
c8db3fff 736 return pkgCache::VerIterator(Cache, 0);
70e706ad
DK
737}
738 /*}}}*/
fdba4d53
DK
739// canNotFindInstCandVer /*{{{*/
740pkgCache::VerIterator CacheSetHelper::canNotGetInstCandVer(pkgCacheFile &Cache,
741 pkgCache::PkgIterator const &Pkg) {
742 if (ShowError == true)
743 _error->Insert(ErrorType, _("Can't select installed nor candidate version from package '%s' as it has neither of them"), Pkg.FullName(true).c_str());
744 return pkgCache::VerIterator(Cache, 0);
745}
746 /*}}}*/
747// canNotFindInstCandVer /*{{{*/
748pkgCache::VerIterator CacheSetHelper::canNotGetCandInstVer(pkgCacheFile &Cache,
749 pkgCache::PkgIterator const &Pkg) {
750 if (ShowError == true)
751 _error->Insert(ErrorType, _("Can't select installed nor candidate version from package '%s' as it has neither of them"), Pkg.FullName(true).c_str());
752 return pkgCache::VerIterator(Cache, 0);
753}
754 /*}}}*/
755 /*}}}*/
756// showPackageSelection - by selector and given pattern /*{{{*/
2b4cead3 757void CacheSetHelper::showPackageSelection(pkgCache::PkgIterator const &pkg, enum PkgSelector const select,
fdba4d53
DK
758 std::string const &pattern) {
759 switch (select) {
586d8704 760APT_IGNORE_DEPRECATED_PUSH
fdba4d53
DK
761 case REGEX: showRegExSelection(pkg, pattern); break;
762 case TASK: showTaskSelection(pkg, pattern); break;
763 case FNMATCH: showFnmatchSelection(pkg, pattern); break;
586d8704 764APT_IGNORE_DEPRECATED_POP
fdba4d53 765 case PACKAGENAME: /* no suprises here */ break;
1e064088 766 case STRING: /* handled by the special cases */ break;
fdba4d53
DK
767 case UNKNOWN: break;
768 }
769}
15fc8636 770// showTaskSelection /*{{{*/
a02db58f 771APT_CONST void CacheSetHelper::showTaskSelection(pkgCache::PkgIterator const &/*pkg*/,
65512241 772 std::string const &/*pattern*/) {
15fc8636
DK
773}
774 /*}}}*/
775// showRegExSelection /*{{{*/
a02db58f 776APT_CONST void CacheSetHelper::showRegExSelection(pkgCache::PkgIterator const &/*pkg*/,
65512241 777 std::string const &/*pattern*/) {
15fc8636
DK
778}
779 /*}}}*/
16724b66 780// showFnmatchSelection /*{{{*/
b58f28d4
MV
781APT_CONST void CacheSetHelper::showFnmatchSelection(pkgCache::PkgIterator const &/*pkg*/,
782 std::string const &/*pattern*/) {
16724b66
MV
783}
784 /*}}}*/
fdba4d53
DK
785 /*}}}*/
786// showVersionSelection /*{{{*/
2b4cead3 787void CacheSetHelper::showVersionSelection(pkgCache::PkgIterator const &Pkg,
fdba4d53
DK
788 pkgCache::VerIterator const &Ver, enum VerSelector const select, std::string const &pattern) {
789 switch (select) {
586d8704 790APT_IGNORE_DEPRECATED_PUSH
fdba4d53
DK
791 case RELEASE:
792 showSelectedVersion(Pkg, Ver, pattern, true);
793 break;
794 case VERSIONNUMBER:
795 showSelectedVersion(Pkg, Ver, pattern, false);
796 break;
586d8704 797APT_IGNORE_DEPRECATED_POP
fdba4d53
DK
798 case NEWEST:
799 case CANDIDATE:
800 case INSTALLED:
801 case CANDINST:
802 case INSTCAND:
803 case ALL:
804 case CANDANDINST:
805 // not really suprises, but in fact: just not implemented
806 break;
807 }
808}
a02db58f 809APT_CONST void CacheSetHelper::showSelectedVersion(pkgCache::PkgIterator const &/*Pkg*/,
65512241
DK
810 pkgCache::VerIterator const /*Ver*/,
811 std::string const &/*ver*/,
812 bool const /*verIsRel*/) {
15fc8636
DK
813}
814 /*}}}*/
ffee1c2b 815}