]>
git.saurik.com Git - apt.git/blob - apt-pkg/algorithms.cc
98bd8dd8bac9be636be9ce3e1007e5bbf372e3d6
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 /*{{{*/
18 #pragma implementation "apt-pkg/algorithms.h"
20 #include <apt-pkg/algorithms.h>
21 #include <apt-pkg/error.h>
22 #include <apt-pkg/configuration.h>
23 #include <apt-pkg/sptr.h>
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::Obsoletes
||
104 End
->Type
== pkgCache::Dep::PreDepends
)
106 if ((Sim
[End
] & pkgDepCache::DepGInstall
) == 0)
108 cout
<< " [" << I
.Name() << " on " << Start
.TargetPkg().Name() << ']';
109 if (Start
->Type
== pkgCache::Dep::Conflicts
)
110 _error
->Error("Fatal, conflicts violated %s",I
.Name());
116 if (Sim
.BrokenCount() != 0)
123 // Simulate::Configure - Simulate configuration of a Package /*{{{*/
124 // ---------------------------------------------------------------------
125 /* This is not an acurate simulation of relatity, we should really not
126 install the package.. For some investigations it may be necessary
128 bool pkgSimulate::Configure(PkgIterator iPkg
)
130 // Adapt the iterator
131 PkgIterator Pkg
= Sim
.FindPkg(iPkg
.Name());
134 // Sim.MarkInstall(Pkg,false);
135 if (Sim
[Pkg
].InstBroken() == true)
137 cout
<< "Conf " << Pkg
.Name() << " broken" << endl
;
141 // Print out each package and the failed dependencies
142 for (pkgCache::DepIterator D
= Sim
[Pkg
].InstVerIter(Sim
).DependsList(); D
.end() == false; D
++)
144 if (Sim
.IsImportantDep(D
) == false ||
145 (Sim
[D
] & pkgDepCache::DepInstall
) != 0)
148 if (D
->Type
== pkgCache::Dep::Obsoletes
)
149 cout
<< " Obsoletes:" << D
.TargetPkg().Name();
150 else if (D
->Type
== pkgCache::Dep::Conflicts
)
151 cout
<< " Conflicts:" << D
.TargetPkg().Name();
153 cout
<< " Depends:" << D
.TargetPkg().Name();
157 _error
->Error("Conf Broken %s",Pkg
.Name());
162 Describe(Pkg
,cout
,false,true);
165 if (Sim
.BrokenCount() != 0)
173 // Simulate::Remove - Simulate the removal of a package /*{{{*/
174 // ---------------------------------------------------------------------
176 bool pkgSimulate::Remove(PkgIterator iPkg
,bool Purge
)
178 // Adapt the iterator
179 PkgIterator Pkg
= Sim
.FindPkg(iPkg
.Name());
187 Describe(Pkg
,cout
,true,false);
189 if (Sim
.BrokenCount() != 0)
197 // Simulate::ShortBreaks - Print out a short line describing all breaks /*{{{*/
198 // ---------------------------------------------------------------------
200 void pkgSimulate::ShortBreaks()
203 for (PkgIterator I
= Sim
.PkgBegin(); I
.end() == false; I
++)
205 if (Sim
[I
].InstBroken() == true)
207 if (Flags
[I
->ID
] == 0)
208 cout
<< I
.Name() << ' ';
210 cout << I.Name() << "! ";*/
216 // ApplyStatus - Adjust for non-ok packages /*{{{*/
217 // ---------------------------------------------------------------------
218 /* We attempt to change the state of the all packages that have failed
219 installation toward their real state. The ordering code will perform
220 the necessary calculations to deal with the problems. */
221 bool pkgApplyStatus(pkgDepCache
&Cache
)
223 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
225 if (I
->VersionList
== 0)
228 // Only choice for a ReInstReq package is to reinstall
229 if (I
->InstState
== pkgCache::State::ReInstReq
||
230 I
->InstState
== pkgCache::State::HoldReInstReq
)
232 if (I
->CurrentVer
!= 0 && I
.CurrentVer().Downloadable() == true)
236 // Is this right? Will dpkg choke on an upgrade?
237 if (Cache
[I
].CandidateVer
!= 0 &&
238 Cache
[I
].CandidateVerIter(Cache
).Downloadable() == true)
239 Cache
.MarkInstall(I
);
241 return _error
->Error(_("The package %s needs to be reinstalled, "
242 "but I can't find an archive for it."),I
.Name());
248 switch (I
->CurrentState
)
250 /* This means installation failed somehow - it does not need to be
251 re-unpacked (probably) */
252 case pkgCache::State::UnPacked
:
253 case pkgCache::State::HalfConfigured
:
254 if ((I
->CurrentVer
!= 0 && I
.CurrentVer().Downloadable() == true) ||
255 I
.State() != pkgCache::PkgIterator::NeedsUnpack
)
259 if (Cache
[I
].CandidateVer
!= 0 &&
260 Cache
[I
].CandidateVerIter(Cache
).Downloadable() == true)
261 Cache
.MarkInstall(I
);
267 // This means removal failed
268 case pkgCache::State::HalfInstalled
:
273 if (I
->InstState
!= pkgCache::State::Ok
)
274 return _error
->Error("The package %s is not ok and I "
275 "don't know how to fix it!",I
.Name());
281 // FixBroken - Fix broken packages /*{{{*/
282 // ---------------------------------------------------------------------
283 /* This autoinstalls every broken package and then runs the problem resolver
285 bool pkgFixBroken(pkgDepCache
&Cache
)
287 // Auto upgrade all broken packages
288 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
289 if (Cache
[I
].NowBroken() == true)
290 Cache
.MarkInstall(I
,true);
292 /* Fix packages that are in a NeedArchive state but don't have a
293 downloadable install version */
294 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
296 if (I
.State() != pkgCache::PkgIterator::NeedsUnpack
||
297 Cache
[I
].Delete() == true)
300 if (Cache
[I
].InstVerIter(Cache
).Downloadable() == false)
303 Cache
.MarkInstall(I
,true);
306 pkgProblemResolver
Fix(&Cache
);
307 return Fix
.Resolve(true);
310 // DistUpgrade - Distribution upgrade /*{{{*/
311 // ---------------------------------------------------------------------
312 /* This autoinstalls every package and then force installs every
313 pre-existing package. This creates the initial set of conditions which
314 most likely contain problems because too many things were installed.
316 The problem resolver is used to resolve the problems.
318 bool pkgDistUpgrade(pkgDepCache
&Cache
)
320 /* Auto upgrade all installed packages, this provides the basis
321 for the installation */
322 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
323 if (I
->CurrentVer
!= 0)
324 Cache
.MarkInstall(I
,true);
326 /* Now, auto upgrade all essential packages - this ensures that
327 the essential packages are present and working */
328 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
329 if ((I
->Flags
& pkgCache::Flag::Essential
) == pkgCache::Flag::Essential
)
330 Cache
.MarkInstall(I
,true);
332 /* We do it again over all previously installed packages to force
333 conflict resolution on them all. */
334 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
335 if (I
->CurrentVer
!= 0)
336 Cache
.MarkInstall(I
,false);
338 pkgProblemResolver
Fix(&Cache
);
340 // Hold back held packages.
341 if (_config
->FindB("APT::Ignore-Hold",false) == false)
343 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
345 if (I
->SelectedState
== pkgCache::State::Hold
)
353 return Fix
.Resolve();
356 // AllUpgrade - Upgrade as many packages as possible /*{{{*/
357 // ---------------------------------------------------------------------
358 /* Right now the system must be consistent before this can be called.
359 It also will not change packages marked for install, it only tries
360 to install packages not marked for install */
361 bool pkgAllUpgrade(pkgDepCache
&Cache
)
363 pkgProblemResolver
Fix(&Cache
);
365 if (Cache
.BrokenCount() != 0)
368 // Upgrade all installed packages
369 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
371 if (Cache
[I
].Install() == true)
374 if (_config
->FindB("APT::Ignore-Hold",false) == false)
375 if (I
->SelectedState
== pkgCache::State::Hold
)
378 if (I
->CurrentVer
!= 0 && Cache
[I
].InstallVer
!= 0)
379 Cache
.MarkInstall(I
,false);
382 return Fix
.ResolveByKeep();
385 // MinimizeUpgrade - Minimizes the set of packages to be upgraded /*{{{*/
386 // ---------------------------------------------------------------------
387 /* This simply goes over the entire set of packages and tries to keep
388 each package marked for upgrade. If a conflict is generated then
389 the package is restored. */
390 bool pkgMinimizeUpgrade(pkgDepCache
&Cache
)
392 if (Cache
.BrokenCount() != 0)
395 // We loop for 10 tries to get the minimal set size.
397 unsigned int Count
= 0;
401 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
404 if (Cache
[I
].Upgrade() == false || Cache
[I
].NewInstall() == true)
407 // Keep it and see if that is OK
409 if (Cache
.BrokenCount() != 0)
410 Cache
.MarkInstall(I
,false);
413 // If keep didnt actually do anything then there was no change..
414 if (Cache
[I
].Upgrade() == false)
420 while (Change
== true && Count
< 10);
422 if (Cache
.BrokenCount() != 0)
423 return _error
->Error("Internal Error in pkgMinimizeUpgrade");
429 // ProblemResolver::pkgProblemResolver - Constructor /*{{{*/
430 // ---------------------------------------------------------------------
432 pkgProblemResolver::pkgProblemResolver(pkgDepCache
*pCache
) : Cache(*pCache
)
435 unsigned long Size
= Cache
.Head().PackageCount
;
436 Scores
= new signed short[Size
];
437 Flags
= new unsigned char[Size
];
438 memset(Flags
,0,sizeof(*Flags
)*Size
);
440 // Set debug to true to see its decision logic
441 Debug
= _config
->FindB("Debug::pkgProblemResolver",false);
444 // ProblemResolver::~pkgProblemResolver - Destructor /*{{{*/
445 // ---------------------------------------------------------------------
447 pkgProblemResolver::~pkgProblemResolver()
453 // ProblemResolver::ScoreSort - Sort the list by score /*{{{*/
454 // ---------------------------------------------------------------------
456 int pkgProblemResolver::ScoreSort(const void *a
,const void *b
)
458 Package
const **A
= (Package
const **)a
;
459 Package
const **B
= (Package
const **)b
;
460 if (This
->Scores
[(*A
)->ID
] > This
->Scores
[(*B
)->ID
])
462 if (This
->Scores
[(*A
)->ID
] < This
->Scores
[(*B
)->ID
])
467 // ProblemResolver::MakeScores - Make the score table /*{{{*/
468 // ---------------------------------------------------------------------
470 void pkgProblemResolver::MakeScores()
472 unsigned long Size
= Cache
.Head().PackageCount
;
473 memset(Scores
,0,sizeof(*Scores
)*Size
);
475 // Generate the base scores for a package based on its properties
476 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
478 if (Cache
[I
].InstallVer
== 0)
481 signed short &Score
= Scores
[I
->ID
];
483 /* This is arbitary, it should be high enough to elevate an
484 essantial package above most other packages but low enough
485 to allow an obsolete essential packages to be removed by
486 a conflicts on a powerfull normal package (ie libc6) */
487 if ((I
->Flags
& pkgCache::Flag::Essential
) == pkgCache::Flag::Essential
)
490 // We transform the priority
491 // Important Required Standard Optional Extra
492 signed short PrioMap
[] = {0,3,2,1,-1,-2};
493 if (Cache
[I
].InstVerIter(Cache
)->Priority
<= 5)
494 Score
+= PrioMap
[Cache
[I
].InstVerIter(Cache
)->Priority
];
496 /* This helps to fix oddball problems with conflicting packages
497 on the same level. We enhance the score of installed packages */
498 if (I
->CurrentVer
!= 0)
502 // Now that we have the base scores we go and propogate dependencies
503 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
505 if (Cache
[I
].InstallVer
== 0)
508 for (pkgCache::DepIterator D
= Cache
[I
].InstVerIter(Cache
).DependsList(); D
.end() == false; D
++)
510 if (D
->Type
== pkgCache::Dep::Depends
|| D
->Type
== pkgCache::Dep::PreDepends
)
511 Scores
[D
.TargetPkg()->ID
]++;
515 // Copy the scores to advoid additive looping
516 SPtrArray
<signed short> OldScores
= new signed short[Size
];
517 memcpy(OldScores
,Scores
,sizeof(*Scores
)*Size
);
519 /* Now we cause 1 level of dependency inheritance, that is we add the
520 score of the packages that depend on the target Package. This
521 fortifies high scoring packages */
522 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
524 if (Cache
[I
].InstallVer
== 0)
527 for (pkgCache::DepIterator D
= I
.RevDependsList(); D
.end() == false; D
++)
529 // Only do it for the install version
530 if ((pkgCache::Version
*)D
.ParentVer() != Cache
[D
.ParentPkg()].InstallVer
||
531 (D
->Type
!= pkgCache::Dep::Depends
&& D
->Type
!= pkgCache::Dep::PreDepends
))
534 Scores
[I
->ID
] += abs(OldScores
[D
.ParentPkg()->ID
]);
538 /* Now we propogate along provides. This makes the packages that
539 provide important packages extremely important */
540 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
542 for (pkgCache::PrvIterator P
= I
.ProvidesList(); P
.end() == false; P
++)
544 // Only do it once per package
545 if ((pkgCache::Version
*)P
.OwnerVer() != Cache
[P
.OwnerPkg()].InstallVer
)
547 Scores
[P
.OwnerPkg()->ID
] += abs(Scores
[I
->ID
] - OldScores
[I
->ID
]);
551 /* Protected things are pushed really high up. This number should put them
552 ahead of everything */
553 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
555 if ((Flags
[I
->ID
] & Protected
) != 0)
556 Scores
[I
->ID
] += 10000;
557 if ((I
->Flags
& pkgCache::Flag::Essential
) == pkgCache::Flag::Essential
)
558 Scores
[I
->ID
] += 5000;
562 // ProblemResolver::DoUpgrade - Attempt to upgrade this package /*{{{*/
563 // ---------------------------------------------------------------------
564 /* This goes through and tries to reinstall packages to make this package
566 bool pkgProblemResolver::DoUpgrade(pkgCache::PkgIterator Pkg
)
568 if ((Flags
[Pkg
->ID
] & Upgradable
) == 0 || Cache
[Pkg
].Upgradable() == false)
570 if ((Flags
[Pkg
->ID
] & Protected
) == Protected
)
573 Flags
[Pkg
->ID
] &= ~Upgradable
;
575 bool WasKept
= Cache
[Pkg
].Keep();
576 Cache
.MarkInstall(Pkg
,false);
578 // This must be a virtual package or something like that.
579 if (Cache
[Pkg
].InstVerIter(Cache
).end() == true)
582 // Isolate the problem dependency
584 for (pkgCache::DepIterator D
= Cache
[Pkg
].InstVerIter(Cache
).DependsList(); D
.end() == false;)
586 // Compute a single dependency element (glob or)
587 pkgCache::DepIterator Start
= D
;
588 pkgCache::DepIterator End
= D
;
589 unsigned char State
= 0;
590 for (bool LastOR
= true; D
.end() == false && LastOR
== true;)
593 LastOR
= (D
->CompareOp
& pkgCache::Dep::Or
) == pkgCache::Dep::Or
;
599 // We only worry about critical deps.
600 if (End
.IsCritical() != true)
603 // Iterate over all the members in the or group
607 if ((Cache
[End
] & pkgDepCache::DepGInstall
) == pkgDepCache::DepGInstall
)
610 // Do not change protected packages
611 PkgIterator P
= Start
.SmartTargetPkg();
612 if ((Flags
[P
->ID
] & Protected
) == Protected
)
615 clog
<< " Reinst Failed because of protected " << P
.Name() << endl
;
620 // Upgrade the package if the candidate version will fix the problem.
621 if ((Cache
[Start
] & pkgDepCache::DepCVer
) == pkgDepCache::DepCVer
)
623 if (DoUpgrade(P
) == false)
626 clog
<< " Reinst Failed because of " << P
.Name() << endl
;
637 /* We let the algorithm deal with conflicts on its next iteration,
638 it is much smarter than us */
639 if (Start
->Type
== pkgCache::Dep::Conflicts
||
640 Start
->Type
== pkgCache::Dep::Obsoletes
)
644 clog
<< " Reinst Failed early because of " << Start
.TargetPkg().Name() << endl
;
657 // Undo our operations - it might be smart to undo everything this did..
663 Cache
.MarkDelete(Pkg
);
668 clog
<< " Re-Instated " << Pkg
.Name() << endl
;
672 // ProblemResolver::Resolve - Run the resolution pass /*{{{*/
673 // ---------------------------------------------------------------------
674 /* This routines works by calculating a score for each package. The score
675 is derived by considering the package's priority and all reverse
676 dependents giving an integer that reflects the amount of breakage that
677 adjusting the package will inflict.
679 It goes from highest score to lowest and corrects all of the breaks by
680 keeping or removing the dependant packages. If that fails then it removes
681 the package itself and goes on. The routine should be able to intelligently
682 go from any broken state to a fixed state.
684 The BrokenFix flag enables a mode where the algorithm tries to
685 upgrade packages to advoid problems. */
686 bool pkgProblemResolver::Resolve(bool BrokenFix
)
688 unsigned long Size
= Cache
.Head().PackageCount
;
690 // Record which packages are marked for install
695 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
697 if (Cache
[I
].Install() == true)
698 Flags
[I
->ID
] |= PreInstalled
;
701 if (Cache
[I
].InstBroken() == true && BrokenFix
== true)
703 Cache
.MarkInstall(I
,false);
704 if (Cache
[I
].Install() == true)
708 Flags
[I
->ID
] &= ~PreInstalled
;
710 Flags
[I
->ID
] |= Upgradable
;
713 while (Again
== true);
716 clog
<< "Starting" << endl
;
720 /* We have to order the packages so that the broken fixing pass
721 operates from highest score to lowest. This prevents problems when
722 high score packages cause the removal of lower score packages that
723 would cause the removal of even lower score packages. */
724 SPtrArray
<pkgCache::Package
*> PList
= new pkgCache::Package
*[Size
];
725 pkgCache::Package
**PEnd
= PList
;
726 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
729 qsort(PList
,PEnd
- PList
,sizeof(*PList
),&ScoreSort
);
731 /* for (pkgCache::Package **K = PList; K != PEnd; K++)
732 if (Scores[(*K)->ID] != 0)
734 pkgCache::PkgIterator Pkg(Cache,*K);
735 clog << Scores[(*K)->ID] << ' ' << Pkg.Name() <<
736 ' ' << (pkgCache::Version *)Pkg.CurrentVer() << ' ' <<
737 Cache[Pkg].InstallVer << ' ' << Cache[Pkg].CandidateVer << endl;
741 clog
<< "Starting 2" << endl
;
743 /* Now consider all broken packages. For each broken package we either
744 remove the package or fix it's problem. We do this once, it should
745 not be possible for a loop to form (that is a < b < c and fixing b by
746 changing a breaks c) */
748 for (int Counter
= 0; Counter
!= 10 && Change
== true; Counter
++)
751 for (pkgCache::Package
**K
= PList
; K
!= PEnd
; K
++)
753 pkgCache::PkgIterator
I(Cache
,*K
);
755 /* We attempt to install this and see if any breaks result,
756 this takes care of some strange cases */
757 if (Cache
[I
].CandidateVer
!= Cache
[I
].InstallVer
&&
758 I
->CurrentVer
!= 0 && Cache
[I
].InstallVer
!= 0 &&
759 (Flags
[I
->ID
] & PreInstalled
) != 0 &&
760 (Flags
[I
->ID
] & Protected
) == 0 &&
761 (Flags
[I
->ID
] & ReInstateTried
) == 0)
764 clog
<< " Try to Re-Instate " << I
.Name() << endl
;
765 unsigned long OldBreaks
= Cache
.BrokenCount();
766 pkgCache::Version
*OldVer
= Cache
[I
].InstallVer
;
767 Flags
[I
->ID
] &= ReInstateTried
;
769 Cache
.MarkInstall(I
,false);
770 if (Cache
[I
].InstBroken() == true ||
771 OldBreaks
< Cache
.BrokenCount())
780 clog
<< "Re-Instated " << I
.Name() << " (" << OldBreaks
<< " vs " << Cache
.BrokenCount() << ')' << endl
;
783 if (Cache
[I
].InstallVer
== 0 || Cache
[I
].InstBroken() == false)
787 cout
<< "Investigating " << I
.Name() << endl
;
789 // Isolate the problem dependency
790 PackageKill KillList
[100];
791 PackageKill
*LEnd
= KillList
;
793 pkgCache::DepIterator Start
;
794 pkgCache::DepIterator End
;
795 PackageKill
*OldEnd
= LEnd
;
797 enum {OrRemove
,OrKeep
} OrOp
= OrRemove
;
798 for (pkgCache::DepIterator D
= Cache
[I
].InstVerIter(Cache
).DependsList();
799 D
.end() == false || InOr
== true;)
801 // Compute a single dependency element (glob or)
807 if (OldEnd
== LEnd
&& OrOp
== OrRemove
)
809 if ((Flags
[I
->ID
] & Protected
) != Protected
)
812 clog
<< " Or group remove for " << I
.Name() << endl
;
817 if (OldEnd
== LEnd
&& OrOp
== OrKeep
)
820 clog
<< " Or group keep for " << I
.Name() << endl
;
826 /* We do an extra loop (as above) to finalize the or group
831 if (Start
.end() == true)
834 // We only worry about critical deps.
835 if (End
.IsCritical() != true)
845 if ((Cache
[End
] & pkgDepCache::DepGInstall
) == pkgDepCache::DepGInstall
)
852 clog
<< "Package " << I
.Name() << " has broken dep on " << Start
.TargetPkg().Name() << endl
;
854 /* Look across the version list. If there are no possible
855 targets then we keep the package and bail. This is necessary
856 if a package has a dep on another package that cant be found */
857 SPtrArray
<pkgCache::Version
*> VList
= Start
.AllTargets();
858 if (*VList
== 0 && (Flags
[I
->ID
] & Protected
) != Protected
&&
859 Start
->Type
!= pkgCache::Dep::Conflicts
&&
860 Start
->Type
!= pkgCache::Dep::Obsoletes
&&
861 Cache
[I
].NowBroken() == false)
865 /* No keep choice because the keep being OK could be the
866 result of another element in the OR group! */
876 for (pkgCache::Version
**V
= VList
; *V
!= 0; V
++)
878 pkgCache::VerIterator
Ver(Cache
,*V
);
879 pkgCache::PkgIterator Pkg
= Ver
.ParentPkg();
882 clog
<< " Considering " << Pkg
.Name() << ' ' << (int)Scores
[Pkg
->ID
] <<
883 " as a solution to " << I
.Name() << ' ' << (int)Scores
[I
->ID
] << endl
;
885 /* Try to fix the package under consideration rather than
886 fiddle with the VList package */
887 if (Scores
[I
->ID
] <= Scores
[Pkg
->ID
] ||
888 ((Cache
[Start
] & pkgDepCache::DepNow
) == 0 &&
889 End
->Type
!= pkgCache::Dep::Conflicts
&&
890 End
->Type
!= pkgCache::Dep::Obsoletes
))
892 // Try a little harder to fix protected packages..
893 if ((Flags
[I
->ID
] & Protected
) == Protected
)
895 if (DoUpgrade(Pkg
) == true)
897 if (Scores
[Pkg
->ID
] > Scores
[I
->ID
])
898 Scores
[Pkg
->ID
] = Scores
[I
->ID
];
905 /* See if a keep will do, unless the package is protected,
906 then installing it will be necessary */
907 bool Installed
= Cache
[I
].Install();
909 if (Cache
[I
].InstBroken() == false)
911 // Unwind operation will be keep now
912 if (OrOp
== OrRemove
)
916 if (InOr
== true && Installed
== true)
917 Cache
.MarkInstall(I
,false);
920 clog
<< " Holding Back " << I
.Name() << " rather than change " << Start
.TargetPkg().Name() << endl
;
924 if (BrokenFix
== false || DoUpgrade(I
) == false)
926 // Consider other options
930 clog
<< " Removing " << I
.Name() << " rather than change " << Start
.TargetPkg().Name() << endl
;
934 if (Scores
[Pkg
->ID
] > Scores
[I
->ID
])
935 Scores
[I
->ID
] = Scores
[Pkg
->ID
];
947 /* This is a conflicts, and the version we are looking
948 at is not the currently selected version of the
949 package, which means it is not necessary to
951 if (Cache
[Pkg
].InstallVer
!= Ver
&&
952 (Start
->Type
== pkgCache::Dep::Conflicts
||
953 Start
->Type
== pkgCache::Dep::Obsoletes
))
956 // Skip adding to the kill list if it is protected
957 if ((Flags
[Pkg
->ID
] & Protected
) != 0)
961 clog
<< " Added " << Pkg
.Name() << " to the remove list" << endl
;
967 if (Start
->Type
!= pkgCache::Dep::Conflicts
&&
968 Start
->Type
!= pkgCache::Dep::Obsoletes
)
973 // Hm, nothing can possibly satisify this dep. Nuke it.
975 Start
->Type
!= pkgCache::Dep::Conflicts
&&
976 Start
->Type
!= pkgCache::Dep::Obsoletes
&&
977 (Flags
[I
->ID
] & Protected
) != Protected
)
979 bool Installed
= Cache
[I
].Install();
981 if (Cache
[I
].InstBroken() == false)
983 // Unwind operation will be keep now
984 if (OrOp
== OrRemove
)
988 if (InOr
== true && Installed
== true)
989 Cache
.MarkInstall(I
,false);
992 clog
<< " Holding Back " << I
.Name() << " because I can't find " << Start
.TargetPkg().Name() << endl
;
997 clog
<< " Removing " << I
.Name() << " because I can't find " << Start
.TargetPkg().Name() << endl
;
1014 // Apply the kill list now
1015 if (Cache
[I
].InstallVer
!= 0)
1017 for (PackageKill
*J
= KillList
; J
!= LEnd
; J
++)
1020 if ((Cache
[J
->Dep
] & pkgDepCache::DepGNow
) == 0)
1022 if (J
->Dep
->Type
== pkgCache::Dep::Conflicts
||
1023 J
->Dep
->Type
== pkgCache::Dep::Obsoletes
)
1026 clog
<< " Fixing " << I
.Name() << " via remove of " << J
->Pkg
.Name() << endl
;
1027 Cache
.MarkDelete(J
->Pkg
);
1033 clog
<< " Fixing " << I
.Name() << " via keep of " << J
->Pkg
.Name() << endl
;
1034 Cache
.MarkKeep(J
->Pkg
);
1039 if (Scores
[I
->ID
] > Scores
[J
->Pkg
->ID
])
1040 Scores
[J
->Pkg
->ID
] = Scores
[I
->ID
];
1048 clog
<< "Done" << endl
;
1050 if (Cache
.BrokenCount() != 0)
1052 // See if this is the result of a hold
1053 pkgCache::PkgIterator I
= Cache
.PkgBegin();
1054 for (;I
.end() != true; I
++)
1056 if (Cache
[I
].InstBroken() == false)
1058 if ((Flags
[I
->ID
] & Protected
) != Protected
)
1059 return _error
->Error(_("Error, pkgProblemResolver::Resolve generated breaks, this may be caused by held packages."));
1061 return _error
->Error(_("Unable to correct problems, you have held broken packages."));
1064 // set the auto-flags (mvo: I'm not sure if we _really_ need this, but
1066 pkgCache::PkgIterator I
= Cache
.PkgBegin();
1067 for (;I
.end() != true; I
++) {
1068 if (Cache
[I
].NewInstall() && !(Flags
[I
->ID
] & PreInstalled
)) {
1069 std::cout
<< "Resolve installed new pkg: " << I
.Name() << " (now marking it as auto)" << std::endl
;
1070 Cache
[I
].Flags
|= pkgCache::Flag::Auto
;
1078 // ProblemResolver::ResolveByKeep - Resolve problems using keep /*{{{*/
1079 // ---------------------------------------------------------------------
1080 /* This is the work horse of the soft upgrade routine. It is very gental
1081 in that it does not install or remove any packages. It is assumed that the
1082 system was non-broken previously. */
1083 bool pkgProblemResolver::ResolveByKeep()
1085 unsigned long Size
= Cache
.Head().PackageCount
;
1088 clog
<< "Entering ResolveByKeep" << endl
;
1092 /* We have to order the packages so that the broken fixing pass
1093 operates from highest score to lowest. This prevents problems when
1094 high score packages cause the removal of lower score packages that
1095 would cause the removal of even lower score packages. */
1096 pkgCache::Package
**PList
= new pkgCache::Package
*[Size
];
1097 pkgCache::Package
**PEnd
= PList
;
1098 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
1101 qsort(PList
,PEnd
- PList
,sizeof(*PList
),&ScoreSort
);
1103 // Consider each broken package
1104 pkgCache::Package
**LastStop
= 0;
1105 for (pkgCache::Package
**K
= PList
; K
!= PEnd
; K
++)
1107 pkgCache::PkgIterator
I(Cache
,*K
);
1109 if (Cache
[I
].InstallVer
== 0 || Cache
[I
].InstBroken() == false)
1112 /* Keep the package. If this works then great, otherwise we have
1113 to be significantly more agressive and manipulate its dependencies */
1114 if ((Flags
[I
->ID
] & Protected
) == 0)
1117 clog
<< "Keeping package " << I
.Name() << endl
;
1119 if (Cache
[I
].InstBroken() == false)
1126 // Isolate the problem dependencies
1127 for (pkgCache::DepIterator D
= Cache
[I
].InstVerIter(Cache
).DependsList(); D
.end() == false;)
1131 D
.GlobOr(Start
,End
);
1133 // We only worry about critical deps.
1134 if (End
.IsCritical() != true)
1138 if ((Cache
[End
] & pkgDepCache::DepGInstall
) == pkgDepCache::DepGInstall
)
1141 /* Hm, the group is broken.. I suppose the best thing to do is to
1142 is to try every combination of keep/not-keep for the set, but thats
1143 slow, and this never happens, just be conservative and assume the
1144 list of ors is in preference and keep till it starts to work. */
1148 clog
<< "Package " << I
.Name() << " has broken dep on " << Start
.TargetPkg().Name() << endl
;
1150 // Look at all the possible provides on this package
1151 SPtrArray
<pkgCache::Version
*> VList
= Start
.AllTargets();
1152 for (pkgCache::Version
**V
= VList
; *V
!= 0; V
++)
1154 pkgCache::VerIterator
Ver(Cache
,*V
);
1155 pkgCache::PkgIterator Pkg
= Ver
.ParentPkg();
1157 // It is not keepable
1158 if (Cache
[Pkg
].InstallVer
== 0 ||
1159 Pkg
->CurrentVer
== 0)
1162 if ((Flags
[I
->ID
] & Protected
) == 0)
1165 clog
<< " Keeping Package " << Pkg
.Name() << " due to dep" << endl
;
1166 Cache
.MarkKeep(Pkg
);
1169 if (Cache
[I
].InstBroken() == false)
1173 if (Cache
[I
].InstBroken() == false)
1181 if (Cache
[I
].InstBroken() == false)
1185 if (Cache
[I
].InstBroken() == true)
1190 return _error
->Error("Internal Error, pkgProblemResolver::ResolveByKeep is looping on package %s.",I
.Name());
1198 // ProblemResolver::InstallProtect - Install all protected packages /*{{{*/
1199 // ---------------------------------------------------------------------
1200 /* This is used to make sure protected packages are installed */
1201 void pkgProblemResolver::InstallProtect()
1203 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
1205 if ((Flags
[I
->ID
] & Protected
) == Protected
)
1207 if ((Flags
[I
->ID
] & ToRemove
) == ToRemove
)
1208 Cache
.MarkDelete(I
);
1210 Cache
.MarkInstall(I
,false);
1216 // PrioSortList - Sort a list of versions by priority /*{{{*/
1217 // ---------------------------------------------------------------------
1218 /* This is ment to be used in conjunction with AllTargets to get a list
1219 of versions ordered by preference. */
1220 static pkgCache
*PrioCache
;
1221 static int PrioComp(const void *A
,const void *B
)
1223 pkgCache::VerIterator
L(*PrioCache
,*(pkgCache::Version
**)A
);
1224 pkgCache::VerIterator
R(*PrioCache
,*(pkgCache::Version
**)B
);
1226 if ((L
.ParentPkg()->Flags
& pkgCache::Flag::Essential
) == pkgCache::Flag::Essential
&&
1227 (R
.ParentPkg()->Flags
& pkgCache::Flag::Essential
) != pkgCache::Flag::Essential
)
1229 if ((L
.ParentPkg()->Flags
& pkgCache::Flag::Essential
) != pkgCache::Flag::Essential
&&
1230 (R
.ParentPkg()->Flags
& pkgCache::Flag::Essential
) == pkgCache::Flag::Essential
)
1233 if (L
->Priority
!= R
->Priority
)
1234 return R
->Priority
- L
->Priority
;
1235 return strcmp(L
.ParentPkg().Name(),R
.ParentPkg().Name());
1237 void pkgPrioSortList(pkgCache
&Cache
,pkgCache::Version
**List
)
1239 unsigned long Count
= 0;
1241 for (pkgCache::Version
**I
= List
; *I
!= 0; I
++)
1243 qsort(List
,Count
,sizeof(*List
),PrioComp
);
1248 // pkgMarkPkgUsed - Mark used packages as dirty /*{{{*/
1249 // ---------------------------------------------------------------------
1250 /* Mark all reachable packages as dirty. */
1251 void pkgMarkPkgUsed(pkgDepCache
&Cache
, pkgCache::PkgIterator Pkg
,
1252 pkgCache::State::PkgRemoveState DirtLevel
)
1254 // If it is not installed, and we are in manual mode, ignore it
1255 if ((Pkg
->CurrentVer
== 0 && Cache
[Pkg
].Install() == false || Cache
[Pkg
].Delete() == true) &&
1256 DirtLevel
== pkgCache::State::RemoveManual
)
1258 // fprintf(stdout,"This one is not installed/virtual %s %d %d\n", Pkg.Name(), Pkg->AutomaticRemove, DirtLevel);
1262 // If it is not installed, and it is not virtual, ignore it
1263 if ((Pkg
->CurrentVer
== 0 && Cache
[Pkg
].Install() == false || Cache
[Pkg
].Delete() == true) &&
1264 Pkg
->VersionList
!= 0)
1266 // fprintf(stdout,"This one is not installed %s %d %d\n", Pkg.Name(), Pkg->AutomaticRemove, DirtLevel);
1270 // If it is similar or more dirty than we are ;-), because we've been here already, don't mark it
1271 // This is necessary because virtual packages just relay the current level,
1272 // so it may be possible e.g. that this was already seen with ::RemoveSuggested, but
1273 // we are ::RemoveRequired
1274 if (Cache
[Pkg
].Dirty() >= DirtLevel
)
1276 //fprintf(stdout,"Seen already %s %d %d\n", Pkg.Name(), Pkg->AutomaticRemove, DirtLevel);
1280 // If it is less important than the current DirtLevel, don't mark it
1281 if (Cache
[Pkg
].AutomaticRemove
!= pkgCache::State::RemoveManual
&&
1282 Cache
[Pkg
].AutomaticRemove
> DirtLevel
)
1284 // fprintf(stdout,"We don't need %s %d %d %d\n", Pkg.Name(), Pkg->AutomaticRemove, DirtLevel, Cache[Pkg].Dirty());
1289 Cache
.SetDirty(Pkg
, DirtLevel
);
1291 //fprintf(stdout,"We keep %s %d %d\n", Pkg.Name(), Pkg->AutomaticRemove, DirtLevel);
1293 // We are a virtual package
1294 if (Pkg
->VersionList
== 0)
1296 // fprintf(stdout,"We are virtual %s %d %d\n", Pkg.Name(), Pkg->AutomaticRemove, DirtLevel);
1297 for (pkgCache::PrvIterator Prv
= Pkg
.ProvidesList(); ! Prv
.end(); ++Prv
)
1298 pkgMarkPkgUsed (Cache
, Prv
.OwnerPkg(), DirtLevel
);
1302 // Depending on the type of dependency, follow it
1303 for (pkgCache::DepIterator D
= Cache
[Pkg
].InstVerIter(Cache
).DependsList(); ! D
.end(); ++D
)
1305 // fprintf(stdout,"We depend on %s %s\n", D.TargetPkg().Name(), D.DepType());
1309 case pkgCache::Dep::Depends
:
1310 case pkgCache::Dep::PreDepends
:
1311 pkgMarkPkgUsed (Cache
, D
.TargetPkg(), pkgCache::State::RemoveRequired
);
1313 case pkgCache::Dep::Recommends
:
1314 pkgMarkPkgUsed (Cache
, D
.TargetPkg(), pkgCache::State::RemoveRecommended
);
1316 case pkgCache::Dep::Suggests
:
1317 pkgMarkPkgUsed (Cache
, D
.TargetPkg(), pkgCache::State::RemoveSuggested
);
1319 case pkgCache::Dep::Conflicts
:
1320 case pkgCache::Dep::Replaces
:
1321 case pkgCache::Dep::Obsoletes
:
1322 // We don't handle these here
1326 // fprintf(stdout,"We keep %s %d %d <END>\n", Pkg.Name(), Pkg->AutomaticRemove, DirtLevel);
1330 bool pkgMarkUsed(pkgDepCache
&Cache
)
1333 for (pkgCache::PkgIterator Pkg
= Cache
.PkgBegin(); ! Pkg
.end(); ++Pkg
)
1334 if(!Cache
[Pkg
].Dirty() && Cache
[Pkg
].AutomaticRemove
> 0)
1335 std::cout
<< "has auto-remove information: " << Pkg
.Name()
1336 << " " << (int)Cache
[Pkg
].AutomaticRemove
1339 // init with defaults
1340 for (pkgCache::PkgIterator Pkg
= Cache
.PkgBegin(); ! Pkg
.end(); ++Pkg
)
1341 Cache
.SetDirty(Pkg
, pkgCache::State::RemoveUnknown
);
1343 // go recursive over the cache
1344 for (pkgCache::PkgIterator Pkg
= Cache
.PkgBegin(); ! Pkg
.end(); ++Pkg
)
1345 pkgMarkPkgUsed (Cache
, Pkg
, pkgCache::State::RemoveManual
);