1 #ifndef APT_PRIVATE_CACHESET_H
2 #define APT_PRIVATE_CACHESET_H
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>
24 #include "private-output.h"
30 struct VersionSortDescriptionLocality
32 bool operator () (const pkgCache::VerIterator
&v_lhs
,
33 const pkgCache::VerIterator
&v_rhs
)
35 pkgCache::DescFile
*A
= v_lhs
.TranslatedDescription().FileList();
36 pkgCache::DescFile
*B
= v_rhs
.TranslatedDescription().FileList();
46 if (A
->File
== B
->File
)
47 return A
->Offset
< B
->Offset
;
49 return A
->File
< B
->File
;
53 // sorted by locality which makes iterating much faster
54 typedef APT::VersionContainer
<
55 std::set
<pkgCache::VerIterator
,
56 VersionSortDescriptionLocality
> > LocalitySortedVersionSet
;
60 virtual bool operator () (const pkgCache::PkgIterator
&/*P*/) {
64 // FIXME: add default argument for OpProgress (or overloaded function)
65 bool GetLocalitySortedVersionSet(pkgCacheFile
&CacheFile
,
66 LocalitySortedVersionSet
&output_set
,
68 OpProgress
&progress
);
69 bool GetLocalitySortedVersionSet(pkgCacheFile
&CacheFile
,
70 LocalitySortedVersionSet
&output_set
,
71 OpProgress
&progress
);
74 // CacheSetHelper saving virtual packages /*{{{*/
75 class CacheSetHelperVirtuals
: public APT::CacheSetHelper
{
77 APT::PackageSet virtualPkgs
;
79 virtual pkgCache::VerIterator
canNotFindCandidateVer(pkgCacheFile
&Cache
, pkgCache::PkgIterator
const &Pkg
) {
80 virtualPkgs
.insert(Pkg
);
81 return CacheSetHelper::canNotFindCandidateVer(Cache
, Pkg
);
84 virtual pkgCache::VerIterator
canNotFindNewestVer(pkgCacheFile
&Cache
, pkgCache::PkgIterator
const &Pkg
) {
85 virtualPkgs
.insert(Pkg
);
86 return CacheSetHelper::canNotFindNewestVer(Cache
, Pkg
);
89 virtual void canNotFindAllVer(APT::VersionContainerInterface
* vci
, pkgCacheFile
&Cache
, pkgCache::PkgIterator
const &Pkg
) {
90 virtualPkgs
.insert(Pkg
);
91 CacheSetHelper::canNotFindAllVer(vci
, Cache
, Pkg
);
94 CacheSetHelperVirtuals(bool const ShowErrors
= true, GlobalError::MsgType
const &ErrorType
= GlobalError::NOTICE
) : CacheSetHelper(ShowErrors
, ErrorType
) {}
98 // CacheSetHelperAPTGet - responsible for message telling from the CacheSets/*{{{*/
99 class CacheSetHelperAPTGet
: public APT::CacheSetHelper
{
100 /** \brief stream message should be printed to */
102 /** \brief were things like Task or RegEx used to select packages? */
103 bool explicitlyNamed
;
105 APT::PackageSet virtualPkgs
;
108 std::list
<std::pair
<pkgCache::VerIterator
, std::string
> > selectedByRelease
;
110 CacheSetHelperAPTGet(std::ostream
&out
) : APT::CacheSetHelper(true), out(out
) {
111 explicitlyNamed
= true;
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;
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;
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;
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())
133 selectedByRelease
.push_back(make_pair(Ver
, ver
));
136 bool showVirtualPackageErrors(pkgCacheFile
&Cache
) {
137 if (virtualPkgs
.empty() == 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());
145 pkgCache::PrvIterator I
= Pkg
.ProvidesList();
146 unsigned short provider
= 0;
147 for (; I
.end() == false; ++I
) {
148 pkgCache::PkgIterator Pkg
= I
.OwnerPkg();
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]");
158 // if we found no candidate which provide this package, show non-candidates
160 for (I
= Pkg
.ProvidesList(); I
.end() == false; ++I
)
161 c1out
<< " " << I
.OwnerPkg().FullName(true) << " " << I
.OwnerVer().VerStr()
162 << _(" [Not candidate version]") << std::endl
;
164 out
<< _("You should explicitly select one to install.") << std::endl
;
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());
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
)
179 if (Seen
[Dep
.ParentPkg()->ID
] == true)
181 Seen
[Dep
.ParentPkg()->ID
] = true;
182 List
+= Dep
.ParentPkg().FullName(true) + " ";
183 //VersionsList += std::string(Dep.ParentPkg().CurVersion) + "\n"; ???
185 ShowList(c1out
,_("However the following packages replace it:"),List
,VersionsList
);
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
);
200 return pkgCache::VerIterator(Cache
, 0);
203 virtual pkgCache::VerIterator
canNotFindNewestVer(pkgCacheFile
&Cache
, pkgCache::PkgIterator
const &Pkg
) {
204 if (Pkg
->ProvidesList
!= 0)
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());
214 pkgCache::GrpIterator Grp
= Pkg
.Group();
215 pkgCache::PkgIterator P
= Grp
.PackageList();
216 for (; P
.end() != true; P
= Grp
.NextPkg(P
))
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());
228 ioprintf(c1out
,_("Package '%s' is not installed, so not removed\n"),Pkg
.FullName(true).c_str());
230 return pkgCache::VerIterator(Cache
, 0);
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();
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();
246 /* Ignore versions that are not a candidate. */
247 if (Cache
[PPkg
].CandidateVer
!= PVer
)
250 if (found_one
== false) {
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
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()))
267 found_one
= false; // we found at least two
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);
277 return APT::VersionSet();
280 inline bool allPkgNamedExplicitly() const { return explicitlyNamed
; }