]> git.saurik.com Git - apt.git/blob - apt-pkg/cacheset.cc
test: framework: Ensure copied status files have trailing lines
[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 std::string const pkgstring = pkg;
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);
274 if (arch == "all" || arch == "native")
275 arch = _config->Find("APT::Architecture");
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
305 pkgCache::PkgIterator Pkg = canNotFindPkgName(Cache, pkgstring);
306 if (Pkg.end() == true)
307 return false;
308
309 pci->insert(Pkg);
310 return true;
311 }
312 /*}}}*/
313 // PackageFromString - Return all packages matching a specific string /*{{{*/
314 bool CacheSetHelper::PackageFromString(PackageContainerInterface * const pci, pkgCacheFile &Cache, std::string const &str) {
315 bool found = true;
316 _error->PushToStack();
317
318 if (PackageFrom(CacheSetHelper::PACKAGENAME, pci, Cache, str) == false &&
319 PackageFrom(CacheSetHelper::TASK, pci, Cache, str) == false &&
320 // FIXME: hm, hm, regexp/fnmatch incompatible?
321 PackageFrom(CacheSetHelper::FNMATCH, pci, Cache, str) == false &&
322 PackageFrom(CacheSetHelper::REGEX, pci, Cache, str) == false)
323 {
324 canNotFindPackage(CacheSetHelper::PACKAGENAME, pci, Cache, str);
325 found = false;
326 }
327
328 if (found == true)
329 _error->RevertToStack();
330 else
331 _error->MergeWithStack();
332 return found;
333 }
334 /*}}}*/
335 // PackageFromCommandLine - Return all packages specified on commandline /*{{{*/
336 bool CacheSetHelper::PackageFromCommandLine(PackageContainerInterface * const pci, pkgCacheFile &Cache, const char **cmdline) {
337 bool found = false;
338 for (const char **I = cmdline; *I != 0; ++I)
339 found |= PackageFrom(CacheSetHelper::STRING, pci, Cache, *I);
340 return found;
341 }
342 /*}}}*/
343 // FromModifierCommandLine - helper doing the work for PKG:GroupedFromCommandLine /*{{{*/
344 bool CacheSetHelper::PackageFromModifierCommandLine(unsigned short &modID, PackageContainerInterface * const pci,
345 pkgCacheFile &Cache, const char * cmdline,
346 std::list<PkgModifier> const &mods) {
347 std::string str = cmdline;
348 unsigned short fallback = modID;
349 bool modifierPresent = false;
350 for (std::list<PkgModifier>::const_iterator mod = mods.begin();
351 mod != mods.end(); ++mod) {
352 size_t const alength = strlen(mod->Alias);
353 switch(mod->Pos) {
354 case PkgModifier::POSTFIX:
355 if (str.compare(str.length() - alength, alength,
356 mod->Alias, 0, alength) != 0)
357 continue;
358 str.erase(str.length() - alength);
359 modID = mod->ID;
360 break;
361 case PkgModifier::PREFIX:
362 continue;
363 case PkgModifier::NONE:
364 continue;
365 }
366 modifierPresent = true;
367 break;
368 }
369 if (modifierPresent == true) {
370 bool const errors = showErrors(false);
371 bool const found = PackageFrom(PACKAGENAME, pci, Cache, cmdline);
372 showErrors(errors);
373 if (found == true) {
374 modID = fallback;
375 return true;
376 }
377 }
378 return PackageFrom(CacheSetHelper::PACKAGENAME, pci, Cache, str);
379 }
380 /*}}}*/
381 // FromModifierCommandLine - helper doing the work for VER:GroupedFromCommandLine /*{{{*/
382 bool VersionContainerInterface::FromModifierCommandLine(unsigned short &modID,
383 VersionContainerInterface * const vci,
384 pkgCacheFile &Cache, const char * cmdline,
385 std::list<Modifier> const &mods,
386 CacheSetHelper &helper) {
387 CacheSetHelper::VerSelector select = CacheSetHelper::NEWEST;
388 std::string str = cmdline;
389 if (unlikely(str.empty() == true))
390 return false;
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:
400 if (str.length() <= alength ||
401 str.compare(str.length() - alength, alength, mod->Alias, 0, alength) != 0)
402 continue;
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;
411 }
412 modifierPresent = true;
413 break;
414 }
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);
419 if (found == true) {
420 modID = fallback;
421 return true;
422 }
423 }
424 return FromString(vci, Cache, str, select, helper);
425 }
426 /*}}}*/
427 // FromCommandLine - Return all versions specified on commandline /*{{{*/
428 bool VersionContainerInterface::FromCommandLine(VersionContainerInterface * const vci,
429 pkgCacheFile &Cache, const char **cmdline,
430 CacheSetHelper::VerSelector const fallback,
431 CacheSetHelper &helper) {
432 bool found = false;
433 for (const char **I = cmdline; *I != 0; ++I)
434 found |= VersionContainerInterface::FromString(vci, Cache, *I, fallback, helper);
435 return found;
436 }
437 /*}}}*/
438 // FromString - Returns all versions spedcified by a string /*{{{*/
439 bool VersionContainerInterface::FromString(VersionContainerInterface * const vci,
440 pkgCacheFile &Cache, std::string pkg,
441 CacheSetHelper::VerSelector const fallback,
442 CacheSetHelper &helper,
443 bool const onlyFromName) {
444 std::string ver;
445 bool verIsRel = false;
446 size_t const vertag = pkg.find_last_of("/=");
447 if (vertag != std::string::npos) {
448 ver = pkg.substr(vertag+1);
449 verIsRel = (pkg[vertag] == '/');
450 pkg.erase(vertag);
451 }
452
453 PackageSet pkgset;
454 if (onlyFromName == false)
455 helper.PackageFrom(CacheSetHelper::STRING, &pkgset, Cache, pkg);
456 else {
457 helper.PackageFrom(CacheSetHelper::PACKAGENAME, &pkgset, Cache, pkg);
458 }
459
460 bool errors = true;
461 if (pkgset.getConstructor() != CacheSetHelper::UNKNOWN)
462 errors = helper.showErrors(false);
463
464 bool found = false;
465 for (PackageSet::const_iterator P = pkgset.begin();
466 P != pkgset.end(); ++P) {
467 if (vertag == std::string::npos) {
468 found |= VersionContainerInterface::FromPackage(vci, Cache, P, fallback, helper);
469 continue;
470 }
471 pkgCache::VerIterator V;
472 if (ver == "installed")
473 V = getInstalledVer(Cache, P, helper);
474 else if (ver == "candidate")
475 V = getCandidateVer(Cache, P, helper);
476 else if (ver == "newest") {
477 if (P->VersionList != 0)
478 V = P.VersionList();
479 else
480 V = helper.canNotGetVersion(CacheSetHelper::NEWEST, Cache, P);
481 } else {
482 pkgVersionMatch Match(ver, (verIsRel == true ? pkgVersionMatch::Release :
483 pkgVersionMatch::Version));
484 V = Match.Find(P);
485 if (V.end() == true) {
486 if (verIsRel == true)
487 _error->Error(_("Release '%s' for '%s' was not found"),
488 ver.c_str(), P.FullName(true).c_str());
489 else
490 _error->Error(_("Version '%s' for '%s' was not found"),
491 ver.c_str(), P.FullName(true).c_str());
492 continue;
493 }
494 }
495 if (V.end() == true)
496 continue;
497 if (verIsRel == true)
498 helper.showVersionSelection(P, V, CacheSetHelper::RELEASE, ver);
499 else
500 helper.showVersionSelection(P, V, CacheSetHelper::VERSIONNUMBER, ver);
501 vci->insert(V);
502 found = true;
503 }
504 if (pkgset.getConstructor() != CacheSetHelper::UNKNOWN)
505 helper.showErrors(errors);
506 return found;
507 }
508 /*}}}*/
509 // FromPackage - versions from package based on fallback /*{{{*/
510 bool VersionContainerInterface::FromPackage(VersionContainerInterface * const vci,
511 pkgCacheFile &Cache,
512 pkgCache::PkgIterator const &P,
513 CacheSetHelper::VerSelector const fallback,
514 CacheSetHelper &helper) {
515 pkgCache::VerIterator V;
516 bool showErrors;
517 bool found = false;
518 switch(fallback) {
519 case CacheSetHelper::ALL:
520 if (P->VersionList != 0)
521 for (V = P.VersionList(); V.end() != true; ++V)
522 found |= vci->insert(V);
523 else
524 helper.canNotFindVersion(CacheSetHelper::ALL, vci, Cache, P);
525 break;
526 case CacheSetHelper::CANDANDINST:
527 found |= vci->insert(getInstalledVer(Cache, P, helper));
528 found |= vci->insert(getCandidateVer(Cache, P, helper));
529 break;
530 case CacheSetHelper::CANDIDATE:
531 found |= vci->insert(getCandidateVer(Cache, P, helper));
532 break;
533 case CacheSetHelper::INSTALLED:
534 found |= vci->insert(getInstalledVer(Cache, P, helper));
535 break;
536 case CacheSetHelper::CANDINST:
537 showErrors = helper.showErrors(false);
538 V = getCandidateVer(Cache, P, helper);
539 if (V.end() == true)
540 V = getInstalledVer(Cache, P, helper);
541 helper.showErrors(showErrors);
542 if (V.end() == false)
543 found |= vci->insert(V);
544 else
545 helper.canNotFindVersion(CacheSetHelper::CANDINST, vci, Cache, P);
546 break;
547 case CacheSetHelper::INSTCAND:
548 showErrors = helper.showErrors(false);
549 V = getInstalledVer(Cache, P, helper);
550 if (V.end() == true)
551 V = getCandidateVer(Cache, P, helper);
552 helper.showErrors(showErrors);
553 if (V.end() == false)
554 found |= vci->insert(V);
555 else
556 helper.canNotFindVersion(CacheSetHelper::INSTCAND, vci, Cache, P);
557 break;
558 case CacheSetHelper::NEWEST:
559 if (P->VersionList != 0)
560 found |= vci->insert(P.VersionList());
561 else
562 helper.canNotFindVersion(CacheSetHelper::NEWEST, vci, Cache, P);
563 break;
564 case CacheSetHelper::RELEASE:
565 case CacheSetHelper::VERSIONNUMBER:
566 // both make no sense here, so always false
567 return false;
568 }
569 return found;
570 }
571 /*}}}*/
572 // FromDependency - versions satisfying a given dependency /*{{{*/
573 bool VersionContainerInterface::FromDependency(VersionContainerInterface * const vci,
574 pkgCacheFile &Cache,
575 pkgCache::DepIterator const &D,
576 CacheSetHelper::VerSelector const selector,
577 CacheSetHelper &helper)
578 {
579 bool found = false;
580 switch(selector) {
581 case CacheSetHelper::ALL:
582 {
583 pkgCache::PkgIterator const T = D.TargetPkg();
584 for (pkgCache::VerIterator Ver = T.VersionList(); Ver.end() == false; ++Ver)
585 {
586 if (D.IsSatisfied(Ver) == true)
587 {
588 vci->insert(Ver);
589 found = true;
590 }
591 for (pkgCache::PrvIterator Prv = T.ProvidesList(); Prv.end() != true; ++Prv)
592 {
593 pkgCache::VerIterator const V = Prv.OwnerVer();
594 if (unlikely(V.end() == true) || D.IsSatisfied(Prv) == false)
595 continue;
596 vci->insert(V);
597 found = true;
598 }
599 }
600 return found;
601 }
602 case CacheSetHelper::CANDANDINST:
603 {
604 found = FromDependency(vci, Cache, D, CacheSetHelper::CANDIDATE, helper);
605 found &= FromDependency(vci, Cache, D, CacheSetHelper::INSTALLED, helper);
606 return found;
607 }
608 case CacheSetHelper::CANDIDATE:
609 {
610 pkgCache::PkgIterator const T = D.TargetPkg();
611 pkgCache::VerIterator const Cand = Cache[T].CandidateVerIter(Cache);
612 if (Cand.end() == false && D.IsSatisfied(Cand) == true)
613 {
614 vci->insert(Cand);
615 found = true;
616 }
617 for (pkgCache::PrvIterator Prv = T.ProvidesList(); Prv.end() != true; ++Prv)
618 {
619 pkgCache::VerIterator const V = Prv.OwnerVer();
620 pkgCache::VerIterator const Cand = Cache[Prv.OwnerPkg()].CandidateVerIter(Cache);
621 if (Cand.end() == true || V != Cand || D.IsSatisfied(Prv) == false)
622 continue;
623 vci->insert(Cand);
624 found = true;
625 }
626 return found;
627 }
628 case CacheSetHelper::INSTALLED:
629 {
630 pkgCache::PkgIterator const T = D.TargetPkg();
631 pkgCache::VerIterator const Cand = T.CurrentVer();
632 if (Cand.end() == false && D.IsSatisfied(Cand) == true)
633 {
634 vci->insert(Cand);
635 found = true;
636 }
637 for (pkgCache::PrvIterator Prv = T.ProvidesList(); Prv.end() != true; ++Prv)
638 {
639 pkgCache::VerIterator const V = Prv.OwnerVer();
640 pkgCache::VerIterator const Cand = Prv.OwnerPkg().CurrentVer();
641 if (Cand.end() == true || V != Cand || D.IsSatisfied(Prv) == false)
642 continue;
643 vci->insert(Cand);
644 found = true;
645 }
646 return found;
647 }
648 case CacheSetHelper::CANDINST:
649 return FromDependency(vci, Cache, D, CacheSetHelper::CANDIDATE, helper) ||
650 FromDependency(vci, Cache, D, CacheSetHelper::INSTALLED, helper);
651 case CacheSetHelper::INSTCAND:
652 return FromDependency(vci, Cache, D, CacheSetHelper::INSTALLED, helper) ||
653 FromDependency(vci, Cache, D, CacheSetHelper::CANDIDATE, helper);
654 case CacheSetHelper::NEWEST:
655 {
656 pkgCache::PkgIterator const T = D.TargetPkg();
657 pkgCache::VerIterator const Cand = T.VersionList();
658 if (Cand.end() == false && D.IsSatisfied(Cand) == true)
659 {
660 vci->insert(Cand);
661 found = true;
662 }
663 for (pkgCache::PrvIterator Prv = T.ProvidesList(); Prv.end() != true; ++Prv)
664 {
665 pkgCache::VerIterator const V = Prv.OwnerVer();
666 pkgCache::VerIterator const Cand = Prv.OwnerPkg().VersionList();
667 if (Cand.end() == true || V != Cand || D.IsSatisfied(Prv) == false)
668 continue;
669 vci->insert(Cand);
670 found = true;
671 }
672 return found;
673 }
674 case CacheSetHelper::RELEASE:
675 case CacheSetHelper::VERSIONNUMBER:
676 // both make no sense here, so always false
677 return false;
678 }
679 return found;
680 }
681 /*}}}*/
682 // getCandidateVer - Returns the candidate version of the given package /*{{{*/
683 pkgCache::VerIterator VersionContainerInterface::getCandidateVer(pkgCacheFile &Cache,
684 pkgCache::PkgIterator const &Pkg, CacheSetHelper &helper) {
685 pkgCache::VerIterator Cand;
686 if (Cache.IsDepCacheBuilt() == true) {
687 Cand = Cache[Pkg].CandidateVerIter(Cache);
688 } else if (unlikely(Cache.GetPolicy() == nullptr)) {
689 return pkgCache::VerIterator(Cache);
690 } else {
691 Cand = Cache.GetPolicy()->GetCandidateVer(Pkg);
692 }
693 if (Cand.end() == true)
694 return helper.canNotGetVersion(CacheSetHelper::CANDIDATE, Cache, Pkg);
695 return Cand;
696 }
697 /*}}}*/
698 // getInstalledVer - Returns the installed version of the given package /*{{{*/
699 pkgCache::VerIterator VersionContainerInterface::getInstalledVer(pkgCacheFile &Cache,
700 pkgCache::PkgIterator const &Pkg, CacheSetHelper &helper) {
701 if (Pkg->CurrentVer == 0)
702 return helper.canNotGetVersion(CacheSetHelper::INSTALLED, Cache, Pkg);
703 return Pkg.CurrentVer();
704 }
705 /*}}}*/
706
707 // canNotFindPackage - with the given selector and pattern /*{{{*/
708 void CacheSetHelper::canNotFindPackage(enum PkgSelector const select,
709 PackageContainerInterface * const pci, pkgCacheFile &Cache,
710 std::string const &pattern) {
711 switch (select) {
712 APT_IGNORE_DEPRECATED_PUSH
713 case REGEX: canNotFindRegEx(pci, Cache, pattern); break;
714 case TASK: canNotFindTask(pci, Cache, pattern); break;
715 case FNMATCH: canNotFindFnmatch(pci, Cache, pattern); break;
716 case PACKAGENAME: canNotFindPackage(pci, Cache, pattern); break;
717 case STRING: canNotFindPackage(pci, Cache, pattern); break;
718 case UNKNOWN: break;
719 APT_IGNORE_DEPRECATED_POP
720 }
721 }
722 // canNotFindTask - handle the case no package is found for a task /*{{{*/
723 void CacheSetHelper::canNotFindTask(PackageContainerInterface * const /*pci*/, pkgCacheFile &/*Cache*/, std::string pattern) {
724 if (ShowError == true)
725 _error->Insert(ErrorType, _("Couldn't find task '%s'"), pattern.c_str());
726 }
727 /*}}}*/
728 // canNotFindRegEx - handle the case no package is found by a regex /*{{{*/
729 void CacheSetHelper::canNotFindRegEx(PackageContainerInterface * const /*pci*/, pkgCacheFile &/*Cache*/, std::string pattern) {
730 if (ShowError == true)
731 _error->Insert(ErrorType, _("Couldn't find any package by regex '%s'"), pattern.c_str());
732 }
733 /*}}}*/
734 // canNotFindFnmatch - handle the case no package is found by a fnmatch /*{{{*/
735 void CacheSetHelper::canNotFindFnmatch(PackageContainerInterface * const /*pci*/, pkgCacheFile &/*Cache*/, std::string pattern) {
736 if (ShowError == true)
737 _error->Insert(ErrorType, _("Couldn't find any package by glob '%s'"), pattern.c_str());
738 }
739 /*}}}*/
740 // canNotFindPackage - handle the case no package is found from a string/*{{{*/
741 APT_CONST void CacheSetHelper::canNotFindPackage(PackageContainerInterface * const /*pci*/, pkgCacheFile &/*Cache*/, std::string const &/*str*/) {
742 }
743 /*}}}*/
744 /*}}}*/
745 // canNotFindPkgName - handle the case no package has this name /*{{{*/
746 pkgCache::PkgIterator CacheSetHelper::canNotFindPkgName(pkgCacheFile &Cache,
747 std::string const &str) {
748 if (ShowError == true)
749 _error->Insert(ErrorType, _("Unable to locate package %s"), str.c_str());
750 return pkgCache::PkgIterator(Cache, 0);
751 }
752 /*}}}*/
753 // canNotFindVersion - for package by selector /*{{{*/
754 void CacheSetHelper::canNotFindVersion(enum VerSelector const select, VersionContainerInterface * const vci, pkgCacheFile &Cache, pkgCache::PkgIterator const &Pkg)
755 {
756 switch (select) {
757 APT_IGNORE_DEPRECATED_PUSH
758 case ALL: canNotFindAllVer(vci, Cache, Pkg); break;
759 case INSTCAND: canNotFindInstCandVer(vci, Cache, Pkg); break;
760 case CANDINST: canNotFindCandInstVer(vci, Cache, Pkg); break;
761 case NEWEST: canNotFindNewestVer(Cache, Pkg); break;
762 case CANDIDATE: canNotFindCandidateVer(Cache, Pkg); break;
763 case INSTALLED: canNotFindInstalledVer(Cache, Pkg); break;
764 APT_IGNORE_DEPRECATED_POP
765 case CANDANDINST: canNotGetCandInstVer(Cache, Pkg); break;
766 case RELEASE:
767 case VERSIONNUMBER:
768 // invalid in this branch
769 break;
770 }
771 }
772 // canNotFindAllVer /*{{{*/
773 void CacheSetHelper::canNotFindAllVer(VersionContainerInterface * const /*vci*/, pkgCacheFile &/*Cache*/,
774 pkgCache::PkgIterator const &Pkg) {
775 if (ShowError == true)
776 _error->Insert(ErrorType, _("Can't select versions from package '%s' as it is purely virtual"), Pkg.FullName(true).c_str());
777 }
778 /*}}}*/
779 // canNotFindInstCandVer /*{{{*/
780 void CacheSetHelper::canNotFindInstCandVer(VersionContainerInterface * const /*vci*/, pkgCacheFile &Cache,
781 pkgCache::PkgIterator const &Pkg) {
782 canNotGetInstCandVer(Cache, Pkg);
783 }
784 /*}}}*/
785 // canNotFindInstCandVer /*{{{*/
786 void CacheSetHelper::canNotFindCandInstVer(VersionContainerInterface * const /*vci*/, pkgCacheFile &Cache,
787 pkgCache::PkgIterator const &Pkg) {
788 canNotGetCandInstVer(Cache, Pkg);
789 }
790 /*}}}*/
791 /*}}}*/
792 // canNotGetVersion - for package by selector /*{{{*/
793 pkgCache::VerIterator CacheSetHelper::canNotGetVersion(enum VerSelector const select, pkgCacheFile &Cache, pkgCache::PkgIterator const &Pkg) {
794 switch (select) {
795 APT_IGNORE_DEPRECATED_PUSH
796 case NEWEST: return canNotFindNewestVer(Cache, Pkg);
797 case CANDIDATE: return canNotFindCandidateVer(Cache, Pkg);
798 case INSTALLED: return canNotFindInstalledVer(Cache, Pkg);
799 APT_IGNORE_DEPRECATED_POP
800 case CANDINST: return canNotGetCandInstVer(Cache, Pkg);
801 case INSTCAND: return canNotGetInstCandVer(Cache, Pkg);
802 case ALL:
803 case CANDANDINST:
804 case RELEASE:
805 case VERSIONNUMBER:
806 // invalid in this branch
807 return pkgCache::VerIterator(Cache, 0);
808 }
809 return pkgCache::VerIterator(Cache, 0);
810 }
811 // canNotFindNewestVer /*{{{*/
812 pkgCache::VerIterator CacheSetHelper::canNotFindNewestVer(pkgCacheFile &Cache,
813 pkgCache::PkgIterator const &Pkg) {
814 if (ShowError == true)
815 _error->Insert(ErrorType, _("Can't select newest version from package '%s' as it is purely virtual"), Pkg.FullName(true).c_str());
816 return pkgCache::VerIterator(Cache, 0);
817 }
818 /*}}}*/
819 // canNotFindCandidateVer /*{{{*/
820 pkgCache::VerIterator CacheSetHelper::canNotFindCandidateVer(pkgCacheFile &Cache,
821 pkgCache::PkgIterator const &Pkg) {
822 if (ShowError == true)
823 _error->Insert(ErrorType, _("Can't select candidate version from package %s as it has no candidate"), Pkg.FullName(true).c_str());
824 return pkgCache::VerIterator(Cache, 0);
825 }
826 /*}}}*/
827 // canNotFindInstalledVer /*{{{*/
828 pkgCache::VerIterator CacheSetHelper::canNotFindInstalledVer(pkgCacheFile &Cache,
829 pkgCache::PkgIterator const &Pkg) {
830 if (ShowError == true)
831 _error->Insert(ErrorType, _("Can't select installed version from package %s as it is not installed"), Pkg.FullName(true).c_str());
832 return pkgCache::VerIterator(Cache, 0);
833 }
834 /*}}}*/
835 // canNotFindInstCandVer /*{{{*/
836 pkgCache::VerIterator CacheSetHelper::canNotGetInstCandVer(pkgCacheFile &Cache,
837 pkgCache::PkgIterator const &Pkg) {
838 if (ShowError == true)
839 _error->Insert(ErrorType, _("Can't select installed nor candidate version from package '%s' as it has neither of them"), Pkg.FullName(true).c_str());
840 return pkgCache::VerIterator(Cache, 0);
841 }
842 /*}}}*/
843 // canNotFindInstCandVer /*{{{*/
844 pkgCache::VerIterator CacheSetHelper::canNotGetCandInstVer(pkgCacheFile &Cache,
845 pkgCache::PkgIterator const &Pkg) {
846 if (ShowError == true)
847 _error->Insert(ErrorType, _("Can't select installed nor candidate version from package '%s' as it has neither of them"), Pkg.FullName(true).c_str());
848 return pkgCache::VerIterator(Cache, 0);
849 }
850 /*}}}*/
851 /*}}}*/
852 // showPackageSelection - by selector and given pattern /*{{{*/
853 void CacheSetHelper::showPackageSelection(pkgCache::PkgIterator const &pkg, enum PkgSelector const select,
854 std::string const &pattern) {
855 switch (select) {
856 APT_IGNORE_DEPRECATED_PUSH
857 case REGEX: showRegExSelection(pkg, pattern); break;
858 case TASK: showTaskSelection(pkg, pattern); break;
859 case FNMATCH: showFnmatchSelection(pkg, pattern); break;
860 APT_IGNORE_DEPRECATED_POP
861 case PACKAGENAME: /* no suprises here */ break;
862 case STRING: /* handled by the special cases */ break;
863 case UNKNOWN: break;
864 }
865 }
866 // showTaskSelection /*{{{*/
867 APT_CONST void CacheSetHelper::showTaskSelection(pkgCache::PkgIterator const &/*pkg*/,
868 std::string const &/*pattern*/) {
869 }
870 /*}}}*/
871 // showRegExSelection /*{{{*/
872 APT_CONST void CacheSetHelper::showRegExSelection(pkgCache::PkgIterator const &/*pkg*/,
873 std::string const &/*pattern*/) {
874 }
875 /*}}}*/
876 // showFnmatchSelection /*{{{*/
877 APT_CONST void CacheSetHelper::showFnmatchSelection(pkgCache::PkgIterator const &/*pkg*/,
878 std::string const &/*pattern*/) {
879 }
880 /*}}}*/
881 /*}}}*/
882 // showVersionSelection /*{{{*/
883 void CacheSetHelper::showVersionSelection(pkgCache::PkgIterator const &Pkg,
884 pkgCache::VerIterator const &Ver, enum VerSelector const select, std::string const &pattern) {
885 switch (select) {
886 APT_IGNORE_DEPRECATED_PUSH
887 case RELEASE:
888 showSelectedVersion(Pkg, Ver, pattern, true);
889 break;
890 case VERSIONNUMBER:
891 showSelectedVersion(Pkg, Ver, pattern, false);
892 break;
893 APT_IGNORE_DEPRECATED_POP
894 case NEWEST:
895 case CANDIDATE:
896 case INSTALLED:
897 case CANDINST:
898 case INSTCAND:
899 case ALL:
900 case CANDANDINST:
901 // not really suprises, but in fact: just not implemented
902 break;
903 }
904 }
905 APT_CONST void CacheSetHelper::showSelectedVersion(pkgCache::PkgIterator const &/*Pkg*/,
906 pkgCache::VerIterator const /*Ver*/,
907 std::string const &/*ver*/,
908 bool const /*verIsRel*/) {
909 }
910 /*}}}*/
911
912 CacheSetHelper::CacheSetHelper(bool const ShowError, GlobalError::MsgType ErrorType) :
913 ShowError(ShowError), ErrorType(ErrorType), d(NULL) {}
914 CacheSetHelper::~CacheSetHelper() {}
915
916 PackageContainerInterface::PackageContainerInterface() : ConstructedBy(CacheSetHelper::UNKNOWN), d(NULL) {}
917 PackageContainerInterface::PackageContainerInterface(CacheSetHelper::PkgSelector const by) : ConstructedBy(by), d(NULL) {}
918 PackageContainerInterface& PackageContainerInterface::operator=(PackageContainerInterface const &other) {
919 if (this != &other)
920 this->ConstructedBy = other.ConstructedBy;
921 return *this;
922 }
923 PackageContainerInterface::~PackageContainerInterface() {}
924
925 PackageUniverse::PackageUniverse(pkgCache * const Owner) : _cont(Owner), d(NULL) {}
926 PackageUniverse::PackageUniverse(pkgCacheFile * const Owner) : _cont(Owner->GetPkgCache()), d(NULL) {}
927 PackageUniverse::~PackageUniverse() {}
928
929 VersionContainerInterface::VersionContainerInterface() : d(NULL) {}
930 VersionContainerInterface& VersionContainerInterface::operator=(VersionContainerInterface const &) {
931 return *this;
932 }
933
934 VersionContainerInterface::~VersionContainerInterface() {}
935 }