]>
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
19 100 = The status file and ButAutomaticUpgrades sources
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;
73 else if ((I
->Flags
& pkgCache::Flag::ButAutomaticUpgrades
) == pkgCache::Flag::ButAutomaticUpgrades
)
74 PFPriority
[I
->ID
] = 100;
75 else if ((I
->Flags
& pkgCache::Flag::NotAutomatic
) == pkgCache::Flag::NotAutomatic
)
76 PFPriority
[I
->ID
] = 1;
79 // Apply the defaults..
80 SPtrArray
<bool> Fixed
= new bool[Cache
->HeaderP
->PackageFileCount
];
81 memset(Fixed
,0,sizeof(*Fixed
)*Cache
->HeaderP
->PackageFileCount
);
83 StatusOverride
= false;
84 for (vector
<Pin
>::const_iterator I
= Defaults
.begin(); I
!= Defaults
.end();
87 pkgVersionMatch
Match(I
->Data
,I
->Type
);
88 for (pkgCache::PkgFileIterator F
= Cache
->FileBegin(); F
!= Cache
->FileEnd(); F
++)
90 if (Match
.FileMatch(F
) == true && Fixed
[F
->ID
] == false)
92 if (I
->Priority
!= 0 && I
->Priority
> 0)
96 PFPriority
[F
->ID
] = I
->Priority
;
98 PFPriority
[F
->ID
] = Cur
;
100 if (PFPriority
[F
->ID
] > 1000)
101 StatusOverride
= true;
108 if (_config
->FindB("Debug::pkgPolicy",false) == true)
109 for (pkgCache::PkgFileIterator F
= Cache
->FileBegin(); F
!= Cache
->FileEnd(); F
++)
110 std::clog
<< "Prio of " << F
.FileName() << ' ' << PFPriority
[F
->ID
] << std::endl
;
115 // Policy::GetCandidateVer - Get the candidate install version /*{{{*/
116 // ---------------------------------------------------------------------
117 /* Evaluate the package pins and the default list to deteremine what the
119 pkgCache::VerIterator
pkgPolicy::GetCandidateVer(pkgCache::PkgIterator
const &Pkg
)
121 // Look for a package pin and evaluate it.
122 signed Max
= GetPriority(Pkg
);
123 pkgCache::VerIterator Pref
= GetMatch(Pkg
);
125 // Alternatives in case we can not find our package pin (Bug#512318).
127 pkgCache::VerIterator PrefAlt
;
129 // no package = no candidate version
130 if (Pkg
.end() == true)
133 // packages with a pin lower than 0 have no newer candidate than the current version
135 return Pkg
.CurrentVer();
137 /* Falling through to the default version.. Setting Max to zero
138 effectively excludes everything <= 0 which are the non-automatic
139 priorities.. The status file is given a prio of 100 which will exclude
140 not-automatic sources, except in a single shot not-installed mode.
141 The second pseduo-status file is at prio 1000, above which will permit
142 the user to force-downgrade things.
144 The user pin is subject to the same priority rules as default
145 selections. Thus there are two ways to create a pin - a pin that
146 tracks the default when the default is taken away, and a permanent
147 pin that stays at that setting.
149 for (pkgCache::VerIterator Ver
= Pkg
.VersionList(); Ver
.end() == false; Ver
++)
151 /* Lets see if this version is the installed version */
152 bool instVer
= (Pkg
.CurrentVer() == Ver
);
153 if (Ver
.Pseudo() == true && instVer
== false)
155 pkgCache::PkgIterator
const allPkg
= Ver
.ParentPkg().Group().FindPkg("all");
156 if (allPkg
->CurrentVer
!= 0 && allPkg
.CurrentVer()->Hash
== Ver
->Hash
&&
157 strcmp(allPkg
.CurVersion(), Ver
.VerStr()) == 0)
161 for (pkgCache::VerFileIterator VF
= Ver
.FileList(); VF
.end() == false; VF
++)
163 /* If this is the status file, and the current version is not the
164 version in the status file (ie it is not installed, or somesuch)
165 then it is not a candidate for installation, ever. This weeds
166 out bogus entries that may be due to config-file states, or
168 if ((VF
.File()->Flags
& pkgCache::Flag::NotSource
) == pkgCache::Flag::NotSource
&&
172 signed Prio
= PFPriority
[VF
.File()->ID
];
185 if (instVer
== true && Max
< 1000)
187 /* Elevate our current selection (or the status file itself)
188 to the Pseudo-status priority. */
189 if (Pref
.end() == true)
193 // Fast path optimize.
194 if (StatusOverride
== false)
198 // If we do not find our candidate, use the one with the highest pin.
199 // This means that if there is a version available with pin > 0; there
200 // will always be a candidate (Closes: #512318)
201 if (!Pref
.IsGood() && MaxAlt
> 0)
207 // Policy::CreatePin - Create an entry in the pin table.. /*{{{*/
208 // ---------------------------------------------------------------------
209 /* For performance we have 3 tables, the default table, the main cache
210 table (hashed to the cache). A blank package name indicates the pin
211 belongs to the default table. Order of insertion matters here, the
212 earlier defaults override later ones. */
213 void pkgPolicy::CreatePin(pkgVersionMatch::MatchType Type
,string Name
,
214 string Data
,signed short Priority
)
216 if (Name
.empty() == true)
218 Pin
*P
= &*Defaults
.insert(Defaults
.end(),PkgPin());
220 P
->Priority
= Priority
;
225 // Get a spot to put the pin
226 pkgCache::GrpIterator Grp
= Cache
->FindGrp(Name
);
227 for (pkgCache::PkgIterator Pkg
= Grp
.FindPkg("any");
228 Pkg
.end() != true; Pkg
= Grp
.NextPkg(Pkg
))
231 if (Pkg
.end() == false)
235 // Check the unmatched table
236 for (vector
<PkgPin
>::iterator I
= Unmatched
.begin();
237 I
!= Unmatched
.end() && P
== 0; I
++)
242 P
= &*Unmatched
.insert(Unmatched
.end(),PkgPin());
245 P
->Priority
= Priority
;
250 // Policy::GetMatch - Get the matching version for a package pin /*{{{*/
251 // ---------------------------------------------------------------------
253 pkgCache::VerIterator
pkgPolicy::GetMatch(pkgCache::PkgIterator
const &Pkg
)
255 const Pin
&PPkg
= Pins
[Pkg
->ID
];
256 if (PPkg
.Type
== pkgVersionMatch::None
)
257 return pkgCache::VerIterator(*Pkg
.Cache());
259 pkgVersionMatch
Match(PPkg
.Data
,PPkg
.Type
);
260 return Match
.Find(Pkg
);
263 // Policy::GetPriority - Get the priority of the package pin /*{{{*/
264 // ---------------------------------------------------------------------
266 signed short pkgPolicy::GetPriority(pkgCache::PkgIterator
const &Pkg
)
268 if (Pins
[Pkg
->ID
].Type
!= pkgVersionMatch::None
)
270 // In this case 0 means default priority
271 if (Pins
[Pkg
->ID
].Priority
== 0)
273 return Pins
[Pkg
->ID
].Priority
;
279 // PreferenceSection class - Overriding the default TrimRecord method /*{{{*/
280 // ---------------------------------------------------------------------
281 /* The preference file is a user generated file so the parser should
282 therefore be a bit more friendly by allowing comments and new lines
283 all over the place rather than forcing a special format */
284 class PreferenceSection
: public pkgTagSection
286 void TrimRecord(bool BeforeRecord
, const char* &End
)
288 for (; Stop
< End
&& (Stop
[0] == '\n' || Stop
[0] == '\r' || Stop
[0] == '#'); Stop
++)
290 Stop
= (const char*) memchr(Stop
,'\n',End
-Stop
);
294 // ReadPinDir - Load the pin files from this dir into a Policy /*{{{*/
295 // ---------------------------------------------------------------------
296 /* This will load each pin file in the given dir into a Policy. If the
297 given dir is empty the dir set in Dir::Etc::PreferencesParts is used.
298 Note also that this method will issue a warning if the dir does not
299 exists but it will return true in this case! */
300 bool ReadPinDir(pkgPolicy
&Plcy
,string Dir
)
302 if (Dir
.empty() == true)
303 Dir
= _config
->FindDir("Dir::Etc::PreferencesParts");
305 if (DirectoryExists(Dir
) == false)
307 _error
->WarningE("DirectoryExists",_("Unable to read %s"),Dir
.c_str());
311 vector
<string
> const List
= GetListOfFilesInDir(Dir
, "pref", true, true);
314 for (vector
<string
>::const_iterator I
= List
.begin(); I
!= List
.end(); I
++)
315 if (ReadPinFile(Plcy
, *I
) == false)
320 // ReadPinFile - Load the pin file into a Policy /*{{{*/
321 // ---------------------------------------------------------------------
322 /* I'd like to see the preferences file store more than just pin information
323 but right now that is the only stuff I have to store. Later there will
324 have to be some kind of combined super parser to get the data into all
325 the right classes.. */
326 bool ReadPinFile(pkgPolicy
&Plcy
,string File
)
328 if (File
.empty() == true)
329 File
= _config
->FindFile("Dir::Etc::Preferences");
331 if (RealFileExists(File
) == false)
334 FileFd
Fd(File
,FileFd::ReadOnly
);
336 if (_error
->PendingError() == true)
339 PreferenceSection Tags
;
340 while (TF
.Step(Tags
) == true)
342 string Name
= Tags
.FindS("Package");
343 if (Name
.empty() == true)
344 return _error
->Error(_("Invalid record in the preferences file %s, no Package header"), File
.c_str());
350 if (Tags
.Find("Pin",Start
,End
) == false)
353 const char *Word
= Start
;
354 for (; Word
!= End
&& isspace(*Word
) == 0; Word
++);
357 pkgVersionMatch::MatchType Type
;
358 if (stringcasecmp(Start
,Word
,"version") == 0 && Name
.empty() == false)
359 Type
= pkgVersionMatch::Version
;
360 else if (stringcasecmp(Start
,Word
,"release") == 0)
361 Type
= pkgVersionMatch::Release
;
362 else if (stringcasecmp(Start
,Word
,"origin") == 0)
363 Type
= pkgVersionMatch::Origin
;
366 _error
->Warning(_("Did not understand pin type %s"),string(Start
,Word
).c_str());
369 for (; Word
!= End
&& isspace(*Word
) != 0; Word
++);
371 short int priority
= Tags
.FindI("Pin-Priority", 0);
374 _error
->Warning(_("No priority (or zero) specified for pin"));
378 istringstream
s(Name
);
383 Plcy
.CreatePin(Type
, pkg
, string(Word
,End
),priority
);