]>
git.saurik.com Git - apt.git/blob - apt-pkg/algorithms.cc
8e55649cac11278bea3d2ee78d7438def806fb33
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/pkgsystem.h>
24 #include <apt-pkg/version.h>
25 #include <apt-pkg/sptr.h>
29 #include <sys/types.h>
35 pkgProblemResolver
*pkgProblemResolver::This
= 0;
37 // Simulate::Simulate - Constructor /*{{{*/
38 // ---------------------------------------------------------------------
39 /* The legacy translations here of input Pkg iterators is obsolete,
40 this is not necessary since the pkgCaches are fully shared now. */
41 pkgSimulate::pkgSimulate(pkgDepCache
*Cache
) : pkgPackageManager(Cache
),
43 Sim(&Cache
->GetCache(),&iPolicy
)
46 Flags
= new unsigned char[Cache
->Head().PackageCount
];
47 memset(Flags
,0,sizeof(*Flags
)*Cache
->Head().PackageCount
);
49 // Fake a filename so as not to activate the media swapping
50 string Jnk
= "SIMULATE";
51 for (unsigned int I
= 0; I
!= Cache
->Head().PackageCount
; I
++)
55 // Simulate::Describe - Describe a package /*{{{*/
56 // ---------------------------------------------------------------------
57 /* Parameter Current == true displays the current package version,
58 Parameter Candidate == true displays the candidate package version */
59 void pkgSimulate::Describe(PkgIterator Pkg
,ostream
&out
,bool Current
,bool Candidate
)
67 Ver
= Pkg
.CurrentVer();
68 if (Ver
.end() == false)
69 out
<< " [" << Ver
.VerStr() << ']';
72 if (Candidate
== true)
74 Ver
= Sim
[Pkg
].CandidateVerIter(Sim
);
75 if (Ver
.end() == true)
78 out
<< " (" << Ver
.VerStr() << ' ' << Ver
.RelStr() << ')';
82 // Simulate::Install - Simulate unpacking of a package /*{{{*/
83 // ---------------------------------------------------------------------
85 bool pkgSimulate::Install(PkgIterator iPkg
,string
/*File*/)
88 PkgIterator Pkg
= Sim
.FindPkg(iPkg
.Name());
92 Describe(Pkg
,cout
,true,true);
93 Sim
.MarkInstall(Pkg
,false);
95 // Look for broken conflicts+predepends.
96 for (PkgIterator I
= Sim
.PkgBegin(); I
.end() == false; I
++)
98 if (Sim
[I
].InstallVer
== 0)
101 for (DepIterator D
= Sim
[I
].InstVerIter(Sim
).DependsList(); D
.end() == false;)
106 if (Start
->Type
== pkgCache::Dep::Conflicts
||
107 Start
->Type
== pkgCache::Dep::Obsoletes
||
108 End
->Type
== pkgCache::Dep::PreDepends
)
110 if ((Sim
[End
] & pkgDepCache::DepGInstall
) == 0)
112 cout
<< " [" << I
.Name() << " on " << Start
.TargetPkg().Name() << ']';
113 if (Start
->Type
== pkgCache::Dep::Conflicts
)
114 _error
->Error("Fatal, conflicts violated %s",I
.Name());
120 if (Sim
.BrokenCount() != 0)
127 // Simulate::Configure - Simulate configuration of a Package /*{{{*/
128 // ---------------------------------------------------------------------
129 /* This is not an acurate simulation of relatity, we should really not
130 install the package.. For some investigations it may be necessary
132 bool pkgSimulate::Configure(PkgIterator iPkg
)
134 // Adapt the iterator
135 PkgIterator Pkg
= Sim
.FindPkg(iPkg
.Name());
138 // Sim.MarkInstall(Pkg,false);
139 if (Sim
[Pkg
].InstBroken() == true)
141 cout
<< "Conf " << Pkg
.Name() << " broken" << endl
;
145 // Print out each package and the failed dependencies
146 for (pkgCache::DepIterator D
= Sim
[Pkg
].InstVerIter(Sim
).DependsList(); D
.end() == false; D
++)
148 if (Sim
.IsImportantDep(D
) == false ||
149 (Sim
[D
] & pkgDepCache::DepInstall
) != 0)
152 if (D
->Type
== pkgCache::Dep::Obsoletes
)
153 cout
<< " Obsoletes:" << D
.TargetPkg().Name();
154 else if (D
->Type
== pkgCache::Dep::Conflicts
)
155 cout
<< " Conflicts:" << D
.TargetPkg().Name();
157 cout
<< " Depends:" << D
.TargetPkg().Name();
161 _error
->Error("Conf Broken %s",Pkg
.Name());
166 Describe(Pkg
,cout
,false,true);
169 if (Sim
.BrokenCount() != 0)
177 // Simulate::Remove - Simulate the removal of a package /*{{{*/
178 // ---------------------------------------------------------------------
180 bool pkgSimulate::Remove(PkgIterator iPkg
,bool Purge
)
182 // Adapt the iterator
183 PkgIterator Pkg
= Sim
.FindPkg(iPkg
.Name());
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
.Name() << ' ';
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 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
229 if (I
->VersionList
== 0)
232 // Only choice for a ReInstReq package is to reinstall
233 if (I
->InstState
== pkgCache::State::ReInstReq
||
234 I
->InstState
== pkgCache::State::HoldReInstReq
)
236 if (I
->CurrentVer
!= 0 && I
.CurrentVer().Downloadable() == true)
240 // Is this right? Will dpkg choke on an upgrade?
241 if (Cache
[I
].CandidateVer
!= 0 &&
242 Cache
[I
].CandidateVerIter(Cache
).Downloadable() == true)
243 Cache
.MarkInstall(I
);
245 return _error
->Error(_("The package %s needs to be reinstalled, "
246 "but I can't find an archive for it."),I
.Name());
252 switch (I
->CurrentState
)
254 /* This means installation failed somehow - it does not need to be
255 re-unpacked (probably) */
256 case pkgCache::State::UnPacked
:
257 case pkgCache::State::HalfConfigured
:
258 if ((I
->CurrentVer
!= 0 && I
.CurrentVer().Downloadable() == true) ||
259 I
.State() != pkgCache::PkgIterator::NeedsUnpack
)
263 if (Cache
[I
].CandidateVer
!= 0 &&
264 Cache
[I
].CandidateVerIter(Cache
).Downloadable() == true)
265 Cache
.MarkInstall(I
);
271 // This means removal failed
272 case pkgCache::State::HalfInstalled
:
277 if (I
->InstState
!= pkgCache::State::Ok
)
278 return _error
->Error("The package %s is not ok and I "
279 "don't know how to fix it!",I
.Name());
285 // FixBroken - Fix broken packages /*{{{*/
286 // ---------------------------------------------------------------------
287 /* This autoinstalls every broken package and then runs the problem resolver
289 bool pkgFixBroken(pkgDepCache
&Cache
)
291 // Auto upgrade all broken packages
292 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
293 if (Cache
[I
].NowBroken() == true)
294 Cache
.MarkInstall(I
,true);
296 /* Fix packages that are in a NeedArchive state but don't have a
297 downloadable install version */
298 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
300 if (I
.State() != pkgCache::PkgIterator::NeedsUnpack
||
301 Cache
[I
].Delete() == true)
304 if (Cache
[I
].InstVerIter(Cache
).Downloadable() == false)
307 Cache
.MarkInstall(I
,true);
310 pkgProblemResolver
Fix(&Cache
);
311 return Fix
.Resolve(true);
314 // DistUpgrade - Distribution upgrade /*{{{*/
315 // ---------------------------------------------------------------------
316 /* This autoinstalls every package and then force installs every
317 pre-existing package. This creates the initial set of conditions which
318 most likely contain problems because too many things were installed.
320 The problem resolver is used to resolve the problems.
322 bool pkgDistUpgrade(pkgDepCache
&Cache
)
324 /* Auto upgrade all installed packages, this provides the basis
325 for the installation */
326 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
327 if (I
->CurrentVer
!= 0)
328 Cache
.MarkInstall(I
,true);
330 /* Now, auto upgrade all essential packages - this ensures that
331 the essential packages are present and working */
332 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
333 if ((I
->Flags
& pkgCache::Flag::Essential
) == pkgCache::Flag::Essential
)
334 Cache
.MarkInstall(I
,true);
336 /* We do it again over all previously installed packages to force
337 conflict resolution on them all. */
338 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
339 if (I
->CurrentVer
!= 0)
340 Cache
.MarkInstall(I
,false);
342 pkgProblemResolver
Fix(&Cache
);
344 // Hold back held packages.
345 if (_config
->FindB("APT::Ignore-Hold",false) == false)
347 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
349 if (I
->SelectedState
== pkgCache::State::Hold
)
357 return Fix
.Resolve();
360 // AllUpgrade - Upgrade as many packages as possible /*{{{*/
361 // ---------------------------------------------------------------------
362 /* Right now the system must be consistent before this can be called.
363 It also will not change packages marked for install, it only tries
364 to install packages not marked for install */
365 bool pkgAllUpgrade(pkgDepCache
&Cache
)
367 pkgProblemResolver
Fix(&Cache
);
369 if (Cache
.BrokenCount() != 0)
372 // Upgrade all installed packages
373 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
375 if (Cache
[I
].Install() == true)
378 if (_config
->FindB("APT::Ignore-Hold",false) == false)
379 if (I
->SelectedState
== pkgCache::State::Hold
)
382 if (I
->CurrentVer
!= 0 && Cache
[I
].InstallVer
!= 0)
383 Cache
.MarkInstall(I
,false);
386 return Fix
.ResolveByKeep();
389 // MinimizeUpgrade - Minimizes the set of packages to be upgraded /*{{{*/
390 // ---------------------------------------------------------------------
391 /* This simply goes over the entire set of packages and tries to keep
392 each package marked for upgrade. If a conflict is generated then
393 the package is restored. */
394 bool pkgMinimizeUpgrade(pkgDepCache
&Cache
)
396 if (Cache
.BrokenCount() != 0)
399 // We loop for 10 tries to get the minimal set size.
401 unsigned int Count
= 0;
405 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
408 if (Cache
[I
].Upgrade() == false || Cache
[I
].NewInstall() == true)
411 // Keep it and see if that is OK
413 if (Cache
.BrokenCount() != 0)
414 Cache
.MarkInstall(I
,false);
417 // If keep didnt actually do anything then there was no change..
418 if (Cache
[I
].Upgrade() == false)
424 while (Change
== true && Count
< 10);
426 if (Cache
.BrokenCount() != 0)
427 return _error
->Error("Internal Error in pkgMinimizeUpgrade");
433 // ProblemResolver::pkgProblemResolver - Constructor /*{{{*/
434 // ---------------------------------------------------------------------
436 pkgProblemResolver::pkgProblemResolver(pkgDepCache
*pCache
) : Cache(*pCache
)
439 unsigned long Size
= Cache
.Head().PackageCount
;
440 Scores
= new signed short[Size
];
441 Flags
= new unsigned char[Size
];
442 memset(Flags
,0,sizeof(*Flags
)*Size
);
444 // Set debug to true to see its decision logic
445 Debug
= _config
->FindB("Debug::pkgProblemResolver",false);
448 // ProblemResolver::~pkgProblemResolver - Destructor /*{{{*/
449 // ---------------------------------------------------------------------
451 pkgProblemResolver::~pkgProblemResolver()
457 // ProblemResolver::ScoreSort - Sort the list by score /*{{{*/
458 // ---------------------------------------------------------------------
460 int pkgProblemResolver::ScoreSort(const void *a
,const void *b
)
462 Package
const **A
= (Package
const **)a
;
463 Package
const **B
= (Package
const **)b
;
464 if (This
->Scores
[(*A
)->ID
] > This
->Scores
[(*B
)->ID
])
466 if (This
->Scores
[(*A
)->ID
] < This
->Scores
[(*B
)->ID
])
471 // ProblemResolver::MakeScores - Make the score table /*{{{*/
472 // ---------------------------------------------------------------------
474 void pkgProblemResolver::MakeScores()
476 unsigned long Size
= Cache
.Head().PackageCount
;
477 memset(Scores
,0,sizeof(*Scores
)*Size
);
479 // Generate the base scores for a package based on its properties
480 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
482 if (Cache
[I
].InstallVer
== 0)
485 signed short &Score
= Scores
[I
->ID
];
487 /* This is arbitary, it should be high enough to elevate an
488 essantial package above most other packages but low enough
489 to allow an obsolete essential packages to be removed by
490 a conflicts on a powerfull normal package (ie libc6) */
491 if ((I
->Flags
& pkgCache::Flag::Essential
) == pkgCache::Flag::Essential
)
494 // We transform the priority
495 // Important Required Standard Optional Extra
496 signed short PrioMap
[] = {0,3,2,1,-1,-2};
497 if (Cache
[I
].InstVerIter(Cache
)->Priority
<= 5)
498 Score
+= PrioMap
[Cache
[I
].InstVerIter(Cache
)->Priority
];
500 /* This helps to fix oddball problems with conflicting packages
501 on the same level. We enhance the score of installed packages */
502 if (I
->CurrentVer
!= 0)
506 // Now that we have the base scores we go and propogate dependencies
507 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
509 if (Cache
[I
].InstallVer
== 0)
512 for (pkgCache::DepIterator D
= Cache
[I
].InstVerIter(Cache
).DependsList(); D
.end() == false; D
++)
514 if (D
->Type
== pkgCache::Dep::Depends
|| D
->Type
== pkgCache::Dep::PreDepends
)
515 Scores
[D
.TargetPkg()->ID
]++;
519 // Copy the scores to advoid additive looping
520 SPtrArray
<signed short> OldScores
= new signed short[Size
];
521 memcpy(OldScores
,Scores
,sizeof(*Scores
)*Size
);
523 /* Now we cause 1 level of dependency inheritance, that is we add the
524 score of the packages that depend on the target Package. This
525 fortifies high scoring packages */
526 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
528 if (Cache
[I
].InstallVer
== 0)
531 for (pkgCache::DepIterator D
= I
.RevDependsList(); D
.end() == false; D
++)
533 // Only do it for the install version
534 if ((pkgCache::Version
*)D
.ParentVer() != Cache
[D
.ParentPkg()].InstallVer
||
535 (D
->Type
!= pkgCache::Dep::Depends
&& D
->Type
!= pkgCache::Dep::PreDepends
))
538 Scores
[I
->ID
] += abs(OldScores
[D
.ParentPkg()->ID
]);
542 /* Now we propogate along provides. This makes the packages that
543 provide important packages extremely important */
544 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
546 for (pkgCache::PrvIterator P
= I
.ProvidesList(); P
.end() == false; P
++)
548 // Only do it once per package
549 if ((pkgCache::Version
*)P
.OwnerVer() != Cache
[P
.OwnerPkg()].InstallVer
)
551 Scores
[P
.OwnerPkg()->ID
] += abs(Scores
[I
->ID
] - OldScores
[I
->ID
]);
555 /* Protected things are pushed really high up. This number should put them
556 ahead of everything */
557 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
559 if ((Flags
[I
->ID
] & Protected
) != 0)
560 Scores
[I
->ID
] += 10000;
561 if ((I
->Flags
& pkgCache::Flag::Essential
) == pkgCache::Flag::Essential
)
562 Scores
[I
->ID
] += 5000;
566 // ProblemResolver::DoUpgrade - Attempt to upgrade this package /*{{{*/
567 // ---------------------------------------------------------------------
568 /* This goes through and tries to reinstall packages to make this package
570 bool pkgProblemResolver::DoUpgrade(pkgCache::PkgIterator Pkg
)
572 if ((Flags
[Pkg
->ID
] & Upgradable
) == 0 || Cache
[Pkg
].Upgradable() == false)
574 if ((Flags
[Pkg
->ID
] & Protected
) == Protected
)
577 Flags
[Pkg
->ID
] &= ~Upgradable
;
579 bool WasKept
= Cache
[Pkg
].Keep();
580 Cache
.MarkInstall(Pkg
,false);
582 // This must be a virtual package or something like that.
583 if (Cache
[Pkg
].InstVerIter(Cache
).end() == true)
586 // Isolate the problem dependency
588 for (pkgCache::DepIterator D
= Cache
[Pkg
].InstVerIter(Cache
).DependsList(); D
.end() == false;)
590 // Compute a single dependency element (glob or)
591 pkgCache::DepIterator Start
= D
;
592 pkgCache::DepIterator End
= D
;
593 unsigned char State
= 0;
594 for (bool LastOR
= true; D
.end() == false && LastOR
== true;)
597 LastOR
= (D
->CompareOp
& pkgCache::Dep::Or
) == pkgCache::Dep::Or
;
603 // We only worry about critical deps.
604 if (End
.IsCritical() != true)
607 // Iterate over all the members in the or group
611 if ((Cache
[End
] & pkgDepCache::DepGInstall
) == pkgDepCache::DepGInstall
)
614 // Do not change protected packages
615 PkgIterator P
= Start
.SmartTargetPkg();
616 if ((Flags
[P
->ID
] & Protected
) == Protected
)
619 clog
<< " Reinst Failed because of protected " << P
.Name() << endl
;
624 // Upgrade the package if the candidate version will fix the problem.
625 if ((Cache
[Start
] & pkgDepCache::DepCVer
) == pkgDepCache::DepCVer
)
627 if (DoUpgrade(P
) == false)
630 clog
<< " Reinst Failed because of " << P
.Name() << endl
;
641 /* We let the algorithm deal with conflicts on its next iteration,
642 it is much smarter than us */
643 if (Start
->Type
== pkgCache::Dep::Conflicts
||
644 Start
->Type
== pkgCache::Dep::Obsoletes
)
648 clog
<< " Reinst Failed early because of " << Start
.TargetPkg().Name() << endl
;
661 // Undo our operations - it might be smart to undo everything this did..
667 Cache
.MarkDelete(Pkg
);
672 clog
<< " Re-Instated " << Pkg
.Name() << endl
;
676 // ProblemResolver::Resolve - Run the resolution pass /*{{{*/
677 // ---------------------------------------------------------------------
678 /* This routines works by calculating a score for each package. The score
679 is derived by considering the package's priority and all reverse
680 dependents giving an integer that reflects the amount of breakage that
681 adjusting the package will inflict.
683 It goes from highest score to lowest and corrects all of the breaks by
684 keeping or removing the dependant packages. If that fails then it removes
685 the package itself and goes on. The routine should be able to intelligently
686 go from any broken state to a fixed state.
688 The BrokenFix flag enables a mode where the algorithm tries to
689 upgrade packages to advoid problems. */
690 bool pkgProblemResolver::Resolve(bool BrokenFix
)
692 unsigned long Size
= Cache
.Head().PackageCount
;
694 // Record which packages are marked for install
699 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
701 if (Cache
[I
].Install() == true)
702 Flags
[I
->ID
] |= PreInstalled
;
705 if (Cache
[I
].InstBroken() == true && BrokenFix
== true)
707 Cache
.MarkInstall(I
,false);
708 if (Cache
[I
].Install() == true)
712 Flags
[I
->ID
] &= ~PreInstalled
;
714 Flags
[I
->ID
] |= Upgradable
;
717 while (Again
== true);
720 clog
<< "Starting" << endl
;
724 /* We have to order the packages so that the broken fixing pass
725 operates from highest score to lowest. This prevents problems when
726 high score packages cause the removal of lower score packages that
727 would cause the removal of even lower score packages. */
728 SPtrArray
<pkgCache::Package
*> PList
= new pkgCache::Package
*[Size
];
729 pkgCache::Package
**PEnd
= PList
;
730 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
733 qsort(PList
,PEnd
- PList
,sizeof(*PList
),&ScoreSort
);
735 /* for (pkgCache::Package **K = PList; K != PEnd; K++)
736 if (Scores[(*K)->ID] != 0)
738 pkgCache::PkgIterator Pkg(Cache,*K);
739 clog << Scores[(*K)->ID] << ' ' << Pkg.Name() <<
740 ' ' << (pkgCache::Version *)Pkg.CurrentVer() << ' ' <<
741 Cache[Pkg].InstallVer << ' ' << Cache[Pkg].CandidateVer << endl;
745 clog
<< "Starting 2" << endl
;
747 /* Now consider all broken packages. For each broken package we either
748 remove the package or fix it's problem. We do this once, it should
749 not be possible for a loop to form (that is a < b < c and fixing b by
750 changing a breaks c) */
752 for (int Counter
= 0; Counter
!= 10 && Change
== true; Counter
++)
755 for (pkgCache::Package
**K
= PList
; K
!= PEnd
; K
++)
757 pkgCache::PkgIterator
I(Cache
,*K
);
759 /* We attempt to install this and see if any breaks result,
760 this takes care of some strange cases */
761 if (Cache
[I
].CandidateVer
!= Cache
[I
].InstallVer
&&
762 I
->CurrentVer
!= 0 && Cache
[I
].InstallVer
!= 0 &&
763 (Flags
[I
->ID
] & PreInstalled
) != 0 &&
764 (Flags
[I
->ID
] & Protected
) == 0 &&
765 (Flags
[I
->ID
] & ReInstateTried
) == 0)
768 clog
<< " Try to Re-Instate " << I
.Name() << endl
;
769 unsigned long OldBreaks
= Cache
.BrokenCount();
770 pkgCache::Version
*OldVer
= Cache
[I
].InstallVer
;
771 Flags
[I
->ID
] &= ReInstateTried
;
773 Cache
.MarkInstall(I
,false);
774 if (Cache
[I
].InstBroken() == true ||
775 OldBreaks
< Cache
.BrokenCount())
784 clog
<< "Re-Instated " << I
.Name() << " (" << OldBreaks
<< " vs " << Cache
.BrokenCount() << ')' << endl
;
787 if (Cache
[I
].InstallVer
== 0 || Cache
[I
].InstBroken() == false)
791 cout
<< "Investigating " << I
.Name() << endl
;
793 // Isolate the problem dependency
794 PackageKill KillList
[100];
795 PackageKill
*LEnd
= KillList
;
797 pkgCache::DepIterator Start
;
798 pkgCache::DepIterator End
;
799 PackageKill
*OldEnd
= LEnd
;
801 enum {OrRemove
,OrKeep
} OrOp
= OrRemove
;
802 for (pkgCache::DepIterator D
= Cache
[I
].InstVerIter(Cache
).DependsList();
803 D
.end() == false || InOr
== true;)
805 // Compute a single dependency element (glob or)
811 if (OldEnd
== LEnd
&& OrOp
== OrRemove
)
813 if ((Flags
[I
->ID
] & Protected
) != Protected
)
816 clog
<< " Or group remove for " << I
.Name() << endl
;
821 if (OldEnd
== LEnd
&& OrOp
== OrKeep
)
824 clog
<< " Or group keep for " << I
.Name() << endl
;
830 /* We do an extra loop (as above) to finalize the or group
835 if (Start
.end() == true)
838 // We only worry about critical deps.
839 if (End
.IsCritical() != true)
849 if ((Cache
[End
] & pkgDepCache::DepGInstall
) == pkgDepCache::DepGInstall
)
856 clog
<< "Package " << I
.Name() << " has broken dep on " << Start
.TargetPkg().Name() << endl
;
858 /* Look across the version list. If there are no possible
859 targets then we keep the package and bail. This is necessary
860 if a package has a dep on another package that cant be found */
861 SPtrArray
<pkgCache::Version
*> VList
= Start
.AllTargets();
862 if (*VList
== 0 && (Flags
[I
->ID
] & Protected
) != Protected
&&
863 Start
->Type
!= pkgCache::Dep::Conflicts
&&
864 Start
->Type
!= pkgCache::Dep::Obsoletes
&&
865 Cache
[I
].NowBroken() == false)
869 /* No keep choice because the keep being OK could be the
870 result of another element in the OR group! */
880 for (pkgCache::Version
**V
= VList
; *V
!= 0; V
++)
882 pkgCache::VerIterator
Ver(Cache
,*V
);
883 pkgCache::PkgIterator Pkg
= Ver
.ParentPkg();
886 clog
<< " Considering " << Pkg
.Name() << ' ' << (int)Scores
[Pkg
->ID
] <<
887 " as a solution to " << I
.Name() << ' ' << (int)Scores
[I
->ID
] << endl
;
889 /* Try to fix the package under consideration rather than
890 fiddle with the VList package */
891 if (Scores
[I
->ID
] <= Scores
[Pkg
->ID
] ||
892 ((Cache
[Start
] & pkgDepCache::DepNow
) == 0 &&
893 End
->Type
!= pkgCache::Dep::Conflicts
&&
894 End
->Type
!= pkgCache::Dep::Obsoletes
))
896 // Try a little harder to fix protected packages..
897 if ((Flags
[I
->ID
] & Protected
) == Protected
)
899 if (DoUpgrade(Pkg
) == true)
901 if (Scores
[Pkg
->ID
] > Scores
[I
->ID
])
902 Scores
[Pkg
->ID
] = Scores
[I
->ID
];
909 /* See if a keep will do, unless the package is protected,
910 then installing it will be necessary */
911 bool Installed
= Cache
[I
].Install();
913 if (Cache
[I
].InstBroken() == false)
915 // Unwind operation will be keep now
916 if (OrOp
== OrRemove
)
920 if (InOr
== true && Installed
== true)
921 Cache
.MarkInstall(I
,false);
924 clog
<< " Holding Back " << I
.Name() << " rather than change " << Start
.TargetPkg().Name() << endl
;
928 if (BrokenFix
== false || DoUpgrade(I
) == false)
930 // Consider other options
934 clog
<< " Removing " << I
.Name() << " rather than change " << Start
.TargetPkg().Name() << endl
;
938 if (Scores
[Pkg
->ID
] > Scores
[I
->ID
])
939 Scores
[I
->ID
] = Scores
[Pkg
->ID
];
951 /* This is a conflicts, and the version we are looking
952 at is not the currently selected version of the
953 package, which means it is not necessary to
955 if (Cache
[Pkg
].InstallVer
!= Ver
&&
956 (Start
->Type
== pkgCache::Dep::Conflicts
||
957 Start
->Type
== pkgCache::Dep::Obsoletes
))
960 // Skip adding to the kill list if it is protected
961 if ((Flags
[Pkg
->ID
] & Protected
) != 0)
965 clog
<< " Added " << Pkg
.Name() << " to the remove list" << endl
;
971 if (Start
->Type
!= pkgCache::Dep::Conflicts
&&
972 Start
->Type
!= pkgCache::Dep::Obsoletes
)
977 // Hm, nothing can possibly satisify this dep. Nuke it.
979 Start
->Type
!= pkgCache::Dep::Conflicts
&&
980 Start
->Type
!= pkgCache::Dep::Obsoletes
&&
981 (Flags
[I
->ID
] & Protected
) != Protected
)
983 bool Installed
= Cache
[I
].Install();
985 if (Cache
[I
].InstBroken() == false)
987 // Unwind operation will be keep now
988 if (OrOp
== OrRemove
)
992 if (InOr
== true && Installed
== true)
993 Cache
.MarkInstall(I
,false);
996 clog
<< " Holding Back " << I
.Name() << " because I can't find " << Start
.TargetPkg().Name() << endl
;
1001 clog
<< " Removing " << I
.Name() << " because I can't find " << Start
.TargetPkg().Name() << endl
;
1003 Cache
.MarkDelete(I
);
1018 // Apply the kill list now
1019 if (Cache
[I
].InstallVer
!= 0)
1021 for (PackageKill
*J
= KillList
; J
!= LEnd
; J
++)
1024 if ((Cache
[J
->Dep
] & pkgDepCache::DepGNow
) == 0)
1026 if (J
->Dep
->Type
== pkgCache::Dep::Conflicts
||
1027 J
->Dep
->Type
== pkgCache::Dep::Obsoletes
)
1030 clog
<< " Fixing " << I
.Name() << " via remove of " << J
->Pkg
.Name() << endl
;
1031 Cache
.MarkDelete(J
->Pkg
);
1037 clog
<< " Fixing " << I
.Name() << " via keep of " << J
->Pkg
.Name() << endl
;
1038 Cache
.MarkKeep(J
->Pkg
);
1043 if (Scores
[I
->ID
] > Scores
[J
->Pkg
->ID
])
1044 Scores
[J
->Pkg
->ID
] = Scores
[I
->ID
];
1052 clog
<< "Done" << endl
;
1054 if (Cache
.BrokenCount() != 0)
1056 // See if this is the result of a hold
1057 pkgCache::PkgIterator I
= Cache
.PkgBegin();
1058 for (;I
.end() != true; I
++)
1060 if (Cache
[I
].InstBroken() == false)
1062 if ((Flags
[I
->ID
] & Protected
) != Protected
)
1063 return _error
->Error(_("Error, pkgProblemResolver::Resolve generated breaks, this may be caused by held packages."));
1065 return _error
->Error(_("Unable to correct problems, you have held broken packages."));
1068 // set the auto-flags (mvo: I'm not sure if we _really_ need this, but
1070 pkgCache::PkgIterator I
= Cache
.PkgBegin();
1071 for (;I
.end() != true; I
++) {
1072 if (Cache
[I
].NewInstall() && !(Flags
[I
->ID
] & PreInstalled
)) {
1073 if(_config
->FindI("Debug::pkgAutoRemove",false)) {
1074 std::clog
<< "Resolve installed new pkg: " << I
.Name()
1075 << " (now marking it as auto)" << std::endl
;
1077 Cache
[I
].Flags
|= pkgCache::Flag::Auto
;
1085 // ProblemResolver::ResolveByKeep - Resolve problems using keep /*{{{*/
1086 // ---------------------------------------------------------------------
1087 /* This is the work horse of the soft upgrade routine. It is very gental
1088 in that it does not install or remove any packages. It is assumed that the
1089 system was non-broken previously. */
1090 bool pkgProblemResolver::ResolveByKeep()
1092 unsigned long Size
= Cache
.Head().PackageCount
;
1095 clog
<< "Entering ResolveByKeep" << endl
;
1099 /* We have to order the packages so that the broken fixing pass
1100 operates from highest score to lowest. This prevents problems when
1101 high score packages cause the removal of lower score packages that
1102 would cause the removal of even lower score packages. */
1103 pkgCache::Package
**PList
= new pkgCache::Package
*[Size
];
1104 pkgCache::Package
**PEnd
= PList
;
1105 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
1108 qsort(PList
,PEnd
- PList
,sizeof(*PList
),&ScoreSort
);
1110 // Consider each broken package
1111 pkgCache::Package
**LastStop
= 0;
1112 for (pkgCache::Package
**K
= PList
; K
!= PEnd
; K
++)
1114 pkgCache::PkgIterator
I(Cache
,*K
);
1116 if (Cache
[I
].InstallVer
== 0 || Cache
[I
].InstBroken() == false)
1119 /* Keep the package. If this works then great, otherwise we have
1120 to be significantly more agressive and manipulate its dependencies */
1121 if ((Flags
[I
->ID
] & Protected
) == 0)
1124 clog
<< "Keeping package " << I
.Name() << endl
;
1126 if (Cache
[I
].InstBroken() == false)
1133 // Isolate the problem dependencies
1134 for (pkgCache::DepIterator D
= Cache
[I
].InstVerIter(Cache
).DependsList(); D
.end() == false;)
1138 D
.GlobOr(Start
,End
);
1140 // We only worry about critical deps.
1141 if (End
.IsCritical() != true)
1145 if ((Cache
[End
] & pkgDepCache::DepGInstall
) == pkgDepCache::DepGInstall
)
1148 /* Hm, the group is broken.. I suppose the best thing to do is to
1149 is to try every combination of keep/not-keep for the set, but thats
1150 slow, and this never happens, just be conservative and assume the
1151 list of ors is in preference and keep till it starts to work. */
1155 clog
<< "Package " << I
.Name() << " has broken dep on " << Start
.TargetPkg().Name() << endl
;
1157 // Look at all the possible provides on this package
1158 SPtrArray
<pkgCache::Version
*> VList
= Start
.AllTargets();
1159 for (pkgCache::Version
**V
= VList
; *V
!= 0; V
++)
1161 pkgCache::VerIterator
Ver(Cache
,*V
);
1162 pkgCache::PkgIterator Pkg
= Ver
.ParentPkg();
1164 // It is not keepable
1165 if (Cache
[Pkg
].InstallVer
== 0 ||
1166 Pkg
->CurrentVer
== 0)
1169 if ((Flags
[I
->ID
] & Protected
) == 0)
1172 clog
<< " Keeping Package " << Pkg
.Name() << " due to dep" << endl
;
1173 Cache
.MarkKeep(Pkg
);
1176 if (Cache
[I
].InstBroken() == false)
1180 if (Cache
[I
].InstBroken() == false)
1188 if (Cache
[I
].InstBroken() == false)
1192 if (Cache
[I
].InstBroken() == true)
1197 return _error
->Error("Internal Error, pkgProblemResolver::ResolveByKeep is looping on package %s.",I
.Name());
1205 // ProblemResolver::InstallProtect - Install all protected packages /*{{{*/
1206 // ---------------------------------------------------------------------
1207 /* This is used to make sure protected packages are installed */
1208 void pkgProblemResolver::InstallProtect()
1210 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
1212 if ((Flags
[I
->ID
] & Protected
) == Protected
)
1214 if ((Flags
[I
->ID
] & ToRemove
) == ToRemove
)
1215 Cache
.MarkDelete(I
);
1217 Cache
.MarkInstall(I
,false);
1223 // PrioSortList - Sort a list of versions by priority /*{{{*/
1224 // ---------------------------------------------------------------------
1225 /* This is ment to be used in conjunction with AllTargets to get a list
1226 of versions ordered by preference. */
1227 static pkgCache
*PrioCache
;
1228 static int PrioComp(const void *A
,const void *B
)
1230 pkgCache::VerIterator
L(*PrioCache
,*(pkgCache::Version
**)A
);
1231 pkgCache::VerIterator
R(*PrioCache
,*(pkgCache::Version
**)B
);
1233 if ((L
.ParentPkg()->Flags
& pkgCache::Flag::Essential
) == pkgCache::Flag::Essential
&&
1234 (R
.ParentPkg()->Flags
& pkgCache::Flag::Essential
) != pkgCache::Flag::Essential
)
1236 if ((L
.ParentPkg()->Flags
& pkgCache::Flag::Essential
) != pkgCache::Flag::Essential
&&
1237 (R
.ParentPkg()->Flags
& pkgCache::Flag::Essential
) == pkgCache::Flag::Essential
)
1240 if (L
->Priority
!= R
->Priority
)
1241 return R
->Priority
- L
->Priority
;
1242 return strcmp(L
.ParentPkg().Name(),R
.ParentPkg().Name());
1244 void pkgPrioSortList(pkgCache
&Cache
,pkgCache::Version
**List
)
1246 unsigned long Count
= 0;
1248 for (pkgCache::Version
**I
= List
; *I
!= 0; I
++)
1250 qsort(List
,Count
,sizeof(*List
),PrioComp
);
1255 // mark a single package in Mark-and-Sweep
1256 void pkgMarkPackage(pkgDepCache
&Cache
,
1257 const pkgCache::PkgIterator
&pkg
,
1258 const pkgCache::VerIterator
&ver
,
1259 bool follow_recommends
,
1260 bool follow_suggests
)
1262 pkgDepCache::StateCache
&state
=Cache
[pkg
];
1263 pkgCache::VerIterator candver
=state
.CandidateVerIter(Cache
);
1264 pkgCache::VerIterator instver
=state
.InstVerIter(Cache
);
1267 // If a package was garbage-collected but is now being marked, we
1268 // should re-select it
1269 // For cases when a pkg is set to upgrade and this trigger the
1270 // removal of a no-longer used dependency. if the pkg is set to
1271 // keep again later it will result in broken deps
1272 if(state
.Delete() && state
.RemoveReason
=pkgDepCache::Unused
)
1275 mark_install(pkg
, false, false, NULL
);
1276 else if(ver
==pkg
.CurrentVer())
1279 instver
=state
.InstVerIter(*this);
1283 // Ignore versions other than the InstVer, and ignore packages
1284 // that are already going to be removed or just left uninstalled.
1285 if(!(ver
==instver
&& !instver
.end()))
1288 // if we are marked already we are done
1292 //std::cout << "Setting Marked for: " << pkg.Name() << std::endl;
1297 for(pkgCache::DepIterator d
=ver
.DependsList(); !d
.end(); ++d
)
1299 if(d
->Type
==pkgCache::Dep::Depends
||
1300 d
->Type
==pkgCache::Dep::PreDepends
||
1301 (follow_recommends
&&
1302 d
->Type
==pkgCache::Dep::Recommends
) ||
1304 d
->Type
==pkgCache::Dep::Suggests
))
1306 // Try all versions of this package.
1307 for(pkgCache::VerIterator V
=d
.TargetPkg().VersionList();
1310 if(_system
->VS
->CheckDep(V
.VerStr(),d
->CompareOp
, d
.TargetVer()))
1312 pkgMarkPackage(Cache
, V
.ParentPkg(), V
,
1313 follow_recommends
, follow_suggests
);
1316 // Now try virtual packages
1317 for(pkgCache::PrvIterator prv
=d
.TargetPkg().ProvidesList();
1320 if(_system
->VS
->CheckDep(prv
.ProvideVersion(), d
->CompareOp
,
1323 pkgMarkPackage(Cache
, prv
.OwnerPkg(), prv
.OwnerVer(),
1324 follow_recommends
, follow_suggests
);
1333 // Helper for APT::NeverAutoRemove, always include the packages matching
1334 // this regexp into the root-set
1336 pkgMarkAlwaysInclude(pkgCache::PkgIterator p
, vector
<regex_t
*> alwaysMark
)
1338 for(unsigned int i
=0;i
<alwaysMark
.size();i
++)
1339 if (regexec(alwaysMark
[i
],p
.Name(),0,0,0) == 0)
1345 // the main mark algorithm
1346 bool pkgMarkUsed(pkgDepCache
&Cache
, InRootSetFunc func
)
1348 bool follow_recommends
;
1349 bool follow_suggests
;
1352 for(pkgCache::PkgIterator p
=Cache
.PkgBegin(); !p
.end(); ++p
)
1354 Cache
[p
].Marked
=false;
1355 Cache
[p
].Garbage
=false;
1359 follow_recommends
=_config
->FindB("APT::AutoRemove::RecommendsImportant",false);
1360 follow_suggests
=_config
->FindB("APT::AutoRemove::SuggestsImportant", false);
1363 // init the "NeverAutoRemove" variable
1364 vector
<regex_t
*> neverAutoRemoveRegexp
;
1365 Configuration::Item
const *Opts
;
1366 Opts
= _config
->Tree("APT::NeverAutoRemove");
1367 if (Opts
!= 0 && Opts
->Child
!= 0)
1370 for (; Opts
!= 0; Opts
= Opts
->Next
)
1372 if (Opts
->Value
.empty() == true)
1375 regex_t
*p
= new regex_t
;
1376 if(regcomp(p
,Opts
->Value
.c_str(),
1377 REG_EXTENDED
| REG_ICASE
| REG_NOSUB
) != 0)
1380 for(unsigned int i
=0;i
<neverAutoRemoveRegexp
.size();i
++)
1381 regfree(neverAutoRemoveRegexp
[i
]);
1382 return _error
->Error("Regex compilation error for APT::NeverAutoRemove");
1384 neverAutoRemoveRegexp
.push_back(p
);
1390 for(pkgCache::PkgIterator p
=Cache
.PkgBegin(); !p
.end(); ++p
)
1392 if( (func
!= NULL
? (*func
)(p
) : false) ||
1393 pkgMarkAlwaysInclude(p
, neverAutoRemoveRegexp
) ||
1394 !(Cache
[p
].Flags
& pkgCache::Flag::Auto
) ||
1395 (p
->Flags
& pkgCache::Flag::Essential
))
1398 if(Cache
[p
].Keep() && !p
.CurrentVer().end())
1399 pkgMarkPackage(Cache
, p
, p
.CurrentVer(),
1400 follow_recommends
, follow_suggests
);
1401 else if(Cache
[p
].Install())
1402 pkgMarkPackage(Cache
, p
, Cache
[p
].InstVerIter(Cache
),
1403 follow_recommends
, follow_suggests
);
1409 for(pkgCache::PkgIterator p
=Cache
.PkgBegin(); !p
.end(); ++p
)
1411 pkgDepCache::StateCache
&state
=Cache
[p
];
1415 // mark installed but not yet marked stuff as garbage
1416 if(p
->CurrentVer
!= 0) {
1418 std::cout
<< "Garbage: " << p
.Name() << std::endl
;
1421 #if 0 // mvo: the below bits still needs to be ported
1423 // Be sure not to re-delete already deleted packages.
1424 if(delete_unused
&& (!p
.CurrentVer().end() || state
.Install()) &&
1427 bool do_delete
=true;
1429 // If the package is being upgraded, check if we're
1430 // losing a versioned dep. If the dependency matches
1431 // the previous version and not the new version, keep
1432 // the package back instead of removing it.
1433 if(!p
.CurrentVer().end() && state
.Install())
1435 const char *vs
=p
.CurrentVer().VerStr();
1437 // Check direct revdeps only. THIS ASSUMES NO
1438 // VERSIONED PROVIDES, but Debian probably won't
1439 // have them for ages if ever.
1440 for(pkgCache::DepIterator revdep
=p
.RevDependsList();
1441 !revdep
.end(); ++revdep
)
1443 pkgCache::PkgIterator depender
=revdep
.ParentPkg();
1444 // Find which version of the depending package
1445 // will be installed.
1446 pkgCache::VerIterator instver
=(*this)[depender
].InstVerIter(*this);
1448 // Only pay attention to strong positive
1449 // dependencies whose parents will be installed.
1450 if(revdep
.ParentVer()==instver
&&
1451 (revdep
->Type
==pkgCache::Dep::Depends
||
1452 revdep
->Type
==pkgCache::Dep::PreDepends
||
1453 (revdep
->Type
==pkgCache::Dep::Recommends
&&
1454 follow_recommends
)))
1456 // If the previous version matched, cancel the
1457 // deletion. (note that I assume that the new
1458 // version does NOT match; otherwise it would
1460 if(_system
->VS
->CheckDep(vs
,
1462 revdep
.TargetVer()))
1464 mark_keep(p
, false, false, undo
);
1473 mark_delete(p
, false, true, undo
);
1480 for(unsigned int i
=0;i
<neverAutoRemoveRegexp
.size();i
++)
1481 regfree(neverAutoRemoveRegexp
[i
]);