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