]> git.saurik.com Git - apt.git/blame - apt-pkg/policy.cc
Slovak translation updated
[apt.git] / apt-pkg / policy.cc
CommitLineData
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
42using 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. */
48pkgPolicy::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/* */
67bool 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. */
120pkgCache::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
9f5bf66a
DK
126 // no package = no candidate version
127 if (Pkg.end() == true)
128 return Pref;
129
130 // packages with a pin lower than 0 have no newer candidate than the current version
131 if (Max < 0)
132 return Pkg.CurrentVer();
133
b2e465d6
AL
134 /* Falling through to the default version.. Setting Max to zero
135 effectively excludes everything <= 0 which are the non-automatic
136 priorities.. The status file is given a prio of 100 which will exclude
137 not-automatic sources, except in a single shot not-installed mode.
138 The second pseduo-status file is at prio 1000, above which will permit
139 the user to force-downgrade things.
140
141 The user pin is subject to the same priority rules as default
142 selections. Thus there are two ways to create a pin - a pin that
143 tracks the default when the default is taken away, and a permanent
144 pin that stays at that setting.
145 */
146 for (pkgCache::VerIterator Ver = Pkg.VersionList(); Ver.end() == false; Ver++)
10639577 147 {
b2e465d6
AL
148 for (pkgCache::VerFileIterator VF = Ver.FileList(); VF.end() == false; VF++)
149 {
6aeda9fa
AL
150 /* If this is the status file, and the current version is not the
151 version in the status file (ie it is not installed, or somesuch)
152 then it is not a candidate for installation, ever. This weeds
153 out bogus entries that may be due to config-file states, or
154 other. */
155 if ((VF.File()->Flags & pkgCache::Flag::NotSource) == pkgCache::Flag::NotSource &&
156 Pkg.CurrentVer() != Ver)
157 continue;
158
b2e465d6
AL
159 signed Prio = PFPriority[VF.File()->ID];
160 if (Prio > Max)
161 {
162 Pref = Ver;
163 Max = Prio;
164 }
165 }
166
167 if (Pkg.CurrentVer() == Ver && Max < 1000)
168 {
169 /* Elevate our current selection (or the status file itself)
170 to the Pseudo-status priority. */
171 if (Pref.end() == true)
172 Pref = Ver;
173 Max = 1000;
174
175 // Fast path optimize.
176 if (StatusOverride == false)
177 break;
178 }
179 }
b2e465d6
AL
180 return Pref;
181}
182 /*}}}*/
183// Policy::CreatePin - Create an entry in the pin table.. /*{{{*/
184// ---------------------------------------------------------------------
185/* For performance we have 3 tables, the default table, the main cache
186 table (hashed to the cache). A blank package name indicates the pin
187 belongs to the default table. Order of insertion matters here, the
188 earlier defaults override later ones. */
189void pkgPolicy::CreatePin(pkgVersionMatch::MatchType Type,string Name,
190 string Data,signed short Priority)
191{
b2e465d6
AL
192 Pin *P = 0;
193
194 if (Name.empty() == true)
b8b7c37d 195 P = &*Defaults.insert(Defaults.end(),PkgPin());
b2e465d6
AL
196 else
197 {
198 // Get a spot to put the pin
d4887f3c 199 pkgCache::PkgIterator Pkg = Cache->FindPkg(Name);
b2e465d6
AL
200 if (Pkg.end() == true)
201 {
202 // Check the unmatched table
203 for (vector<PkgPin>::iterator I = Unmatched.begin();
204 I != Unmatched.end() && P == 0; I++)
205 if (I->Pkg == Name)
d4887f3c 206 P = &*I;
b2e465d6
AL
207
208 if (P == 0)
b8b7c37d 209 P = &*Unmatched.insert(Unmatched.end(),PkgPin());
b2e465d6
AL
210 }
211 else
212 {
213 P = Pins + Pkg->ID;
214 }
215 }
216
217 // Set..
218 P->Type = Type;
219 P->Priority = Priority;
220 P->Data = Data;
221}
222 /*}}}*/
af87ab54
AL
223// Policy::GetMatch - Get the matching version for a package pin /*{{{*/
224// ---------------------------------------------------------------------
225/* */
226pkgCache::VerIterator pkgPolicy::GetMatch(pkgCache::PkgIterator Pkg)
227{
228 const Pin &PPkg = Pins[Pkg->ID];
229 if (PPkg.Type != pkgVersionMatch::None)
230 {
231 pkgVersionMatch Match(PPkg.Data,PPkg.Type);
e7b470ee 232 return Match.Find(Pkg);
af87ab54
AL
233 }
234 return pkgCache::VerIterator(*Pkg.Cache());
235}
236 /*}}}*/
237// Policy::GetPriority - Get the priority of the package pin /*{{{*/
238// ---------------------------------------------------------------------
239/* */
240signed short pkgPolicy::GetPriority(pkgCache::PkgIterator const &Pkg)
241{
242 if (Pins[Pkg->ID].Type != pkgVersionMatch::None)
243 {
244 // In this case 0 means default priority
245 if (Pins[Pkg->ID].Priority == 0)
246 return 989;
247 return Pins[Pkg->ID].Priority;
248 }
249
250 return 0;
251}
252 /*}}}*/
81e9789b
MV
253// PreferenceSection class - Overriding the default TrimRecord method /*{{{*/
254// ---------------------------------------------------------------------
255/* The preference file is a user generated file so the parser should
256 therefore be a bit more friendly by allowing comments and new lines
257 all over the place rather than forcing a special format */
258class PreferenceSection : public pkgTagSection
259{
260 void TrimRecord(bool BeforeRecord, const char* &End)
261 {
262 for (; Stop < End && (Stop[0] == '\n' || Stop[0] == '\r' || Stop[0] == '#'); Stop++)
263 if (Stop[0] == '#')
264 Stop = (const char*) memchr(Stop,'\n',End-Stop);
265 }
266};
267 /*}}}*/
ef1dff93
DK
268// ReadPinDir - Load the pin files from this dir into a Policy /*{{{*/
269// ---------------------------------------------------------------------
6009e60d
DK
270/* This will load each pin file in the given dir into a Policy. If the
271 given dir is empty the dir set in Dir::Etc::PreferencesParts is used.
272 Note also that this method will issue a warning if the dir does not
273 exists but it will return true in this case! */
e68ca100
JAK
274bool ReadPinDir(pkgPolicy &Plcy,string Dir)
275{
276 if (Dir.empty() == true)
277 Dir = _config->FindDir("Dir::Etc::PreferencesParts");
278
6009e60d
DK
279 if (FileExists(Dir) == false)
280 {
281 _error->WarningE("FileExists",_("Unable to read %s"),Dir.c_str());
282 return true;
283 }
284
e68ca100
JAK
285 DIR *D = opendir(Dir.c_str());
286 if (D == 0)
287 return _error->Errno("opendir",_("Unable to read %s"),Dir.c_str());
288
289 vector<string> List;
ef1dff93 290
e68ca100
JAK
291 for (struct dirent *Ent = readdir(D); Ent != 0; Ent = readdir(D))
292 {
293 if (Ent->d_name[0] == '.')
294 continue;
ef1dff93 295
e68ca100
JAK
296 // Skip bad file names ala run-parts
297 const char *C = Ent->d_name;
298 for (; *C != 0; C++)
299 if (isalpha(*C) == 0 && isdigit(*C) == 0 && *C != '_' && *C != '-')
300 break;
301 if (*C != 0)
302 continue;
ef1dff93 303
e68ca100
JAK
304 // Make sure it is a file and not something else
305 string File = flCombine(Dir,Ent->d_name);
306 struct stat St;
307 if (stat(File.c_str(),&St) != 0 || S_ISREG(St.st_mode) == 0)
308 continue;
ef1dff93
DK
309
310 List.push_back(File);
311 }
e68ca100 312 closedir(D);
ef1dff93 313
e68ca100
JAK
314 sort(List.begin(),List.end());
315
316 // Read the files
317 for (vector<string>::const_iterator I = List.begin(); I != List.end(); I++)
318 if (ReadPinFile(Plcy, *I) == false)
319 return false;
320 return true;
321}
81e9789b 322 /*}}}*/
b2e465d6
AL
323// ReadPinFile - Load the pin file into a Policy /*{{{*/
324// ---------------------------------------------------------------------
325/* I'd like to see the preferences file store more than just pin information
326 but right now that is the only stuff I have to store. Later there will
327 have to be some kind of combined super parser to get the data into all
328 the right classes.. */
329bool ReadPinFile(pkgPolicy &Plcy,string File)
330{
331 if (File.empty() == true)
332 File = _config->FindFile("Dir::Etc::Preferences");
333
334 if (FileExists(File) == false)
335 return true;
336
337 FileFd Fd(File,FileFd::ReadOnly);
338 pkgTagFile TF(&Fd);
339 if (_error->PendingError() == true)
340 return false;
341
81e9789b 342 PreferenceSection Tags;
b2e465d6
AL
343 while (TF.Step(Tags) == true)
344 {
345 string Name = Tags.FindS("Package");
346 if (Name.empty() == true)
e68ca100 347 return _error->Error(_("Invalid record in the preferences file %s, no Package header"), File.c_str());
b2e465d6
AL
348 if (Name == "*")
349 Name = string();
350
351 const char *Start;
352 const char *End;
353 if (Tags.Find("Pin",Start,End) == false)
354 continue;
355
356 const char *Word = Start;
357 for (; Word != End && isspace(*Word) == 0; Word++);
358
359 // Parse the type..
360 pkgVersionMatch::MatchType Type;
361 if (stringcasecmp(Start,Word,"version") == 0 && Name.empty() == false)
362 Type = pkgVersionMatch::Version;
363 else if (stringcasecmp(Start,Word,"release") == 0)
364 Type = pkgVersionMatch::Release;
365 else if (stringcasecmp(Start,Word,"origin") == 0)
366 Type = pkgVersionMatch::Origin;
367 else
368 {
369 _error->Warning(_("Did not understand pin type %s"),string(Start,Word).c_str());
370 continue;
371 }
372 for (; Word != End && isspace(*Word) != 0; Word++);
373
56298634
AL
374 short int priority = Tags.FindI("Pin-Priority", 0);
375 if (priority == 0)
376 {
377 _error->Warning(_("No priority (or zero) specified for pin"));
378 continue;
379 }
380
1c62ab24
MV
381 istringstream s(Name);
382 string pkg;
383 while(!s.eof())
384 {
385 s >> pkg;
386 Plcy.CreatePin(Type, pkg, string(Word,End),priority);
387 };
b2e465d6
AL
388 }
389
390 Plcy.InitDefaults();
391 return true;
392}
393 /*}}}*/