]>
git.saurik.com Git - apt.git/blob - apt-pkg/policy.cc
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: policy.cc,v 1.10 2003/08/12 00:17:37 mdz Exp $
4 /* ######################################################################
6 Package Version Policy implementation
8 This is just a really simple wrapper around pkgVersionMatch with
9 some added goodies to manage the list of things..
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 and ButAutomaticUpgrades sources
20 0 -> 100 = NotAutomatic sources like experimental
21 -inf -> 0 = Never selected
23 ##################################################################### */
25 // Include Files /*{{{*/
26 #include <apt-pkg/policy.h>
27 #include <apt-pkg/configuration.h>
28 #include <apt-pkg/tagfile.h>
29 #include <apt-pkg/strutl.h>
30 #include <apt-pkg/fileutl.h>
31 #include <apt-pkg/error.h>
32 #include <apt-pkg/sptr.h>
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. */
46 pkgPolicy::pkgPolicy(pkgCache
*Owner
) : Pins(0), PFPriority(0), Cache(Owner
)
48 if (Owner
== 0 || &(Owner
->Head()) == 0)
50 PFPriority
= new signed short[Owner
->Head().PackageFileCount
];
51 Pins
= new Pin
[Owner
->Head().PackageCount
];
53 for (unsigned long I
= 0; I
!= Owner
->Head().PackageCount
; I
++)
54 Pins
[I
].Type
= pkgVersionMatch::None
;
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);
64 // Policy::InitDefaults - Compute the default selections /*{{{*/
65 // ---------------------------------------------------------------------
67 bool pkgPolicy::InitDefaults()
69 // Initialize the priorities based on the status of the package file
70 for (pkgCache::PkgFileIterator I
= Cache
->FileBegin(); I
!= Cache
->FileEnd(); I
++)
72 PFPriority
[I
->ID
] = 500;
73 if ((I
->Flags
& pkgCache::Flag::NotSource
) == pkgCache::Flag::NotSource
)
74 PFPriority
[I
->ID
] = 100;
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;
81 // Apply the defaults..
82 SPtrArray
<bool> Fixed
= new bool[Cache
->HeaderP
->PackageFileCount
];
83 memset(Fixed
,0,sizeof(*Fixed
)*Cache
->HeaderP
->PackageFileCount
);
85 StatusOverride
= false;
86 for (vector
<Pin
>::const_iterator I
= Defaults
.begin(); I
!= Defaults
.end();
89 pkgVersionMatch
Match(I
->Data
,I
->Type
);
90 for (pkgCache::PkgFileIterator F
= Cache
->FileBegin(); F
!= Cache
->FileEnd(); F
++)
92 if (Match
.FileMatch(F
) == true && Fixed
[F
->ID
] == false)
94 if (I
->Priority
!= 0 && I
->Priority
> 0)
98 PFPriority
[F
->ID
] = I
->Priority
;
100 PFPriority
[F
->ID
] = Cur
;
102 if (PFPriority
[F
->ID
] > 1000)
103 StatusOverride
= true;
110 if (_config
->FindB("Debug::pkgPolicy",false) == true)
111 for (pkgCache::PkgFileIterator F
= Cache
->FileBegin(); F
!= Cache
->FileEnd(); F
++)
112 std::clog
<< "Prio of " << F
.FileName() << ' ' << PFPriority
[F
->ID
] << std::endl
;
117 // Policy::GetCandidateVer - Get the candidate install version /*{{{*/
118 // ---------------------------------------------------------------------
119 /* Evaluate the package pins and the default list to deteremine what the
121 pkgCache::VerIterator
pkgPolicy::GetCandidateVer(pkgCache::PkgIterator
const &Pkg
)
123 // Look for a package pin and evaluate it.
124 signed Max
= GetPriority(Pkg
);
125 pkgCache::VerIterator Pref
= GetMatch(Pkg
);
127 // Alternatives in case we can not find our package pin (Bug#512318).
129 pkgCache::VerIterator PrefAlt
;
131 // no package = no candidate version
132 if (Pkg
.end() == true)
135 // packages with a pin lower than 0 have no newer candidate than the current version
137 return Pkg
.CurrentVer();
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.
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.
151 for (pkgCache::VerIterator Ver
= Pkg
.VersionList(); Ver
.end() == false; Ver
++)
153 /* Lets see if this version is the installed version */
154 bool instVer
= (Pkg
.CurrentVer() == Ver
);
156 for (pkgCache::VerFileIterator VF
= Ver
.FileList(); VF
.end() == false; VF
++)
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
163 if ((VF
.File()->Flags
& pkgCache::Flag::NotSource
) == pkgCache::Flag::NotSource
&&
167 signed Prio
= PFPriority
[VF
.File()->ID
];
180 if (instVer
== true && Max
< 1000)
182 /* Elevate our current selection (or the status file itself)
183 to the Pseudo-status priority. */
184 if (Pref
.end() == true)
188 // Fast path optimize.
189 if (StatusOverride
== false)
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)
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. */
208 void pkgPolicy::CreatePin(pkgVersionMatch::MatchType Type
,string Name
,
209 string Data
,signed short Priority
)
211 if (Name
.empty() == true)
213 Pin
*P
= &*Defaults
.insert(Defaults
.end(),PkgPin());
215 P
->Priority
= Priority
;
220 // Allow pinning by wildcards
221 // TODO: Maybe we should always prefer specific pins over non-
223 if (Name
[0] == '/' || Name
.find_first_of("*[?") != string::npos
)
225 pkgVersionMatch
match(Data
, Type
);
226 for (pkgCache::GrpIterator G
= Cache
->GrpBegin(); G
.end() != true; ++G
)
227 if (match
.ExpressionMatches(Name
, G
.Name()))
228 CreatePin(Type
, G
.Name(), Data
, Priority
);
232 // Get a spot to put the pin
233 pkgCache::GrpIterator Grp
= Cache
->FindGrp(Name
);
234 for (pkgCache::PkgIterator Pkg
= Grp
.PackageList();
235 Pkg
.end() != true; Pkg
= Grp
.NextPkg(Pkg
))
238 if (Pkg
.end() == false)
242 // Check the unmatched table
243 for (vector
<PkgPin
>::iterator I
= Unmatched
.begin();
244 I
!= Unmatched
.end() && P
== 0; I
++)
249 P
= &*Unmatched
.insert(Unmatched
.end(),PkgPin());
252 P
->Priority
= Priority
;
257 // Policy::GetMatch - Get the matching version for a package pin /*{{{*/
258 // ---------------------------------------------------------------------
260 pkgCache::VerIterator
pkgPolicy::GetMatch(pkgCache::PkgIterator
const &Pkg
)
262 const Pin
&PPkg
= Pins
[Pkg
->ID
];
263 if (PPkg
.Type
== pkgVersionMatch::None
)
264 return pkgCache::VerIterator(*Pkg
.Cache());
266 pkgVersionMatch
Match(PPkg
.Data
,PPkg
.Type
);
267 return Match
.Find(Pkg
);
270 // Policy::GetPriority - Get the priority of the package pin /*{{{*/
271 // ---------------------------------------------------------------------
273 signed short pkgPolicy::GetPriority(pkgCache::PkgIterator
const &Pkg
)
275 if (Pins
[Pkg
->ID
].Type
!= pkgVersionMatch::None
)
277 // In this case 0 means default priority
278 if (Pins
[Pkg
->ID
].Priority
== 0)
280 return Pins
[Pkg
->ID
].Priority
;
285 signed short pkgPolicy::GetPriority(pkgCache::PkgFileIterator
const &File
)
287 return PFPriority
[File
->ID
];
290 // PreferenceSection class - Overriding the default TrimRecord method /*{{{*/
291 // ---------------------------------------------------------------------
292 /* The preference file is a user generated file so the parser should
293 therefore be a bit more friendly by allowing comments and new lines
294 all over the place rather than forcing a special format */
295 class PreferenceSection
: public pkgTagSection
297 void TrimRecord(bool BeforeRecord
, const char* &End
)
299 for (; Stop
< End
&& (Stop
[0] == '\n' || Stop
[0] == '\r' || Stop
[0] == '#'); Stop
++)
301 Stop
= (const char*) memchr(Stop
,'\n',End
-Stop
);
305 // ReadPinDir - Load the pin files from this dir into a Policy /*{{{*/
306 // ---------------------------------------------------------------------
307 /* This will load each pin file in the given dir into a Policy. If the
308 given dir is empty the dir set in Dir::Etc::PreferencesParts is used.
309 Note also that this method will issue a warning if the dir does not
310 exists but it will return true in this case! */
311 bool ReadPinDir(pkgPolicy
&Plcy
,string Dir
)
313 if (Dir
.empty() == true)
314 Dir
= _config
->FindDir("Dir::Etc::PreferencesParts");
316 if (DirectoryExists(Dir
) == false)
318 _error
->WarningE("DirectoryExists",_("Unable to read %s"),Dir
.c_str());
322 vector
<string
> const List
= GetListOfFilesInDir(Dir
, "pref", true, true);
325 for (vector
<string
>::const_iterator I
= List
.begin(); I
!= List
.end(); I
++)
326 if (ReadPinFile(Plcy
, *I
) == false)
331 // ReadPinFile - Load the pin file into a Policy /*{{{*/
332 // ---------------------------------------------------------------------
333 /* I'd like to see the preferences file store more than just pin information
334 but right now that is the only stuff I have to store. Later there will
335 have to be some kind of combined super parser to get the data into all
336 the right classes.. */
337 bool ReadPinFile(pkgPolicy
&Plcy
,string File
)
339 if (File
.empty() == true)
340 File
= _config
->FindFile("Dir::Etc::Preferences");
342 if (RealFileExists(File
) == false)
345 FileFd
Fd(File
,FileFd::ReadOnly
);
347 if (_error
->PendingError() == true)
350 PreferenceSection Tags
;
351 while (TF
.Step(Tags
) == true)
353 string Name
= Tags
.FindS("Package");
354 if (Name
.empty() == true)
355 return _error
->Error(_("Invalid record in the preferences file %s, no Package header"), File
.c_str());
361 if (Tags
.Find("Pin",Start
,End
) == false)
364 const char *Word
= Start
;
365 for (; Word
!= End
&& isspace(*Word
) == 0; Word
++);
368 pkgVersionMatch::MatchType Type
;
369 if (stringcasecmp(Start
,Word
,"version") == 0 && Name
.empty() == false)
370 Type
= pkgVersionMatch::Version
;
371 else if (stringcasecmp(Start
,Word
,"release") == 0)
372 Type
= pkgVersionMatch::Release
;
373 else if (stringcasecmp(Start
,Word
,"origin") == 0)
374 Type
= pkgVersionMatch::Origin
;
377 _error
->Warning(_("Did not understand pin type %s"),string(Start
,Word
).c_str());
380 for (; Word
!= End
&& isspace(*Word
) != 0; Word
++);
382 short int priority
= Tags
.FindI("Pin-Priority", 0);
385 _error
->Warning(_("No priority (or zero) specified for pin"));
389 istringstream
s(Name
);
394 Plcy
.CreatePin(Type
, pkg
, string(Word
,End
),priority
);