]> git.saurik.com Git - apt.git/blame - cmdline/cacheset.cc
rename AddSelectedVersion() to a better public FromPackage()
[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] != '^')
bd631595 36 return APT::PackageSet();
dc0f01f7
DK
37 pattern.erase(pattern.length()-1);
38
bd631595
DK
39 if (unlikely(Cache.GetPkgCache() == 0 || Cache.GetDepCache() == 0))
40 return APT::PackageSet();
41
42 PackageSet pkgset;
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)
bd631595 88 return PackageSet();
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);
bd631595 106 return PackageSet();
ffee1c2b
DK
107 }
108
bd631595
DK
109 if (unlikely(Cache.GetPkgCache() == 0))
110 return PackageSet();
111
112 PackageSet pkgset;
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
DK
320 VersionSet verset;
321 for (PackageSet::const_iterator P = pkgset.begin();
322 P != pkgset.end(); ++P) {
323 if (vertag == string::npos) {
fb83c1d0 324 verset.insert(VersionSet::FromPackage(Cache, P, fallback, helper));
55c59998 325 continue;
856d3b06 326 }
55c59998
DK
327 pkgCache::VerIterator V;
328 if (ver == "installed")
70e706ad 329 V = getInstalledVer(Cache, P, helper);
55c59998 330 else if (ver == "candidate")
70e706ad 331 V = getCandidateVer(Cache, P, helper);
55c59998
DK
332 else {
333 pkgVersionMatch Match(ver, (verIsRel == true ? pkgVersionMatch::Release :
334 pkgVersionMatch::Version));
335 V = Match.Find(P);
336 if (V.end() == true) {
337 if (verIsRel == true)
338 _error->Error(_("Release '%s' for '%s' was not found"),
339 ver.c_str(), P.FullName(true).c_str());
340 else
341 _error->Error(_("Version '%s' for '%s' was not found"),
342 ver.c_str(), P.FullName(true).c_str());
84910ad5
DK
343 continue;
344 }
78c32596 345 }
55c59998
DK
346 if (V.end() == true)
347 continue;
70e706ad 348 helper.showSelectedVersion(P, V, ver, verIsRel);
55c59998 349 verset.insert(V);
78c32596 350 }
856d3b06
DK
351 return verset;
352}
353 /*}}}*/
fb83c1d0
DK
354// FromPackage - versions from package based on fallback /*{{{*/
355VersionSet VersionSet::FromPackage(pkgCacheFile &Cache, pkgCache::PkgIterator const &P,
356 VersionSet::Version const &fallback, CacheSetHelper &helper) {
357 VersionSet verset;
84910ad5 358 pkgCache::VerIterator V;
70e706ad 359 bool showErrors;
84910ad5
DK
360 switch(fallback) {
361 case VersionSet::ALL:
362 if (P->VersionList != 0)
363 for (V = P.VersionList(); V.end() != true; ++V)
364 verset.insert(V);
84910ad5 365 else
70e706ad 366 verset.insert(helper.canNotFindAllVer(Cache, P));
84910ad5
DK
367 break;
368 case VersionSet::CANDANDINST:
70e706ad
DK
369 verset.insert(getInstalledVer(Cache, P, helper));
370 verset.insert(getCandidateVer(Cache, P, helper));
84910ad5
DK
371 break;
372 case VersionSet::CANDIDATE:
70e706ad 373 verset.insert(getCandidateVer(Cache, P, helper));
84910ad5
DK
374 break;
375 case VersionSet::INSTALLED:
70e706ad 376 verset.insert(getInstalledVer(Cache, P, helper));
84910ad5
DK
377 break;
378 case VersionSet::CANDINST:
70e706ad
DK
379 showErrors = helper.showErrors(false);
380 V = getCandidateVer(Cache, P, helper);
84910ad5 381 if (V.end() == true)
70e706ad
DK
382 V = getInstalledVer(Cache, P, helper);
383 helper.showErrors(showErrors);
84910ad5
DK
384 if (V.end() == false)
385 verset.insert(V);
84910ad5 386 else
70e706ad 387 verset.insert(helper.canNotFindInstCandVer(Cache, P));
84910ad5
DK
388 break;
389 case VersionSet::INSTCAND:
70e706ad
DK
390 showErrors = helper.showErrors(false);
391 V = getInstalledVer(Cache, P, helper);
84910ad5 392 if (V.end() == true)
70e706ad
DK
393 V = getCandidateVer(Cache, P, helper);
394 helper.showErrors(showErrors);
84910ad5
DK
395 if (V.end() == false)
396 verset.insert(V);
84910ad5 397 else
70e706ad 398 verset.insert(helper.canNotFindInstCandVer(Cache, P));
84910ad5
DK
399 break;
400 case VersionSet::NEWEST:
401 if (P->VersionList != 0)
402 verset.insert(P.VersionList());
84910ad5 403 else
70e706ad 404 verset.insert(helper.canNotFindNewestVer(Cache, P));
84910ad5
DK
405 break;
406 }
fb83c1d0 407 return verset;
84910ad5
DK
408}
409 /*}}}*/
856d3b06
DK
410// getCandidateVer - Returns the candidate version of the given package /*{{{*/
411pkgCache::VerIterator VersionSet::getCandidateVer(pkgCacheFile &Cache,
70e706ad 412 pkgCache::PkgIterator const &Pkg, CacheSetHelper &helper) {
a8ef7efd
DK
413 pkgCache::VerIterator Cand;
414 if (Cache.IsDepCacheBuilt() == true)
415 Cand = Cache[Pkg].CandidateVerIter(Cache);
416 else {
bd631595
DK
417 if (unlikely(Cache.GetPolicy() == 0))
418 return pkgCache::VerIterator(Cache);
a8ef7efd
DK
419 Cand = Cache.GetPolicy()->GetCandidateVer(Pkg);
420 }
70e706ad
DK
421 if (Cand.end() == true)
422 return helper.canNotFindCandidateVer(Cache, Pkg);
856d3b06
DK
423 return Cand;
424}
425 /*}}}*/
426// getInstalledVer - Returns the installed version of the given package /*{{{*/
427pkgCache::VerIterator VersionSet::getInstalledVer(pkgCacheFile &Cache,
70e706ad
DK
428 pkgCache::PkgIterator const &Pkg, CacheSetHelper &helper) {
429 if (Pkg->CurrentVer == 0)
430 return helper.canNotFindInstalledVer(Cache, Pkg);
856d3b06 431 return Pkg.CurrentVer();
ffee1c2b
DK
432}
433 /*}}}*/
bd631595
DK
434// canNotFindPkgName - handle the case no package has this name /*{{{*/
435pkgCache::PkgIterator CacheSetHelper::canNotFindPkgName(pkgCacheFile &Cache,
436 std::string const &str) {
437 if (ShowError == true)
438 _error->Error(_("Unable to locate package %s"), str.c_str());
439 return pkgCache::PkgIterator(Cache, 0);
440}
441 /*}}}*/
70e706ad
DK
442// canNotFindTask - handle the case no package is found for a task /*{{{*/
443PackageSet CacheSetHelper::canNotFindTask(pkgCacheFile &Cache, std::string pattern) {
444 if (ShowError == true)
445 _error->Error(_("Couldn't find task '%s'"), pattern.c_str());
446 return PackageSet();
447}
448 /*}}}*/
449// canNotFindRegEx - handle the case no package is found by a regex /*{{{*/
450PackageSet CacheSetHelper::canNotFindRegEx(pkgCacheFile &Cache, std::string pattern) {
451 if (ShowError == true)
452 _error->Error(_("Couldn't find any package by regex '%s'"), pattern.c_str());
453 return PackageSet();
454}
455 /*}}}*/
456// canNotFindPackage - handle the case no package is found from a string/*{{{*/
457PackageSet CacheSetHelper::canNotFindPackage(pkgCacheFile &Cache, std::string const &str) {
70e706ad
DK
458 return PackageSet();
459}
460 /*}}}*/
461// canNotFindAllVer /*{{{*/
462VersionSet CacheSetHelper::canNotFindAllVer(pkgCacheFile &Cache,
463 pkgCache::PkgIterator const &Pkg) {
464 if (ShowError == true)
465 _error->Error(_("Can't select versions from package '%s' as it purely virtual"), Pkg.FullName(true).c_str());
466 return VersionSet();
467}
468 /*}}}*/
469// canNotFindInstCandVer /*{{{*/
470VersionSet CacheSetHelper::canNotFindInstCandVer(pkgCacheFile &Cache,
471 pkgCache::PkgIterator const &Pkg) {
472 if (ShowError == true)
473 _error->Error(_("Can't select installed nor candidate version from package '%s' as it has neither of them"), Pkg.FullName(true).c_str());
474 return VersionSet();
475}
476 /*}}}*/
477// canNotFindNewestVer /*{{{*/
478pkgCache::VerIterator CacheSetHelper::canNotFindNewestVer(pkgCacheFile &Cache,
479 pkgCache::PkgIterator const &Pkg) {
480 if (ShowError == true)
481 _error->Error(_("Can't select newest version from package '%s' as it is purely virtual"), Pkg.FullName(true).c_str());
bd631595 482 return pkgCache::VerIterator(Cache);
70e706ad
DK
483}
484 /*}}}*/
485// canNotFindCandidateVer /*{{{*/
486pkgCache::VerIterator CacheSetHelper::canNotFindCandidateVer(pkgCacheFile &Cache,
487 pkgCache::PkgIterator const &Pkg) {
488 if (ShowError == true)
489 _error->Error(_("Can't select candidate version from package %s as it has no candidate"), Pkg.FullName(true).c_str());
bd631595 490 return pkgCache::VerIterator(Cache);
70e706ad
DK
491}
492 /*}}}*/
493// canNotFindInstalledVer /*{{{*/
494pkgCache::VerIterator CacheSetHelper::canNotFindInstalledVer(pkgCacheFile &Cache,
495 pkgCache::PkgIterator const &Pkg) {
496 if (ShowError == true)
497 _error->Error(_("Can't select installed version from package %s as it is not installed"), Pkg.FullName(true).c_str());
bd631595 498 return pkgCache::VerIterator(Cache);
70e706ad
DK
499}
500 /*}}}*/
ffee1c2b 501}