Reverted unnecessary string changes
[ntk/apt.git] / apt-pkg / policy.cc
CommitLineData
b2e465d6
AL
1// -*- mode: cpp; mode: fold -*-
2// Description /*{{{*/
e7b470ee 3// $Id: policy.cc,v 1.7 2001/05/27 05:36:04 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>
e7b470ee
AL
37
38#include <iostream>
b2e465d6
AL
39 /*}}}*/
40
e7b470ee
AL
41using namespace std;
42
b2e465d6
AL
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. */
47pkgPolicy::pkgPolicy(pkgCache *Owner) : Pins(0), PFPriority(0), Cache(Owner)
48{
49 PFPriority = new signed short[Owner->Head().PackageFileCount];
50 Pins = new Pin[Owner->Head().PackageCount];
51
52 for (unsigned long I = 0; I != Owner->Head().PackageCount; I++)
53 Pins[I].Type = pkgVersionMatch::None;
54
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);
59
60 InitDefaults();
61}
62 /*}}}*/
63// Policy::InitDefaults - Compute the default selections /*{{{*/
64// ---------------------------------------------------------------------
65/* */
66bool pkgPolicy::InitDefaults()
67{
68 // Initialize the priorities based on the status of the package file
69 for (pkgCache::PkgFileIterator I = Cache->FileBegin(); I != Cache->FileEnd(); I++)
70 {
71 PFPriority[I->ID] = 500;
72 if ((I->Flags & pkgCache::Flag::NotSource) == pkgCache::Flag::NotSource)
73 PFPriority[I->ID] = 100;
74 else
75 if ((I->Flags & pkgCache::Flag::NotAutomatic) == pkgCache::Flag::NotAutomatic)
76 PFPriority[I->ID] = 1;
77 }
78
79 // Apply the defaults..
20ebd488 80 SPtrArray<bool> Fixed = new bool[Cache->HeaderP->PackageFileCount];
b2e465d6
AL
81 memset(Fixed,0,sizeof(*Fixed)*Cache->HeaderP->PackageFileCount);
82 signed Cur = 989;
83 StatusOverride = false;
84 for (vector<Pin>::const_iterator I = Defaults.begin(); I != Defaults.end();
85 I++, Cur--)
86 {
87 pkgVersionMatch Match(I->Data,I->Type);
88 for (pkgCache::PkgFileIterator F = Cache->FileBegin(); F != Cache->FileEnd(); F++)
89 {
b2e465d6
AL
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{
b2e465d6 121 // Look for a package pin and evaluate it.
af87ab54
AL
122 signed Max = GetPriority(Pkg);
123 pkgCache::VerIterator Pref = GetMatch(Pkg);
e7b470ee 124
b2e465d6
AL
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.
131
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.
136 */
137 for (pkgCache::VerIterator Ver = Pkg.VersionList(); Ver.end() == false; Ver++)
10639577 138 {
b2e465d6
AL
139 for (pkgCache::VerFileIterator VF = Ver.FileList(); VF.end() == false; VF++)
140 {
6aeda9fa
AL
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
145 other. */
146 if ((VF.File()->Flags & pkgCache::Flag::NotSource) == pkgCache::Flag::NotSource &&
147 Pkg.CurrentVer() != Ver)
148 continue;
149
b2e465d6
AL
150 signed Prio = PFPriority[VF.File()->ID];
151 if (Prio > Max)
152 {
153 Pref = Ver;
154 Max = Prio;
155 }
156 }
157
158 if (Pkg.CurrentVer() == Ver && Max < 1000)
159 {
160 /* Elevate our current selection (or the status file itself)
161 to the Pseudo-status priority. */
162 if (Pref.end() == true)
163 Pref = Ver;
164 Max = 1000;
165
166 // Fast path optimize.
167 if (StatusOverride == false)
168 break;
169 }
170 }
e7b470ee
AL
171
172 if (Pref.end() == true)
173 return Pkg.VersionList();
b2e465d6
AL
174 return Pref;
175}
176 /*}}}*/
177// Policy::CreatePin - Create an entry in the pin table.. /*{{{*/
178// ---------------------------------------------------------------------
179/* For performance we have 3 tables, the default table, the main cache
180 table (hashed to the cache). A blank package name indicates the pin
181 belongs to the default table. Order of insertion matters here, the
182 earlier defaults override later ones. */
183void pkgPolicy::CreatePin(pkgVersionMatch::MatchType Type,string Name,
184 string Data,signed short Priority)
185{
186 pkgCache::PkgIterator Pkg = Cache->FindPkg(Name);
187 Pin *P = 0;
188
189 if (Name.empty() == true)
e7b470ee
AL
190 // tausq:g++v3 begin
191 ;
192 // P = Defaults.insert(Defaults.end());
193 // tausq:g++v3 end
b2e465d6
AL
194 else
195 {
196 // Get a spot to put the pin
197 if (Pkg.end() == true)
198 {
199 // Check the unmatched table
200 for (vector<PkgPin>::iterator I = Unmatched.begin();
201 I != Unmatched.end() && P == 0; I++)
202 if (I->Pkg == Name)
e7b470ee
AL
203 // tausq:g++-v3 begin
204 P = &(*I);
205 // P = I;
206 // tausq:g++-v3 end
b2e465d6
AL
207
208 if (P == 0)
e7b470ee
AL
209 // tausq:g++v3 begin
210 ;
211 // P = Unmatched.insert(Unmatched.end());
212 // tausq:g++v3 end
b2e465d6
AL
213 }
214 else
215 {
216 P = Pins + Pkg->ID;
217 }
218 }
219
220 // Set..
221 P->Type = Type;
222 P->Priority = Priority;
223 P->Data = Data;
224}
225 /*}}}*/
af87ab54
AL
226// Policy::GetMatch - Get the matching version for a package pin /*{{{*/
227// ---------------------------------------------------------------------
228/* */
229pkgCache::VerIterator pkgPolicy::GetMatch(pkgCache::PkgIterator Pkg)
230{
231 const Pin &PPkg = Pins[Pkg->ID];
232 if (PPkg.Type != pkgVersionMatch::None)
233 {
234 pkgVersionMatch Match(PPkg.Data,PPkg.Type);
e7b470ee 235 return Match.Find(Pkg);
af87ab54
AL
236 }
237 return pkgCache::VerIterator(*Pkg.Cache());
238}
239 /*}}}*/
240// Policy::GetPriority - Get the priority of the package pin /*{{{*/
241// ---------------------------------------------------------------------
242/* */
243signed short pkgPolicy::GetPriority(pkgCache::PkgIterator const &Pkg)
244{
245 if (Pins[Pkg->ID].Type != pkgVersionMatch::None)
246 {
247 // In this case 0 means default priority
248 if (Pins[Pkg->ID].Priority == 0)
249 return 989;
250 return Pins[Pkg->ID].Priority;
251 }
252
253 return 0;
254}
255 /*}}}*/
b2e465d6
AL
256
257// ReadPinFile - Load the pin file into a Policy /*{{{*/
258// ---------------------------------------------------------------------
259/* I'd like to see the preferences file store more than just pin information
260 but right now that is the only stuff I have to store. Later there will
261 have to be some kind of combined super parser to get the data into all
262 the right classes.. */
263bool ReadPinFile(pkgPolicy &Plcy,string File)
264{
265 if (File.empty() == true)
266 File = _config->FindFile("Dir::Etc::Preferences");
267
268 if (FileExists(File) == false)
269 return true;
270
271 FileFd Fd(File,FileFd::ReadOnly);
272 pkgTagFile TF(&Fd);
273 if (_error->PendingError() == true)
274 return false;
275
276 pkgTagSection Tags;
277 while (TF.Step(Tags) == true)
278 {
279 string Name = Tags.FindS("Package");
280 if (Name.empty() == true)
281 return _error->Error(_("Invalid record in the preferences file, no Package header"));
282 if (Name == "*")
283 Name = string();
284
285 const char *Start;
286 const char *End;
287 if (Tags.Find("Pin",Start,End) == false)
288 continue;
289
290 const char *Word = Start;
291 for (; Word != End && isspace(*Word) == 0; Word++);
292
293 // Parse the type..
294 pkgVersionMatch::MatchType Type;
295 if (stringcasecmp(Start,Word,"version") == 0 && Name.empty() == false)
296 Type = pkgVersionMatch::Version;
297 else if (stringcasecmp(Start,Word,"release") == 0)
298 Type = pkgVersionMatch::Release;
299 else if (stringcasecmp(Start,Word,"origin") == 0)
300 Type = pkgVersionMatch::Origin;
301 else
302 {
303 _error->Warning(_("Did not understand pin type %s"),string(Start,Word).c_str());
304 continue;
305 }
306 for (; Word != End && isspace(*Word) != 0; Word++);
307
308 Plcy.CreatePin(Type,Name,string(Word,End),
309 Tags.FindI("Pin-Priority"));
310 }
311
312 Plcy.InitDefaults();
313 return true;
314}
315 /*}}}*/