]>
Commit | Line | Data |
---|---|---|
1 | // -*- mode: cpp; mode: fold -*- | |
2 | // Description /*{{{*/ | |
3 | // $Id: policy.cc,v 1.10 2003/08/12 00:17:37 mdz Exp $ | |
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 | See man apt_preferences for what value means what. | |
12 | ||
13 | ##################################################################### */ | |
14 | /*}}}*/ | |
15 | // Include Files /*{{{*/ | |
16 | #include<config.h> | |
17 | ||
18 | #include <apt-pkg/policy.h> | |
19 | #include <apt-pkg/configuration.h> | |
20 | #include <apt-pkg/cachefilter.h> | |
21 | #include <apt-pkg/tagfile.h> | |
22 | #include <apt-pkg/strutl.h> | |
23 | #include <apt-pkg/fileutl.h> | |
24 | #include <apt-pkg/error.h> | |
25 | #include <apt-pkg/sptr.h> | |
26 | #include <apt-pkg/cacheiterators.h> | |
27 | #include <apt-pkg/pkgcache.h> | |
28 | #include <apt-pkg/versionmatch.h> | |
29 | #include <apt-pkg/version.h> | |
30 | ||
31 | #include <ctype.h> | |
32 | #include <stddef.h> | |
33 | #include <string.h> | |
34 | #include <string> | |
35 | #include <vector> | |
36 | #include <iostream> | |
37 | #include <sstream> | |
38 | ||
39 | #include <apti18n.h> | |
40 | /*}}}*/ | |
41 | ||
42 | using namespace std; | |
43 | ||
44 | // Policy::Init - Startup and bind to a cache /*{{{*/ | |
45 | // --------------------------------------------------------------------- | |
46 | /* Set the defaults for operation. The default mode with no loaded policy | |
47 | file matches the V0 policy engine. */ | |
48 | pkgPolicy::pkgPolicy(pkgCache *Owner) : Pins(0), PFPriority(0), Cache(Owner), d(NULL) | |
49 | { | |
50 | if (Owner == 0) | |
51 | return; | |
52 | PFPriority = new signed short[Owner->Head().PackageFileCount]; | |
53 | Pins = new Pin[Owner->Head().PackageCount]; | |
54 | VerPins = new Pin[Owner->Head().VersionCount]; | |
55 | ||
56 | for (unsigned long I = 0; I != Owner->Head().PackageCount; I++) | |
57 | Pins[I].Type = pkgVersionMatch::None; | |
58 | for (unsigned long I = 0; I != Owner->Head().VersionCount; I++) | |
59 | VerPins[I].Type = pkgVersionMatch::None; | |
60 | ||
61 | // The config file has a master override. | |
62 | string DefRel = _config->Find("APT::Default-Release"); | |
63 | if (DefRel.empty() == false) | |
64 | { | |
65 | bool found = false; | |
66 | // FIXME: make ExpressionMatches static to use it here easily | |
67 | pkgVersionMatch vm("", pkgVersionMatch::None); | |
68 | for (pkgCache::PkgFileIterator F = Cache->FileBegin(); F != Cache->FileEnd(); ++F) | |
69 | { | |
70 | if (vm.ExpressionMatches(DefRel, F.Archive()) || | |
71 | vm.ExpressionMatches(DefRel, F.Codename()) || | |
72 | vm.ExpressionMatches(DefRel, F.Version()) || | |
73 | (DefRel.length() > 2 && DefRel[1] == '=')) | |
74 | found = true; | |
75 | } | |
76 | if (found == false) | |
77 | _error->Error(_("The value '%s' is invalid for APT::Default-Release as such a release is not available in the sources"), DefRel.c_str()); | |
78 | else | |
79 | CreatePin(pkgVersionMatch::Release,"",DefRel,990); | |
80 | } | |
81 | InitDefaults(); | |
82 | } | |
83 | /*}}}*/ | |
84 | // Policy::InitDefaults - Compute the default selections /*{{{*/ | |
85 | // --------------------------------------------------------------------- | |
86 | /* */ | |
87 | bool pkgPolicy::InitDefaults() | |
88 | { | |
89 | // Initialize the priorities based on the status of the package file | |
90 | for (pkgCache::PkgFileIterator I = Cache->FileBegin(); I != Cache->FileEnd(); ++I) | |
91 | { | |
92 | PFPriority[I->ID] = 500; | |
93 | if (I.Flagged(pkgCache::Flag::NotSource)) | |
94 | PFPriority[I->ID] = 100; | |
95 | else if (I.Flagged(pkgCache::Flag::ButAutomaticUpgrades)) | |
96 | PFPriority[I->ID] = 100; | |
97 | else if (I.Flagged(pkgCache::Flag::NotAutomatic)) | |
98 | PFPriority[I->ID] = 1; | |
99 | } | |
100 | ||
101 | // Apply the defaults.. | |
102 | SPtrArray<bool> Fixed = new bool[Cache->HeaderP->PackageFileCount]; | |
103 | memset(Fixed,0,sizeof(*Fixed)*Cache->HeaderP->PackageFileCount); | |
104 | StatusOverride = false; | |
105 | for (vector<Pin>::const_iterator I = Defaults.begin(); I != Defaults.end(); ++I) | |
106 | { | |
107 | pkgVersionMatch Match(I->Data,I->Type); | |
108 | for (pkgCache::PkgFileIterator F = Cache->FileBegin(); F != Cache->FileEnd(); ++F) | |
109 | { | |
110 | if (Fixed[F->ID] == false && Match.FileMatch(F) == true) | |
111 | { | |
112 | PFPriority[F->ID] = I->Priority; | |
113 | ||
114 | if (PFPriority[F->ID] >= 1000) | |
115 | StatusOverride = true; | |
116 | ||
117 | Fixed[F->ID] = true; | |
118 | } | |
119 | } | |
120 | } | |
121 | ||
122 | if (_config->FindB("Debug::pkgPolicy",false) == true) | |
123 | for (pkgCache::PkgFileIterator F = Cache->FileBegin(); F != Cache->FileEnd(); ++F) | |
124 | std::clog << "Prio of " << F.FileName() << ' ' << PFPriority[F->ID] << std::endl; | |
125 | ||
126 | return true; | |
127 | } | |
128 | /*}}}*/ | |
129 | // Policy::GetCandidateVer - Get the candidate install version /*{{{*/ | |
130 | // --------------------------------------------------------------------- | |
131 | /* Evaluate the package pins and the default list to deteremine what the | |
132 | best package is. */ | |
133 | pkgCache::VerIterator pkgPolicy::GetCandidateVer(pkgCache::PkgIterator const &Pkg) | |
134 | { | |
135 | if (_config->FindI("APT::Policy", 1) >= 1) { | |
136 | return GetCandidateVerNew(Pkg); | |
137 | } | |
138 | ||
139 | // Look for a package pin and evaluate it. | |
140 | signed Max = GetPriority(Pkg); | |
141 | pkgCache::VerIterator Pref = GetMatch(Pkg); | |
142 | ||
143 | // Alternatives in case we can not find our package pin (Bug#512318). | |
144 | signed MaxAlt = 0; | |
145 | pkgCache::VerIterator PrefAlt; | |
146 | ||
147 | // no package = no candidate version | |
148 | if (Pkg.end() == true) | |
149 | return Pref; | |
150 | ||
151 | // packages with a pin lower than 0 have no newer candidate than the current version | |
152 | if (Max < 0) | |
153 | return Pkg.CurrentVer(); | |
154 | ||
155 | /* Falling through to the default version.. Setting Max to zero | |
156 | effectively excludes everything <= 0 which are the non-automatic | |
157 | priorities.. The status file is given a prio of 100 which will exclude | |
158 | not-automatic sources, except in a single shot not-installed mode. | |
159 | ||
160 | The user pin is subject to the same priority rules as default | |
161 | selections. Thus there are two ways to create a pin - a pin that | |
162 | tracks the default when the default is taken away, and a permanent | |
163 | pin that stays at that setting. | |
164 | */ | |
165 | bool PrefSeen = false; | |
166 | for (pkgCache::VerIterator Ver = Pkg.VersionList(); Ver.end() == false; ++Ver) | |
167 | { | |
168 | /* Lets see if this version is the installed version */ | |
169 | bool instVer = (Pkg.CurrentVer() == Ver); | |
170 | ||
171 | if (Pref == Ver) | |
172 | PrefSeen = true; | |
173 | ||
174 | for (pkgCache::VerFileIterator VF = Ver.FileList(); VF.end() == false; ++VF) | |
175 | { | |
176 | /* If this is the status file, and the current version is not the | |
177 | version in the status file (ie it is not installed, or somesuch) | |
178 | then it is not a candidate for installation, ever. This weeds | |
179 | out bogus entries that may be due to config-file states, or | |
180 | other. */ | |
181 | if (VF.File().Flagged(pkgCache::Flag::NotSource) && instVer == false) | |
182 | continue; | |
183 | ||
184 | signed Prio = PFPriority[VF.File()->ID]; | |
185 | if (Prio > Max) | |
186 | { | |
187 | Pref = Ver; | |
188 | Max = Prio; | |
189 | PrefSeen = true; | |
190 | } | |
191 | if (Prio > MaxAlt) | |
192 | { | |
193 | PrefAlt = Ver; | |
194 | MaxAlt = Prio; | |
195 | } | |
196 | } | |
197 | ||
198 | if (instVer == true && Max < 1000) | |
199 | { | |
200 | /* Not having seen the Pref yet means we have a specific pin below 1000 | |
201 | on a version below the current installed one, so ignore the specific pin | |
202 | as this would be a downgrade otherwise */ | |
203 | if (PrefSeen == false || Pref.end() == true) | |
204 | { | |
205 | Pref = Ver; | |
206 | PrefSeen = true; | |
207 | } | |
208 | /* Elevate our current selection (or the status file itself) so that only | |
209 | a downgrade can override it from now on */ | |
210 | Max = 999; | |
211 | ||
212 | // Fast path optimize. | |
213 | if (StatusOverride == false) | |
214 | break; | |
215 | } | |
216 | } | |
217 | // If we do not find our candidate, use the one with the highest pin. | |
218 | // This means that if there is a version available with pin > 0; there | |
219 | // will always be a candidate (Closes: #512318) | |
220 | if (!Pref.IsGood() && MaxAlt > 0) | |
221 | Pref = PrefAlt; | |
222 | ||
223 | return Pref; | |
224 | } | |
225 | ||
226 | // Policy::GetCandidateVer - Get the candidate install version /*{{{*/ | |
227 | // --------------------------------------------------------------------- | |
228 | /* Evaluate the package pins and the default list to deteremine what the | |
229 | best package is. */ | |
230 | pkgCache::VerIterator pkgPolicy::GetCandidateVerNew(pkgCache::PkgIterator const &Pkg) | |
231 | { | |
232 | // TODO: Replace GetCandidateVer() | |
233 | pkgCache::VerIterator cand; | |
234 | pkgCache::VerIterator cur = Pkg.CurrentVer(); | |
235 | int candPriority = -1; | |
236 | pkgVersioningSystem *vs = Cache->VS; | |
237 | ||
238 | for (pkgCache::VerIterator ver = Pkg.VersionList(); ver.end() == false; ver++) { | |
239 | int priority = GetPriority(ver); | |
240 | ||
241 | if (priority == 0 || priority <= candPriority) | |
242 | continue; | |
243 | ||
244 | // TODO: Maybe optimize to not compare versions | |
245 | if (!cur.end() && priority < 1000 | |
246 | && (vs->CmpVersion(ver.VerStr(), cur.VerStr()) < 0)) | |
247 | continue; | |
248 | ||
249 | candPriority = priority; | |
250 | cand = ver; | |
251 | } | |
252 | ||
253 | return cand; | |
254 | } | |
255 | /*}}}*/ | |
256 | // Policy::CreatePin - Create an entry in the pin table.. /*{{{*/ | |
257 | // --------------------------------------------------------------------- | |
258 | /* For performance we have 3 tables, the default table, the main cache | |
259 | table (hashed to the cache). A blank package name indicates the pin | |
260 | belongs to the default table. Order of insertion matters here, the | |
261 | earlier defaults override later ones. */ | |
262 | void pkgPolicy::CreatePin(pkgVersionMatch::MatchType Type,string Name, | |
263 | string Data,signed short Priority) | |
264 | { | |
265 | if (Name.empty() == true) | |
266 | { | |
267 | Pin *P = &*Defaults.insert(Defaults.end(),Pin()); | |
268 | P->Type = Type; | |
269 | P->Priority = Priority; | |
270 | P->Data = Data; | |
271 | return; | |
272 | } | |
273 | ||
274 | size_t found = Name.rfind(':'); | |
275 | string Arch; | |
276 | if (found != string::npos) { | |
277 | Arch = Name.substr(found+1); | |
278 | Name.erase(found); | |
279 | } | |
280 | ||
281 | // Allow pinning by wildcards | |
282 | // TODO: Maybe we should always prefer specific pins over non- | |
283 | // specific ones. | |
284 | if (Name[0] == '/' || Name.find_first_of("*[?") != string::npos) | |
285 | { | |
286 | pkgVersionMatch match(Data, Type); | |
287 | for (pkgCache::GrpIterator G = Cache->GrpBegin(); G.end() != true; ++G) | |
288 | if (match.ExpressionMatches(Name, G.Name())) | |
289 | { | |
290 | if (Arch.empty() == false) | |
291 | CreatePin(Type, string(G.Name()).append(":").append(Arch), Data, Priority); | |
292 | else | |
293 | CreatePin(Type, G.Name(), Data, Priority); | |
294 | } | |
295 | return; | |
296 | } | |
297 | ||
298 | // find the package (group) this pin applies to | |
299 | pkgCache::GrpIterator Grp = Cache->FindGrp(Name); | |
300 | bool matched = false; | |
301 | if (Grp.end() == false) | |
302 | { | |
303 | std::string MatchingArch; | |
304 | if (Arch.empty() == true) | |
305 | MatchingArch = Cache->NativeArch(); | |
306 | else | |
307 | MatchingArch = Arch; | |
308 | APT::CacheFilter::PackageArchitectureMatchesSpecification pams(MatchingArch); | |
309 | for (pkgCache::PkgIterator Pkg = Grp.PackageList(); Pkg.end() != true; Pkg = Grp.NextPkg(Pkg)) | |
310 | { | |
311 | if (pams(Pkg.Arch()) == false) | |
312 | continue; | |
313 | Pin *P = Pins + Pkg->ID; | |
314 | // the first specific stanza for a package is the ruler, | |
315 | // all others need to be ignored | |
316 | if (P->Type != pkgVersionMatch::None) | |
317 | P = &*Unmatched.insert(Unmatched.end(),PkgPin(Pkg.FullName())); | |
318 | P->Type = Type; | |
319 | P->Priority = Priority; | |
320 | P->Data = Data; | |
321 | matched = true; | |
322 | ||
323 | // Find matching version(s) and copy the pin into it | |
324 | pkgVersionMatch Match(P->Data,P->Type); | |
325 | for (pkgCache::VerIterator Ver = Pkg.VersionList(); Ver.end() != true; Ver++) | |
326 | { | |
327 | if (Match.VersionMatches(Ver)) { | |
328 | Pin *VP = VerPins + Ver->ID; | |
329 | if (VP->Type == pkgVersionMatch::None) | |
330 | *VP = *P; | |
331 | } | |
332 | } | |
333 | } | |
334 | } | |
335 | ||
336 | if (matched == false) | |
337 | { | |
338 | PkgPin *P = &*Unmatched.insert(Unmatched.end(),PkgPin(Name)); | |
339 | if (Arch.empty() == false) | |
340 | P->Pkg.append(":").append(Arch); | |
341 | P->Type = Type; | |
342 | P->Priority = Priority; | |
343 | P->Data = Data; | |
344 | return; | |
345 | } | |
346 | } | |
347 | /*}}}*/ | |
348 | // Policy::GetMatch - Get the matching version for a package pin /*{{{*/ | |
349 | // --------------------------------------------------------------------- | |
350 | /* */ | |
351 | pkgCache::VerIterator pkgPolicy::GetMatch(pkgCache::PkgIterator const &Pkg) | |
352 | { | |
353 | const Pin &PPkg = Pins[Pkg->ID]; | |
354 | if (PPkg.Type == pkgVersionMatch::None) | |
355 | return pkgCache::VerIterator(*Pkg.Cache()); | |
356 | ||
357 | pkgVersionMatch Match(PPkg.Data,PPkg.Type); | |
358 | return Match.Find(Pkg); | |
359 | } | |
360 | /*}}}*/ | |
361 | // Policy::GetPriority - Get the priority of the package pin /*{{{*/ | |
362 | // --------------------------------------------------------------------- | |
363 | /* */ | |
364 | APT_PURE signed short pkgPolicy::GetPriority(pkgCache::PkgIterator const &Pkg) | |
365 | { | |
366 | if (Pins[Pkg->ID].Type != pkgVersionMatch::None) | |
367 | return Pins[Pkg->ID].Priority; | |
368 | return 0; | |
369 | } | |
370 | APT_PURE signed short pkgPolicy::GetPriority(pkgCache::VerIterator const &Ver) | |
371 | { | |
372 | if (VerPins[Ver->ID].Type != pkgVersionMatch::None) | |
373 | return VerPins[Ver->ID].Priority; | |
374 | ||
375 | ||
376 | int priority = INT_MIN; | |
377 | for (pkgCache::VerFileIterator file = Ver.FileList(); file.end() == false; file++) | |
378 | { | |
379 | /* If this is the status file, and the current version is not the | |
380 | version in the status file (ie it is not installed, or somesuch) | |
381 | then it is not a candidate for installation, ever. This weeds | |
382 | out bogus entries that may be due to config-file states, or | |
383 | other. */ | |
384 | if (file.File().Flagged(pkgCache::Flag::NotSource) && Ver.ParentPkg().CurrentVer() != Ver) { | |
385 | if (priority < 0) | |
386 | priority = 0; | |
387 | } else if (GetPriority(file.File()) > priority) { | |
388 | priority = GetPriority(file.File()); | |
389 | } | |
390 | } | |
391 | ||
392 | return priority == INT_MIN ? 0 : priority; | |
393 | } | |
394 | APT_PURE signed short pkgPolicy::GetPriority(pkgCache::PkgFileIterator const &File) | |
395 | { | |
396 | return PFPriority[File->ID]; | |
397 | } | |
398 | /*}}}*/ | |
399 | // PreferenceSection class - Overriding the default TrimRecord method /*{{{*/ | |
400 | // --------------------------------------------------------------------- | |
401 | /* The preference file is a user generated file so the parser should | |
402 | therefore be a bit more friendly by allowing comments and new lines | |
403 | all over the place rather than forcing a special format */ | |
404 | class PreferenceSection : public pkgTagSection | |
405 | { | |
406 | void TrimRecord(bool /*BeforeRecord*/, const char* &End) | |
407 | { | |
408 | for (; Stop < End && (Stop[0] == '\n' || Stop[0] == '\r' || Stop[0] == '#'); Stop++) | |
409 | if (Stop[0] == '#') | |
410 | Stop = (const char*) memchr(Stop,'\n',End-Stop); | |
411 | } | |
412 | }; | |
413 | /*}}}*/ | |
414 | // ReadPinDir - Load the pin files from this dir into a Policy /*{{{*/ | |
415 | // --------------------------------------------------------------------- | |
416 | /* This will load each pin file in the given dir into a Policy. If the | |
417 | given dir is empty the dir set in Dir::Etc::PreferencesParts is used. | |
418 | Note also that this method will issue a warning if the dir does not | |
419 | exists but it will return true in this case! */ | |
420 | bool ReadPinDir(pkgPolicy &Plcy,string Dir) | |
421 | { | |
422 | if (Dir.empty() == true) | |
423 | Dir = _config->FindDir("Dir::Etc::PreferencesParts"); | |
424 | ||
425 | if (DirectoryExists(Dir) == false) | |
426 | { | |
427 | _error->WarningE("DirectoryExists",_("Unable to read %s"),Dir.c_str()); | |
428 | return true; | |
429 | } | |
430 | ||
431 | vector<string> const List = GetListOfFilesInDir(Dir, "pref", true, true); | |
432 | ||
433 | // Read the files | |
434 | for (vector<string>::const_iterator I = List.begin(); I != List.end(); ++I) | |
435 | if (ReadPinFile(Plcy, *I) == false) | |
436 | return false; | |
437 | return true; | |
438 | } | |
439 | /*}}}*/ | |
440 | // ReadPinFile - Load the pin file into a Policy /*{{{*/ | |
441 | // --------------------------------------------------------------------- | |
442 | /* I'd like to see the preferences file store more than just pin information | |
443 | but right now that is the only stuff I have to store. Later there will | |
444 | have to be some kind of combined super parser to get the data into all | |
445 | the right classes.. */ | |
446 | bool ReadPinFile(pkgPolicy &Plcy,string File) | |
447 | { | |
448 | if (File.empty() == true) | |
449 | File = _config->FindFile("Dir::Etc::Preferences"); | |
450 | ||
451 | if (RealFileExists(File) == false) | |
452 | return true; | |
453 | ||
454 | FileFd Fd(File,FileFd::ReadOnly); | |
455 | pkgTagFile TF(&Fd); | |
456 | if (_error->PendingError() == true) | |
457 | return false; | |
458 | ||
459 | PreferenceSection Tags; | |
460 | while (TF.Step(Tags) == true) | |
461 | { | |
462 | // can happen when there are only comments in a record | |
463 | if (Tags.Count() == 0) | |
464 | continue; | |
465 | ||
466 | string Name = Tags.FindS("Package"); | |
467 | if (Name.empty() == true) | |
468 | return _error->Error(_("Invalid record in the preferences file %s, no Package header"), File.c_str()); | |
469 | if (Name == "*") | |
470 | Name = string(); | |
471 | ||
472 | const char *Start; | |
473 | const char *End; | |
474 | if (Tags.Find("Pin",Start,End) == false) | |
475 | continue; | |
476 | ||
477 | const char *Word = Start; | |
478 | for (; Word != End && isspace(*Word) == 0; Word++); | |
479 | ||
480 | // Parse the type.. | |
481 | pkgVersionMatch::MatchType Type; | |
482 | if (stringcasecmp(Start,Word,"version") == 0 && Name.empty() == false) | |
483 | Type = pkgVersionMatch::Version; | |
484 | else if (stringcasecmp(Start,Word,"release") == 0) | |
485 | Type = pkgVersionMatch::Release; | |
486 | else if (stringcasecmp(Start,Word,"origin") == 0) | |
487 | Type = pkgVersionMatch::Origin; | |
488 | else | |
489 | { | |
490 | _error->Warning(_("Did not understand pin type %s"),string(Start,Word).c_str()); | |
491 | continue; | |
492 | } | |
493 | for (; Word != End && isspace(*Word) != 0; Word++); | |
494 | ||
495 | short int priority = Tags.FindI("Pin-Priority", 0); | |
496 | if (priority == 0) | |
497 | { | |
498 | _error->Warning(_("No priority (or zero) specified for pin")); | |
499 | continue; | |
500 | } | |
501 | ||
502 | istringstream s(Name); | |
503 | string pkg; | |
504 | while(!s.eof()) | |
505 | { | |
506 | s >> pkg; | |
507 | Plcy.CreatePin(Type, pkg, string(Word,End),priority); | |
508 | }; | |
509 | } | |
510 | ||
511 | Plcy.InitDefaults(); | |
512 | return true; | |
513 | } | |
514 | /*}}}*/ | |
515 | ||
516 | pkgPolicy::~pkgPolicy() {delete [] PFPriority; delete [] Pins; delete [] VerPins; } |