]>
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 | 24 | #include <apt-pkg/error.h> |
453b82a3 DK |
25 | #include <apt-pkg/cacheiterators.h> |
26 | #include <apt-pkg/pkgcache.h> | |
27 | #include <apt-pkg/versionmatch.h> | |
a91aae40 | 28 | #include <apt-pkg/version.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> |
ac7f8f79 | 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. */ | |
50bac728 DK |
47 | pkgPolicy::pkgPolicy(pkgCache *Owner) : Pins(nullptr), VerPins(nullptr), |
48 | PFPriority(nullptr), Cache(Owner), d(NULL) | |
b2e465d6 | 49 | { |
3f732aa6 | 50 | if (Owner == 0) |
c55b8a54 | 51 | return; |
b2e465d6 AL |
52 | PFPriority = new signed short[Owner->Head().PackageFileCount]; |
53 | Pins = new Pin[Owner->Head().PackageCount]; | |
9fb02ab0 | 54 | VerPins = new Pin[Owner->Head().VersionCount]; |
b2e465d6 AL |
55 | |
56 | for (unsigned long I = 0; I != Owner->Head().PackageCount; I++) | |
57 | Pins[I].Type = pkgVersionMatch::None; | |
9fb02ab0 JAK |
58 | for (unsigned long I = 0; I != Owner->Head().VersionCount; I++) |
59 | VerPins[I].Type = pkgVersionMatch::None; | |
b2e465d6 AL |
60 | |
61 | // The config file has a master override. | |
62 | string DefRel = _config->Find("APT::Default-Release"); | |
63 | if (DefRel.empty() == false) | |
a3bbbab7 DK |
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 | { | |
b07aeb1a DK |
70 | if (vm.ExpressionMatches(DefRel, F.Archive()) || |
71 | vm.ExpressionMatches(DefRel, F.Codename()) || | |
72 | vm.ExpressionMatches(DefRel, F.Version()) || | |
061c58b6 | 73 | (DefRel.length() > 2 && DefRel[1] == '=')) |
a3bbbab7 DK |
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 | } | |
b2e465d6 AL |
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 | |
f7f0d6c7 | 90 | for (pkgCache::PkgFileIterator I = Cache->FileBegin(); I != Cache->FileEnd(); ++I) |
b2e465d6 AL |
91 | { |
92 | PFPriority[I->ID] = 500; | |
b07aeb1a | 93 | if (I.Flagged(pkgCache::Flag::NotSource)) |
b2e465d6 | 94 | PFPriority[I->ID] = 100; |
b07aeb1a | 95 | else if (I.Flagged(pkgCache::Flag::ButAutomaticUpgrades)) |
5ed56f93 | 96 | PFPriority[I->ID] = 100; |
b07aeb1a | 97 | else if (I.Flagged(pkgCache::Flag::NotAutomatic)) |
5ed56f93 | 98 | PFPriority[I->ID] = 1; |
b2e465d6 AL |
99 | } |
100 | ||
101 | // Apply the defaults.. | |
98cc7fd2 JAK |
102 | std::unique_ptr<bool[]> Fixed(new bool[Cache->HeaderP->PackageFileCount]); |
103 | memset(Fixed.get(),0,sizeof(Fixed[0])*Cache->HeaderP->PackageFileCount); | |
b2e465d6 | 104 | StatusOverride = false; |
ab640001 | 105 | for (vector<Pin>::const_iterator I = Defaults.begin(); I != Defaults.end(); ++I) |
b2e465d6 AL |
106 | { |
107 | pkgVersionMatch Match(I->Data,I->Type); | |
f7f0d6c7 | 108 | for (pkgCache::PkgFileIterator F = Cache->FileBegin(); F != Cache->FileEnd(); ++F) |
b2e465d6 | 109 | { |
ab640001 | 110 | if (Fixed[F->ID] == false && Match.FileMatch(F) == true) |
b2e465d6 | 111 | { |
ab640001 DK |
112 | PFPriority[F->ID] = I->Priority; |
113 | ||
3f732aa6 | 114 | if (PFPriority[F->ID] >= 1000) |
b2e465d6 | 115 | StatusOverride = true; |
ab640001 | 116 | |
b2e465d6 | 117 | Fixed[F->ID] = true; |
ab640001 DK |
118 | } |
119 | } | |
b2e465d6 AL |
120 | } |
121 | ||
122 | if (_config->FindB("Debug::pkgPolicy",false) == true) | |
f7f0d6c7 | 123 | for (pkgCache::PkgFileIterator F = Cache->FileBegin(); F != Cache->FileEnd(); ++F) |
ab640001 DK |
124 | std::clog << "Prio of " << F.FileName() << ' ' << PFPriority[F->ID] << std::endl; |
125 | ||
126 | return true; | |
b2e465d6 AL |
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. */ | |
9ee8287e | 133 | pkgCache::VerIterator pkgPolicy::GetCandidateVer(pkgCache::PkgIterator const &Pkg) |
a91aae40 | 134 | { |
a91aae40 | 135 | pkgCache::VerIterator cand; |
3436218c | 136 | pkgCache::VerIterator cur = Pkg.CurrentVer(); |
a91aae40 | 137 | int candPriority = -1; |
a91aae40 JAK |
138 | pkgVersioningSystem *vs = Cache->VS; |
139 | ||
258b9e51 | 140 | for (pkgCache::VerIterator ver = Pkg.VersionList(); ver.end() == false; ++ver) { |
632f2476 | 141 | int priority = GetPriority(ver, true); |
a91aae40 | 142 | |
632f2476 JAK |
143 | if (priority == 0 || priority <= candPriority) |
144 | continue; | |
3436218c | 145 | |
632f2476 JAK |
146 | // TODO: Maybe optimize to not compare versions |
147 | if (!cur.end() && priority < 1000 | |
148 | && (vs->CmpVersion(ver.VerStr(), cur.VerStr()) < 0)) | |
149 | continue; | |
a91aae40 JAK |
150 | |
151 | candPriority = priority; | |
a91aae40 JAK |
152 | cand = ver; |
153 | } | |
154 | ||
155 | return cand; | |
156 | } | |
b2e465d6 AL |
157 | /*}}}*/ |
158 | // Policy::CreatePin - Create an entry in the pin table.. /*{{{*/ | |
159 | // --------------------------------------------------------------------- | |
160 | /* For performance we have 3 tables, the default table, the main cache | |
161 | table (hashed to the cache). A blank package name indicates the pin | |
162 | belongs to the default table. Order of insertion matters here, the | |
163 | earlier defaults override later ones. */ | |
164 | void pkgPolicy::CreatePin(pkgVersionMatch::MatchType Type,string Name, | |
165 | string Data,signed short Priority) | |
166 | { | |
b2e465d6 | 167 | if (Name.empty() == true) |
b2e465d6 | 168 | { |
00c6e1a3 | 169 | Pin *P = &*Defaults.insert(Defaults.end(),Pin()); |
4a6d2163 DK |
170 | P->Type = Type; |
171 | P->Priority = Priority; | |
172 | P->Data = Data; | |
173 | return; | |
174 | } | |
00c6e1a3 MV |
175 | |
176 | size_t found = Name.rfind(':'); | |
177 | string Arch; | |
178 | if (found != string::npos) { | |
179 | Arch = Name.substr(found+1); | |
180 | Name.erase(found); | |
181 | } | |
182 | ||
1a4c9766 JAK |
183 | // Allow pinning by wildcards |
184 | // TODO: Maybe we should always prefer specific pins over non- | |
185 | // specific ones. | |
a8d7c101 DK |
186 | if (Name[0] == '/' || Name.find_first_of("*[?") != string::npos) |
187 | { | |
188 | pkgVersionMatch match(Data, Type); | |
189 | for (pkgCache::GrpIterator G = Cache->GrpBegin(); G.end() != true; ++G) | |
190 | if (match.ExpressionMatches(Name, G.Name())) | |
00c6e1a3 MV |
191 | { |
192 | if (Arch.empty() == false) | |
193 | CreatePin(Type, string(G.Name()).append(":").append(Arch), Data, Priority); | |
194 | else | |
195 | CreatePin(Type, G.Name(), Data, Priority); | |
196 | } | |
1a4c9766 JAK |
197 | return; |
198 | } | |
4a6d2163 | 199 | |
00c6e1a3 | 200 | // find the package (group) this pin applies to |
9ed80cdd DK |
201 | pkgCache::GrpIterator Grp = Cache->FindGrp(Name); |
202 | bool matched = false; | |
203 | if (Grp.end() == false) | |
204 | { | |
205 | std::string MatchingArch; | |
206 | if (Arch.empty() == true) | |
207 | MatchingArch = Cache->NativeArch(); | |
208 | else | |
209 | MatchingArch = Arch; | |
210 | APT::CacheFilter::PackageArchitectureMatchesSpecification pams(MatchingArch); | |
211 | for (pkgCache::PkgIterator Pkg = Grp.PackageList(); Pkg.end() != true; Pkg = Grp.NextPkg(Pkg)) | |
212 | { | |
213 | if (pams(Pkg.Arch()) == false) | |
214 | continue; | |
215 | Pin *P = Pins + Pkg->ID; | |
216 | // the first specific stanza for a package is the ruler, | |
217 | // all others need to be ignored | |
218 | if (P->Type != pkgVersionMatch::None) | |
219 | P = &*Unmatched.insert(Unmatched.end(),PkgPin(Pkg.FullName())); | |
220 | P->Type = Type; | |
221 | P->Priority = Priority; | |
222 | P->Data = Data; | |
223 | matched = true; | |
7fd9c353 JAK |
224 | |
225 | // Find matching version(s) and copy the pin into it | |
226 | pkgVersionMatch Match(P->Data,P->Type); | |
258b9e51 | 227 | for (pkgCache::VerIterator Ver = Pkg.VersionList(); Ver.end() != true; ++Ver) |
7fd9c353 JAK |
228 | { |
229 | if (Match.VersionMatches(Ver)) { | |
230 | Pin *VP = VerPins + Ver->ID; | |
231 | if (VP->Type == pkgVersionMatch::None) | |
232 | *VP = *P; | |
233 | } | |
234 | } | |
9ed80cdd | 235 | } |
00c6e1a3 MV |
236 | } |
237 | ||
9ed80cdd | 238 | if (matched == false) |
4a6d2163 | 239 | { |
00c6e1a3 MV |
240 | PkgPin *P = &*Unmatched.insert(Unmatched.end(),PkgPin(Name)); |
241 | if (Arch.empty() == false) | |
242 | P->Pkg.append(":").append(Arch); | |
243 | P->Type = Type; | |
244 | P->Priority = Priority; | |
245 | P->Data = Data; | |
246 | return; | |
247 | } | |
b2e465d6 AL |
248 | } |
249 | /*}}}*/ | |
af87ab54 AL |
250 | // Policy::GetMatch - Get the matching version for a package pin /*{{{*/ |
251 | // --------------------------------------------------------------------- | |
252 | /* */ | |
9ee8287e | 253 | pkgCache::VerIterator pkgPolicy::GetMatch(pkgCache::PkgIterator const &Pkg) |
af87ab54 AL |
254 | { |
255 | const Pin &PPkg = Pins[Pkg->ID]; | |
9ee8287e DK |
256 | if (PPkg.Type == pkgVersionMatch::None) |
257 | return pkgCache::VerIterator(*Pkg.Cache()); | |
258 | ||
259 | pkgVersionMatch Match(PPkg.Data,PPkg.Type); | |
260 | return Match.Find(Pkg); | |
af87ab54 AL |
261 | } |
262 | /*}}}*/ | |
263 | // Policy::GetPriority - Get the priority of the package pin /*{{{*/ | |
264 | // --------------------------------------------------------------------- | |
265 | /* */ | |
a02db58f | 266 | APT_PURE signed short pkgPolicy::GetPriority(pkgCache::PkgIterator const &Pkg) |
af87ab54 AL |
267 | { |
268 | if (Pins[Pkg->ID].Type != pkgVersionMatch::None) | |
af87ab54 | 269 | return Pins[Pkg->ID].Priority; |
af87ab54 | 270 | return 0; |
6d38011b | 271 | } |
a0a66955 | 272 | APT_PURE signed short pkgPolicy::GetPriority(pkgCache::VerIterator const &Ver, bool ConsiderFiles) |
9fb02ab0 JAK |
273 | { |
274 | if (VerPins[Ver->ID].Type != pkgVersionMatch::None) | |
275 | return VerPins[Ver->ID].Priority; | |
a0a66955 | 276 | if (!ConsiderFiles) |
10b39ae6 | 277 | return 0; |
5f9386e5 | 278 | |
fd2e438b DK |
279 | // priorities are short ints, but we want to pick a value outside the valid range here |
280 | auto priority = std::numeric_limits<signed int>::min(); | |
5f9386e5 JAK |
281 | for (pkgCache::VerFileIterator file = Ver.FileList(); file.end() == false; file++) |
282 | { | |
3d5e93f7 JAK |
283 | /* If this is the status file, and the current version is not the |
284 | version in the status file (ie it is not installed, or somesuch) | |
285 | then it is not a candidate for installation, ever. This weeds | |
286 | out bogus entries that may be due to config-file states, or | |
287 | other. */ | |
69cea1ef DK |
288 | if (file.File().Flagged(pkgCache::Flag::NotSource) && Ver.ParentPkg().CurrentVer() != Ver) |
289 | priority = std::max(priority, static_cast<decltype(priority)>(-1)); | |
290 | else | |
fd2e438b | 291 | priority = std::max(priority, static_cast<decltype(priority)>(GetPriority(file.File()))); |
5f9386e5 JAK |
292 | } |
293 | ||
69cea1ef | 294 | return priority == std::numeric_limits<decltype(priority)>::min() ? 0 : priority; |
9fb02ab0 | 295 | } |
a02db58f | 296 | APT_PURE signed short pkgPolicy::GetPriority(pkgCache::PkgFileIterator const &File) |
6d38011b DK |
297 | { |
298 | return PFPriority[File->ID]; | |
af87ab54 AL |
299 | } |
300 | /*}}}*/ | |
ef1dff93 DK |
301 | // ReadPinDir - Load the pin files from this dir into a Policy /*{{{*/ |
302 | // --------------------------------------------------------------------- | |
6009e60d DK |
303 | /* This will load each pin file in the given dir into a Policy. If the |
304 | given dir is empty the dir set in Dir::Etc::PreferencesParts is used. | |
305 | Note also that this method will issue a warning if the dir does not | |
306 | exists but it will return true in this case! */ | |
e68ca100 JAK |
307 | bool ReadPinDir(pkgPolicy &Plcy,string Dir) |
308 | { | |
309 | if (Dir.empty() == true) | |
310 | Dir = _config->FindDir("Dir::Etc::PreferencesParts"); | |
311 | ||
448eaf8b | 312 | if (DirectoryExists(Dir) == false) |
6009e60d | 313 | { |
188a6fcf DK |
314 | if (Dir != "/dev/null") |
315 | _error->WarningE("DirectoryExists",_("Unable to read %s"),Dir.c_str()); | |
6009e60d DK |
316 | return true; |
317 | } | |
318 | ||
fdf9eef4 | 319 | _error->PushToStack(); |
b39c1859 | 320 | vector<string> const List = GetListOfFilesInDir(Dir, "pref", true, true); |
fdf9eef4 DK |
321 | bool const PendingErrors = _error->PendingError(); |
322 | _error->MergeWithStack(); | |
323 | if (PendingErrors) | |
324 | return false; | |
e68ca100 JAK |
325 | |
326 | // Read the files | |
f7f0d6c7 | 327 | for (vector<string>::const_iterator I = List.begin(); I != List.end(); ++I) |
e68ca100 JAK |
328 | if (ReadPinFile(Plcy, *I) == false) |
329 | return false; | |
330 | return true; | |
331 | } | |
81e9789b | 332 | /*}}}*/ |
b2e465d6 AL |
333 | // ReadPinFile - Load the pin file into a Policy /*{{{*/ |
334 | // --------------------------------------------------------------------- | |
335 | /* I'd like to see the preferences file store more than just pin information | |
336 | but right now that is the only stuff I have to store. Later there will | |
337 | have to be some kind of combined super parser to get the data into all | |
338 | the right classes.. */ | |
339 | bool ReadPinFile(pkgPolicy &Plcy,string File) | |
340 | { | |
341 | if (File.empty() == true) | |
342 | File = _config->FindFile("Dir::Etc::Preferences"); | |
343 | ||
36f1098a | 344 | if (RealFileExists(File) == false) |
b2e465d6 AL |
345 | return true; |
346 | ||
347 | FileFd Fd(File,FileFd::ReadOnly); | |
f6459e64 | 348 | pkgTagFile TF(&Fd, pkgTagFile::SUPPORT_COMMENTS); |
95278287 | 349 | if (Fd.IsOpen() == false || Fd.Failed()) |
b2e465d6 | 350 | return false; |
81460e32 | 351 | |
f6459e64 | 352 | pkgTagSection Tags; |
b2e465d6 AL |
353 | while (TF.Step(Tags) == true) |
354 | { | |
75ab11ae MV |
355 | // can happen when there are only comments in a record |
356 | if (Tags.Count() == 0) | |
357 | continue; | |
358 | ||
b2e465d6 AL |
359 | string Name = Tags.FindS("Package"); |
360 | if (Name.empty() == true) | |
e68ca100 | 361 | return _error->Error(_("Invalid record in the preferences file %s, no Package header"), File.c_str()); |
b2e465d6 AL |
362 | if (Name == "*") |
363 | Name = string(); | |
364 | ||
365 | const char *Start; | |
366 | const char *End; | |
367 | if (Tags.Find("Pin",Start,End) == false) | |
368 | continue; | |
369 | ||
370 | const char *Word = Start; | |
371 | for (; Word != End && isspace(*Word) == 0; Word++); | |
372 | ||
373 | // Parse the type.. | |
374 | pkgVersionMatch::MatchType Type; | |
375 | if (stringcasecmp(Start,Word,"version") == 0 && Name.empty() == false) | |
376 | Type = pkgVersionMatch::Version; | |
377 | else if (stringcasecmp(Start,Word,"release") == 0) | |
378 | Type = pkgVersionMatch::Release; | |
379 | else if (stringcasecmp(Start,Word,"origin") == 0) | |
380 | Type = pkgVersionMatch::Origin; | |
381 | else | |
382 | { | |
383 | _error->Warning(_("Did not understand pin type %s"),string(Start,Word).c_str()); | |
384 | continue; | |
385 | } | |
386 | for (; Word != End && isspace(*Word) != 0; Word++); | |
387 | ||
95278287 DK |
388 | _error->PushToStack(); |
389 | int const priority = Tags.FindI("Pin-Priority", 0); | |
390 | bool const newError = _error->PendingError(); | |
391 | _error->MergeWithStack(); | |
809aa216 JAK |
392 | if (priority < std::numeric_limits<short>::min() || |
393 | priority > std::numeric_limits<short>::max() || | |
95278287 | 394 | newError) { |
809aa216 JAK |
395 | return _error->Error(_("%s: Value %s is outside the range of valid pin priorities (%d to %d)"), |
396 | File.c_str(), Tags.FindS("Pin-Priority").c_str(), | |
397 | std::numeric_limits<short>::min(), | |
398 | std::numeric_limits<short>::max()); | |
399 | } | |
56298634 AL |
400 | if (priority == 0) |
401 | { | |
809aa216 | 402 | return _error->Error(_("No priority (or zero) specified for pin")); |
56298634 AL |
403 | } |
404 | ||
1c62ab24 MV |
405 | istringstream s(Name); |
406 | string pkg; | |
407 | while(!s.eof()) | |
408 | { | |
409 | s >> pkg; | |
410 | Plcy.CreatePin(Type, pkg, string(Word,End),priority); | |
411 | }; | |
b2e465d6 AL |
412 | } |
413 | ||
414 | Plcy.InitDefaults(); | |
415 | return true; | |
416 | } | |
417 | /*}}}*/ | |
c8a4ce6c | 418 | |
9fb02ab0 | 419 | pkgPolicy::~pkgPolicy() {delete [] PFPriority; delete [] Pins; delete [] VerPins; } |