]> git.saurik.com Git - apt.git/blob - apt-pkg/cacheset.cc
c0b06ba323c58b17d5dde95c9bcdf1c7b704c53d
[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 <apt-pkg/aptconfiguration.h>
13 #include <apt-pkg/error.h>
14 #include <apt-pkg/cacheset.h>
15 #include <apt-pkg/strutl.h>
16 #include <apt-pkg/versionmatch.h>
17
18 #include <apti18n.h>
19
20 #include <vector>
21
22 #include <regex.h>
23 /*}}}*/
24 namespace APT {
25 // FromRegEx - Return all packages in the cache matching a pattern /*{{{*/
26 PackageSet PackageSet::FromRegEx(pkgCacheFile &Cache, std::string pattern, std::ostream &out) {
27 PackageSet pkgset;
28 std::string arch = "native";
29 static const char * const isregex = ".?+*|[^$";
30
31 if (pattern.find_first_of(isregex) == std::string::npos)
32 return pkgset;
33
34 size_t archfound = pattern.find_last_of(':');
35 if (archfound != std::string::npos) {
36 arch = pattern.substr(archfound+1);
37 if (arch.find_first_of(isregex) == std::string::npos)
38 pattern.erase(archfound);
39 else
40 arch = "native";
41 }
42
43 regex_t Pattern;
44 int Res;
45 if ((Res = regcomp(&Pattern, pattern.c_str() , REG_EXTENDED | REG_ICASE | REG_NOSUB)) != 0) {
46 char Error[300];
47 regerror(Res, &Pattern, Error, sizeof(Error));
48 _error->Error(_("Regex compilation error - %s"), Error);
49 return pkgset;
50 }
51
52 for (pkgCache::GrpIterator Grp = Cache.GetPkgCache()->GrpBegin(); Grp.end() == false; ++Grp)
53 {
54 if (regexec(&Pattern, Grp.Name(), 0, 0, 0) != 0)
55 continue;
56 pkgCache::PkgIterator Pkg = Grp.FindPkg(arch);
57 if (Pkg.end() == true) {
58 if (archfound == std::string::npos) {
59 std::vector<std::string> archs = APT::Configuration::getArchitectures();
60 for (std::vector<std::string>::const_iterator a = archs.begin();
61 a != archs.end() && Pkg.end() != true; ++a)
62 Pkg = Grp.FindPkg(*a);
63 }
64 if (Pkg.end() == true)
65 continue;
66 }
67
68 ioprintf(out, _("Note, selecting %s for regex '%s'\n"),
69 Pkg.FullName(true).c_str(), pattern.c_str());
70
71 pkgset.insert(Pkg);
72 }
73
74 regfree(&Pattern);
75
76 return pkgset;
77 }
78 /*}}}*/
79 // FromCommandLine - Return all packages specified on commandline /*{{{*/
80 PackageSet PackageSet::FromCommandLine(pkgCacheFile &Cache, const char **cmdline, std::ostream &out) {
81 PackageSet pkgset;
82 for (const char **I = cmdline; *I != 0; ++I) {
83 PackageSet pset = FromString(Cache, *I, out);
84 pkgset.insert(pset.begin(), pset.end());
85 }
86 return pkgset;
87 }
88 /*}}}*/
89 // FromString - Return all packages matching a specific string /*{{{*/
90 PackageSet PackageSet::FromString(pkgCacheFile &Cache, std::string const &str, std::ostream &out) {
91 std::string pkg = str;
92 size_t archfound = pkg.find_last_of(':');
93 std::string arch;
94 if (archfound != std::string::npos) {
95 arch = pkg.substr(archfound+1);
96 pkg.erase(archfound);
97 }
98
99 pkgCache::PkgIterator Pkg;
100 if (arch.empty() == true) {
101 pkgCache::GrpIterator Grp = Cache.GetPkgCache()->FindGrp(pkg);
102 if (Grp.end() == false)
103 Pkg = Grp.FindPreferredPkg();
104 } else
105 Pkg = Cache.GetPkgCache()->FindPkg(pkg, arch);
106
107 if (Pkg.end() == false) {
108 PackageSet pkgset;
109 pkgset.insert(Pkg);
110 return pkgset;
111 }
112 PackageSet regex = FromRegEx(Cache, str, out);
113 if (regex.empty() == true)
114 _error->Warning(_("Unable to locate package %s"), str.c_str());
115 return regex;
116 }
117 /*}}}*/
118 // FromCommandLine - Return all versions specified on commandline /*{{{*/
119 APT::VersionSet VersionSet::FromCommandLine(pkgCacheFile &Cache, const char **cmdline,
120 APT::VersionSet::Version const &fallback, std::ostream &out) {
121 VersionSet verset;
122 for (const char **I = cmdline; *I != 0; ++I) {
123 std::string pkg = *I;
124 std::string ver;
125 bool verIsRel = false;
126 size_t const vertag = pkg.find_last_of("/=");
127 if (vertag != string::npos) {
128 ver = pkg.substr(vertag+1);
129 verIsRel = (pkg[vertag] == '/');
130 pkg.erase(vertag);
131 }
132 PackageSet pkgset = PackageSet::FromString(Cache, pkg.c_str(), out);
133 for (PackageSet::const_iterator P = pkgset.begin();
134 P != pkgset.end(); ++P) {
135 if (vertag == string::npos) {
136 AddSelectedVersion(Cache, verset, P, fallback);
137 continue;
138 }
139 pkgCache::VerIterator V;
140 if (ver == "installed")
141 V = getInstalledVer(Cache, P);
142 else if (ver == "candidate")
143 V = getCandidateVer(Cache, P);
144 else {
145 pkgVersionMatch Match(ver, (verIsRel == true ? pkgVersionMatch::Release :
146 pkgVersionMatch::Version));
147 V = Match.Find(P);
148 if (V.end() == true) {
149 if (verIsRel == true)
150 _error->Error(_("Release '%s' for '%s' was not found"),
151 ver.c_str(), P.FullName(true).c_str());
152 else
153 _error->Error(_("Version '%s' for '%s' was not found"),
154 ver.c_str(), P.FullName(true).c_str());
155 continue;
156 }
157 }
158 if (V.end() == true)
159 continue;
160 if (ver == V.VerStr())
161 ioprintf(out, _("Selected version '%s' (%s) for '%s'\n"),
162 V.VerStr(), V.RelStr().c_str(), P.FullName(true).c_str());
163 verset.insert(V);
164 }
165 }
166 return verset;
167 }
168 /*}}}*/
169 // AddSelectedVersion - add version from package based on fallback /*{{{*/
170 bool VersionSet::AddSelectedVersion(pkgCacheFile &Cache, VersionSet &verset,
171 pkgCache::PkgIterator const &P, VersionSet::Version const &fallback,
172 bool const &AllowError) {
173 pkgCache::VerIterator V;
174 switch(fallback) {
175 case VersionSet::ALL:
176 if (P->VersionList != 0)
177 for (V = P.VersionList(); V.end() != true; ++V)
178 verset.insert(V);
179 else if (AllowError == false)
180 return _error->Error(_("Can't select versions from package '%s' as it purely virtual"), P.FullName(true).c_str());
181 else
182 return false;
183 break;
184 case VersionSet::CANDANDINST:
185 verset.insert(getInstalledVer(Cache, P, AllowError));
186 verset.insert(getCandidateVer(Cache, P, AllowError));
187 break;
188 case VersionSet::CANDIDATE:
189 verset.insert(getCandidateVer(Cache, P, AllowError));
190 break;
191 case VersionSet::INSTALLED:
192 verset.insert(getInstalledVer(Cache, P, AllowError));
193 break;
194 case VersionSet::CANDINST:
195 V = getCandidateVer(Cache, P, true);
196 if (V.end() == true)
197 V = getInstalledVer(Cache, P, true);
198 if (V.end() == false)
199 verset.insert(V);
200 else if (AllowError == false)
201 return _error->Error(_("Can't select installed nor candidate version from package '%s' as it has neither of them"), P.FullName(true).c_str());
202 else
203 return false;
204 break;
205 case VersionSet::INSTCAND:
206 V = getInstalledVer(Cache, P, true);
207 if (V.end() == true)
208 V = getCandidateVer(Cache, P, true);
209 if (V.end() == false)
210 verset.insert(V);
211 else if (AllowError == false)
212 return _error->Error(_("Can't select installed nor candidate version from package '%s' as it has neither of them"), P.FullName(true).c_str());
213 else
214 return false;
215 break;
216 case VersionSet::NEWEST:
217 if (P->VersionList != 0)
218 verset.insert(P.VersionList());
219 else if (AllowError == false)
220 return _error->Error(_("Can't select newest version from package '%s' as it is purely virtual"), P.FullName(true).c_str());
221 else
222 return false;
223 break;
224 }
225 return true;
226 }
227 /*}}}*/
228 // getCandidateVer - Returns the candidate version of the given package /*{{{*/
229 pkgCache::VerIterator VersionSet::getCandidateVer(pkgCacheFile &Cache,
230 pkgCache::PkgIterator const &Pkg, bool const &AllowError) {
231 if (unlikely(Cache.BuildDepCache() == false))
232 return pkgCache::VerIterator(*Cache);
233 pkgCache::VerIterator Cand = Cache[Pkg].CandidateVerIter(Cache);
234 if (AllowError == false && Cand.end() == true)
235 _error->Error(_("Can't select candidate version from package %s as it has no candidate"), Pkg.FullName(true).c_str());
236 return Cand;
237 }
238 /*}}}*/
239 // getInstalledVer - Returns the installed version of the given package /*{{{*/
240 pkgCache::VerIterator VersionSet::getInstalledVer(pkgCacheFile &Cache,
241 pkgCache::PkgIterator const &Pkg, bool const &AllowError) {
242 if (AllowError == false && Pkg->CurrentVer == 0)
243 _error->Error(_("Can't select installed version from package %s as it is not installed"), Pkg.FullName(true).c_str());
244 return Pkg.CurrentVer();
245 }
246 /*}}}*/
247 }