fix and non-silent fail dpkg-overwrite error test
[ntk/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 canNotFindCandidateVer(pkgCacheFile &Cache, pkgCache::PkgIterator const &Pkg) {
80 virtualPkgs.insert(Pkg);
81 return CacheSetHelper::canNotFindCandidateVer(Cache, Pkg);
82 }
83
84 virtual pkgCache::VerIterator canNotFindNewestVer(pkgCacheFile &Cache, pkgCache::PkgIterator const &Pkg) {
85 virtualPkgs.insert(Pkg);
86 return CacheSetHelper::canNotFindNewestVer(Cache, Pkg);
87 }
88
89 virtual void canNotFindAllVer(APT::VersionContainerInterface * vci, pkgCacheFile &Cache, pkgCache::PkgIterator const &Pkg) {
90 virtualPkgs.insert(Pkg);
91 CacheSetHelper::canNotFindAllVer(vci, Cache, Pkg);
92 }
93
94 CacheSetHelperVirtuals(bool const ShowErrors = true, GlobalError::MsgType const &ErrorType = GlobalError::NOTICE) : CacheSetHelper(ShowErrors, ErrorType) {}
95 };
96 /*}}}*/
97
98 // CacheSetHelperAPTGet - responsible for message telling from the CacheSets/*{{{*/
99 class CacheSetHelperAPTGet : public APT::CacheSetHelper {
100 /** \brief stream message should be printed to */
101 std::ostream &out;
102 /** \brief were things like Task or RegEx used to select packages? */
103 bool explicitlyNamed;
104
105 APT::PackageSet virtualPkgs;
106
107 public:
108 std::list<std::pair<pkgCache::VerIterator, std::string> > selectedByRelease;
109
110 CacheSetHelperAPTGet(std::ostream &out) : APT::CacheSetHelper(true), out(out) {
111 explicitlyNamed = true;
112 }
113
114 virtual void showTaskSelection(pkgCache::PkgIterator const &Pkg, std::string const &pattern) {
115 ioprintf(out, _("Note, selecting '%s' for task '%s'\n"),
116 Pkg.FullName(true).c_str(), pattern.c_str());
117 explicitlyNamed = false;
118 }
119 virtual void showFnmatchSelection(pkgCache::PkgIterator const &Pkg, std::string const &pattern) {
120 ioprintf(out, _("Note, selecting '%s' for glob '%s'\n"),
121 Pkg.FullName(true).c_str(), pattern.c_str());
122 explicitlyNamed = false;
123 }
124 virtual void showRegExSelection(pkgCache::PkgIterator const &Pkg, std::string const &pattern) {
125 ioprintf(out, _("Note, selecting '%s' for regex '%s'\n"),
126 Pkg.FullName(true).c_str(), pattern.c_str());
127 explicitlyNamed = false;
128 }
129 virtual void showSelectedVersion(pkgCache::PkgIterator const &/*Pkg*/, pkgCache::VerIterator const Ver,
130 std::string const &ver, bool const /*verIsRel*/) {
131 if (ver == Ver.VerStr())
132 return;
133 selectedByRelease.push_back(make_pair(Ver, ver));
134 }
135
136 bool showVirtualPackageErrors(pkgCacheFile &Cache) {
137 if (virtualPkgs.empty() == true)
138 return true;
139 for (APT::PackageSet::const_iterator Pkg = virtualPkgs.begin();
140 Pkg != virtualPkgs.end(); ++Pkg) {
141 if (Pkg->ProvidesList != 0) {
142 ioprintf(c1out,_("Package %s is a virtual package provided by:\n"),
143 Pkg.FullName(true).c_str());
144
145 pkgCache::PrvIterator I = Pkg.ProvidesList();
146 unsigned short provider = 0;
147 for (; I.end() == false; ++I) {
148 pkgCache::PkgIterator Pkg = I.OwnerPkg();
149
150 if (Cache[Pkg].CandidateVerIter(Cache) == I.OwnerVer()) {
151 c1out << " " << Pkg.FullName(true) << " " << I.OwnerVer().VerStr();
152 if (Cache[Pkg].Install() == true && Cache[Pkg].NewInstall() == false)
153 c1out << _(" [Installed]");
154 c1out << std::endl;
155 ++provider;
156 }
157 }
158 // if we found no candidate which provide this package, show non-candidates
159 if (provider == 0)
160 for (I = Pkg.ProvidesList(); I.end() == false; ++I)
161 c1out << " " << I.OwnerPkg().FullName(true) << " " << I.OwnerVer().VerStr()
162 << _(" [Not candidate version]") << std::endl;
163 else
164 out << _("You should explicitly select one to install.") << std::endl;
165 } else {
166 ioprintf(c1out,
167 _("Package %s is not available, but is referred to by another package.\n"
168 "This may mean that the package is missing, has been obsoleted, or\n"
169 "is only available from another source\n"),Pkg.FullName(true).c_str());
170
171 std::string List;
172 std::string VersionsList;
173 SPtrArray<bool> Seen = new bool[Cache.GetPkgCache()->Head().PackageCount];
174 memset(Seen,0,Cache.GetPkgCache()->Head().PackageCount*sizeof(*Seen));
175 for (pkgCache::DepIterator Dep = Pkg.RevDependsList();
176 Dep.end() == false; ++Dep) {
177 if (Dep->Type != pkgCache::Dep::Replaces)
178 continue;
179 if (Seen[Dep.ParentPkg()->ID] == true)
180 continue;
181 Seen[Dep.ParentPkg()->ID] = true;
182 List += Dep.ParentPkg().FullName(true) + " ";
183 //VersionsList += std::string(Dep.ParentPkg().CurVersion) + "\n"; ???
184 }
185 ShowList(c1out,_("However the following packages replace it:"),List,VersionsList);
186 }
187 c1out << std::endl;
188 }
189 return false;
190 }
191
192 virtual pkgCache::VerIterator canNotFindCandidateVer(pkgCacheFile &Cache, pkgCache::PkgIterator const &Pkg) {
193 APT::VersionSet const verset = tryVirtualPackage(Cache, Pkg, APT::VersionSet::CANDIDATE);
194 if (verset.empty() == false)
195 return *(verset.begin());
196 else if (ShowError == true) {
197 _error->Error(_("Package '%s' has no installation candidate"),Pkg.FullName(true).c_str());
198 virtualPkgs.insert(Pkg);
199 }
200 return pkgCache::VerIterator(Cache, 0);
201 }
202
203 virtual pkgCache::VerIterator canNotFindNewestVer(pkgCacheFile &Cache, pkgCache::PkgIterator const &Pkg) {
204 if (Pkg->ProvidesList != 0)
205 {
206 APT::VersionSet const verset = tryVirtualPackage(Cache, Pkg, APT::VersionSet::NEWEST);
207 if (verset.empty() == false)
208 return *(verset.begin());
209 if (ShowError == true)
210 ioprintf(out, _("Virtual packages like '%s' can't be removed\n"), Pkg.FullName(true).c_str());
211 }
212 else
213 {
214 pkgCache::GrpIterator Grp = Pkg.Group();
215 pkgCache::PkgIterator P = Grp.PackageList();
216 for (; P.end() != true; P = Grp.NextPkg(P))
217 {
218 if (P == Pkg)
219 continue;
220 if (P->CurrentVer != 0) {
221 // TRANSLATORS: Note, this is not an interactive question
222 ioprintf(c1out,_("Package '%s' is not installed, so not removed. Did you mean '%s'?\n"),
223 Pkg.FullName(true).c_str(), P.FullName(true).c_str());
224 break;
225 }
226 }
227 if (P.end() == true)
228 ioprintf(c1out,_("Package '%s' is not installed, so not removed\n"),Pkg.FullName(true).c_str());
229 }
230 return pkgCache::VerIterator(Cache, 0);
231 }
232
233 APT::VersionSet tryVirtualPackage(pkgCacheFile &Cache, pkgCache::PkgIterator const &Pkg,
234 APT::VersionSet::Version const &select) {
235 /* This is a pure virtual package and there is a single available
236 candidate providing it. */
237 if (unlikely(Cache[Pkg].CandidateVer != 0) || Pkg->ProvidesList == 0)
238 return APT::VersionSet();
239
240 pkgCache::PkgIterator Prov;
241 bool found_one = false;
242 for (pkgCache::PrvIterator P = Pkg.ProvidesList(); P; ++P) {
243 pkgCache::VerIterator const PVer = P.OwnerVer();
244 pkgCache::PkgIterator const PPkg = PVer.ParentPkg();
245
246 /* Ignore versions that are not a candidate. */
247 if (Cache[PPkg].CandidateVer != PVer)
248 continue;
249
250 if (found_one == false) {
251 Prov = PPkg;
252 found_one = true;
253 } else if (PPkg != Prov) {
254 // same group, so it's a foreign package
255 if (PPkg->Group == Prov->Group) {
256 // do we already have the requested arch?
257 if (strcmp(Pkg.Arch(), Prov.Arch()) == 0 ||
258 strcmp(Prov.Arch(), "all") == 0 ||
259 unlikely(strcmp(PPkg.Arch(), Prov.Arch()) == 0)) // packages have only on candidate, but just to be sure
260 continue;
261 // see which architecture we prefer more and switch to it
262 std::vector<std::string> archs = APT::Configuration::getArchitectures();
263 if (std::find(archs.begin(), archs.end(), PPkg.Arch()) < std::find(archs.begin(), archs.end(), Prov.Arch()))
264 Prov = PPkg;
265 continue;
266 }
267 found_one = false; // we found at least two
268 break;
269 }
270 }
271
272 if (found_one == true) {
273 ioprintf(out, _("Note, selecting '%s' instead of '%s'\n"),
274 Prov.FullName(true).c_str(), Pkg.FullName(true).c_str());
275 return APT::VersionSet::FromPackage(Cache, Prov, select, *this);
276 }
277 return APT::VersionSet();
278 }
279
280 inline bool allPkgNamedExplicitly() const { return explicitlyNamed; }
281
282 };
283 /*}}}*/
284
285 #endif