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