]>
Commit | Line | Data |
---|---|---|
b2e465d6 AL |
1 | // -*- mode: cpp; mode: fold -*- |
2 | // Description /*{{{*/ | |
af87ab54 | 3 | // $Id: policy.cc,v 1.6 2001/04/29 05:13:51 jgg 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 /*{{{*/ | |
26 | #ifdef __GNUG__ | |
27 | #pragma implementation "apt-pkg/policy.h" | |
28 | #endif | |
29 | #include <apt-pkg/policy.h> | |
30 | #include <apt-pkg/configuration.h> | |
31 | #include <apt-pkg/tagfile.h> | |
32 | #include <apt-pkg/strutl.h> | |
33 | #include <apt-pkg/error.h> | |
34 | #include <apt-pkg/sptr.h> | |
35 | ||
36 | #include <apti18n.h> | |
37 | /*}}}*/ | |
38 | ||
39 | // Policy::Init - Startup and bind to a cache /*{{{*/ | |
40 | // --------------------------------------------------------------------- | |
41 | /* Set the defaults for operation. The default mode with no loaded policy | |
42 | file matches the V0 policy engine. */ | |
43 | pkgPolicy::pkgPolicy(pkgCache *Owner) : Pins(0), PFPriority(0), Cache(Owner) | |
44 | { | |
45 | PFPriority = new signed short[Owner->Head().PackageFileCount]; | |
46 | Pins = new Pin[Owner->Head().PackageCount]; | |
47 | ||
48 | for (unsigned long I = 0; I != Owner->Head().PackageCount; I++) | |
49 | Pins[I].Type = pkgVersionMatch::None; | |
50 | ||
51 | // The config file has a master override. | |
52 | string DefRel = _config->Find("APT::Default-Release"); | |
53 | if (DefRel.empty() == false) | |
54 | CreatePin(pkgVersionMatch::Release,"",DefRel,990); | |
55 | ||
56 | InitDefaults(); | |
57 | } | |
58 | /*}}}*/ | |
59 | // Policy::InitDefaults - Compute the default selections /*{{{*/ | |
60 | // --------------------------------------------------------------------- | |
61 | /* */ | |
62 | bool pkgPolicy::InitDefaults() | |
63 | { | |
64 | // Initialize the priorities based on the status of the package file | |
65 | for (pkgCache::PkgFileIterator I = Cache->FileBegin(); I != Cache->FileEnd(); I++) | |
66 | { | |
67 | PFPriority[I->ID] = 500; | |
68 | if ((I->Flags & pkgCache::Flag::NotSource) == pkgCache::Flag::NotSource) | |
69 | PFPriority[I->ID] = 100; | |
70 | else | |
71 | if ((I->Flags & pkgCache::Flag::NotAutomatic) == pkgCache::Flag::NotAutomatic) | |
72 | PFPriority[I->ID] = 1; | |
73 | } | |
74 | ||
75 | // Apply the defaults.. | |
20ebd488 | 76 | SPtrArray<bool> Fixed = new bool[Cache->HeaderP->PackageFileCount]; |
b2e465d6 AL |
77 | memset(Fixed,0,sizeof(*Fixed)*Cache->HeaderP->PackageFileCount); |
78 | signed Cur = 989; | |
79 | StatusOverride = false; | |
80 | for (vector<Pin>::const_iterator I = Defaults.begin(); I != Defaults.end(); | |
81 | I++, Cur--) | |
82 | { | |
83 | pkgVersionMatch Match(I->Data,I->Type); | |
84 | for (pkgCache::PkgFileIterator F = Cache->FileBegin(); F != Cache->FileEnd(); F++) | |
85 | { | |
b2e465d6 AL |
86 | if (Match.FileMatch(F) == true && Fixed[F->ID] == false) |
87 | { | |
88 | if (I->Priority != 0 && I->Priority > 0) | |
89 | Cur = I->Priority; | |
90 | ||
91 | if (I->Priority < 0) | |
92 | PFPriority[F->ID] = I->Priority; | |
93 | else | |
94 | PFPriority[F->ID] = Cur; | |
95 | ||
96 | if (PFPriority[F->ID] > 1000) | |
97 | StatusOverride = true; | |
98 | ||
99 | Fixed[F->ID] = true; | |
100 | } | |
101 | } | |
102 | } | |
103 | ||
104 | if (_config->FindB("Debug::pkgPolicy",false) == true) | |
105 | for (pkgCache::PkgFileIterator F = Cache->FileBegin(); F != Cache->FileEnd(); F++) | |
106 | cout << "Prio of " << F.FileName() << ' ' << PFPriority[F->ID] << endl; | |
107 | ||
108 | return true; | |
109 | } | |
110 | /*}}}*/ | |
111 | // Policy::GetCandidateVer - Get the candidate install version /*{{{*/ | |
112 | // --------------------------------------------------------------------- | |
113 | /* Evaluate the package pins and the default list to deteremine what the | |
114 | best package is. */ | |
115 | pkgCache::VerIterator pkgPolicy::GetCandidateVer(pkgCache::PkgIterator Pkg) | |
116 | { | |
b2e465d6 | 117 | // Look for a package pin and evaluate it. |
af87ab54 AL |
118 | signed Max = GetPriority(Pkg); |
119 | pkgCache::VerIterator Pref = GetMatch(Pkg); | |
b2e465d6 AL |
120 | |
121 | /* Falling through to the default version.. Setting Max to zero | |
122 | effectively excludes everything <= 0 which are the non-automatic | |
123 | priorities.. The status file is given a prio of 100 which will exclude | |
124 | not-automatic sources, except in a single shot not-installed mode. | |
125 | The second pseduo-status file is at prio 1000, above which will permit | |
126 | the user to force-downgrade things. | |
127 | ||
128 | The user pin is subject to the same priority rules as default | |
129 | selections. Thus there are two ways to create a pin - a pin that | |
130 | tracks the default when the default is taken away, and a permanent | |
131 | pin that stays at that setting. | |
132 | */ | |
133 | for (pkgCache::VerIterator Ver = Pkg.VersionList(); Ver.end() == false; Ver++) | |
10639577 | 134 | { |
b2e465d6 AL |
135 | for (pkgCache::VerFileIterator VF = Ver.FileList(); VF.end() == false; VF++) |
136 | { | |
6aeda9fa AL |
137 | /* If this is the status file, and the current version is not the |
138 | version in the status file (ie it is not installed, or somesuch) | |
139 | then it is not a candidate for installation, ever. This weeds | |
140 | out bogus entries that may be due to config-file states, or | |
141 | other. */ | |
142 | if ((VF.File()->Flags & pkgCache::Flag::NotSource) == pkgCache::Flag::NotSource && | |
143 | Pkg.CurrentVer() != Ver) | |
144 | continue; | |
145 | ||
b2e465d6 AL |
146 | signed Prio = PFPriority[VF.File()->ID]; |
147 | if (Prio > Max) | |
148 | { | |
149 | Pref = Ver; | |
150 | Max = Prio; | |
151 | } | |
152 | } | |
153 | ||
154 | if (Pkg.CurrentVer() == Ver && Max < 1000) | |
155 | { | |
156 | /* Elevate our current selection (or the status file itself) | |
157 | to the Pseudo-status priority. */ | |
158 | if (Pref.end() == true) | |
159 | Pref = Ver; | |
160 | Max = 1000; | |
161 | ||
162 | // Fast path optimize. | |
163 | if (StatusOverride == false) | |
164 | break; | |
165 | } | |
166 | } | |
167 | ||
168 | return Pref; | |
169 | } | |
170 | /*}}}*/ | |
171 | // Policy::CreatePin - Create an entry in the pin table.. /*{{{*/ | |
172 | // --------------------------------------------------------------------- | |
173 | /* For performance we have 3 tables, the default table, the main cache | |
174 | table (hashed to the cache). A blank package name indicates the pin | |
175 | belongs to the default table. Order of insertion matters here, the | |
176 | earlier defaults override later ones. */ | |
177 | void pkgPolicy::CreatePin(pkgVersionMatch::MatchType Type,string Name, | |
178 | string Data,signed short Priority) | |
179 | { | |
180 | pkgCache::PkgIterator Pkg = Cache->FindPkg(Name); | |
181 | Pin *P = 0; | |
182 | ||
183 | if (Name.empty() == true) | |
184 | P = Defaults.insert(Defaults.end()); | |
185 | else | |
186 | { | |
187 | // Get a spot to put the pin | |
188 | if (Pkg.end() == true) | |
189 | { | |
190 | // Check the unmatched table | |
191 | for (vector<PkgPin>::iterator I = Unmatched.begin(); | |
192 | I != Unmatched.end() && P == 0; I++) | |
193 | if (I->Pkg == Name) | |
194 | P = I; | |
195 | ||
196 | if (P == 0) | |
197 | P = Unmatched.insert(Unmatched.end()); | |
198 | } | |
199 | else | |
200 | { | |
201 | P = Pins + Pkg->ID; | |
202 | } | |
203 | } | |
204 | ||
205 | // Set.. | |
206 | P->Type = Type; | |
207 | P->Priority = Priority; | |
208 | P->Data = Data; | |
209 | } | |
210 | /*}}}*/ | |
af87ab54 AL |
211 | // Policy::GetMatch - Get the matching version for a package pin /*{{{*/ |
212 | // --------------------------------------------------------------------- | |
213 | /* */ | |
214 | pkgCache::VerIterator pkgPolicy::GetMatch(pkgCache::PkgIterator Pkg) | |
215 | { | |
216 | const Pin &PPkg = Pins[Pkg->ID]; | |
217 | if (PPkg.Type != pkgVersionMatch::None) | |
218 | { | |
219 | pkgVersionMatch Match(PPkg.Data,PPkg.Type); | |
220 | return Match.Find(Pkg); | |
221 | } | |
222 | return pkgCache::VerIterator(*Pkg.Cache()); | |
223 | } | |
224 | /*}}}*/ | |
225 | // Policy::GetPriority - Get the priority of the package pin /*{{{*/ | |
226 | // --------------------------------------------------------------------- | |
227 | /* */ | |
228 | signed short pkgPolicy::GetPriority(pkgCache::PkgIterator const &Pkg) | |
229 | { | |
230 | if (Pins[Pkg->ID].Type != pkgVersionMatch::None) | |
231 | { | |
232 | // In this case 0 means default priority | |
233 | if (Pins[Pkg->ID].Priority == 0) | |
234 | return 989; | |
235 | return Pins[Pkg->ID].Priority; | |
236 | } | |
237 | ||
238 | return 0; | |
239 | } | |
240 | /*}}}*/ | |
b2e465d6 AL |
241 | |
242 | // ReadPinFile - Load the pin file into a Policy /*{{{*/ | |
243 | // --------------------------------------------------------------------- | |
244 | /* I'd like to see the preferences file store more than just pin information | |
245 | but right now that is the only stuff I have to store. Later there will | |
246 | have to be some kind of combined super parser to get the data into all | |
247 | the right classes.. */ | |
248 | bool ReadPinFile(pkgPolicy &Plcy,string File) | |
249 | { | |
250 | if (File.empty() == true) | |
251 | File = _config->FindFile("Dir::Etc::Preferences"); | |
252 | ||
253 | if (FileExists(File) == false) | |
254 | return true; | |
255 | ||
256 | FileFd Fd(File,FileFd::ReadOnly); | |
257 | pkgTagFile TF(&Fd); | |
258 | if (_error->PendingError() == true) | |
259 | return false; | |
260 | ||
261 | pkgTagSection Tags; | |
262 | while (TF.Step(Tags) == true) | |
263 | { | |
264 | string Name = Tags.FindS("Package"); | |
265 | if (Name.empty() == true) | |
266 | return _error->Error(_("Invalid record in the preferences file, no Package header")); | |
267 | if (Name == "*") | |
268 | Name = string(); | |
269 | ||
270 | const char *Start; | |
271 | const char *End; | |
272 | if (Tags.Find("Pin",Start,End) == false) | |
273 | continue; | |
274 | ||
275 | const char *Word = Start; | |
276 | for (; Word != End && isspace(*Word) == 0; Word++); | |
277 | ||
278 | // Parse the type.. | |
279 | pkgVersionMatch::MatchType Type; | |
280 | if (stringcasecmp(Start,Word,"version") == 0 && Name.empty() == false) | |
281 | Type = pkgVersionMatch::Version; | |
282 | else if (stringcasecmp(Start,Word,"release") == 0) | |
283 | Type = pkgVersionMatch::Release; | |
284 | else if (stringcasecmp(Start,Word,"origin") == 0) | |
285 | Type = pkgVersionMatch::Origin; | |
286 | else | |
287 | { | |
288 | _error->Warning(_("Did not understand pin type %s"),string(Start,Word).c_str()); | |
289 | continue; | |
290 | } | |
291 | for (; Word != End && isspace(*Word) != 0; Word++); | |
292 | ||
293 | Plcy.CreatePin(Type,Name,string(Word,End), | |
294 | Tags.FindI("Pin-Priority")); | |
295 | } | |
296 | ||
297 | Plcy.InitDefaults(); | |
298 | return true; | |
299 | } | |
300 | /*}}}*/ |