]> git.saurik.com Git - apt.git/blob - apt-private/private-cacheset.h
adapt to the new CacheSetHelper API
[apt.git] / apt-private / private-cacheset.h
1 #ifndef APT_PRIVATE_CACHESET_H
2 #define APT_PRIVATE_CACHESET_H
3
4 #include <apt-pkg/aptconfiguration.h>
5 #include <apt-pkg/cachefile.h>
6 #include <apt-pkg/cacheset.h>
7 #include <apt-pkg/sptr.h>
8 #include <apt-pkg/strutl.h>
9 #include <apt-pkg/depcache.h>
10 #include <apt-pkg/error.h>
11 #include <apt-pkg/pkgcache.h>
12 #include <apt-pkg/cacheiterators.h>
13 #include <apt-pkg/macros.h>
14
15 #include <algorithm>
16 #include <vector>
17 #include <string.h>
18 #include <list>
19 #include <ostream>
20 #include <set>
21 #include <string>
22 #include <utility>
23
24 #include "private-output.h"
25
26 #include <apti18n.h>
27
28 class OpProgress;
29
30 struct VersionSortDescriptionLocality
31 {
32 bool operator () (const pkgCache::VerIterator &v_lhs,
33 const pkgCache::VerIterator &v_rhs)
34 {
35 pkgCache::DescFile *A = v_lhs.TranslatedDescription().FileList();
36 pkgCache::DescFile *B = v_rhs.TranslatedDescription().FileList();
37 if (A == 0 && B == 0)
38 return false;
39
40 if (A == 0)
41 return true;
42
43 if (B == 0)
44 return false;
45
46 if (A->File == B->File)
47 return A->Offset < B->Offset;
48
49 return A->File < B->File;
50 }
51 };
52
53 // sorted by locality which makes iterating much faster
54 typedef APT::VersionContainer<
55 std::set<pkgCache::VerIterator,
56 VersionSortDescriptionLocality> > LocalitySortedVersionSet;
57
58 class Matcher {
59 public:
60 virtual bool operator () (const pkgCache::PkgIterator &/*P*/) {
61 return true;}
62 };
63
64 // FIXME: add default argument for OpProgress (or overloaded function)
65 bool GetLocalitySortedVersionSet(pkgCacheFile &CacheFile,
66 APT::VersionContainerInterface * const vci,
67 Matcher &matcher,
68 OpProgress * const progress);
69 bool GetLocalitySortedVersionSet(pkgCacheFile &CacheFile,
70 APT::VersionContainerInterface * const vci,
71 OpProgress * const progress);
72
73
74 // CacheSetHelper saving virtual packages /*{{{*/
75 class CacheSetHelperVirtuals: public APT::CacheSetHelper {
76 public:
77 APT::PackageSet virtualPkgs;
78
79 virtual pkgCache::VerIterator canNotGetVersion(enum CacheSetHelper::VerSelector const select, pkgCacheFile &Cache, pkgCache::PkgIterator const &Pkg) {
80 if (select == NEWEST || select == CANDIDATE || select == ALL)
81 virtualPkgs.insert(Pkg);
82 return CacheSetHelper::canNotGetVersion(select, Cache, Pkg);
83 }
84
85 virtual void canNotFindVersion(enum CacheSetHelper::VerSelector const select, APT::VersionContainerInterface * vci, pkgCacheFile &Cache, pkgCache::PkgIterator const &Pkg) {
86 if (select == NEWEST || select == CANDIDATE || select == ALL)
87 virtualPkgs.insert(Pkg);
88 return CacheSetHelper::canNotFindVersion(select, vci, Cache, Pkg);
89 }
90
91 CacheSetHelperVirtuals(bool const ShowErrors = true, GlobalError::MsgType const &ErrorType = GlobalError::NOTICE) : CacheSetHelper(ShowErrors, ErrorType) {}
92 };
93 /*}}}*/
94
95 // CacheSetHelperAPTGet - responsible for message telling from the CacheSets/*{{{*/
96 class CacheSetHelperAPTGet : public APT::CacheSetHelper {
97 /** \brief stream message should be printed to */
98 std::ostream &out;
99 /** \brief were things like Task or RegEx used to select packages? */
100 bool explicitlyNamed;
101
102 APT::PackageSet virtualPkgs;
103
104 public:
105 std::list<std::pair<pkgCache::VerIterator, std::string> > selectedByRelease;
106
107 CacheSetHelperAPTGet(std::ostream &out) : APT::CacheSetHelper(true), out(out) {
108 explicitlyNamed = true;
109 }
110
111 virtual void showTaskSelection(pkgCache::PkgIterator const &Pkg, std::string const &pattern) {
112 ioprintf(out, _("Note, selecting '%s' for task '%s'\n"),
113 Pkg.FullName(true).c_str(), pattern.c_str());
114 explicitlyNamed = false;
115 }
116 virtual void showFnmatchSelection(pkgCache::PkgIterator const &Pkg, std::string const &pattern) {
117 ioprintf(out, _("Note, selecting '%s' for glob '%s'\n"),
118 Pkg.FullName(true).c_str(), pattern.c_str());
119 explicitlyNamed = false;
120 }
121 virtual void showRegExSelection(pkgCache::PkgIterator const &Pkg, std::string const &pattern) {
122 ioprintf(out, _("Note, selecting '%s' for regex '%s'\n"),
123 Pkg.FullName(true).c_str(), pattern.c_str());
124 explicitlyNamed = false;
125 }
126 virtual void showSelectedVersion(pkgCache::PkgIterator const &/*Pkg*/, pkgCache::VerIterator const Ver,
127 std::string const &ver, bool const /*verIsRel*/) {
128 if (ver == Ver.VerStr())
129 return;
130 selectedByRelease.push_back(make_pair(Ver, ver));
131 }
132
133 bool showVirtualPackageErrors(pkgCacheFile &Cache) {
134 if (virtualPkgs.empty() == true)
135 return true;
136 for (APT::PackageSet::const_iterator Pkg = virtualPkgs.begin();
137 Pkg != virtualPkgs.end(); ++Pkg) {
138 if (Pkg->ProvidesList != 0) {
139 ioprintf(c1out,_("Package %s is a virtual package provided by:\n"),
140 Pkg.FullName(true).c_str());
141
142 pkgCache::PrvIterator I = Pkg.ProvidesList();
143 unsigned short provider = 0;
144 for (; I.end() == false; ++I) {
145 pkgCache::PkgIterator Pkg = I.OwnerPkg();
146
147 if (Cache[Pkg].CandidateVerIter(Cache) == I.OwnerVer()) {
148 c1out << " " << Pkg.FullName(true) << " " << I.OwnerVer().VerStr();
149 if (Cache[Pkg].Install() == true && Cache[Pkg].NewInstall() == false)
150 c1out << _(" [Installed]");
151 c1out << std::endl;
152 ++provider;
153 }
154 }
155 // if we found no candidate which provide this package, show non-candidates
156 if (provider == 0)
157 for (I = Pkg.ProvidesList(); I.end() == false; ++I)
158 c1out << " " << I.OwnerPkg().FullName(true) << " " << I.OwnerVer().VerStr()
159 << _(" [Not candidate version]") << std::endl;
160 else
161 out << _("You should explicitly select one to install.") << std::endl;
162 } else {
163 ioprintf(c1out,
164 _("Package %s is not available, but is referred to by another package.\n"
165 "This may mean that the package is missing, has been obsoleted, or\n"
166 "is only available from another source\n"),Pkg.FullName(true).c_str());
167
168 std::string List;
169 std::string VersionsList;
170 SPtrArray<bool> Seen = new bool[Cache.GetPkgCache()->Head().PackageCount];
171 memset(Seen,0,Cache.GetPkgCache()->Head().PackageCount*sizeof(*Seen));
172 for (pkgCache::DepIterator Dep = Pkg.RevDependsList();
173 Dep.end() == false; ++Dep) {
174 if (Dep->Type != pkgCache::Dep::Replaces)
175 continue;
176 if (Seen[Dep.ParentPkg()->ID] == true)
177 continue;
178 Seen[Dep.ParentPkg()->ID] = true;
179 List += Dep.ParentPkg().FullName(true) + " ";
180 //VersionsList += std::string(Dep.ParentPkg().CurVersion) + "\n"; ???
181 }
182 ShowList(c1out,_("However the following packages replace it:"),List,VersionsList);
183 }
184 c1out << std::endl;
185 }
186 return false;
187 }
188
189 virtual pkgCache::VerIterator canNotFindCandidateVer(pkgCacheFile &Cache, pkgCache::PkgIterator const &Pkg) {
190 APT::VersionSet const verset = tryVirtualPackage(Cache, Pkg, CacheSetHelper::CANDIDATE);
191 if (verset.empty() == false)
192 return *(verset.begin());
193 else if (ShowError == true) {
194 _error->Error(_("Package '%s' has no installation candidate"),Pkg.FullName(true).c_str());
195 virtualPkgs.insert(Pkg);
196 }
197 return pkgCache::VerIterator(Cache, 0);
198 }
199
200 virtual pkgCache::VerIterator canNotFindNewestVer(pkgCacheFile &Cache, pkgCache::PkgIterator const &Pkg) {
201 if (Pkg->ProvidesList != 0)
202 {
203 APT::VersionSet const verset = tryVirtualPackage(Cache, Pkg, CacheSetHelper::NEWEST);
204 if (verset.empty() == false)
205 return *(verset.begin());
206 if (ShowError == true)
207 ioprintf(out, _("Virtual packages like '%s' can't be removed\n"), Pkg.FullName(true).c_str());
208 }
209 else
210 {
211 pkgCache::GrpIterator Grp = Pkg.Group();
212 pkgCache::PkgIterator P = Grp.PackageList();
213 for (; P.end() != true; P = Grp.NextPkg(P))
214 {
215 if (P == Pkg)
216 continue;
217 if (P->CurrentVer != 0) {
218 // TRANSLATORS: Note, this is not an interactive question
219 ioprintf(c1out,_("Package '%s' is not installed, so not removed. Did you mean '%s'?\n"),
220 Pkg.FullName(true).c_str(), P.FullName(true).c_str());
221 break;
222 }
223 }
224 if (P.end() == true)
225 ioprintf(c1out,_("Package '%s' is not installed, so not removed\n"),Pkg.FullName(true).c_str());
226 }
227 return pkgCache::VerIterator(Cache, 0);
228 }
229
230 APT::VersionSet tryVirtualPackage(pkgCacheFile &Cache, pkgCache::PkgIterator const &Pkg,
231 CacheSetHelper::VerSelector const select) {
232 /* This is a pure virtual package and there is a single available
233 candidate providing it. */
234 if (unlikely(Cache[Pkg].CandidateVer != 0) || Pkg->ProvidesList == 0)
235 return APT::VersionSet();
236
237 pkgCache::PkgIterator Prov;
238 bool found_one = false;
239 for (pkgCache::PrvIterator P = Pkg.ProvidesList(); P; ++P) {
240 pkgCache::VerIterator const PVer = P.OwnerVer();
241 pkgCache::PkgIterator const PPkg = PVer.ParentPkg();
242
243 /* Ignore versions that are not a candidate. */
244 if (Cache[PPkg].CandidateVer != PVer)
245 continue;
246
247 if (found_one == false) {
248 Prov = PPkg;
249 found_one = true;
250 } else if (PPkg != Prov) {
251 // same group, so it's a foreign package
252 if (PPkg->Group == Prov->Group) {
253 // do we already have the requested arch?
254 if (strcmp(Pkg.Arch(), Prov.Arch()) == 0 ||
255 strcmp(Prov.Arch(), "all") == 0 ||
256 unlikely(strcmp(PPkg.Arch(), Prov.Arch()) == 0)) // packages have only on candidate, but just to be sure
257 continue;
258 // see which architecture we prefer more and switch to it
259 std::vector<std::string> archs = APT::Configuration::getArchitectures();
260 if (std::find(archs.begin(), archs.end(), PPkg.Arch()) < std::find(archs.begin(), archs.end(), Prov.Arch()))
261 Prov = PPkg;
262 continue;
263 }
264 found_one = false; // we found at least two
265 break;
266 }
267 }
268
269 if (found_one == true) {
270 ioprintf(out, _("Note, selecting '%s' instead of '%s'\n"),
271 Prov.FullName(true).c_str(), Pkg.FullName(true).c_str());
272 return APT::VersionSet::FromPackage(Cache, Prov, select, *this);
273 }
274 return APT::VersionSet();
275 }
276
277 inline bool allPkgNamedExplicitly() const { return explicitlyNamed; }
278
279 };
280 /*}}}*/
281
282 #endif