]>
git.saurik.com Git - apt.git/blob - apt-pkg/algorithms.cc
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: algorithms.cc,v 1.32 2001/02/20 07:03:17 jgg Exp $
4 /* ######################################################################
6 Algorithms - A set of misc algorithms
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.
14 ##################################################################### */
16 // Include Files /*{{{*/
18 #pragma implementation "apt-pkg/algorithms.h"
20 #include <apt-pkg/algorithms.h>
21 #include <apt-pkg/error.h>
22 #include <apt-pkg/configuration.h>
23 #include <apt-pkg/sptr.h>
30 pkgProblemResolver
*pkgProblemResolver::This
= 0;
32 // Simulate::Simulate - Constructor /*{{{*/
33 // ---------------------------------------------------------------------
34 /* The legacy translations here of input Pkg iterators is obsolete,
35 this is not necessary since the pkgCaches are fully shared now. */
36 pkgSimulate::pkgSimulate(pkgDepCache
*Cache
) : pkgPackageManager(Cache
),
38 Sim(&Cache
->GetCache(),&iPolicy
)
41 Flags
= new unsigned char[Cache
->Head().PackageCount
];
42 memset(Flags
,0,sizeof(*Flags
)*Cache
->Head().PackageCount
);
44 // Fake a filename so as not to activate the media swapping
45 string Jnk
= "SIMULATE";
46 for (unsigned int I
= 0; I
!= Cache
->Head().PackageCount
; I
++)
50 // Simulate::Describe - Describe a package /*{{{*/
51 // ---------------------------------------------------------------------
53 void pkgSimulate::Describe(PkgIterator Pkg
,ostream
&out
,bool Now
)
57 Ver
= Pkg
.CurrentVer();
59 Ver
= Sim
[Pkg
].CandidateVerIter(Sim
);
63 if (Ver
.end() == true)
66 out
<< " (" << Ver
.VerStr() << ' ' << Ver
.RelStr() << ')';
69 // Simulate::Install - Simulate unpacking of a package /*{{{*/
70 // ---------------------------------------------------------------------
72 bool pkgSimulate::Install(PkgIterator iPkg
,string
/*File*/)
75 PkgIterator Pkg
= Sim
.FindPkg(iPkg
.Name());
79 Describe(Pkg
,cout
,false);
80 Sim
.MarkInstall(Pkg
,false);
82 // Look for broken conflicts+predepends.
83 for (PkgIterator I
= Sim
.PkgBegin(); I
.end() == false; I
++)
85 if (Sim
[I
].InstallVer
== 0)
88 for (DepIterator D
= Sim
[I
].InstVerIter(Sim
).DependsList(); D
.end() == false;)
93 if (Start
->Type
== pkgCache::Dep::Conflicts
||
94 Start
->Type
== pkgCache::Dep::Obsoletes
||
95 End
->Type
== pkgCache::Dep::PreDepends
)
97 if ((Sim
[End
] & pkgDepCache::DepGInstall
) == 0)
99 cout
<< " [" << I
.Name() << " on " << Start
.TargetPkg().Name() << ']';
100 if (Start
->Type
== pkgCache::Dep::Conflicts
)
101 _error
->Error("Fatal, conflicts violated %s",I
.Name());
107 if (Sim
.BrokenCount() != 0)
114 // Simulate::Configure - Simulate configuration of a Package /*{{{*/
115 // ---------------------------------------------------------------------
116 /* This is not an acurate simulation of relatity, we should really not
117 install the package.. For some investigations it may be necessary
119 bool pkgSimulate::Configure(PkgIterator iPkg
)
121 // Adapt the iterator
122 PkgIterator Pkg
= Sim
.FindPkg(iPkg
.Name());
125 // Sim.MarkInstall(Pkg,false);
126 if (Sim
[Pkg
].InstBroken() == true)
128 cout
<< "Conf " << Pkg
.Name() << " broken" << endl
;
132 // Print out each package and the failed dependencies
133 for (pkgCache::DepIterator D
= Sim
[Pkg
].InstVerIter(Sim
).DependsList(); D
.end() == false; D
++)
135 if (Sim
.IsImportantDep(D
) == false ||
136 (Sim
[D
] & pkgDepCache::DepInstall
) != 0)
139 if (D
->Type
== pkgCache::Dep::Obsoletes
)
140 cout
<< " Obsoletes:" << D
.TargetPkg().Name();
141 else if (D
->Type
== pkgCache::Dep::Conflicts
)
142 cout
<< " Conflicts:" << D
.TargetPkg().Name();
144 cout
<< " Depends:" << D
.TargetPkg().Name();
148 _error
->Error("Conf Broken %s",Pkg
.Name());
153 Describe(Pkg
,cout
,false);
156 if (Sim
.BrokenCount() != 0)
164 // Simulate::Remove - Simulate the removal of a package /*{{{*/
165 // ---------------------------------------------------------------------
167 bool pkgSimulate::Remove(PkgIterator iPkg
,bool Purge
)
169 // Adapt the iterator
170 PkgIterator Pkg
= Sim
.FindPkg(iPkg
.Name());
178 Describe(Pkg
,cout
,false);
180 if (Sim
.BrokenCount() != 0)
188 // Simulate::ShortBreaks - Print out a short line describing all breaks /*{{{*/
189 // ---------------------------------------------------------------------
191 void pkgSimulate::ShortBreaks()
194 for (PkgIterator I
= Sim
.PkgBegin(); I
.end() == false; I
++)
196 if (Sim
[I
].InstBroken() == true)
198 if (Flags
[I
->ID
] == 0)
199 cout
<< I
.Name() << ' ';
201 cout << I.Name() << "! ";*/
207 // ApplyStatus - Adjust for non-ok packages /*{{{*/
208 // ---------------------------------------------------------------------
209 /* We attempt to change the state of the all packages that have failed
210 installation toward their real state. The ordering code will perform
211 the necessary calculations to deal with the problems. */
212 bool pkgApplyStatus(pkgDepCache
&Cache
)
214 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
216 // Only choice for a ReInstReq package is to reinstall
217 if (I
->InstState
== pkgCache::State::ReInstReq
||
218 I
->InstState
== pkgCache::State::HoldReInstReq
)
220 if (I
.CurrentVer().Downloadable() == true)
224 // Is this right? Will dpkg choke on an upgrade?
225 if (Cache
[I
].CandidateVerIter(Cache
).Downloadable() == true)
226 Cache
.MarkInstall(I
);
228 return _error
->Error(_("The package %s needs to be reinstalled, "
229 "but I can't find an archive for it."),I
.Name());
235 switch (I
->CurrentState
)
237 /* This means installation failed somehow - it does not need to be
238 re-unpacked (probably) */
239 case pkgCache::State::UnPacked
:
240 case pkgCache::State::HalfConfigured
:
241 if (I
.CurrentVer().Downloadable() == true ||
242 I
.State() != pkgCache::PkgIterator::NeedsUnpack
)
246 if (Cache
[I
].CandidateVerIter(Cache
).Downloadable() == true)
247 Cache
.MarkInstall(I
);
253 // This means removal failed
254 case pkgCache::State::HalfInstalled
:
259 if (I
->InstState
!= pkgCache::State::Ok
)
260 return _error
->Error("The package %s is not ok and I "
261 "don't know how to fix it!",I
.Name());
267 // FixBroken - Fix broken packages /*{{{*/
268 // ---------------------------------------------------------------------
269 /* This autoinstalls every broken package and then runs the problem resolver
271 bool pkgFixBroken(pkgDepCache
&Cache
)
273 // Auto upgrade all broken packages
274 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
275 if (Cache
[I
].NowBroken() == true)
276 Cache
.MarkInstall(I
,true);
278 /* Fix packages that are in a NeedArchive state but don't have a
279 downloadable install version */
280 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
282 if (I
.State() != pkgCache::PkgIterator::NeedsUnpack
||
283 Cache
[I
].Delete() == true)
286 if (Cache
[I
].InstVerIter(Cache
).Downloadable() == false)
289 Cache
.MarkInstall(I
,true);
292 pkgProblemResolver
Fix(&Cache
);
293 return Fix
.Resolve(true);
296 // DistUpgrade - Distribution upgrade /*{{{*/
297 // ---------------------------------------------------------------------
298 /* This autoinstalls every package and then force installs every
299 pre-existing package. This creates the initial set of conditions which
300 most likely contain problems because too many things were installed.
302 The problem resolver is used to resolve the problems.
304 bool pkgDistUpgrade(pkgDepCache
&Cache
)
306 /* Auto upgrade all installed packages, this provides the basis
307 for the installation */
308 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
309 if (I
->CurrentVer
!= 0)
310 Cache
.MarkInstall(I
,true);
312 /* Now, auto upgrade all essential packages - this ensures that
313 the essential packages are present and working */
314 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
315 if ((I
->Flags
& pkgCache::Flag::Essential
) == pkgCache::Flag::Essential
)
316 Cache
.MarkInstall(I
,true);
318 /* We do it again over all previously installed packages to force
319 conflict resolution on them all. */
320 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
321 if (I
->CurrentVer
!= 0)
322 Cache
.MarkInstall(I
,false);
324 pkgProblemResolver
Fix(&Cache
);
326 // Hold back held packages.
327 if (_config
->FindB("APT::Ignore-Hold",false) == false)
329 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
331 if (I
->SelectedState
== pkgCache::State::Hold
)
339 return Fix
.Resolve();
342 // AllUpgrade - Upgrade as many packages as possible /*{{{*/
343 // ---------------------------------------------------------------------
344 /* Right now the system must be consistent before this can be called.
345 It also will not change packages marked for install, it only tries
346 to install packages not marked for install */
347 bool pkgAllUpgrade(pkgDepCache
&Cache
)
349 pkgProblemResolver
Fix(&Cache
);
351 if (Cache
.BrokenCount() != 0)
354 // Upgrade all installed packages
355 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
357 if (Cache
[I
].Install() == true)
360 if (_config
->FindB("APT::Ignore-Hold",false) == false)
361 if (I
->SelectedState
== pkgCache::State::Hold
)
364 if (I
->CurrentVer
!= 0 && Cache
[I
].InstallVer
!= 0)
365 Cache
.MarkInstall(I
,false);
368 return Fix
.ResolveByKeep();
371 // MinimizeUpgrade - Minimizes the set of packages to be upgraded /*{{{*/
372 // ---------------------------------------------------------------------
373 /* This simply goes over the entire set of packages and tries to keep
374 each package marked for upgrade. If a conflict is generated then
375 the package is restored. */
376 bool pkgMinimizeUpgrade(pkgDepCache
&Cache
)
378 if (Cache
.BrokenCount() != 0)
381 // We loop indefinately to get the minimal set size.
383 unsigned int Count
= 0;
387 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
390 if (Cache
[I
].Upgrade() == false || Cache
[I
].NewInstall() == true)
393 // Keep it and see if that is OK
395 if (Cache
.BrokenCount() != 0)
396 Cache
.MarkInstall(I
,false);
399 // If keep didnt actually do anything then there was no change..
400 if (Cache
[I
].Upgrade() == false)
406 while (Change
== true && Count
< 10);
408 if (Cache
.BrokenCount() != 0)
409 return _error
->Error("Internal Error in pkgMinimizeUpgrade");
415 // ProblemResolver::pkgProblemResolver - Constructor /*{{{*/
416 // ---------------------------------------------------------------------
418 pkgProblemResolver::pkgProblemResolver(pkgDepCache
*pCache
) : Cache(*pCache
)
421 unsigned long Size
= Cache
.Head().PackageCount
;
422 Scores
= new signed short[Size
];
423 Flags
= new unsigned char[Size
];
424 memset(Flags
,0,sizeof(*Flags
)*Size
);
426 // Set debug to true to see its decision logic
427 Debug
= _config
->FindB("Debug::pkgProblemResolver",false);
430 // ProblemResolver::~pkgProblemResolver - Destructor /*{{{*/
431 // ---------------------------------------------------------------------
433 pkgProblemResolver::~pkgProblemResolver()
439 // ProblemResolver::ScoreSort - Sort the list by score /*{{{*/
440 // ---------------------------------------------------------------------
442 int pkgProblemResolver::ScoreSort(const void *a
,const void *b
)
444 Package
const **A
= (Package
const **)a
;
445 Package
const **B
= (Package
const **)b
;
446 if (This
->Scores
[(*A
)->ID
] > This
->Scores
[(*B
)->ID
])
448 if (This
->Scores
[(*A
)->ID
] < This
->Scores
[(*B
)->ID
])
453 // ProblemResolver::MakeScores - Make the score table /*{{{*/
454 // ---------------------------------------------------------------------
456 void pkgProblemResolver::MakeScores()
458 unsigned long Size
= Cache
.Head().PackageCount
;
459 memset(Scores
,0,sizeof(*Scores
)*Size
);
461 // Generate the base scores for a package based on its properties
462 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
464 if (Cache
[I
].InstallVer
== 0)
467 signed short &Score
= Scores
[I
->ID
];
469 /* This is arbitary, it should be high enough to elevate an
470 essantial package above most other packages but low enough
471 to allow an obsolete essential packages to be removed by
472 a conflicts on a powerfull normal package (ie libc6) */
473 if ((I
->Flags
& pkgCache::Flag::Essential
) == pkgCache::Flag::Essential
)
476 // We transform the priority
477 // Important Required Standard Optional Extra
478 signed short PrioMap
[] = {0,3,2,1,-1,-2};
479 if (Cache
[I
].InstVerIter(Cache
)->Priority
<= 5)
480 Score
+= PrioMap
[Cache
[I
].InstVerIter(Cache
)->Priority
];
482 /* This helps to fix oddball problems with conflicting packages
483 on the same level. We enhance the score of installed packages */
484 if (I
->CurrentVer
!= 0)
488 // Now that we have the base scores we go and propogate dependencies
489 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
491 if (Cache
[I
].InstallVer
== 0)
494 for (pkgCache::DepIterator D
= Cache
[I
].InstVerIter(Cache
).DependsList(); D
.end() == false; D
++)
496 if (D
->Type
== pkgCache::Dep::Depends
|| D
->Type
== pkgCache::Dep::PreDepends
)
497 Scores
[D
.TargetPkg()->ID
]++;
501 // Copy the scores to advoid additive looping
502 SPtrArray
<signed short> OldScores
= new signed short[Size
];
503 memcpy(OldScores
,Scores
,sizeof(*Scores
)*Size
);
505 /* Now we cause 1 level of dependency inheritance, that is we add the
506 score of the packages that depend on the target Package. This
507 fortifies high scoring packages */
508 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
510 if (Cache
[I
].InstallVer
== 0)
513 for (pkgCache::DepIterator D
= I
.RevDependsList(); D
.end() == false; D
++)
515 // Only do it for the install version
516 if ((pkgCache::Version
*)D
.ParentVer() != Cache
[D
.ParentPkg()].InstallVer
||
517 (D
->Type
!= pkgCache::Dep::Depends
&& D
->Type
!= pkgCache::Dep::PreDepends
))
520 Scores
[I
->ID
] += abs(OldScores
[D
.ParentPkg()->ID
]);
524 /* Now we propogate along provides. This makes the packages that
525 provide important packages extremely important */
526 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
528 for (pkgCache::PrvIterator P
= I
.ProvidesList(); P
.end() == false; P
++)
530 // Only do it once per package
531 if ((pkgCache::Version
*)P
.OwnerVer() != Cache
[P
.OwnerPkg()].InstallVer
)
533 Scores
[P
.OwnerPkg()->ID
] += abs(Scores
[I
->ID
] - OldScores
[I
->ID
]);
537 /* Protected things are pushed really high up. This number should put them
538 ahead of everything */
539 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
541 if ((Flags
[I
->ID
] & Protected
) != 0)
542 Scores
[I
->ID
] += 10000;
543 if ((I
->Flags
& pkgCache::Flag::Essential
) == pkgCache::Flag::Essential
)
544 Scores
[I
->ID
] += 5000;
548 // ProblemResolver::DoUpgrade - Attempt to upgrade this package /*{{{*/
549 // ---------------------------------------------------------------------
550 /* This goes through and tries to reinstall packages to make this package
552 bool pkgProblemResolver::DoUpgrade(pkgCache::PkgIterator Pkg
)
554 if ((Flags
[Pkg
->ID
] & Upgradable
) == 0 || Cache
[Pkg
].Upgradable() == false)
557 Flags
[Pkg
->ID
] &= ~Upgradable
;
559 bool WasKept
= Cache
[Pkg
].Keep();
560 Cache
.MarkInstall(Pkg
,false);
562 // This must be a virtual package or something like that.
563 if (Cache
[Pkg
].InstVerIter(Cache
).end() == true)
566 // Isolate the problem dependency
568 for (pkgCache::DepIterator D
= Cache
[Pkg
].InstVerIter(Cache
).DependsList(); D
.end() == false;)
570 // Compute a single dependency element (glob or)
571 pkgCache::DepIterator Start
= D
;
572 pkgCache::DepIterator End
= D
;
573 unsigned char State
= 0;
574 for (bool LastOR
= true; D
.end() == false && LastOR
== true;)
577 LastOR
= (D
->CompareOp
& pkgCache::Dep::Or
) == pkgCache::Dep::Or
;
583 // We only worry about critical deps.
584 if (End
.IsCritical() != true)
587 // Iterate over all the members in the or group
591 if ((Cache
[End
] & pkgDepCache::DepGInstall
) == pkgDepCache::DepGInstall
)
594 // Do not change protected packages
595 PkgIterator P
= Start
.SmartTargetPkg();
596 if ((Flags
[P
->ID
] & Protected
) == Protected
)
599 clog
<< " Reinst Failed because of protected " << P
.Name() << endl
;
604 // Upgrade the package if the candidate version will fix the problem.
605 if ((Cache
[Start
] & pkgDepCache::DepCVer
) == pkgDepCache::DepCVer
)
607 if (DoUpgrade(P
) == false)
610 clog
<< " Reinst Failed because of " << P
.Name() << endl
;
621 /* We let the algorithm deal with conflicts on its next iteration,
622 it is much smarter than us */
623 if (Start
->Type
== pkgCache::Dep::Conflicts
||
624 Start
->Type
== pkgCache::Dep::Obsoletes
)
628 clog
<< " Reinst Failed early because of " << Start
.TargetPkg().Name() << endl
;
641 // Undo our operations - it might be smart to undo everything this did..
647 Cache
.MarkDelete(Pkg
);
652 clog
<< " Re-Instated " << Pkg
.Name() << endl
;
656 // ProblemResolver::Resolve - Run the resolution pass /*{{{*/
657 // ---------------------------------------------------------------------
658 /* This routines works by calculating a score for each package. The score
659 is derived by considering the package's priority and all reverse
660 dependents giving an integer that reflects the amount of breakage that
661 adjusting the package will inflict.
663 It goes from highest score to lowest and corrects all of the breaks by
664 keeping or removing the dependant packages. If that fails then it removes
665 the package itself and goes on. The routine should be able to intelligently
666 go from any broken state to a fixed state.
668 The BrokenFix flag enables a mode where the algorithm tries to
669 upgrade packages to advoid problems. */
670 bool pkgProblemResolver::Resolve(bool BrokenFix
)
672 unsigned long Size
= Cache
.Head().PackageCount
;
674 // Record which packages are marked for install
679 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
681 if (Cache
[I
].Install() == true)
682 Flags
[I
->ID
] |= PreInstalled
;
685 if (Cache
[I
].InstBroken() == true && BrokenFix
== true)
687 Cache
.MarkInstall(I
,false);
688 if (Cache
[I
].Install() == true)
692 Flags
[I
->ID
] &= ~PreInstalled
;
694 Flags
[I
->ID
] |= Upgradable
;
697 while (Again
== true);
700 clog
<< "Starting" << endl
;
704 /* We have to order the packages so that the broken fixing pass
705 operates from highest score to lowest. This prevents problems when
706 high score packages cause the removal of lower score packages that
707 would cause the removal of even lower score packages. */
708 SPtrArray
<pkgCache::Package
*> PList
= new pkgCache::Package
*[Size
];
709 pkgCache::Package
**PEnd
= PList
;
710 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
713 qsort(PList
,PEnd
- PList
,sizeof(*PList
),&ScoreSort
);
715 /* for (pkgCache::Package **K = PList; K != PEnd; K++)
716 if (Scores[(*K)->ID] != 0)
718 pkgCache::PkgIterator Pkg(Cache,*K);
719 clog << Scores[(*K)->ID] << ' ' << Pkg.Name() <<
720 ' ' << (pkgCache::Version *)Pkg.CurrentVer() << ' ' <<
721 Cache[Pkg].InstallVer << ' ' << Cache[Pkg].CandidateVer << endl;
725 clog
<< "Starting 2" << endl
;
727 /* Now consider all broken packages. For each broken package we either
728 remove the package or fix it's problem. We do this once, it should
729 not be possible for a loop to form (that is a < b < c and fixing b by
730 changing a breaks c) */
732 for (int Counter
= 0; Counter
!= 10 && Change
== true; Counter
++)
735 for (pkgCache::Package
**K
= PList
; K
!= PEnd
; K
++)
737 pkgCache::PkgIterator
I(Cache
,*K
);
739 /* We attempt to install this and see if any breaks result,
740 this takes care of some strange cases */
741 if (Cache
[I
].CandidateVer
!= Cache
[I
].InstallVer
&&
742 I
->CurrentVer
!= 0 && Cache
[I
].InstallVer
!= 0 &&
743 (Flags
[I
->ID
] & PreInstalled
) != 0 &&
744 (Flags
[I
->ID
] & Protected
) == 0 &&
745 (Flags
[I
->ID
] & ReInstateTried
) == 0)
748 clog
<< " Try to Re-Instate " << I
.Name() << endl
;
749 unsigned long OldBreaks
= Cache
.BrokenCount();
750 pkgCache::Version
*OldVer
= Cache
[I
].InstallVer
;
751 Flags
[I
->ID
] &= ReInstateTried
;
753 Cache
.MarkInstall(I
,false);
754 if (Cache
[I
].InstBroken() == true ||
755 OldBreaks
< Cache
.BrokenCount())
764 clog
<< "Re-Instated " << I
.Name() << " (" << OldBreaks
<< " vs " << Cache
.BrokenCount() << ')' << endl
;
767 if (Cache
[I
].InstallVer
== 0 || Cache
[I
].InstBroken() == false)
771 cout
<< "Investigating " << I
.Name() << endl
;
773 // Isolate the problem dependency
774 PackageKill KillList
[100];
775 PackageKill
*LEnd
= KillList
;
777 pkgCache::DepIterator Start
;
778 pkgCache::DepIterator End
;
779 PackageKill
*OldEnd
= LEnd
;
781 enum {OrRemove
,OrKeep
} OrOp
= OrRemove
;
782 for (pkgCache::DepIterator D
= Cache
[I
].InstVerIter(Cache
).DependsList();
783 D
.end() == false || InOr
== true;)
785 // Compute a single dependency element (glob or)
791 if (OldEnd
== LEnd
&& OrOp
== OrRemove
)
793 if ((Flags
[I
->ID
] & Protected
) != Protected
)
796 clog
<< " Or group remove for " << I
.Name() << endl
;
800 if (OldEnd
== LEnd
&& OrOp
== OrKeep
)
803 clog
<< " Or group keep for " << I
.Name() << endl
;
808 /* We do an extra loop (as above) to finalize the or group
813 if (Start
.end() == true)
816 // We only worry about critical deps.
817 if (End
.IsCritical() != true)
827 if ((Cache
[End
] & pkgDepCache::DepGInstall
) == pkgDepCache::DepGInstall
)
831 clog
<< "Package " << I
.Name() << " has broken dep on " << Start
.TargetPkg().Name() << endl
;
833 /* Look across the version list. If there are no possible
834 targets then we keep the package and bail. This is necessary
835 if a package has a dep on another package that cant be found */
836 SPtrArray
<pkgCache::Version
*> VList
= Start
.AllTargets();
837 if (*VList
== 0 && (Flags
[I
->ID
] & Protected
) != Protected
&&
838 Start
->Type
!= pkgCache::Dep::Conflicts
&&
839 Start
->Type
!= pkgCache::Dep::Obsoletes
&&
840 Cache
[I
].NowBroken() == false)
844 /* No keep choice because the keep being OK could be the
845 result of another element in the OR group! */
855 for (pkgCache::Version
**V
= VList
; *V
!= 0; V
++)
857 pkgCache::VerIterator
Ver(Cache
,*V
);
858 pkgCache::PkgIterator Pkg
= Ver
.ParentPkg();
861 clog
<< " Considering " << Pkg
.Name() << ' ' << (int)Scores
[Pkg
->ID
] <<
862 " as a solution to " << I
.Name() << ' ' << (int)Scores
[I
->ID
] << endl
;
863 if (Scores
[I
->ID
] <= Scores
[Pkg
->ID
] ||
864 ((Cache
[Start
] & pkgDepCache::DepNow
) == 0 &&
865 End
->Type
!= pkgCache::Dep::Conflicts
&&
866 End
->Type
!= pkgCache::Dep::Obsoletes
))
868 // Try a little harder to fix protected packages..
869 if ((Flags
[I
->ID
] & Protected
) == Protected
)
871 if (DoUpgrade(Pkg
) == true)
873 if (Scores
[Pkg
->ID
] > Scores
[I
->ID
])
874 Scores
[Pkg
->ID
] = Scores
[I
->ID
];
881 /* See if a keep will do, unless the package is protected,
882 then installing it will be necessary */
883 bool Installed
= Cache
[I
].Install();
885 if (Cache
[I
].InstBroken() == false)
887 // Unwind operation will be keep now
888 if (OrOp
== OrRemove
)
892 if (InOr
== true && Installed
== true)
893 Cache
.MarkInstall(I
,false);
896 clog
<< " Holding Back " << I
.Name() << " rather than change " << Start
.TargetPkg().Name() << endl
;
900 if (BrokenFix
== false || DoUpgrade(I
) == false)
902 // Consider other options
906 clog
<< " Removing " << I
.Name() << " rather than change " << Start
.TargetPkg().Name() << endl
;
910 if (Scores
[Pkg
->ID
] > Scores
[I
->ID
])
911 Scores
[I
->ID
] = Scores
[Pkg
->ID
];
924 clog
<< " Added " << Pkg
.Name() << " to the remove list" << endl
;
925 // Skip adding to the kill list if it is protected
926 if ((Flags
[Pkg
->ID
] & Protected
) != 0)
933 if (Start
->Type
!= pkgCache::Dep::Conflicts
&&
934 Start
->Type
!= pkgCache::Dep::Obsoletes
)
939 // Hm, nothing can possibly satisify this dep. Nuke it.
941 Start
->Type
!= pkgCache::Dep::Conflicts
&&
942 Start
->Type
!= pkgCache::Dep::Obsoletes
&&
943 (Flags
[I
->ID
] & Protected
) != Protected
)
945 bool Installed
= Cache
[I
].Install();
947 if (Cache
[I
].InstBroken() == false)
949 // Unwind operation will be keep now
950 if (OrOp
== OrRemove
)
954 if (InOr
== true && Installed
== true)
955 Cache
.MarkInstall(I
,false);
958 clog
<< " Holding Back " << I
.Name() << " because I can't find " << Start
.TargetPkg().Name() << endl
;
963 clog
<< " Removing " << I
.Name() << " because I can't find " << Start
.TargetPkg().Name() << endl
;
980 // Apply the kill list now
981 if (Cache
[I
].InstallVer
!= 0)
983 for (PackageKill
*J
= KillList
; J
!= LEnd
; J
++)
986 if ((Cache
[J
->Dep
] & pkgDepCache::DepGNow
) == 0)
988 if (J
->Dep
->Type
== pkgCache::Dep::Conflicts
||
989 J
->Dep
->Type
== pkgCache::Dep::Obsoletes
)
992 clog
<< " Fixing " << I
.Name() << " via remove of " << J
->Pkg
.Name() << endl
;
993 Cache
.MarkDelete(J
->Pkg
);
999 clog
<< " Fixing " << I
.Name() << " via keep of " << J
->Pkg
.Name() << endl
;
1000 Cache
.MarkKeep(J
->Pkg
);
1005 if (Scores
[I
->ID
] > Scores
[J
->Pkg
->ID
])
1006 Scores
[J
->Pkg
->ID
] = Scores
[I
->ID
];
1014 clog
<< "Done" << endl
;
1016 if (Cache
.BrokenCount() != 0)
1018 // See if this is the result of a hold
1019 pkgCache::PkgIterator I
= Cache
.PkgBegin();
1020 for (;I
.end() != true; I
++)
1022 if (Cache
[I
].InstBroken() == false)
1024 if ((Flags
[I
->ID
] & Protected
) != Protected
)
1025 return _error
->Error(_("Error, pkgProblemResolver::Resolve generated breaks, this may be caused by held packages."));
1027 return _error
->Error(_("Unable to correct problems, you have held broken packages."));
1033 // ProblemResolver::ResolveByKeep - Resolve problems using keep /*{{{*/
1034 // ---------------------------------------------------------------------
1035 /* This is the work horse of the soft upgrade routine. It is very gental
1036 in that it does not install or remove any packages. It is assumed that the
1037 system was non-broken previously. */
1038 bool pkgProblemResolver::ResolveByKeep()
1040 unsigned long Size
= Cache
.Head().PackageCount
;
1043 clog
<< "Entering ResolveByKeep" << endl
;
1047 /* We have to order the packages so that the broken fixing pass
1048 operates from highest score to lowest. This prevents problems when
1049 high score packages cause the removal of lower score packages that
1050 would cause the removal of even lower score packages. */
1051 pkgCache::Package
**PList
= new pkgCache::Package
*[Size
];
1052 pkgCache::Package
**PEnd
= PList
;
1053 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
1056 qsort(PList
,PEnd
- PList
,sizeof(*PList
),&ScoreSort
);
1058 // Consider each broken package
1059 pkgCache::Package
**LastStop
= 0;
1060 for (pkgCache::Package
**K
= PList
; K
!= PEnd
; K
++)
1062 pkgCache::PkgIterator
I(Cache
,*K
);
1064 if (Cache
[I
].InstallVer
== 0 || Cache
[I
].InstBroken() == false)
1067 /* Keep the package. If this works then great, otherwise we have
1068 to be significantly more agressive and manipulate its dependencies */
1069 if ((Flags
[I
->ID
] & Protected
) == 0)
1072 clog
<< "Keeping package " << I
.Name() << endl
;
1074 if (Cache
[I
].InstBroken() == false)
1081 // Isolate the problem dependencies
1082 for (pkgCache::DepIterator D
= Cache
[I
].InstVerIter(Cache
).DependsList(); D
.end() == false;)
1084 // Compute a single dependency element (glob or)
1085 pkgCache::DepIterator Start
= D
;
1086 pkgCache::DepIterator End
= D
;
1087 unsigned char State
= 0;
1088 for (bool LastOR
= true; D
.end() == false && LastOR
== true; D
++)
1091 LastOR
= (D
->CompareOp
& pkgCache::Dep::Or
) == pkgCache::Dep::Or
;
1096 // We only worry about critical deps.
1097 if (End
.IsCritical() != true)
1101 if ((Cache
[End
] & pkgDepCache::DepGInstall
) == pkgDepCache::DepGInstall
)
1104 // Hm, the group is broken.. I have no idea how to handle this
1107 clog
<< "Note, a broken or group was found in " << I
.Name() << "." << endl
;
1108 if ((Flags
[I
->ID
] & Protected
) == 0)
1114 clog
<< "Package " << I
.Name() << " has broken dep on " << End
.TargetPkg().Name() << endl
;
1116 // Look at all the possible provides on this package
1117 SPtrArray
<pkgCache::Version
*> VList
= End
.AllTargets();
1118 for (pkgCache::Version
**V
= VList
; *V
!= 0; V
++)
1120 pkgCache::VerIterator
Ver(Cache
,*V
);
1121 pkgCache::PkgIterator Pkg
= Ver
.ParentPkg();
1123 // It is not keepable
1124 if (Cache
[Pkg
].InstallVer
== 0 ||
1125 Pkg
->CurrentVer
== 0)
1128 if ((Flags
[I
->ID
] & Protected
) == 0)
1131 clog
<< " Keeping Package " << Pkg
.Name() << " due to dep" << endl
;
1132 Cache
.MarkKeep(Pkg
);
1135 if (Cache
[I
].InstBroken() == false)
1139 if (Cache
[I
].InstBroken() == false)
1143 if (Cache
[I
].InstBroken() == true)
1148 return _error
->Error("Internal Error, pkgProblemResolver::ResolveByKeep is looping on package %s.",I
.Name());
1156 // ProblemResolver::InstallProtect - Install all protected packages /*{{{*/
1157 // ---------------------------------------------------------------------
1158 /* This is used to make sure protected packages are installed */
1159 void pkgProblemResolver::InstallProtect()
1161 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
1163 if ((Flags
[I
->ID
] & Protected
) == Protected
)
1165 if ((Flags
[I
->ID
] & ToRemove
) == ToRemove
)
1166 Cache
.MarkDelete(I
);
1168 Cache
.MarkInstall(I
,false);
1174 // PrioSortList - Sort a list of versions by priority /*{{{*/
1175 // ---------------------------------------------------------------------
1176 /* This is ment to be used in conjunction with AllTargets to get a list
1177 of versions ordered by preference. */
1178 static pkgCache
*PrioCache
;
1179 static int PrioComp(const void *A
,const void *B
)
1181 pkgCache::VerIterator
L(*PrioCache
,*(pkgCache::Version
**)A
);
1182 pkgCache::VerIterator
R(*PrioCache
,*(pkgCache::Version
**)B
);
1184 if ((L
.ParentPkg()->Flags
& pkgCache::Flag::Essential
) == pkgCache::Flag::Essential
&&
1185 (L
.ParentPkg()->Flags
& pkgCache::Flag::Essential
) != pkgCache::Flag::Essential
)
1187 if ((L
.ParentPkg()->Flags
& pkgCache::Flag::Essential
) != pkgCache::Flag::Essential
&&
1188 (L
.ParentPkg()->Flags
& pkgCache::Flag::Essential
) == pkgCache::Flag::Essential
)
1191 if (L
->Priority
!= R
->Priority
)
1192 return L
->Priority
- R
->Priority
;
1193 return strcmp(L
.ParentPkg().Name(),R
.ParentPkg().Name());
1195 void pkgPrioSortList(pkgCache
&Cache
,pkgCache::Version
**List
)
1197 unsigned long Count
= 0;
1199 for (pkgCache::Version
**I
= List
; *I
!= 0; I
++)
1201 qsort(List
,Count
,sizeof(*List
),PrioComp
);