]> git.saurik.com Git - apt.git/blame - apt-pkg/policy.cc
Core correctness patches
[apt.git] / apt-pkg / policy.cc
CommitLineData
b2e465d6
AL
1// -*- mode: cpp; mode: fold -*-
2// Description /*{{{*/
20ebd488 3// $Id: policy.cc,v 1.5 2001/03/13 05:23:42 jgg Exp $
b2e465d6
AL
4/* ######################################################################
5
6 Package Version Policy implementation
7
8 This is just a really simple wrapper around pkgVersionMatch with
9 some added goodies to manage the list of things..
10
11 Priority Table:
12
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
19 100 = The status file
20 0 -> 100 = NotAutomatic sources like experimental
21 -inf -> 0 = Never selected
22
23 ##################################################################### */
24 /*}}}*/
25// Include Files /*{{{*/
26#ifdef __GNUG__
27#pragma implementation "apt-pkg/policy.h"
28#endif
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>
35
36#include <apti18n.h>
37 /*}}}*/
38
39// Policy::Init - Startup and bind to a cache /*{{{*/
40// ---------------------------------------------------------------------
41/* Set the defaults for operation. The default mode with no loaded policy
42 file matches the V0 policy engine. */
43pkgPolicy::pkgPolicy(pkgCache *Owner) : Pins(0), PFPriority(0), Cache(Owner)
44{
45 PFPriority = new signed short[Owner->Head().PackageFileCount];
46 Pins = new Pin[Owner->Head().PackageCount];
47
48 for (unsigned long I = 0; I != Owner->Head().PackageCount; I++)
49 Pins[I].Type = pkgVersionMatch::None;
50
51 // The config file has a master override.
52 string DefRel = _config->Find("APT::Default-Release");
53 if (DefRel.empty() == false)
54 CreatePin(pkgVersionMatch::Release,"",DefRel,990);
55
56 InitDefaults();
57}
58 /*}}}*/
59// Policy::InitDefaults - Compute the default selections /*{{{*/
60// ---------------------------------------------------------------------
61/* */
62bool pkgPolicy::InitDefaults()
63{
64 // Initialize the priorities based on the status of the package file
65 for (pkgCache::PkgFileIterator I = Cache->FileBegin(); I != Cache->FileEnd(); I++)
66 {
67 PFPriority[I->ID] = 500;
68 if ((I->Flags & pkgCache::Flag::NotSource) == pkgCache::Flag::NotSource)
69 PFPriority[I->ID] = 100;
70 else
71 if ((I->Flags & pkgCache::Flag::NotAutomatic) == pkgCache::Flag::NotAutomatic)
72 PFPriority[I->ID] = 1;
73 }
74
75 // Apply the defaults..
20ebd488 76 SPtrArray<bool> Fixed = new bool[Cache->HeaderP->PackageFileCount];
b2e465d6
AL
77 memset(Fixed,0,sizeof(*Fixed)*Cache->HeaderP->PackageFileCount);
78 signed Cur = 989;
79 StatusOverride = false;
80 for (vector<Pin>::const_iterator I = Defaults.begin(); I != Defaults.end();
81 I++, Cur--)
82 {
83 pkgVersionMatch Match(I->Data,I->Type);
84 for (pkgCache::PkgFileIterator F = Cache->FileBegin(); F != Cache->FileEnd(); F++)
85 {
86/* hmm?
87 if ((F->Flags & pkgCache::Flag::NotSource) == pkgCache::Flag::NotSource)
88 continue;*/
89
90 if (Match.FileMatch(F) == true && Fixed[F->ID] == false)
91 {
92 if (I->Priority != 0 && I->Priority > 0)
93 Cur = I->Priority;
94
95 if (I->Priority < 0)
96 PFPriority[F->ID] = I->Priority;
97 else
98 PFPriority[F->ID] = Cur;
99
100 if (PFPriority[F->ID] > 1000)
101 StatusOverride = true;
102
103 Fixed[F->ID] = true;
104 }
105 }
106 }
107
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;
111
112 return true;
113}
114 /*}}}*/
115// Policy::GetCandidateVer - Get the candidate install version /*{{{*/
116// ---------------------------------------------------------------------
117/* Evaluate the package pins and the default list to deteremine what the
118 best package is. */
119pkgCache::VerIterator pkgPolicy::GetCandidateVer(pkgCache::PkgIterator Pkg)
120{
121 const Pin &PPkg = Pins[Pkg->ID];
122
123 // Look for a package pin and evaluate it.
124 signed Max = 0;
125 pkgCache::VerIterator Pref(*Cache);
126 if (PPkg.Type != pkgVersionMatch::None)
127 {
128 pkgVersionMatch Match(PPkg.Data,PPkg.Type);
129 Pref = Match.Find(Pkg);
130 Max = PPkg.Priority;
131 if (PPkg.Priority == 0)
132 Max = 989;
133 }
134
135 /* Falling through to the default version.. Setting Max to zero
136 effectively excludes everything <= 0 which are the non-automatic
137 priorities.. The status file is given a prio of 100 which will exclude
138 not-automatic sources, except in a single shot not-installed mode.
139 The second pseduo-status file is at prio 1000, above which will permit
140 the user to force-downgrade things.
141
142 The user pin is subject to the same priority rules as default
143 selections. Thus there are two ways to create a pin - a pin that
144 tracks the default when the default is taken away, and a permanent
145 pin that stays at that setting.
146 */
147 for (pkgCache::VerIterator Ver = Pkg.VersionList(); Ver.end() == false; Ver++)
10639577 148 {
b2e465d6
AL
149 for (pkgCache::VerFileIterator VF = Ver.FileList(); VF.end() == false; VF++)
150 {
6aeda9fa
AL
151 /* If this is the status file, and the current version is not the
152 version in the status file (ie it is not installed, or somesuch)
153 then it is not a candidate for installation, ever. This weeds
154 out bogus entries that may be due to config-file states, or
155 other. */
156 if ((VF.File()->Flags & pkgCache::Flag::NotSource) == pkgCache::Flag::NotSource &&
157 Pkg.CurrentVer() != Ver)
158 continue;
159
b2e465d6
AL
160 signed Prio = PFPriority[VF.File()->ID];
161 if (Prio > Max)
162 {
163 Pref = Ver;
164 Max = Prio;
165 }
166 }
167
168 if (Pkg.CurrentVer() == Ver && Max < 1000)
169 {
170 /* Elevate our current selection (or the status file itself)
171 to the Pseudo-status priority. */
172 if (Pref.end() == true)
173 Pref = Ver;
174 Max = 1000;
175
176 // Fast path optimize.
177 if (StatusOverride == false)
178 break;
179 }
180 }
181
182 return Pref;
183}
184 /*}}}*/
185// Policy::CreatePin - Create an entry in the pin table.. /*{{{*/
186// ---------------------------------------------------------------------
187/* For performance we have 3 tables, the default table, the main cache
188 table (hashed to the cache). A blank package name indicates the pin
189 belongs to the default table. Order of insertion matters here, the
190 earlier defaults override later ones. */
191void pkgPolicy::CreatePin(pkgVersionMatch::MatchType Type,string Name,
192 string Data,signed short Priority)
193{
194 pkgCache::PkgIterator Pkg = Cache->FindPkg(Name);
195 Pin *P = 0;
196
197 if (Name.empty() == true)
198 P = Defaults.insert(Defaults.end());
199 else
200 {
201 // Get a spot to put the pin
202 if (Pkg.end() == true)
203 {
204 // Check the unmatched table
205 for (vector<PkgPin>::iterator I = Unmatched.begin();
206 I != Unmatched.end() && P == 0; I++)
207 if (I->Pkg == Name)
208 P = I;
209
210 if (P == 0)
211 P = Unmatched.insert(Unmatched.end());
212 }
213 else
214 {
215 P = Pins + Pkg->ID;
216 }
217 }
218
219 // Set..
220 P->Type = Type;
221 P->Priority = Priority;
222 P->Data = Data;
223}
224 /*}}}*/
225
226// ReadPinFile - Load the pin file into a Policy /*{{{*/
227// ---------------------------------------------------------------------
228/* I'd like to see the preferences file store more than just pin information
229 but right now that is the only stuff I have to store. Later there will
230 have to be some kind of combined super parser to get the data into all
231 the right classes.. */
232bool ReadPinFile(pkgPolicy &Plcy,string File)
233{
234 if (File.empty() == true)
235 File = _config->FindFile("Dir::Etc::Preferences");
236
237 if (FileExists(File) == false)
238 return true;
239
240 FileFd Fd(File,FileFd::ReadOnly);
241 pkgTagFile TF(&Fd);
242 if (_error->PendingError() == true)
243 return false;
244
245 pkgTagSection Tags;
246 while (TF.Step(Tags) == true)
247 {
248 string Name = Tags.FindS("Package");
249 if (Name.empty() == true)
250 return _error->Error(_("Invalid record in the preferences file, no Package header"));
251 if (Name == "*")
252 Name = string();
253
254 const char *Start;
255 const char *End;
256 if (Tags.Find("Pin",Start,End) == false)
257 continue;
258
259 const char *Word = Start;
260 for (; Word != End && isspace(*Word) == 0; Word++);
261
262 // Parse the type..
263 pkgVersionMatch::MatchType Type;
264 if (stringcasecmp(Start,Word,"version") == 0 && Name.empty() == false)
265 Type = pkgVersionMatch::Version;
266 else if (stringcasecmp(Start,Word,"release") == 0)
267 Type = pkgVersionMatch::Release;
268 else if (stringcasecmp(Start,Word,"origin") == 0)
269 Type = pkgVersionMatch::Origin;
270 else
271 {
272 _error->Warning(_("Did not understand pin type %s"),string(Start,Word).c_str());
273 continue;
274 }
275 for (; Word != End && isspace(*Word) != 0; Word++);
276
277 Plcy.CreatePin(Type,Name,string(Word,End),
278 Tags.FindI("Pin-Priority"));
279 }
280
281 Plcy.InitDefaults();
282 return true;
283}
284 /*}}}*/