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