]>
git.saurik.com Git - apt.git/blob - apt-pkg/algorithms.cc
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: algorithms.cc,v 1.41 2002/04/26 05:36:20 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
].CandidateVer
!= 0 &&
229 Cache
[I
].CandidateVerIter(Cache
).Downloadable() == true)
230 Cache
.MarkInstall(I
);
232 return _error
->Error(_("The package %s needs to be reinstalled, "
233 "but I can't find an archive for it."),I
.Name());
239 switch (I
->CurrentState
)
241 /* This means installation failed somehow - it does not need to be
242 re-unpacked (probably) */
243 case pkgCache::State::UnPacked
:
244 case pkgCache::State::HalfConfigured
:
245 if ((I
->CurrentVer
!= 0 && I
.CurrentVer().Downloadable() == true) ||
246 I
.State() != pkgCache::PkgIterator::NeedsUnpack
)
250 if (Cache
[I
].CandidateVer
!= 0 &&
251 Cache
[I
].CandidateVerIter(Cache
).Downloadable() == true)
252 Cache
.MarkInstall(I
);
258 // This means removal failed
259 case pkgCache::State::HalfInstalled
:
264 if (I
->InstState
!= pkgCache::State::Ok
)
265 return _error
->Error("The package %s is not ok and I "
266 "don't know how to fix it!",I
.Name());
272 // FixBroken - Fix broken packages /*{{{*/
273 // ---------------------------------------------------------------------
274 /* This autoinstalls every broken package and then runs the problem resolver
276 bool pkgFixBroken(pkgDepCache
&Cache
)
278 // Auto upgrade all broken packages
279 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
280 if (Cache
[I
].NowBroken() == true)
281 Cache
.MarkInstall(I
,true);
283 /* Fix packages that are in a NeedArchive state but don't have a
284 downloadable install version */
285 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
287 if (I
.State() != pkgCache::PkgIterator::NeedsUnpack
||
288 Cache
[I
].Delete() == true)
291 if (Cache
[I
].InstVerIter(Cache
).Downloadable() == false)
294 Cache
.MarkInstall(I
,true);
297 pkgProblemResolver
Fix(&Cache
);
298 return Fix
.Resolve(true);
301 // DistUpgrade - Distribution upgrade /*{{{*/
302 // ---------------------------------------------------------------------
303 /* This autoinstalls every package and then force installs every
304 pre-existing package. This creates the initial set of conditions which
305 most likely contain problems because too many things were installed.
307 The problem resolver is used to resolve the problems.
309 bool pkgDistUpgrade(pkgDepCache
&Cache
)
311 /* Auto upgrade all installed packages, this provides the basis
312 for the installation */
313 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
314 if (I
->CurrentVer
!= 0)
315 Cache
.MarkInstall(I
,true);
317 /* Now, auto upgrade all essential packages - this ensures that
318 the essential packages are present and working */
319 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
320 if ((I
->Flags
& pkgCache::Flag::Essential
) == pkgCache::Flag::Essential
)
321 Cache
.MarkInstall(I
,true);
323 /* We do it again over all previously installed packages to force
324 conflict resolution on them all. */
325 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
326 if (I
->CurrentVer
!= 0)
327 Cache
.MarkInstall(I
,false);
329 pkgProblemResolver
Fix(&Cache
);
331 // Hold back held packages.
332 if (_config
->FindB("APT::Ignore-Hold",false) == false)
334 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
336 if (I
->SelectedState
== pkgCache::State::Hold
)
344 return Fix
.Resolve();
347 // AllUpgrade - Upgrade as many packages as possible /*{{{*/
348 // ---------------------------------------------------------------------
349 /* Right now the system must be consistent before this can be called.
350 It also will not change packages marked for install, it only tries
351 to install packages not marked for install */
352 bool pkgAllUpgrade(pkgDepCache
&Cache
)
354 pkgProblemResolver
Fix(&Cache
);
356 if (Cache
.BrokenCount() != 0)
359 // Upgrade all installed packages
360 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
362 if (Cache
[I
].Install() == true)
365 if (_config
->FindB("APT::Ignore-Hold",false) == false)
366 if (I
->SelectedState
== pkgCache::State::Hold
)
369 if (I
->CurrentVer
!= 0 && Cache
[I
].InstallVer
!= 0)
370 Cache
.MarkInstall(I
,false);
373 return Fix
.ResolveByKeep();
376 // MinimizeUpgrade - Minimizes the set of packages to be upgraded /*{{{*/
377 // ---------------------------------------------------------------------
378 /* This simply goes over the entire set of packages and tries to keep
379 each package marked for upgrade. If a conflict is generated then
380 the package is restored. */
381 bool pkgMinimizeUpgrade(pkgDepCache
&Cache
)
383 if (Cache
.BrokenCount() != 0)
386 // We loop for 10 tries to get the minimal set size.
388 unsigned int Count
= 0;
392 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
395 if (Cache
[I
].Upgrade() == false || Cache
[I
].NewInstall() == true)
398 // Keep it and see if that is OK
400 if (Cache
.BrokenCount() != 0)
401 Cache
.MarkInstall(I
,false);
404 // If keep didnt actually do anything then there was no change..
405 if (Cache
[I
].Upgrade() == false)
411 while (Change
== true && Count
< 10);
413 if (Cache
.BrokenCount() != 0)
414 return _error
->Error("Internal Error in pkgMinimizeUpgrade");
420 // ProblemResolver::pkgProblemResolver - Constructor /*{{{*/
421 // ---------------------------------------------------------------------
423 pkgProblemResolver::pkgProblemResolver(pkgDepCache
*pCache
) : Cache(*pCache
)
426 unsigned long Size
= Cache
.Head().PackageCount
;
427 Scores
= new signed short[Size
];
428 Flags
= new unsigned char[Size
];
429 memset(Flags
,0,sizeof(*Flags
)*Size
);
431 // Set debug to true to see its decision logic
432 Debug
= _config
->FindB("Debug::pkgProblemResolver",false);
435 // ProblemResolver::~pkgProblemResolver - Destructor /*{{{*/
436 // ---------------------------------------------------------------------
438 pkgProblemResolver::~pkgProblemResolver()
444 // ProblemResolver::ScoreSort - Sort the list by score /*{{{*/
445 // ---------------------------------------------------------------------
447 int pkgProblemResolver::ScoreSort(const void *a
,const void *b
)
449 Package
const **A
= (Package
const **)a
;
450 Package
const **B
= (Package
const **)b
;
451 if (This
->Scores
[(*A
)->ID
] > This
->Scores
[(*B
)->ID
])
453 if (This
->Scores
[(*A
)->ID
] < This
->Scores
[(*B
)->ID
])
458 // ProblemResolver::MakeScores - Make the score table /*{{{*/
459 // ---------------------------------------------------------------------
461 void pkgProblemResolver::MakeScores()
463 unsigned long Size
= Cache
.Head().PackageCount
;
464 memset(Scores
,0,sizeof(*Scores
)*Size
);
466 // Generate the base scores for a package based on its properties
467 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
469 if (Cache
[I
].InstallVer
== 0)
472 signed short &Score
= Scores
[I
->ID
];
474 /* This is arbitary, it should be high enough to elevate an
475 essantial package above most other packages but low enough
476 to allow an obsolete essential packages to be removed by
477 a conflicts on a powerfull normal package (ie libc6) */
478 if ((I
->Flags
& pkgCache::Flag::Essential
) == pkgCache::Flag::Essential
)
481 // We transform the priority
482 // Important Required Standard Optional Extra
483 signed short PrioMap
[] = {0,3,2,1,-1,-2};
484 if (Cache
[I
].InstVerIter(Cache
)->Priority
<= 5)
485 Score
+= PrioMap
[Cache
[I
].InstVerIter(Cache
)->Priority
];
487 /* This helps to fix oddball problems with conflicting packages
488 on the same level. We enhance the score of installed packages */
489 if (I
->CurrentVer
!= 0)
493 // Now that we have the base scores we go and propogate dependencies
494 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
496 if (Cache
[I
].InstallVer
== 0)
499 for (pkgCache::DepIterator D
= Cache
[I
].InstVerIter(Cache
).DependsList(); D
.end() == false; D
++)
501 if (D
->Type
== pkgCache::Dep::Depends
|| D
->Type
== pkgCache::Dep::PreDepends
)
502 Scores
[D
.TargetPkg()->ID
]++;
506 // Copy the scores to advoid additive looping
507 SPtrArray
<signed short> OldScores
= new signed short[Size
];
508 memcpy(OldScores
,Scores
,sizeof(*Scores
)*Size
);
510 /* Now we cause 1 level of dependency inheritance, that is we add the
511 score of the packages that depend on the target Package. This
512 fortifies high scoring packages */
513 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
515 if (Cache
[I
].InstallVer
== 0)
518 for (pkgCache::DepIterator D
= I
.RevDependsList(); D
.end() == false; D
++)
520 // Only do it for the install version
521 if ((pkgCache::Version
*)D
.ParentVer() != Cache
[D
.ParentPkg()].InstallVer
||
522 (D
->Type
!= pkgCache::Dep::Depends
&& D
->Type
!= pkgCache::Dep::PreDepends
))
525 Scores
[I
->ID
] += abs(OldScores
[D
.ParentPkg()->ID
]);
529 /* Now we propogate along provides. This makes the packages that
530 provide important packages extremely important */
531 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
533 for (pkgCache::PrvIterator P
= I
.ProvidesList(); P
.end() == false; P
++)
535 // Only do it once per package
536 if ((pkgCache::Version
*)P
.OwnerVer() != Cache
[P
.OwnerPkg()].InstallVer
)
538 Scores
[P
.OwnerPkg()->ID
] += abs(Scores
[I
->ID
] - OldScores
[I
->ID
]);
542 /* Protected things are pushed really high up. This number should put them
543 ahead of everything */
544 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
546 if ((Flags
[I
->ID
] & Protected
) != 0)
547 Scores
[I
->ID
] += 10000;
548 if ((I
->Flags
& pkgCache::Flag::Essential
) == pkgCache::Flag::Essential
)
549 Scores
[I
->ID
] += 5000;
553 // ProblemResolver::DoUpgrade - Attempt to upgrade this package /*{{{*/
554 // ---------------------------------------------------------------------
555 /* This goes through and tries to reinstall packages to make this package
557 bool pkgProblemResolver::DoUpgrade(pkgCache::PkgIterator Pkg
)
559 if ((Flags
[Pkg
->ID
] & Upgradable
) == 0 || Cache
[Pkg
].Upgradable() == false)
561 if ((Flags
[Pkg
->ID
] & Protected
) == Protected
)
564 Flags
[Pkg
->ID
] &= ~Upgradable
;
566 bool WasKept
= Cache
[Pkg
].Keep();
567 Cache
.MarkInstall(Pkg
,false);
569 // This must be a virtual package or something like that.
570 if (Cache
[Pkg
].InstVerIter(Cache
).end() == true)
573 // Isolate the problem dependency
575 for (pkgCache::DepIterator D
= Cache
[Pkg
].InstVerIter(Cache
).DependsList(); D
.end() == false;)
577 // Compute a single dependency element (glob or)
578 pkgCache::DepIterator Start
= D
;
579 pkgCache::DepIterator End
= D
;
580 unsigned char State
= 0;
581 for (bool LastOR
= true; D
.end() == false && LastOR
== true;)
584 LastOR
= (D
->CompareOp
& pkgCache::Dep::Or
) == pkgCache::Dep::Or
;
590 // We only worry about critical deps.
591 if (End
.IsCritical() != true)
594 // Iterate over all the members in the or group
598 if ((Cache
[End
] & pkgDepCache::DepGInstall
) == pkgDepCache::DepGInstall
)
601 // Do not change protected packages
602 PkgIterator P
= Start
.SmartTargetPkg();
603 if ((Flags
[P
->ID
] & Protected
) == Protected
)
606 clog
<< " Reinst Failed because of protected " << P
.Name() << endl
;
611 // Upgrade the package if the candidate version will fix the problem.
612 if ((Cache
[Start
] & pkgDepCache::DepCVer
) == pkgDepCache::DepCVer
)
614 if (DoUpgrade(P
) == false)
617 clog
<< " Reinst Failed because of " << P
.Name() << endl
;
628 /* We let the algorithm deal with conflicts on its next iteration,
629 it is much smarter than us */
630 if (Start
->Type
== pkgCache::Dep::Conflicts
||
631 Start
->Type
== pkgCache::Dep::Obsoletes
)
635 clog
<< " Reinst Failed early because of " << Start
.TargetPkg().Name() << endl
;
648 // Undo our operations - it might be smart to undo everything this did..
654 Cache
.MarkDelete(Pkg
);
659 clog
<< " Re-Instated " << Pkg
.Name() << endl
;
663 // ProblemResolver::Resolve - Run the resolution pass /*{{{*/
664 // ---------------------------------------------------------------------
665 /* This routines works by calculating a score for each package. The score
666 is derived by considering the package's priority and all reverse
667 dependents giving an integer that reflects the amount of breakage that
668 adjusting the package will inflict.
670 It goes from highest score to lowest and corrects all of the breaks by
671 keeping or removing the dependant packages. If that fails then it removes
672 the package itself and goes on. The routine should be able to intelligently
673 go from any broken state to a fixed state.
675 The BrokenFix flag enables a mode where the algorithm tries to
676 upgrade packages to advoid problems. */
677 bool pkgProblemResolver::Resolve(bool BrokenFix
)
679 unsigned long Size
= Cache
.Head().PackageCount
;
681 // Record which packages are marked for install
686 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
688 if (Cache
[I
].Install() == true)
689 Flags
[I
->ID
] |= PreInstalled
;
692 if (Cache
[I
].InstBroken() == true && BrokenFix
== true)
694 Cache
.MarkInstall(I
,false);
695 if (Cache
[I
].Install() == true)
699 Flags
[I
->ID
] &= ~PreInstalled
;
701 Flags
[I
->ID
] |= Upgradable
;
704 while (Again
== true);
707 clog
<< "Starting" << endl
;
711 /* We have to order the packages so that the broken fixing pass
712 operates from highest score to lowest. This prevents problems when
713 high score packages cause the removal of lower score packages that
714 would cause the removal of even lower score packages. */
715 SPtrArray
<pkgCache::Package
*> PList
= new pkgCache::Package
*[Size
];
716 pkgCache::Package
**PEnd
= PList
;
717 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
720 qsort(PList
,PEnd
- PList
,sizeof(*PList
),&ScoreSort
);
722 /* for (pkgCache::Package **K = PList; K != PEnd; K++)
723 if (Scores[(*K)->ID] != 0)
725 pkgCache::PkgIterator Pkg(Cache,*K);
726 clog << Scores[(*K)->ID] << ' ' << Pkg.Name() <<
727 ' ' << (pkgCache::Version *)Pkg.CurrentVer() << ' ' <<
728 Cache[Pkg].InstallVer << ' ' << Cache[Pkg].CandidateVer << endl;
732 clog
<< "Starting 2" << endl
;
734 /* Now consider all broken packages. For each broken package we either
735 remove the package or fix it's problem. We do this once, it should
736 not be possible for a loop to form (that is a < b < c and fixing b by
737 changing a breaks c) */
739 for (int Counter
= 0; Counter
!= 10 && Change
== true; Counter
++)
742 for (pkgCache::Package
**K
= PList
; K
!= PEnd
; K
++)
744 pkgCache::PkgIterator
I(Cache
,*K
);
746 /* We attempt to install this and see if any breaks result,
747 this takes care of some strange cases */
748 if (Cache
[I
].CandidateVer
!= Cache
[I
].InstallVer
&&
749 I
->CurrentVer
!= 0 && Cache
[I
].InstallVer
!= 0 &&
750 (Flags
[I
->ID
] & PreInstalled
) != 0 &&
751 (Flags
[I
->ID
] & Protected
) == 0 &&
752 (Flags
[I
->ID
] & ReInstateTried
) == 0)
755 clog
<< " Try to Re-Instate " << I
.Name() << endl
;
756 unsigned long OldBreaks
= Cache
.BrokenCount();
757 pkgCache::Version
*OldVer
= Cache
[I
].InstallVer
;
758 Flags
[I
->ID
] &= ReInstateTried
;
760 Cache
.MarkInstall(I
,false);
761 if (Cache
[I
].InstBroken() == true ||
762 OldBreaks
< Cache
.BrokenCount())
771 clog
<< "Re-Instated " << I
.Name() << " (" << OldBreaks
<< " vs " << Cache
.BrokenCount() << ')' << endl
;
774 if (Cache
[I
].InstallVer
== 0 || Cache
[I
].InstBroken() == false)
778 cout
<< "Investigating " << I
.Name() << endl
;
780 // Isolate the problem dependency
781 PackageKill KillList
[100];
782 PackageKill
*LEnd
= KillList
;
784 pkgCache::DepIterator Start
;
785 pkgCache::DepIterator End
;
786 PackageKill
*OldEnd
= LEnd
;
788 enum {OrRemove
,OrKeep
} OrOp
= OrRemove
;
789 for (pkgCache::DepIterator D
= Cache
[I
].InstVerIter(Cache
).DependsList();
790 D
.end() == false || InOr
== true;)
792 // Compute a single dependency element (glob or)
798 if (OldEnd
== LEnd
&& OrOp
== OrRemove
)
800 if ((Flags
[I
->ID
] & Protected
) != Protected
)
803 clog
<< " Or group remove for " << I
.Name() << endl
;
808 if (OldEnd
== LEnd
&& OrOp
== OrKeep
)
811 clog
<< " Or group keep for " << I
.Name() << endl
;
817 /* We do an extra loop (as above) to finalize the or group
822 if (Start
.end() == true)
825 // We only worry about critical deps.
826 if (End
.IsCritical() != true)
836 if ((Cache
[End
] & pkgDepCache::DepGInstall
) == pkgDepCache::DepGInstall
)
843 clog
<< "Package " << I
.Name() << " has broken dep on " << Start
.TargetPkg().Name() << endl
;
845 /* Look across the version list. If there are no possible
846 targets then we keep the package and bail. This is necessary
847 if a package has a dep on another package that cant be found */
848 SPtrArray
<pkgCache::Version
*> VList
= Start
.AllTargets();
849 if (*VList
== 0 && (Flags
[I
->ID
] & Protected
) != Protected
&&
850 Start
->Type
!= pkgCache::Dep::Conflicts
&&
851 Start
->Type
!= pkgCache::Dep::Obsoletes
&&
852 Cache
[I
].NowBroken() == false)
856 /* No keep choice because the keep being OK could be the
857 result of another element in the OR group! */
867 for (pkgCache::Version
**V
= VList
; *V
!= 0; V
++)
869 pkgCache::VerIterator
Ver(Cache
,*V
);
870 pkgCache::PkgIterator Pkg
= Ver
.ParentPkg();
873 clog
<< " Considering " << Pkg
.Name() << ' ' << (int)Scores
[Pkg
->ID
] <<
874 " as a solution to " << I
.Name() << ' ' << (int)Scores
[I
->ID
] << endl
;
876 /* Try to fix the package under consideration rather than
877 fiddle with the VList package */
878 if (Scores
[I
->ID
] <= Scores
[Pkg
->ID
] ||
879 ((Cache
[Start
] & pkgDepCache::DepNow
) == 0 &&
880 End
->Type
!= pkgCache::Dep::Conflicts
&&
881 End
->Type
!= pkgCache::Dep::Obsoletes
))
883 // Try a little harder to fix protected packages..
884 if ((Flags
[I
->ID
] & Protected
) == Protected
)
886 if (DoUpgrade(Pkg
) == true)
888 if (Scores
[Pkg
->ID
] > Scores
[I
->ID
])
889 Scores
[Pkg
->ID
] = Scores
[I
->ID
];
896 /* See if a keep will do, unless the package is protected,
897 then installing it will be necessary */
898 bool Installed
= Cache
[I
].Install();
900 if (Cache
[I
].InstBroken() == false)
902 // Unwind operation will be keep now
903 if (OrOp
== OrRemove
)
907 if (InOr
== true && Installed
== true)
908 Cache
.MarkInstall(I
,false);
911 clog
<< " Holding Back " << I
.Name() << " rather than change " << Start
.TargetPkg().Name() << endl
;
915 if (BrokenFix
== false || DoUpgrade(I
) == false)
917 // Consider other options
921 clog
<< " Removing " << I
.Name() << " rather than change " << Start
.TargetPkg().Name() << endl
;
925 if (Scores
[Pkg
->ID
] > Scores
[I
->ID
])
926 Scores
[I
->ID
] = Scores
[Pkg
->ID
];
938 /* This is a conflicts, and the version we are looking
939 at is not the currently selected version of the
940 package, which means it is not necessary to
942 if (Cache
[Pkg
].InstallVer
!= Ver
&&
943 (Start
->Type
== pkgCache::Dep::Conflicts
||
944 Start
->Type
== pkgCache::Dep::Obsoletes
))
947 // Skip adding to the kill list if it is protected
948 if ((Flags
[Pkg
->ID
] & Protected
) != 0)
952 clog
<< " Added " << Pkg
.Name() << " to the remove list" << endl
;
958 if (Start
->Type
!= pkgCache::Dep::Conflicts
&&
959 Start
->Type
!= pkgCache::Dep::Obsoletes
)
964 // Hm, nothing can possibly satisify this dep. Nuke it.
966 Start
->Type
!= pkgCache::Dep::Conflicts
&&
967 Start
->Type
!= pkgCache::Dep::Obsoletes
&&
968 (Flags
[I
->ID
] & Protected
) != Protected
)
970 bool Installed
= Cache
[I
].Install();
972 if (Cache
[I
].InstBroken() == false)
974 // Unwind operation will be keep now
975 if (OrOp
== OrRemove
)
979 if (InOr
== true && Installed
== true)
980 Cache
.MarkInstall(I
,false);
983 clog
<< " Holding Back " << I
.Name() << " because I can't find " << Start
.TargetPkg().Name() << endl
;
988 clog
<< " Removing " << I
.Name() << " because I can't find " << Start
.TargetPkg().Name() << endl
;
1005 // Apply the kill list now
1006 if (Cache
[I
].InstallVer
!= 0)
1008 for (PackageKill
*J
= KillList
; J
!= LEnd
; J
++)
1011 if ((Cache
[J
->Dep
] & pkgDepCache::DepGNow
) == 0)
1013 if (J
->Dep
->Type
== pkgCache::Dep::Conflicts
||
1014 J
->Dep
->Type
== pkgCache::Dep::Obsoletes
)
1017 clog
<< " Fixing " << I
.Name() << " via remove of " << J
->Pkg
.Name() << endl
;
1018 Cache
.MarkDelete(J
->Pkg
);
1024 clog
<< " Fixing " << I
.Name() << " via keep of " << J
->Pkg
.Name() << endl
;
1025 Cache
.MarkKeep(J
->Pkg
);
1030 if (Scores
[I
->ID
] > Scores
[J
->Pkg
->ID
])
1031 Scores
[J
->Pkg
->ID
] = Scores
[I
->ID
];
1039 clog
<< "Done" << endl
;
1041 if (Cache
.BrokenCount() != 0)
1043 // See if this is the result of a hold
1044 pkgCache::PkgIterator I
= Cache
.PkgBegin();
1045 for (;I
.end() != true; I
++)
1047 if (Cache
[I
].InstBroken() == false)
1049 if ((Flags
[I
->ID
] & Protected
) != Protected
)
1050 return _error
->Error(_("Error, pkgProblemResolver::Resolve generated breaks, this may be caused by held packages."));
1052 return _error
->Error(_("Unable to correct problems, you have held broken packages."));
1058 // ProblemResolver::ResolveByKeep - Resolve problems using keep /*{{{*/
1059 // ---------------------------------------------------------------------
1060 /* This is the work horse of the soft upgrade routine. It is very gental
1061 in that it does not install or remove any packages. It is assumed that the
1062 system was non-broken previously. */
1063 bool pkgProblemResolver::ResolveByKeep()
1065 unsigned long Size
= Cache
.Head().PackageCount
;
1068 clog
<< "Entering ResolveByKeep" << endl
;
1072 /* We have to order the packages so that the broken fixing pass
1073 operates from highest score to lowest. This prevents problems when
1074 high score packages cause the removal of lower score packages that
1075 would cause the removal of even lower score packages. */
1076 pkgCache::Package
**PList
= new pkgCache::Package
*[Size
];
1077 pkgCache::Package
**PEnd
= PList
;
1078 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
1081 qsort(PList
,PEnd
- PList
,sizeof(*PList
),&ScoreSort
);
1083 // Consider each broken package
1084 pkgCache::Package
**LastStop
= 0;
1085 for (pkgCache::Package
**K
= PList
; K
!= PEnd
; K
++)
1087 pkgCache::PkgIterator
I(Cache
,*K
);
1089 if (Cache
[I
].InstallVer
== 0 || Cache
[I
].InstBroken() == false)
1092 /* Keep the package. If this works then great, otherwise we have
1093 to be significantly more agressive and manipulate its dependencies */
1094 if ((Flags
[I
->ID
] & Protected
) == 0)
1097 clog
<< "Keeping package " << I
.Name() << endl
;
1099 if (Cache
[I
].InstBroken() == false)
1106 // Isolate the problem dependencies
1107 for (pkgCache::DepIterator D
= Cache
[I
].InstVerIter(Cache
).DependsList(); D
.end() == false;)
1111 D
.GlobOr(Start
,End
);
1113 // We only worry about critical deps.
1114 if (End
.IsCritical() != true)
1118 if ((Cache
[End
] & pkgDepCache::DepGInstall
) == pkgDepCache::DepGInstall
)
1121 /* Hm, the group is broken.. I suppose the best thing to do is to
1122 is to try every combination of keep/not-keep for the set, but thats
1123 slow, and this never happens, just be conservative and assume the
1124 list of ors is in preference and keep till it starts to work. */
1128 clog
<< "Package " << I
.Name() << " has broken dep on " << Start
.TargetPkg().Name() << endl
;
1130 // Look at all the possible provides on this package
1131 SPtrArray
<pkgCache::Version
*> VList
= Start
.AllTargets();
1132 for (pkgCache::Version
**V
= VList
; *V
!= 0; V
++)
1134 pkgCache::VerIterator
Ver(Cache
,*V
);
1135 pkgCache::PkgIterator Pkg
= Ver
.ParentPkg();
1137 // It is not keepable
1138 if (Cache
[Pkg
].InstallVer
== 0 ||
1139 Pkg
->CurrentVer
== 0)
1142 if ((Flags
[I
->ID
] & Protected
) == 0)
1145 clog
<< " Keeping Package " << Pkg
.Name() << " due to dep" << endl
;
1146 Cache
.MarkKeep(Pkg
);
1149 if (Cache
[I
].InstBroken() == false)
1153 if (Cache
[I
].InstBroken() == false)
1161 if (Cache
[I
].InstBroken() == false)
1165 if (Cache
[I
].InstBroken() == true)
1170 return _error
->Error("Internal Error, pkgProblemResolver::ResolveByKeep is looping on package %s.",I
.Name());
1178 // ProblemResolver::InstallProtect - Install all protected packages /*{{{*/
1179 // ---------------------------------------------------------------------
1180 /* This is used to make sure protected packages are installed */
1181 void pkgProblemResolver::InstallProtect()
1183 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
1185 if ((Flags
[I
->ID
] & Protected
) == Protected
)
1187 if ((Flags
[I
->ID
] & ToRemove
) == ToRemove
)
1188 Cache
.MarkDelete(I
);
1190 Cache
.MarkInstall(I
,false);
1196 // PrioSortList - Sort a list of versions by priority /*{{{*/
1197 // ---------------------------------------------------------------------
1198 /* This is ment to be used in conjunction with AllTargets to get a list
1199 of versions ordered by preference. */
1200 static pkgCache
*PrioCache
;
1201 static int PrioComp(const void *A
,const void *B
)
1203 pkgCache::VerIterator
L(*PrioCache
,*(pkgCache::Version
**)A
);
1204 pkgCache::VerIterator
R(*PrioCache
,*(pkgCache::Version
**)B
);
1206 if ((L
.ParentPkg()->Flags
& pkgCache::Flag::Essential
) == pkgCache::Flag::Essential
&&
1207 (L
.ParentPkg()->Flags
& pkgCache::Flag::Essential
) != pkgCache::Flag::Essential
)
1209 if ((L
.ParentPkg()->Flags
& pkgCache::Flag::Essential
) != pkgCache::Flag::Essential
&&
1210 (L
.ParentPkg()->Flags
& pkgCache::Flag::Essential
) == pkgCache::Flag::Essential
)
1213 if (L
->Priority
!= R
->Priority
)
1214 return L
->Priority
- R
->Priority
;
1215 return strcmp(L
.ParentPkg().Name(),R
.ParentPkg().Name());
1217 void pkgPrioSortList(pkgCache
&Cache
,pkgCache::Version
**List
)
1219 unsigned long Count
= 0;
1221 for (pkgCache::Version
**I
= List
; *I
!= 0; I
++)
1223 qsort(List
,Count
,sizeof(*List
),PrioComp
);