grr, nevermind... reverting last "fix"
[ntk/apt.git] / apt-pkg / policy.cc
CommitLineData
b2e465d6
AL
1// -*- mode: cpp; mode: fold -*-
2// Description /*{{{*/
6aeda9fa 3// $Id: policy.cc,v 1.3 2001/03/03 23:27:24 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..
76 SPtr<bool> Fixed = new bool[Cache->HeaderP->PackageFileCount];
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++)
6aeda9fa
AL
148 {
149 /* This is a side effect of the condition below, and is added for
150 completeness, though it should not be necessary */
151 if (Ver->Arch == 0)
152 continue;
153
b2e465d6
AL
154 for (pkgCache::VerFileIterator VF = Ver.FileList(); VF.end() == false; VF++)
155 {
6aeda9fa
AL
156 /* If this is the status file, and the current version is not the
157 version in the status file (ie it is not installed, or somesuch)
158 then it is not a candidate for installation, ever. This weeds
159 out bogus entries that may be due to config-file states, or
160 other. */
161 if ((VF.File()->Flags & pkgCache::Flag::NotSource) == pkgCache::Flag::NotSource &&
162 Pkg.CurrentVer() != Ver)
163 continue;
164
b2e465d6
AL
165 signed Prio = PFPriority[VF.File()->ID];
166 if (Prio > Max)
167 {
168 Pref = Ver;
169 Max = Prio;
170 }
171 }
172
173 if (Pkg.CurrentVer() == Ver && Max < 1000)
174 {
175 /* Elevate our current selection (or the status file itself)
176 to the Pseudo-status priority. */
177 if (Pref.end() == true)
178 Pref = Ver;
179 Max = 1000;
180
181 // Fast path optimize.
182 if (StatusOverride == false)
183 break;
184 }
185 }
186
187 return Pref;
188}
189 /*}}}*/
190// Policy::CreatePin - Create an entry in the pin table.. /*{{{*/
191// ---------------------------------------------------------------------
192/* For performance we have 3 tables, the default table, the main cache
193 table (hashed to the cache). A blank package name indicates the pin
194 belongs to the default table. Order of insertion matters here, the
195 earlier defaults override later ones. */
196void pkgPolicy::CreatePin(pkgVersionMatch::MatchType Type,string Name,
197 string Data,signed short Priority)
198{
199 pkgCache::PkgIterator Pkg = Cache->FindPkg(Name);
200 Pin *P = 0;
201
202 if (Name.empty() == true)
203 P = Defaults.insert(Defaults.end());
204 else
205 {
206 // Get a spot to put the pin
207 if (Pkg.end() == true)
208 {
209 // Check the unmatched table
210 for (vector<PkgPin>::iterator I = Unmatched.begin();
211 I != Unmatched.end() && P == 0; I++)
212 if (I->Pkg == Name)
213 P = I;
214
215 if (P == 0)
216 P = Unmatched.insert(Unmatched.end());
217 }
218 else
219 {
220 P = Pins + Pkg->ID;
221 }
222 }
223
224 // Set..
225 P->Type = Type;
226 P->Priority = Priority;
227 P->Data = Data;
228}
229 /*}}}*/
230
231// ReadPinFile - Load the pin file into a Policy /*{{{*/
232// ---------------------------------------------------------------------
233/* I'd like to see the preferences file store more than just pin information
234 but right now that is the only stuff I have to store. Later there will
235 have to be some kind of combined super parser to get the data into all
236 the right classes.. */
237bool ReadPinFile(pkgPolicy &Plcy,string File)
238{
239 if (File.empty() == true)
240 File = _config->FindFile("Dir::Etc::Preferences");
241
242 if (FileExists(File) == false)
243 return true;
244
245 FileFd Fd(File,FileFd::ReadOnly);
246 pkgTagFile TF(&Fd);
247 if (_error->PendingError() == true)
248 return false;
249
250 pkgTagSection Tags;
251 while (TF.Step(Tags) == true)
252 {
253 string Name = Tags.FindS("Package");
254 if (Name.empty() == true)
255 return _error->Error(_("Invalid record in the preferences file, no Package header"));
256 if (Name == "*")
257 Name = string();
258
259 const char *Start;
260 const char *End;
261 if (Tags.Find("Pin",Start,End) == false)
262 continue;
263
264 const char *Word = Start;
265 for (; Word != End && isspace(*Word) == 0; Word++);
266
267 // Parse the type..
268 pkgVersionMatch::MatchType Type;
269 if (stringcasecmp(Start,Word,"version") == 0 && Name.empty() == false)
270 Type = pkgVersionMatch::Version;
271 else if (stringcasecmp(Start,Word,"release") == 0)
272 Type = pkgVersionMatch::Release;
273 else if (stringcasecmp(Start,Word,"origin") == 0)
274 Type = pkgVersionMatch::Origin;
275 else
276 {
277 _error->Warning(_("Did not understand pin type %s"),string(Start,Word).c_str());
278 continue;
279 }
280 for (; Word != End && isspace(*Word) != 0; Word++);
281
282 Plcy.CreatePin(Type,Name,string(Word,End),
283 Tags.FindI("Pin-Priority"));
284 }
285
286 Plcy.InitDefaults();
287 return true;
288}
289 /*}}}*/