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