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