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
)
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());
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
->Type
== pkgCache::Dep::Conflicts
||
105 Start
->Type
== pkgCache::Dep::DpkgBreaks
||
106 Start
->Type
== pkgCache::Dep::Obsoletes
||
107 End
->Type
== pkgCache::Dep::PreDepends
)
109 if ((Sim
[End
] & pkgDepCache::DepGInstall
) == 0)
111 cout
<< " [" << I
.Name() << " on " << Start
.TargetPkg().Name() << ']';
112 if (Start
->Type
== pkgCache::Dep::Conflicts
)
113 _error
->Error("Fatal, conflicts violated %s",I
.Name());
119 if (Sim
.BrokenCount() != 0)
126 // Simulate::Configure - Simulate configuration of a Package /*{{{*/
127 // ---------------------------------------------------------------------
128 /* This is not an acurate simulation of relatity, we should really not
129 install the package.. For some investigations it may be necessary
131 bool pkgSimulate::Configure(PkgIterator iPkg
)
133 // Adapt the iterator
134 PkgIterator Pkg
= Sim
.FindPkg(iPkg
.Name());
137 // Sim.MarkInstall(Pkg,false);
138 if (Sim
[Pkg
].InstBroken() == true)
140 cout
<< "Conf " << Pkg
.Name() << " broken" << endl
;
144 // Print out each package and the failed dependencies
145 for (pkgCache::DepIterator D
= Sim
[Pkg
].InstVerIter(Sim
).DependsList(); D
.end() == false; D
++)
147 if (Sim
.IsImportantDep(D
) == false ||
148 (Sim
[D
] & pkgDepCache::DepInstall
) != 0)
151 if (D
->Type
== pkgCache::Dep::Obsoletes
)
152 cout
<< " Obsoletes:" << D
.TargetPkg().Name();
153 else if (D
->Type
== pkgCache::Dep::Conflicts
)
154 cout
<< " Conflicts:" << D
.TargetPkg().Name();
155 else if (D
->Type
== pkgCache::Dep::DpkgBreaks
)
156 cout
<< " Breaks:" << D
.TargetPkg().Name();
158 cout
<< " Depends:" << D
.TargetPkg().Name();
162 _error
->Error("Conf Broken %s",Pkg
.Name());
167 Describe(Pkg
,cout
,false,true);
170 if (Sim
.BrokenCount() != 0)
178 // Simulate::Remove - Simulate the removal of a package /*{{{*/
179 // ---------------------------------------------------------------------
181 bool pkgSimulate::Remove(PkgIterator iPkg
,bool Purge
)
183 // Adapt the iterator
184 PkgIterator Pkg
= Sim
.FindPkg(iPkg
.Name());
192 Describe(Pkg
,cout
,true,false);
194 if (Sim
.BrokenCount() != 0)
202 // Simulate::ShortBreaks - Print out a short line describing all breaks /*{{{*/
203 // ---------------------------------------------------------------------
205 void pkgSimulate::ShortBreaks()
208 for (PkgIterator I
= Sim
.PkgBegin(); I
.end() == false; I
++)
210 if (Sim
[I
].InstBroken() == true)
212 if (Flags
[I
->ID
] == 0)
213 cout
<< I
.Name() << ' ';
215 cout << I.Name() << "! ";*/
221 // ApplyStatus - Adjust for non-ok packages /*{{{*/
222 // ---------------------------------------------------------------------
223 /* We attempt to change the state of the all packages that have failed
224 installation toward their real state. The ordering code will perform
225 the necessary calculations to deal with the problems. */
226 bool pkgApplyStatus(pkgDepCache
&Cache
)
228 pkgDepCache::ActionGroup
group(Cache
);
230 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
232 if (I
->VersionList
== 0)
235 // Only choice for a ReInstReq package is to reinstall
236 if (I
->InstState
== pkgCache::State::ReInstReq
||
237 I
->InstState
== pkgCache::State::HoldReInstReq
)
239 if (I
->CurrentVer
!= 0 && I
.CurrentVer().Downloadable() == true)
240 Cache
.MarkKeep(I
, false, false);
243 // Is this right? Will dpkg choke on an upgrade?
244 if (Cache
[I
].CandidateVer
!= 0 &&
245 Cache
[I
].CandidateVerIter(Cache
).Downloadable() == true)
246 Cache
.MarkInstall(I
, false, 0, false);
248 return _error
->Error(_("The package %s needs to be reinstalled, "
249 "but I can't find an archive for it."),I
.Name());
255 switch (I
->CurrentState
)
257 /* This means installation failed somehow - it does not need to be
258 re-unpacked (probably) */
259 case pkgCache::State::UnPacked
:
260 case pkgCache::State::HalfConfigured
:
261 case pkgCache::State::TriggersAwaited
:
262 case pkgCache::State::TriggersPending
:
263 if ((I
->CurrentVer
!= 0 && I
.CurrentVer().Downloadable() == true) ||
264 I
.State() != pkgCache::PkgIterator::NeedsUnpack
)
265 Cache
.MarkKeep(I
, false, false);
268 if (Cache
[I
].CandidateVer
!= 0 &&
269 Cache
[I
].CandidateVerIter(Cache
).Downloadable() == true)
270 Cache
.MarkInstall(I
, true, 0, false);
276 // This means removal failed
277 case pkgCache::State::HalfInstalled
:
282 if (I
->InstState
!= pkgCache::State::Ok
)
283 return _error
->Error("The package %s is not ok and I "
284 "don't know how to fix it!",I
.Name());
290 // FixBroken - Fix broken packages /*{{{*/
291 // ---------------------------------------------------------------------
292 /* This autoinstalls every broken package and then runs the problem resolver
294 bool pkgFixBroken(pkgDepCache
&Cache
)
296 pkgDepCache::ActionGroup
group(Cache
);
298 // Auto upgrade all broken packages
299 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
300 if (Cache
[I
].NowBroken() == true)
301 Cache
.MarkInstall(I
, true, 0, false);
303 /* Fix packages that are in a NeedArchive state but don't have a
304 downloadable install version */
305 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
307 if (I
.State() != pkgCache::PkgIterator::NeedsUnpack
||
308 Cache
[I
].Delete() == true)
311 if (Cache
[I
].InstVerIter(Cache
).Downloadable() == false)
314 Cache
.MarkInstall(I
, true, 0, false);
317 pkgProblemResolver
Fix(&Cache
);
318 return Fix
.Resolve(true);
321 // DistUpgrade - Distribution upgrade /*{{{*/
322 // ---------------------------------------------------------------------
323 /* This autoinstalls every package and then force installs every
324 pre-existing package. This creates the initial set of conditions which
325 most likely contain problems because too many things were installed.
327 The problem resolver is used to resolve the problems.
329 bool pkgDistUpgrade(pkgDepCache
&Cache
)
331 pkgDepCache::ActionGroup
group(Cache
);
333 /* Auto upgrade all installed packages, this provides the basis
334 for the installation */
335 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
336 if (I
->CurrentVer
!= 0)
337 Cache
.MarkInstall(I
, true, 0, false);
339 /* Now, auto upgrade all essential packages - this ensures that
340 the essential packages are present and working */
341 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
342 if ((I
->Flags
& pkgCache::Flag::Essential
) == pkgCache::Flag::Essential
)
343 Cache
.MarkInstall(I
, true, 0, false);
345 /* We do it again over all previously installed packages to force
346 conflict resolution on them all. */
347 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
348 if (I
->CurrentVer
!= 0)
349 Cache
.MarkInstall(I
, false, 0, false);
351 pkgProblemResolver
Fix(&Cache
);
353 // Hold back held packages.
354 if (_config
->FindB("APT::Ignore-Hold",false) == false)
356 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
358 if (I
->SelectedState
== pkgCache::State::Hold
)
361 Cache
.MarkKeep(I
, false, false);
366 return Fix
.Resolve();
369 // AllUpgrade - Upgrade as many packages as possible /*{{{*/
370 // ---------------------------------------------------------------------
371 /* Right now the system must be consistent before this can be called.
372 It also will not change packages marked for install, it only tries
373 to install packages not marked for install */
374 bool pkgAllUpgrade(pkgDepCache
&Cache
)
376 pkgDepCache::ActionGroup
group(Cache
);
378 pkgProblemResolver
Fix(&Cache
);
380 if (Cache
.BrokenCount() != 0)
383 // Upgrade all installed packages
384 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
386 if (Cache
[I
].Install() == true)
389 if (_config
->FindB("APT::Ignore-Hold",false) == false)
390 if (I
->SelectedState
== pkgCache::State::Hold
)
393 if (I
->CurrentVer
!= 0 && Cache
[I
].InstallVer
!= 0)
394 Cache
.MarkInstall(I
, false, 0, false);
397 return Fix
.ResolveByKeep();
400 // MinimizeUpgrade - Minimizes the set of packages to be upgraded /*{{{*/
401 // ---------------------------------------------------------------------
402 /* This simply goes over the entire set of packages and tries to keep
403 each package marked for upgrade. If a conflict is generated then
404 the package is restored. */
405 bool pkgMinimizeUpgrade(pkgDepCache
&Cache
)
407 pkgDepCache::ActionGroup
group(Cache
);
409 if (Cache
.BrokenCount() != 0)
412 // We loop for 10 tries to get the minimal set size.
414 unsigned int Count
= 0;
418 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
421 if (Cache
[I
].Upgrade() == false || Cache
[I
].NewInstall() == true)
424 // Keep it and see if that is OK
425 Cache
.MarkKeep(I
, false, false);
426 if (Cache
.BrokenCount() != 0)
427 Cache
.MarkInstall(I
, false, 0, false);
430 // If keep didnt actually do anything then there was no change..
431 if (Cache
[I
].Upgrade() == false)
437 while (Change
== true && Count
< 10);
439 if (Cache
.BrokenCount() != 0)
440 return _error
->Error("Internal Error in pkgMinimizeUpgrade");
446 // ProblemResolver::pkgProblemResolver - Constructor /*{{{*/
447 // ---------------------------------------------------------------------
449 pkgProblemResolver::pkgProblemResolver(pkgDepCache
*pCache
) : Cache(*pCache
)
452 unsigned long Size
= Cache
.Head().PackageCount
;
453 Scores
= new signed short[Size
];
454 Flags
= new unsigned char[Size
];
455 memset(Flags
,0,sizeof(*Flags
)*Size
);
457 // Set debug to true to see its decision logic
458 Debug
= _config
->FindB("Debug::pkgProblemResolver",false);
461 // ProblemResolver::~pkgProblemResolver - Destructor /*{{{*/
462 // ---------------------------------------------------------------------
464 pkgProblemResolver::~pkgProblemResolver()
470 // ProblemResolver::ScoreSort - Sort the list by score /*{{{*/
471 // ---------------------------------------------------------------------
473 int pkgProblemResolver::ScoreSort(const void *a
,const void *b
)
475 Package
const **A
= (Package
const **)a
;
476 Package
const **B
= (Package
const **)b
;
477 if (This
->Scores
[(*A
)->ID
] > This
->Scores
[(*B
)->ID
])
479 if (This
->Scores
[(*A
)->ID
] < This
->Scores
[(*B
)->ID
])
484 // ProblemResolver::MakeScores - Make the score table /*{{{*/
485 // ---------------------------------------------------------------------
487 void pkgProblemResolver::MakeScores()
489 unsigned long Size
= Cache
.Head().PackageCount
;
490 memset(Scores
,0,sizeof(*Scores
)*Size
);
492 // Important Required Standard Optional Extra
493 signed short PrioMap
[] = {
495 _config
->FindI("pkgProblemResolver::Scores::Important",3),
496 _config
->FindI("pkgProblemResolver::Scores::Required",2),
497 _config
->FindI("pkgProblemResolver::Scores::Standard",1),
498 _config
->FindI("pkgProblemResolver::Scores::Optional",-1),
499 _config
->FindI("pkgProblemResolver::Scores::Extra",-2)
501 signed short PrioEssentials
= _config
->FindI("pkgProblemResolver::Scores::Essentials",100);
502 signed short PrioInstalledAndNotObsolete
= _config
->FindI("pkgProblemResolver::Scores::NotObsolete",1);
503 signed short PrioDepends
= _config
->FindI("pkgProblemResolver::Scores::Depends",1);
504 signed short PrioRecommends
= _config
->FindI("pkgProblemResolver::Scores::Recommends",1);
505 signed short AddProtected
= _config
->FindI("pkgProblemResolver::Scores::AddProtected",10000);
506 signed short AddEssential
= _config
->FindI("pkgProblemResolver::Scores::AddEssential",5000);
508 if (_config
->FindB("Debug::pkgProblemResolver::ShowScores",false) == true)
509 clog
<< "Settings used to calculate pkgProblemResolver::Scores::" << endl
510 << " Important => " << PrioMap
[1] << endl
511 << " Required => " << PrioMap
[2] << endl
512 << " Standard => " << PrioMap
[3] << endl
513 << " Optional => " << PrioMap
[4] << endl
514 << " Extra => " << PrioMap
[5] << endl
515 << " Essentials => " << PrioEssentials
<< endl
516 << " InstalledAndNotObsolete => " << PrioInstalledAndNotObsolete
<< endl
517 << " Depends => " << PrioDepends
<< endl
518 << " Recommends => " << PrioRecommends
<< endl
519 << " AddProtected => " << AddProtected
<< endl
520 << " AddEssential => " << AddEssential
<< endl
;
522 // Generate the base scores for a package based on its properties
523 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
525 if (Cache
[I
].InstallVer
== 0)
528 signed short &Score
= Scores
[I
->ID
];
530 /* This is arbitrary, it should be high enough to elevate an
531 essantial package above most other packages but low enough
532 to allow an obsolete essential packages to be removed by
533 a conflicts on a powerfull normal package (ie libc6) */
534 if ((I
->Flags
& pkgCache::Flag::Essential
) == pkgCache::Flag::Essential
)
535 Score
+= PrioEssentials
;
537 // We transform the priority
538 if (Cache
[I
].InstVerIter(Cache
)->Priority
<= 5)
539 Score
+= PrioMap
[Cache
[I
].InstVerIter(Cache
)->Priority
];
541 /* This helps to fix oddball problems with conflicting packages
542 on the same level. We enhance the score of installed packages
543 if those are not obsolete
545 if (I
->CurrentVer
!= 0 && Cache
[I
].CandidateVer
!= 0 && Cache
[I
].CandidateVerIter(Cache
).Downloadable())
546 Score
+= PrioInstalledAndNotObsolete
;
549 // Now that we have the base scores we go and propogate dependencies
550 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
552 if (Cache
[I
].InstallVer
== 0)
555 for (pkgCache::DepIterator D
= Cache
[I
].InstVerIter(Cache
).DependsList(); D
.end() == false; D
++)
557 if (D
->Type
== pkgCache::Dep::Depends
||
558 D
->Type
== pkgCache::Dep::PreDepends
)
559 Scores
[D
.TargetPkg()->ID
] += PrioDepends
;
560 else if (D
->Type
== pkgCache::Dep::Recommends
)
561 Scores
[D
.TargetPkg()->ID
] += PrioRecommends
;
565 // Copy the scores to advoid additive looping
566 SPtrArray
<signed short> OldScores
= new signed short[Size
];
567 memcpy(OldScores
,Scores
,sizeof(*Scores
)*Size
);
569 /* Now we cause 1 level of dependency inheritance, that is we add the
570 score of the packages that depend on the target Package. This
571 fortifies high scoring packages */
572 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
574 if (Cache
[I
].InstallVer
== 0)
577 for (pkgCache::DepIterator D
= I
.RevDependsList(); D
.end() == false; D
++)
579 // Only do it for the install version
580 if ((pkgCache::Version
*)D
.ParentVer() != Cache
[D
.ParentPkg()].InstallVer
||
581 (D
->Type
!= pkgCache::Dep::Depends
&&
582 D
->Type
!= pkgCache::Dep::PreDepends
&&
583 D
->Type
!= pkgCache::Dep::Recommends
))
586 Scores
[I
->ID
] += abs(OldScores
[D
.ParentPkg()->ID
]);
590 /* Now we propogate along provides. This makes the packages that
591 provide important packages extremely important */
592 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
594 for (pkgCache::PrvIterator P
= I
.ProvidesList(); P
.end() == false; P
++)
596 // Only do it once per package
597 if ((pkgCache::Version
*)P
.OwnerVer() != Cache
[P
.OwnerPkg()].InstallVer
)
599 Scores
[P
.OwnerPkg()->ID
] += abs(Scores
[I
->ID
] - OldScores
[I
->ID
]);
603 /* Protected things are pushed really high up. This number should put them
604 ahead of everything */
605 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
607 if ((Flags
[I
->ID
] & Protected
) != 0)
608 Scores
[I
->ID
] += AddProtected
;
609 if ((I
->Flags
& pkgCache::Flag::Essential
) == pkgCache::Flag::Essential
)
610 Scores
[I
->ID
] += AddEssential
;
614 // ProblemResolver::DoUpgrade - Attempt to upgrade this package /*{{{*/
615 // ---------------------------------------------------------------------
616 /* This goes through and tries to reinstall packages to make this package
618 bool pkgProblemResolver::DoUpgrade(pkgCache::PkgIterator Pkg
)
620 pkgDepCache::ActionGroup
group(Cache
);
622 if ((Flags
[Pkg
->ID
] & Upgradable
) == 0 || Cache
[Pkg
].Upgradable() == false)
624 if ((Flags
[Pkg
->ID
] & Protected
) == Protected
)
627 Flags
[Pkg
->ID
] &= ~Upgradable
;
629 bool WasKept
= Cache
[Pkg
].Keep();
630 Cache
.MarkInstall(Pkg
, false, 0, false);
632 // This must be a virtual package or something like that.
633 if (Cache
[Pkg
].InstVerIter(Cache
).end() == true)
636 // Isolate the problem dependency
638 for (pkgCache::DepIterator D
= Cache
[Pkg
].InstVerIter(Cache
).DependsList(); D
.end() == false;)
640 // Compute a single dependency element (glob or)
641 pkgCache::DepIterator Start
= D
;
642 pkgCache::DepIterator End
= D
;
643 unsigned char State
= 0;
644 for (bool LastOR
= true; D
.end() == false && LastOR
== true;)
647 LastOR
= (D
->CompareOp
& pkgCache::Dep::Or
) == pkgCache::Dep::Or
;
653 // We only worry about critical deps.
654 if (End
.IsCritical() != true)
657 // Iterate over all the members in the or group
661 if ((Cache
[End
] & pkgDepCache::DepGInstall
) == pkgDepCache::DepGInstall
)
664 // Do not change protected packages
665 PkgIterator P
= Start
.SmartTargetPkg();
666 if ((Flags
[P
->ID
] & Protected
) == Protected
)
669 clog
<< " Reinst Failed because of protected " << P
.Name() << endl
;
674 // Upgrade the package if the candidate version will fix the problem.
675 if ((Cache
[Start
] & pkgDepCache::DepCVer
) == pkgDepCache::DepCVer
)
677 if (DoUpgrade(P
) == false)
680 clog
<< " Reinst Failed because of " << P
.Name() << endl
;
691 /* We let the algorithm deal with conflicts on its next iteration,
692 it is much smarter than us */
693 if (Start
->Type
== pkgCache::Dep::Conflicts
||
694 Start
->Type
== pkgCache::Dep::DpkgBreaks
||
695 Start
->Type
== pkgCache::Dep::Obsoletes
)
699 clog
<< " Reinst Failed early because of " << Start
.TargetPkg().Name() << endl
;
712 // Undo our operations - it might be smart to undo everything this did..
716 Cache
.MarkKeep(Pkg
, false, false);
718 Cache
.MarkDelete(Pkg
);
723 clog
<< " Re-Instated " << Pkg
.Name() << endl
;
727 // ProblemResolver::Resolve - Run the resolution pass /*{{{*/
728 // ---------------------------------------------------------------------
729 /* This routines works by calculating a score for each package. The score
730 is derived by considering the package's priority and all reverse
731 dependents giving an integer that reflects the amount of breakage that
732 adjusting the package will inflict.
734 It goes from highest score to lowest and corrects all of the breaks by
735 keeping or removing the dependant packages. If that fails then it removes
736 the package itself and goes on. The routine should be able to intelligently
737 go from any broken state to a fixed state.
739 The BrokenFix flag enables a mode where the algorithm tries to
740 upgrade packages to advoid problems. */
741 bool pkgProblemResolver::Resolve(bool BrokenFix
)
743 pkgDepCache::ActionGroup
group(Cache
);
745 unsigned long Size
= Cache
.Head().PackageCount
;
747 // Record which packages are marked for install
752 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
754 if (Cache
[I
].Install() == true)
755 Flags
[I
->ID
] |= PreInstalled
;
758 if (Cache
[I
].InstBroken() == true && BrokenFix
== true)
760 Cache
.MarkInstall(I
, false, 0, false);
761 if (Cache
[I
].Install() == true)
765 Flags
[I
->ID
] &= ~PreInstalled
;
767 Flags
[I
->ID
] |= Upgradable
;
770 while (Again
== true);
773 clog
<< "Starting" << endl
;
777 /* We have to order the packages so that the broken fixing pass
778 operates from highest score to lowest. This prevents problems when
779 high score packages cause the removal of lower score packages that
780 would cause the removal of even lower score packages. */
781 SPtrArray
<pkgCache::Package
*> PList
= new pkgCache::Package
*[Size
];
782 pkgCache::Package
**PEnd
= PList
;
783 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
786 qsort(PList
,PEnd
- PList
,sizeof(*PList
),&ScoreSort
);
788 if (_config
->FindB("Debug::pkgProblemResolver::ShowScores",false) == true)
790 clog
<< "Show Scores" << endl
;
791 for (pkgCache::Package
**K
= PList
; K
!= PEnd
; K
++)
792 if (Scores
[(*K
)->ID
] != 0)
794 pkgCache::PkgIterator
Pkg(Cache
,*K
);
795 clog
<< Scores
[(*K
)->ID
] << ' ' << Pkg
<< std::endl
;
800 clog
<< "Starting 2" << endl
;
802 /* Now consider all broken packages. For each broken package we either
803 remove the package or fix it's problem. We do this once, it should
804 not be possible for a loop to form (that is a < b < c and fixing b by
805 changing a breaks c) */
807 for (int Counter
= 0; Counter
!= 10 && Change
== true; Counter
++)
810 for (pkgCache::Package
**K
= PList
; K
!= PEnd
; K
++)
812 pkgCache::PkgIterator
I(Cache
,*K
);
814 /* We attempt to install this and see if any breaks result,
815 this takes care of some strange cases */
816 if (Cache
[I
].CandidateVer
!= Cache
[I
].InstallVer
&&
817 I
->CurrentVer
!= 0 && Cache
[I
].InstallVer
!= 0 &&
818 (Flags
[I
->ID
] & PreInstalled
) != 0 &&
819 (Flags
[I
->ID
] & Protected
) == 0 &&
820 (Flags
[I
->ID
] & ReInstateTried
) == 0)
823 clog
<< " Try to Re-Instate " << I
.Name() << endl
;
824 unsigned long OldBreaks
= Cache
.BrokenCount();
825 pkgCache::Version
*OldVer
= Cache
[I
].InstallVer
;
826 Flags
[I
->ID
] &= ReInstateTried
;
828 Cache
.MarkInstall(I
, false, 0, false);
829 if (Cache
[I
].InstBroken() == true ||
830 OldBreaks
< Cache
.BrokenCount())
835 Cache
.MarkKeep(I
, false, false);
839 clog
<< "Re-Instated " << I
.Name() << " (" << OldBreaks
<< " vs " << Cache
.BrokenCount() << ')' << endl
;
842 if (Cache
[I
].InstallVer
== 0 || Cache
[I
].InstBroken() == false)
846 clog
<< "Investigating " << I
.Name() << endl
;
848 // Isolate the problem dependency
849 PackageKill KillList
[100];
850 PackageKill
*LEnd
= KillList
;
852 pkgCache::DepIterator Start
;
853 pkgCache::DepIterator End
;
854 PackageKill
*OldEnd
= LEnd
;
856 enum {OrRemove
,OrKeep
} OrOp
= OrRemove
;
857 for (pkgCache::DepIterator D
= Cache
[I
].InstVerIter(Cache
).DependsList();
858 D
.end() == false || InOr
== true;)
860 // Compute a single dependency element (glob or)
866 if (OldEnd
== LEnd
&& OrOp
== OrRemove
)
868 if ((Flags
[I
->ID
] & Protected
) != Protected
)
871 clog
<< " Or group remove for " << I
.Name() << endl
;
876 if (OldEnd
== LEnd
&& OrOp
== OrKeep
)
879 clog
<< " Or group keep for " << I
.Name() << endl
;
880 Cache
.MarkKeep(I
, false, false);
885 /* We do an extra loop (as above) to finalize the or group
890 if (Start
.end() == true)
893 // We only worry about critical deps.
894 if (End
.IsCritical() != true)
903 // We only worry about critical deps.
904 if (Start
.IsCritical() != true)
909 if ((Cache
[End
] & pkgDepCache::DepGInstall
) == pkgDepCache::DepGInstall
)
916 clog
<< "Package " << I
.Name() << " has broken " << Start
.DepType() << " on " << Start
.TargetPkg().Name() << endl
;
918 /* Look across the version list. If there are no possible
919 targets then we keep the package and bail. This is necessary
920 if a package has a dep on another package that cant be found */
921 SPtrArray
<pkgCache::Version
*> VList
= Start
.AllTargets();
922 if (*VList
== 0 && (Flags
[I
->ID
] & Protected
) != Protected
&&
923 Start
->Type
!= pkgCache::Dep::Conflicts
&&
924 Start
->Type
!= pkgCache::Dep::DpkgBreaks
&&
925 Start
->Type
!= pkgCache::Dep::Obsoletes
&&
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();
947 clog
<< " Considering " << Pkg
.Name() << ' ' << (int)Scores
[Pkg
->ID
] <<
948 " as a solution to " << I
.Name() << ' ' << (int)Scores
[I
->ID
] << endl
;
950 /* Try to fix the package under consideration rather than
951 fiddle with the VList package */
952 if (Scores
[I
->ID
] <= Scores
[Pkg
->ID
] ||
953 ((Cache
[Start
] & pkgDepCache::DepNow
) == 0 &&
954 End
->Type
!= pkgCache::Dep::Conflicts
&&
955 End
->Type
!= pkgCache::Dep::DpkgBreaks
&&
956 End
->Type
!= pkgCache::Dep::Obsoletes
))
958 // Try a little harder to fix protected packages..
959 if ((Flags
[I
->ID
] & Protected
) == Protected
)
961 if (DoUpgrade(Pkg
) == true)
963 if (Scores
[Pkg
->ID
] > Scores
[I
->ID
])
964 Scores
[Pkg
->ID
] = Scores
[I
->ID
];
971 /* See if a keep will do, unless the package is protected,
972 then installing it will be necessary */
973 bool Installed
= Cache
[I
].Install();
974 Cache
.MarkKeep(I
, false, false);
975 if (Cache
[I
].InstBroken() == false)
977 // Unwind operation will be keep now
978 if (OrOp
== OrRemove
)
982 if (InOr
== true && Installed
== true)
983 Cache
.MarkInstall(I
, false, 0, false);
986 clog
<< " Holding Back " << I
.Name() << " rather than change " << Start
.TargetPkg().Name() << endl
;
990 if (BrokenFix
== false || DoUpgrade(I
) == false)
992 // Consider other options
995 if (Cache
.AutoInstOk(I
, Cache
[I
].CandidateVerIter(Cache
),Start
) == true)
998 clog
<< " Removing " << I
.Name() << " rather than change " << Start
.TargetPkg().Name() << endl
;
1002 if (Scores
[Pkg
->ID
] > Scores
[I
->ID
])
1003 Scores
[I
->ID
] = Scores
[Pkg
->ID
];
1006 /* The dependency of the TargetPkg would be satisfiable with I but it is
1007 forbidden to install I automatical, so anything we can do is hold
1011 clog
<< " Hold back " << Start
.TargetPkg().Name() <<
1012 " rather than change denied AutoInstall " << I
.Name() << endl
;
1013 Cache
.MarkKeep(Start
.TargetPkg());
1025 /* This is a conflicts, and the version we are looking
1026 at is not the currently selected version of the
1027 package, which means it is not necessary to
1029 if (Cache
[Pkg
].InstallVer
!= Ver
&&
1030 (Start
->Type
== pkgCache::Dep::Conflicts
||
1031 Start
->Type
== pkgCache::Dep::Obsoletes
))
1034 if (Start
->Type
== pkgCache::Dep::DpkgBreaks
)
1036 // first, try upgradring the package, if that
1037 // does not help, the breaks goes onto the
1039 // FIXME: use DoUpgrade(Pkg) instead?
1040 if (Cache
[End
] & pkgDepCache::DepGCVer
)
1043 clog
<< " Upgrading " << Pkg
.Name() << " due to Breaks field in " << I
.Name() << endl
;
1044 Cache
.MarkInstall(Pkg
, false, 0, false);
1049 // Skip adding to the kill list if it is protected
1050 if ((Flags
[Pkg
->ID
] & Protected
) != 0)
1054 clog
<< " Added " << Pkg
.Name() << " to the remove list" << endl
;
1060 if (Start
->Type
!= pkgCache::Dep::Conflicts
&&
1061 Start
->Type
!= pkgCache::Dep::Obsoletes
)
1066 // Hm, nothing can possibly satisify this dep. Nuke it.
1067 if (VList
[0] == 0 &&
1068 Start
->Type
!= pkgCache::Dep::Conflicts
&&
1069 Start
->Type
!= pkgCache::Dep::DpkgBreaks
&&
1070 Start
->Type
!= pkgCache::Dep::Obsoletes
&&
1071 (Flags
[I
->ID
] & Protected
) != Protected
)
1073 bool Installed
= Cache
[I
].Install();
1075 if (Cache
[I
].InstBroken() == false)
1077 // Unwind operation will be keep now
1078 if (OrOp
== OrRemove
)
1082 if (InOr
== true && Installed
== true)
1083 Cache
.MarkInstall(I
, false, 0, false);
1086 clog
<< " Holding Back " << I
.Name() << " because I can't find " << Start
.TargetPkg().Name() << endl
;
1091 clog
<< " Removing " << I
.Name() << " because I can't find " << Start
.TargetPkg().Name() << endl
;
1093 Cache
.MarkDelete(I
);
1108 // Apply the kill list now
1109 if (Cache
[I
].InstallVer
!= 0)
1111 for (PackageKill
*J
= KillList
; J
!= LEnd
; J
++)
1114 if ((Cache
[J
->Dep
] & pkgDepCache::DepGNow
) == 0)
1116 if (J
->Dep
->Type
== pkgCache::Dep::Conflicts
||
1117 J
->Dep
->Type
== pkgCache::Dep::DpkgBreaks
||
1118 J
->Dep
->Type
== pkgCache::Dep::Obsoletes
)
1121 clog
<< " Fixing " << I
.Name() << " via remove of " << J
->Pkg
.Name() << endl
;
1122 Cache
.MarkDelete(J
->Pkg
);
1128 clog
<< " Fixing " << I
.Name() << " via keep of " << J
->Pkg
.Name() << endl
;
1129 Cache
.MarkKeep(J
->Pkg
, false, false);
1134 if (Scores
[I
->ID
] > Scores
[J
->Pkg
->ID
])
1135 Scores
[J
->Pkg
->ID
] = Scores
[I
->ID
];
1143 clog
<< "Done" << endl
;
1145 if (Cache
.BrokenCount() != 0)
1147 // See if this is the result of a hold
1148 pkgCache::PkgIterator I
= Cache
.PkgBegin();
1149 for (;I
.end() != true; I
++)
1151 if (Cache
[I
].InstBroken() == false)
1153 if ((Flags
[I
->ID
] & Protected
) != Protected
)
1154 return _error
->Error(_("Error, pkgProblemResolver::Resolve generated breaks, this may be caused by held packages."));
1156 return _error
->Error(_("Unable to correct problems, you have held broken packages."));
1159 // set the auto-flags (mvo: I'm not sure if we _really_ need this, but
1161 pkgCache::PkgIterator I
= Cache
.PkgBegin();
1162 for (;I
.end() != true; I
++) {
1163 if (Cache
[I
].NewInstall() && !(Flags
[I
->ID
] & PreInstalled
)) {
1164 if(_config
->FindI("Debug::pkgAutoRemove",false)) {
1165 std::clog
<< "Resolve installed new pkg: " << I
.Name()
1166 << " (now marking it as auto)" << std::endl
;
1168 Cache
[I
].Flags
|= pkgCache::Flag::Auto
;
1176 // ProblemResolver::ResolveByKeep - Resolve problems using keep /*{{{*/
1177 // ---------------------------------------------------------------------
1178 /* This is the work horse of the soft upgrade routine. It is very gental
1179 in that it does not install or remove any packages. It is assumed that the
1180 system was non-broken previously. */
1181 bool pkgProblemResolver::ResolveByKeep()
1183 pkgDepCache::ActionGroup
group(Cache
);
1185 unsigned long Size
= Cache
.Head().PackageCount
;
1189 /* We have to order the packages so that the broken fixing pass
1190 operates from highest score to lowest. This prevents problems when
1191 high score packages cause the removal of lower score packages that
1192 would cause the removal of even lower score packages. */
1193 pkgCache::Package
**PList
= new pkgCache::Package
*[Size
];
1194 pkgCache::Package
**PEnd
= PList
;
1195 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
1198 qsort(PList
,PEnd
- PList
,sizeof(*PList
),&ScoreSort
);
1200 if (_config
->FindB("Debug::pkgProblemResolver::ShowScores",false) == true)
1202 clog
<< "Show Scores" << endl
;
1203 for (pkgCache::Package
**K
= PList
; K
!= PEnd
; K
++)
1204 if (Scores
[(*K
)->ID
] != 0)
1206 pkgCache::PkgIterator
Pkg(Cache
,*K
);
1207 clog
<< Scores
[(*K
)->ID
] << ' ' << Pkg
<< std::endl
;
1212 clog
<< "Entering ResolveByKeep" << endl
;
1214 // Consider each broken package
1215 pkgCache::Package
**LastStop
= 0;
1216 for (pkgCache::Package
**K
= PList
; K
!= PEnd
; K
++)
1218 pkgCache::PkgIterator
I(Cache
,*K
);
1220 if (Cache
[I
].InstallVer
== 0 || Cache
[I
].InstBroken() == false)
1223 /* Keep the package. If this works then great, otherwise we have
1224 to be significantly more agressive and manipulate its dependencies */
1225 if ((Flags
[I
->ID
] & Protected
) == 0)
1228 clog
<< "Keeping package " << I
.Name() << endl
;
1229 Cache
.MarkKeep(I
, false, false);
1230 if (Cache
[I
].InstBroken() == false)
1237 // Isolate the problem dependencies
1238 for (pkgCache::DepIterator D
= Cache
[I
].InstVerIter(Cache
).DependsList(); D
.end() == false;)
1242 D
.GlobOr(Start
,End
);
1244 // We only worry about critical deps.
1245 if (End
.IsCritical() != true)
1249 if ((Cache
[End
] & pkgDepCache::DepGInstall
) == pkgDepCache::DepGInstall
)
1252 /* Hm, the group is broken.. I suppose the best thing to do is to
1253 is to try every combination of keep/not-keep for the set, but thats
1254 slow, and this never happens, just be conservative and assume the
1255 list of ors is in preference and keep till it starts to work. */
1259 clog
<< "Package " << I
.Name() << " has broken " << Start
.DepType() << " on " << Start
.TargetPkg().Name() << endl
;
1261 // Look at all the possible provides on this package
1262 SPtrArray
<pkgCache::Version
*> VList
= Start
.AllTargets();
1263 for (pkgCache::Version
**V
= VList
; *V
!= 0; V
++)
1265 pkgCache::VerIterator
Ver(Cache
,*V
);
1266 pkgCache::PkgIterator Pkg
= Ver
.ParentPkg();
1268 // It is not keepable
1269 if (Cache
[Pkg
].InstallVer
== 0 ||
1270 Pkg
->CurrentVer
== 0)
1273 if ((Flags
[I
->ID
] & Protected
) == 0)
1276 clog
<< " Keeping Package " << Pkg
.Name() << " due to " << Start
.DepType() << endl
;
1277 Cache
.MarkKeep(Pkg
, false, false);
1280 if (Cache
[I
].InstBroken() == false)
1284 if (Cache
[I
].InstBroken() == false)
1292 if (Cache
[I
].InstBroken() == false)
1296 if (Cache
[I
].InstBroken() == true)
1301 return _error
->Error("Internal Error, pkgProblemResolver::ResolveByKeep is looping on package %s.",I
.Name());
1309 // ProblemResolver::InstallProtect - Install all protected packages /*{{{*/
1310 // ---------------------------------------------------------------------
1311 /* This is used to make sure protected packages are installed */
1312 void pkgProblemResolver::InstallProtect()
1314 pkgDepCache::ActionGroup
group(Cache
);
1316 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
1318 if ((Flags
[I
->ID
] & Protected
) == Protected
)
1320 if ((Flags
[I
->ID
] & ToRemove
) == ToRemove
)
1321 Cache
.MarkDelete(I
);
1324 // preserve the information whether the package was auto
1325 // or manually installed
1326 bool autoInst
= (Cache
[I
].Flags
& pkgCache::Flag::Auto
);
1327 Cache
.MarkInstall(I
, false, 0, !autoInst
);
1334 // PrioSortList - Sort a list of versions by priority /*{{{*/
1335 // ---------------------------------------------------------------------
1336 /* This is ment to be used in conjunction with AllTargets to get a list
1337 of versions ordered by preference. */
1338 static pkgCache
*PrioCache
;
1339 static int PrioComp(const void *A
,const void *B
)
1341 pkgCache::VerIterator
L(*PrioCache
,*(pkgCache::Version
**)A
);
1342 pkgCache::VerIterator
R(*PrioCache
,*(pkgCache::Version
**)B
);
1344 if ((L
.ParentPkg()->Flags
& pkgCache::Flag::Essential
) == pkgCache::Flag::Essential
&&
1345 (R
.ParentPkg()->Flags
& pkgCache::Flag::Essential
) != pkgCache::Flag::Essential
)
1347 if ((L
.ParentPkg()->Flags
& pkgCache::Flag::Essential
) != pkgCache::Flag::Essential
&&
1348 (R
.ParentPkg()->Flags
& pkgCache::Flag::Essential
) == pkgCache::Flag::Essential
)
1351 if (L
->Priority
!= R
->Priority
)
1352 return R
->Priority
- L
->Priority
;
1353 return strcmp(L
.ParentPkg().Name(),R
.ParentPkg().Name());
1355 void pkgPrioSortList(pkgCache
&Cache
,pkgCache::Version
**List
)
1357 unsigned long Count
= 0;
1359 for (pkgCache::Version
**I
= List
; *I
!= 0; I
++)
1361 qsort(List
,Count
,sizeof(*List
),PrioComp
);
1365 // CacheFile::ListUpdate - update the cache files /*{{{*/
1366 // ---------------------------------------------------------------------
1367 /* This is a simple wrapper to update the cache. it will fetch stuff
1368 * from the network (or any other sources defined in sources.list)
1370 bool ListUpdate(pkgAcquireStatus
&Stat
,
1371 pkgSourceList
&List
,
1374 pkgAcquire::RunResult res
;
1375 pkgAcquire
Fetcher(&Stat
);
1377 // Populate it with the source selection
1378 if (List
.GetIndexes(&Fetcher
) == false)
1382 RunScripts("APT::Update::Pre-Invoke");
1386 res
= Fetcher
.Run(PulseInterval
);
1388 res
= Fetcher
.Run();
1390 if (res
== pkgAcquire::Failed
)
1393 bool Failed
= false;
1394 bool TransientNetworkFailure
= false;
1395 for (pkgAcquire::ItemIterator I
= Fetcher
.ItemsBegin();
1396 I
!= Fetcher
.ItemsEnd(); I
++)
1398 if ((*I
)->Status
== pkgAcquire::Item::StatDone
)
1403 ::URI
uri((*I
)->DescURI());
1405 uri
.Password
.clear();
1406 string descUri
= string(uri
);
1407 _error
->Warning(_("Failed to fetch %s %s\n"), descUri
.c_str(),
1408 (*I
)->ErrorText
.c_str());
1410 if ((*I
)->Status
== pkgAcquire::Item::StatTransientNetworkError
)
1412 TransientNetworkFailure
= true;
1419 // Clean out any old list files
1420 // Keep "APT::Get::List-Cleanup" name for compatibility, but
1421 // this is really a global option for the APT library now
1422 if (!TransientNetworkFailure
&& !Failed
&&
1423 (_config
->FindB("APT::Get::List-Cleanup",true) == true &&
1424 _config
->FindB("APT::List-Cleanup",true) == true))
1426 if (Fetcher
.Clean(_config
->FindDir("Dir::State::lists")) == false ||
1427 Fetcher
.Clean(_config
->FindDir("Dir::State::lists") + "partial/") == false)
1428 // something went wrong with the clean
1432 if (TransientNetworkFailure
== true)
1433 _error
->Warning(_("Some index files failed to download, they have been ignored, or old ones used instead."));
1434 else if (Failed
== true)
1435 return _error
->Error(_("Some index files failed to download, they have been ignored, or old ones used instead."));
1438 // Run the success scripts if all was fine
1439 if(!TransientNetworkFailure
&& !Failed
)
1440 RunScripts("APT::Update::Post-Invoke-Success");
1442 // Run the other scripts
1443 RunScripts("APT::Update::Post-Invoke");