]>
git.saurik.com Git - apt.git/blob - apt-pkg/algorithms.cc
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: algorithms.cc,v 1.44 2002/11/28 18:49:16 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 /*{{{*/
17 #include <apt-pkg/algorithms.h>
18 #include <apt-pkg/error.h>
19 #include <apt-pkg/configuration.h>
20 #include <apt-pkg/version.h>
21 #include <apt-pkg/sptr.h>
31 pkgProblemResolver
*pkgProblemResolver::This
= 0;
33 // Simulate::Simulate - Constructor /*{{{*/
34 // ---------------------------------------------------------------------
35 /* The legacy translations here of input Pkg iterators is obsolete,
36 this is not necessary since the pkgCaches are fully shared now. */
37 pkgSimulate::pkgSimulate(pkgDepCache
*Cache
) : pkgPackageManager(Cache
),
39 Sim(&Cache
->GetCache(),&iPolicy
)
42 Flags
= new unsigned char[Cache
->Head().PackageCount
];
43 memset(Flags
,0,sizeof(*Flags
)*Cache
->Head().PackageCount
);
45 // Fake a filename so as not to activate the media swapping
46 string Jnk
= "SIMULATE";
47 for (unsigned int I
= 0; I
!= Cache
->Head().PackageCount
; I
++)
51 // Simulate::Describe - Describe a package /*{{{*/
52 // ---------------------------------------------------------------------
53 /* Parameter Current == true displays the current package version,
54 Parameter Candidate == true displays the candidate package version */
55 void pkgSimulate::Describe(PkgIterator Pkg
,ostream
&out
,bool Current
,bool Candidate
)
63 Ver
= Pkg
.CurrentVer();
64 if (Ver
.end() == false)
65 out
<< " [" << Ver
.VerStr() << ']';
68 if (Candidate
== true)
70 Ver
= Sim
[Pkg
].CandidateVerIter(Sim
);
71 if (Ver
.end() == true)
74 out
<< " (" << Ver
.VerStr() << ' ' << Ver
.RelStr() << ')';
78 // Simulate::Install - Simulate unpacking of a package /*{{{*/
79 // ---------------------------------------------------------------------
81 bool pkgSimulate::Install(PkgIterator iPkg
,string
/*File*/)
84 PkgIterator Pkg
= Sim
.FindPkg(iPkg
.Name());
88 Describe(Pkg
,cout
,true,true);
89 Sim
.MarkInstall(Pkg
,false);
91 // Look for broken conflicts+predepends.
92 for (PkgIterator I
= Sim
.PkgBegin(); I
.end() == false; I
++)
94 if (Sim
[I
].InstallVer
== 0)
97 for (DepIterator D
= Sim
[I
].InstVerIter(Sim
).DependsList(); D
.end() == false;)
102 if (Start
->Type
== pkgCache::Dep::Conflicts
||
103 Start
->Type
== pkgCache::Dep::DpkgBreaks
||
104 Start
->Type
== pkgCache::Dep::Obsoletes
||
105 End
->Type
== pkgCache::Dep::PreDepends
)
107 if ((Sim
[End
] & pkgDepCache::DepGInstall
) == 0)
109 cout
<< " [" << I
.Name() << " on " << Start
.TargetPkg().Name() << ']';
110 if (Start
->Type
== pkgCache::Dep::Conflicts
)
111 _error
->Error("Fatal, conflicts violated %s",I
.Name());
117 if (Sim
.BrokenCount() != 0)
124 // Simulate::Configure - Simulate configuration of a Package /*{{{*/
125 // ---------------------------------------------------------------------
126 /* This is not an acurate simulation of relatity, we should really not
127 install the package.. For some investigations it may be necessary
129 bool pkgSimulate::Configure(PkgIterator iPkg
)
131 // Adapt the iterator
132 PkgIterator Pkg
= Sim
.FindPkg(iPkg
.Name());
135 // Sim.MarkInstall(Pkg,false);
136 if (Sim
[Pkg
].InstBroken() == true)
138 cout
<< "Conf " << Pkg
.Name() << " broken" << endl
;
142 // Print out each package and the failed dependencies
143 for (pkgCache::DepIterator D
= Sim
[Pkg
].InstVerIter(Sim
).DependsList(); D
.end() == false; D
++)
145 if (Sim
.IsImportantDep(D
) == false ||
146 (Sim
[D
] & pkgDepCache::DepInstall
) != 0)
149 if (D
->Type
== pkgCache::Dep::Obsoletes
)
150 cout
<< " Obsoletes:" << D
.TargetPkg().Name();
151 else if (D
->Type
== pkgCache::Dep::Conflicts
)
152 cout
<< " Conflicts:" << D
.TargetPkg().Name();
153 else if (D
->Type
== pkgCache::Dep::DpkgBreaks
)
154 cout
<< " Breaks:" << D
.TargetPkg().Name();
156 cout
<< " Depends:" << D
.TargetPkg().Name();
160 _error
->Error("Conf Broken %s",Pkg
.Name());
165 Describe(Pkg
,cout
,false,true);
168 if (Sim
.BrokenCount() != 0)
176 // Simulate::Remove - Simulate the removal of a package /*{{{*/
177 // ---------------------------------------------------------------------
179 bool pkgSimulate::Remove(PkgIterator iPkg
,bool Purge
)
181 // Adapt the iterator
182 PkgIterator Pkg
= Sim
.FindPkg(iPkg
.Name());
190 Describe(Pkg
,cout
,true,false);
192 if (Sim
.BrokenCount() != 0)
200 // Simulate::ShortBreaks - Print out a short line describing all breaks /*{{{*/
201 // ---------------------------------------------------------------------
203 void pkgSimulate::ShortBreaks()
206 for (PkgIterator I
= Sim
.PkgBegin(); I
.end() == false; I
++)
208 if (Sim
[I
].InstBroken() == true)
210 if (Flags
[I
->ID
] == 0)
211 cout
<< I
.Name() << ' ';
213 cout << I.Name() << "! ";*/
219 // ApplyStatus - Adjust for non-ok packages /*{{{*/
220 // ---------------------------------------------------------------------
221 /* We attempt to change the state of the all packages that have failed
222 installation toward their real state. The ordering code will perform
223 the necessary calculations to deal with the problems. */
224 bool pkgApplyStatus(pkgDepCache
&Cache
)
226 pkgDepCache::ActionGroup
group(Cache
);
228 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
230 if (I
->VersionList
== 0)
233 // Only choice for a ReInstReq package is to reinstall
234 if (I
->InstState
== pkgCache::State::ReInstReq
||
235 I
->InstState
== pkgCache::State::HoldReInstReq
)
237 if (I
->CurrentVer
!= 0 && I
.CurrentVer().Downloadable() == true)
238 Cache
.MarkKeep(I
, false, false);
241 // Is this right? Will dpkg choke on an upgrade?
242 if (Cache
[I
].CandidateVer
!= 0 &&
243 Cache
[I
].CandidateVerIter(Cache
).Downloadable() == true)
244 Cache
.MarkInstall(I
, false, 0, false);
246 return _error
->Error(_("The package %s needs to be reinstalled, "
247 "but I can't find an archive for it."),I
.Name());
253 switch (I
->CurrentState
)
255 /* This means installation failed somehow - it does not need to be
256 re-unpacked (probably) */
257 case pkgCache::State::UnPacked
:
258 case pkgCache::State::HalfConfigured
:
259 if ((I
->CurrentVer
!= 0 && I
.CurrentVer().Downloadable() == true) ||
260 I
.State() != pkgCache::PkgIterator::NeedsUnpack
)
261 Cache
.MarkKeep(I
, false, false);
264 if (Cache
[I
].CandidateVer
!= 0 &&
265 Cache
[I
].CandidateVerIter(Cache
).Downloadable() == true)
266 Cache
.MarkInstall(I
, true, 0, false);
272 // This means removal failed
273 case pkgCache::State::HalfInstalled
:
278 if (I
->InstState
!= pkgCache::State::Ok
)
279 return _error
->Error("The package %s is not ok and I "
280 "don't know how to fix it!",I
.Name());
286 // FixBroken - Fix broken packages /*{{{*/
287 // ---------------------------------------------------------------------
288 /* This autoinstalls every broken package and then runs the problem resolver
290 bool pkgFixBroken(pkgDepCache
&Cache
)
292 pkgDepCache::ActionGroup
group(Cache
);
294 // Auto upgrade all broken packages
295 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
296 if (Cache
[I
].NowBroken() == true)
297 Cache
.MarkInstall(I
, true, 0, false);
299 /* Fix packages that are in a NeedArchive state but don't have a
300 downloadable install version */
301 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
303 if (I
.State() != pkgCache::PkgIterator::NeedsUnpack
||
304 Cache
[I
].Delete() == true)
307 if (Cache
[I
].InstVerIter(Cache
).Downloadable() == false)
310 Cache
.MarkInstall(I
, true, 0, false);
313 pkgProblemResolver
Fix(&Cache
);
314 return Fix
.Resolve(true);
317 // DistUpgrade - Distribution upgrade /*{{{*/
318 // ---------------------------------------------------------------------
319 /* This autoinstalls every package and then force installs every
320 pre-existing package. This creates the initial set of conditions which
321 most likely contain problems because too many things were installed.
323 The problem resolver is used to resolve the problems.
325 bool pkgDistUpgrade(pkgDepCache
&Cache
)
327 pkgDepCache::ActionGroup
group(Cache
);
329 /* Auto upgrade all installed packages, this provides the basis
330 for the installation */
331 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
332 if (I
->CurrentVer
!= 0)
333 Cache
.MarkInstall(I
, true, 0, false);
335 /* Now, auto upgrade all essential packages - this ensures that
336 the essential packages are present and working */
337 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
338 if ((I
->Flags
& pkgCache::Flag::Essential
) == pkgCache::Flag::Essential
)
339 Cache
.MarkInstall(I
, true, 0, false);
341 /* We do it again over all previously installed packages to force
342 conflict resolution on them all. */
343 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
344 if (I
->CurrentVer
!= 0)
345 Cache
.MarkInstall(I
, false, 0, false);
347 pkgProblemResolver
Fix(&Cache
);
349 // Hold back held packages.
350 if (_config
->FindB("APT::Ignore-Hold",false) == false)
352 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
354 if (I
->SelectedState
== pkgCache::State::Hold
)
357 Cache
.MarkKeep(I
, false, false);
362 return Fix
.Resolve();
365 // AllUpgrade - Upgrade as many packages as possible /*{{{*/
366 // ---------------------------------------------------------------------
367 /* Right now the system must be consistent before this can be called.
368 It also will not change packages marked for install, it only tries
369 to install packages not marked for install */
370 bool pkgAllUpgrade(pkgDepCache
&Cache
)
372 pkgDepCache::ActionGroup
group(Cache
);
374 pkgProblemResolver
Fix(&Cache
);
376 if (Cache
.BrokenCount() != 0)
379 // Upgrade all installed packages
380 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
382 if (Cache
[I
].Install() == true)
385 if (_config
->FindB("APT::Ignore-Hold",false) == false)
386 if (I
->SelectedState
== pkgCache::State::Hold
)
389 if (I
->CurrentVer
!= 0 && Cache
[I
].InstallVer
!= 0)
390 Cache
.MarkInstall(I
, false, 0, false);
393 return Fix
.ResolveByKeep();
396 // MinimizeUpgrade - Minimizes the set of packages to be upgraded /*{{{*/
397 // ---------------------------------------------------------------------
398 /* This simply goes over the entire set of packages and tries to keep
399 each package marked for upgrade. If a conflict is generated then
400 the package is restored. */
401 bool pkgMinimizeUpgrade(pkgDepCache
&Cache
)
403 pkgDepCache::ActionGroup
group(Cache
);
405 if (Cache
.BrokenCount() != 0)
408 // We loop for 10 tries to get the minimal set size.
410 unsigned int Count
= 0;
414 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
417 if (Cache
[I
].Upgrade() == false || Cache
[I
].NewInstall() == true)
420 // Keep it and see if that is OK
421 Cache
.MarkKeep(I
, false, false);
422 if (Cache
.BrokenCount() != 0)
423 Cache
.MarkInstall(I
, false, 0, false);
426 // If keep didnt actually do anything then there was no change..
427 if (Cache
[I
].Upgrade() == false)
433 while (Change
== true && Count
< 10);
435 if (Cache
.BrokenCount() != 0)
436 return _error
->Error("Internal Error in pkgMinimizeUpgrade");
442 // ProblemResolver::pkgProblemResolver - Constructor /*{{{*/
443 // ---------------------------------------------------------------------
445 pkgProblemResolver::pkgProblemResolver(pkgDepCache
*pCache
) : Cache(*pCache
)
448 unsigned long Size
= Cache
.Head().PackageCount
;
449 Scores
= new signed short[Size
];
450 Flags
= new unsigned char[Size
];
451 memset(Flags
,0,sizeof(*Flags
)*Size
);
453 // Set debug to true to see its decision logic
454 Debug
= _config
->FindB("Debug::pkgProblemResolver",false);
457 // ProblemResolver::~pkgProblemResolver - Destructor /*{{{*/
458 // ---------------------------------------------------------------------
460 pkgProblemResolver::~pkgProblemResolver()
466 // ProblemResolver::ScoreSort - Sort the list by score /*{{{*/
467 // ---------------------------------------------------------------------
469 int pkgProblemResolver::ScoreSort(const void *a
,const void *b
)
471 Package
const **A
= (Package
const **)a
;
472 Package
const **B
= (Package
const **)b
;
473 if (This
->Scores
[(*A
)->ID
] > This
->Scores
[(*B
)->ID
])
475 if (This
->Scores
[(*A
)->ID
] < This
->Scores
[(*B
)->ID
])
480 // ProblemResolver::MakeScores - Make the score table /*{{{*/
481 // ---------------------------------------------------------------------
483 void pkgProblemResolver::MakeScores()
485 unsigned long Size
= Cache
.Head().PackageCount
;
486 memset(Scores
,0,sizeof(*Scores
)*Size
);
488 // Generate the base scores for a package based on its properties
489 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
491 if (Cache
[I
].InstallVer
== 0)
494 signed short &Score
= Scores
[I
->ID
];
496 /* This is arbitary, it should be high enough to elevate an
497 essantial package above most other packages but low enough
498 to allow an obsolete essential packages to be removed by
499 a conflicts on a powerfull normal package (ie libc6) */
500 if ((I
->Flags
& pkgCache::Flag::Essential
) == pkgCache::Flag::Essential
)
503 // We transform the priority
504 // Important Required Standard Optional Extra
505 signed short PrioMap
[] = {0,3,2,1,-1,-2};
506 if (Cache
[I
].InstVerIter(Cache
)->Priority
<= 5)
507 Score
+= PrioMap
[Cache
[I
].InstVerIter(Cache
)->Priority
];
509 /* This helps to fix oddball problems with conflicting packages
510 on the same level. We enhance the score of installed packages
511 if those are not obsolete
513 if (I
->CurrentVer
!= 0 && Cache
[I
].CandidateVer
!= 0 && Cache
[I
].CandidateVerIter(Cache
).Downloadable())
517 // Now that we have the base scores we go and propogate dependencies
518 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
520 if (Cache
[I
].InstallVer
== 0)
523 for (pkgCache::DepIterator D
= Cache
[I
].InstVerIter(Cache
).DependsList(); D
.end() == false; D
++)
525 if (D
->Type
== pkgCache::Dep::Depends
|| D
->Type
== pkgCache::Dep::PreDepends
)
526 Scores
[D
.TargetPkg()->ID
]++;
530 // Copy the scores to advoid additive looping
531 SPtrArray
<signed short> OldScores
= new signed short[Size
];
532 memcpy(OldScores
,Scores
,sizeof(*Scores
)*Size
);
534 /* Now we cause 1 level of dependency inheritance, that is we add the
535 score of the packages that depend on the target Package. This
536 fortifies high scoring packages */
537 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
539 if (Cache
[I
].InstallVer
== 0)
542 for (pkgCache::DepIterator D
= I
.RevDependsList(); D
.end() == false; D
++)
544 // Only do it for the install version
545 if ((pkgCache::Version
*)D
.ParentVer() != Cache
[D
.ParentPkg()].InstallVer
||
546 (D
->Type
!= pkgCache::Dep::Depends
&& D
->Type
!= pkgCache::Dep::PreDepends
))
549 Scores
[I
->ID
] += abs(OldScores
[D
.ParentPkg()->ID
]);
553 /* Now we propogate along provides. This makes the packages that
554 provide important packages extremely important */
555 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
557 for (pkgCache::PrvIterator P
= I
.ProvidesList(); P
.end() == false; P
++)
559 // Only do it once per package
560 if ((pkgCache::Version
*)P
.OwnerVer() != Cache
[P
.OwnerPkg()].InstallVer
)
562 Scores
[P
.OwnerPkg()->ID
] += abs(Scores
[I
->ID
] - OldScores
[I
->ID
]);
566 /* Protected things are pushed really high up. This number should put them
567 ahead of everything */
568 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
570 if ((Flags
[I
->ID
] & Protected
) != 0)
571 Scores
[I
->ID
] += 10000;
572 if ((I
->Flags
& pkgCache::Flag::Essential
) == pkgCache::Flag::Essential
)
573 Scores
[I
->ID
] += 5000;
577 // ProblemResolver::DoUpgrade - Attempt to upgrade this package /*{{{*/
578 // ---------------------------------------------------------------------
579 /* This goes through and tries to reinstall packages to make this package
581 bool pkgProblemResolver::DoUpgrade(pkgCache::PkgIterator Pkg
)
583 pkgDepCache::ActionGroup
group(Cache
);
585 if ((Flags
[Pkg
->ID
] & Upgradable
) == 0 || Cache
[Pkg
].Upgradable() == false)
587 if ((Flags
[Pkg
->ID
] & Protected
) == Protected
)
590 Flags
[Pkg
->ID
] &= ~Upgradable
;
592 bool WasKept
= Cache
[Pkg
].Keep();
593 Cache
.MarkInstall(Pkg
, false, 0, false);
595 // This must be a virtual package or something like that.
596 if (Cache
[Pkg
].InstVerIter(Cache
).end() == true)
599 // Isolate the problem dependency
601 for (pkgCache::DepIterator D
= Cache
[Pkg
].InstVerIter(Cache
).DependsList(); D
.end() == false;)
603 // Compute a single dependency element (glob or)
604 pkgCache::DepIterator Start
= D
;
605 pkgCache::DepIterator End
= D
;
606 unsigned char State
= 0;
607 for (bool LastOR
= true; D
.end() == false && LastOR
== true;)
610 LastOR
= (D
->CompareOp
& pkgCache::Dep::Or
) == pkgCache::Dep::Or
;
616 // We only worry about critical deps.
617 if (End
.IsCritical() != true)
620 // Iterate over all the members in the or group
624 if ((Cache
[End
] & pkgDepCache::DepGInstall
) == pkgDepCache::DepGInstall
)
627 // Do not change protected packages
628 PkgIterator P
= Start
.SmartTargetPkg();
629 if ((Flags
[P
->ID
] & Protected
) == Protected
)
632 clog
<< " Reinst Failed because of protected " << P
.Name() << endl
;
637 // Upgrade the package if the candidate version will fix the problem.
638 if ((Cache
[Start
] & pkgDepCache::DepCVer
) == pkgDepCache::DepCVer
)
640 if (DoUpgrade(P
) == false)
643 clog
<< " Reinst Failed because of " << P
.Name() << endl
;
654 /* We let the algorithm deal with conflicts on its next iteration,
655 it is much smarter than us */
656 if (Start
->Type
== pkgCache::Dep::Conflicts
||
657 Start
->Type
== pkgCache::Dep::DpkgBreaks
||
658 Start
->Type
== pkgCache::Dep::Obsoletes
)
662 clog
<< " Reinst Failed early because of " << Start
.TargetPkg().Name() << endl
;
675 // Undo our operations - it might be smart to undo everything this did..
679 Cache
.MarkKeep(Pkg
, false, false);
681 Cache
.MarkDelete(Pkg
);
686 clog
<< " Re-Instated " << Pkg
.Name() << endl
;
690 // ProblemResolver::Resolve - Run the resolution pass /*{{{*/
691 // ---------------------------------------------------------------------
692 /* This routines works by calculating a score for each package. The score
693 is derived by considering the package's priority and all reverse
694 dependents giving an integer that reflects the amount of breakage that
695 adjusting the package will inflict.
697 It goes from highest score to lowest and corrects all of the breaks by
698 keeping or removing the dependant packages. If that fails then it removes
699 the package itself and goes on. The routine should be able to intelligently
700 go from any broken state to a fixed state.
702 The BrokenFix flag enables a mode where the algorithm tries to
703 upgrade packages to advoid problems. */
704 bool pkgProblemResolver::Resolve(bool BrokenFix
)
706 pkgDepCache::ActionGroup
group(Cache
);
708 unsigned long Size
= Cache
.Head().PackageCount
;
710 // Record which packages are marked for install
715 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
717 if (Cache
[I
].Install() == true)
718 Flags
[I
->ID
] |= PreInstalled
;
721 if (Cache
[I
].InstBroken() == true && BrokenFix
== true)
723 Cache
.MarkInstall(I
, false, 0, false);
724 if (Cache
[I
].Install() == true)
728 Flags
[I
->ID
] &= ~PreInstalled
;
730 Flags
[I
->ID
] |= Upgradable
;
733 while (Again
== true);
736 clog
<< "Starting" << endl
;
740 /* We have to order the packages so that the broken fixing pass
741 operates from highest score to lowest. This prevents problems when
742 high score packages cause the removal of lower score packages that
743 would cause the removal of even lower score packages. */
744 SPtrArray
<pkgCache::Package
*> PList
= new pkgCache::Package
*[Size
];
745 pkgCache::Package
**PEnd
= PList
;
746 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
749 qsort(PList
,PEnd
- PList
,sizeof(*PList
),&ScoreSort
);
751 /* for (pkgCache::Package **K = PList; K != PEnd; K++)
752 if (Scores[(*K)->ID] != 0)
754 pkgCache::PkgIterator Pkg(Cache,*K);
755 clog << Scores[(*K)->ID] << ' ' << Pkg.Name() <<
756 ' ' << (pkgCache::Version *)Pkg.CurrentVer() << ' ' <<
757 Cache[Pkg].InstallVer << ' ' << Cache[Pkg].CandidateVer << endl;
761 clog
<< "Starting 2" << endl
;
763 /* Now consider all broken packages. For each broken package we either
764 remove the package or fix it's problem. We do this once, it should
765 not be possible for a loop to form (that is a < b < c and fixing b by
766 changing a breaks c) */
768 for (int Counter
= 0; Counter
!= 10 && Change
== true; Counter
++)
771 for (pkgCache::Package
**K
= PList
; K
!= PEnd
; K
++)
773 pkgCache::PkgIterator
I(Cache
,*K
);
775 /* We attempt to install this and see if any breaks result,
776 this takes care of some strange cases */
777 if (Cache
[I
].CandidateVer
!= Cache
[I
].InstallVer
&&
778 I
->CurrentVer
!= 0 && Cache
[I
].InstallVer
!= 0 &&
779 (Flags
[I
->ID
] & PreInstalled
) != 0 &&
780 (Flags
[I
->ID
] & Protected
) == 0 &&
781 (Flags
[I
->ID
] & ReInstateTried
) == 0)
784 clog
<< " Try to Re-Instate " << I
.Name() << endl
;
785 unsigned long OldBreaks
= Cache
.BrokenCount();
786 pkgCache::Version
*OldVer
= Cache
[I
].InstallVer
;
787 Flags
[I
->ID
] &= ReInstateTried
;
789 Cache
.MarkInstall(I
, false, 0, false);
790 if (Cache
[I
].InstBroken() == true ||
791 OldBreaks
< Cache
.BrokenCount())
796 Cache
.MarkKeep(I
, false, false);
800 clog
<< "Re-Instated " << I
.Name() << " (" << OldBreaks
<< " vs " << Cache
.BrokenCount() << ')' << endl
;
803 if (Cache
[I
].InstallVer
== 0 || Cache
[I
].InstBroken() == false)
807 clog
<< "Investigating " << I
.Name() << endl
;
809 // Isolate the problem dependency
810 PackageKill KillList
[100];
811 PackageKill
*LEnd
= KillList
;
813 pkgCache::DepIterator Start
;
814 pkgCache::DepIterator End
;
815 PackageKill
*OldEnd
= LEnd
;
817 enum {OrRemove
,OrKeep
} OrOp
= OrRemove
;
818 for (pkgCache::DepIterator D
= Cache
[I
].InstVerIter(Cache
).DependsList();
819 D
.end() == false || InOr
== true;)
821 // Compute a single dependency element (glob or)
827 if (OldEnd
== LEnd
&& OrOp
== OrRemove
)
829 if ((Flags
[I
->ID
] & Protected
) != Protected
)
832 clog
<< " Or group remove for " << I
.Name() << endl
;
837 if (OldEnd
== LEnd
&& OrOp
== OrKeep
)
840 clog
<< " Or group keep for " << I
.Name() << endl
;
841 Cache
.MarkKeep(I
, false, false);
846 /* We do an extra loop (as above) to finalize the or group
851 if (Start
.end() == true)
854 // We only worry about critical deps.
855 if (End
.IsCritical() != true)
864 // We only worry about critical deps.
865 if (Start
.IsCritical() != true)
870 if ((Cache
[End
] & pkgDepCache::DepGInstall
) == pkgDepCache::DepGInstall
)
877 clog
<< "Package " << I
.Name() << " has broken dep on " << Start
.TargetPkg().Name() << endl
;
879 /* Look across the version list. If there are no possible
880 targets then we keep the package and bail. This is necessary
881 if a package has a dep on another package that cant be found */
882 SPtrArray
<pkgCache::Version
*> VList
= Start
.AllTargets();
883 if (*VList
== 0 && (Flags
[I
->ID
] & Protected
) != Protected
&&
884 Start
->Type
!= pkgCache::Dep::Conflicts
&&
885 Start
->Type
!= pkgCache::Dep::DpkgBreaks
&&
886 Start
->Type
!= pkgCache::Dep::Obsoletes
&&
887 Cache
[I
].NowBroken() == false)
891 /* No keep choice because the keep being OK could be the
892 result of another element in the OR group! */
897 Cache
.MarkKeep(I
, false, false);
902 for (pkgCache::Version
**V
= VList
; *V
!= 0; V
++)
904 pkgCache::VerIterator
Ver(Cache
,*V
);
905 pkgCache::PkgIterator Pkg
= Ver
.ParentPkg();
908 clog
<< " Considering " << Pkg
.Name() << ' ' << (int)Scores
[Pkg
->ID
] <<
909 " as a solution to " << I
.Name() << ' ' << (int)Scores
[I
->ID
] << endl
;
911 /* Try to fix the package under consideration rather than
912 fiddle with the VList package */
913 if (Scores
[I
->ID
] <= Scores
[Pkg
->ID
] ||
914 ((Cache
[Start
] & pkgDepCache::DepNow
) == 0 &&
915 End
->Type
!= pkgCache::Dep::Conflicts
&&
916 End
->Type
!= pkgCache::Dep::DpkgBreaks
&&
917 End
->Type
!= pkgCache::Dep::Obsoletes
))
919 // Try a little harder to fix protected packages..
920 if ((Flags
[I
->ID
] & Protected
) == Protected
)
922 if (DoUpgrade(Pkg
) == true)
924 if (Scores
[Pkg
->ID
] > Scores
[I
->ID
])
925 Scores
[Pkg
->ID
] = Scores
[I
->ID
];
932 /* See if a keep will do, unless the package is protected,
933 then installing it will be necessary */
934 bool Installed
= Cache
[I
].Install();
935 Cache
.MarkKeep(I
, false, false);
936 if (Cache
[I
].InstBroken() == false)
938 // Unwind operation will be keep now
939 if (OrOp
== OrRemove
)
943 if (InOr
== true && Installed
== true)
944 Cache
.MarkInstall(I
, false, 0, false);
947 clog
<< " Holding Back " << I
.Name() << " rather than change " << Start
.TargetPkg().Name() << endl
;
951 if (BrokenFix
== false || DoUpgrade(I
) == false)
953 // Consider other options
957 clog
<< " Removing " << I
.Name() << " rather than change " << Start
.TargetPkg().Name() << endl
;
961 if (Scores
[Pkg
->ID
] > Scores
[I
->ID
])
962 Scores
[I
->ID
] = Scores
[Pkg
->ID
];
974 /* This is a conflicts, and the version we are looking
975 at is not the currently selected version of the
976 package, which means it is not necessary to
978 if (Cache
[Pkg
].InstallVer
!= Ver
&&
979 (Start
->Type
== pkgCache::Dep::Conflicts
||
980 Start
->Type
== pkgCache::Dep::Obsoletes
))
983 if (Start
->Type
== pkgCache::Dep::DpkgBreaks
)
985 /* Would it help if we upgraded? */
986 if (Cache
[End
] & pkgDepCache::DepGCVer
) {
988 clog
<< " Upgrading " << Pkg
.Name() << " due to Breaks field in " << I
.Name() << endl
;
989 Cache
.MarkInstall(Pkg
, false, 0, false);
993 clog
<< " Will not break " << Pkg
.Name() << " as stated in Breaks field in " << I
.Name() <<endl
;
994 Cache
.MarkKeep(I
, false, false);
998 // Skip adding to the kill list if it is protected
999 if ((Flags
[Pkg
->ID
] & Protected
) != 0)
1003 clog
<< " Added " << Pkg
.Name() << " to the remove list" << endl
;
1009 if (Start
->Type
!= pkgCache::Dep::Conflicts
&&
1010 Start
->Type
!= pkgCache::Dep::Obsoletes
)
1015 // Hm, nothing can possibly satisify this dep. Nuke it.
1016 if (VList
[0] == 0 &&
1017 Start
->Type
!= pkgCache::Dep::Conflicts
&&
1018 Start
->Type
!= pkgCache::Dep::DpkgBreaks
&&
1019 Start
->Type
!= pkgCache::Dep::Obsoletes
&&
1020 (Flags
[I
->ID
] & Protected
) != Protected
)
1022 bool Installed
= Cache
[I
].Install();
1024 if (Cache
[I
].InstBroken() == false)
1026 // Unwind operation will be keep now
1027 if (OrOp
== OrRemove
)
1031 if (InOr
== true && Installed
== true)
1032 Cache
.MarkInstall(I
, false, 0, false);
1035 clog
<< " Holding Back " << I
.Name() << " because I can't find " << Start
.TargetPkg().Name() << endl
;
1040 clog
<< " Removing " << I
.Name() << " because I can't find " << Start
.TargetPkg().Name() << endl
;
1042 Cache
.MarkDelete(I
);
1057 // Apply the kill list now
1058 if (Cache
[I
].InstallVer
!= 0)
1060 for (PackageKill
*J
= KillList
; J
!= LEnd
; J
++)
1063 if ((Cache
[J
->Dep
] & pkgDepCache::DepGNow
) == 0)
1065 if (J
->Dep
->Type
== pkgCache::Dep::Conflicts
||
1066 J
->Dep
->Type
== pkgCache::Dep::Obsoletes
)
1069 clog
<< " Fixing " << I
.Name() << " via remove of " << J
->Pkg
.Name() << endl
;
1070 Cache
.MarkDelete(J
->Pkg
);
1076 clog
<< " Fixing " << I
.Name() << " via keep of " << J
->Pkg
.Name() << endl
;
1077 Cache
.MarkKeep(J
->Pkg
, false, false);
1082 if (Scores
[I
->ID
] > Scores
[J
->Pkg
->ID
])
1083 Scores
[J
->Pkg
->ID
] = Scores
[I
->ID
];
1091 clog
<< "Done" << endl
;
1093 if (Cache
.BrokenCount() != 0)
1095 // See if this is the result of a hold
1096 pkgCache::PkgIterator I
= Cache
.PkgBegin();
1097 for (;I
.end() != true; I
++)
1099 if (Cache
[I
].InstBroken() == false)
1101 if ((Flags
[I
->ID
] & Protected
) != Protected
)
1102 return _error
->Error(_("Error, pkgProblemResolver::Resolve generated breaks, this may be caused by held packages."));
1104 return _error
->Error(_("Unable to correct problems, you have held broken packages."));
1107 // set the auto-flags (mvo: I'm not sure if we _really_ need this, but
1109 pkgCache::PkgIterator I
= Cache
.PkgBegin();
1110 for (;I
.end() != true; I
++) {
1111 if (Cache
[I
].NewInstall() && !(Flags
[I
->ID
] & PreInstalled
)) {
1112 if(_config
->FindI("Debug::pkgAutoRemove",false)) {
1113 std::clog
<< "Resolve installed new pkg: " << I
.Name()
1114 << " (now marking it as auto)" << std::endl
;
1116 Cache
[I
].Flags
|= pkgCache::Flag::Auto
;
1124 // ProblemResolver::ResolveByKeep - Resolve problems using keep /*{{{*/
1125 // ---------------------------------------------------------------------
1126 /* This is the work horse of the soft upgrade routine. It is very gental
1127 in that it does not install or remove any packages. It is assumed that the
1128 system was non-broken previously. */
1129 bool pkgProblemResolver::ResolveByKeep()
1131 pkgDepCache::ActionGroup
group(Cache
);
1133 unsigned long Size
= Cache
.Head().PackageCount
;
1136 clog
<< "Entering ResolveByKeep" << endl
;
1140 /* We have to order the packages so that the broken fixing pass
1141 operates from highest score to lowest. This prevents problems when
1142 high score packages cause the removal of lower score packages that
1143 would cause the removal of even lower score packages. */
1144 pkgCache::Package
**PList
= new pkgCache::Package
*[Size
];
1145 pkgCache::Package
**PEnd
= PList
;
1146 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
1149 qsort(PList
,PEnd
- PList
,sizeof(*PList
),&ScoreSort
);
1151 // Consider each broken package
1152 pkgCache::Package
**LastStop
= 0;
1153 for (pkgCache::Package
**K
= PList
; K
!= PEnd
; K
++)
1155 pkgCache::PkgIterator
I(Cache
,*K
);
1157 if (Cache
[I
].InstallVer
== 0 || Cache
[I
].InstBroken() == false)
1160 /* Keep the package. If this works then great, otherwise we have
1161 to be significantly more agressive and manipulate its dependencies */
1162 if ((Flags
[I
->ID
] & Protected
) == 0)
1165 clog
<< "Keeping package " << I
.Name() << endl
;
1166 Cache
.MarkKeep(I
, false, false);
1167 if (Cache
[I
].InstBroken() == false)
1174 // Isolate the problem dependencies
1175 for (pkgCache::DepIterator D
= Cache
[I
].InstVerIter(Cache
).DependsList(); D
.end() == false;)
1179 D
.GlobOr(Start
,End
);
1181 // We only worry about critical deps.
1182 if (End
.IsCritical() != true)
1186 if ((Cache
[End
] & pkgDepCache::DepGInstall
) == pkgDepCache::DepGInstall
)
1189 /* Hm, the group is broken.. I suppose the best thing to do is to
1190 is to try every combination of keep/not-keep for the set, but thats
1191 slow, and this never happens, just be conservative and assume the
1192 list of ors is in preference and keep till it starts to work. */
1196 clog
<< "Package " << I
.Name() << " has broken dep on " << Start
.TargetPkg().Name() << endl
;
1198 // Look at all the possible provides on this package
1199 SPtrArray
<pkgCache::Version
*> VList
= Start
.AllTargets();
1200 for (pkgCache::Version
**V
= VList
; *V
!= 0; V
++)
1202 pkgCache::VerIterator
Ver(Cache
,*V
);
1203 pkgCache::PkgIterator Pkg
= Ver
.ParentPkg();
1205 // It is not keepable
1206 if (Cache
[Pkg
].InstallVer
== 0 ||
1207 Pkg
->CurrentVer
== 0)
1210 if ((Flags
[I
->ID
] & Protected
) == 0)
1213 clog
<< " Keeping Package " << Pkg
.Name() << " due to dep" << endl
;
1214 Cache
.MarkKeep(Pkg
, false, false);
1217 if (Cache
[I
].InstBroken() == false)
1221 if (Cache
[I
].InstBroken() == false)
1229 if (Cache
[I
].InstBroken() == false)
1233 if (Cache
[I
].InstBroken() == true)
1238 return _error
->Error("Internal Error, pkgProblemResolver::ResolveByKeep is looping on package %s.",I
.Name());
1246 // ProblemResolver::InstallProtect - Install all protected packages /*{{{*/
1247 // ---------------------------------------------------------------------
1248 /* This is used to make sure protected packages are installed */
1249 void pkgProblemResolver::InstallProtect()
1251 pkgDepCache::ActionGroup
group(Cache
);
1253 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
1255 if ((Flags
[I
->ID
] & Protected
) == Protected
)
1257 if ((Flags
[I
->ID
] & ToRemove
) == ToRemove
)
1258 Cache
.MarkDelete(I
);
1261 // preserver the information if the package was auto
1262 // or manual installed
1263 bool autoInst
= (Cache
[I
].Flags
& pkgCache::Flag::Auto
);
1264 Cache
.MarkInstall(I
, false, 0, !autoInst
);
1271 // PrioSortList - Sort a list of versions by priority /*{{{*/
1272 // ---------------------------------------------------------------------
1273 /* This is ment to be used in conjunction with AllTargets to get a list
1274 of versions ordered by preference. */
1275 static pkgCache
*PrioCache
;
1276 static int PrioComp(const void *A
,const void *B
)
1278 pkgCache::VerIterator
L(*PrioCache
,*(pkgCache::Version
**)A
);
1279 pkgCache::VerIterator
R(*PrioCache
,*(pkgCache::Version
**)B
);
1281 if ((L
.ParentPkg()->Flags
& pkgCache::Flag::Essential
) == pkgCache::Flag::Essential
&&
1282 (R
.ParentPkg()->Flags
& pkgCache::Flag::Essential
) != pkgCache::Flag::Essential
)
1284 if ((L
.ParentPkg()->Flags
& pkgCache::Flag::Essential
) != pkgCache::Flag::Essential
&&
1285 (R
.ParentPkg()->Flags
& pkgCache::Flag::Essential
) == pkgCache::Flag::Essential
)
1288 if (L
->Priority
!= R
->Priority
)
1289 return R
->Priority
- L
->Priority
;
1290 return strcmp(L
.ParentPkg().Name(),R
.ParentPkg().Name());
1292 void pkgPrioSortList(pkgCache
&Cache
,pkgCache::Version
**List
)
1294 unsigned long Count
= 0;
1296 for (pkgCache::Version
**I
= List
; *I
!= 0; I
++)
1298 qsort(List
,Count
,sizeof(*List
),PrioComp
);