* apt-pkg/cacheset.cc:
[ntk/apt.git] / apt-pkg / cacheset.cc
CommitLineData
ffee1c2b
DK
1// -*- mode: cpp; mode: fold -*-
2// Description /*{{{*/
3/* ######################################################################
4
5 Simple wrapper around a std::set to provide a similar interface to
7959c5ed
DK
6 a set of cache structures as to the complete set of all structures
7 in the pkgCache. Currently only Package is supported.
ffee1c2b
DK
8
9 ##################################################################### */
10 /*}}}*/
11// Include Files /*{{{*/
78c32596 12#include <apt-pkg/aptconfiguration.h>
9ba5aa3b 13#include <apt-pkg/cachefilter.h>
8fde7239 14#include <apt-pkg/cacheset.h>
ffee1c2b 15#include <apt-pkg/error.h>
ffee1c2b 16#include <apt-pkg/strutl.h>
856d3b06 17#include <apt-pkg/versionmatch.h>
ffee1c2b
DK
18
19#include <apti18n.h>
20
78c32596
DK
21#include <vector>
22
ffee1c2b
DK
23#include <regex.h>
24 /*}}}*/
25namespace APT {
dc0f01f7 26// FromTask - Return all packages in the cache from a specific task /*{{{*/
70e706ad 27PackageSet PackageSet::FromTask(pkgCacheFile &Cache, std::string pattern, CacheSetHelper &helper) {
bd631595 28 size_t const archfound = pattern.find_last_of(':');
dc0f01f7
DK
29 std::string arch = "native";
30 if (archfound != std::string::npos) {
31 arch = pattern.substr(archfound+1);
32 pattern.erase(archfound);
33 }
34
35 if (pattern[pattern.length() -1] != '^')
c8db3fff 36 return APT::PackageSet(TASK);
dc0f01f7
DK
37 pattern.erase(pattern.length()-1);
38
bd631595 39 if (unlikely(Cache.GetPkgCache() == 0 || Cache.GetDepCache() == 0))
c8db3fff 40 return APT::PackageSet(TASK);
bd631595 41
c8db3fff 42 PackageSet pkgset(TASK);
dc0f01f7
DK
43 // get the records
44 pkgRecords Recs(Cache);
45
46 // build regexp for the task
47 regex_t Pattern;
48 char S[300];
49 snprintf(S, sizeof(S), "^Task:.*[, ]%s([, ]|$)", pattern.c_str());
50 if(regcomp(&Pattern,S, REG_EXTENDED | REG_NOSUB | REG_NEWLINE) != 0) {
51 _error->Error("Failed to compile task regexp");
52 return pkgset;
53 }
54
55 for (pkgCache::GrpIterator Grp = Cache->GrpBegin(); Grp.end() == false; ++Grp) {
56 pkgCache::PkgIterator Pkg = Grp.FindPkg(arch);
57 if (Pkg.end() == true)
58 continue;
59 pkgCache::VerIterator ver = Cache[Pkg].CandidateVerIter(Cache);
60 if(ver.end() == true)
61 continue;
62
63 pkgRecords::Parser &parser = Recs.Lookup(ver.FileList());
64 const char *start, *end;
65 parser.GetRec(start,end);
66 unsigned int const length = end - start;
67 char buf[length];
68 strncpy(buf, start, length);
69 buf[length-1] = '\0';
70e706ad
DK
70 if (regexec(&Pattern, buf, 0, 0, 0) != 0)
71 continue;
72
73 pkgset.insert(Pkg);
dc0f01f7 74 }
70e706ad 75 regfree(&Pattern);
dc0f01f7
DK
76
77 if (pkgset.empty() == true)
70e706ad 78 return helper.canNotFindTask(Cache, pattern);
dc0f01f7 79
70e706ad 80 helper.showTaskSelection(pkgset, pattern);
dc0f01f7
DK
81 return pkgset;
82}
83 /*}}}*/
ffee1c2b 84// FromRegEx - Return all packages in the cache matching a pattern /*{{{*/
70e706ad 85PackageSet PackageSet::FromRegEx(pkgCacheFile &Cache, std::string pattern, CacheSetHelper &helper) {
6e235c66 86 static const char * const isregex = ".?+*|[^$";
6e235c66 87 if (pattern.find_first_of(isregex) == std::string::npos)
c8db3fff 88 return PackageSet(REGEX);
ffee1c2b 89
6e235c66 90 size_t archfound = pattern.find_last_of(':');
dc0f01f7 91 std::string arch = "native";
6e235c66
DK
92 if (archfound != std::string::npos) {
93 arch = pattern.substr(archfound+1);
94 if (arch.find_first_of(isregex) == std::string::npos)
95 pattern.erase(archfound);
96 else
97 arch = "native";
98 }
99
bd631595 100 if (unlikely(Cache.GetPkgCache() == 0))
c8db3fff 101 return PackageSet(REGEX);
bd631595 102
9ba5aa3b
DK
103 APT::CacheFilter::PackageNameMatchesRegEx regexfilter(pattern);
104
c8db3fff 105 PackageSet pkgset(REGEX);
9ba5aa3b
DK
106 for (pkgCache::GrpIterator Grp = Cache.GetPkgCache()->GrpBegin(); Grp.end() == false; ++Grp) {
107 if (regexfilter(Grp) == false)
ffee1c2b 108 continue;
6e235c66 109 pkgCache::PkgIterator Pkg = Grp.FindPkg(arch);
78c32596 110 if (Pkg.end() == true) {
6e235c66
DK
111 if (archfound == std::string::npos) {
112 std::vector<std::string> archs = APT::Configuration::getArchitectures();
113 for (std::vector<std::string>::const_iterator a = archs.begin();
114 a != archs.end() && Pkg.end() != true; ++a)
115 Pkg = Grp.FindPkg(*a);
78c32596
DK
116 }
117 if (Pkg.end() == true)
118 continue;
119 }
ffee1c2b 120
ffee1c2b
DK
121 pkgset.insert(Pkg);
122 }
ffee1c2b 123
70e706ad
DK
124 if (pkgset.empty() == true)
125 return helper.canNotFindRegEx(Cache, pattern);
126
127 helper.showRegExSelection(pkgset, pattern);
78c32596
DK
128 return pkgset;
129}
130 /*}}}*/
bd631595
DK
131// FromName - Returns the package defined by this string /*{{{*/
132pkgCache::PkgIterator PackageSet::FromName(pkgCacheFile &Cache,
133 std::string const &str, CacheSetHelper &helper) {
134 std::string pkg = str;
135 size_t archfound = pkg.find_last_of(':');
136 std::string arch;
137 if (archfound != std::string::npos) {
138 arch = pkg.substr(archfound+1);
139 pkg.erase(archfound);
140 }
141
142 if (Cache.GetPkgCache() == 0)
143 return pkgCache::PkgIterator(Cache, 0);
144
145 pkgCache::PkgIterator Pkg(Cache, 0);
146 if (arch.empty() == true) {
147 pkgCache::GrpIterator Grp = Cache.GetPkgCache()->FindGrp(pkg);
148 if (Grp.end() == false)
149 Pkg = Grp.FindPreferredPkg();
150 } else
151 Pkg = Cache.GetPkgCache()->FindPkg(pkg, arch);
152
153 if (Pkg.end() == true)
154 return helper.canNotFindPkgName(Cache, str);
155 return Pkg;
156}
157 /*}}}*/
9cc83a6f
DK
158// GroupedFromCommandLine - Return all versions specified on commandline/*{{{*/
159std::map<unsigned short, PackageSet> PackageSet::GroupedFromCommandLine(
160 pkgCacheFile &Cache, const char **cmdline,
161 std::list<PackageSet::Modifier> const &mods,
70e706ad 162 unsigned short const &fallback, CacheSetHelper &helper) {
9cc83a6f
DK
163 std::map<unsigned short, PackageSet> pkgsets;
164 for (const char **I = cmdline; *I != 0; ++I) {
165 unsigned short modID = fallback;
166 std::string str = *I;
bd631595 167 bool modifierPresent = false;
9cc83a6f
DK
168 for (std::list<PackageSet::Modifier>::const_iterator mod = mods.begin();
169 mod != mods.end(); ++mod) {
170 size_t const alength = strlen(mod->Alias);
171 switch(mod->Pos) {
172 case PackageSet::Modifier::POSTFIX:
173 if (str.compare(str.length() - alength, alength,
55c59998 174 mod->Alias, 0, alength) != 0)
9cc83a6f
DK
175 continue;
176 str.erase(str.length() - alength);
177 modID = mod->ID;
178 break;
179 case PackageSet::Modifier::PREFIX:
180 continue;
181 case PackageSet::Modifier::NONE:
182 continue;
183 }
bd631595 184 modifierPresent = true;
9cc83a6f
DK
185 break;
186 }
bd631595
DK
187 if (modifierPresent == true) {
188 bool const errors = helper.showErrors(false);
189 pkgCache::PkgIterator Pkg = FromName(Cache, *I, helper);
190 helper.showErrors(errors);
191 if (Pkg.end() == false) {
192 pkgsets[fallback].insert(Pkg);
193 continue;
194 }
195 }
70e706ad 196 pkgsets[modID].insert(PackageSet::FromString(Cache, str, helper));
9cc83a6f
DK
197 }
198 return pkgsets;
199}
200 /*}}}*/
78c32596 201// FromCommandLine - Return all packages specified on commandline /*{{{*/
70e706ad 202PackageSet PackageSet::FromCommandLine(pkgCacheFile &Cache, const char **cmdline, CacheSetHelper &helper) {
78c32596 203 PackageSet pkgset;
9f1f17cc 204 for (const char **I = cmdline; *I != 0; ++I) {
70e706ad 205 PackageSet pset = FromString(Cache, *I, helper);
856d3b06
DK
206 pkgset.insert(pset.begin(), pset.end());
207 }
208 return pkgset;
209}
210 /*}}}*/
211// FromString - Return all packages matching a specific string /*{{{*/
70e706ad 212PackageSet PackageSet::FromString(pkgCacheFile &Cache, std::string const &str, CacheSetHelper &helper) {
48c39e32
DK
213 _error->PushToStack();
214
bd631595
DK
215 PackageSet pkgset;
216 pkgCache::PkgIterator Pkg = FromName(Cache, str, helper);
217 if (Pkg.end() == false)
218 pkgset.insert(Pkg);
219 else {
220 pkgset = FromTask(Cache, str, helper);
221 if (pkgset.empty() == true) {
222 pkgset = FromRegEx(Cache, str, helper);
223 if (pkgset.empty() == true)
224 pkgset = helper.canNotFindPackage(Cache, str);
225 }
48c39e32 226 }
dc0f01f7 227
bd631595 228 if (pkgset.empty() == false)
48c39e32
DK
229 _error->RevertToStack();
230 else
231 _error->MergeWithStack();
bd631595 232 return pkgset;
856d3b06
DK
233}
234 /*}}}*/
55c59998
DK
235// GroupedFromCommandLine - Return all versions specified on commandline/*{{{*/
236std::map<unsigned short, VersionSet> VersionSet::GroupedFromCommandLine(
237 pkgCacheFile &Cache, const char **cmdline,
238 std::list<VersionSet::Modifier> const &mods,
70e706ad 239 unsigned short const &fallback, CacheSetHelper &helper) {
55c59998
DK
240 std::map<unsigned short, VersionSet> versets;
241 for (const char **I = cmdline; *I != 0; ++I) {
242 unsigned short modID = fallback;
243 VersionSet::Version select = VersionSet::NEWEST;
244 std::string str = *I;
bd631595 245 bool modifierPresent = false;
55c59998
DK
246 for (std::list<VersionSet::Modifier>::const_iterator mod = mods.begin();
247 mod != mods.end(); ++mod) {
248 if (modID == fallback && mod->ID == fallback)
249 select = mod->SelectVersion;
250 size_t const alength = strlen(mod->Alias);
251 switch(mod->Pos) {
252 case VersionSet::Modifier::POSTFIX:
253 if (str.compare(str.length() - alength, alength,
254 mod->Alias, 0, alength) != 0)
255 continue;
256 str.erase(str.length() - alength);
257 modID = mod->ID;
258 select = mod->SelectVersion;
259 break;
260 case VersionSet::Modifier::PREFIX:
261 continue;
262 case VersionSet::Modifier::NONE:
263 continue;
264 }
bd631595 265 modifierPresent = true;
55c59998
DK
266 break;
267 }
bd631595
DK
268
269 if (modifierPresent == true) {
270 bool const errors = helper.showErrors(false);
271 VersionSet const vset = VersionSet::FromString(Cache, std::string(*I), select, helper, true);
272 helper.showErrors(errors);
273 if (vset.empty() == false) {
274 versets[fallback].insert(vset);
275 continue;
276 }
277 }
70e706ad 278 versets[modID].insert(VersionSet::FromString(Cache, str, select , helper));
55c59998
DK
279 }
280 return versets;
281}
282 /*}}}*/
856d3b06
DK
283// FromCommandLine - Return all versions specified on commandline /*{{{*/
284APT::VersionSet VersionSet::FromCommandLine(pkgCacheFile &Cache, const char **cmdline,
70e706ad 285 APT::VersionSet::Version const &fallback, CacheSetHelper &helper) {
856d3b06 286 VersionSet verset;
bd631595
DK
287 for (const char **I = cmdline; *I != 0; ++I)
288 verset.insert(VersionSet::FromString(Cache, *I, fallback, helper));
55c59998
DK
289 return verset;
290}
291 /*}}}*/
292// FromString - Returns all versions spedcified by a string /*{{{*/
293APT::VersionSet VersionSet::FromString(pkgCacheFile &Cache, std::string pkg,
bd631595
DK
294 APT::VersionSet::Version const &fallback, CacheSetHelper &helper,
295 bool const &onlyFromName) {
55c59998
DK
296 std::string ver;
297 bool verIsRel = false;
298 size_t const vertag = pkg.find_last_of("/=");
299 if (vertag != string::npos) {
300 ver = pkg.substr(vertag+1);
301 verIsRel = (pkg[vertag] == '/');
302 pkg.erase(vertag);
303 }
bd631595
DK
304 PackageSet pkgset;
305 if (onlyFromName == false)
306 pkgset = PackageSet::FromString(Cache, pkg, helper);
307 else {
308 pkgset.insert(PackageSet::FromName(Cache, pkg, helper));
309 }
310
55c59998 311 VersionSet verset;
c8db3fff
DK
312 bool errors = true;
313 if (pkgset.getConstructor() != PackageSet::UNKNOWN)
314 errors = helper.showErrors(false);
55c59998
DK
315 for (PackageSet::const_iterator P = pkgset.begin();
316 P != pkgset.end(); ++P) {
317 if (vertag == string::npos) {
fb83c1d0 318 verset.insert(VersionSet::FromPackage(Cache, P, fallback, helper));
55c59998 319 continue;
856d3b06 320 }
55c59998
DK
321 pkgCache::VerIterator V;
322 if (ver == "installed")
70e706ad 323 V = getInstalledVer(Cache, P, helper);
55c59998 324 else if (ver == "candidate")
70e706ad 325 V = getCandidateVer(Cache, P, helper);
f1a58ff8
DK
326 else if (ver == "newest") {
327 if (P->VersionList != 0)
328 V = P.VersionList();
329 else
330 V = helper.canNotFindNewestVer(Cache, P);
331 } else {
55c59998
DK
332 pkgVersionMatch Match(ver, (verIsRel == true ? pkgVersionMatch::Release :
333 pkgVersionMatch::Version));
334 V = Match.Find(P);
335 if (V.end() == true) {
336 if (verIsRel == true)
337 _error->Error(_("Release '%s' for '%s' was not found"),
338 ver.c_str(), P.FullName(true).c_str());
339 else
340 _error->Error(_("Version '%s' for '%s' was not found"),
341 ver.c_str(), P.FullName(true).c_str());
84910ad5
DK
342 continue;
343 }
78c32596 344 }
55c59998
DK
345 if (V.end() == true)
346 continue;
70e706ad 347 helper.showSelectedVersion(P, V, ver, verIsRel);
55c59998 348 verset.insert(V);
78c32596 349 }
c8db3fff
DK
350 if (pkgset.getConstructor() != PackageSet::UNKNOWN)
351 helper.showErrors(errors);
856d3b06
DK
352 return verset;
353}
354 /*}}}*/
fb83c1d0
DK
355// FromPackage - versions from package based on fallback /*{{{*/
356VersionSet VersionSet::FromPackage(pkgCacheFile &Cache, pkgCache::PkgIterator const &P,
357 VersionSet::Version const &fallback, CacheSetHelper &helper) {
358 VersionSet verset;
84910ad5 359 pkgCache::VerIterator V;
70e706ad 360 bool showErrors;
84910ad5
DK
361 switch(fallback) {
362 case VersionSet::ALL:
363 if (P->VersionList != 0)
364 for (V = P.VersionList(); V.end() != true; ++V)
365 verset.insert(V);
84910ad5 366 else
70e706ad 367 verset.insert(helper.canNotFindAllVer(Cache, P));
84910ad5
DK
368 break;
369 case VersionSet::CANDANDINST:
70e706ad
DK
370 verset.insert(getInstalledVer(Cache, P, helper));
371 verset.insert(getCandidateVer(Cache, P, helper));
84910ad5
DK
372 break;
373 case VersionSet::CANDIDATE:
70e706ad 374 verset.insert(getCandidateVer(Cache, P, helper));
84910ad5
DK
375 break;
376 case VersionSet::INSTALLED:
70e706ad 377 verset.insert(getInstalledVer(Cache, P, helper));
84910ad5
DK
378 break;
379 case VersionSet::CANDINST:
70e706ad
DK
380 showErrors = helper.showErrors(false);
381 V = getCandidateVer(Cache, P, helper);
84910ad5 382 if (V.end() == true)
70e706ad
DK
383 V = getInstalledVer(Cache, P, helper);
384 helper.showErrors(showErrors);
84910ad5
DK
385 if (V.end() == false)
386 verset.insert(V);
84910ad5 387 else
70e706ad 388 verset.insert(helper.canNotFindInstCandVer(Cache, P));
84910ad5
DK
389 break;
390 case VersionSet::INSTCAND:
70e706ad
DK
391 showErrors = helper.showErrors(false);
392 V = getInstalledVer(Cache, P, helper);
84910ad5 393 if (V.end() == true)
70e706ad
DK
394 V = getCandidateVer(Cache, P, helper);
395 helper.showErrors(showErrors);
84910ad5
DK
396 if (V.end() == false)
397 verset.insert(V);
84910ad5 398 else
70e706ad 399 verset.insert(helper.canNotFindInstCandVer(Cache, P));
84910ad5
DK
400 break;
401 case VersionSet::NEWEST:
402 if (P->VersionList != 0)
403 verset.insert(P.VersionList());
84910ad5 404 else
70e706ad 405 verset.insert(helper.canNotFindNewestVer(Cache, P));
84910ad5
DK
406 break;
407 }
fb83c1d0 408 return verset;
84910ad5
DK
409}
410 /*}}}*/
856d3b06
DK
411// getCandidateVer - Returns the candidate version of the given package /*{{{*/
412pkgCache::VerIterator VersionSet::getCandidateVer(pkgCacheFile &Cache,
70e706ad 413 pkgCache::PkgIterator const &Pkg, CacheSetHelper &helper) {
a8ef7efd 414 pkgCache::VerIterator Cand;
2fbfb111
DK
415 if (Cache.IsPolicyBuilt() == true || Cache.IsDepCacheBuilt() == false)
416 {
bd631595
DK
417 if (unlikely(Cache.GetPolicy() == 0))
418 return pkgCache::VerIterator(Cache);
a8ef7efd 419 Cand = Cache.GetPolicy()->GetCandidateVer(Pkg);
2fbfb111
DK
420 } else {
421 Cand = Cache[Pkg].CandidateVerIter(Cache);
a8ef7efd 422 }
70e706ad
DK
423 if (Cand.end() == true)
424 return helper.canNotFindCandidateVer(Cache, Pkg);
856d3b06
DK
425 return Cand;
426}
427 /*}}}*/
428// getInstalledVer - Returns the installed version of the given package /*{{{*/
429pkgCache::VerIterator VersionSet::getInstalledVer(pkgCacheFile &Cache,
70e706ad
DK
430 pkgCache::PkgIterator const &Pkg, CacheSetHelper &helper) {
431 if (Pkg->CurrentVer == 0)
432 return helper.canNotFindInstalledVer(Cache, Pkg);
856d3b06 433 return Pkg.CurrentVer();
ffee1c2b
DK
434}
435 /*}}}*/
bd631595
DK
436// canNotFindPkgName - handle the case no package has this name /*{{{*/
437pkgCache::PkgIterator CacheSetHelper::canNotFindPkgName(pkgCacheFile &Cache,
438 std::string const &str) {
439 if (ShowError == true)
440 _error->Error(_("Unable to locate package %s"), str.c_str());
441 return pkgCache::PkgIterator(Cache, 0);
442}
443 /*}}}*/
70e706ad
DK
444// canNotFindTask - handle the case no package is found for a task /*{{{*/
445PackageSet CacheSetHelper::canNotFindTask(pkgCacheFile &Cache, std::string pattern) {
446 if (ShowError == true)
447 _error->Error(_("Couldn't find task '%s'"), pattern.c_str());
448 return PackageSet();
449}
450 /*}}}*/
451// canNotFindRegEx - handle the case no package is found by a regex /*{{{*/
452PackageSet CacheSetHelper::canNotFindRegEx(pkgCacheFile &Cache, std::string pattern) {
453 if (ShowError == true)
454 _error->Error(_("Couldn't find any package by regex '%s'"), pattern.c_str());
455 return PackageSet();
456}
457 /*}}}*/
458// canNotFindPackage - handle the case no package is found from a string/*{{{*/
459PackageSet CacheSetHelper::canNotFindPackage(pkgCacheFile &Cache, std::string const &str) {
70e706ad
DK
460 return PackageSet();
461}
462 /*}}}*/
463// canNotFindAllVer /*{{{*/
464VersionSet CacheSetHelper::canNotFindAllVer(pkgCacheFile &Cache,
465 pkgCache::PkgIterator const &Pkg) {
466 if (ShowError == true)
4ed3806e 467 _error->Notice(_("Can't select versions from package '%s' as it purely virtual"), Pkg.FullName(true).c_str());
70e706ad
DK
468 return VersionSet();
469}
470 /*}}}*/
471// canNotFindInstCandVer /*{{{*/
472VersionSet CacheSetHelper::canNotFindInstCandVer(pkgCacheFile &Cache,
473 pkgCache::PkgIterator const &Pkg) {
474 if (ShowError == true)
475 _error->Error(_("Can't select installed nor candidate version from package '%s' as it has neither of them"), Pkg.FullName(true).c_str());
476 return VersionSet();
477}
478 /*}}}*/
cf28bcad
DK
479// canNotFindInstCandVer /*{{{*/
480VersionSet CacheSetHelper::canNotFindCandInstVer(pkgCacheFile &Cache,
481 pkgCache::PkgIterator const &Pkg) {
482 if (ShowError == true)
483 _error->Error(_("Can't select installed nor candidate version from package '%s' as it has neither of them"), Pkg.FullName(true).c_str());
484 return VersionSet();
485}
486 /*}}}*/
70e706ad
DK
487// canNotFindNewestVer /*{{{*/
488pkgCache::VerIterator CacheSetHelper::canNotFindNewestVer(pkgCacheFile &Cache,
489 pkgCache::PkgIterator const &Pkg) {
490 if (ShowError == true)
491 _error->Error(_("Can't select newest version from package '%s' as it is purely virtual"), Pkg.FullName(true).c_str());
c8db3fff 492 return pkgCache::VerIterator(Cache, 0);
70e706ad
DK
493}
494 /*}}}*/
495// canNotFindCandidateVer /*{{{*/
496pkgCache::VerIterator CacheSetHelper::canNotFindCandidateVer(pkgCacheFile &Cache,
497 pkgCache::PkgIterator const &Pkg) {
498 if (ShowError == true)
499 _error->Error(_("Can't select candidate version from package %s as it has no candidate"), Pkg.FullName(true).c_str());
c8db3fff 500 return pkgCache::VerIterator(Cache, 0);
70e706ad
DK
501}
502 /*}}}*/
503// canNotFindInstalledVer /*{{{*/
504pkgCache::VerIterator CacheSetHelper::canNotFindInstalledVer(pkgCacheFile &Cache,
505 pkgCache::PkgIterator const &Pkg) {
506 if (ShowError == true)
507 _error->Error(_("Can't select installed version from package %s as it is not installed"), Pkg.FullName(true).c_str());
c8db3fff 508 return pkgCache::VerIterator(Cache, 0);
70e706ad
DK
509}
510 /*}}}*/
ffee1c2b 511}