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