summaryrefslogtreecommitdiff
path: root/apt-private/private-cacheset.h
blob: 322b3be6b95dbbb29ae1e9fa10711f577abe2283 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#ifndef APT_PRIVATE_CACHESET_H
#define APT_PRIVATE_CACHESET_H

#include <apt-pkg/cachefile.h>
#include <apt-pkg/cacheset.h>
#include <apt-pkg/sptr.h>

#include <algorithm>
#include <vector>

#include "private-output.h"

#include <apti18n.h>

struct VersionSortDescriptionLocality
{
   bool operator () (const pkgCache::VerIterator &v_lhs, 
                     const pkgCache::VerIterator &v_rhs)
    {
        pkgCache::DescFile *A = v_lhs.TranslatedDescription().FileList();
        pkgCache::DescFile *B = v_rhs.TranslatedDescription().FileList();
        if (A == 0 && B == 0)
           return false;

       if (A == 0)
          return true;

       if (B == 0)
          return false;

       if (A->File == B->File)
          return A->Offset < B->Offset;

       return A->File < B->File;
    }
};

// sorted by locality which makes iterating much faster
typedef APT::VersionContainer<
   std::set<pkgCache::VerIterator,
            VersionSortDescriptionLocality> > LocalitySortedVersionSet;

class Matcher {
public:
    virtual bool operator () (const pkgCache::PkgIterator &P) {
        return true;};
};

// FIXME: add default argument for OpProgress (or overloaded function)
bool GetLocalitySortedVersionSet(pkgCacheFile &CacheFile, 
                                    LocalitySortedVersionSet &output_set,
                                    Matcher &matcher,
                                    OpProgress &progress);
bool GetLocalitySortedVersionSet(pkgCacheFile &CacheFile, 
                                    LocalitySortedVersionSet &output_set,
                                    OpProgress &progress);


// CacheSetHelper saving virtual packages				/*{{{*/
class CacheSetHelperVirtuals: public APT::CacheSetHelper {
public:
   APT::PackageSet virtualPkgs;

   virtual pkgCache::VerIterator canNotFindCandidateVer(pkgCacheFile &Cache, pkgCache::PkgIterator const &Pkg) {
      virtualPkgs.insert(Pkg);
      return CacheSetHelper::canNotFindCandidateVer(Cache, Pkg);
   }

   virtual pkgCache::VerIterator canNotFindNewestVer(pkgCacheFile &Cache, pkgCache::PkgIterator const &Pkg) {
      virtualPkgs.insert(Pkg);
      return CacheSetHelper::canNotFindNewestVer(Cache, Pkg);
   }

   virtual void canNotFindAllVer(APT::VersionContainerInterface * vci, pkgCacheFile &Cache, pkgCache::PkgIterator const &Pkg) {
      virtualPkgs.insert(Pkg);
      CacheSetHelper::canNotFindAllVer(vci, Cache, Pkg);
   }

   CacheSetHelperVirtuals(bool const ShowErrors = true, GlobalError::MsgType const &ErrorType = GlobalError::NOTICE) : CacheSetHelper(ShowErrors, ErrorType) {}
};
									/*}}}*/

// CacheSetHelperAPTGet - responsible for message telling from the CacheSets/*{{{*/
class CacheSetHelperAPTGet : public APT::CacheSetHelper {
	/** \brief stream message should be printed to */
	std::ostream &out;
	/** \brief were things like Task or RegEx used to select packages? */
	bool explicitlyNamed;

	APT::PackageSet virtualPkgs;

public:
	std::list<std::pair<pkgCache::VerIterator, std::string> > selectedByRelease;

	CacheSetHelperAPTGet(std::ostream &out) : APT::CacheSetHelper(true), out(out) {
		explicitlyNamed = true;
	}

	virtual void showTaskSelection(pkgCache::PkgIterator const &Pkg, std::string const &pattern) {
		ioprintf(out, _("Note, selecting '%s' for task '%s'\n"),
				Pkg.FullName(true).c_str(), pattern.c_str());
		explicitlyNamed = false;
	}
        virtual void showFnmatchSelection(pkgCache::PkgIterator const &Pkg, std::string const &pattern) {
		ioprintf(out, _("Note, selecting '%s' for glob '%s'\n"),
				Pkg.FullName(true).c_str(), pattern.c_str());
		explicitlyNamed = false;
	}
	virtual void showRegExSelection(pkgCache::PkgIterator const &Pkg, std::string const &pattern) {
		ioprintf(out, _("Note, selecting '%s' for regex '%s'\n"),
				Pkg.FullName(true).c_str(), pattern.c_str());
		explicitlyNamed = false;
	}
	virtual void showSelectedVersion(pkgCache::PkgIterator const &Pkg, pkgCache::VerIterator const Ver,
				 std::string const &ver, bool const verIsRel) {
		if (ver == Ver.VerStr())
			return;
		selectedByRelease.push_back(make_pair(Ver, ver));
	}

	bool showVirtualPackageErrors(pkgCacheFile &Cache) {
		if (virtualPkgs.empty() == true)
			return true;
		for (APT::PackageSet::const_iterator Pkg = virtualPkgs.begin();
		     Pkg != virtualPkgs.end(); ++Pkg) {
			if (Pkg->ProvidesList != 0) {
				ioprintf(c1out,_("Package %s is a virtual package provided by:\n"),
					 Pkg.FullName(true).c_str());

				pkgCache::PrvIterator I = Pkg.ProvidesList();
				unsigned short provider = 0;
				for (; I.end() == false; ++I) {
					pkgCache::PkgIterator Pkg = I.OwnerPkg();

					if (Cache[Pkg].CandidateVerIter(Cache) == I.OwnerVer()) {
						c1out << "  " << Pkg.FullName(true) << " " << I.OwnerVer().VerStr();
						if (Cache[Pkg].Install() == true && Cache[Pkg].NewInstall() == false)
							c1out << _(" [Installed]");
						c1out << std::endl;
						++provider;
					}
				}
				// if we found no candidate which provide this package, show non-candidates
				if (provider == 0)
					for (I = Pkg.ProvidesList(); I.end() == false; ++I)
						c1out << "  " << I.OwnerPkg().FullName(true) << " " << I.OwnerVer().VerStr()
                                                      << _(" [Not candidate version]") << std::endl;
				else
                                   out << _("You should explicitly select one to install.") << std::endl;
			} else {
				ioprintf(c1out,
					_("Package %s is not available, but is referred to by another package.\n"
					  "This may mean that the package is missing, has been obsoleted, or\n"
					  "is only available from another source\n"),Pkg.FullName(true).c_str());

				std::string List;
				std::string VersionsList;
				SPtrArray<bool> Seen = new bool[Cache.GetPkgCache()->Head().PackageCount];
				memset(Seen,0,Cache.GetPkgCache()->Head().PackageCount*sizeof(*Seen));
				for (pkgCache::DepIterator Dep = Pkg.RevDependsList();
				     Dep.end() == false; ++Dep) {
					if (Dep->Type != pkgCache::Dep::Replaces)
						continue;
					if (Seen[Dep.ParentPkg()->ID] == true)
						continue;
					Seen[Dep.ParentPkg()->ID] = true;
					List += Dep.ParentPkg().FullName(true) + " ";
					//VersionsList += std::string(Dep.ParentPkg().CurVersion) + "\n"; ???
				}
				ShowList(c1out,_("However the following packages replace it:"),List,VersionsList);
			}
			c1out << std::endl;
		}
		return false;
	}

	virtual pkgCache::VerIterator canNotFindCandidateVer(pkgCacheFile &Cache, pkgCache::PkgIterator const &Pkg) {
		APT::VersionSet const verset = tryVirtualPackage(Cache, Pkg, APT::VersionSet::CANDIDATE);
		if (verset.empty() == false)
			return *(verset.begin());
		else if (ShowError == true) {
			_error->Error(_("Package '%s' has no installation candidate"),Pkg.FullName(true).c_str());
			virtualPkgs.insert(Pkg);
		}
		return pkgCache::VerIterator(Cache, 0);
	}

	virtual pkgCache::VerIterator canNotFindNewestVer(pkgCacheFile &Cache, pkgCache::PkgIterator const &Pkg) {
		if (Pkg->ProvidesList != 0)
		{
			APT::VersionSet const verset = tryVirtualPackage(Cache, Pkg, APT::VersionSet::NEWEST);
			if (verset.empty() == false)
				return *(verset.begin());
			if (ShowError == true)
				ioprintf(out, _("Virtual packages like '%s' can't be removed\n"), Pkg.FullName(true).c_str());
		}
		else
		{
			pkgCache::GrpIterator Grp = Pkg.Group();
			pkgCache::PkgIterator P = Grp.PackageList();
			for (; P.end() != true; P = Grp.NextPkg(P))
			{
				if (P == Pkg)
					continue;
				if (P->CurrentVer != 0) {
					// TRANSLATORS: Note, this is not an interactive question
					ioprintf(c1out,_("Package '%s' is not installed, so not removed. Did you mean '%s'?\n"),
						 Pkg.FullName(true).c_str(), P.FullName(true).c_str());
					break;
				}
			}
			if (P.end() == true)
				ioprintf(c1out,_("Package '%s' is not installed, so not removed\n"),Pkg.FullName(true).c_str());
		}
		return pkgCache::VerIterator(Cache, 0);
	}

	APT::VersionSet tryVirtualPackage(pkgCacheFile &Cache, pkgCache::PkgIterator const &Pkg,
						APT::VersionSet::Version const &select) {
		/* This is a pure virtual package and there is a single available
		   candidate providing it. */
		if (unlikely(Cache[Pkg].CandidateVer != 0) || Pkg->ProvidesList == 0)
			return APT::VersionSet();

		pkgCache::PkgIterator Prov;
		bool found_one = false;
		for (pkgCache::PrvIterator P = Pkg.ProvidesList(); P; ++P) {
			pkgCache::VerIterator const PVer = P.OwnerVer();
			pkgCache::PkgIterator const PPkg = PVer.ParentPkg();

			/* Ignore versions that are not a candidate. */
			if (Cache[PPkg].CandidateVer != PVer)
				continue;

			if (found_one == false) {
				Prov = PPkg;
				found_one = true;
			} else if (PPkg != Prov) {
				// same group, so it's a foreign package
				if (PPkg->Group == Prov->Group) {
					// do we already have the requested arch?
					if (strcmp(Pkg.Arch(), Prov.Arch()) == 0 ||
					    strcmp(Prov.Arch(), "all") == 0 ||
					    unlikely(strcmp(PPkg.Arch(), Prov.Arch()) == 0)) // packages have only on candidate, but just to be sure
						continue;
					// see which architecture we prefer more and switch to it
					std::vector<std::string> archs = APT::Configuration::getArchitectures();
					if (std::find(archs.begin(), archs.end(), PPkg.Arch()) < std::find(archs.begin(), archs.end(), Prov.Arch()))
						Prov = PPkg;
					continue;
				}
				found_one = false; // we found at least two
				break;
			}
		}

		if (found_one == true) {
			ioprintf(out, _("Note, selecting '%s' instead of '%s'\n"),
				 Prov.FullName(true).c_str(), Pkg.FullName(true).c_str());
			return APT::VersionSet::FromPackage(Cache, Prov, select, *this);
		}
		return APT::VersionSet();
	}

	inline bool allPkgNamedExplicitly() const { return explicitlyNamed; }

};
									/*}}}*/

#endif