]> git.saurik.com Git - apt.git/blame - apt-pkg/algorithms.cc
* changelog fixes
[apt.git] / apt-pkg / algorithms.cc
CommitLineData
6c139d6e
AL
1// -*- mode: cpp; mode: fold -*-
2// Description /*{{{*/
b8c0f9b7 3// $Id: algorithms.cc,v 1.44 2002/11/28 18:49:16 jgg Exp $
6c139d6e
AL
4/* ######################################################################
5
6 Algorithms - A set of misc algorithms
7
0a8e3465
AL
8 The pkgProblemResolver class has become insanely complex and
9 very sophisticated, it handles every test case I have thrown at it
10 to my satisfaction. Understanding exactly why all the steps the class
11 does are required is difficult and changing though not very risky
12 may result in other cases not working.
13
6c139d6e
AL
14 ##################################################################### */
15 /*}}}*/
16// Include Files /*{{{*/
17#ifdef __GNUG__
094a497d 18#pragma implementation "apt-pkg/algorithms.h"
6c139d6e 19#endif
094a497d
AL
20#include <apt-pkg/algorithms.h>
21#include <apt-pkg/error.h>
0a8e3465 22#include <apt-pkg/configuration.h>
0a57c0f0 23#include <apt-pkg/version.h>
b2e465d6 24#include <apt-pkg/sptr.h>
0a57c0f0 25
b2e465d6
AL
26
27#include <apti18n.h>
22dcc318 28#include <sys/types.h>
90f057fd 29#include <iostream>
6c139d6e 30 /*}}}*/
584e4558 31using namespace std;
6c139d6e
AL
32
33pkgProblemResolver *pkgProblemResolver::This = 0;
34
35// Simulate::Simulate - Constructor /*{{{*/
36// ---------------------------------------------------------------------
b2e465d6
AL
37/* The legacy translations here of input Pkg iterators is obsolete,
38 this is not necessary since the pkgCaches are fully shared now. */
39pkgSimulate::pkgSimulate(pkgDepCache *Cache) : pkgPackageManager(Cache),
40 iPolicy(Cache),
41 Sim(&Cache->GetCache(),&iPolicy)
6c139d6e 42{
b2e465d6
AL
43 Sim.Init(0);
44 Flags = new unsigned char[Cache->Head().PackageCount];
45 memset(Flags,0,sizeof(*Flags)*Cache->Head().PackageCount);
281daf46
AL
46
47 // Fake a filename so as not to activate the media swapping
48 string Jnk = "SIMULATE";
b2e465d6 49 for (unsigned int I = 0; I != Cache->Head().PackageCount; I++)
281daf46 50 FileNames[I] = Jnk;
6c139d6e
AL
51}
52 /*}}}*/
b2e465d6
AL
53// Simulate::Describe - Describe a package /*{{{*/
54// ---------------------------------------------------------------------
3826564e
MZ
55/* Parameter Current == true displays the current package version,
56 Parameter Candidate == true displays the candidate package version */
57void pkgSimulate::Describe(PkgIterator Pkg,ostream &out,bool Current,bool Candidate)
b2e465d6
AL
58{
59 VerIterator Ver(Sim);
e59458f7
AL
60
61 out << Pkg.Name();
62
3826564e 63 if (Current == true)
e59458f7 64 {
b2e465d6 65 Ver = Pkg.CurrentVer();
e59458f7
AL
66 if (Ver.end() == false)
67 out << " [" << Ver.VerStr() << ']';
68 }
b2e465d6 69
3826564e
MZ
70 if (Candidate == true)
71 {
72 Ver = Sim[Pkg].CandidateVerIter(Sim);
73 if (Ver.end() == true)
74 return;
b2e465d6 75
3826564e
MZ
76 out << " (" << Ver.VerStr() << ' ' << Ver.RelStr() << ')';
77 }
b2e465d6
AL
78}
79 /*}}}*/
6c139d6e
AL
80// Simulate::Install - Simulate unpacking of a package /*{{{*/
81// ---------------------------------------------------------------------
82/* */
83bool pkgSimulate::Install(PkgIterator iPkg,string /*File*/)
84{
85 // Adapt the iterator
86 PkgIterator Pkg = Sim.FindPkg(iPkg.Name());
87 Flags[Pkg->ID] = 1;
88
b2e465d6 89 cout << "Inst ";
3826564e 90 Describe(Pkg,cout,true,true);
6c139d6e
AL
91 Sim.MarkInstall(Pkg,false);
92
93 // Look for broken conflicts+predepends.
94 for (PkgIterator I = Sim.PkgBegin(); I.end() == false; I++)
95 {
96 if (Sim[I].InstallVer == 0)
97 continue;
98
b2e465d6
AL
99 for (DepIterator D = Sim[I].InstVerIter(Sim).DependsList(); D.end() == false;)
100 {
101 DepIterator Start;
102 DepIterator End;
103 D.GlobOr(Start,End);
104 if (Start->Type == pkgCache::Dep::Conflicts ||
308c7d30 105 Start->Type == pkgCache::Dep::DpkgBreaks ||
b2e465d6
AL
106 Start->Type == pkgCache::Dep::Obsoletes ||
107 End->Type == pkgCache::Dep::PreDepends)
6c139d6e 108 {
b2e465d6 109 if ((Sim[End] & pkgDepCache::DepGInstall) == 0)
6c139d6e 110 {
b2e465d6
AL
111 cout << " [" << I.Name() << " on " << Start.TargetPkg().Name() << ']';
112 if (Start->Type == pkgCache::Dep::Conflicts)
6c139d6e
AL
113 _error->Error("Fatal, conflicts violated %s",I.Name());
114 }
b2e465d6
AL
115 }
116 }
6c139d6e
AL
117 }
118
119 if (Sim.BrokenCount() != 0)
120 ShortBreaks();
121 else
04aa15a8 122 cout << endl;
6c139d6e
AL
123 return true;
124}
125 /*}}}*/
126// Simulate::Configure - Simulate configuration of a Package /*{{{*/
127// ---------------------------------------------------------------------
128/* This is not an acurate simulation of relatity, we should really not
129 install the package.. For some investigations it may be necessary
130 however. */
131bool pkgSimulate::Configure(PkgIterator iPkg)
132{
133 // Adapt the iterator
134 PkgIterator Pkg = Sim.FindPkg(iPkg.Name());
135
136 Flags[Pkg->ID] = 2;
137// Sim.MarkInstall(Pkg,false);
138 if (Sim[Pkg].InstBroken() == true)
139 {
04aa15a8 140 cout << "Conf " << Pkg.Name() << " broken" << endl;
6c139d6e
AL
141
142 Sim.Update();
143
144 // Print out each package and the failed dependencies
145 for (pkgCache::DepIterator D = Sim[Pkg].InstVerIter(Sim).DependsList(); D.end() == false; D++)
146 {
147 if (Sim.IsImportantDep(D) == false ||
148 (Sim[D] & pkgDepCache::DepInstall) != 0)
149 continue;
150
b2e465d6
AL
151 if (D->Type == pkgCache::Dep::Obsoletes)
152 cout << " Obsoletes:" << D.TargetPkg().Name();
153 else if (D->Type == pkgCache::Dep::Conflicts)
04aa15a8 154 cout << " Conflicts:" << D.TargetPkg().Name();
308c7d30
IJ
155 else if (D->Type == pkgCache::Dep::DpkgBreaks)
156 cout << " Breaks:" << D.TargetPkg().Name();
6c139d6e 157 else
04aa15a8 158 cout << " Depends:" << D.TargetPkg().Name();
6c139d6e 159 }
04aa15a8 160 cout << endl;
6c139d6e
AL
161
162 _error->Error("Conf Broken %s",Pkg.Name());
163 }
164 else
b2e465d6
AL
165 {
166 cout << "Conf ";
3826564e 167 Describe(Pkg,cout,false,true);
b2e465d6 168 }
6c139d6e
AL
169
170 if (Sim.BrokenCount() != 0)
171 ShortBreaks();
172 else
04aa15a8 173 cout << endl;
6c139d6e
AL
174
175 return true;
176}
177 /*}}}*/
178// Simulate::Remove - Simulate the removal of a package /*{{{*/
179// ---------------------------------------------------------------------
180/* */
fc4b5c9f 181bool pkgSimulate::Remove(PkgIterator iPkg,bool Purge)
6c139d6e
AL
182{
183 // Adapt the iterator
184 PkgIterator Pkg = Sim.FindPkg(iPkg.Name());
185
186 Flags[Pkg->ID] = 3;
187 Sim.MarkDelete(Pkg);
fc4b5c9f 188 if (Purge == true)
b2e465d6 189 cout << "Purg ";
fc4b5c9f 190 else
b2e465d6 191 cout << "Remv ";
3826564e 192 Describe(Pkg,cout,true,false);
6c139d6e
AL
193
194 if (Sim.BrokenCount() != 0)
195 ShortBreaks();
196 else
04aa15a8 197 cout << endl;
6c139d6e
AL
198
199 return true;
200}
201 /*}}}*/
202// Simulate::ShortBreaks - Print out a short line describing all breaks /*{{{*/
203// ---------------------------------------------------------------------
204/* */
205void pkgSimulate::ShortBreaks()
206{
04aa15a8 207 cout << " [";
6c139d6e
AL
208 for (PkgIterator I = Sim.PkgBegin(); I.end() == false; I++)
209 {
210 if (Sim[I].InstBroken() == true)
211 {
212 if (Flags[I->ID] == 0)
04aa15a8 213 cout << I.Name() << ' ';
6c139d6e 214/* else
04aa15a8 215 cout << I.Name() << "! ";*/
6c139d6e
AL
216 }
217 }
04aa15a8 218 cout << ']' << endl;
6c139d6e
AL
219}
220 /*}}}*/
221// ApplyStatus - Adjust for non-ok packages /*{{{*/
222// ---------------------------------------------------------------------
223/* We attempt to change the state of the all packages that have failed
224 installation toward their real state. The ordering code will perform
225 the necessary calculations to deal with the problems. */
226bool pkgApplyStatus(pkgDepCache &Cache)
227{
74a05226
MV
228 pkgDepCache::ActionGroup group(Cache);
229
6c139d6e
AL
230 for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
231 {
e481d5b0
AL
232 if (I->VersionList == 0)
233 continue;
234
d38b7b3d
AL
235 // Only choice for a ReInstReq package is to reinstall
236 if (I->InstState == pkgCache::State::ReInstReq ||
237 I->InstState == pkgCache::State::HoldReInstReq)
238 {
5871718b 239 if (I->CurrentVer != 0 && I.CurrentVer().Downloadable() == true)
74a05226 240 Cache.MarkKeep(I, false, false);
813c8eea
AL
241 else
242 {
243 // Is this right? Will dpkg choke on an upgrade?
2a3f3893
AL
244 if (Cache[I].CandidateVer != 0 &&
245 Cache[I].CandidateVerIter(Cache).Downloadable() == true)
74a05226 246 Cache.MarkInstall(I, false, 0, false);
813c8eea 247 else
b2e465d6
AL
248 return _error->Error(_("The package %s needs to be reinstalled, "
249 "but I can't find an archive for it."),I.Name());
813c8eea
AL
250 }
251
d38b7b3d
AL
252 continue;
253 }
254
6c139d6e
AL
255 switch (I->CurrentState)
256 {
813c8eea
AL
257 /* This means installation failed somehow - it does not need to be
258 re-unpacked (probably) */
b518cca6
AL
259 case pkgCache::State::UnPacked:
260 case pkgCache::State::HalfConfigured:
5871718b 261 if ((I->CurrentVer != 0 && I.CurrentVer().Downloadable() == true) ||
813c8eea 262 I.State() != pkgCache::PkgIterator::NeedsUnpack)
74a05226 263 Cache.MarkKeep(I, false, false);
813c8eea
AL
264 else
265 {
2a3f3893
AL
266 if (Cache[I].CandidateVer != 0 &&
267 Cache[I].CandidateVerIter(Cache).Downloadable() == true)
74a05226 268 Cache.MarkInstall(I, true, 0, false);
813c8eea
AL
269 else
270 Cache.MarkDelete(I);
271 }
6c139d6e
AL
272 break;
273
274 // This means removal failed
b518cca6 275 case pkgCache::State::HalfInstalled:
6c139d6e
AL
276 Cache.MarkDelete(I);
277 break;
278
279 default:
b518cca6 280 if (I->InstState != pkgCache::State::Ok)
6c139d6e
AL
281 return _error->Error("The package %s is not ok and I "
282 "don't know how to fix it!",I.Name());
283 }
284 }
285 return true;
286}
287 /*}}}*/
288// FixBroken - Fix broken packages /*{{{*/
289// ---------------------------------------------------------------------
0a8e3465
AL
290/* This autoinstalls every broken package and then runs the problem resolver
291 on the result. */
6c139d6e
AL
292bool pkgFixBroken(pkgDepCache &Cache)
293{
74a05226
MV
294 pkgDepCache::ActionGroup group(Cache);
295
6c139d6e
AL
296 // Auto upgrade all broken packages
297 for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
298 if (Cache[I].NowBroken() == true)
74a05226 299 Cache.MarkInstall(I, true, 0, false);
7e798dd7 300
6c139d6e
AL
301 /* Fix packages that are in a NeedArchive state but don't have a
302 downloadable install version */
303 for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
304 {
305 if (I.State() != pkgCache::PkgIterator::NeedsUnpack ||
306 Cache[I].Delete() == true)
307 continue;
308
b518cca6 309 if (Cache[I].InstVerIter(Cache).Downloadable() == false)
6c139d6e
AL
310 continue;
311
74a05226 312 Cache.MarkInstall(I, true, 0, false);
6c139d6e
AL
313 }
314
b2e465d6 315 pkgProblemResolver Fix(&Cache);
6c139d6e
AL
316 return Fix.Resolve(true);
317}
318 /*}}}*/
319// DistUpgrade - Distribution upgrade /*{{{*/
320// ---------------------------------------------------------------------
321/* This autoinstalls every package and then force installs every
322 pre-existing package. This creates the initial set of conditions which
323 most likely contain problems because too many things were installed.
324
0a8e3465 325 The problem resolver is used to resolve the problems.
6c139d6e
AL
326 */
327bool pkgDistUpgrade(pkgDepCache &Cache)
328{
74a05226
MV
329 pkgDepCache::ActionGroup group(Cache);
330
6c139d6e
AL
331 /* Auto upgrade all installed packages, this provides the basis
332 for the installation */
333 for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
334 if (I->CurrentVer != 0)
74a05226 335 Cache.MarkInstall(I, true, 0, false);
6c139d6e
AL
336
337 /* Now, auto upgrade all essential packages - this ensures that
338 the essential packages are present and working */
339 for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
b518cca6 340 if ((I->Flags & pkgCache::Flag::Essential) == pkgCache::Flag::Essential)
74a05226 341 Cache.MarkInstall(I, true, 0, false);
6c139d6e
AL
342
343 /* We do it again over all previously installed packages to force
344 conflict resolution on them all. */
345 for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
346 if (I->CurrentVer != 0)
74a05226 347 Cache.MarkInstall(I, false, 0, false);
6c139d6e 348
b2e465d6 349 pkgProblemResolver Fix(&Cache);
c88edf1d 350
6c139d6e 351 // Hold back held packages.
4490f2de 352 if (_config->FindB("APT::Ignore-Hold",false) == false)
6c139d6e 353 {
c88edf1d 354 for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
6c139d6e 355 {
c88edf1d
AL
356 if (I->SelectedState == pkgCache::State::Hold)
357 {
358 Fix.Protect(I);
74a05226 359 Cache.MarkKeep(I, false, false);
c88edf1d 360 }
6c139d6e
AL
361 }
362 }
363
364 return Fix.Resolve();
365}
366 /*}}}*/
0a8e3465
AL
367// AllUpgrade - Upgrade as many packages as possible /*{{{*/
368// ---------------------------------------------------------------------
369/* Right now the system must be consistent before this can be called.
370 It also will not change packages marked for install, it only tries
371 to install packages not marked for install */
372bool pkgAllUpgrade(pkgDepCache &Cache)
373{
74a05226
MV
374 pkgDepCache::ActionGroup group(Cache);
375
b2e465d6 376 pkgProblemResolver Fix(&Cache);
0a8e3465
AL
377
378 if (Cache.BrokenCount() != 0)
379 return false;
380
381 // Upgrade all installed packages
382 for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
383 {
384 if (Cache[I].Install() == true)
385 Fix.Protect(I);
386
b2e465d6 387 if (_config->FindB("APT::Ignore-Hold",false) == false)
c88edf1d
AL
388 if (I->SelectedState == pkgCache::State::Hold)
389 continue;
0a8e3465
AL
390
391 if (I->CurrentVer != 0 && Cache[I].InstallVer != 0)
74a05226 392 Cache.MarkInstall(I, false, 0, false);
0a8e3465
AL
393 }
394
395 return Fix.ResolveByKeep();
396}
397 /*}}}*/
7e798dd7
AL
398// MinimizeUpgrade - Minimizes the set of packages to be upgraded /*{{{*/
399// ---------------------------------------------------------------------
400/* This simply goes over the entire set of packages and tries to keep
401 each package marked for upgrade. If a conflict is generated then
402 the package is restored. */
403bool pkgMinimizeUpgrade(pkgDepCache &Cache)
404{
74a05226
MV
405 pkgDepCache::ActionGroup group(Cache);
406
7e798dd7
AL
407 if (Cache.BrokenCount() != 0)
408 return false;
409
abc8419e 410 // We loop for 10 tries to get the minimal set size.
7e798dd7 411 bool Change = false;
a005475e 412 unsigned int Count = 0;
7e798dd7
AL
413 do
414 {
415 Change = false;
416 for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
417 {
418 // Not interesting
419 if (Cache[I].Upgrade() == false || Cache[I].NewInstall() == true)
420 continue;
a005475e 421
7e798dd7 422 // Keep it and see if that is OK
74a05226 423 Cache.MarkKeep(I, false, false);
7e798dd7 424 if (Cache.BrokenCount() != 0)
74a05226 425 Cache.MarkInstall(I, false, 0, false);
7e798dd7 426 else
a005475e
AL
427 {
428 // If keep didnt actually do anything then there was no change..
429 if (Cache[I].Upgrade() == false)
430 Change = true;
431 }
7e798dd7 432 }
a005475e 433 Count++;
7e798dd7 434 }
a005475e 435 while (Change == true && Count < 10);
7e798dd7
AL
436
437 if (Cache.BrokenCount() != 0)
438 return _error->Error("Internal Error in pkgMinimizeUpgrade");
439
440 return true;
441}
442 /*}}}*/
6c139d6e
AL
443
444// ProblemResolver::pkgProblemResolver - Constructor /*{{{*/
445// ---------------------------------------------------------------------
446/* */
b2e465d6 447pkgProblemResolver::pkgProblemResolver(pkgDepCache *pCache) : Cache(*pCache)
6c139d6e
AL
448{
449 // Allocate memory
b2e465d6 450 unsigned long Size = Cache.Head().PackageCount;
6c139d6e
AL
451 Scores = new signed short[Size];
452 Flags = new unsigned char[Size];
453 memset(Flags,0,sizeof(*Flags)*Size);
454
455 // Set debug to true to see its decision logic
0a8e3465 456 Debug = _config->FindB("Debug::pkgProblemResolver",false);
6c139d6e
AL
457}
458 /*}}}*/
b2e465d6
AL
459// ProblemResolver::~pkgProblemResolver - Destructor /*{{{*/
460// ---------------------------------------------------------------------
461/* */
462pkgProblemResolver::~pkgProblemResolver()
463{
464 delete [] Scores;
465 delete [] Flags;
466}
467 /*}}}*/
6c139d6e
AL
468// ProblemResolver::ScoreSort - Sort the list by score /*{{{*/
469// ---------------------------------------------------------------------
470/* */
471int pkgProblemResolver::ScoreSort(const void *a,const void *b)
472{
473 Package const **A = (Package const **)a;
474 Package const **B = (Package const **)b;
475 if (This->Scores[(*A)->ID] > This->Scores[(*B)->ID])
476 return -1;
477 if (This->Scores[(*A)->ID] < This->Scores[(*B)->ID])
478 return 1;
479 return 0;
480}
481 /*}}}*/
482// ProblemResolver::MakeScores - Make the score table /*{{{*/
483// ---------------------------------------------------------------------
484/* */
485void pkgProblemResolver::MakeScores()
486{
b2e465d6 487 unsigned long Size = Cache.Head().PackageCount;
6c139d6e
AL
488 memset(Scores,0,sizeof(*Scores)*Size);
489
490 // Generate the base scores for a package based on its properties
491 for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
492 {
493 if (Cache[I].InstallVer == 0)
494 continue;
495
496 signed short &Score = Scores[I->ID];
497
498 /* This is arbitary, it should be high enough to elevate an
499 essantial package above most other packages but low enough
500 to allow an obsolete essential packages to be removed by
501 a conflicts on a powerfull normal package (ie libc6) */
b518cca6 502 if ((I->Flags & pkgCache::Flag::Essential) == pkgCache::Flag::Essential)
6c139d6e
AL
503 Score += 100;
504
505 // We transform the priority
506 // Important Required Standard Optional Extra
507 signed short PrioMap[] = {0,3,2,1,-1,-2};
508 if (Cache[I].InstVerIter(Cache)->Priority <= 5)
509 Score += PrioMap[Cache[I].InstVerIter(Cache)->Priority];
510
511 /* This helps to fix oddball problems with conflicting packages
512 on the same level. We enhance the score of installed packages */
513 if (I->CurrentVer != 0)
514 Score += 1;
515 }
516
517 // Now that we have the base scores we go and propogate dependencies
518 for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
519 {
520 if (Cache[I].InstallVer == 0)
521 continue;
522
523 for (pkgCache::DepIterator D = Cache[I].InstVerIter(Cache).DependsList(); D.end() == false; D++)
524 {
b518cca6 525 if (D->Type == pkgCache::Dep::Depends || D->Type == pkgCache::Dep::PreDepends)
6c139d6e
AL
526 Scores[D.TargetPkg()->ID]++;
527 }
528 }
529
530 // Copy the scores to advoid additive looping
b2e465d6 531 SPtrArray<signed short> OldScores = new signed short[Size];
6c139d6e
AL
532 memcpy(OldScores,Scores,sizeof(*Scores)*Size);
533
534 /* Now we cause 1 level of dependency inheritance, that is we add the
535 score of the packages that depend on the target Package. This
536 fortifies high scoring packages */
537 for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
538 {
539 if (Cache[I].InstallVer == 0)
540 continue;
541
542 for (pkgCache::DepIterator D = I.RevDependsList(); D.end() == false; D++)
543 {
544 // Only do it for the install version
545 if ((pkgCache::Version *)D.ParentVer() != Cache[D.ParentPkg()].InstallVer ||
b518cca6 546 (D->Type != pkgCache::Dep::Depends && D->Type != pkgCache::Dep::PreDepends))
6c139d6e
AL
547 continue;
548
549 Scores[I->ID] += abs(OldScores[D.ParentPkg()->ID]);
550 }
551 }
552
553 /* Now we propogate along provides. This makes the packages that
554 provide important packages extremely important */
555 for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
556 {
557 for (pkgCache::PrvIterator P = I.ProvidesList(); P.end() == false; P++)
558 {
559 // Only do it once per package
560 if ((pkgCache::Version *)P.OwnerVer() != Cache[P.OwnerPkg()].InstallVer)
561 continue;
562 Scores[P.OwnerPkg()->ID] += abs(Scores[I->ID] - OldScores[I->ID]);
563 }
564 }
565
566 /* Protected things are pushed really high up. This number should put them
567 ahead of everything */
568 for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
d2685fd6 569 {
6c139d6e
AL
570 if ((Flags[I->ID] & Protected) != 0)
571 Scores[I->ID] += 10000;
d2685fd6
AL
572 if ((I->Flags & pkgCache::Flag::Essential) == pkgCache::Flag::Essential)
573 Scores[I->ID] += 5000;
b2e465d6 574 }
6c139d6e
AL
575}
576 /*}}}*/
577// ProblemResolver::DoUpgrade - Attempt to upgrade this package /*{{{*/
578// ---------------------------------------------------------------------
579/* This goes through and tries to reinstall packages to make this package
580 installable */
581bool pkgProblemResolver::DoUpgrade(pkgCache::PkgIterator Pkg)
582{
74a05226
MV
583 pkgDepCache::ActionGroup group(Cache);
584
6c139d6e
AL
585 if ((Flags[Pkg->ID] & Upgradable) == 0 || Cache[Pkg].Upgradable() == false)
586 return false;
3a486305
AL
587 if ((Flags[Pkg->ID] & Protected) == Protected)
588 return false;
0a8e3465 589
6c139d6e
AL
590 Flags[Pkg->ID] &= ~Upgradable;
591
592 bool WasKept = Cache[Pkg].Keep();
74a05226 593 Cache.MarkInstall(Pkg, false, 0, false);
6c139d6e 594
0a8e3465
AL
595 // This must be a virtual package or something like that.
596 if (Cache[Pkg].InstVerIter(Cache).end() == true)
597 return false;
598
6c139d6e
AL
599 // Isolate the problem dependency
600 bool Fail = false;
601 for (pkgCache::DepIterator D = Cache[Pkg].InstVerIter(Cache).DependsList(); D.end() == false;)
602 {
603 // Compute a single dependency element (glob or)
604 pkgCache::DepIterator Start = D;
605 pkgCache::DepIterator End = D;
606 unsigned char State = 0;
4b1b89c5 607 for (bool LastOR = true; D.end() == false && LastOR == true;)
6c139d6e
AL
608 {
609 State |= Cache[D];
b518cca6 610 LastOR = (D->CompareOp & pkgCache::Dep::Or) == pkgCache::Dep::Or;
4b1b89c5 611 D++;
6c139d6e
AL
612 if (LastOR == true)
613 End = D;
614 }
615
616 // We only worry about critical deps.
617 if (End.IsCritical() != true)
618 continue;
4b1b89c5
AL
619
620 // Iterate over all the members in the or group
621 while (1)
0a8e3465 622 {
4b1b89c5
AL
623 // Dep is ok now
624 if ((Cache[End] & pkgDepCache::DepGInstall) == pkgDepCache::DepGInstall)
625 break;
626
627 // Do not change protected packages
628 PkgIterator P = Start.SmartTargetPkg();
629 if ((Flags[P->ID] & Protected) == Protected)
630 {
631 if (Debug == true)
648e3cb4 632 clog << " Reinst Failed because of protected " << P.Name() << endl;
4b1b89c5 633 Fail = true;
4b1b89c5 634 }
648e3cb4 635 else
6c139d6e 636 {
648e3cb4
AL
637 // Upgrade the package if the candidate version will fix the problem.
638 if ((Cache[Start] & pkgDepCache::DepCVer) == pkgDepCache::DepCVer)
639 {
640 if (DoUpgrade(P) == false)
641 {
642 if (Debug == true)
643 clog << " Reinst Failed because of " << P.Name() << endl;
644 Fail = true;
645 }
646 else
647 {
648 Fail = false;
649 break;
650 }
651 }
652 else
4b1b89c5 653 {
648e3cb4
AL
654 /* We let the algorithm deal with conflicts on its next iteration,
655 it is much smarter than us */
b2e465d6 656 if (Start->Type == pkgCache::Dep::Conflicts ||
308c7d30 657 Start->Type == pkgCache::Dep::DpkgBreaks ||
b2e465d6
AL
658 Start->Type == pkgCache::Dep::Obsoletes)
659 break;
648e3cb4 660
4b1b89c5 661 if (Debug == true)
648e3cb4 662 clog << " Reinst Failed early because of " << Start.TargetPkg().Name() << endl;
4b1b89c5 663 Fail = true;
648e3cb4 664 }
4b1b89c5 665 }
6c139d6e 666
4b1b89c5
AL
667 if (Start == End)
668 break;
669 Start++;
670 }
671 if (Fail == true)
6c139d6e 672 break;
6c139d6e
AL
673 }
674
675 // Undo our operations - it might be smart to undo everything this did..
676 if (Fail == true)
677 {
678 if (WasKept == true)
74a05226 679 Cache.MarkKeep(Pkg, false, false);
6c139d6e
AL
680 else
681 Cache.MarkDelete(Pkg);
682 return false;
683 }
684
685 if (Debug == true)
0a8e3465 686 clog << " Re-Instated " << Pkg.Name() << endl;
6c139d6e
AL
687 return true;
688}
689 /*}}}*/
690// ProblemResolver::Resolve - Run the resolution pass /*{{{*/
691// ---------------------------------------------------------------------
692/* This routines works by calculating a score for each package. The score
693 is derived by considering the package's priority and all reverse
694 dependents giving an integer that reflects the amount of breakage that
695 adjusting the package will inflict.
696
697 It goes from highest score to lowest and corrects all of the breaks by
698 keeping or removing the dependant packages. If that fails then it removes
699 the package itself and goes on. The routine should be able to intelligently
700 go from any broken state to a fixed state.
701
702 The BrokenFix flag enables a mode where the algorithm tries to
703 upgrade packages to advoid problems. */
704bool pkgProblemResolver::Resolve(bool BrokenFix)
705{
74a05226
MV
706 pkgDepCache::ActionGroup group(Cache);
707
b2e465d6 708 unsigned long Size = Cache.Head().PackageCount;
6c139d6e
AL
709
710 // Record which packages are marked for install
711 bool Again = false;
712 do
713 {
714 Again = false;
715 for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
716 {
717 if (Cache[I].Install() == true)
718 Flags[I->ID] |= PreInstalled;
719 else
720 {
721 if (Cache[I].InstBroken() == true && BrokenFix == true)
722 {
74a05226 723 Cache.MarkInstall(I, false, 0, false);
6c139d6e
AL
724 if (Cache[I].Install() == true)
725 Again = true;
726 }
727
728 Flags[I->ID] &= ~PreInstalled;
729 }
730 Flags[I->ID] |= Upgradable;
731 }
732 }
733 while (Again == true);
734
735 if (Debug == true)
0a8e3465 736 clog << "Starting" << endl;
6c139d6e
AL
737
738 MakeScores();
739
740 /* We have to order the packages so that the broken fixing pass
741 operates from highest score to lowest. This prevents problems when
742 high score packages cause the removal of lower score packages that
743 would cause the removal of even lower score packages. */
b2e465d6 744 SPtrArray<pkgCache::Package *> PList = new pkgCache::Package *[Size];
6c139d6e
AL
745 pkgCache::Package **PEnd = PList;
746 for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
747 *PEnd++ = I;
748 This = this;
749 qsort(PList,PEnd - PList,sizeof(*PList),&ScoreSort);
750
751/* for (pkgCache::Package **K = PList; K != PEnd; K++)
752 if (Scores[(*K)->ID] != 0)
753 {
754 pkgCache::PkgIterator Pkg(Cache,*K);
0a8e3465 755 clog << Scores[(*K)->ID] << ' ' << Pkg.Name() <<
6c139d6e
AL
756 ' ' << (pkgCache::Version *)Pkg.CurrentVer() << ' ' <<
757 Cache[Pkg].InstallVer << ' ' << Cache[Pkg].CandidateVer << endl;
758 } */
759
760 if (Debug == true)
0a8e3465 761 clog << "Starting 2" << endl;
6c139d6e
AL
762
763 /* Now consider all broken packages. For each broken package we either
764 remove the package or fix it's problem. We do this once, it should
765 not be possible for a loop to form (that is a < b < c and fixing b by
766 changing a breaks c) */
767 bool Change = true;
768 for (int Counter = 0; Counter != 10 && Change == true; Counter++)
769 {
770 Change = false;
771 for (pkgCache::Package **K = PList; K != PEnd; K++)
772 {
773 pkgCache::PkgIterator I(Cache,*K);
774
775 /* We attempt to install this and see if any breaks result,
776 this takes care of some strange cases */
777 if (Cache[I].CandidateVer != Cache[I].InstallVer &&
778 I->CurrentVer != 0 && Cache[I].InstallVer != 0 &&
779 (Flags[I->ID] & PreInstalled) != 0 &&
0a8e3465
AL
780 (Flags[I->ID] & Protected) == 0 &&
781 (Flags[I->ID] & ReInstateTried) == 0)
6c139d6e
AL
782 {
783 if (Debug == true)
0a8e3465 784 clog << " Try to Re-Instate " << I.Name() << endl;
a6568219 785 unsigned long OldBreaks = Cache.BrokenCount();
6c139d6e 786 pkgCache::Version *OldVer = Cache[I].InstallVer;
0a8e3465
AL
787 Flags[I->ID] &= ReInstateTried;
788
74a05226 789 Cache.MarkInstall(I, false, 0, false);
6c139d6e
AL
790 if (Cache[I].InstBroken() == true ||
791 OldBreaks < Cache.BrokenCount())
792 {
793 if (OldVer == 0)
794 Cache.MarkDelete(I);
795 else
74a05226 796 Cache.MarkKeep(I, false, false);
6c139d6e
AL
797 }
798 else
799 if (Debug == true)
0a8e3465 800 clog << "Re-Instated " << I.Name() << " (" << OldBreaks << " vs " << Cache.BrokenCount() << ')' << endl;
6c139d6e
AL
801 }
802
803 if (Cache[I].InstallVer == 0 || Cache[I].InstBroken() == false)
804 continue;
805
00b47c98
AL
806 if (Debug == true)
807 cout << "Investigating " << I.Name() << endl;
808
6c139d6e
AL
809 // Isolate the problem dependency
810 PackageKill KillList[100];
811 PackageKill *LEnd = KillList;
421c8d10
AL
812 bool InOr = false;
813 pkgCache::DepIterator Start;
814 pkgCache::DepIterator End;
b2e465d6 815 PackageKill *OldEnd = LEnd;
648e3cb4
AL
816
817 enum {OrRemove,OrKeep} OrOp = OrRemove;
421c8d10
AL
818 for (pkgCache::DepIterator D = Cache[I].InstVerIter(Cache).DependsList();
819 D.end() == false || InOr == true;)
6c139d6e
AL
820 {
821 // Compute a single dependency element (glob or)
648e3cb4
AL
822 if (Start == End)
823 {
824 // Decide what to do
825 if (InOr == true)
826 {
827 if (OldEnd == LEnd && OrOp == OrRemove)
70777d4b
AL
828 {
829 if ((Flags[I->ID] & Protected) != Protected)
00b47c98
AL
830 {
831 if (Debug == true)
832 clog << " Or group remove for " << I.Name() << endl;
70777d4b 833 Cache.MarkDelete(I);
cd14eaf2 834 Change = true;
00b47c98 835 }
70777d4b 836 }
648e3cb4 837 if (OldEnd == LEnd && OrOp == OrKeep)
00b47c98
AL
838 {
839 if (Debug == true)
840 clog << " Or group keep for " << I.Name() << endl;
74a05226 841 Cache.MarkKeep(I, false, false);
cd14eaf2 842 Change = true;
b2e465d6 843 }
648e3cb4
AL
844 }
845
b2e465d6
AL
846 /* We do an extra loop (as above) to finalize the or group
847 processing */
848 InOr = false;
648e3cb4 849 OrOp = OrRemove;
421c8d10 850 D.GlobOr(Start,End);
b2e465d6
AL
851 if (Start.end() == true)
852 break;
cd14eaf2 853
b2e465d6
AL
854 // We only worry about critical deps.
855 if (End.IsCritical() != true)
856 continue;
cd14eaf2 857
648e3cb4
AL
858 InOr = Start != End;
859 OldEnd = LEnd;
cd14eaf2 860 }
421c8d10
AL
861 else
862 Start++;
cd14eaf2 863
6c139d6e
AL
864 // Dep is ok
865 if ((Cache[End] & pkgDepCache::DepGInstall) == pkgDepCache::DepGInstall)
cd14eaf2
AL
866 {
867 InOr = false;
6c139d6e 868 continue;
cd14eaf2
AL
869 }
870
6c139d6e 871 if (Debug == true)
421c8d10 872 clog << "Package " << I.Name() << " has broken dep on " << Start.TargetPkg().Name() << endl;
fcf85120
AL
873
874 /* Look across the version list. If there are no possible
875 targets then we keep the package and bail. This is necessary
876 if a package has a dep on another package that cant be found */
b2e465d6 877 SPtrArray<pkgCache::Version *> VList = Start.AllTargets();
fcf85120 878 if (*VList == 0 && (Flags[I->ID] & Protected) != Protected &&
648e3cb4 879 Start->Type != pkgCache::Dep::Conflicts &&
308c7d30 880 Start->Type != pkgCache::Dep::DpkgBreaks &&
b2e465d6 881 Start->Type != pkgCache::Dep::Obsoletes &&
fcf85120 882 Cache[I].NowBroken() == false)
648e3cb4
AL
883 {
884 if (InOr == true)
885 {
886 /* No keep choice because the keep being OK could be the
887 result of another element in the OR group! */
888 continue;
889 }
890
fcf85120 891 Change = true;
74a05226 892 Cache.MarkKeep(I, false, false);
fcf85120
AL
893 break;
894 }
895
6c139d6e
AL
896 bool Done = false;
897 for (pkgCache::Version **V = VList; *V != 0; V++)
898 {
899 pkgCache::VerIterator Ver(Cache,*V);
900 pkgCache::PkgIterator Pkg = Ver.ParentPkg();
a6bfe583 901
6c139d6e 902 if (Debug == true)
421c8d10 903 clog << " Considering " << Pkg.Name() << ' ' << (int)Scores[Pkg->ID] <<
6c139d6e 904 " as a solution to " << I.Name() << ' ' << (int)Scores[I->ID] << endl;
a6bfe583
AL
905
906 /* Try to fix the package under consideration rather than
907 fiddle with the VList package */
6c139d6e 908 if (Scores[I->ID] <= Scores[Pkg->ID] ||
421c8d10 909 ((Cache[Start] & pkgDepCache::DepNow) == 0 &&
b2e465d6 910 End->Type != pkgCache::Dep::Conflicts &&
308c7d30 911 End->Type != pkgCache::Dep::DpkgBreaks &&
b2e465d6 912 End->Type != pkgCache::Dep::Obsoletes))
6c139d6e 913 {
200f8c52 914 // Try a little harder to fix protected packages..
3b5421b4 915 if ((Flags[I->ID] & Protected) == Protected)
200f8c52
AL
916 {
917 if (DoUpgrade(Pkg) == true)
0296c633 918 {
b2e465d6
AL
919 if (Scores[Pkg->ID] > Scores[I->ID])
920 Scores[Pkg->ID] = Scores[I->ID];
0296c633
AL
921 break;
922 }
923
6c139d6e 924 continue;
200f8c52
AL
925 }
926
927 /* See if a keep will do, unless the package is protected,
648e3cb4
AL
928 then installing it will be necessary */
929 bool Installed = Cache[I].Install();
74a05226 930 Cache.MarkKeep(I, false, false);
6c139d6e
AL
931 if (Cache[I].InstBroken() == false)
932 {
648e3cb4
AL
933 // Unwind operation will be keep now
934 if (OrOp == OrRemove)
935 OrOp = OrKeep;
936
937 // Restore
938 if (InOr == true && Installed == true)
74a05226 939 Cache.MarkInstall(I, false, 0, false);
648e3cb4 940
6c139d6e 941 if (Debug == true)
421c8d10 942 clog << " Holding Back " << I.Name() << " rather than change " << Start.TargetPkg().Name() << endl;
6c139d6e
AL
943 }
944 else
421c8d10 945 {
6c139d6e
AL
946 if (BrokenFix == false || DoUpgrade(I) == false)
947 {
421c8d10
AL
948 // Consider other options
949 if (InOr == false)
950 {
951 if (Debug == true)
952 clog << " Removing " << I.Name() << " rather than change " << Start.TargetPkg().Name() << endl;
953 Cache.MarkDelete(I);
954 if (Counter > 1)
b2e465d6
AL
955 {
956 if (Scores[Pkg->ID] > Scores[I->ID])
957 Scores[I->ID] = Scores[Pkg->ID];
958 }
421c8d10 959 }
0a8e3465 960 }
6c139d6e 961 }
b5dc9785 962
6c139d6e
AL
963 Change = true;
964 Done = true;
965 break;
966 }
967 else
968 {
a6bfe583
AL
969 /* This is a conflicts, and the version we are looking
970 at is not the currently selected version of the
971 package, which means it is not necessary to
972 remove/keep */
973 if (Cache[Pkg].InstallVer != Ver &&
974 (Start->Type == pkgCache::Dep::Conflicts ||
975 Start->Type == pkgCache::Dep::Obsoletes))
976 continue;
308c7d30
IJ
977
978 if (Start->Type == pkgCache::Dep::DpkgBreaks)
979 {
980 /* Would it help if we upgraded? */
981 if (Cache[End] & pkgDepCache::DepGCVer) {
982 if (Debug)
983 clog << " Upgrading " << Pkg.Name() << " due to Breaks field in " << I.Name() << endl;
984 Cache.MarkInstall(Pkg, false, 0, false);
985 continue;
986 }
987 if (Debug)
988 clog << " Will not break " << Pkg.Name() << " as stated in Breaks field in " << I.Name() <<endl;
989 continue;
990 }
991
648e3cb4 992 // Skip adding to the kill list if it is protected
6c139d6e
AL
993 if ((Flags[Pkg->ID] & Protected) != 0)
994 continue;
a6bfe583
AL
995
996 if (Debug == true)
997 clog << " Added " << Pkg.Name() << " to the remove list" << endl;
6c139d6e
AL
998
999 LEnd->Pkg = Pkg;
1000 LEnd->Dep = End;
1001 LEnd++;
0a8e3465 1002
b2e465d6
AL
1003 if (Start->Type != pkgCache::Dep::Conflicts &&
1004 Start->Type != pkgCache::Dep::Obsoletes)
6c139d6e
AL
1005 break;
1006 }
1007 }
1008
1009 // Hm, nothing can possibly satisify this dep. Nuke it.
b2e465d6
AL
1010 if (VList[0] == 0 &&
1011 Start->Type != pkgCache::Dep::Conflicts &&
308c7d30 1012 Start->Type != pkgCache::Dep::DpkgBreaks &&
b2e465d6 1013 Start->Type != pkgCache::Dep::Obsoletes &&
648e3cb4 1014 (Flags[I->ID] & Protected) != Protected)
6c139d6e 1015 {
648e3cb4 1016 bool Installed = Cache[I].Install();
6c139d6e
AL
1017 Cache.MarkKeep(I);
1018 if (Cache[I].InstBroken() == false)
1019 {
648e3cb4
AL
1020 // Unwind operation will be keep now
1021 if (OrOp == OrRemove)
1022 OrOp = OrKeep;
1023
1024 // Restore
1025 if (InOr == true && Installed == true)
74a05226 1026 Cache.MarkInstall(I, false, 0, false);
648e3cb4 1027
6c139d6e 1028 if (Debug == true)
421c8d10 1029 clog << " Holding Back " << I.Name() << " because I can't find " << Start.TargetPkg().Name() << endl;
6c139d6e
AL
1030 }
1031 else
1032 {
1033 if (Debug == true)
421c8d10 1034 clog << " Removing " << I.Name() << " because I can't find " << Start.TargetPkg().Name() << endl;
648e3cb4
AL
1035 if (InOr == false)
1036 Cache.MarkDelete(I);
6c139d6e
AL
1037 }
1038
1039 Change = true;
1040 Done = true;
1041 }
1042
421c8d10
AL
1043 // Try some more
1044 if (InOr == true)
1045 continue;
1046
6c139d6e
AL
1047 if (Done == true)
1048 break;
1049 }
1050
1051 // Apply the kill list now
1052 if (Cache[I].InstallVer != 0)
648e3cb4 1053 {
6c139d6e 1054 for (PackageKill *J = KillList; J != LEnd; J++)
6c139d6e 1055 {
648e3cb4
AL
1056 Change = true;
1057 if ((Cache[J->Dep] & pkgDepCache::DepGNow) == 0)
1058 {
b2e465d6
AL
1059 if (J->Dep->Type == pkgCache::Dep::Conflicts ||
1060 J->Dep->Type == pkgCache::Dep::Obsoletes)
648e3cb4
AL
1061 {
1062 if (Debug == true)
1063 clog << " Fixing " << I.Name() << " via remove of " << J->Pkg.Name() << endl;
1064 Cache.MarkDelete(J->Pkg);
1065 }
1066 }
1067 else
6c139d6e
AL
1068 {
1069 if (Debug == true)
648e3cb4 1070 clog << " Fixing " << I.Name() << " via keep of " << J->Pkg.Name() << endl;
74a05226 1071 Cache.MarkKeep(J->Pkg, false, false);
6c139d6e 1072 }
b2e465d6 1073
648e3cb4 1074 if (Counter > 1)
b2e465d6
AL
1075 {
1076 if (Scores[I->ID] > Scores[J->Pkg->ID])
1077 Scores[J->Pkg->ID] = Scores[I->ID];
1078 }
648e3cb4
AL
1079 }
1080 }
1081 }
6c139d6e
AL
1082 }
1083
1084 if (Debug == true)
0a8e3465 1085 clog << "Done" << endl;
b2e465d6 1086
6c139d6e 1087 if (Cache.BrokenCount() != 0)
b5dc9785
AL
1088 {
1089 // See if this is the result of a hold
1090 pkgCache::PkgIterator I = Cache.PkgBegin();
1091 for (;I.end() != true; I++)
1092 {
1093 if (Cache[I].InstBroken() == false)
1094 continue;
1095 if ((Flags[I->ID] & Protected) != Protected)
b2e465d6 1096 return _error->Error(_("Error, pkgProblemResolver::Resolve generated breaks, this may be caused by held packages."));
b5dc9785 1097 }
b2e465d6 1098 return _error->Error(_("Unable to correct problems, you have held broken packages."));
b5dc9785
AL
1099 }
1100
80fa0d8a
MV
1101 // set the auto-flags (mvo: I'm not sure if we _really_ need this, but
1102 // I didn't managed
1103 pkgCache::PkgIterator I = Cache.PkgBegin();
1104 for (;I.end() != true; I++) {
1105 if (Cache[I].NewInstall() && !(Flags[I->ID] & PreInstalled)) {
120365ce
MV
1106 if(_config->FindI("Debug::pkgAutoRemove",false)) {
1107 std::clog << "Resolve installed new pkg: " << I.Name()
1108 << " (now marking it as auto)" << std::endl;
1109 }
80fa0d8a
MV
1110 Cache[I].Flags |= pkgCache::Flag::Auto;
1111 }
1112 }
1113
1114
0a8e3465
AL
1115 return true;
1116}
1117 /*}}}*/
1118// ProblemResolver::ResolveByKeep - Resolve problems using keep /*{{{*/
1119// ---------------------------------------------------------------------
1120/* This is the work horse of the soft upgrade routine. It is very gental
1121 in that it does not install or remove any packages. It is assumed that the
1122 system was non-broken previously. */
1123bool pkgProblemResolver::ResolveByKeep()
1124{
74a05226
MV
1125 pkgDepCache::ActionGroup group(Cache);
1126
b2e465d6 1127 unsigned long Size = Cache.Head().PackageCount;
0a8e3465
AL
1128
1129 if (Debug == true)
1130 clog << "Entering ResolveByKeep" << endl;
1131
1132 MakeScores();
1133
1134 /* We have to order the packages so that the broken fixing pass
1135 operates from highest score to lowest. This prevents problems when
1136 high score packages cause the removal of lower score packages that
1137 would cause the removal of even lower score packages. */
1138 pkgCache::Package **PList = new pkgCache::Package *[Size];
1139 pkgCache::Package **PEnd = PList;
1140 for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
1141 *PEnd++ = I;
1142 This = this;
1143 qsort(PList,PEnd - PList,sizeof(*PList),&ScoreSort);
1144
1145 // Consider each broken package
1146 pkgCache::Package **LastStop = 0;
1147 for (pkgCache::Package **K = PList; K != PEnd; K++)
1148 {
1149 pkgCache::PkgIterator I(Cache,*K);
1150
1151 if (Cache[I].InstallVer == 0 || Cache[I].InstBroken() == false)
1152 continue;
1153
1154 /* Keep the package. If this works then great, otherwise we have
b2e465d6 1155 to be significantly more agressive and manipulate its dependencies */
0a8e3465
AL
1156 if ((Flags[I->ID] & Protected) == 0)
1157 {
1158 if (Debug == true)
1159 clog << "Keeping package " << I.Name() << endl;
74a05226 1160 Cache.MarkKeep(I, false, false);
0a8e3465
AL
1161 if (Cache[I].InstBroken() == false)
1162 {
b2e465d6 1163 K = PList - 1;
0a8e3465
AL
1164 continue;
1165 }
1166 }
1167
1168 // Isolate the problem dependencies
1169 for (pkgCache::DepIterator D = Cache[I].InstVerIter(Cache).DependsList(); D.end() == false;)
1170 {
c5532863
AL
1171 DepIterator Start;
1172 DepIterator End;
1173 D.GlobOr(Start,End);
1174
0a8e3465
AL
1175 // We only worry about critical deps.
1176 if (End.IsCritical() != true)
1177 continue;
1178
1179 // Dep is ok
1180 if ((Cache[End] & pkgDepCache::DepGInstall) == pkgDepCache::DepGInstall)
1181 continue;
c5532863
AL
1182
1183 /* Hm, the group is broken.. I suppose the best thing to do is to
1184 is to try every combination of keep/not-keep for the set, but thats
1185 slow, and this never happens, just be conservative and assume the
1186 list of ors is in preference and keep till it starts to work. */
1187 while (true)
0a8e3465 1188 {
c5532863
AL
1189 if (Debug == true)
1190 clog << "Package " << I.Name() << " has broken dep on " << Start.TargetPkg().Name() << endl;
0a8e3465 1191
c5532863
AL
1192 // Look at all the possible provides on this package
1193 SPtrArray<pkgCache::Version *> VList = Start.AllTargets();
1194 for (pkgCache::Version **V = VList; *V != 0; V++)
0a8e3465 1195 {
c5532863
AL
1196 pkgCache::VerIterator Ver(Cache,*V);
1197 pkgCache::PkgIterator Pkg = Ver.ParentPkg();
1198
1199 // It is not keepable
1200 if (Cache[Pkg].InstallVer == 0 ||
1201 Pkg->CurrentVer == 0)
1202 continue;
1203
1204 if ((Flags[I->ID] & Protected) == 0)
1205 {
1206 if (Debug == true)
1207 clog << " Keeping Package " << Pkg.Name() << " due to dep" << endl;
74a05226 1208 Cache.MarkKeep(Pkg, false, false);
c5532863
AL
1209 }
1210
1211 if (Cache[I].InstBroken() == false)
1212 break;
0a8e3465
AL
1213 }
1214
1215 if (Cache[I].InstBroken() == false)
1216 break;
0a8e3465 1217
c5532863
AL
1218 if (Start == End)
1219 break;
1220 Start++;
1221 }
1222
0a8e3465
AL
1223 if (Cache[I].InstBroken() == false)
1224 break;
1225 }
1226
1227 if (Cache[I].InstBroken() == true)
1228 continue;
1229
1230 // Restart again.
1231 if (K == LastStop)
1232 return _error->Error("Internal Error, pkgProblemResolver::ResolveByKeep is looping on package %s.",I.Name());
1233 LastStop = K;
b2e465d6 1234 K = PList - 1;
0a8e3465 1235 }
6c139d6e
AL
1236
1237 return true;
1238}
1239 /*}}}*/
3b5421b4
AL
1240// ProblemResolver::InstallProtect - Install all protected packages /*{{{*/
1241// ---------------------------------------------------------------------
1242/* This is used to make sure protected packages are installed */
1243void pkgProblemResolver::InstallProtect()
1244{
74a05226
MV
1245 pkgDepCache::ActionGroup group(Cache);
1246
3b5421b4
AL
1247 for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
1248 {
1249 if ((Flags[I->ID] & Protected) == Protected)
1250 {
1251 if ((Flags[I->ID] & ToRemove) == ToRemove)
1252 Cache.MarkDelete(I);
c15f5690
MV
1253 else
1254 {
1255 // preserver the information if the package was auto
1256 // or manual installed
1257 bool autoInst = (Cache[I].Flags & pkgCache::Flag::Auto);
1258 Cache.MarkInstall(I, false, 0, !autoInst);
1259 }
3b5421b4
AL
1260 }
1261 }
1262}
1263 /*}}}*/
b2e465d6
AL
1264
1265// PrioSortList - Sort a list of versions by priority /*{{{*/
1266// ---------------------------------------------------------------------
1267/* This is ment to be used in conjunction with AllTargets to get a list
1268 of versions ordered by preference. */
1269static pkgCache *PrioCache;
1270static int PrioComp(const void *A,const void *B)
1271{
1272 pkgCache::VerIterator L(*PrioCache,*(pkgCache::Version **)A);
1273 pkgCache::VerIterator R(*PrioCache,*(pkgCache::Version **)B);
1274
1275 if ((L.ParentPkg()->Flags & pkgCache::Flag::Essential) == pkgCache::Flag::Essential &&
b8c0f9b7
AL
1276 (R.ParentPkg()->Flags & pkgCache::Flag::Essential) != pkgCache::Flag::Essential)
1277 return 1;
b2e465d6 1278 if ((L.ParentPkg()->Flags & pkgCache::Flag::Essential) != pkgCache::Flag::Essential &&
b8c0f9b7
AL
1279 (R.ParentPkg()->Flags & pkgCache::Flag::Essential) == pkgCache::Flag::Essential)
1280 return -1;
b2e465d6
AL
1281
1282 if (L->Priority != R->Priority)
b8c0f9b7 1283 return R->Priority - L->Priority;
b2e465d6
AL
1284 return strcmp(L.ParentPkg().Name(),R.ParentPkg().Name());
1285}
1286void pkgPrioSortList(pkgCache &Cache,pkgCache::Version **List)
1287{
1288 unsigned long Count = 0;
1289 PrioCache = &Cache;
1290 for (pkgCache::Version **I = List; *I != 0; I++)
1291 Count++;
1292 qsort(List,Count,sizeof(*List),PrioComp);
1293}
1294 /*}}}*/
db1e7193 1295