]> git.saurik.com Git - apt.git/blame - apt-pkg/policy.cc
* ftparchive/writer.cc:
[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{
c55b8a54
DK
48 if (Owner == 0 || &(Owner->Head()) == 0)
49 return;
b2e465d6
AL
50 PFPriority = new signed short[Owner->Head().PackageFileCount];
51 Pins = new Pin[Owner->Head().PackageCount];
52
53 for (unsigned long I = 0; I != Owner->Head().PackageCount; I++)
54 Pins[I].Type = pkgVersionMatch::None;
55
56 // The config file has a master override.
57 string DefRel = _config->Find("APT::Default-Release");
58 if (DefRel.empty() == false)
59 CreatePin(pkgVersionMatch::Release,"",DefRel,990);
60
61 InitDefaults();
62}
63 /*}}}*/
64// Policy::InitDefaults - Compute the default selections /*{{{*/
65// ---------------------------------------------------------------------
66/* */
67bool pkgPolicy::InitDefaults()
68{
69 // Initialize the priorities based on the status of the package file
70 for (pkgCache::PkgFileIterator I = Cache->FileBegin(); I != Cache->FileEnd(); I++)
71 {
72 PFPriority[I->ID] = 500;
73 if ((I->Flags & pkgCache::Flag::NotSource) == pkgCache::Flag::NotSource)
74 PFPriority[I->ID] = 100;
5ed56f93
DK
75 else if ((I->Flags & pkgCache::Flag::ButAutomaticUpgrades) == pkgCache::Flag::ButAutomaticUpgrades)
76 PFPriority[I->ID] = 100;
77 else if ((I->Flags & pkgCache::Flag::NotAutomatic) == pkgCache::Flag::NotAutomatic)
78 PFPriority[I->ID] = 1;
b2e465d6
AL
79 }
80
81 // Apply the defaults..
20ebd488 82 SPtrArray<bool> Fixed = new bool[Cache->HeaderP->PackageFileCount];
b2e465d6
AL
83 memset(Fixed,0,sizeof(*Fixed)*Cache->HeaderP->PackageFileCount);
84 signed Cur = 989;
85 StatusOverride = false;
86 for (vector<Pin>::const_iterator I = Defaults.begin(); I != Defaults.end();
87 I++, Cur--)
88 {
89 pkgVersionMatch Match(I->Data,I->Type);
90 for (pkgCache::PkgFileIterator F = Cache->FileBegin(); F != Cache->FileEnd(); F++)
91 {
b2e465d6
AL
92 if (Match.FileMatch(F) == true && Fixed[F->ID] == false)
93 {
94 if (I->Priority != 0 && I->Priority > 0)
95 Cur = I->Priority;
96
97 if (I->Priority < 0)
98 PFPriority[F->ID] = I->Priority;
99 else
100 PFPriority[F->ID] = Cur;
101
102 if (PFPriority[F->ID] > 1000)
103 StatusOverride = true;
104
105 Fixed[F->ID] = true;
106 }
107 }
108 }
109
110 if (_config->FindB("Debug::pkgPolicy",false) == true)
111 for (pkgCache::PkgFileIterator F = Cache->FileBegin(); F != Cache->FileEnd(); F++)
4dec007b 112 std::clog << "Prio of " << F.FileName() << ' ' << PFPriority[F->ID] << std::endl;
b2e465d6
AL
113
114 return true;
115}
116 /*}}}*/
117// Policy::GetCandidateVer - Get the candidate install version /*{{{*/
118// ---------------------------------------------------------------------
119/* Evaluate the package pins and the default list to deteremine what the
120 best package is. */
9ee8287e 121pkgCache::VerIterator pkgPolicy::GetCandidateVer(pkgCache::PkgIterator const &Pkg)
b2e465d6 122{
b2e465d6 123 // Look for a package pin and evaluate it.
af87ab54
AL
124 signed Max = GetPriority(Pkg);
125 pkgCache::VerIterator Pref = GetMatch(Pkg);
e7b470ee 126
8f5525e9
JAK
127 // Alternatives in case we can not find our package pin (Bug#512318).
128 signed MaxAlt = 0;
129 pkgCache::VerIterator PrefAlt;
130
9f5bf66a
DK
131 // no package = no candidate version
132 if (Pkg.end() == true)
133 return Pref;
134
135 // packages with a pin lower than 0 have no newer candidate than the current version
136 if (Max < 0)
137 return Pkg.CurrentVer();
138
b2e465d6
AL
139 /* Falling through to the default version.. Setting Max to zero
140 effectively excludes everything <= 0 which are the non-automatic
141 priorities.. The status file is given a prio of 100 which will exclude
142 not-automatic sources, except in a single shot not-installed mode.
143 The second pseduo-status file is at prio 1000, above which will permit
144 the user to force-downgrade things.
145
146 The user pin is subject to the same priority rules as default
147 selections. Thus there are two ways to create a pin - a pin that
148 tracks the default when the default is taken away, and a permanent
149 pin that stays at that setting.
150 */
151 for (pkgCache::VerIterator Ver = Pkg.VersionList(); Ver.end() == false; Ver++)
10639577 152 {
9ee8287e
DK
153 /* Lets see if this version is the installed version */
154 bool instVer = (Pkg.CurrentVer() == Ver);
9ee8287e 155
b2e465d6
AL
156 for (pkgCache::VerFileIterator VF = Ver.FileList(); VF.end() == false; VF++)
157 {
6aeda9fa
AL
158 /* If this is the status file, and the current version is not the
159 version in the status file (ie it is not installed, or somesuch)
160 then it is not a candidate for installation, ever. This weeds
161 out bogus entries that may be due to config-file states, or
162 other. */
163 if ((VF.File()->Flags & pkgCache::Flag::NotSource) == pkgCache::Flag::NotSource &&
9ee8287e 164 instVer == false)
6aeda9fa 165 continue;
9ee8287e 166
b2e465d6
AL
167 signed Prio = PFPriority[VF.File()->ID];
168 if (Prio > Max)
169 {
170 Pref = Ver;
171 Max = Prio;
8f5525e9
JAK
172 }
173 if (Prio > MaxAlt)
174 {
175 PrefAlt = Ver;
176 MaxAlt = Prio;
b2e465d6
AL
177 }
178 }
179
9ee8287e 180 if (instVer == true && Max < 1000)
b2e465d6
AL
181 {
182 /* Elevate our current selection (or the status file itself)
183 to the Pseudo-status priority. */
184 if (Pref.end() == true)
185 Pref = Ver;
186 Max = 1000;
187
188 // Fast path optimize.
189 if (StatusOverride == false)
190 break;
191 }
192 }
8f5525e9
JAK
193 // If we do not find our candidate, use the one with the highest pin.
194 // This means that if there is a version available with pin > 0; there
195 // will always be a candidate (Closes: #512318)
196 if (!Pref.IsGood() && MaxAlt > 0)
197 Pref = PrefAlt;
9ee8287e 198
b2e465d6
AL
199 return Pref;
200}
201 /*}}}*/
202// Policy::CreatePin - Create an entry in the pin table.. /*{{{*/
203// ---------------------------------------------------------------------
204/* For performance we have 3 tables, the default table, the main cache
205 table (hashed to the cache). A blank package name indicates the pin
206 belongs to the default table. Order of insertion matters here, the
207 earlier defaults override later ones. */
208void pkgPolicy::CreatePin(pkgVersionMatch::MatchType Type,string Name,
209 string Data,signed short Priority)
210{
b2e465d6 211 if (Name.empty() == true)
b2e465d6 212 {
00c6e1a3 213 Pin *P = &*Defaults.insert(Defaults.end(),Pin());
4a6d2163
DK
214 P->Type = Type;
215 P->Priority = Priority;
216 P->Data = Data;
217 return;
218 }
00c6e1a3
MV
219
220 size_t found = Name.rfind(':');
221 string Arch;
222 if (found != string::npos) {
223 Arch = Name.substr(found+1);
224 Name.erase(found);
225 }
226
1a4c9766
JAK
227 // Allow pinning by wildcards
228 // TODO: Maybe we should always prefer specific pins over non-
229 // specific ones.
a8d7c101
DK
230 if (Name[0] == '/' || Name.find_first_of("*[?") != string::npos)
231 {
232 pkgVersionMatch match(Data, Type);
233 for (pkgCache::GrpIterator G = Cache->GrpBegin(); G.end() != true; ++G)
234 if (match.ExpressionMatches(Name, G.Name()))
00c6e1a3
MV
235 {
236 if (Arch.empty() == false)
237 CreatePin(Type, string(G.Name()).append(":").append(Arch), Data, Priority);
238 else
239 CreatePin(Type, G.Name(), Data, Priority);
240 }
1a4c9766
JAK
241 return;
242 }
4a6d2163 243
00c6e1a3
MV
244 // find the package (group) this pin applies to
245 pkgCache::GrpIterator Grp;
246 pkgCache::PkgIterator Pkg;
247 if (Arch.empty() == false)
248 Pkg = Cache->FindPkg(Name, Arch);
249 else {
250 Grp = Cache->FindGrp(Name);
251 if (Grp.end() == false)
252 Pkg = Grp.PackageList();
253 }
254
255 if (Pkg.end() == true)
4a6d2163 256 {
00c6e1a3
MV
257 PkgPin *P = &*Unmatched.insert(Unmatched.end(),PkgPin(Name));
258 if (Arch.empty() == false)
259 P->Pkg.append(":").append(Arch);
260 P->Type = Type;
261 P->Priority = Priority;
262 P->Data = Data;
263 return;
264 }
4a6d2163 265
00c6e1a3
MV
266 for (; Pkg.end() != true; Pkg = Grp.NextPkg(Pkg))
267 {
268 Pin *P = Pins + Pkg->ID;
269 // the first specific stanza for a package is the ruler,
270 // all others need to be ignored
271 if (P->Type != pkgVersionMatch::None)
272 P = &*Unmatched.insert(Unmatched.end(),PkgPin(Pkg.FullName()));
4a6d2163
DK
273 P->Type = Type;
274 P->Priority = Priority;
275 P->Data = Data;
00c6e1a3
MV
276 if (Grp.end() == true)
277 break;
b2e465d6 278 }
b2e465d6
AL
279}
280 /*}}}*/
af87ab54
AL
281// Policy::GetMatch - Get the matching version for a package pin /*{{{*/
282// ---------------------------------------------------------------------
283/* */
9ee8287e 284pkgCache::VerIterator pkgPolicy::GetMatch(pkgCache::PkgIterator const &Pkg)
af87ab54
AL
285{
286 const Pin &PPkg = Pins[Pkg->ID];
9ee8287e
DK
287 if (PPkg.Type == pkgVersionMatch::None)
288 return pkgCache::VerIterator(*Pkg.Cache());
289
290 pkgVersionMatch Match(PPkg.Data,PPkg.Type);
291 return Match.Find(Pkg);
af87ab54
AL
292}
293 /*}}}*/
294// Policy::GetPriority - Get the priority of the package pin /*{{{*/
295// ---------------------------------------------------------------------
296/* */
297signed short pkgPolicy::GetPriority(pkgCache::PkgIterator const &Pkg)
298{
299 if (Pins[Pkg->ID].Type != pkgVersionMatch::None)
300 {
301 // In this case 0 means default priority
302 if (Pins[Pkg->ID].Priority == 0)
303 return 989;
304 return Pins[Pkg->ID].Priority;
305 }
306
307 return 0;
6d38011b
DK
308}
309signed short pkgPolicy::GetPriority(pkgCache::PkgFileIterator const &File)
310{
311 return PFPriority[File->ID];
af87ab54
AL
312}
313 /*}}}*/
81e9789b
MV
314// PreferenceSection class - Overriding the default TrimRecord method /*{{{*/
315// ---------------------------------------------------------------------
316/* The preference file is a user generated file so the parser should
317 therefore be a bit more friendly by allowing comments and new lines
318 all over the place rather than forcing a special format */
319class PreferenceSection : public pkgTagSection
320{
321 void TrimRecord(bool BeforeRecord, const char* &End)
322 {
323 for (; Stop < End && (Stop[0] == '\n' || Stop[0] == '\r' || Stop[0] == '#'); Stop++)
324 if (Stop[0] == '#')
325 Stop = (const char*) memchr(Stop,'\n',End-Stop);
326 }
327};
328 /*}}}*/
ef1dff93
DK
329// ReadPinDir - Load the pin files from this dir into a Policy /*{{{*/
330// ---------------------------------------------------------------------
6009e60d
DK
331/* This will load each pin file in the given dir into a Policy. If the
332 given dir is empty the dir set in Dir::Etc::PreferencesParts is used.
333 Note also that this method will issue a warning if the dir does not
334 exists but it will return true in this case! */
e68ca100
JAK
335bool ReadPinDir(pkgPolicy &Plcy,string Dir)
336{
337 if (Dir.empty() == true)
338 Dir = _config->FindDir("Dir::Etc::PreferencesParts");
339
448eaf8b 340 if (DirectoryExists(Dir) == false)
6009e60d 341 {
448eaf8b 342 _error->WarningE("DirectoryExists",_("Unable to read %s"),Dir.c_str());
6009e60d
DK
343 return true;
344 }
345
b39c1859 346 vector<string> const List = GetListOfFilesInDir(Dir, "pref", true, true);
e68ca100
JAK
347
348 // Read the files
349 for (vector<string>::const_iterator I = List.begin(); I != List.end(); I++)
350 if (ReadPinFile(Plcy, *I) == false)
351 return false;
352 return true;
353}
81e9789b 354 /*}}}*/
b2e465d6
AL
355// ReadPinFile - Load the pin file into a Policy /*{{{*/
356// ---------------------------------------------------------------------
357/* I'd like to see the preferences file store more than just pin information
358 but right now that is the only stuff I have to store. Later there will
359 have to be some kind of combined super parser to get the data into all
360 the right classes.. */
361bool ReadPinFile(pkgPolicy &Plcy,string File)
362{
363 if (File.empty() == true)
364 File = _config->FindFile("Dir::Etc::Preferences");
365
36f1098a 366 if (RealFileExists(File) == false)
b2e465d6
AL
367 return true;
368
369 FileFd Fd(File,FileFd::ReadOnly);
370 pkgTagFile TF(&Fd);
371 if (_error->PendingError() == true)
372 return false;
373
81e9789b 374 PreferenceSection Tags;
b2e465d6
AL
375 while (TF.Step(Tags) == true)
376 {
377 string Name = Tags.FindS("Package");
378 if (Name.empty() == true)
e68ca100 379 return _error->Error(_("Invalid record in the preferences file %s, no Package header"), File.c_str());
b2e465d6
AL
380 if (Name == "*")
381 Name = string();
382
383 const char *Start;
384 const char *End;
385 if (Tags.Find("Pin",Start,End) == false)
386 continue;
387
388 const char *Word = Start;
389 for (; Word != End && isspace(*Word) == 0; Word++);
390
391 // Parse the type..
392 pkgVersionMatch::MatchType Type;
393 if (stringcasecmp(Start,Word,"version") == 0 && Name.empty() == false)
394 Type = pkgVersionMatch::Version;
395 else if (stringcasecmp(Start,Word,"release") == 0)
396 Type = pkgVersionMatch::Release;
397 else if (stringcasecmp(Start,Word,"origin") == 0)
398 Type = pkgVersionMatch::Origin;
399 else
400 {
401 _error->Warning(_("Did not understand pin type %s"),string(Start,Word).c_str());
402 continue;
403 }
404 for (; Word != End && isspace(*Word) != 0; Word++);
405
56298634
AL
406 short int priority = Tags.FindI("Pin-Priority", 0);
407 if (priority == 0)
408 {
409 _error->Warning(_("No priority (or zero) specified for pin"));
410 continue;
411 }
412
1c62ab24
MV
413 istringstream s(Name);
414 string pkg;
415 while(!s.eof())
416 {
417 s >> pkg;
418 Plcy.CreatePin(Type, pkg, string(Word,End),priority);
419 };
b2e465d6
AL
420 }
421
422 Plcy.InitDefaults();
423 return true;
424}
425 /*}}}*/