]> git.saurik.com Git - apt.git/blame - apt-pkg/policy.cc
merged from the debian-sid branch
[apt.git] / apt-pkg / policy.cc
CommitLineData
b2e465d6
AL
1// -*- mode: cpp; mode: fold -*-
2// Description /*{{{*/
56298634 3// $Id: policy.cc,v 1.10 2003/08/12 00:17:37 mdz 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
5ed56f93 19 100 = The status file and ButAutomaticUpgrades sources
b2e465d6
AL
20 0 -> 100 = NotAutomatic sources like experimental
21 -inf -> 0 = Never selected
22
23 ##################################################################### */
24 /*}}}*/
25// Include Files /*{{{*/
b2e465d6
AL
26#include <apt-pkg/policy.h>
27#include <apt-pkg/configuration.h>
28#include <apt-pkg/tagfile.h>
29#include <apt-pkg/strutl.h>
46e39c8e 30#include <apt-pkg/fileutl.h>
b2e465d6
AL
31#include <apt-pkg/error.h>
32#include <apt-pkg/sptr.h>
46e39c8e 33
b2e465d6 34#include <apti18n.h>
e7b470ee
AL
35
36#include <iostream>
1c62ab24 37#include <sstream>
b2e465d6
AL
38 /*}}}*/
39
e7b470ee
AL
40using namespace std;
41
b2e465d6
AL
42// Policy::Init - Startup and bind to a cache /*{{{*/
43// ---------------------------------------------------------------------
44/* Set the defaults for operation. The default mode with no loaded policy
45 file matches the V0 policy engine. */
46pkgPolicy::pkgPolicy(pkgCache *Owner) : Pins(0), PFPriority(0), Cache(Owner)
47{
48 PFPriority = new signed short[Owner->Head().PackageFileCount];
49 Pins = new Pin[Owner->Head().PackageCount];
50
51 for (unsigned long I = 0; I != Owner->Head().PackageCount; I++)
52 Pins[I].Type = pkgVersionMatch::None;
53
54 // The config file has a master override.
55 string DefRel = _config->Find("APT::Default-Release");
56 if (DefRel.empty() == false)
57 CreatePin(pkgVersionMatch::Release,"",DefRel,990);
58
59 InitDefaults();
60}
61 /*}}}*/
62// Policy::InitDefaults - Compute the default selections /*{{{*/
63// ---------------------------------------------------------------------
64/* */
65bool pkgPolicy::InitDefaults()
66{
67 // Initialize the priorities based on the status of the package file
68 for (pkgCache::PkgFileIterator I = Cache->FileBegin(); I != Cache->FileEnd(); I++)
69 {
70 PFPriority[I->ID] = 500;
71 if ((I->Flags & pkgCache::Flag::NotSource) == pkgCache::Flag::NotSource)
72 PFPriority[I->ID] = 100;
5ed56f93
DK
73 else if ((I->Flags & pkgCache::Flag::ButAutomaticUpgrades) == pkgCache::Flag::ButAutomaticUpgrades)
74 PFPriority[I->ID] = 100;
75 else if ((I->Flags & pkgCache::Flag::NotAutomatic) == pkgCache::Flag::NotAutomatic)
76 PFPriority[I->ID] = 1;
b2e465d6
AL
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++)
4dec007b 110 std::clog << "Prio of " << F.FileName() << ' ' << PFPriority[F->ID] << std::endl;
b2e465d6
AL
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. */
9ee8287e 119pkgCache::VerIterator pkgPolicy::GetCandidateVer(pkgCache::PkgIterator const &Pkg)
b2e465d6 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
8f5525e9
JAK
125 // Alternatives in case we can not find our package pin (Bug#512318).
126 signed MaxAlt = 0;
127 pkgCache::VerIterator PrefAlt;
128
9f5bf66a
DK
129 // no package = no candidate version
130 if (Pkg.end() == true)
131 return Pref;
132
133 // packages with a pin lower than 0 have no newer candidate than the current version
134 if (Max < 0)
135 return Pkg.CurrentVer();
136
b2e465d6
AL
137 /* Falling through to the default version.. Setting Max to zero
138 effectively excludes everything <= 0 which are the non-automatic
139 priorities.. The status file is given a prio of 100 which will exclude
140 not-automatic sources, except in a single shot not-installed mode.
141 The second pseduo-status file is at prio 1000, above which will permit
142 the user to force-downgrade things.
143
144 The user pin is subject to the same priority rules as default
145 selections. Thus there are two ways to create a pin - a pin that
146 tracks the default when the default is taken away, and a permanent
147 pin that stays at that setting.
148 */
149 for (pkgCache::VerIterator Ver = Pkg.VersionList(); Ver.end() == false; Ver++)
10639577 150 {
9ee8287e
DK
151 /* Lets see if this version is the installed version */
152 bool instVer = (Pkg.CurrentVer() == Ver);
153 if (Ver.Pseudo() == true && instVer == false)
154 {
155 pkgCache::PkgIterator const allPkg = Ver.ParentPkg().Group().FindPkg("all");
156 if (allPkg->CurrentVer != 0 && allPkg.CurrentVer()->Hash == Ver->Hash &&
157 strcmp(allPkg.CurVersion(), Ver.VerStr()) == 0)
158 instVer = true;
159 }
160
b2e465d6
AL
161 for (pkgCache::VerFileIterator VF = Ver.FileList(); VF.end() == false; VF++)
162 {
6aeda9fa
AL
163 /* If this is the status file, and the current version is not the
164 version in the status file (ie it is not installed, or somesuch)
165 then it is not a candidate for installation, ever. This weeds
166 out bogus entries that may be due to config-file states, or
167 other. */
168 if ((VF.File()->Flags & pkgCache::Flag::NotSource) == pkgCache::Flag::NotSource &&
9ee8287e 169 instVer == false)
6aeda9fa 170 continue;
9ee8287e 171
b2e465d6
AL
172 signed Prio = PFPriority[VF.File()->ID];
173 if (Prio > Max)
174 {
175 Pref = Ver;
176 Max = Prio;
8f5525e9
JAK
177 }
178 if (Prio > MaxAlt)
179 {
180 PrefAlt = Ver;
181 MaxAlt = Prio;
b2e465d6
AL
182 }
183 }
184
9ee8287e 185 if (instVer == true && Max < 1000)
b2e465d6
AL
186 {
187 /* Elevate our current selection (or the status file itself)
188 to the Pseudo-status priority. */
189 if (Pref.end() == true)
190 Pref = Ver;
191 Max = 1000;
192
193 // Fast path optimize.
194 if (StatusOverride == false)
195 break;
196 }
197 }
8f5525e9
JAK
198 // If we do not find our candidate, use the one with the highest pin.
199 // This means that if there is a version available with pin > 0; there
200 // will always be a candidate (Closes: #512318)
201 if (!Pref.IsGood() && MaxAlt > 0)
202 Pref = PrefAlt;
9ee8287e 203
b2e465d6
AL
204 return Pref;
205}
206 /*}}}*/
207// Policy::CreatePin - Create an entry in the pin table.. /*{{{*/
208// ---------------------------------------------------------------------
209/* For performance we have 3 tables, the default table, the main cache
210 table (hashed to the cache). A blank package name indicates the pin
211 belongs to the default table. Order of insertion matters here, the
212 earlier defaults override later ones. */
213void pkgPolicy::CreatePin(pkgVersionMatch::MatchType Type,string Name,
214 string Data,signed short Priority)
215{
b2e465d6 216 if (Name.empty() == true)
b2e465d6 217 {
4a6d2163
DK
218 Pin *P = &*Defaults.insert(Defaults.end(),PkgPin());
219 P->Type = Type;
220 P->Priority = Priority;
221 P->Data = Data;
222 return;
223 }
224
225 // Get a spot to put the pin
226 pkgCache::GrpIterator Grp = Cache->FindGrp(Name);
227 for (pkgCache::PkgIterator Pkg = Grp.FindPkg("any");
228 Pkg.end() != true; Pkg = Grp.NextPkg(Pkg))
229 {
230 Pin *P = 0;
231 if (Pkg.end() == false)
232 P = Pins + Pkg->ID;
233 else
b2e465d6
AL
234 {
235 // Check the unmatched table
4a6d2163 236 for (vector<PkgPin>::iterator I = Unmatched.begin();
b2e465d6
AL
237 I != Unmatched.end() && P == 0; I++)
238 if (I->Pkg == Name)
d4887f3c 239 P = &*I;
4a6d2163 240
b2e465d6 241 if (P == 0)
4a6d2163 242 P = &*Unmatched.insert(Unmatched.end(),PkgPin());
b2e465d6 243 }
4a6d2163
DK
244 P->Type = Type;
245 P->Priority = Priority;
246 P->Data = Data;
b2e465d6 247 }
b2e465d6
AL
248}
249 /*}}}*/
af87ab54
AL
250// Policy::GetMatch - Get the matching version for a package pin /*{{{*/
251// ---------------------------------------------------------------------
252/* */
9ee8287e 253pkgCache::VerIterator pkgPolicy::GetMatch(pkgCache::PkgIterator const &Pkg)
af87ab54
AL
254{
255 const Pin &PPkg = Pins[Pkg->ID];
9ee8287e
DK
256 if (PPkg.Type == pkgVersionMatch::None)
257 return pkgCache::VerIterator(*Pkg.Cache());
258
259 pkgVersionMatch Match(PPkg.Data,PPkg.Type);
260 return Match.Find(Pkg);
af87ab54
AL
261}
262 /*}}}*/
263// Policy::GetPriority - Get the priority of the package pin /*{{{*/
264// ---------------------------------------------------------------------
265/* */
266signed short pkgPolicy::GetPriority(pkgCache::PkgIterator const &Pkg)
267{
268 if (Pins[Pkg->ID].Type != pkgVersionMatch::None)
269 {
270 // In this case 0 means default priority
271 if (Pins[Pkg->ID].Priority == 0)
272 return 989;
273 return Pins[Pkg->ID].Priority;
274 }
275
276 return 0;
277}
278 /*}}}*/
81e9789b
MV
279// PreferenceSection class - Overriding the default TrimRecord method /*{{{*/
280// ---------------------------------------------------------------------
281/* The preference file is a user generated file so the parser should
282 therefore be a bit more friendly by allowing comments and new lines
283 all over the place rather than forcing a special format */
284class PreferenceSection : public pkgTagSection
285{
286 void TrimRecord(bool BeforeRecord, const char* &End)
287 {
288 for (; Stop < End && (Stop[0] == '\n' || Stop[0] == '\r' || Stop[0] == '#'); Stop++)
289 if (Stop[0] == '#')
290 Stop = (const char*) memchr(Stop,'\n',End-Stop);
291 }
292};
293 /*}}}*/
ef1dff93
DK
294// ReadPinDir - Load the pin files from this dir into a Policy /*{{{*/
295// ---------------------------------------------------------------------
6009e60d
DK
296/* This will load each pin file in the given dir into a Policy. If the
297 given dir is empty the dir set in Dir::Etc::PreferencesParts is used.
298 Note also that this method will issue a warning if the dir does not
299 exists but it will return true in this case! */
e68ca100
JAK
300bool ReadPinDir(pkgPolicy &Plcy,string Dir)
301{
302 if (Dir.empty() == true)
303 Dir = _config->FindDir("Dir::Etc::PreferencesParts");
304
448eaf8b 305 if (DirectoryExists(Dir) == false)
6009e60d 306 {
448eaf8b 307 _error->WarningE("DirectoryExists",_("Unable to read %s"),Dir.c_str());
6009e60d
DK
308 return true;
309 }
310
b39c1859 311 vector<string> const List = GetListOfFilesInDir(Dir, "pref", true, true);
e68ca100
JAK
312
313 // Read the files
314 for (vector<string>::const_iterator I = List.begin(); I != List.end(); I++)
315 if (ReadPinFile(Plcy, *I) == false)
316 return false;
317 return true;
318}
81e9789b 319 /*}}}*/
b2e465d6
AL
320// ReadPinFile - Load the pin file into a Policy /*{{{*/
321// ---------------------------------------------------------------------
322/* I'd like to see the preferences file store more than just pin information
323 but right now that is the only stuff I have to store. Later there will
324 have to be some kind of combined super parser to get the data into all
325 the right classes.. */
326bool ReadPinFile(pkgPolicy &Plcy,string File)
327{
328 if (File.empty() == true)
329 File = _config->FindFile("Dir::Etc::Preferences");
330
331 if (FileExists(File) == false)
332 return true;
333
334 FileFd Fd(File,FileFd::ReadOnly);
335 pkgTagFile TF(&Fd);
336 if (_error->PendingError() == true)
337 return false;
338
81e9789b 339 PreferenceSection Tags;
b2e465d6
AL
340 while (TF.Step(Tags) == true)
341 {
342 string Name = Tags.FindS("Package");
343 if (Name.empty() == true)
e68ca100 344 return _error->Error(_("Invalid record in the preferences file %s, no Package header"), File.c_str());
b2e465d6
AL
345 if (Name == "*")
346 Name = string();
347
348 const char *Start;
349 const char *End;
350 if (Tags.Find("Pin",Start,End) == false)
351 continue;
352
353 const char *Word = Start;
354 for (; Word != End && isspace(*Word) == 0; Word++);
355
356 // Parse the type..
357 pkgVersionMatch::MatchType Type;
358 if (stringcasecmp(Start,Word,"version") == 0 && Name.empty() == false)
359 Type = pkgVersionMatch::Version;
360 else if (stringcasecmp(Start,Word,"release") == 0)
361 Type = pkgVersionMatch::Release;
362 else if (stringcasecmp(Start,Word,"origin") == 0)
363 Type = pkgVersionMatch::Origin;
364 else
365 {
366 _error->Warning(_("Did not understand pin type %s"),string(Start,Word).c_str());
367 continue;
368 }
369 for (; Word != End && isspace(*Word) != 0; Word++);
370
56298634
AL
371 short int priority = Tags.FindI("Pin-Priority", 0);
372 if (priority == 0)
373 {
374 _error->Warning(_("No priority (or zero) specified for pin"));
375 continue;
376 }
377
1c62ab24
MV
378 istringstream s(Name);
379 string pkg;
380 while(!s.eof())
381 {
382 s >> pkg;
383 Plcy.CreatePin(Type, pkg, string(Word,End),priority);
384 };
b2e465d6
AL
385 }
386
387 Plcy.InitDefaults();
388 return true;
389}
390 /*}}}*/