]>
git.saurik.com Git - apt.git/blob - apt-pkg/policy.cc
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: policy.cc,v 1.10 2003/08/12 00:17:37 mdz Exp $
4 /* ######################################################################
6 Package Version Policy implementation
8 This is just a really simple wrapper around pkgVersionMatch with
9 some added goodies to manage the list of things..
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
20 0 -> 100 = NotAutomatic sources like experimental
21 -inf -> 0 = Never selected
23 ##################################################################### */
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/fileutl.h>
31 #include <apt-pkg/error.h>
32 #include <apt-pkg/sptr.h>
42 // Policy::Init - Startup and bind to a cache /*{{{*/
43 // ---------------------------------------------------------------------
44 /* Set the defaults for operation. The default mode with no loaded policy
45 file matches the V0 policy engine. */
46 pkgPolicy::pkgPolicy(pkgCache
*Owner
) : Pins(0), PFPriority(0), Cache(Owner
)
48 PFPriority
= new signed short[Owner
->Head().PackageFileCount
];
49 Pins
= new Pin
[Owner
->Head().PackageCount
];
51 for (unsigned long I
= 0; I
!= Owner
->Head().PackageCount
; I
++)
52 Pins
[I
].Type
= pkgVersionMatch::None
;
54 // The config file has a master override.
55 string DefRel
= _config
->Find("APT::Default-Release");
56 if (DefRel
.empty() == false)
57 CreatePin(pkgVersionMatch::Release
,"",DefRel
,990);
62 // Policy::InitDefaults - Compute the default selections /*{{{*/
63 // ---------------------------------------------------------------------
65 bool pkgPolicy::InitDefaults()
67 // Initialize the priorities based on the status of the package file
68 for (pkgCache::PkgFileIterator I
= Cache
->FileBegin(); I
!= Cache
->FileEnd(); I
++)
70 PFPriority
[I
->ID
] = 500;
71 if ((I
->Flags
& pkgCache::Flag::NotSource
) == pkgCache::Flag::NotSource
)
72 PFPriority
[I
->ID
] = 100;
74 if ((I
->Flags
& pkgCache::Flag::NotAutomatic
) == pkgCache::Flag::NotAutomatic
)
75 PFPriority
[I
->ID
] = 1;
78 // Apply the defaults..
79 SPtrArray
<bool> Fixed
= new bool[Cache
->HeaderP
->PackageFileCount
];
80 memset(Fixed
,0,sizeof(*Fixed
)*Cache
->HeaderP
->PackageFileCount
);
82 StatusOverride
= false;
83 for (vector
<Pin
>::const_iterator I
= Defaults
.begin(); I
!= Defaults
.end();
86 pkgVersionMatch
Match(I
->Data
,I
->Type
);
87 for (pkgCache::PkgFileIterator F
= Cache
->FileBegin(); F
!= Cache
->FileEnd(); F
++)
89 if (Match
.FileMatch(F
) == true && Fixed
[F
->ID
] == false)
91 if (I
->Priority
!= 0 && I
->Priority
> 0)
95 PFPriority
[F
->ID
] = I
->Priority
;
97 PFPriority
[F
->ID
] = Cur
;
99 if (PFPriority
[F
->ID
] > 1000)
100 StatusOverride
= true;
107 if (_config
->FindB("Debug::pkgPolicy",false) == true)
108 for (pkgCache::PkgFileIterator F
= Cache
->FileBegin(); F
!= Cache
->FileEnd(); F
++)
109 std::clog
<< "Prio of " << F
.FileName() << ' ' << PFPriority
[F
->ID
] << std::endl
;
114 // Policy::GetCandidateVer - Get the candidate install version /*{{{*/
115 // ---------------------------------------------------------------------
116 /* Evaluate the package pins and the default list to deteremine what the
118 pkgCache::VerIterator
pkgPolicy::GetCandidateVer(pkgCache::PkgIterator Pkg
)
120 // Look for a package pin and evaluate it.
121 signed Max
= GetPriority(Pkg
);
122 pkgCache::VerIterator Pref
= GetMatch(Pkg
);
124 // no package = no candidate version
125 if (Pkg
.end() == true)
128 // packages with a pin lower than 0 have no newer candidate than the current version
130 return Pkg
.CurrentVer();
132 /* Falling through to the default version.. Setting Max to zero
133 effectively excludes everything <= 0 which are the non-automatic
134 priorities.. The status file is given a prio of 100 which will exclude
135 not-automatic sources, except in a single shot not-installed mode.
136 The second pseduo-status file is at prio 1000, above which will permit
137 the user to force-downgrade things.
139 The user pin is subject to the same priority rules as default
140 selections. Thus there are two ways to create a pin - a pin that
141 tracks the default when the default is taken away, and a permanent
142 pin that stays at that setting.
144 for (pkgCache::VerIterator Ver
= Pkg
.VersionList(); Ver
.end() == false; Ver
++)
146 for (pkgCache::VerFileIterator VF
= Ver
.FileList(); VF
.end() == false; VF
++)
148 /* If this is the status file, and the current version is not the
149 version in the status file (ie it is not installed, or somesuch)
150 then it is not a candidate for installation, ever. This weeds
151 out bogus entries that may be due to config-file states, or
153 if ((VF
.File()->Flags
& pkgCache::Flag::NotSource
) == pkgCache::Flag::NotSource
&&
154 Pkg
.CurrentVer() != Ver
)
157 signed Prio
= PFPriority
[VF
.File()->ID
];
165 if (Pkg
.CurrentVer() == Ver
&& Max
< 1000)
167 /* Elevate our current selection (or the status file itself)
168 to the Pseudo-status priority. */
169 if (Pref
.end() == true)
173 // Fast path optimize.
174 if (StatusOverride
== false)
181 // Policy::CreatePin - Create an entry in the pin table.. /*{{{*/
182 // ---------------------------------------------------------------------
183 /* For performance we have 3 tables, the default table, the main cache
184 table (hashed to the cache). A blank package name indicates the pin
185 belongs to the default table. Order of insertion matters here, the
186 earlier defaults override later ones. */
187 void pkgPolicy::CreatePin(pkgVersionMatch::MatchType Type
,string Name
,
188 string Data
,signed short Priority
)
192 if (Name
.empty() == true)
193 P
= &*Defaults
.insert(Defaults
.end(),PkgPin());
196 // Get a spot to put the pin
197 pkgCache::PkgIterator Pkg
= Cache
->FindPkg(Name
);
198 if (Pkg
.end() == true)
200 // Check the unmatched table
201 for (vector
<PkgPin
>::iterator I
= Unmatched
.begin();
202 I
!= Unmatched
.end() && P
== 0; I
++)
207 P
= &*Unmatched
.insert(Unmatched
.end(),PkgPin());
217 P
->Priority
= Priority
;
221 // Policy::GetMatch - Get the matching version for a package pin /*{{{*/
222 // ---------------------------------------------------------------------
224 pkgCache::VerIterator
pkgPolicy::GetMatch(pkgCache::PkgIterator Pkg
)
226 const Pin
&PPkg
= Pins
[Pkg
->ID
];
227 if (PPkg
.Type
!= pkgVersionMatch::None
)
229 pkgVersionMatch
Match(PPkg
.Data
,PPkg
.Type
);
230 return Match
.Find(Pkg
);
232 return pkgCache::VerIterator(*Pkg
.Cache());
235 // Policy::GetPriority - Get the priority of the package pin /*{{{*/
236 // ---------------------------------------------------------------------
238 signed short pkgPolicy::GetPriority(pkgCache::PkgIterator
const &Pkg
)
240 if (Pins
[Pkg
->ID
].Type
!= pkgVersionMatch::None
)
242 // In this case 0 means default priority
243 if (Pins
[Pkg
->ID
].Priority
== 0)
245 return Pins
[Pkg
->ID
].Priority
;
251 // PreferenceSection class - Overriding the default TrimRecord method /*{{{*/
252 // ---------------------------------------------------------------------
253 /* The preference file is a user generated file so the parser should
254 therefore be a bit more friendly by allowing comments and new lines
255 all over the place rather than forcing a special format */
256 class PreferenceSection
: public pkgTagSection
258 void TrimRecord(bool BeforeRecord
, const char* &End
)
260 for (; Stop
< End
&& (Stop
[0] == '\n' || Stop
[0] == '\r' || Stop
[0] == '#'); Stop
++)
262 Stop
= (const char*) memchr(Stop
,'\n',End
-Stop
);
266 // ReadPinDir - Load the pin files from this dir into a Policy /*{{{*/
267 // ---------------------------------------------------------------------
268 /* This will load each pin file in the given dir into a Policy. If the
269 given dir is empty the dir set in Dir::Etc::PreferencesParts is used.
270 Note also that this method will issue a warning if the dir does not
271 exists but it will return true in this case! */
272 bool ReadPinDir(pkgPolicy
&Plcy
,string Dir
)
274 if (Dir
.empty() == true)
275 Dir
= _config
->FindDir("Dir::Etc::PreferencesParts");
277 if (FileExists(Dir
) == false)
279 _error
->WarningE("FileExists",_("Unable to read %s"),Dir
.c_str());
283 vector
<string
> const List
= GetListOfFilesInDir(Dir
, "pref", true, true);
286 for (vector
<string
>::const_iterator I
= List
.begin(); I
!= List
.end(); I
++)
287 if (ReadPinFile(Plcy
, *I
) == false)
292 // ReadPinFile - Load the pin file into a Policy /*{{{*/
293 // ---------------------------------------------------------------------
294 /* I'd like to see the preferences file store more than just pin information
295 but right now that is the only stuff I have to store. Later there will
296 have to be some kind of combined super parser to get the data into all
297 the right classes.. */
298 bool ReadPinFile(pkgPolicy
&Plcy
,string File
)
300 if (File
.empty() == true)
301 File
= _config
->FindFile("Dir::Etc::Preferences");
303 if (FileExists(File
) == false)
306 FileFd
Fd(File
,FileFd::ReadOnly
);
308 if (_error
->PendingError() == true)
311 PreferenceSection Tags
;
312 while (TF
.Step(Tags
) == true)
314 string Name
= Tags
.FindS("Package");
315 if (Name
.empty() == true)
316 return _error
->Error(_("Invalid record in the preferences file %s, no Package header"), File
.c_str());
322 if (Tags
.Find("Pin",Start
,End
) == false)
325 const char *Word
= Start
;
326 for (; Word
!= End
&& isspace(*Word
) == 0; Word
++);
329 pkgVersionMatch::MatchType Type
;
330 if (stringcasecmp(Start
,Word
,"version") == 0 && Name
.empty() == false)
331 Type
= pkgVersionMatch::Version
;
332 else if (stringcasecmp(Start
,Word
,"release") == 0)
333 Type
= pkgVersionMatch::Release
;
334 else if (stringcasecmp(Start
,Word
,"origin") == 0)
335 Type
= pkgVersionMatch::Origin
;
338 _error
->Warning(_("Did not understand pin type %s"),string(Start
,Word
).c_str());
341 for (; Word
!= End
&& isspace(*Word
) != 0; Word
++);
343 short int priority
= Tags
.FindI("Pin-Priority", 0);
346 _error
->Warning(_("No priority (or zero) specified for pin"));
350 istringstream
s(Name
);
355 Plcy
.CreatePin(Type
, pkg
, string(Word
,End
),priority
);