]>
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 | |
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 /*{{{*/ | |
b2e465d6 AL |
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> | |
e7b470ee | 34 | |
e68ca100 JAK |
35 | #include <dirent.h> |
36 | #include <sys/stat.h> | |
37 | #include <algorithm> | |
e7b470ee | 38 | #include <iostream> |
1c62ab24 | 39 | #include <sstream> |
b2e465d6 AL |
40 | /*}}}*/ |
41 | ||
e7b470ee AL |
42 | using namespace std; |
43 | ||
b2e465d6 AL |
44 | // Policy::Init - Startup and bind to a cache /*{{{*/ |
45 | // --------------------------------------------------------------------- | |
46 | /* Set the defaults for operation. The default mode with no loaded policy | |
47 | file matches the V0 policy engine. */ | |
48 | pkgPolicy::pkgPolicy(pkgCache *Owner) : Pins(0), PFPriority(0), Cache(Owner) | |
49 | { | |
50 | PFPriority = new signed short[Owner->Head().PackageFileCount]; | |
51 | Pins = new Pin[Owner->Head().PackageCount]; | |
52 | ||
53 | for (unsigned long I = 0; I != Owner->Head().PackageCount; I++) | |
54 | Pins[I].Type = pkgVersionMatch::None; | |
55 | ||
56 | // The config file has a master override. | |
57 | string DefRel = _config->Find("APT::Default-Release"); | |
58 | if (DefRel.empty() == false) | |
59 | CreatePin(pkgVersionMatch::Release,"",DefRel,990); | |
60 | ||
61 | InitDefaults(); | |
62 | } | |
63 | /*}}}*/ | |
64 | // Policy::InitDefaults - Compute the default selections /*{{{*/ | |
65 | // --------------------------------------------------------------------- | |
66 | /* */ | |
67 | bool pkgPolicy::InitDefaults() | |
68 | { | |
69 | // Initialize the priorities based on the status of the package file | |
70 | for (pkgCache::PkgFileIterator I = Cache->FileBegin(); I != Cache->FileEnd(); I++) | |
71 | { | |
72 | PFPriority[I->ID] = 500; | |
73 | if ((I->Flags & pkgCache::Flag::NotSource) == pkgCache::Flag::NotSource) | |
74 | PFPriority[I->ID] = 100; | |
75 | else | |
76 | if ((I->Flags & pkgCache::Flag::NotAutomatic) == pkgCache::Flag::NotAutomatic) | |
77 | PFPriority[I->ID] = 1; | |
78 | } | |
79 | ||
80 | // Apply the defaults.. | |
20ebd488 | 81 | SPtrArray<bool> Fixed = new bool[Cache->HeaderP->PackageFileCount]; |
b2e465d6 AL |
82 | memset(Fixed,0,sizeof(*Fixed)*Cache->HeaderP->PackageFileCount); |
83 | signed Cur = 989; | |
84 | StatusOverride = false; | |
85 | for (vector<Pin>::const_iterator I = Defaults.begin(); I != Defaults.end(); | |
86 | I++, Cur--) | |
87 | { | |
88 | pkgVersionMatch Match(I->Data,I->Type); | |
89 | for (pkgCache::PkgFileIterator F = Cache->FileBegin(); F != Cache->FileEnd(); F++) | |
90 | { | |
b2e465d6 AL |
91 | if (Match.FileMatch(F) == true && Fixed[F->ID] == false) |
92 | { | |
93 | if (I->Priority != 0 && I->Priority > 0) | |
94 | Cur = I->Priority; | |
95 | ||
96 | if (I->Priority < 0) | |
97 | PFPriority[F->ID] = I->Priority; | |
98 | else | |
99 | PFPriority[F->ID] = Cur; | |
100 | ||
101 | if (PFPriority[F->ID] > 1000) | |
102 | StatusOverride = true; | |
103 | ||
104 | Fixed[F->ID] = true; | |
105 | } | |
106 | } | |
107 | } | |
108 | ||
109 | if (_config->FindB("Debug::pkgPolicy",false) == true) | |
110 | for (pkgCache::PkgFileIterator F = Cache->FileBegin(); F != Cache->FileEnd(); F++) | |
111 | cout << "Prio of " << F.FileName() << ' ' << PFPriority[F->ID] << endl; | |
112 | ||
113 | return true; | |
114 | } | |
115 | /*}}}*/ | |
116 | // Policy::GetCandidateVer - Get the candidate install version /*{{{*/ | |
117 | // --------------------------------------------------------------------- | |
118 | /* Evaluate the package pins and the default list to deteremine what the | |
119 | best package is. */ | |
120 | pkgCache::VerIterator pkgPolicy::GetCandidateVer(pkgCache::PkgIterator Pkg) | |
121 | { | |
b2e465d6 | 122 | // Look for a package pin and evaluate it. |
af87ab54 AL |
123 | signed Max = GetPriority(Pkg); |
124 | pkgCache::VerIterator Pref = GetMatch(Pkg); | |
e7b470ee | 125 | |
b2e465d6 AL |
126 | /* Falling through to the default version.. Setting Max to zero |
127 | effectively excludes everything <= 0 which are the non-automatic | |
128 | priorities.. The status file is given a prio of 100 which will exclude | |
129 | not-automatic sources, except in a single shot not-installed mode. | |
130 | The second pseduo-status file is at prio 1000, above which will permit | |
131 | the user to force-downgrade things. | |
132 | ||
133 | The user pin is subject to the same priority rules as default | |
134 | selections. Thus there are two ways to create a pin - a pin that | |
135 | tracks the default when the default is taken away, and a permanent | |
136 | pin that stays at that setting. | |
137 | */ | |
138 | for (pkgCache::VerIterator Ver = Pkg.VersionList(); Ver.end() == false; Ver++) | |
10639577 | 139 | { |
b2e465d6 AL |
140 | for (pkgCache::VerFileIterator VF = Ver.FileList(); VF.end() == false; VF++) |
141 | { | |
6aeda9fa AL |
142 | /* If this is the status file, and the current version is not the |
143 | version in the status file (ie it is not installed, or somesuch) | |
144 | then it is not a candidate for installation, ever. This weeds | |
145 | out bogus entries that may be due to config-file states, or | |
146 | other. */ | |
147 | if ((VF.File()->Flags & pkgCache::Flag::NotSource) == pkgCache::Flag::NotSource && | |
148 | Pkg.CurrentVer() != Ver) | |
149 | continue; | |
150 | ||
b2e465d6 AL |
151 | signed Prio = PFPriority[VF.File()->ID]; |
152 | if (Prio > Max) | |
153 | { | |
154 | Pref = Ver; | |
155 | Max = Prio; | |
156 | } | |
157 | } | |
158 | ||
159 | if (Pkg.CurrentVer() == Ver && Max < 1000) | |
160 | { | |
161 | /* Elevate our current selection (or the status file itself) | |
162 | to the Pseudo-status priority. */ | |
163 | if (Pref.end() == true) | |
164 | Pref = Ver; | |
165 | Max = 1000; | |
166 | ||
167 | // Fast path optimize. | |
168 | if (StatusOverride == false) | |
169 | break; | |
170 | } | |
171 | } | |
b2e465d6 AL |
172 | return Pref; |
173 | } | |
174 | /*}}}*/ | |
175 | // Policy::CreatePin - Create an entry in the pin table.. /*{{{*/ | |
176 | // --------------------------------------------------------------------- | |
177 | /* For performance we have 3 tables, the default table, the main cache | |
178 | table (hashed to the cache). A blank package name indicates the pin | |
179 | belongs to the default table. Order of insertion matters here, the | |
180 | earlier defaults override later ones. */ | |
181 | void pkgPolicy::CreatePin(pkgVersionMatch::MatchType Type,string Name, | |
182 | string Data,signed short Priority) | |
183 | { | |
b2e465d6 AL |
184 | Pin *P = 0; |
185 | ||
186 | if (Name.empty() == true) | |
b8b7c37d | 187 | P = &*Defaults.insert(Defaults.end(),PkgPin()); |
b2e465d6 AL |
188 | else |
189 | { | |
190 | // Get a spot to put the pin | |
d4887f3c | 191 | pkgCache::PkgIterator Pkg = Cache->FindPkg(Name); |
b2e465d6 AL |
192 | if (Pkg.end() == true) |
193 | { | |
194 | // Check the unmatched table | |
195 | for (vector<PkgPin>::iterator I = Unmatched.begin(); | |
196 | I != Unmatched.end() && P == 0; I++) | |
197 | if (I->Pkg == Name) | |
d4887f3c | 198 | P = &*I; |
b2e465d6 AL |
199 | |
200 | if (P == 0) | |
b8b7c37d | 201 | P = &*Unmatched.insert(Unmatched.end(),PkgPin()); |
b2e465d6 AL |
202 | } |
203 | else | |
204 | { | |
205 | P = Pins + Pkg->ID; | |
206 | } | |
207 | } | |
208 | ||
209 | // Set.. | |
210 | P->Type = Type; | |
211 | P->Priority = Priority; | |
212 | P->Data = Data; | |
213 | } | |
214 | /*}}}*/ | |
af87ab54 AL |
215 | // Policy::GetMatch - Get the matching version for a package pin /*{{{*/ |
216 | // --------------------------------------------------------------------- | |
217 | /* */ | |
218 | pkgCache::VerIterator pkgPolicy::GetMatch(pkgCache::PkgIterator Pkg) | |
219 | { | |
220 | const Pin &PPkg = Pins[Pkg->ID]; | |
221 | if (PPkg.Type != pkgVersionMatch::None) | |
222 | { | |
223 | pkgVersionMatch Match(PPkg.Data,PPkg.Type); | |
e7b470ee | 224 | return Match.Find(Pkg); |
af87ab54 AL |
225 | } |
226 | return pkgCache::VerIterator(*Pkg.Cache()); | |
227 | } | |
228 | /*}}}*/ | |
229 | // Policy::GetPriority - Get the priority of the package pin /*{{{*/ | |
230 | // --------------------------------------------------------------------- | |
231 | /* */ | |
232 | signed short pkgPolicy::GetPriority(pkgCache::PkgIterator const &Pkg) | |
233 | { | |
234 | if (Pins[Pkg->ID].Type != pkgVersionMatch::None) | |
235 | { | |
236 | // In this case 0 means default priority | |
237 | if (Pins[Pkg->ID].Priority == 0) | |
238 | return 989; | |
239 | return Pins[Pkg->ID].Priority; | |
240 | } | |
241 | ||
242 | return 0; | |
243 | } | |
244 | /*}}}*/ | |
81e9789b MV |
245 | // PreferenceSection class - Overriding the default TrimRecord method /*{{{*/ |
246 | // --------------------------------------------------------------------- | |
247 | /* The preference file is a user generated file so the parser should | |
248 | therefore be a bit more friendly by allowing comments and new lines | |
249 | all over the place rather than forcing a special format */ | |
250 | class PreferenceSection : public pkgTagSection | |
251 | { | |
252 | void TrimRecord(bool BeforeRecord, const char* &End) | |
253 | { | |
254 | for (; Stop < End && (Stop[0] == '\n' || Stop[0] == '\r' || Stop[0] == '#'); Stop++) | |
255 | if (Stop[0] == '#') | |
256 | Stop = (const char*) memchr(Stop,'\n',End-Stop); | |
257 | } | |
258 | }; | |
e68ca100 JAK |
259 | |
260 | ||
261 | bool ReadPinDir(pkgPolicy &Plcy,string Dir) | |
262 | { | |
263 | if (Dir.empty() == true) | |
264 | Dir = _config->FindDir("Dir::Etc::PreferencesParts"); | |
265 | ||
266 | DIR *D = opendir(Dir.c_str()); | |
267 | if (D == 0) | |
268 | return _error->Errno("opendir",_("Unable to read %s"),Dir.c_str()); | |
269 | ||
270 | vector<string> List; | |
271 | ||
272 | for (struct dirent *Ent = readdir(D); Ent != 0; Ent = readdir(D)) | |
273 | { | |
274 | if (Ent->d_name[0] == '.') | |
275 | continue; | |
276 | ||
277 | // Skip bad file names ala run-parts | |
278 | const char *C = Ent->d_name; | |
279 | for (; *C != 0; C++) | |
280 | if (isalpha(*C) == 0 && isdigit(*C) == 0 && *C != '_' && *C != '-') | |
281 | break; | |
282 | if (*C != 0) | |
283 | continue; | |
284 | ||
285 | // Make sure it is a file and not something else | |
286 | string File = flCombine(Dir,Ent->d_name); | |
287 | struct stat St; | |
288 | if (stat(File.c_str(),&St) != 0 || S_ISREG(St.st_mode) == 0) | |
289 | continue; | |
290 | ||
291 | List.push_back(File); | |
292 | } | |
293 | closedir(D); | |
294 | ||
295 | sort(List.begin(),List.end()); | |
296 | ||
297 | // Read the files | |
298 | for (vector<string>::const_iterator I = List.begin(); I != List.end(); I++) | |
299 | if (ReadPinFile(Plcy, *I) == false) | |
300 | return false; | |
301 | return true; | |
302 | } | |
303 | ||
81e9789b | 304 | /*}}}*/ |
b2e465d6 AL |
305 | // ReadPinFile - Load the pin file into a Policy /*{{{*/ |
306 | // --------------------------------------------------------------------- | |
307 | /* I'd like to see the preferences file store more than just pin information | |
308 | but right now that is the only stuff I have to store. Later there will | |
309 | have to be some kind of combined super parser to get the data into all | |
310 | the right classes.. */ | |
311 | bool ReadPinFile(pkgPolicy &Plcy,string File) | |
312 | { | |
313 | if (File.empty() == true) | |
314 | File = _config->FindFile("Dir::Etc::Preferences"); | |
315 | ||
316 | if (FileExists(File) == false) | |
317 | return true; | |
318 | ||
319 | FileFd Fd(File,FileFd::ReadOnly); | |
320 | pkgTagFile TF(&Fd); | |
321 | if (_error->PendingError() == true) | |
322 | return false; | |
323 | ||
81e9789b | 324 | PreferenceSection Tags; |
b2e465d6 AL |
325 | while (TF.Step(Tags) == true) |
326 | { | |
327 | string Name = Tags.FindS("Package"); | |
328 | if (Name.empty() == true) | |
e68ca100 | 329 | return _error->Error(_("Invalid record in the preferences file %s, no Package header"), File.c_str()); |
b2e465d6 AL |
330 | if (Name == "*") |
331 | Name = string(); | |
332 | ||
333 | const char *Start; | |
334 | const char *End; | |
335 | if (Tags.Find("Pin",Start,End) == false) | |
336 | continue; | |
337 | ||
338 | const char *Word = Start; | |
339 | for (; Word != End && isspace(*Word) == 0; Word++); | |
340 | ||
341 | // Parse the type.. | |
342 | pkgVersionMatch::MatchType Type; | |
343 | if (stringcasecmp(Start,Word,"version") == 0 && Name.empty() == false) | |
344 | Type = pkgVersionMatch::Version; | |
345 | else if (stringcasecmp(Start,Word,"release") == 0) | |
346 | Type = pkgVersionMatch::Release; | |
347 | else if (stringcasecmp(Start,Word,"origin") == 0) | |
348 | Type = pkgVersionMatch::Origin; | |
349 | else | |
350 | { | |
351 | _error->Warning(_("Did not understand pin type %s"),string(Start,Word).c_str()); | |
352 | continue; | |
353 | } | |
354 | for (; Word != End && isspace(*Word) != 0; Word++); | |
355 | ||
56298634 AL |
356 | short int priority = Tags.FindI("Pin-Priority", 0); |
357 | if (priority == 0) | |
358 | { | |
359 | _error->Warning(_("No priority (or zero) specified for pin")); | |
360 | continue; | |
361 | } | |
362 | ||
1c62ab24 MV |
363 | istringstream s(Name); |
364 | string pkg; | |
365 | while(!s.eof()) | |
366 | { | |
367 | s >> pkg; | |
368 | Plcy.CreatePin(Type, pkg, string(Word,End),priority); | |
369 | }; | |
b2e465d6 AL |
370 | } |
371 | ||
372 | Plcy.InitDefaults(); | |
373 | return true; | |
374 | } | |
375 | /*}}}*/ |