]> git.saurik.com Git - apt.git/blob - cmdline/cacheset.cc
42bc79693521634aecaaca2a7265607493ea95b0
[apt.git] / cmdline / 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 <apt-pkg/aptconfiguration.h>
13 #include <apt-pkg/error.h>
14 #include <apt-pkg/strutl.h>
15 #include <apt-pkg/versionmatch.h>
16
17 #include <apti18n.h>
18
19 #include "cacheset.h"
20
21 #include <vector>
22
23 #include <regex.h>
24 /*}}}*/
25 namespace APT {
26 // FromTask - Return all packages in the cache from a specific task /*{{{*/
27 PackageSet PackageSet::FromTask(pkgCacheFile &Cache, std::string pattern, CacheSetHelper &helper) {
28 size_t const archfound = pattern.find_last_of(':');
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] != '^')
36 return APT::PackageSet();
37 pattern.erase(pattern.length()-1);
38
39 if (unlikely(Cache.GetPkgCache() == 0 || Cache.GetDepCache() == 0))
40 return APT::PackageSet();
41
42 PackageSet pkgset;
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';
70 if (regexec(&Pattern, buf, 0, 0, 0) != 0)
71 continue;
72
73 pkgset.insert(Pkg);
74 }
75 regfree(&Pattern);
76
77 if (pkgset.empty() == true)
78 return helper.canNotFindTask(Cache, pattern);
79
80 helper.showTaskSelection(pkgset, pattern);
81 return pkgset;
82 }
83 /*}}}*/
84 // FromRegEx - Return all packages in the cache matching a pattern /*{{{*/
85 PackageSet PackageSet::FromRegEx(pkgCacheFile &Cache, std::string pattern, CacheSetHelper &helper) {
86 static const char * const isregex = ".?+*|[^$";
87 if (pattern.find_first_of(isregex) == std::string::npos)
88 return PackageSet();
89
90 size_t archfound = pattern.find_last_of(':');
91 std::string arch = "native";
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
100 regex_t Pattern;
101 int Res;
102 if ((Res = regcomp(&Pattern, pattern.c_str() , REG_EXTENDED | REG_ICASE | REG_NOSUB)) != 0) {
103 char Error[300];
104 regerror(Res, &Pattern, Error, sizeof(Error));
105 _error->Error(_("Regex compilation error - %s"), Error);
106 return PackageSet();
107 }
108
109 if (unlikely(Cache.GetPkgCache() == 0))
110 return PackageSet();
111
112 PackageSet pkgset;
113 for (pkgCache::GrpIterator Grp = Cache.GetPkgCache()->GrpBegin(); Grp.end() == false; ++Grp)
114 {
115 if (regexec(&Pattern, Grp.Name(), 0, 0, 0) != 0)
116 continue;
117 pkgCache::PkgIterator Pkg = Grp.FindPkg(arch);
118 if (Pkg.end() == true) {
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);
124 }
125 if (Pkg.end() == true)
126 continue;
127 }
128
129 pkgset.insert(Pkg);
130 }
131 regfree(&Pattern);
132
133 if (pkgset.empty() == true)
134 return helper.canNotFindRegEx(Cache, pattern);
135
136 helper.showRegExSelection(pkgset, pattern);
137 return pkgset;
138 }
139 /*}}}*/
140 // FromName - Returns the package defined by this string /*{{{*/
141 pkgCache::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 /*}}}*/
167 // GroupedFromCommandLine - Return all versions specified on commandline/*{{{*/
168 std::map<unsigned short, PackageSet> PackageSet::GroupedFromCommandLine(
169 pkgCacheFile &Cache, const char **cmdline,
170 std::list<PackageSet::Modifier> const &mods,
171 unsigned short const &fallback, CacheSetHelper &helper) {
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;
176 bool modifierPresent = false;
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,
183 mod->Alias, 0, alength) != 0)
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 }
193 modifierPresent = true;
194 break;
195 }
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 }
205 pkgsets[modID].insert(PackageSet::FromString(Cache, str, helper));
206 }
207 return pkgsets;
208 }
209 /*}}}*/
210 // FromCommandLine - Return all packages specified on commandline /*{{{*/
211 PackageSet PackageSet::FromCommandLine(pkgCacheFile &Cache, const char **cmdline, CacheSetHelper &helper) {
212 PackageSet pkgset;
213 for (const char **I = cmdline; *I != 0; ++I) {
214 PackageSet pset = FromString(Cache, *I, helper);
215 pkgset.insert(pset.begin(), pset.end());
216 }
217 return pkgset;
218 }
219 /*}}}*/
220 // FromString - Return all packages matching a specific string /*{{{*/
221 PackageSet PackageSet::FromString(pkgCacheFile &Cache, std::string const &str, CacheSetHelper &helper) {
222 _error->PushToStack();
223
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 }
235 }
236
237 if (pkgset.empty() == false)
238 _error->RevertToStack();
239 else
240 _error->MergeWithStack();
241 return pkgset;
242 }
243 /*}}}*/
244 // GroupedFromCommandLine - Return all versions specified on commandline/*{{{*/
245 std::map<unsigned short, VersionSet> VersionSet::GroupedFromCommandLine(
246 pkgCacheFile &Cache, const char **cmdline,
247 std::list<VersionSet::Modifier> const &mods,
248 unsigned short const &fallback, CacheSetHelper &helper) {
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;
254 bool modifierPresent = false;
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 }
274 modifierPresent = true;
275 break;
276 }
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 }
287 versets[modID].insert(VersionSet::FromString(Cache, str, select , helper));
288 }
289 return versets;
290 }
291 /*}}}*/
292 // FromCommandLine - Return all versions specified on commandline /*{{{*/
293 APT::VersionSet VersionSet::FromCommandLine(pkgCacheFile &Cache, const char **cmdline,
294 APT::VersionSet::Version const &fallback, CacheSetHelper &helper) {
295 VersionSet verset;
296 for (const char **I = cmdline; *I != 0; ++I)
297 verset.insert(VersionSet::FromString(Cache, *I, fallback, helper));
298 return verset;
299 }
300 /*}}}*/
301 // FromString - Returns all versions spedcified by a string /*{{{*/
302 APT::VersionSet VersionSet::FromString(pkgCacheFile &Cache, std::string pkg,
303 APT::VersionSet::Version const &fallback, CacheSetHelper &helper,
304 bool const &onlyFromName) {
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 }
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
320 VersionSet verset;
321 for (PackageSet::const_iterator P = pkgset.begin();
322 P != pkgset.end(); ++P) {
323 if (vertag == string::npos) {
324 AddSelectedVersion(Cache, verset, P, fallback, helper);
325 continue;
326 }
327 pkgCache::VerIterator V;
328 if (ver == "installed")
329 V = getInstalledVer(Cache, P, helper);
330 else if (ver == "candidate")
331 V = getCandidateVer(Cache, P, helper);
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());
343 continue;
344 }
345 }
346 if (V.end() == true)
347 continue;
348 helper.showSelectedVersion(P, V, ver, verIsRel);
349 verset.insert(V);
350 }
351 return verset;
352 }
353 /*}}}*/
354 // AddSelectedVersion - add version from package based on fallback /*{{{*/
355 void VersionSet::AddSelectedVersion(pkgCacheFile &Cache, VersionSet &verset,
356 pkgCache::PkgIterator const &P, VersionSet::Version const &fallback,
357 CacheSetHelper &helper) {
358 pkgCache::VerIterator V;
359 bool showErrors;
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);
365 else
366 verset.insert(helper.canNotFindAllVer(Cache, P));
367 break;
368 case VersionSet::CANDANDINST:
369 verset.insert(getInstalledVer(Cache, P, helper));
370 verset.insert(getCandidateVer(Cache, P, helper));
371 break;
372 case VersionSet::CANDIDATE:
373 verset.insert(getCandidateVer(Cache, P, helper));
374 break;
375 case VersionSet::INSTALLED:
376 verset.insert(getInstalledVer(Cache, P, helper));
377 break;
378 case VersionSet::CANDINST:
379 showErrors = helper.showErrors(false);
380 V = getCandidateVer(Cache, P, helper);
381 if (V.end() == true)
382 V = getInstalledVer(Cache, P, helper);
383 helper.showErrors(showErrors);
384 if (V.end() == false)
385 verset.insert(V);
386 else
387 verset.insert(helper.canNotFindInstCandVer(Cache, P));
388 break;
389 case VersionSet::INSTCAND:
390 showErrors = helper.showErrors(false);
391 V = getInstalledVer(Cache, P, helper);
392 if (V.end() == true)
393 V = getCandidateVer(Cache, P, helper);
394 helper.showErrors(showErrors);
395 if (V.end() == false)
396 verset.insert(V);
397 else
398 verset.insert(helper.canNotFindInstCandVer(Cache, P));
399 break;
400 case VersionSet::NEWEST:
401 if (P->VersionList != 0)
402 verset.insert(P.VersionList());
403 else
404 verset.insert(helper.canNotFindNewestVer(Cache, P));
405 break;
406 }
407 }
408 /*}}}*/
409 // getCandidateVer - Returns the candidate version of the given package /*{{{*/
410 pkgCache::VerIterator VersionSet::getCandidateVer(pkgCacheFile &Cache,
411 pkgCache::PkgIterator const &Pkg, CacheSetHelper &helper) {
412 pkgCache::VerIterator Cand;
413 if (Cache.IsDepCacheBuilt() == true)
414 Cand = Cache[Pkg].CandidateVerIter(Cache);
415 else {
416 if (unlikely(Cache.GetPolicy() == 0))
417 return pkgCache::VerIterator(Cache);
418 Cand = Cache.GetPolicy()->GetCandidateVer(Pkg);
419 }
420 if (Cand.end() == true)
421 return helper.canNotFindCandidateVer(Cache, Pkg);
422 return Cand;
423 }
424 /*}}}*/
425 // getInstalledVer - Returns the installed version of the given package /*{{{*/
426 pkgCache::VerIterator VersionSet::getInstalledVer(pkgCacheFile &Cache,
427 pkgCache::PkgIterator const &Pkg, CacheSetHelper &helper) {
428 if (Pkg->CurrentVer == 0)
429 return helper.canNotFindInstalledVer(Cache, Pkg);
430 return Pkg.CurrentVer();
431 }
432 /*}}}*/
433 // canNotFindPkgName - handle the case no package has this name /*{{{*/
434 pkgCache::PkgIterator CacheSetHelper::canNotFindPkgName(pkgCacheFile &Cache,
435 std::string const &str) {
436 if (ShowError == true)
437 _error->Error(_("Unable to locate package %s"), str.c_str());
438 return pkgCache::PkgIterator(Cache, 0);
439 }
440 /*}}}*/
441 // canNotFindTask - handle the case no package is found for a task /*{{{*/
442 PackageSet CacheSetHelper::canNotFindTask(pkgCacheFile &Cache, std::string pattern) {
443 if (ShowError == true)
444 _error->Error(_("Couldn't find task '%s'"), pattern.c_str());
445 return PackageSet();
446 }
447 /*}}}*/
448 // canNotFindRegEx - handle the case no package is found by a regex /*{{{*/
449 PackageSet CacheSetHelper::canNotFindRegEx(pkgCacheFile &Cache, std::string pattern) {
450 if (ShowError == true)
451 _error->Error(_("Couldn't find any package by regex '%s'"), pattern.c_str());
452 return PackageSet();
453 }
454 /*}}}*/
455 // canNotFindPackage - handle the case no package is found from a string/*{{{*/
456 PackageSet CacheSetHelper::canNotFindPackage(pkgCacheFile &Cache, std::string const &str) {
457 return PackageSet();
458 }
459 /*}}}*/
460 // canNotFindAllVer /*{{{*/
461 VersionSet CacheSetHelper::canNotFindAllVer(pkgCacheFile &Cache,
462 pkgCache::PkgIterator const &Pkg) {
463 if (ShowError == true)
464 _error->Error(_("Can't select versions from package '%s' as it purely virtual"), Pkg.FullName(true).c_str());
465 return VersionSet();
466 }
467 /*}}}*/
468 // canNotFindInstCandVer /*{{{*/
469 VersionSet CacheSetHelper::canNotFindInstCandVer(pkgCacheFile &Cache,
470 pkgCache::PkgIterator const &Pkg) {
471 if (ShowError == true)
472 _error->Error(_("Can't select installed nor candidate version from package '%s' as it has neither of them"), Pkg.FullName(true).c_str());
473 return VersionSet();
474 }
475 /*}}}*/
476 // canNotFindNewestVer /*{{{*/
477 pkgCache::VerIterator CacheSetHelper::canNotFindNewestVer(pkgCacheFile &Cache,
478 pkgCache::PkgIterator const &Pkg) {
479 if (ShowError == true)
480 _error->Error(_("Can't select newest version from package '%s' as it is purely virtual"), Pkg.FullName(true).c_str());
481 return pkgCache::VerIterator(Cache);
482 }
483 /*}}}*/
484 // canNotFindCandidateVer /*{{{*/
485 pkgCache::VerIterator CacheSetHelper::canNotFindCandidateVer(pkgCacheFile &Cache,
486 pkgCache::PkgIterator const &Pkg) {
487 if (ShowError == true)
488 _error->Error(_("Can't select candidate version from package %s as it has no candidate"), Pkg.FullName(true).c_str());
489 return pkgCache::VerIterator(Cache);
490 }
491 /*}}}*/
492 // canNotFindInstalledVer /*{{{*/
493 pkgCache::VerIterator CacheSetHelper::canNotFindInstalledVer(pkgCacheFile &Cache,
494 pkgCache::PkgIterator const &Pkg) {
495 if (ShowError == true)
496 _error->Error(_("Can't select installed version from package %s as it is not installed"), Pkg.FullName(true).c_str());
497 return pkgCache::VerIterator(Cache);
498 }
499 /*}}}*/
500 }