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