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>
22 #include <apt-pkg/acquire-item.h>
25 #include <sys/types.h>
32 pkgProblemResolver
*pkgProblemResolver::This
= 0;
34 // Simulate::Simulate - Constructor /*{{{*/
35 // ---------------------------------------------------------------------
36 /* The legacy translations here of input Pkg iterators is obsolete,
37 this is not necessary since the pkgCaches are fully shared now. */
38 pkgSimulate::pkgSimulate(pkgDepCache
*Cache
) : pkgPackageManager(Cache
),
40 Sim(&Cache
->GetCache(),&iPolicy
),
44 Flags
= new unsigned char[Cache
->Head().PackageCount
];
45 memset(Flags
,0,sizeof(*Flags
)*Cache
->Head().PackageCount
);
47 // Fake a filename so as not to activate the media swapping
48 string Jnk
= "SIMULATE";
49 for (unsigned int I
= 0; I
!= Cache
->Head().PackageCount
; I
++)
53 // Simulate::Describe - Describe a package /*{{{*/
54 // ---------------------------------------------------------------------
55 /* Parameter Current == true displays the current package version,
56 Parameter Candidate == true displays the candidate package version */
57 void pkgSimulate::Describe(PkgIterator Pkg
,ostream
&out
,bool Current
,bool Candidate
)
61 out
<< Pkg
.FullName(true);
65 Ver
= Pkg
.CurrentVer();
66 if (Ver
.end() == false)
67 out
<< " [" << Ver
.VerStr() << ']';
70 if (Candidate
== true)
72 Ver
= Sim
[Pkg
].CandidateVerIter(Sim
);
73 if (Ver
.end() == true)
76 out
<< " (" << Ver
.VerStr() << ' ' << Ver
.RelStr() << ')';
80 // Simulate::Install - Simulate unpacking of a package /*{{{*/
81 // ---------------------------------------------------------------------
83 bool pkgSimulate::Install(PkgIterator iPkg
,string
/*File*/)
86 PkgIterator Pkg
= Sim
.FindPkg(iPkg
.Name(), iPkg
.Arch());
90 Describe(Pkg
,cout
,true,true);
91 Sim
.MarkInstall(Pkg
,false);
93 // Look for broken conflicts+predepends.
94 for (PkgIterator I
= Sim
.PkgBegin(); I
.end() == false; I
++)
96 if (Sim
[I
].InstallVer
== 0)
99 for (DepIterator D
= Sim
[I
].InstVerIter(Sim
).DependsList(); D
.end() == false;)
104 if (Start
.IsNegative() == true ||
105 End
->Type
== pkgCache::Dep::PreDepends
)
107 if ((Sim
[End
] & pkgDepCache::DepGInstall
) == 0)
109 cout
<< " [" << I
.FullName(false) << " on " << Start
.TargetPkg().FullName(false) << ']';
110 if (Start
->Type
== pkgCache::Dep::Conflicts
)
111 _error
->Error("Fatal, conflicts violated %s",I
.FullName(false).c_str());
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(), iPkg
.Arch());
136 if (Sim
[Pkg
].InstBroken() == true)
138 cout
<< "Conf " << Pkg
.FullName(false) << " 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().FullName(false);
151 else if (D
->Type
== pkgCache::Dep::Conflicts
)
152 cout
<< " Conflicts:" << D
.TargetPkg().FullName(false);
153 else if (D
->Type
== pkgCache::Dep::DpkgBreaks
)
154 cout
<< " Breaks:" << D
.TargetPkg().FullName(false);
156 cout
<< " Depends:" << D
.TargetPkg().FullName(false);
160 _error
->Error("Conf Broken %s",Pkg
.FullName(false).c_str());
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(), iPkg
.Arch());
191 Describe(Pkg
,cout
,true,false);
193 if (Sim
.BrokenCount() != 0)
201 // Simulate::ShortBreaks - Print out a short line describing all breaks /*{{{*/
202 // ---------------------------------------------------------------------
204 void pkgSimulate::ShortBreaks()
207 for (PkgIterator I
= Sim
.PkgBegin(); I
.end() == false; I
++)
209 if (Sim
[I
].InstBroken() == true)
211 if (Flags
[I
->ID
] == 0)
212 cout
<< I
.FullName(false) << ' ';
214 cout << I.Name() << "! ";*/
220 // ApplyStatus - Adjust for non-ok packages /*{{{*/
221 // ---------------------------------------------------------------------
222 /* We attempt to change the state of the all packages that have failed
223 installation toward their real state. The ordering code will perform
224 the necessary calculations to deal with the problems. */
225 bool pkgApplyStatus(pkgDepCache
&Cache
)
227 pkgDepCache::ActionGroup
group(Cache
);
229 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
231 if (I
->VersionList
== 0)
234 // Only choice for a ReInstReq package is to reinstall
235 if (I
->InstState
== pkgCache::State::ReInstReq
||
236 I
->InstState
== pkgCache::State::HoldReInstReq
)
238 if (I
->CurrentVer
!= 0 && I
.CurrentVer().Downloadable() == true)
239 Cache
.MarkKeep(I
, false, false);
242 // Is this right? Will dpkg choke on an upgrade?
243 if (Cache
[I
].CandidateVer
!= 0 &&
244 Cache
[I
].CandidateVerIter(Cache
).Downloadable() == true)
245 Cache
.MarkInstall(I
, false, 0, false);
247 return _error
->Error(_("The package %s needs to be reinstalled, "
248 "but I can't find an archive for it."),I
.FullName(true).c_str());
254 switch (I
->CurrentState
)
256 /* This means installation failed somehow - it does not need to be
257 re-unpacked (probably) */
258 case pkgCache::State::UnPacked
:
259 case pkgCache::State::HalfConfigured
:
260 case pkgCache::State::TriggersAwaited
:
261 case pkgCache::State::TriggersPending
:
262 if ((I
->CurrentVer
!= 0 && I
.CurrentVer().Downloadable() == true) ||
263 I
.State() != pkgCache::PkgIterator::NeedsUnpack
)
264 Cache
.MarkKeep(I
, false, false);
267 if (Cache
[I
].CandidateVer
!= 0 &&
268 Cache
[I
].CandidateVerIter(Cache
).Downloadable() == true)
269 Cache
.MarkInstall(I
, true, 0, false);
275 // This means removal failed
276 case pkgCache::State::HalfInstalled
:
281 if (I
->InstState
!= pkgCache::State::Ok
)
282 return _error
->Error("The package %s is not ok and I "
283 "don't know how to fix it!",I
.FullName(false).c_str());
289 // FixBroken - Fix broken packages /*{{{*/
290 // ---------------------------------------------------------------------
291 /* This autoinstalls every broken package and then runs the problem resolver
293 bool pkgFixBroken(pkgDepCache
&Cache
)
295 pkgDepCache::ActionGroup
group(Cache
);
297 // Auto upgrade all broken packages
298 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
299 if (Cache
[I
].NowBroken() == true)
300 Cache
.MarkInstall(I
, true, 0, false);
302 /* Fix packages that are in a NeedArchive state but don't have a
303 downloadable install version */
304 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
306 if (I
.State() != pkgCache::PkgIterator::NeedsUnpack
||
307 Cache
[I
].Delete() == true)
310 if (Cache
[I
].InstVerIter(Cache
).Downloadable() == false)
313 Cache
.MarkInstall(I
, true, 0, false);
316 pkgProblemResolver
Fix(&Cache
);
317 return Fix
.Resolve(true);
320 // DistUpgrade - Distribution upgrade /*{{{*/
321 // ---------------------------------------------------------------------
322 /* This autoinstalls every package and then force installs every
323 pre-existing package. This creates the initial set of conditions which
324 most likely contain problems because too many things were installed.
326 The problem resolver is used to resolve the problems.
328 bool pkgDistUpgrade(pkgDepCache
&Cache
)
330 pkgDepCache::ActionGroup
group(Cache
);
332 /* Upgrade all installed packages first without autoinst to help the resolver
333 in versioned or-groups to upgrade the old solver instead of installing
334 a new one (if the old solver is not the first one [anymore]) */
335 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; ++I
)
336 if (I
->CurrentVer
!= 0)
337 Cache
.MarkInstall(I
, false, 0, false);
339 /* Auto upgrade all installed packages, this provides the basis
340 for the installation */
341 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
342 if (I
->CurrentVer
!= 0)
343 Cache
.MarkInstall(I
, true, 0, false);
345 /* Now, auto upgrade all essential packages - this ensures that
346 the essential packages are present and working */
347 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
348 if ((I
->Flags
& pkgCache::Flag::Essential
) == pkgCache::Flag::Essential
)
349 Cache
.MarkInstall(I
, true, 0, false);
351 /* We do it again over all previously installed packages to force
352 conflict resolution on them all. */
353 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
354 if (I
->CurrentVer
!= 0)
355 Cache
.MarkInstall(I
, false, 0, false);
357 pkgProblemResolver
Fix(&Cache
);
359 // Hold back held packages.
360 if (_config
->FindB("APT::Ignore-Hold",false) == false)
362 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
364 if (I
->SelectedState
== pkgCache::State::Hold
)
367 Cache
.MarkKeep(I
, false, false);
372 return Fix
.Resolve();
375 // AllUpgrade - Upgrade as many packages as possible /*{{{*/
376 // ---------------------------------------------------------------------
377 /* Right now the system must be consistent before this can be called.
378 It also will not change packages marked for install, it only tries
379 to install packages not marked for install */
380 bool pkgAllUpgrade(pkgDepCache
&Cache
)
382 pkgDepCache::ActionGroup
group(Cache
);
384 pkgProblemResolver
Fix(&Cache
);
386 if (Cache
.BrokenCount() != 0)
389 // Upgrade all installed packages
390 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
392 if (Cache
[I
].Install() == true)
395 if (_config
->FindB("APT::Ignore-Hold",false) == false)
396 if (I
->SelectedState
== pkgCache::State::Hold
)
399 if (I
->CurrentVer
!= 0 && Cache
[I
].InstallVer
!= 0)
400 Cache
.MarkInstall(I
, false, 0, false);
403 return Fix
.ResolveByKeep();
406 // MinimizeUpgrade - Minimizes the set of packages to be upgraded /*{{{*/
407 // ---------------------------------------------------------------------
408 /* This simply goes over the entire set of packages and tries to keep
409 each package marked for upgrade. If a conflict is generated then
410 the package is restored. */
411 bool pkgMinimizeUpgrade(pkgDepCache
&Cache
)
413 pkgDepCache::ActionGroup
group(Cache
);
415 if (Cache
.BrokenCount() != 0)
418 // We loop for 10 tries to get the minimal set size.
420 unsigned int Count
= 0;
424 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
427 if (Cache
[I
].Upgrade() == false || Cache
[I
].NewInstall() == true)
430 // Keep it and see if that is OK
431 Cache
.MarkKeep(I
, false, false);
432 if (Cache
.BrokenCount() != 0)
433 Cache
.MarkInstall(I
, false, 0, false);
436 // If keep didnt actually do anything then there was no change..
437 if (Cache
[I
].Upgrade() == false)
443 while (Change
== true && Count
< 10);
445 if (Cache
.BrokenCount() != 0)
446 return _error
->Error("Internal Error in pkgMinimizeUpgrade");
451 // ProblemResolver::pkgProblemResolver - Constructor /*{{{*/
452 // ---------------------------------------------------------------------
454 pkgProblemResolver::pkgProblemResolver(pkgDepCache
*pCache
) : Cache(*pCache
)
457 unsigned long Size
= Cache
.Head().PackageCount
;
458 Scores
= new signed short[Size
];
459 Flags
= new unsigned char[Size
];
460 memset(Flags
,0,sizeof(*Flags
)*Size
);
462 // Set debug to true to see its decision logic
463 Debug
= _config
->FindB("Debug::pkgProblemResolver",false);
466 // ProblemResolver::~pkgProblemResolver - Destructor /*{{{*/
467 // ---------------------------------------------------------------------
469 pkgProblemResolver::~pkgProblemResolver()
475 // ProblemResolver::ScoreSort - Sort the list by score /*{{{*/
476 // ---------------------------------------------------------------------
478 int pkgProblemResolver::ScoreSort(const void *a
,const void *b
)
480 Package
const **A
= (Package
const **)a
;
481 Package
const **B
= (Package
const **)b
;
482 if (This
->Scores
[(*A
)->ID
] > This
->Scores
[(*B
)->ID
])
484 if (This
->Scores
[(*A
)->ID
] < This
->Scores
[(*B
)->ID
])
489 // ProblemResolver::MakeScores - Make the score table /*{{{*/
490 // ---------------------------------------------------------------------
492 void pkgProblemResolver::MakeScores()
494 unsigned long Size
= Cache
.Head().PackageCount
;
495 memset(Scores
,0,sizeof(*Scores
)*Size
);
497 // Important Required Standard Optional Extra
498 signed short PrioMap
[] = {
500 (signed short) _config
->FindI("pkgProblemResolver::Scores::Important",3),
501 (signed short) _config
->FindI("pkgProblemResolver::Scores::Required",2),
502 (signed short) _config
->FindI("pkgProblemResolver::Scores::Standard",1),
503 (signed short) _config
->FindI("pkgProblemResolver::Scores::Optional",-1),
504 (signed short) _config
->FindI("pkgProblemResolver::Scores::Extra",-2)
506 signed short PrioEssentials
= _config
->FindI("pkgProblemResolver::Scores::Essentials",100);
507 signed short PrioInstalledAndNotObsolete
= _config
->FindI("pkgProblemResolver::Scores::NotObsolete",1);
508 signed short PrioDepends
= _config
->FindI("pkgProblemResolver::Scores::Depends",1);
509 signed short PrioRecommends
= _config
->FindI("pkgProblemResolver::Scores::Recommends",1);
510 signed short AddProtected
= _config
->FindI("pkgProblemResolver::Scores::AddProtected",10000);
511 signed short AddEssential
= _config
->FindI("pkgProblemResolver::Scores::AddEssential",5000);
513 if (_config
->FindB("Debug::pkgProblemResolver::ShowScores",false) == true)
514 clog
<< "Settings used to calculate pkgProblemResolver::Scores::" << endl
515 << " Important => " << PrioMap
[1] << endl
516 << " Required => " << PrioMap
[2] << endl
517 << " Standard => " << PrioMap
[3] << endl
518 << " Optional => " << PrioMap
[4] << endl
519 << " Extra => " << PrioMap
[5] << endl
520 << " Essentials => " << PrioEssentials
<< endl
521 << " InstalledAndNotObsolete => " << PrioInstalledAndNotObsolete
<< endl
522 << " Depends => " << PrioDepends
<< endl
523 << " Recommends => " << PrioRecommends
<< endl
524 << " AddProtected => " << AddProtected
<< endl
525 << " AddEssential => " << AddEssential
<< endl
;
527 // Generate the base scores for a package based on its properties
528 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
530 if (Cache
[I
].InstallVer
== 0)
533 signed short &Score
= Scores
[I
->ID
];
535 /* This is arbitrary, it should be high enough to elevate an
536 essantial package above most other packages but low enough
537 to allow an obsolete essential packages to be removed by
538 a conflicts on a powerfull normal package (ie libc6) */
539 if ((I
->Flags
& pkgCache::Flag::Essential
) == pkgCache::Flag::Essential
)
540 Score
+= PrioEssentials
;
542 // We transform the priority
543 if (Cache
[I
].InstVerIter(Cache
)->Priority
<= 5)
544 Score
+= PrioMap
[Cache
[I
].InstVerIter(Cache
)->Priority
];
546 /* This helps to fix oddball problems with conflicting packages
547 on the same level. We enhance the score of installed packages
548 if those are not obsolete
550 if (I
->CurrentVer
!= 0 && Cache
[I
].CandidateVer
!= 0 && Cache
[I
].CandidateVerIter(Cache
).Downloadable())
551 Score
+= PrioInstalledAndNotObsolete
;
554 // Now that we have the base scores we go and propogate dependencies
555 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
557 if (Cache
[I
].InstallVer
== 0)
560 for (pkgCache::DepIterator D
= Cache
[I
].InstVerIter(Cache
).DependsList(); D
.end() == false; D
++)
562 if (D
->Type
== pkgCache::Dep::Depends
||
563 D
->Type
== pkgCache::Dep::PreDepends
)
564 Scores
[D
.TargetPkg()->ID
] += PrioDepends
;
565 else if (D
->Type
== pkgCache::Dep::Recommends
)
566 Scores
[D
.TargetPkg()->ID
] += PrioRecommends
;
570 // Copy the scores to advoid additive looping
571 SPtrArray
<signed short> OldScores
= new signed short[Size
];
572 memcpy(OldScores
,Scores
,sizeof(*Scores
)*Size
);
574 /* Now we cause 1 level of dependency inheritance, that is we add the
575 score of the packages that depend on the target Package. This
576 fortifies high scoring packages */
577 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
579 if (Cache
[I
].InstallVer
== 0)
582 for (pkgCache::DepIterator D
= I
.RevDependsList(); D
.end() == false; D
++)
584 // Only do it for the install version
585 if ((pkgCache::Version
*)D
.ParentVer() != Cache
[D
.ParentPkg()].InstallVer
||
586 (D
->Type
!= pkgCache::Dep::Depends
&&
587 D
->Type
!= pkgCache::Dep::PreDepends
&&
588 D
->Type
!= pkgCache::Dep::Recommends
))
591 Scores
[I
->ID
] += abs(OldScores
[D
.ParentPkg()->ID
]);
595 /* Now we propogate along provides. This makes the packages that
596 provide important packages extremely important */
597 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
599 for (pkgCache::PrvIterator P
= I
.ProvidesList(); P
.end() == false; P
++)
601 // Only do it once per package
602 if ((pkgCache::Version
*)P
.OwnerVer() != Cache
[P
.OwnerPkg()].InstallVer
)
604 Scores
[P
.OwnerPkg()->ID
] += abs(Scores
[I
->ID
] - OldScores
[I
->ID
]);
608 /* Protected things are pushed really high up. This number should put them
609 ahead of everything */
610 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
612 if ((Flags
[I
->ID
] & Protected
) != 0)
613 Scores
[I
->ID
] += AddProtected
;
614 if ((I
->Flags
& pkgCache::Flag::Essential
) == pkgCache::Flag::Essential
)
615 Scores
[I
->ID
] += AddEssential
;
619 // ProblemResolver::DoUpgrade - Attempt to upgrade this package /*{{{*/
620 // ---------------------------------------------------------------------
621 /* This goes through and tries to reinstall packages to make this package
623 bool pkgProblemResolver::DoUpgrade(pkgCache::PkgIterator Pkg
)
625 pkgDepCache::ActionGroup
group(Cache
);
627 if ((Flags
[Pkg
->ID
] & Upgradable
) == 0 || Cache
[Pkg
].Upgradable() == false)
629 if ((Flags
[Pkg
->ID
] & Protected
) == Protected
)
632 Flags
[Pkg
->ID
] &= ~Upgradable
;
634 bool WasKept
= Cache
[Pkg
].Keep();
635 Cache
.MarkInstall(Pkg
, false, 0, false);
637 // This must be a virtual package or something like that.
638 if (Cache
[Pkg
].InstVerIter(Cache
).end() == true)
641 // Isolate the problem dependency
643 for (pkgCache::DepIterator D
= Cache
[Pkg
].InstVerIter(Cache
).DependsList(); D
.end() == false;)
645 // Compute a single dependency element (glob or)
646 pkgCache::DepIterator Start
= D
;
647 pkgCache::DepIterator End
= D
;
648 for (bool LastOR
= true; D
.end() == false && LastOR
== true;)
650 LastOR
= (D
->CompareOp
& pkgCache::Dep::Or
) == pkgCache::Dep::Or
;
656 // We only worry about critical deps.
657 if (End
.IsCritical() != true)
660 // Iterate over all the members in the or group
664 if ((Cache
[End
] & pkgDepCache::DepGInstall
) == pkgDepCache::DepGInstall
)
667 // Do not change protected packages
668 PkgIterator P
= Start
.SmartTargetPkg();
669 if ((Flags
[P
->ID
] & Protected
) == Protected
)
672 clog
<< " Reinst Failed because of protected " << P
.FullName(false) << endl
;
677 // Upgrade the package if the candidate version will fix the problem.
678 if ((Cache
[Start
] & pkgDepCache::DepCVer
) == pkgDepCache::DepCVer
)
680 if (DoUpgrade(P
) == false)
683 clog
<< " Reinst Failed because of " << P
.FullName(false) << endl
;
694 /* We let the algorithm deal with conflicts on its next iteration,
695 it is much smarter than us */
696 if (Start
.IsNegative() == true)
700 clog
<< " Reinst Failed early because of " << Start
.TargetPkg().FullName(false) << endl
;
713 // Undo our operations - it might be smart to undo everything this did..
717 Cache
.MarkKeep(Pkg
, false, false);
719 Cache
.MarkDelete(Pkg
);
724 clog
<< " Re-Instated " << Pkg
.FullName(false) << endl
;
728 // ProblemResolver::Resolve - Run the resolution pass /*{{{*/
729 // ---------------------------------------------------------------------
730 /* This routines works by calculating a score for each package. The score
731 is derived by considering the package's priority and all reverse
732 dependents giving an integer that reflects the amount of breakage that
733 adjusting the package will inflict.
735 It goes from highest score to lowest and corrects all of the breaks by
736 keeping or removing the dependant packages. If that fails then it removes
737 the package itself and goes on. The routine should be able to intelligently
738 go from any broken state to a fixed state.
740 The BrokenFix flag enables a mode where the algorithm tries to
741 upgrade packages to advoid problems. */
742 bool pkgProblemResolver::Resolve(bool BrokenFix
)
744 pkgDepCache::ActionGroup
group(Cache
);
746 unsigned long Size
= Cache
.Head().PackageCount
;
748 // Record which packages are marked for install
753 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
755 if (Cache
[I
].Install() == true)
756 Flags
[I
->ID
] |= PreInstalled
;
759 if (Cache
[I
].InstBroken() == true && BrokenFix
== true)
761 Cache
.MarkInstall(I
, false, 0, false);
762 if (Cache
[I
].Install() == true)
766 Flags
[I
->ID
] &= ~PreInstalled
;
768 Flags
[I
->ID
] |= Upgradable
;
771 while (Again
== true);
774 clog
<< "Starting" << endl
;
778 /* We have to order the packages so that the broken fixing pass
779 operates from highest score to lowest. This prevents problems when
780 high score packages cause the removal of lower score packages that
781 would cause the removal of even lower score packages. */
782 SPtrArray
<pkgCache::Package
*> PList
= new pkgCache::Package
*[Size
];
783 pkgCache::Package
**PEnd
= PList
;
784 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
787 qsort(PList
,PEnd
- PList
,sizeof(*PList
),&ScoreSort
);
789 if (_config
->FindB("Debug::pkgProblemResolver::ShowScores",false) == true)
791 clog
<< "Show Scores" << endl
;
792 for (pkgCache::Package
**K
= PList
; K
!= PEnd
; K
++)
793 if (Scores
[(*K
)->ID
] != 0)
795 pkgCache::PkgIterator
Pkg(Cache
,*K
);
796 clog
<< Scores
[(*K
)->ID
] << ' ' << Pkg
<< std::endl
;
801 clog
<< "Starting 2" << endl
;
803 /* Now consider all broken packages. For each broken package we either
804 remove the package or fix it's problem. We do this once, it should
805 not be possible for a loop to form (that is a < b < c and fixing b by
806 changing a breaks c) */
808 bool const TryFixByInstall
= _config
->FindB("pkgProblemResolver::FixByInstall", true);
809 for (int Counter
= 0; Counter
!= 10 && Change
== true; Counter
++)
812 for (pkgCache::Package
**K
= PList
; K
!= PEnd
; K
++)
814 pkgCache::PkgIterator
I(Cache
,*K
);
816 /* We attempt to install this and see if any breaks result,
817 this takes care of some strange cases */
818 if (Cache
[I
].CandidateVer
!= Cache
[I
].InstallVer
&&
819 I
->CurrentVer
!= 0 && Cache
[I
].InstallVer
!= 0 &&
820 (Flags
[I
->ID
] & PreInstalled
) != 0 &&
821 (Flags
[I
->ID
] & Protected
) == 0 &&
822 (Flags
[I
->ID
] & ReInstateTried
) == 0)
825 clog
<< " Try to Re-Instate (" << Counter
<< ") " << I
.FullName(false) << endl
;
826 unsigned long OldBreaks
= Cache
.BrokenCount();
827 pkgCache::Version
*OldVer
= Cache
[I
].InstallVer
;
828 Flags
[I
->ID
] &= ReInstateTried
;
830 Cache
.MarkInstall(I
, false, 0, false);
831 if (Cache
[I
].InstBroken() == true ||
832 OldBreaks
< Cache
.BrokenCount())
837 Cache
.MarkKeep(I
, false, false);
841 clog
<< "Re-Instated " << I
.FullName(false) << " (" << OldBreaks
<< " vs " << Cache
.BrokenCount() << ')' << endl
;
844 if (Cache
[I
].InstallVer
== 0 || Cache
[I
].InstBroken() == false)
848 clog
<< "Investigating (" << Counter
<< ") " << I
<< endl
;
850 // Isolate the problem dependency
851 PackageKill KillList
[100];
852 PackageKill
*LEnd
= KillList
;
854 pkgCache::DepIterator Start
;
855 pkgCache::DepIterator End
;
856 PackageKill
*OldEnd
= LEnd
;
858 enum {OrRemove
,OrKeep
} OrOp
= OrRemove
;
859 for (pkgCache::DepIterator D
= Cache
[I
].InstVerIter(Cache
).DependsList();
860 D
.end() == false || InOr
== true;)
862 // Compute a single dependency element (glob or)
866 if (InOr
== true && OldEnd
== LEnd
)
868 if (OrOp
== OrRemove
)
870 if ((Flags
[I
->ID
] & Protected
) != Protected
)
873 clog
<< " Or group remove for " << I
.FullName(false) << endl
;
878 else if (OrOp
== OrKeep
)
881 clog
<< " Or group keep for " << I
.FullName(false) << endl
;
882 Cache
.MarkKeep(I
, false, false);
887 /* We do an extra loop (as above) to finalize the or group
892 if (Start
.end() == true)
895 // We only worry about critical deps.
896 if (End
.IsCritical() != true)
905 // We only worry about critical deps.
906 if (Start
.IsCritical() != true)
911 if ((Cache
[End
] & pkgDepCache::DepGInstall
) == pkgDepCache::DepGInstall
)
918 clog
<< "Broken " << Start
<< endl
;
920 /* Look across the version list. If there are no possible
921 targets then we keep the package and bail. This is necessary
922 if a package has a dep on another package that cant be found */
923 SPtrArray
<pkgCache::Version
*> VList
= Start
.AllTargets();
924 if (*VList
== 0 && (Flags
[I
->ID
] & Protected
) != Protected
&&
925 Start
.IsNegative() == false &&
926 Cache
[I
].NowBroken() == false)
930 /* No keep choice because the keep being OK could be the
931 result of another element in the OR group! */
936 Cache
.MarkKeep(I
, false, false);
941 for (pkgCache::Version
**V
= VList
; *V
!= 0; V
++)
943 pkgCache::VerIterator
Ver(Cache
,*V
);
944 pkgCache::PkgIterator Pkg
= Ver
.ParentPkg();
946 /* This is a conflicts, and the version we are looking
947 at is not the currently selected version of the
948 package, which means it is not necessary to
950 if (Cache
[Pkg
].InstallVer
!= Ver
&& Start
.IsNegative() == true)
953 clog
<< " Conflicts//Breaks against version "
954 << Ver
.VerStr() << " for " << Pkg
.Name()
955 << " but that is not InstVer, ignoring"
961 clog
<< " Considering " << Pkg
.FullName(false) << ' ' << (int)Scores
[Pkg
->ID
] <<
962 " as a solution to " << I
.FullName(false) << ' ' << (int)Scores
[I
->ID
] << endl
;
964 /* Try to fix the package under consideration rather than
965 fiddle with the VList package */
966 if (Scores
[I
->ID
] <= Scores
[Pkg
->ID
] ||
967 ((Cache
[Start
] & pkgDepCache::DepNow
) == 0 &&
968 End
.IsNegative() == false))
970 // Try a little harder to fix protected packages..
971 if ((Flags
[I
->ID
] & Protected
) == Protected
)
973 if (DoUpgrade(Pkg
) == true)
975 if (Scores
[Pkg
->ID
] > Scores
[I
->ID
])
976 Scores
[Pkg
->ID
] = Scores
[I
->ID
];
983 /* See if a keep will do, unless the package is protected,
984 then installing it will be necessary */
985 bool Installed
= Cache
[I
].Install();
986 Cache
.MarkKeep(I
, false, false);
987 if (Cache
[I
].InstBroken() == false)
989 // Unwind operation will be keep now
990 if (OrOp
== OrRemove
)
994 if (InOr
== true && Installed
== true)
995 Cache
.MarkInstall(I
, false, 0, false);
998 clog
<< " Holding Back " << I
.FullName(false) << " rather than change " << Start
.TargetPkg().FullName(false) << endl
;
1002 if (BrokenFix
== false || DoUpgrade(I
) == false)
1004 // Consider other options
1008 clog
<< " Removing " << I
.FullName(false) << " rather than change " << Start
.TargetPkg().FullName(false) << endl
;
1009 Cache
.MarkDelete(I
);
1010 if (Counter
> 1 && Scores
[Pkg
->ID
] > Scores
[I
->ID
])
1011 Scores
[I
->ID
] = Scores
[Pkg
->ID
];
1013 else if (TryFixByInstall
== true &&
1014 Start
.TargetPkg()->CurrentVer
== 0 &&
1015 Cache
[Start
.TargetPkg()].Delete() == false &&
1016 (Flags
[Start
.TargetPkg()->ID
] & ToRemove
) != ToRemove
&&
1017 Cache
.GetCandidateVer(Start
.TargetPkg()).end() == false)
1019 /* Before removing or keeping the package with the broken dependency
1020 try instead to install the first not previously installed package
1021 solving this dependency. This helps every time a previous solver
1022 is removed by the resolver because of a conflict or alike but it is
1023 dangerous as it could trigger new breaks/conflicts… */
1025 clog
<< " Try Installing " << Start
.TargetPkg() << " before changing " << I
.FullName(false) << std::endl
;
1026 unsigned long const OldBroken
= Cache
.BrokenCount();
1027 Cache
.MarkInstall(Start
.TargetPkg(), true, 1, false);
1028 // FIXME: we should undo the complete MarkInstall process here
1029 if (Cache
[Start
.TargetPkg()].InstBroken() == true || Cache
.BrokenCount() > OldBroken
)
1030 Cache
.MarkDelete(Start
.TargetPkg(), false, 1, false);
1041 if (Start
->Type
== pkgCache::Dep::DpkgBreaks
)
1043 // first, try upgradring the package, if that
1044 // does not help, the breaks goes onto the
1047 // FIXME: use DoUpgrade(Pkg) instead?
1048 if (Cache
[End
] & pkgDepCache::DepGCVer
)
1051 clog
<< " Upgrading " << Pkg
.FullName(false) << " due to Breaks field in " << I
.FullName(false) << endl
;
1052 Cache
.MarkInstall(Pkg
, false, 0, false);
1057 // Skip adding to the kill list if it is protected
1058 if ((Flags
[Pkg
->ID
] & Protected
) != 0)
1062 clog
<< " Added " << Pkg
.FullName(false) << " to the remove list" << endl
;
1068 if (Start
->Type
!= pkgCache::Dep::Conflicts
&&
1069 Start
->Type
!= pkgCache::Dep::Obsoletes
)
1074 // Hm, nothing can possibly satisify this dep. Nuke it.
1075 if (VList
[0] == 0 &&
1076 Start
.IsNegative() == false &&
1077 (Flags
[I
->ID
] & Protected
) != Protected
)
1079 bool Installed
= Cache
[I
].Install();
1081 if (Cache
[I
].InstBroken() == false)
1083 // Unwind operation will be keep now
1084 if (OrOp
== OrRemove
)
1088 if (InOr
== true && Installed
== true)
1089 Cache
.MarkInstall(I
, false, 0, false);
1092 clog
<< " Holding Back " << I
.FullName(false) << " because I can't find " << Start
.TargetPkg().FullName(false) << endl
;
1097 clog
<< " Removing " << I
.FullName(false) << " because I can't find " << Start
.TargetPkg().FullName(false) << endl
;
1099 Cache
.MarkDelete(I
);
1114 // Apply the kill list now
1115 if (Cache
[I
].InstallVer
!= 0)
1117 for (PackageKill
*J
= KillList
; J
!= LEnd
; J
++)
1120 if ((Cache
[J
->Dep
] & pkgDepCache::DepGNow
) == 0)
1122 if (J
->Dep
.IsNegative() == true)
1125 clog
<< " Fixing " << I
.FullName(false) << " via remove of " << J
->Pkg
.FullName(false) << endl
;
1126 Cache
.MarkDelete(J
->Pkg
);
1132 clog
<< " Fixing " << I
.FullName(false) << " via keep of " << J
->Pkg
.FullName(false) << endl
;
1133 Cache
.MarkKeep(J
->Pkg
, false, false);
1138 if (Scores
[I
->ID
] > Scores
[J
->Pkg
->ID
])
1139 Scores
[J
->Pkg
->ID
] = Scores
[I
->ID
];
1147 clog
<< "Done" << endl
;
1149 if (Cache
.BrokenCount() != 0)
1151 // See if this is the result of a hold
1152 pkgCache::PkgIterator I
= Cache
.PkgBegin();
1153 for (;I
.end() != true; I
++)
1155 if (Cache
[I
].InstBroken() == false)
1157 if ((Flags
[I
->ID
] & Protected
) != Protected
)
1158 return _error
->Error(_("Error, pkgProblemResolver::Resolve generated breaks, this may be caused by held packages."));
1160 return _error
->Error(_("Unable to correct problems, you have held broken packages."));
1163 // set the auto-flags (mvo: I'm not sure if we _really_ need this)
1164 pkgCache::PkgIterator I
= Cache
.PkgBegin();
1165 for (;I
.end() != true; I
++) {
1166 if (Cache
[I
].NewInstall() && !(Flags
[I
->ID
] & PreInstalled
)) {
1167 if(_config
->FindI("Debug::pkgAutoRemove",false)) {
1168 std::clog
<< "Resolve installed new pkg: " << I
.FullName(false)
1169 << " (now marking it as auto)" << std::endl
;
1171 Cache
[I
].Flags
|= pkgCache::Flag::Auto
;
1180 // ProblemResolver::BreaksInstOrPolicy - Check if the given pkg is broken/*{{{*/
1181 // ---------------------------------------------------------------------
1182 /* This checks if the given package is broken either by a hard dependency
1183 (InstBroken()) or by introducing a new policy breakage e.g. new
1184 unsatisfied recommends for a package that was in "policy-good" state
1186 Note that this is not perfect as it will ignore further breakage
1187 for already broken policy (recommends)
1189 bool pkgProblemResolver::InstOrNewPolicyBroken(pkgCache::PkgIterator I
)
1192 // a broken install is always a problem
1193 if (Cache
[I
].InstBroken() == true)
1196 // a newly broken policy (recommends/suggests) is a problem
1197 if (Cache
[I
].NowPolicyBroken() == false &&
1198 Cache
[I
].InstPolicyBroken() == true)
1204 // ProblemResolver::ResolveByKeep - Resolve problems using keep /*{{{*/
1205 // ---------------------------------------------------------------------
1206 /* This is the work horse of the soft upgrade routine. It is very gental
1207 in that it does not install or remove any packages. It is assumed that the
1208 system was non-broken previously. */
1209 bool pkgProblemResolver::ResolveByKeep()
1211 pkgDepCache::ActionGroup
group(Cache
);
1213 unsigned long Size
= Cache
.Head().PackageCount
;
1217 /* We have to order the packages so that the broken fixing pass
1218 operates from highest score to lowest. This prevents problems when
1219 high score packages cause the removal of lower score packages that
1220 would cause the removal of even lower score packages. */
1221 pkgCache::Package
**PList
= new pkgCache::Package
*[Size
];
1222 pkgCache::Package
**PEnd
= PList
;
1223 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
1226 qsort(PList
,PEnd
- PList
,sizeof(*PList
),&ScoreSort
);
1228 if (_config
->FindB("Debug::pkgProblemResolver::ShowScores",false) == true)
1230 clog
<< "Show Scores" << endl
;
1231 for (pkgCache::Package
**K
= PList
; K
!= PEnd
; K
++)
1232 if (Scores
[(*K
)->ID
] != 0)
1234 pkgCache::PkgIterator
Pkg(Cache
,*K
);
1235 clog
<< Scores
[(*K
)->ID
] << ' ' << Pkg
<< std::endl
;
1240 clog
<< "Entering ResolveByKeep" << endl
;
1242 // Consider each broken package
1243 pkgCache::Package
**LastStop
= 0;
1244 for (pkgCache::Package
**K
= PList
; K
!= PEnd
; K
++)
1246 pkgCache::PkgIterator
I(Cache
,*K
);
1248 if (Cache
[I
].InstallVer
== 0)
1251 if (InstOrNewPolicyBroken(I
) == false)
1254 /* Keep the package. If this works then great, otherwise we have
1255 to be significantly more agressive and manipulate its dependencies */
1256 if ((Flags
[I
->ID
] & Protected
) == 0)
1259 clog
<< "Keeping package " << I
.FullName(false) << endl
;
1260 Cache
.MarkKeep(I
, false, false);
1261 if (InstOrNewPolicyBroken(I
) == false)
1268 // Isolate the problem dependencies
1269 for (pkgCache::DepIterator D
= Cache
[I
].InstVerIter(Cache
).DependsList(); D
.end() == false;)
1273 D
.GlobOr(Start
,End
);
1275 // We only worry about critical deps.
1276 if (End
.IsCritical() != true)
1280 if ((Cache
[End
] & pkgDepCache::DepGInstall
) == pkgDepCache::DepGInstall
)
1283 /* Hm, the group is broken.. I suppose the best thing to do is to
1284 is to try every combination of keep/not-keep for the set, but thats
1285 slow, and this never happens, just be conservative and assume the
1286 list of ors is in preference and keep till it starts to work. */
1290 clog
<< "Package " << I
.FullName(false) << " " << Start
<< endl
;
1292 // Look at all the possible provides on this package
1293 SPtrArray
<pkgCache::Version
*> VList
= Start
.AllTargets();
1294 for (pkgCache::Version
**V
= VList
; *V
!= 0; V
++)
1296 pkgCache::VerIterator
Ver(Cache
,*V
);
1297 pkgCache::PkgIterator Pkg
= Ver
.ParentPkg();
1299 // It is not keepable
1300 if (Cache
[Pkg
].InstallVer
== 0 ||
1301 Pkg
->CurrentVer
== 0)
1304 if ((Flags
[I
->ID
] & Protected
) == 0)
1307 clog
<< " Keeping Package " << Pkg
.FullName(false) << " due to " << Start
.DepType() << endl
;
1308 Cache
.MarkKeep(Pkg
, false, false);
1311 if (InstOrNewPolicyBroken(I
) == false)
1315 if (InstOrNewPolicyBroken(I
) == false)
1323 if (InstOrNewPolicyBroken(I
) == false)
1327 if (InstOrNewPolicyBroken(I
) == true)
1332 return _error
->Error("Internal Error, pkgProblemResolver::ResolveByKeep is looping on package %s.",I
.FullName(false).c_str());
1340 // ProblemResolver::InstallProtect - Install all protected packages /*{{{*/
1341 // ---------------------------------------------------------------------
1342 /* This is used to make sure protected packages are installed */
1343 void pkgProblemResolver::InstallProtect()
1345 pkgDepCache::ActionGroup
group(Cache
);
1347 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
1349 if ((Flags
[I
->ID
] & Protected
) == Protected
)
1351 if ((Flags
[I
->ID
] & ToRemove
) == ToRemove
)
1352 Cache
.MarkDelete(I
);
1355 // preserve the information whether the package was auto
1356 // or manually installed
1357 bool autoInst
= (Cache
[I
].Flags
& pkgCache::Flag::Auto
);
1358 Cache
.MarkInstall(I
, false, 0, !autoInst
);
1364 // PrioSortList - Sort a list of versions by priority /*{{{*/
1365 // ---------------------------------------------------------------------
1366 /* This is ment to be used in conjunction with AllTargets to get a list
1367 of versions ordered by preference. */
1368 static pkgCache
*PrioCache
;
1369 static int PrioComp(const void *A
,const void *B
)
1371 pkgCache::VerIterator
L(*PrioCache
,*(pkgCache::Version
**)A
);
1372 pkgCache::VerIterator
R(*PrioCache
,*(pkgCache::Version
**)B
);
1374 if ((L
.ParentPkg()->Flags
& pkgCache::Flag::Essential
) == pkgCache::Flag::Essential
&&
1375 (R
.ParentPkg()->Flags
& pkgCache::Flag::Essential
) != pkgCache::Flag::Essential
)
1377 if ((L
.ParentPkg()->Flags
& pkgCache::Flag::Essential
) != pkgCache::Flag::Essential
&&
1378 (R
.ParentPkg()->Flags
& pkgCache::Flag::Essential
) == pkgCache::Flag::Essential
)
1381 if (L
->Priority
!= R
->Priority
)
1382 return R
->Priority
- L
->Priority
;
1383 return strcmp(L
.ParentPkg().Name(),R
.ParentPkg().Name());
1385 void pkgPrioSortList(pkgCache
&Cache
,pkgCache::Version
**List
)
1387 unsigned long Count
= 0;
1389 for (pkgCache::Version
**I
= List
; *I
!= 0; I
++)
1391 qsort(List
,Count
,sizeof(*List
),PrioComp
);
1394 // CacheFile::ListUpdate - update the cache files /*{{{*/
1395 // ---------------------------------------------------------------------
1396 /* This is a simple wrapper to update the cache. it will fetch stuff
1397 * from the network (or any other sources defined in sources.list)
1399 bool ListUpdate(pkgAcquireStatus
&Stat
,
1400 pkgSourceList
&List
,
1403 pkgAcquire::RunResult res
;
1405 if (Fetcher
.Setup(&Stat
, _config
->FindDir("Dir::State::Lists")) == false)
1408 // Populate it with the source selection
1409 if (List
.GetIndexes(&Fetcher
) == false)
1413 RunScripts("APT::Update::Pre-Invoke");
1417 res
= Fetcher
.Run(PulseInterval
);
1419 res
= Fetcher
.Run();
1421 if (res
== pkgAcquire::Failed
)
1424 bool Failed
= false;
1425 bool TransientNetworkFailure
= false;
1426 for (pkgAcquire::ItemIterator I
= Fetcher
.ItemsBegin();
1427 I
!= Fetcher
.ItemsEnd(); I
++)
1429 if ((*I
)->Status
== pkgAcquire::Item::StatDone
)
1434 ::URI
uri((*I
)->DescURI());
1436 uri
.Password
.clear();
1437 string descUri
= string(uri
);
1438 _error
->Warning(_("Failed to fetch %s %s\n"), descUri
.c_str(),
1439 (*I
)->ErrorText
.c_str());
1441 if ((*I
)->Status
== pkgAcquire::Item::StatTransientNetworkError
)
1443 TransientNetworkFailure
= true;
1450 // Clean out any old list files
1451 // Keep "APT::Get::List-Cleanup" name for compatibility, but
1452 // this is really a global option for the APT library now
1453 if (!TransientNetworkFailure
&& !Failed
&&
1454 (_config
->FindB("APT::Get::List-Cleanup",true) == true &&
1455 _config
->FindB("APT::List-Cleanup",true) == true))
1457 if (Fetcher
.Clean(_config
->FindDir("Dir::State::lists")) == false ||
1458 Fetcher
.Clean(_config
->FindDir("Dir::State::lists") + "partial/") == false)
1459 // something went wrong with the clean
1463 if (TransientNetworkFailure
== true)
1464 _error
->Warning(_("Some index files failed to download. They have been ignored, or old ones used instead."));
1465 else if (Failed
== true)
1466 return _error
->Error(_("Some index files failed to download. They have been ignored, or old ones used instead."));
1469 // Run the success scripts if all was fine
1470 if(!TransientNetworkFailure
&& !Failed
)
1471 RunScripts("APT::Update::Post-Invoke-Success");
1473 // Run the other scripts
1474 RunScripts("APT::Update::Post-Invoke");