]> git.saurik.com Git - apt.git/blob - apt-pkg/cacheset.cc
8fcffaf9a47690108070124d0f8c64fac852a4c5
[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 pkgVersionMatch Match(ver, (verIsRel == true ? pkgVersionMatch::Release :
137 pkgVersionMatch::Version));
138 pkgCache::VerIterator V = Match.Find(P);
139 if (V.end() == true) {
140 if (verIsRel == true)
141 _error->Error(_("Release '%s' for '%s' was not found"),
142 ver.c_str(), P.FullName(true).c_str());
143 else
144 _error->Error(_("Version '%s' for '%s' was not found"),
145 ver.c_str(), P.FullName(true).c_str());
146 continue;
147 }
148 if (strcmp(ver.c_str(), V.VerStr()) != 0)
149 ioprintf(out, _("Selected version %s (%s) for %s\n"),
150 V.VerStr(), V.RelStr().c_str(), P.FullName(true).c_str());
151 verset.insert(V);
152 } else {
153 pkgCache::VerIterator V;
154 switch(fallback) {
155 case VersionSet::ALL:
156 for (V = P.VersionList(); V.end() != true; ++V)
157 verset.insert(V);
158 break;
159 case VersionSet::CANDANDINST:
160 verset.insert(getInstalledVer(Cache, P));
161 verset.insert(getCandidateVer(Cache, P));
162 break;
163 case VersionSet::CANDIDATE:
164 verset.insert(getCandidateVer(Cache, P));
165 break;
166 case VersionSet::INSTALLED:
167 verset.insert(getInstalledVer(Cache, P));
168 break;
169 case VersionSet::CANDINST:
170 V = getCandidateVer(Cache, P, true);
171 if (V.end() == true)
172 V = getInstalledVer(Cache, P, true);
173 if (V.end() == false)
174 verset.insert(V);
175 else
176 _error->Error(_("Can't select installed nor candidate version from package %s as it has neither of them"), P.FullName(true).c_str());
177 break;
178 case VersionSet::INSTCAND:
179 V = getInstalledVer(Cache, P, true);
180 if (V.end() == true)
181 V = getCandidateVer(Cache, P, true);
182 if (V.end() == false)
183 verset.insert(V);
184 else
185 _error->Error(_("Can't select installed nor candidate version from package %s as it has neither of them"), P.FullName(true).c_str());
186 break;
187 case VersionSet::NEWEST:
188 if (P->VersionList != 0)
189 verset.insert(P.VersionList());
190 else
191 _error->Error(_("Can't select newest version from package %s as it is purely virtual"), P.FullName(true).c_str());
192 break;
193 }
194 }
195 }
196 }
197 return verset;
198 }
199 /*}}}*/
200 // getCandidateVer - Returns the candidate version of the given package /*{{{*/
201 pkgCache::VerIterator VersionSet::getCandidateVer(pkgCacheFile &Cache,
202 pkgCache::PkgIterator const &Pkg, bool const &AllowError) {
203 if (unlikely(Cache.BuildDepCache() == false))
204 return pkgCache::VerIterator(*Cache);
205 pkgCache::VerIterator Cand = Cache[Pkg].CandidateVerIter(Cache);
206 if (AllowError == false && Cand.end() == true)
207 _error->Error(_("Can't select candidate version from package %s as it has no candidate"), Pkg.FullName(true).c_str());
208 return Cand;
209 }
210 /*}}}*/
211 // getInstalledVer - Returns the installed version of the given package /*{{{*/
212 pkgCache::VerIterator VersionSet::getInstalledVer(pkgCacheFile &Cache,
213 pkgCache::PkgIterator const &Pkg, bool const &AllowError) {
214 if (AllowError == false && Pkg->CurrentVer == 0)
215 _error->Error(_("Can't select installed version from package %s as it is not installed"), Pkg.FullName(true).c_str());
216 return Pkg.CurrentVer();
217 }
218 /*}}}*/
219 }