Add a method to get a VersionSet from the Commandline and refactor
[ntk/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 + 1; *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, const char * const str, std::ostream &out) {
91 pkgCache::GrpIterator Grp = Cache.GetPkgCache()->FindGrp(str);
92 if (Grp.end() == false) {
93 pkgCache::PkgIterator Pkg = Grp.FindPreferredPkg();
94 PackageSet pkgset;
95 pkgset.insert(Pkg);
96 return pkgset;
97 }
98 PackageSet regex = FromRegEx(Cache, str, out);
99 if (regex.empty() == true)
100 _error->Warning(_("Unable to locate package %s"), str);
101 return regex;
102 }
103 /*}}}*/
104 // FromCommandLine - Return all versions specified on commandline /*{{{*/
105 APT::VersionSet VersionSet::FromCommandLine(pkgCacheFile &Cache, const char **cmdline,
106 APT::VersionSet::Version const &fallback, std::ostream &out) {
107 VersionSet verset;
108 for (const char **I = cmdline + 1; *I != 0; I++) {
109 std::string pkg = *I;
110 std::string ver;
111 bool verIsRel = false;
112 size_t const vertag = pkg.find_last_of("/=");
113 if (vertag != string::npos) {
114 ver = pkg.substr(vertag+1);
115 verIsRel = (pkg[vertag] == '/');
116 pkg.erase(vertag);
117 }
118 PackageSet pkgset = PackageSet::FromString(Cache, pkg.c_str(), out);
119 for (PackageSet::const_iterator P = pkgset.begin();
120 P != pkgset.end(); ++P) {
121 if (vertag != string::npos) {
122 pkgVersionMatch Match(ver, (verIsRel == true ? pkgVersionMatch::Release :
123 pkgVersionMatch::Version));
124 pkgCache::VerIterator V = Match.Find(P);
125 if (V.end() == true) {
126 if (verIsRel == true)
127 _error->Error(_("Release '%s' for '%s' was not found"),
128 ver.c_str(), P.FullName(true).c_str());
129 else
130 _error->Error(_("Version '%s' for '%s' was not found"),
131 ver.c_str(), P.FullName(true).c_str());
132 continue;
133 }
134 if (strcmp(ver.c_str(), V.VerStr()) != 0)
135 ioprintf(out, _("Selected version %s (%s) for %s\n"),
136 V.VerStr(), V.RelStr().c_str(), P.FullName(true).c_str());
137 verset.insert(V);
138 } else {
139 pkgCache::VerIterator V;
140 switch(fallback) {
141 case VersionSet::ALL:
142 for (V = P.VersionList(); V.end() != true; ++V)
143 verset.insert(V);
144 break;
145 case VersionSet::CANDANDINST:
146 verset.insert(getInstalledVer(Cache, P));
147 verset.insert(getCandidateVer(Cache, P));
148 break;
149 case VersionSet::CANDIDATE:
150 verset.insert(getCandidateVer(Cache, P));
151 break;
152 case VersionSet::INSTALLED:
153 verset.insert(getInstalledVer(Cache, P));
154 break;
155 case VersionSet::CANDINST:
156 V = getCandidateVer(Cache, P, true);
157 if (V.end() == true)
158 V = getInstalledVer(Cache, P, true);
159 if (V.end() == false)
160 verset.insert(V);
161 else
162 _error->Error(_("Can't select installed nor candidate version from package %s as it has neither of them"), P.FullName(true).c_str());
163 break;
164 case VersionSet::INSTCAND:
165 V = getInstalledVer(Cache, P, true);
166 if (V.end() == true)
167 V = getCandidateVer(Cache, P, true);
168 if (V.end() == false)
169 verset.insert(V);
170 else
171 _error->Error(_("Can't select installed nor candidate version from package %s as it has neither of them"), P.FullName(true).c_str());
172 break;
173 case VersionSet::NEWEST:
174 if (P->VersionList != 0)
175 verset.insert(P.VersionList());
176 else
177 _error->Error(_("Can't select newest version from package %s as it is purely virtual"), P.FullName(true).c_str());
178 break;
179 }
180 }
181 }
182 }
183 return verset;
184 }
185 /*}}}*/
186 // getCandidateVer - Returns the candidate version of the given package /*{{{*/
187 pkgCache::VerIterator VersionSet::getCandidateVer(pkgCacheFile &Cache,
188 pkgCache::PkgIterator const &Pkg, bool const &AllowError) {
189 if (unlikely(Cache.BuildDepCache() == false))
190 return pkgCache::VerIterator(*Cache);
191 pkgCache::VerIterator Cand = Cache[Pkg].InstVerIter(Cache);
192 if (AllowError == false && Cand.end() == true)
193 _error->Error(_("Can't select candidate version from package %s as it has no candidate"), Pkg.FullName(true).c_str());
194 return Cand;
195 }
196 /*}}}*/
197 // getInstalledVer - Returns the installed version of the given package /*{{{*/
198 pkgCache::VerIterator VersionSet::getInstalledVer(pkgCacheFile &Cache,
199 pkgCache::PkgIterator const &Pkg, bool const &AllowError) {
200 if (AllowError == false && Pkg->CurrentVer == 0)
201 _error->Error(_("Can't select installed version from package %s as it is not installed"), Pkg.FullName(true).c_str());
202 return Pkg.CurrentVer();
203 }
204 /*}}}*/
205 }