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