]>
git.saurik.com Git - apt.git/blob - apt-pkg/algorithms.cc
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: algorithms.cc,v 1.36 2001/05/27 03:46:10 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 if (I
->VersionList
== 0)
219 // Only choice for a ReInstReq package is to reinstall
220 if (I
->InstState
== pkgCache::State::ReInstReq
||
221 I
->InstState
== pkgCache::State::HoldReInstReq
)
223 if (I
->CurrentVer
!= 0 && I
.CurrentVer().Downloadable() == true)
227 // Is this right? Will dpkg choke on an upgrade?
228 if (Cache
[I
].CandidateVerIter(Cache
).Downloadable() == true)
229 Cache
.MarkInstall(I
);
231 return _error
->Error(_("The package %s needs to be reinstalled, "
232 "but I can't find an archive for it."),I
.Name());
238 switch (I
->CurrentState
)
240 /* This means installation failed somehow - it does not need to be
241 re-unpacked (probably) */
242 case pkgCache::State::UnPacked
:
243 case pkgCache::State::HalfConfigured
:
244 if ((I
->CurrentVer
!= 0 && I
.CurrentVer().Downloadable() == true) ||
245 I
.State() != pkgCache::PkgIterator::NeedsUnpack
)
249 if (Cache
[I
].CandidateVerIter(Cache
).Downloadable() == true)
250 Cache
.MarkInstall(I
);
256 // This means removal failed
257 case pkgCache::State::HalfInstalled
:
262 if (I
->InstState
!= pkgCache::State::Ok
)
263 return _error
->Error("The package %s is not ok and I "
264 "don't know how to fix it!",I
.Name());
270 // FixBroken - Fix broken packages /*{{{*/
271 // ---------------------------------------------------------------------
272 /* This autoinstalls every broken package and then runs the problem resolver
274 bool pkgFixBroken(pkgDepCache
&Cache
)
276 // Auto upgrade all broken packages
277 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
278 if (Cache
[I
].NowBroken() == true)
279 Cache
.MarkInstall(I
,true);
281 /* Fix packages that are in a NeedArchive state but don't have a
282 downloadable install version */
283 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
285 if (I
.State() != pkgCache::PkgIterator::NeedsUnpack
||
286 Cache
[I
].Delete() == true)
289 if (Cache
[I
].InstVerIter(Cache
).Downloadable() == false)
292 Cache
.MarkInstall(I
,true);
295 pkgProblemResolver
Fix(&Cache
);
296 return Fix
.Resolve(true);
299 // DistUpgrade - Distribution upgrade /*{{{*/
300 // ---------------------------------------------------------------------
301 /* This autoinstalls every package and then force installs every
302 pre-existing package. This creates the initial set of conditions which
303 most likely contain problems because too many things were installed.
305 The problem resolver is used to resolve the problems.
307 bool pkgDistUpgrade(pkgDepCache
&Cache
)
309 /* Auto upgrade all installed packages, this provides the basis
310 for the installation */
311 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
312 if (I
->CurrentVer
!= 0)
313 Cache
.MarkInstall(I
,true);
315 /* Now, auto upgrade all essential packages - this ensures that
316 the essential packages are present and working */
317 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
318 if ((I
->Flags
& pkgCache::Flag::Essential
) == pkgCache::Flag::Essential
)
319 Cache
.MarkInstall(I
,true);
321 /* We do it again over all previously installed packages to force
322 conflict resolution on them all. */
323 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
324 if (I
->CurrentVer
!= 0)
325 Cache
.MarkInstall(I
,false);
327 pkgProblemResolver
Fix(&Cache
);
329 // Hold back held packages.
330 if (_config
->FindB("APT::Ignore-Hold",false) == false)
332 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
334 if (I
->SelectedState
== pkgCache::State::Hold
)
342 return Fix
.Resolve();
345 // AllUpgrade - Upgrade as many packages as possible /*{{{*/
346 // ---------------------------------------------------------------------
347 /* Right now the system must be consistent before this can be called.
348 It also will not change packages marked for install, it only tries
349 to install packages not marked for install */
350 bool pkgAllUpgrade(pkgDepCache
&Cache
)
352 pkgProblemResolver
Fix(&Cache
);
354 if (Cache
.BrokenCount() != 0)
357 // Upgrade all installed packages
358 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
360 if (Cache
[I
].Install() == true)
363 if (_config
->FindB("APT::Ignore-Hold",false) == false)
364 if (I
->SelectedState
== pkgCache::State::Hold
)
367 if (I
->CurrentVer
!= 0 && Cache
[I
].InstallVer
!= 0)
368 Cache
.MarkInstall(I
,false);
371 return Fix
.ResolveByKeep();
374 // MinimizeUpgrade - Minimizes the set of packages to be upgraded /*{{{*/
375 // ---------------------------------------------------------------------
376 /* This simply goes over the entire set of packages and tries to keep
377 each package marked for upgrade. If a conflict is generated then
378 the package is restored. */
379 bool pkgMinimizeUpgrade(pkgDepCache
&Cache
)
381 if (Cache
.BrokenCount() != 0)
384 // We loop for 10 tries to get the minimal set size.
386 unsigned int Count
= 0;
390 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
393 if (Cache
[I
].Upgrade() == false || Cache
[I
].NewInstall() == true)
396 // Keep it and see if that is OK
398 if (Cache
.BrokenCount() != 0)
399 Cache
.MarkInstall(I
,false);
402 // If keep didnt actually do anything then there was no change..
403 if (Cache
[I
].Upgrade() == false)
409 while (Change
== true && Count
< 10);
411 if (Cache
.BrokenCount() != 0)
412 return _error
->Error("Internal Error in pkgMinimizeUpgrade");
418 // ProblemResolver::pkgProblemResolver - Constructor /*{{{*/
419 // ---------------------------------------------------------------------
421 pkgProblemResolver::pkgProblemResolver(pkgDepCache
*pCache
) : Cache(*pCache
)
424 unsigned long Size
= Cache
.Head().PackageCount
;
425 Scores
= new signed short[Size
];
426 Flags
= new unsigned char[Size
];
427 memset(Flags
,0,sizeof(*Flags
)*Size
);
429 // Set debug to true to see its decision logic
430 Debug
= _config
->FindB("Debug::pkgProblemResolver",false);
433 // ProblemResolver::~pkgProblemResolver - Destructor /*{{{*/
434 // ---------------------------------------------------------------------
436 pkgProblemResolver::~pkgProblemResolver()
442 // ProblemResolver::ScoreSort - Sort the list by score /*{{{*/
443 // ---------------------------------------------------------------------
445 int pkgProblemResolver::ScoreSort(const void *a
,const void *b
)
447 Package
const **A
= (Package
const **)a
;
448 Package
const **B
= (Package
const **)b
;
449 if (This
->Scores
[(*A
)->ID
] > This
->Scores
[(*B
)->ID
])
451 if (This
->Scores
[(*A
)->ID
] < This
->Scores
[(*B
)->ID
])
456 // ProblemResolver::MakeScores - Make the score table /*{{{*/
457 // ---------------------------------------------------------------------
459 void pkgProblemResolver::MakeScores()
461 unsigned long Size
= Cache
.Head().PackageCount
;
462 memset(Scores
,0,sizeof(*Scores
)*Size
);
464 // Generate the base scores for a package based on its properties
465 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
467 if (Cache
[I
].InstallVer
== 0)
470 signed short &Score
= Scores
[I
->ID
];
472 /* This is arbitary, it should be high enough to elevate an
473 essantial package above most other packages but low enough
474 to allow an obsolete essential packages to be removed by
475 a conflicts on a powerfull normal package (ie libc6) */
476 if ((I
->Flags
& pkgCache::Flag::Essential
) == pkgCache::Flag::Essential
)
479 // We transform the priority
480 // Important Required Standard Optional Extra
481 signed short PrioMap
[] = {0,3,2,1,-1,-2};
482 if (Cache
[I
].InstVerIter(Cache
)->Priority
<= 5)
483 Score
+= PrioMap
[Cache
[I
].InstVerIter(Cache
)->Priority
];
485 /* This helps to fix oddball problems with conflicting packages
486 on the same level. We enhance the score of installed packages */
487 if (I
->CurrentVer
!= 0)
491 // Now that we have the base scores we go and propogate dependencies
492 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
494 if (Cache
[I
].InstallVer
== 0)
497 for (pkgCache::DepIterator D
= Cache
[I
].InstVerIter(Cache
).DependsList(); D
.end() == false; D
++)
499 if (D
->Type
== pkgCache::Dep::Depends
|| D
->Type
== pkgCache::Dep::PreDepends
)
500 Scores
[D
.TargetPkg()->ID
]++;
504 // Copy the scores to advoid additive looping
505 SPtrArray
<signed short> OldScores
= new signed short[Size
];
506 memcpy(OldScores
,Scores
,sizeof(*Scores
)*Size
);
508 /* Now we cause 1 level of dependency inheritance, that is we add the
509 score of the packages that depend on the target Package. This
510 fortifies high scoring packages */
511 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
513 if (Cache
[I
].InstallVer
== 0)
516 for (pkgCache::DepIterator D
= I
.RevDependsList(); D
.end() == false; D
++)
518 // Only do it for the install version
519 if ((pkgCache::Version
*)D
.ParentVer() != Cache
[D
.ParentPkg()].InstallVer
||
520 (D
->Type
!= pkgCache::Dep::Depends
&& D
->Type
!= pkgCache::Dep::PreDepends
))
523 Scores
[I
->ID
] += abs(OldScores
[D
.ParentPkg()->ID
]);
527 /* Now we propogate along provides. This makes the packages that
528 provide important packages extremely important */
529 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
531 for (pkgCache::PrvIterator P
= I
.ProvidesList(); P
.end() == false; P
++)
533 // Only do it once per package
534 if ((pkgCache::Version
*)P
.OwnerVer() != Cache
[P
.OwnerPkg()].InstallVer
)
536 Scores
[P
.OwnerPkg()->ID
] += abs(Scores
[I
->ID
] - OldScores
[I
->ID
]);
540 /* Protected things are pushed really high up. This number should put them
541 ahead of everything */
542 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
544 if ((Flags
[I
->ID
] & Protected
) != 0)
545 Scores
[I
->ID
] += 10000;
546 if ((I
->Flags
& pkgCache::Flag::Essential
) == pkgCache::Flag::Essential
)
547 Scores
[I
->ID
] += 5000;
551 // ProblemResolver::DoUpgrade - Attempt to upgrade this package /*{{{*/
552 // ---------------------------------------------------------------------
553 /* This goes through and tries to reinstall packages to make this package
555 bool pkgProblemResolver::DoUpgrade(pkgCache::PkgIterator Pkg
)
557 if ((Flags
[Pkg
->ID
] & Upgradable
) == 0 || Cache
[Pkg
].Upgradable() == false)
560 Flags
[Pkg
->ID
] &= ~Upgradable
;
562 bool WasKept
= Cache
[Pkg
].Keep();
563 Cache
.MarkInstall(Pkg
,false);
565 // This must be a virtual package or something like that.
566 if (Cache
[Pkg
].InstVerIter(Cache
).end() == true)
569 // Isolate the problem dependency
571 for (pkgCache::DepIterator D
= Cache
[Pkg
].InstVerIter(Cache
).DependsList(); D
.end() == false;)
573 // Compute a single dependency element (glob or)
574 pkgCache::DepIterator Start
= D
;
575 pkgCache::DepIterator End
= D
;
576 unsigned char State
= 0;
577 for (bool LastOR
= true; D
.end() == false && LastOR
== true;)
580 LastOR
= (D
->CompareOp
& pkgCache::Dep::Or
) == pkgCache::Dep::Or
;
586 // We only worry about critical deps.
587 if (End
.IsCritical() != true)
590 // Iterate over all the members in the or group
594 if ((Cache
[End
] & pkgDepCache::DepGInstall
) == pkgDepCache::DepGInstall
)
597 // Do not change protected packages
598 PkgIterator P
= Start
.SmartTargetPkg();
599 if ((Flags
[P
->ID
] & Protected
) == Protected
)
602 clog
<< " Reinst Failed because of protected " << P
.Name() << endl
;
607 // Upgrade the package if the candidate version will fix the problem.
608 if ((Cache
[Start
] & pkgDepCache::DepCVer
) == pkgDepCache::DepCVer
)
610 if (DoUpgrade(P
) == false)
613 clog
<< " Reinst Failed because of " << P
.Name() << endl
;
624 /* We let the algorithm deal with conflicts on its next iteration,
625 it is much smarter than us */
626 if (Start
->Type
== pkgCache::Dep::Conflicts
||
627 Start
->Type
== pkgCache::Dep::Obsoletes
)
631 clog
<< " Reinst Failed early because of " << Start
.TargetPkg().Name() << endl
;
644 // Undo our operations - it might be smart to undo everything this did..
650 Cache
.MarkDelete(Pkg
);
655 clog
<< " Re-Instated " << Pkg
.Name() << endl
;
659 // ProblemResolver::Resolve - Run the resolution pass /*{{{*/
660 // ---------------------------------------------------------------------
661 /* This routines works by calculating a score for each package. The score
662 is derived by considering the package's priority and all reverse
663 dependents giving an integer that reflects the amount of breakage that
664 adjusting the package will inflict.
666 It goes from highest score to lowest and corrects all of the breaks by
667 keeping or removing the dependant packages. If that fails then it removes
668 the package itself and goes on. The routine should be able to intelligently
669 go from any broken state to a fixed state.
671 The BrokenFix flag enables a mode where the algorithm tries to
672 upgrade packages to advoid problems. */
673 bool pkgProblemResolver::Resolve(bool BrokenFix
)
675 unsigned long Size
= Cache
.Head().PackageCount
;
677 // Record which packages are marked for install
682 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
684 if (Cache
[I
].Install() == true)
685 Flags
[I
->ID
] |= PreInstalled
;
688 if (Cache
[I
].InstBroken() == true && BrokenFix
== true)
690 Cache
.MarkInstall(I
,false);
691 if (Cache
[I
].Install() == true)
695 Flags
[I
->ID
] &= ~PreInstalled
;
697 Flags
[I
->ID
] |= Upgradable
;
700 while (Again
== true);
703 clog
<< "Starting" << endl
;
707 /* We have to order the packages so that the broken fixing pass
708 operates from highest score to lowest. This prevents problems when
709 high score packages cause the removal of lower score packages that
710 would cause the removal of even lower score packages. */
711 SPtrArray
<pkgCache::Package
*> PList
= new pkgCache::Package
*[Size
];
712 pkgCache::Package
**PEnd
= PList
;
713 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
716 qsort(PList
,PEnd
- PList
,sizeof(*PList
),&ScoreSort
);
718 /* for (pkgCache::Package **K = PList; K != PEnd; K++)
719 if (Scores[(*K)->ID] != 0)
721 pkgCache::PkgIterator Pkg(Cache,*K);
722 clog << Scores[(*K)->ID] << ' ' << Pkg.Name() <<
723 ' ' << (pkgCache::Version *)Pkg.CurrentVer() << ' ' <<
724 Cache[Pkg].InstallVer << ' ' << Cache[Pkg].CandidateVer << endl;
728 clog
<< "Starting 2" << endl
;
730 /* Now consider all broken packages. For each broken package we either
731 remove the package or fix it's problem. We do this once, it should
732 not be possible for a loop to form (that is a < b < c and fixing b by
733 changing a breaks c) */
735 for (int Counter
= 0; Counter
!= 10 && Change
== true; Counter
++)
738 for (pkgCache::Package
**K
= PList
; K
!= PEnd
; K
++)
740 pkgCache::PkgIterator
I(Cache
,*K
);
742 /* We attempt to install this and see if any breaks result,
743 this takes care of some strange cases */
744 if (Cache
[I
].CandidateVer
!= Cache
[I
].InstallVer
&&
745 I
->CurrentVer
!= 0 && Cache
[I
].InstallVer
!= 0 &&
746 (Flags
[I
->ID
] & PreInstalled
) != 0 &&
747 (Flags
[I
->ID
] & Protected
) == 0 &&
748 (Flags
[I
->ID
] & ReInstateTried
) == 0)
751 clog
<< " Try to Re-Instate " << I
.Name() << endl
;
752 unsigned long OldBreaks
= Cache
.BrokenCount();
753 pkgCache::Version
*OldVer
= Cache
[I
].InstallVer
;
754 Flags
[I
->ID
] &= ReInstateTried
;
756 Cache
.MarkInstall(I
,false);
757 if (Cache
[I
].InstBroken() == true ||
758 OldBreaks
< Cache
.BrokenCount())
767 clog
<< "Re-Instated " << I
.Name() << " (" << OldBreaks
<< " vs " << Cache
.BrokenCount() << ')' << endl
;
770 if (Cache
[I
].InstallVer
== 0 || Cache
[I
].InstBroken() == false)
774 cout
<< "Investigating " << I
.Name() << endl
;
776 // Isolate the problem dependency
777 PackageKill KillList
[100];
778 PackageKill
*LEnd
= KillList
;
780 pkgCache::DepIterator Start
;
781 pkgCache::DepIterator End
;
782 PackageKill
*OldEnd
= LEnd
;
784 enum {OrRemove
,OrKeep
} OrOp
= OrRemove
;
785 for (pkgCache::DepIterator D
= Cache
[I
].InstVerIter(Cache
).DependsList();
786 D
.end() == false || InOr
== true;)
788 // Compute a single dependency element (glob or)
794 if (OldEnd
== LEnd
&& OrOp
== OrRemove
)
796 if ((Flags
[I
->ID
] & Protected
) != Protected
)
799 clog
<< " Or group remove for " << I
.Name() << endl
;
803 if (OldEnd
== LEnd
&& OrOp
== OrKeep
)
806 clog
<< " Or group keep for " << I
.Name() << endl
;
811 /* We do an extra loop (as above) to finalize the or group
816 if (Start
.end() == true)
819 // We only worry about critical deps.
820 if (End
.IsCritical() != true)
830 if ((Cache
[End
] & pkgDepCache::DepGInstall
) == pkgDepCache::DepGInstall
)
834 clog
<< "Package " << I
.Name() << " has broken dep on " << Start
.TargetPkg().Name() << endl
;
836 /* Look across the version list. If there are no possible
837 targets then we keep the package and bail. This is necessary
838 if a package has a dep on another package that cant be found */
839 SPtrArray
<pkgCache::Version
*> VList
= Start
.AllTargets();
840 if (*VList
== 0 && (Flags
[I
->ID
] & Protected
) != Protected
&&
841 Start
->Type
!= pkgCache::Dep::Conflicts
&&
842 Start
->Type
!= pkgCache::Dep::Obsoletes
&&
843 Cache
[I
].NowBroken() == false)
847 /* No keep choice because the keep being OK could be the
848 result of another element in the OR group! */
858 for (pkgCache::Version
**V
= VList
; *V
!= 0; V
++)
860 pkgCache::VerIterator
Ver(Cache
,*V
);
861 pkgCache::PkgIterator Pkg
= Ver
.ParentPkg();
864 clog
<< " Considering " << Pkg
.Name() << ' ' << (int)Scores
[Pkg
->ID
] <<
865 " as a solution to " << I
.Name() << ' ' << (int)Scores
[I
->ID
] << endl
;
867 /* Try to fix the package under consideration rather than
868 fiddle with the VList package */
869 if (Scores
[I
->ID
] <= Scores
[Pkg
->ID
] ||
870 ((Cache
[Start
] & pkgDepCache::DepNow
) == 0 &&
871 End
->Type
!= pkgCache::Dep::Conflicts
&&
872 End
->Type
!= pkgCache::Dep::Obsoletes
))
874 // Try a little harder to fix protected packages..
875 if ((Flags
[I
->ID
] & Protected
) == Protected
)
877 if (DoUpgrade(Pkg
) == true)
879 if (Scores
[Pkg
->ID
] > Scores
[I
->ID
])
880 Scores
[Pkg
->ID
] = Scores
[I
->ID
];
887 /* See if a keep will do, unless the package is protected,
888 then installing it will be necessary */
889 bool Installed
= Cache
[I
].Install();
891 if (Cache
[I
].InstBroken() == false)
893 // Unwind operation will be keep now
894 if (OrOp
== OrRemove
)
898 if (InOr
== true && Installed
== true)
899 Cache
.MarkInstall(I
,false);
902 clog
<< " Holding Back " << I
.Name() << " rather than change " << Start
.TargetPkg().Name() << endl
;
906 if (BrokenFix
== false || DoUpgrade(I
) == false)
908 // Consider other options
912 clog
<< " Removing " << I
.Name() << " rather than change " << Start
.TargetPkg().Name() << endl
;
916 if (Scores
[Pkg
->ID
] > Scores
[I
->ID
])
917 Scores
[I
->ID
] = Scores
[Pkg
->ID
];
929 /* This is a conflicts, and the version we are looking
930 at is not the currently selected version of the
931 package, which means it is not necessary to
933 if (Cache
[Pkg
].InstallVer
!= Ver
&&
934 (Start
->Type
== pkgCache::Dep::Conflicts
||
935 Start
->Type
== pkgCache::Dep::Obsoletes
))
938 // Skip adding to the kill list if it is protected
939 if ((Flags
[Pkg
->ID
] & Protected
) != 0)
943 clog
<< " Added " << Pkg
.Name() << " to the remove list" << endl
;
949 if (Start
->Type
!= pkgCache::Dep::Conflicts
&&
950 Start
->Type
!= pkgCache::Dep::Obsoletes
)
955 // Hm, nothing can possibly satisify this dep. Nuke it.
957 Start
->Type
!= pkgCache::Dep::Conflicts
&&
958 Start
->Type
!= pkgCache::Dep::Obsoletes
&&
959 (Flags
[I
->ID
] & Protected
) != Protected
)
961 bool Installed
= Cache
[I
].Install();
963 if (Cache
[I
].InstBroken() == false)
965 // Unwind operation will be keep now
966 if (OrOp
== OrRemove
)
970 if (InOr
== true && Installed
== true)
971 Cache
.MarkInstall(I
,false);
974 clog
<< " Holding Back " << I
.Name() << " because I can't find " << Start
.TargetPkg().Name() << endl
;
979 clog
<< " Removing " << I
.Name() << " because I can't find " << Start
.TargetPkg().Name() << endl
;
996 // Apply the kill list now
997 if (Cache
[I
].InstallVer
!= 0)
999 for (PackageKill
*J
= KillList
; J
!= LEnd
; J
++)
1002 if ((Cache
[J
->Dep
] & pkgDepCache::DepGNow
) == 0)
1004 if (J
->Dep
->Type
== pkgCache::Dep::Conflicts
||
1005 J
->Dep
->Type
== pkgCache::Dep::Obsoletes
)
1008 clog
<< " Fixing " << I
.Name() << " via remove of " << J
->Pkg
.Name() << endl
;
1009 Cache
.MarkDelete(J
->Pkg
);
1015 clog
<< " Fixing " << I
.Name() << " via keep of " << J
->Pkg
.Name() << endl
;
1016 Cache
.MarkKeep(J
->Pkg
);
1021 if (Scores
[I
->ID
] > Scores
[J
->Pkg
->ID
])
1022 Scores
[J
->Pkg
->ID
] = Scores
[I
->ID
];
1030 clog
<< "Done" << endl
;
1032 if (Cache
.BrokenCount() != 0)
1034 // See if this is the result of a hold
1035 pkgCache::PkgIterator I
= Cache
.PkgBegin();
1036 for (;I
.end() != true; I
++)
1038 if (Cache
[I
].InstBroken() == false)
1040 if ((Flags
[I
->ID
] & Protected
) != Protected
)
1041 return _error
->Error(_("Error, pkgProblemResolver::Resolve generated breaks, this may be caused by held packages."));
1043 return _error
->Error(_("Unable to correct problems, you have held broken packages."));
1049 // ProblemResolver::ResolveByKeep - Resolve problems using keep /*{{{*/
1050 // ---------------------------------------------------------------------
1051 /* This is the work horse of the soft upgrade routine. It is very gental
1052 in that it does not install or remove any packages. It is assumed that the
1053 system was non-broken previously. */
1054 bool pkgProblemResolver::ResolveByKeep()
1056 unsigned long Size
= Cache
.Head().PackageCount
;
1059 clog
<< "Entering ResolveByKeep" << endl
;
1063 /* We have to order the packages so that the broken fixing pass
1064 operates from highest score to lowest. This prevents problems when
1065 high score packages cause the removal of lower score packages that
1066 would cause the removal of even lower score packages. */
1067 pkgCache::Package
**PList
= new pkgCache::Package
*[Size
];
1068 pkgCache::Package
**PEnd
= PList
;
1069 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
1072 qsort(PList
,PEnd
- PList
,sizeof(*PList
),&ScoreSort
);
1074 // Consider each broken package
1075 pkgCache::Package
**LastStop
= 0;
1076 for (pkgCache::Package
**K
= PList
; K
!= PEnd
; K
++)
1078 pkgCache::PkgIterator
I(Cache
,*K
);
1080 if (Cache
[I
].InstallVer
== 0 || Cache
[I
].InstBroken() == false)
1083 /* Keep the package. If this works then great, otherwise we have
1084 to be significantly more agressive and manipulate its dependencies */
1085 if ((Flags
[I
->ID
] & Protected
) == 0)
1088 clog
<< "Keeping package " << I
.Name() << endl
;
1090 if (Cache
[I
].InstBroken() == false)
1097 // Isolate the problem dependencies
1098 for (pkgCache::DepIterator D
= Cache
[I
].InstVerIter(Cache
).DependsList(); D
.end() == false;)
1100 // Compute a single dependency element (glob or)
1101 pkgCache::DepIterator Start
= D
;
1102 pkgCache::DepIterator End
= D
;
1103 unsigned char State
= 0;
1104 for (bool LastOR
= true; D
.end() == false && LastOR
== true; D
++)
1107 LastOR
= (D
->CompareOp
& pkgCache::Dep::Or
) == pkgCache::Dep::Or
;
1112 // We only worry about critical deps.
1113 if (End
.IsCritical() != true)
1117 if ((Cache
[End
] & pkgDepCache::DepGInstall
) == pkgDepCache::DepGInstall
)
1120 // Hm, the group is broken.. I have no idea how to handle this
1123 clog
<< "Note, a broken or group was found in " << I
.Name() << "." << endl
;
1124 if ((Flags
[I
->ID
] & Protected
) == 0)
1130 clog
<< "Package " << I
.Name() << " has broken dep on " << End
.TargetPkg().Name() << endl
;
1132 // Look at all the possible provides on this package
1133 SPtrArray
<pkgCache::Version
*> VList
= End
.AllTargets();
1134 for (pkgCache::Version
**V
= VList
; *V
!= 0; V
++)
1136 pkgCache::VerIterator
Ver(Cache
,*V
);
1137 pkgCache::PkgIterator Pkg
= Ver
.ParentPkg();
1139 // It is not keepable
1140 if (Cache
[Pkg
].InstallVer
== 0 ||
1141 Pkg
->CurrentVer
== 0)
1144 if ((Flags
[I
->ID
] & Protected
) == 0)
1147 clog
<< " Keeping Package " << Pkg
.Name() << " due to dep" << endl
;
1148 Cache
.MarkKeep(Pkg
);
1151 if (Cache
[I
].InstBroken() == false)
1155 if (Cache
[I
].InstBroken() == false)
1159 if (Cache
[I
].InstBroken() == true)
1164 return _error
->Error("Internal Error, pkgProblemResolver::ResolveByKeep is looping on package %s.",I
.Name());
1172 // ProblemResolver::InstallProtect - Install all protected packages /*{{{*/
1173 // ---------------------------------------------------------------------
1174 /* This is used to make sure protected packages are installed */
1175 void pkgProblemResolver::InstallProtect()
1177 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
1179 if ((Flags
[I
->ID
] & Protected
) == Protected
)
1181 if ((Flags
[I
->ID
] & ToRemove
) == ToRemove
)
1182 Cache
.MarkDelete(I
);
1184 Cache
.MarkInstall(I
,false);
1190 // PrioSortList - Sort a list of versions by priority /*{{{*/
1191 // ---------------------------------------------------------------------
1192 /* This is ment to be used in conjunction with AllTargets to get a list
1193 of versions ordered by preference. */
1194 static pkgCache
*PrioCache
;
1195 static int PrioComp(const void *A
,const void *B
)
1197 pkgCache::VerIterator
L(*PrioCache
,*(pkgCache::Version
**)A
);
1198 pkgCache::VerIterator
R(*PrioCache
,*(pkgCache::Version
**)B
);
1200 if ((L
.ParentPkg()->Flags
& pkgCache::Flag::Essential
) == pkgCache::Flag::Essential
&&
1201 (L
.ParentPkg()->Flags
& pkgCache::Flag::Essential
) != pkgCache::Flag::Essential
)
1203 if ((L
.ParentPkg()->Flags
& pkgCache::Flag::Essential
) != pkgCache::Flag::Essential
&&
1204 (L
.ParentPkg()->Flags
& pkgCache::Flag::Essential
) == pkgCache::Flag::Essential
)
1207 if (L
->Priority
!= R
->Priority
)
1208 return L
->Priority
- R
->Priority
;
1209 return strcmp(L
.ParentPkg().Name(),R
.ParentPkg().Name());
1211 void pkgPrioSortList(pkgCache
&Cache
,pkgCache::Version
**List
)
1213 unsigned long Count
= 0;
1215 for (pkgCache::Version
**I
= List
; *I
!= 0; I
++)
1217 qsort(List
,Count
,sizeof(*List
),PrioComp
);