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