1
// -*- mode: cpp; mode: fold -*-
3
// $Id: policy.cc,v 1.8 2001/05/27 23:40:56 jgg Exp $
4
/* ######################################################################
6
Package Version Policy implementation
8
This is just a really simple wrapper around pkgVersionMatch with
9
some added goodies to manage the list of things..
13
1000 -> inf = Downgradeable priorities
14
1000 = The 'no downgrade' pseduo-status file
15
100 -> 1000 = Standard priorities
16
990 = Config file override package files
17
989 = Start for preference auto-priorities
18
500 = Default package files
20
0 -> 100 = NotAutomatic sources like experimental
21
-inf -> 0 = Never selected
23
##################################################################### */
25
// Include Files /*{{{*/
27
#pragma implementation "apt-pkg/policy.h"
29
#include <apt-pkg/policy.h>
30
#include <apt-pkg/configuration.h>
31
#include <apt-pkg/tagfile.h>
32
#include <apt-pkg/strutl.h>
33
#include <apt-pkg/error.h>
34
#include <apt-pkg/sptr.h>
43
// Policy::Init - Startup and bind to a cache /*{{{*/
44
// ---------------------------------------------------------------------
45
/* Set the defaults for operation. The default mode with no loaded policy
46
file matches the V0 policy engine. */
47
pkgPolicy::pkgPolicy(pkgCache *Owner) : Pins(0), PFPriority(0), Cache(Owner)
49
PFPriority = new signed short[Owner->Head().PackageFileCount];
50
Pins = new Pin[Owner->Head().PackageCount];
52
for (unsigned long I = 0; I != Owner->Head().PackageCount; I++)
53
Pins[I].Type = pkgVersionMatch::None;
55
// The config file has a master override.
56
string DefRel = _config->Find("APT::Default-Release");
57
if (DefRel.empty() == false)
58
CreatePin(pkgVersionMatch::Release,"",DefRel,990);
63
// Policy::InitDefaults - Compute the default selections /*{{{*/
64
// ---------------------------------------------------------------------
66
bool pkgPolicy::InitDefaults()
68
// Initialize the priorities based on the status of the package file
69
for (pkgCache::PkgFileIterator I = Cache->FileBegin(); I != Cache->FileEnd(); I++)
71
PFPriority[I->ID] = 500;
72
if ((I->Flags & pkgCache::Flag::NotSource) == pkgCache::Flag::NotSource)
73
PFPriority[I->ID] = 100;
75
if ((I->Flags & pkgCache::Flag::NotAutomatic) == pkgCache::Flag::NotAutomatic)
76
PFPriority[I->ID] = 1;
79
// Apply the defaults..
80
SPtrArray<bool> Fixed = new bool[Cache->HeaderP->PackageFileCount];
81
memset(Fixed,0,sizeof(*Fixed)*Cache->HeaderP->PackageFileCount);
83
StatusOverride = false;
84
for (vector<Pin>::const_iterator I = Defaults.begin(); I != Defaults.end();
87
pkgVersionMatch Match(I->Data,I->Type);
88
for (pkgCache::PkgFileIterator F = Cache->FileBegin(); F != Cache->FileEnd(); F++)
90
if (Match.FileMatch(F) == true && Fixed[F->ID] == false)
92
if (I->Priority != 0 && I->Priority > 0)
96
PFPriority[F->ID] = I->Priority;
98
PFPriority[F->ID] = Cur;
100
if (PFPriority[F->ID] > 1000)
101
StatusOverride = true;
108
if (_config->FindB("Debug::pkgPolicy",false) == true)
109
for (pkgCache::PkgFileIterator F = Cache->FileBegin(); F != Cache->FileEnd(); F++)
110
cout << "Prio of " << F.FileName() << ' ' << PFPriority[F->ID] << endl;
115
// Policy::GetCandidateVer - Get the candidate install version /*{{{*/
116
// ---------------------------------------------------------------------
117
/* Evaluate the package pins and the default list to deteremine what the
119
pkgCache::VerIterator pkgPolicy::GetCandidateVer(pkgCache::PkgIterator Pkg)
121
// Look for a package pin and evaluate it.
122
signed Max = GetPriority(Pkg);
123
pkgCache::VerIterator Pref = GetMatch(Pkg);
125
/* Falling through to the default version.. Setting Max to zero
126
effectively excludes everything <= 0 which are the non-automatic
127
priorities.. The status file is given a prio of 100 which will exclude
128
not-automatic sources, except in a single shot not-installed mode.
129
The second pseduo-status file is at prio 1000, above which will permit
130
the user to force-downgrade things.
132
The user pin is subject to the same priority rules as default
133
selections. Thus there are two ways to create a pin - a pin that
134
tracks the default when the default is taken away, and a permanent
135
pin that stays at that setting.
137
for (pkgCache::VerIterator Ver = Pkg.VersionList(); Ver.end() == false; Ver++)
139
for (pkgCache::VerFileIterator VF = Ver.FileList(); VF.end() == false; VF++)
141
/* If this is the status file, and the current version is not the
142
version in the status file (ie it is not installed, or somesuch)
143
then it is not a candidate for installation, ever. This weeds
144
out bogus entries that may be due to config-file states, or
146
if ((VF.File()->Flags & pkgCache::Flag::NotSource) == pkgCache::Flag::NotSource &&
147
Pkg.CurrentVer() != Ver)
150
signed Prio = PFPriority[VF.File()->ID];
158
if (Pkg.CurrentVer() == Ver && Max < 1000)
160
/* Elevate our current selection (or the status file itself)
161
to the Pseudo-status priority. */
162
if (Pref.end() == true)
166
// Fast path optimize.
167
if (StatusOverride == false)
174
// Policy::CreatePin - Create an entry in the pin table.. /*{{{*/
175
// ---------------------------------------------------------------------
176
/* For performance we have 3 tables, the default table, the main cache
177
table (hashed to the cache). A blank package name indicates the pin
178
belongs to the default table. Order of insertion matters here, the
179
earlier defaults override later ones. */
180
void pkgPolicy::CreatePin(pkgVersionMatch::MatchType Type,string Name,
181
string Data,signed short Priority)
185
if (Name.empty() == true)
186
P = &*Defaults.insert(Defaults.end());
189
// Get a spot to put the pin
190
pkgCache::PkgIterator Pkg = Cache->FindPkg(Name);
191
if (Pkg.end() == true)
193
// Check the unmatched table
194
for (vector<PkgPin>::iterator I = Unmatched.begin();
195
I != Unmatched.end() && P == 0; I++)
200
P = &*Unmatched.insert(Unmatched.end());
210
P->Priority = Priority;
214
// Policy::GetMatch - Get the matching version for a package pin /*{{{*/
215
// ---------------------------------------------------------------------
217
pkgCache::VerIterator pkgPolicy::GetMatch(pkgCache::PkgIterator Pkg)
219
const Pin &PPkg = Pins[Pkg->ID];
220
if (PPkg.Type != pkgVersionMatch::None)
222
pkgVersionMatch Match(PPkg.Data,PPkg.Type);
223
return Match.Find(Pkg);
225
return pkgCache::VerIterator(*Pkg.Cache());
228
// Policy::GetPriority - Get the priority of the package pin /*{{{*/
229
// ---------------------------------------------------------------------
231
signed short pkgPolicy::GetPriority(pkgCache::PkgIterator const &Pkg)
233
if (Pins[Pkg->ID].Type != pkgVersionMatch::None)
235
// In this case 0 means default priority
236
if (Pins[Pkg->ID].Priority == 0)
238
return Pins[Pkg->ID].Priority;
245
// ReadPinFile - Load the pin file into a Policy /*{{{*/
246
// ---------------------------------------------------------------------
247
/* I'd like to see the preferences file store more than just pin information
248
but right now that is the only stuff I have to store. Later there will
249
have to be some kind of combined super parser to get the data into all
250
the right classes.. */
251
bool ReadPinFile(pkgPolicy &Plcy,string File)
253
if (File.empty() == true)
254
File = _config->FindFile("Dir::Etc::Preferences");
256
if (FileExists(File) == false)
259
FileFd Fd(File,FileFd::ReadOnly);
261
if (_error->PendingError() == true)
265
while (TF.Step(Tags) == true)
267
string Name = Tags.FindS("Package");
268
if (Name.empty() == true)
269
return _error->Error(_("Invalid record in the preferences file, no Package header"));
275
if (Tags.Find("Pin",Start,End) == false)
278
const char *Word = Start;
279
for (; Word != End && isspace(*Word) == 0; Word++);
282
pkgVersionMatch::MatchType Type;
283
if (stringcasecmp(Start,Word,"version") == 0 && Name.empty() == false)
284
Type = pkgVersionMatch::Version;
285
else if (stringcasecmp(Start,Word,"release") == 0)
286
Type = pkgVersionMatch::Release;
287
else if (stringcasecmp(Start,Word,"origin") == 0)
288
Type = pkgVersionMatch::Origin;
291
_error->Warning(_("Did not understand pin type %s"),string(Start,Word).c_str());
294
for (; Word != End && isspace(*Word) != 0; Word++);
296
Plcy.CreatePin(Type,Name,string(Word,End),
297
Tags.FindI("Pin-Priority"));