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