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
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/error.h>
31 #include <apt-pkg/sptr.h>
40 // Policy::Init - Startup and bind to a cache /*{{{*/
41 // ---------------------------------------------------------------------
42 /* Set the defaults for operation. The default mode with no loaded policy
43 file matches the V0 policy engine. */
44 pkgPolicy::pkgPolicy(pkgCache
*Owner
) : Pins(0), PFPriority(0), Cache(Owner
)
46 PFPriority
= new signed short[Owner
->Head().PackageFileCount
];
47 Pins
= new Pin
[Owner
->Head().PackageCount
];
49 for (unsigned long I
= 0; I
!= Owner
->Head().PackageCount
; I
++)
50 Pins
[I
].Type
= pkgVersionMatch::None
;
52 // The config file has a master override.
53 string DefRel
= _config
->Find("APT::Default-Release");
54 if (DefRel
.empty() == false)
55 CreatePin(pkgVersionMatch::Release
,"",DefRel
,990);
60 // Policy::InitDefaults - Compute the default selections /*{{{*/
61 // ---------------------------------------------------------------------
63 bool pkgPolicy::InitDefaults()
65 // Initialize the priorities based on the status of the package file
66 for (pkgCache::PkgFileIterator I
= Cache
->FileBegin(); I
!= Cache
->FileEnd(); I
++)
68 PFPriority
[I
->ID
] = 500;
69 if ((I
->Flags
& pkgCache::Flag::NotSource
) == pkgCache::Flag::NotSource
)
70 PFPriority
[I
->ID
] = 100;
72 if ((I
->Flags
& pkgCache::Flag::NotAutomatic
) == pkgCache::Flag::NotAutomatic
)
73 PFPriority
[I
->ID
] = 1;
76 // Apply the defaults..
77 SPtrArray
<bool> Fixed
= new bool[Cache
->HeaderP
->PackageFileCount
];
78 memset(Fixed
,0,sizeof(*Fixed
)*Cache
->HeaderP
->PackageFileCount
);
80 StatusOverride
= false;
81 for (vector
<Pin
>::const_iterator I
= Defaults
.begin(); I
!= Defaults
.end();
84 pkgVersionMatch
Match(I
->Data
,I
->Type
);
85 for (pkgCache::PkgFileIterator F
= Cache
->FileBegin(); F
!= Cache
->FileEnd(); F
++)
87 if (Match
.FileMatch(F
) == true && Fixed
[F
->ID
] == false)
89 if (I
->Priority
!= 0 && I
->Priority
> 0)
93 PFPriority
[F
->ID
] = I
->Priority
;
95 PFPriority
[F
->ID
] = Cur
;
97 if (PFPriority
[F
->ID
] > 1000)
98 StatusOverride
= true;
105 if (_config
->FindB("Debug::pkgPolicy",false) == true)
106 for (pkgCache::PkgFileIterator F
= Cache
->FileBegin(); F
!= Cache
->FileEnd(); F
++)
107 cout
<< "Prio of " << F
.FileName() << ' ' << PFPriority
[F
->ID
] << endl
;
112 // Policy::GetCandidateVer - Get the candidate install version /*{{{*/
113 // ---------------------------------------------------------------------
114 /* Evaluate the package pins and the default list to deteremine what the
116 pkgCache::VerIterator
pkgPolicy::GetCandidateVer(pkgCache::PkgIterator Pkg
)
118 // Look for a package pin and evaluate it.
119 signed Max
= GetPriority(Pkg
);
120 pkgCache::VerIterator Pref
= GetMatch(Pkg
);
122 /* Falling through to the default version.. Setting Max to zero
123 effectively excludes everything <= 0 which are the non-automatic
124 priorities.. The status file is given a prio of 100 which will exclude
125 not-automatic sources, except in a single shot not-installed mode.
126 The second pseduo-status file is at prio 1000, above which will permit
127 the user to force-downgrade things.
129 The user pin is subject to the same priority rules as default
130 selections. Thus there are two ways to create a pin - a pin that
131 tracks the default when the default is taken away, and a permanent
132 pin that stays at that setting.
134 for (pkgCache::VerIterator Ver
= Pkg
.VersionList(); Ver
.end() == false; Ver
++)
136 for (pkgCache::VerFileIterator VF
= Ver
.FileList(); VF
.end() == false; VF
++)
138 /* If this is the status file, and the current version is not the
139 version in the status file (ie it is not installed, or somesuch)
140 then it is not a candidate for installation, ever. This weeds
141 out bogus entries that may be due to config-file states, or
143 if ((VF
.File()->Flags
& pkgCache::Flag::NotSource
) == pkgCache::Flag::NotSource
&&
144 Pkg
.CurrentVer() != Ver
)
147 signed Prio
= PFPriority
[VF
.File()->ID
];
155 if (Pkg
.CurrentVer() == Ver
&& Max
< 1000)
157 /* Elevate our current selection (or the status file itself)
158 to the Pseudo-status priority. */
159 if (Pref
.end() == true)
163 // Fast path optimize.
164 if (StatusOverride
== false)
171 // Policy::CreatePin - Create an entry in the pin table.. /*{{{*/
172 // ---------------------------------------------------------------------
173 /* For performance we have 3 tables, the default table, the main cache
174 table (hashed to the cache). A blank package name indicates the pin
175 belongs to the default table. Order of insertion matters here, the
176 earlier defaults override later ones. */
177 void pkgPolicy::CreatePin(pkgVersionMatch::MatchType Type
,string Name
,
178 string Data
,signed short Priority
)
182 if (Name
.empty() == true)
183 P
= &*Defaults
.insert(Defaults
.end(),PkgPin());
186 // Get a spot to put the pin
187 pkgCache::PkgIterator Pkg
= Cache
->FindPkg(Name
);
188 if (Pkg
.end() == true)
190 // Check the unmatched table
191 for (vector
<PkgPin
>::iterator I
= Unmatched
.begin();
192 I
!= Unmatched
.end() && P
== 0; I
++)
197 P
= &*Unmatched
.insert(Unmatched
.end(),PkgPin());
207 P
->Priority
= Priority
;
211 // Policy::GetMatch - Get the matching version for a package pin /*{{{*/
212 // ---------------------------------------------------------------------
214 pkgCache::VerIterator
pkgPolicy::GetMatch(pkgCache::PkgIterator Pkg
)
216 const Pin
&PPkg
= Pins
[Pkg
->ID
];
217 if (PPkg
.Type
!= pkgVersionMatch::None
)
219 pkgVersionMatch
Match(PPkg
.Data
,PPkg
.Type
);
220 return Match
.Find(Pkg
);
222 return pkgCache::VerIterator(*Pkg
.Cache());
225 // Policy::GetPriority - Get the priority of the package pin /*{{{*/
226 // ---------------------------------------------------------------------
228 signed short pkgPolicy::GetPriority(pkgCache::PkgIterator
const &Pkg
)
230 if (Pins
[Pkg
->ID
].Type
!= pkgVersionMatch::None
)
232 // In this case 0 means default priority
233 if (Pins
[Pkg
->ID
].Priority
== 0)
235 return Pins
[Pkg
->ID
].Priority
;
242 // ReadPinFile - Load the pin file into a Policy /*{{{*/
243 // ---------------------------------------------------------------------
244 /* I'd like to see the preferences file store more than just pin information
245 but right now that is the only stuff I have to store. Later there will
246 have to be some kind of combined super parser to get the data into all
247 the right classes.. */
248 bool ReadPinFile(pkgPolicy
&Plcy
,string File
)
250 if (File
.empty() == true)
251 File
= _config
->FindFile("Dir::Etc::Preferences");
253 if (FileExists(File
) == false)
256 FileFd
Fd(File
,FileFd::ReadOnly
);
258 if (_error
->PendingError() == true)
262 while (TF
.Step(Tags
) == true)
264 string Name
= Tags
.FindS("Package");
265 if (Name
.empty() == true)
266 return _error
->Error(_("Invalid record in the preferences file, no Package header"));
272 if (Tags
.Find("Pin",Start
,End
) == false)
275 const char *Word
= Start
;
276 for (; Word
!= End
&& isspace(*Word
) == 0; Word
++);
279 pkgVersionMatch::MatchType Type
;
280 if (stringcasecmp(Start
,Word
,"version") == 0 && Name
.empty() == false)
281 Type
= pkgVersionMatch::Version
;
282 else if (stringcasecmp(Start
,Word
,"release") == 0)
283 Type
= pkgVersionMatch::Release
;
284 else if (stringcasecmp(Start
,Word
,"origin") == 0)
285 Type
= pkgVersionMatch::Origin
;
288 _error
->Warning(_("Did not understand pin type %s"),string(Start
,Word
).c_str());
291 for (; Word
!= End
&& isspace(*Word
) != 0; Word
++);
293 short int priority
= Tags
.FindI("Pin-Priority", 0);
296 _error
->Warning(_("No priority (or zero) specified for pin"));
300 Plcy
.CreatePin(Type
,Name
,string(Word
,End
),priority
);