]> git.saurik.com Git - apt.git/blame - apt-pkg/algorithms.cc
Fixed warning
[apt.git] / apt-pkg / algorithms.cc
CommitLineData
6c139d6e
AL
1// -*- mode: cpp; mode: fold -*-
2// Description /*{{{*/
b2e465d6 3// $Id: algorithms.cc,v 1.32 2001/02/20 07:03:17 jgg Exp $
6c139d6e
AL
4/* ######################################################################
5
6 Algorithms - A set of misc algorithms
7
0a8e3465
AL
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.
13
6c139d6e
AL
14 ##################################################################### */
15 /*}}}*/
16// Include Files /*{{{*/
17#ifdef __GNUG__
094a497d 18#pragma implementation "apt-pkg/algorithms.h"
6c139d6e 19#endif
094a497d
AL
20#include <apt-pkg/algorithms.h>
21#include <apt-pkg/error.h>
0a8e3465 22#include <apt-pkg/configuration.h>
b2e465d6
AL
23#include <apt-pkg/sptr.h>
24
25#include <apti18n.h>
26
6c139d6e
AL
27#include <iostream.h>
28 /*}}}*/
29
30pkgProblemResolver *pkgProblemResolver::This = 0;
31
32// Simulate::Simulate - Constructor /*{{{*/
33// ---------------------------------------------------------------------
b2e465d6
AL
34/* The legacy translations here of input Pkg iterators is obsolete,
35 this is not necessary since the pkgCaches are fully shared now. */
36pkgSimulate::pkgSimulate(pkgDepCache *Cache) : pkgPackageManager(Cache),
37 iPolicy(Cache),
38 Sim(&Cache->GetCache(),&iPolicy)
6c139d6e 39{
b2e465d6
AL
40 Sim.Init(0);
41 Flags = new unsigned char[Cache->Head().PackageCount];
42 memset(Flags,0,sizeof(*Flags)*Cache->Head().PackageCount);
281daf46
AL
43
44 // Fake a filename so as not to activate the media swapping
45 string Jnk = "SIMULATE";
b2e465d6 46 for (unsigned int I = 0; I != Cache->Head().PackageCount; I++)
281daf46 47 FileNames[I] = Jnk;
6c139d6e
AL
48}
49 /*}}}*/
b2e465d6
AL
50// Simulate::Describe - Describe a package /*{{{*/
51// ---------------------------------------------------------------------
52/* */
53void pkgSimulate::Describe(PkgIterator Pkg,ostream &out,bool Now)
54{
55 VerIterator Ver(Sim);
56 if (Now == true)
57 Ver = Pkg.CurrentVer();
58 else
59 Ver = Sim[Pkg].CandidateVerIter(Sim);
60
61 out << Pkg.Name();
62
63 if (Ver.end() == true)
64 return;
65
66 out << " (" << Ver.VerStr() << ' ' << Ver.RelStr() << ')';
67}
68 /*}}}*/
6c139d6e
AL
69// Simulate::Install - Simulate unpacking of a package /*{{{*/
70// ---------------------------------------------------------------------
71/* */
72bool pkgSimulate::Install(PkgIterator iPkg,string /*File*/)
73{
74 // Adapt the iterator
75 PkgIterator Pkg = Sim.FindPkg(iPkg.Name());
76 Flags[Pkg->ID] = 1;
77
b2e465d6
AL
78 cout << "Inst ";
79 Describe(Pkg,cout,false);
6c139d6e
AL
80 Sim.MarkInstall(Pkg,false);
81
82 // Look for broken conflicts+predepends.
83 for (PkgIterator I = Sim.PkgBegin(); I.end() == false; I++)
84 {
85 if (Sim[I].InstallVer == 0)
86 continue;
87
b2e465d6
AL
88 for (DepIterator D = Sim[I].InstVerIter(Sim).DependsList(); D.end() == false;)
89 {
90 DepIterator Start;
91 DepIterator End;
92 D.GlobOr(Start,End);
93 if (Start->Type == pkgCache::Dep::Conflicts ||
94 Start->Type == pkgCache::Dep::Obsoletes ||
95 End->Type == pkgCache::Dep::PreDepends)
6c139d6e 96 {
b2e465d6 97 if ((Sim[End] & pkgDepCache::DepGInstall) == 0)
6c139d6e 98 {
b2e465d6
AL
99 cout << " [" << I.Name() << " on " << Start.TargetPkg().Name() << ']';
100 if (Start->Type == pkgCache::Dep::Conflicts)
6c139d6e
AL
101 _error->Error("Fatal, conflicts violated %s",I.Name());
102 }
b2e465d6
AL
103 }
104 }
6c139d6e
AL
105 }
106
107 if (Sim.BrokenCount() != 0)
108 ShortBreaks();
109 else
04aa15a8 110 cout << endl;
6c139d6e
AL
111 return true;
112}
113 /*}}}*/
114// Simulate::Configure - Simulate configuration of a Package /*{{{*/
115// ---------------------------------------------------------------------
116/* This is not an acurate simulation of relatity, we should really not
117 install the package.. For some investigations it may be necessary
118 however. */
119bool pkgSimulate::Configure(PkgIterator iPkg)
120{
121 // Adapt the iterator
122 PkgIterator Pkg = Sim.FindPkg(iPkg.Name());
123
124 Flags[Pkg->ID] = 2;
125// Sim.MarkInstall(Pkg,false);
126 if (Sim[Pkg].InstBroken() == true)
127 {
04aa15a8 128 cout << "Conf " << Pkg.Name() << " broken" << endl;
6c139d6e
AL
129
130 Sim.Update();
131
132 // Print out each package and the failed dependencies
133 for (pkgCache::DepIterator D = Sim[Pkg].InstVerIter(Sim).DependsList(); D.end() == false; D++)
134 {
135 if (Sim.IsImportantDep(D) == false ||
136 (Sim[D] & pkgDepCache::DepInstall) != 0)
137 continue;
138
b2e465d6
AL
139 if (D->Type == pkgCache::Dep::Obsoletes)
140 cout << " Obsoletes:" << D.TargetPkg().Name();
141 else if (D->Type == pkgCache::Dep::Conflicts)
04aa15a8 142 cout << " Conflicts:" << D.TargetPkg().Name();
6c139d6e 143 else
04aa15a8 144 cout << " Depends:" << D.TargetPkg().Name();
6c139d6e 145 }
04aa15a8 146 cout << endl;
6c139d6e
AL
147
148 _error->Error("Conf Broken %s",Pkg.Name());
149 }
150 else
b2e465d6
AL
151 {
152 cout << "Conf ";
153 Describe(Pkg,cout,false);
154 }
6c139d6e
AL
155
156 if (Sim.BrokenCount() != 0)
157 ShortBreaks();
158 else
04aa15a8 159 cout << endl;
6c139d6e
AL
160
161 return true;
162}
163 /*}}}*/
164// Simulate::Remove - Simulate the removal of a package /*{{{*/
165// ---------------------------------------------------------------------
166/* */
fc4b5c9f 167bool pkgSimulate::Remove(PkgIterator iPkg,bool Purge)
6c139d6e
AL
168{
169 // Adapt the iterator
170 PkgIterator Pkg = Sim.FindPkg(iPkg.Name());
171
172 Flags[Pkg->ID] = 3;
173 Sim.MarkDelete(Pkg);
fc4b5c9f 174 if (Purge == true)
b2e465d6 175 cout << "Purg ";
fc4b5c9f 176 else
b2e465d6
AL
177 cout << "Remv ";
178 Describe(Pkg,cout,false);
6c139d6e
AL
179
180 if (Sim.BrokenCount() != 0)
181 ShortBreaks();
182 else
04aa15a8 183 cout << endl;
6c139d6e
AL
184
185 return true;
186}
187 /*}}}*/
188// Simulate::ShortBreaks - Print out a short line describing all breaks /*{{{*/
189// ---------------------------------------------------------------------
190/* */
191void pkgSimulate::ShortBreaks()
192{
04aa15a8 193 cout << " [";
6c139d6e
AL
194 for (PkgIterator I = Sim.PkgBegin(); I.end() == false; I++)
195 {
196 if (Sim[I].InstBroken() == true)
197 {
198 if (Flags[I->ID] == 0)
04aa15a8 199 cout << I.Name() << ' ';
6c139d6e 200/* else
04aa15a8 201 cout << I.Name() << "! ";*/
6c139d6e
AL
202 }
203 }
04aa15a8 204 cout << ']' << endl;
6c139d6e
AL
205}
206 /*}}}*/
207// ApplyStatus - Adjust for non-ok packages /*{{{*/
208// ---------------------------------------------------------------------
209/* We attempt to change the state of the all packages that have failed
210 installation toward their real state. The ordering code will perform
211 the necessary calculations to deal with the problems. */
212bool pkgApplyStatus(pkgDepCache &Cache)
213{
214 for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
215 {
d38b7b3d
AL
216 // Only choice for a ReInstReq package is to reinstall
217 if (I->InstState == pkgCache::State::ReInstReq ||
218 I->InstState == pkgCache::State::HoldReInstReq)
219 {
813c8eea
AL
220 if (I.CurrentVer().Downloadable() == true)
221 Cache.MarkKeep(I);
222 else
223 {
224 // Is this right? Will dpkg choke on an upgrade?
225 if (Cache[I].CandidateVerIter(Cache).Downloadable() == true)
226 Cache.MarkInstall(I);
227 else
b2e465d6
AL
228 return _error->Error(_("The package %s needs to be reinstalled, "
229 "but I can't find an archive for it."),I.Name());
813c8eea
AL
230 }
231
d38b7b3d
AL
232 continue;
233 }
234
6c139d6e
AL
235 switch (I->CurrentState)
236 {
813c8eea
AL
237 /* This means installation failed somehow - it does not need to be
238 re-unpacked (probably) */
b518cca6
AL
239 case pkgCache::State::UnPacked:
240 case pkgCache::State::HalfConfigured:
813c8eea
AL
241 if (I.CurrentVer().Downloadable() == true ||
242 I.State() != pkgCache::PkgIterator::NeedsUnpack)
243 Cache.MarkKeep(I);
244 else
245 {
246 if (Cache[I].CandidateVerIter(Cache).Downloadable() == true)
247 Cache.MarkInstall(I);
248 else
249 Cache.MarkDelete(I);
250 }
6c139d6e
AL
251 break;
252
253 // This means removal failed
b518cca6 254 case pkgCache::State::HalfInstalled:
6c139d6e
AL
255 Cache.MarkDelete(I);
256 break;
257
258 default:
b518cca6 259 if (I->InstState != pkgCache::State::Ok)
6c139d6e
AL
260 return _error->Error("The package %s is not ok and I "
261 "don't know how to fix it!",I.Name());
262 }
263 }
264 return true;
265}
266 /*}}}*/
267// FixBroken - Fix broken packages /*{{{*/
268// ---------------------------------------------------------------------
0a8e3465
AL
269/* This autoinstalls every broken package and then runs the problem resolver
270 on the result. */
6c139d6e
AL
271bool pkgFixBroken(pkgDepCache &Cache)
272{
273 // Auto upgrade all broken packages
274 for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
275 if (Cache[I].NowBroken() == true)
276 Cache.MarkInstall(I,true);
7e798dd7 277
6c139d6e
AL
278 /* Fix packages that are in a NeedArchive state but don't have a
279 downloadable install version */
280 for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
281 {
282 if (I.State() != pkgCache::PkgIterator::NeedsUnpack ||
283 Cache[I].Delete() == true)
284 continue;
285
b518cca6 286 if (Cache[I].InstVerIter(Cache).Downloadable() == false)
6c139d6e
AL
287 continue;
288
7e798dd7 289 Cache.MarkInstall(I,true);
6c139d6e
AL
290 }
291
b2e465d6 292 pkgProblemResolver Fix(&Cache);
6c139d6e
AL
293 return Fix.Resolve(true);
294}
295 /*}}}*/
296// DistUpgrade - Distribution upgrade /*{{{*/
297// ---------------------------------------------------------------------
298/* This autoinstalls every package and then force installs every
299 pre-existing package. This creates the initial set of conditions which
300 most likely contain problems because too many things were installed.
301
0a8e3465 302 The problem resolver is used to resolve the problems.
6c139d6e
AL
303 */
304bool pkgDistUpgrade(pkgDepCache &Cache)
305{
306 /* Auto upgrade all installed packages, this provides the basis
307 for the installation */
308 for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
309 if (I->CurrentVer != 0)
310 Cache.MarkInstall(I,true);
311
312 /* Now, auto upgrade all essential packages - this ensures that
313 the essential packages are present and working */
314 for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
b518cca6 315 if ((I->Flags & pkgCache::Flag::Essential) == pkgCache::Flag::Essential)
6c139d6e
AL
316 Cache.MarkInstall(I,true);
317
318 /* We do it again over all previously installed packages to force
319 conflict resolution on them all. */
320 for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
321 if (I->CurrentVer != 0)
322 Cache.MarkInstall(I,false);
323
b2e465d6 324 pkgProblemResolver Fix(&Cache);
c88edf1d 325
6c139d6e 326 // Hold back held packages.
4490f2de 327 if (_config->FindB("APT::Ignore-Hold",false) == false)
6c139d6e 328 {
c88edf1d 329 for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
6c139d6e 330 {
c88edf1d
AL
331 if (I->SelectedState == pkgCache::State::Hold)
332 {
333 Fix.Protect(I);
334 Cache.MarkKeep(I);
335 }
6c139d6e
AL
336 }
337 }
338
339 return Fix.Resolve();
340}
341 /*}}}*/
0a8e3465
AL
342// AllUpgrade - Upgrade as many packages as possible /*{{{*/
343// ---------------------------------------------------------------------
344/* Right now the system must be consistent before this can be called.
345 It also will not change packages marked for install, it only tries
346 to install packages not marked for install */
347bool pkgAllUpgrade(pkgDepCache &Cache)
348{
b2e465d6 349 pkgProblemResolver Fix(&Cache);
0a8e3465
AL
350
351 if (Cache.BrokenCount() != 0)
352 return false;
353
354 // Upgrade all installed packages
355 for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
356 {
357 if (Cache[I].Install() == true)
358 Fix.Protect(I);
359
b2e465d6 360 if (_config->FindB("APT::Ignore-Hold",false) == false)
c88edf1d
AL
361 if (I->SelectedState == pkgCache::State::Hold)
362 continue;
0a8e3465
AL
363
364 if (I->CurrentVer != 0 && Cache[I].InstallVer != 0)
365 Cache.MarkInstall(I,false);
366 }
367
368 return Fix.ResolveByKeep();
369}
370 /*}}}*/
7e798dd7
AL
371// MinimizeUpgrade - Minimizes the set of packages to be upgraded /*{{{*/
372// ---------------------------------------------------------------------
373/* This simply goes over the entire set of packages and tries to keep
374 each package marked for upgrade. If a conflict is generated then
375 the package is restored. */
376bool pkgMinimizeUpgrade(pkgDepCache &Cache)
377{
378 if (Cache.BrokenCount() != 0)
379 return false;
380
381 // We loop indefinately to get the minimal set size.
382 bool Change = false;
a005475e 383 unsigned int Count = 0;
7e798dd7
AL
384 do
385 {
386 Change = false;
387 for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
388 {
389 // Not interesting
390 if (Cache[I].Upgrade() == false || Cache[I].NewInstall() == true)
391 continue;
a005475e 392
7e798dd7
AL
393 // Keep it and see if that is OK
394 Cache.MarkKeep(I);
395 if (Cache.BrokenCount() != 0)
396 Cache.MarkInstall(I,false);
397 else
a005475e
AL
398 {
399 // If keep didnt actually do anything then there was no change..
400 if (Cache[I].Upgrade() == false)
401 Change = true;
402 }
7e798dd7 403 }
a005475e 404 Count++;
7e798dd7 405 }
a005475e 406 while (Change == true && Count < 10);
7e798dd7
AL
407
408 if (Cache.BrokenCount() != 0)
409 return _error->Error("Internal Error in pkgMinimizeUpgrade");
410
411 return true;
412}
413 /*}}}*/
6c139d6e
AL
414
415// ProblemResolver::pkgProblemResolver - Constructor /*{{{*/
416// ---------------------------------------------------------------------
417/* */
b2e465d6 418pkgProblemResolver::pkgProblemResolver(pkgDepCache *pCache) : Cache(*pCache)
6c139d6e
AL
419{
420 // Allocate memory
b2e465d6 421 unsigned long Size = Cache.Head().PackageCount;
6c139d6e
AL
422 Scores = new signed short[Size];
423 Flags = new unsigned char[Size];
424 memset(Flags,0,sizeof(*Flags)*Size);
425
426 // Set debug to true to see its decision logic
0a8e3465 427 Debug = _config->FindB("Debug::pkgProblemResolver",false);
6c139d6e
AL
428}
429 /*}}}*/
b2e465d6
AL
430// ProblemResolver::~pkgProblemResolver - Destructor /*{{{*/
431// ---------------------------------------------------------------------
432/* */
433pkgProblemResolver::~pkgProblemResolver()
434{
435 delete [] Scores;
436 delete [] Flags;
437}
438 /*}}}*/
6c139d6e
AL
439// ProblemResolver::ScoreSort - Sort the list by score /*{{{*/
440// ---------------------------------------------------------------------
441/* */
442int pkgProblemResolver::ScoreSort(const void *a,const void *b)
443{
444 Package const **A = (Package const **)a;
445 Package const **B = (Package const **)b;
446 if (This->Scores[(*A)->ID] > This->Scores[(*B)->ID])
447 return -1;
448 if (This->Scores[(*A)->ID] < This->Scores[(*B)->ID])
449 return 1;
450 return 0;
451}
452 /*}}}*/
453// ProblemResolver::MakeScores - Make the score table /*{{{*/
454// ---------------------------------------------------------------------
455/* */
456void pkgProblemResolver::MakeScores()
457{
b2e465d6 458 unsigned long Size = Cache.Head().PackageCount;
6c139d6e
AL
459 memset(Scores,0,sizeof(*Scores)*Size);
460
461 // Generate the base scores for a package based on its properties
462 for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
463 {
464 if (Cache[I].InstallVer == 0)
465 continue;
466
467 signed short &Score = Scores[I->ID];
468
469 /* This is arbitary, it should be high enough to elevate an
470 essantial package above most other packages but low enough
471 to allow an obsolete essential packages to be removed by
472 a conflicts on a powerfull normal package (ie libc6) */
b518cca6 473 if ((I->Flags & pkgCache::Flag::Essential) == pkgCache::Flag::Essential)
6c139d6e
AL
474 Score += 100;
475
476 // We transform the priority
477 // Important Required Standard Optional Extra
478 signed short PrioMap[] = {0,3,2,1,-1,-2};
479 if (Cache[I].InstVerIter(Cache)->Priority <= 5)
480 Score += PrioMap[Cache[I].InstVerIter(Cache)->Priority];
481
482 /* This helps to fix oddball problems with conflicting packages
483 on the same level. We enhance the score of installed packages */
484 if (I->CurrentVer != 0)
485 Score += 1;
486 }
487
488 // Now that we have the base scores we go and propogate dependencies
489 for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
490 {
491 if (Cache[I].InstallVer == 0)
492 continue;
493
494 for (pkgCache::DepIterator D = Cache[I].InstVerIter(Cache).DependsList(); D.end() == false; D++)
495 {
b518cca6 496 if (D->Type == pkgCache::Dep::Depends || D->Type == pkgCache::Dep::PreDepends)
6c139d6e
AL
497 Scores[D.TargetPkg()->ID]++;
498 }
499 }
500
501 // Copy the scores to advoid additive looping
b2e465d6 502 SPtrArray<signed short> OldScores = new signed short[Size];
6c139d6e
AL
503 memcpy(OldScores,Scores,sizeof(*Scores)*Size);
504
505 /* Now we cause 1 level of dependency inheritance, that is we add the
506 score of the packages that depend on the target Package. This
507 fortifies high scoring packages */
508 for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
509 {
510 if (Cache[I].InstallVer == 0)
511 continue;
512
513 for (pkgCache::DepIterator D = I.RevDependsList(); D.end() == false; D++)
514 {
515 // Only do it for the install version
516 if ((pkgCache::Version *)D.ParentVer() != Cache[D.ParentPkg()].InstallVer ||
b518cca6 517 (D->Type != pkgCache::Dep::Depends && D->Type != pkgCache::Dep::PreDepends))
6c139d6e
AL
518 continue;
519
520 Scores[I->ID] += abs(OldScores[D.ParentPkg()->ID]);
521 }
522 }
523
524 /* Now we propogate along provides. This makes the packages that
525 provide important packages extremely important */
526 for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
527 {
528 for (pkgCache::PrvIterator P = I.ProvidesList(); P.end() == false; P++)
529 {
530 // Only do it once per package
531 if ((pkgCache::Version *)P.OwnerVer() != Cache[P.OwnerPkg()].InstallVer)
532 continue;
533 Scores[P.OwnerPkg()->ID] += abs(Scores[I->ID] - OldScores[I->ID]);
534 }
535 }
536
537 /* Protected things are pushed really high up. This number should put them
538 ahead of everything */
539 for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
d2685fd6 540 {
6c139d6e
AL
541 if ((Flags[I->ID] & Protected) != 0)
542 Scores[I->ID] += 10000;
d2685fd6
AL
543 if ((I->Flags & pkgCache::Flag::Essential) == pkgCache::Flag::Essential)
544 Scores[I->ID] += 5000;
b2e465d6 545 }
6c139d6e
AL
546}
547 /*}}}*/
548// ProblemResolver::DoUpgrade - Attempt to upgrade this package /*{{{*/
549// ---------------------------------------------------------------------
550/* This goes through and tries to reinstall packages to make this package
551 installable */
552bool pkgProblemResolver::DoUpgrade(pkgCache::PkgIterator Pkg)
553{
554 if ((Flags[Pkg->ID] & Upgradable) == 0 || Cache[Pkg].Upgradable() == false)
555 return false;
0a8e3465 556
6c139d6e
AL
557 Flags[Pkg->ID] &= ~Upgradable;
558
559 bool WasKept = Cache[Pkg].Keep();
560 Cache.MarkInstall(Pkg,false);
561
0a8e3465
AL
562 // This must be a virtual package or something like that.
563 if (Cache[Pkg].InstVerIter(Cache).end() == true)
564 return false;
565
6c139d6e
AL
566 // Isolate the problem dependency
567 bool Fail = false;
568 for (pkgCache::DepIterator D = Cache[Pkg].InstVerIter(Cache).DependsList(); D.end() == false;)
569 {
570 // Compute a single dependency element (glob or)
571 pkgCache::DepIterator Start = D;
572 pkgCache::DepIterator End = D;
573 unsigned char State = 0;
4b1b89c5 574 for (bool LastOR = true; D.end() == false && LastOR == true;)
6c139d6e
AL
575 {
576 State |= Cache[D];
b518cca6 577 LastOR = (D->CompareOp & pkgCache::Dep::Or) == pkgCache::Dep::Or;
4b1b89c5 578 D++;
6c139d6e
AL
579 if (LastOR == true)
580 End = D;
581 }
582
583 // We only worry about critical deps.
584 if (End.IsCritical() != true)
585 continue;
4b1b89c5
AL
586
587 // Iterate over all the members in the or group
588 while (1)
0a8e3465 589 {
4b1b89c5
AL
590 // Dep is ok now
591 if ((Cache[End] & pkgDepCache::DepGInstall) == pkgDepCache::DepGInstall)
592 break;
593
594 // Do not change protected packages
595 PkgIterator P = Start.SmartTargetPkg();
596 if ((Flags[P->ID] & Protected) == Protected)
597 {
598 if (Debug == true)
648e3cb4 599 clog << " Reinst Failed because of protected " << P.Name() << endl;
4b1b89c5 600 Fail = true;
4b1b89c5 601 }
648e3cb4 602 else
6c139d6e 603 {
648e3cb4
AL
604 // Upgrade the package if the candidate version will fix the problem.
605 if ((Cache[Start] & pkgDepCache::DepCVer) == pkgDepCache::DepCVer)
606 {
607 if (DoUpgrade(P) == false)
608 {
609 if (Debug == true)
610 clog << " Reinst Failed because of " << P.Name() << endl;
611 Fail = true;
612 }
613 else
614 {
615 Fail = false;
616 break;
617 }
618 }
619 else
4b1b89c5 620 {
648e3cb4
AL
621 /* We let the algorithm deal with conflicts on its next iteration,
622 it is much smarter than us */
b2e465d6
AL
623 if (Start->Type == pkgCache::Dep::Conflicts ||
624 Start->Type == pkgCache::Dep::Obsoletes)
625 break;
648e3cb4 626
4b1b89c5 627 if (Debug == true)
648e3cb4 628 clog << " Reinst Failed early because of " << Start.TargetPkg().Name() << endl;
4b1b89c5 629 Fail = true;
648e3cb4 630 }
4b1b89c5 631 }
6c139d6e 632
4b1b89c5
AL
633 if (Start == End)
634 break;
635 Start++;
636 }
637 if (Fail == true)
6c139d6e 638 break;
6c139d6e
AL
639 }
640
641 // Undo our operations - it might be smart to undo everything this did..
642 if (Fail == true)
643 {
644 if (WasKept == true)
645 Cache.MarkKeep(Pkg);
646 else
647 Cache.MarkDelete(Pkg);
648 return false;
649 }
650
651 if (Debug == true)
0a8e3465 652 clog << " Re-Instated " << Pkg.Name() << endl;
6c139d6e
AL
653 return true;
654}
655 /*}}}*/
656// ProblemResolver::Resolve - Run the resolution pass /*{{{*/
657// ---------------------------------------------------------------------
658/* This routines works by calculating a score for each package. The score
659 is derived by considering the package's priority and all reverse
660 dependents giving an integer that reflects the amount of breakage that
661 adjusting the package will inflict.
662
663 It goes from highest score to lowest and corrects all of the breaks by
664 keeping or removing the dependant packages. If that fails then it removes
665 the package itself and goes on. The routine should be able to intelligently
666 go from any broken state to a fixed state.
667
668 The BrokenFix flag enables a mode where the algorithm tries to
669 upgrade packages to advoid problems. */
670bool pkgProblemResolver::Resolve(bool BrokenFix)
671{
b2e465d6 672 unsigned long Size = Cache.Head().PackageCount;
6c139d6e
AL
673
674 // Record which packages are marked for install
675 bool Again = false;
676 do
677 {
678 Again = false;
679 for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
680 {
681 if (Cache[I].Install() == true)
682 Flags[I->ID] |= PreInstalled;
683 else
684 {
685 if (Cache[I].InstBroken() == true && BrokenFix == true)
686 {
687 Cache.MarkInstall(I,false);
688 if (Cache[I].Install() == true)
689 Again = true;
690 }
691
692 Flags[I->ID] &= ~PreInstalled;
693 }
694 Flags[I->ID] |= Upgradable;
695 }
696 }
697 while (Again == true);
698
699 if (Debug == true)
0a8e3465 700 clog << "Starting" << endl;
6c139d6e
AL
701
702 MakeScores();
703
704 /* We have to order the packages so that the broken fixing pass
705 operates from highest score to lowest. This prevents problems when
706 high score packages cause the removal of lower score packages that
707 would cause the removal of even lower score packages. */
b2e465d6 708 SPtrArray<pkgCache::Package *> PList = new pkgCache::Package *[Size];
6c139d6e
AL
709 pkgCache::Package **PEnd = PList;
710 for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
711 *PEnd++ = I;
712 This = this;
713 qsort(PList,PEnd - PList,sizeof(*PList),&ScoreSort);
714
715/* for (pkgCache::Package **K = PList; K != PEnd; K++)
716 if (Scores[(*K)->ID] != 0)
717 {
718 pkgCache::PkgIterator Pkg(Cache,*K);
0a8e3465 719 clog << Scores[(*K)->ID] << ' ' << Pkg.Name() <<
6c139d6e
AL
720 ' ' << (pkgCache::Version *)Pkg.CurrentVer() << ' ' <<
721 Cache[Pkg].InstallVer << ' ' << Cache[Pkg].CandidateVer << endl;
722 } */
723
724 if (Debug == true)
0a8e3465 725 clog << "Starting 2" << endl;
6c139d6e
AL
726
727 /* Now consider all broken packages. For each broken package we either
728 remove the package or fix it's problem. We do this once, it should
729 not be possible for a loop to form (that is a < b < c and fixing b by
730 changing a breaks c) */
731 bool Change = true;
732 for (int Counter = 0; Counter != 10 && Change == true; Counter++)
733 {
734 Change = false;
735 for (pkgCache::Package **K = PList; K != PEnd; K++)
736 {
737 pkgCache::PkgIterator I(Cache,*K);
738
739 /* We attempt to install this and see if any breaks result,
740 this takes care of some strange cases */
741 if (Cache[I].CandidateVer != Cache[I].InstallVer &&
742 I->CurrentVer != 0 && Cache[I].InstallVer != 0 &&
743 (Flags[I->ID] & PreInstalled) != 0 &&
0a8e3465
AL
744 (Flags[I->ID] & Protected) == 0 &&
745 (Flags[I->ID] & ReInstateTried) == 0)
6c139d6e
AL
746 {
747 if (Debug == true)
0a8e3465 748 clog << " Try to Re-Instate " << I.Name() << endl;
a6568219 749 unsigned long OldBreaks = Cache.BrokenCount();
6c139d6e 750 pkgCache::Version *OldVer = Cache[I].InstallVer;
0a8e3465
AL
751 Flags[I->ID] &= ReInstateTried;
752
6c139d6e
AL
753 Cache.MarkInstall(I,false);
754 if (Cache[I].InstBroken() == true ||
755 OldBreaks < Cache.BrokenCount())
756 {
757 if (OldVer == 0)
758 Cache.MarkDelete(I);
759 else
760 Cache.MarkKeep(I);
761 }
762 else
763 if (Debug == true)
0a8e3465 764 clog << "Re-Instated " << I.Name() << " (" << OldBreaks << " vs " << Cache.BrokenCount() << ')' << endl;
6c139d6e
AL
765 }
766
767 if (Cache[I].InstallVer == 0 || Cache[I].InstBroken() == false)
768 continue;
769
00b47c98
AL
770 if (Debug == true)
771 cout << "Investigating " << I.Name() << endl;
772
6c139d6e
AL
773 // Isolate the problem dependency
774 PackageKill KillList[100];
775 PackageKill *LEnd = KillList;
421c8d10
AL
776 bool InOr = false;
777 pkgCache::DepIterator Start;
778 pkgCache::DepIterator End;
b2e465d6 779 PackageKill *OldEnd = LEnd;
648e3cb4
AL
780
781 enum {OrRemove,OrKeep} OrOp = OrRemove;
421c8d10
AL
782 for (pkgCache::DepIterator D = Cache[I].InstVerIter(Cache).DependsList();
783 D.end() == false || InOr == true;)
6c139d6e
AL
784 {
785 // Compute a single dependency element (glob or)
648e3cb4
AL
786 if (Start == End)
787 {
788 // Decide what to do
789 if (InOr == true)
790 {
791 if (OldEnd == LEnd && OrOp == OrRemove)
70777d4b
AL
792 {
793 if ((Flags[I->ID] & Protected) != Protected)
00b47c98
AL
794 {
795 if (Debug == true)
796 clog << " Or group remove for " << I.Name() << endl;
70777d4b 797 Cache.MarkDelete(I);
00b47c98 798 }
70777d4b 799 }
648e3cb4 800 if (OldEnd == LEnd && OrOp == OrKeep)
00b47c98
AL
801 {
802 if (Debug == true)
803 clog << " Or group keep for " << I.Name() << endl;
648e3cb4 804 Cache.MarkKeep(I);
b2e465d6 805 }
648e3cb4
AL
806 }
807
b2e465d6
AL
808 /* We do an extra loop (as above) to finalize the or group
809 processing */
810 InOr = false;
648e3cb4 811 OrOp = OrRemove;
421c8d10 812 D.GlobOr(Start,End);
b2e465d6
AL
813 if (Start.end() == true)
814 break;
815
816 // We only worry about critical deps.
817 if (End.IsCritical() != true)
818 continue;
819
648e3cb4
AL
820 InOr = Start != End;
821 OldEnd = LEnd;
822 }
421c8d10
AL
823 else
824 Start++;
6c139d6e
AL
825
826 // Dep is ok
827 if ((Cache[End] & pkgDepCache::DepGInstall) == pkgDepCache::DepGInstall)
828 continue;
648e3cb4 829
6c139d6e 830 if (Debug == true)
421c8d10 831 clog << "Package " << I.Name() << " has broken dep on " << Start.TargetPkg().Name() << endl;
fcf85120
AL
832
833 /* Look across the version list. If there are no possible
834 targets then we keep the package and bail. This is necessary
835 if a package has a dep on another package that cant be found */
b2e465d6 836 SPtrArray<pkgCache::Version *> VList = Start.AllTargets();
fcf85120 837 if (*VList == 0 && (Flags[I->ID] & Protected) != Protected &&
648e3cb4 838 Start->Type != pkgCache::Dep::Conflicts &&
b2e465d6 839 Start->Type != pkgCache::Dep::Obsoletes &&
fcf85120 840 Cache[I].NowBroken() == false)
648e3cb4
AL
841 {
842 if (InOr == true)
843 {
844 /* No keep choice because the keep being OK could be the
845 result of another element in the OR group! */
846 continue;
847 }
848
fcf85120 849 Change = true;
648e3cb4 850 Cache.MarkKeep(I);
fcf85120
AL
851 break;
852 }
853
6c139d6e
AL
854 bool Done = false;
855 for (pkgCache::Version **V = VList; *V != 0; V++)
856 {
857 pkgCache::VerIterator Ver(Cache,*V);
858 pkgCache::PkgIterator Pkg = Ver.ParentPkg();
859
860 if (Debug == true)
421c8d10 861 clog << " Considering " << Pkg.Name() << ' ' << (int)Scores[Pkg->ID] <<
6c139d6e
AL
862 " as a solution to " << I.Name() << ' ' << (int)Scores[I->ID] << endl;
863 if (Scores[I->ID] <= Scores[Pkg->ID] ||
421c8d10 864 ((Cache[Start] & pkgDepCache::DepNow) == 0 &&
b2e465d6
AL
865 End->Type != pkgCache::Dep::Conflicts &&
866 End->Type != pkgCache::Dep::Obsoletes))
6c139d6e 867 {
200f8c52 868 // Try a little harder to fix protected packages..
3b5421b4 869 if ((Flags[I->ID] & Protected) == Protected)
200f8c52
AL
870 {
871 if (DoUpgrade(Pkg) == true)
0296c633 872 {
b2e465d6
AL
873 if (Scores[Pkg->ID] > Scores[I->ID])
874 Scores[Pkg->ID] = Scores[I->ID];
0296c633
AL
875 break;
876 }
877
6c139d6e 878 continue;
200f8c52
AL
879 }
880
881 /* See if a keep will do, unless the package is protected,
648e3cb4
AL
882 then installing it will be necessary */
883 bool Installed = Cache[I].Install();
6c139d6e
AL
884 Cache.MarkKeep(I);
885 if (Cache[I].InstBroken() == false)
886 {
648e3cb4
AL
887 // Unwind operation will be keep now
888 if (OrOp == OrRemove)
889 OrOp = OrKeep;
890
891 // Restore
892 if (InOr == true && Installed == true)
893 Cache.MarkInstall(I,false);
894
6c139d6e 895 if (Debug == true)
421c8d10 896 clog << " Holding Back " << I.Name() << " rather than change " << Start.TargetPkg().Name() << endl;
6c139d6e
AL
897 }
898 else
421c8d10 899 {
6c139d6e
AL
900 if (BrokenFix == false || DoUpgrade(I) == false)
901 {
421c8d10
AL
902 // Consider other options
903 if (InOr == false)
904 {
905 if (Debug == true)
906 clog << " Removing " << I.Name() << " rather than change " << Start.TargetPkg().Name() << endl;
907 Cache.MarkDelete(I);
908 if (Counter > 1)
b2e465d6
AL
909 {
910 if (Scores[Pkg->ID] > Scores[I->ID])
911 Scores[I->ID] = Scores[Pkg->ID];
912 }
421c8d10 913 }
0a8e3465 914 }
6c139d6e 915 }
b5dc9785 916
6c139d6e
AL
917 Change = true;
918 Done = true;
919 break;
920 }
921 else
922 {
00b47c98
AL
923 if (Debug == true)
924 clog << " Added " << Pkg.Name() << " to the remove list" << endl;
648e3cb4 925 // Skip adding to the kill list if it is protected
6c139d6e
AL
926 if ((Flags[Pkg->ID] & Protected) != 0)
927 continue;
928
929 LEnd->Pkg = Pkg;
930 LEnd->Dep = End;
931 LEnd++;
0a8e3465 932
b2e465d6
AL
933 if (Start->Type != pkgCache::Dep::Conflicts &&
934 Start->Type != pkgCache::Dep::Obsoletes)
6c139d6e
AL
935 break;
936 }
937 }
938
939 // Hm, nothing can possibly satisify this dep. Nuke it.
b2e465d6
AL
940 if (VList[0] == 0 &&
941 Start->Type != pkgCache::Dep::Conflicts &&
942 Start->Type != pkgCache::Dep::Obsoletes &&
648e3cb4 943 (Flags[I->ID] & Protected) != Protected)
6c139d6e 944 {
648e3cb4 945 bool Installed = Cache[I].Install();
6c139d6e
AL
946 Cache.MarkKeep(I);
947 if (Cache[I].InstBroken() == false)
948 {
648e3cb4
AL
949 // Unwind operation will be keep now
950 if (OrOp == OrRemove)
951 OrOp = OrKeep;
952
953 // Restore
954 if (InOr == true && Installed == true)
955 Cache.MarkInstall(I,false);
956
6c139d6e 957 if (Debug == true)
421c8d10 958 clog << " Holding Back " << I.Name() << " because I can't find " << Start.TargetPkg().Name() << endl;
6c139d6e
AL
959 }
960 else
961 {
962 if (Debug == true)
421c8d10 963 clog << " Removing " << I.Name() << " because I can't find " << Start.TargetPkg().Name() << endl;
648e3cb4
AL
964 if (InOr == false)
965 Cache.MarkDelete(I);
6c139d6e
AL
966 }
967
968 Change = true;
969 Done = true;
970 }
971
421c8d10
AL
972 // Try some more
973 if (InOr == true)
974 continue;
975
6c139d6e
AL
976 if (Done == true)
977 break;
978 }
979
980 // Apply the kill list now
981 if (Cache[I].InstallVer != 0)
648e3cb4 982 {
6c139d6e 983 for (PackageKill *J = KillList; J != LEnd; J++)
6c139d6e 984 {
648e3cb4
AL
985 Change = true;
986 if ((Cache[J->Dep] & pkgDepCache::DepGNow) == 0)
987 {
b2e465d6
AL
988 if (J->Dep->Type == pkgCache::Dep::Conflicts ||
989 J->Dep->Type == pkgCache::Dep::Obsoletes)
648e3cb4
AL
990 {
991 if (Debug == true)
992 clog << " Fixing " << I.Name() << " via remove of " << J->Pkg.Name() << endl;
993 Cache.MarkDelete(J->Pkg);
994 }
995 }
996 else
6c139d6e
AL
997 {
998 if (Debug == true)
648e3cb4
AL
999 clog << " Fixing " << I.Name() << " via keep of " << J->Pkg.Name() << endl;
1000 Cache.MarkKeep(J->Pkg);
6c139d6e 1001 }
b2e465d6 1002
648e3cb4 1003 if (Counter > 1)
b2e465d6
AL
1004 {
1005 if (Scores[I->ID] > Scores[J->Pkg->ID])
1006 Scores[J->Pkg->ID] = Scores[I->ID];
1007 }
648e3cb4
AL
1008 }
1009 }
1010 }
6c139d6e
AL
1011 }
1012
1013 if (Debug == true)
0a8e3465 1014 clog << "Done" << endl;
b2e465d6 1015
6c139d6e 1016 if (Cache.BrokenCount() != 0)
b5dc9785
AL
1017 {
1018 // See if this is the result of a hold
1019 pkgCache::PkgIterator I = Cache.PkgBegin();
1020 for (;I.end() != true; I++)
1021 {
1022 if (Cache[I].InstBroken() == false)
1023 continue;
1024 if ((Flags[I->ID] & Protected) != Protected)
b2e465d6 1025 return _error->Error(_("Error, pkgProblemResolver::Resolve generated breaks, this may be caused by held packages."));
b5dc9785 1026 }
b2e465d6 1027 return _error->Error(_("Unable to correct problems, you have held broken packages."));
b5dc9785
AL
1028 }
1029
0a8e3465
AL
1030 return true;
1031}
1032 /*}}}*/
1033// ProblemResolver::ResolveByKeep - Resolve problems using keep /*{{{*/
1034// ---------------------------------------------------------------------
1035/* This is the work horse of the soft upgrade routine. It is very gental
1036 in that it does not install or remove any packages. It is assumed that the
1037 system was non-broken previously. */
1038bool pkgProblemResolver::ResolveByKeep()
1039{
b2e465d6 1040 unsigned long Size = Cache.Head().PackageCount;
0a8e3465
AL
1041
1042 if (Debug == true)
1043 clog << "Entering ResolveByKeep" << endl;
1044
1045 MakeScores();
1046
1047 /* We have to order the packages so that the broken fixing pass
1048 operates from highest score to lowest. This prevents problems when
1049 high score packages cause the removal of lower score packages that
1050 would cause the removal of even lower score packages. */
1051 pkgCache::Package **PList = new pkgCache::Package *[Size];
1052 pkgCache::Package **PEnd = PList;
1053 for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
1054 *PEnd++ = I;
1055 This = this;
1056 qsort(PList,PEnd - PList,sizeof(*PList),&ScoreSort);
1057
1058 // Consider each broken package
1059 pkgCache::Package **LastStop = 0;
1060 for (pkgCache::Package **K = PList; K != PEnd; K++)
1061 {
1062 pkgCache::PkgIterator I(Cache,*K);
1063
1064 if (Cache[I].InstallVer == 0 || Cache[I].InstBroken() == false)
1065 continue;
1066
1067 /* Keep the package. If this works then great, otherwise we have
b2e465d6 1068 to be significantly more agressive and manipulate its dependencies */
0a8e3465
AL
1069 if ((Flags[I->ID] & Protected) == 0)
1070 {
1071 if (Debug == true)
1072 clog << "Keeping package " << I.Name() << endl;
1073 Cache.MarkKeep(I);
1074 if (Cache[I].InstBroken() == false)
1075 {
b2e465d6 1076 K = PList - 1;
0a8e3465
AL
1077 continue;
1078 }
1079 }
1080
1081 // Isolate the problem dependencies
1082 for (pkgCache::DepIterator D = Cache[I].InstVerIter(Cache).DependsList(); D.end() == false;)
1083 {
1084 // Compute a single dependency element (glob or)
1085 pkgCache::DepIterator Start = D;
1086 pkgCache::DepIterator End = D;
1087 unsigned char State = 0;
1088 for (bool LastOR = true; D.end() == false && LastOR == true; D++)
1089 {
1090 State |= Cache[D];
1091 LastOR = (D->CompareOp & pkgCache::Dep::Or) == pkgCache::Dep::Or;
1092 if (LastOR == true)
1093 End = D;
1094 }
1095
1096 // We only worry about critical deps.
1097 if (End.IsCritical() != true)
1098 continue;
1099
1100 // Dep is ok
1101 if ((Cache[End] & pkgDepCache::DepGInstall) == pkgDepCache::DepGInstall)
1102 continue;
1103
1104 // Hm, the group is broken.. I have no idea how to handle this
1105 if (Start != End)
1106 {
1107 clog << "Note, a broken or group was found in " << I.Name() << "." << endl;
1108 if ((Flags[I->ID] & Protected) == 0)
1109 Cache.MarkKeep(I);
1110 break;
1111 }
1112
1113 if (Debug == true)
1114 clog << "Package " << I.Name() << " has broken dep on " << End.TargetPkg().Name() << endl;
1115
1116 // Look at all the possible provides on this package
b2e465d6 1117 SPtrArray<pkgCache::Version *> VList = End.AllTargets();
0a8e3465
AL
1118 for (pkgCache::Version **V = VList; *V != 0; V++)
1119 {
1120 pkgCache::VerIterator Ver(Cache,*V);
1121 pkgCache::PkgIterator Pkg = Ver.ParentPkg();
1122
1123 // It is not keepable
1124 if (Cache[Pkg].InstallVer == 0 ||
1125 Pkg->CurrentVer == 0)
1126 continue;
1127
1128 if ((Flags[I->ID] & Protected) == 0)
1129 {
1130 if (Debug == true)
1131 clog << " Keeping Package " << Pkg.Name() << " due to dep" << endl;
1132 Cache.MarkKeep(Pkg);
1133 }
1134
1135 if (Cache[I].InstBroken() == false)
1136 break;
1137 }
1138
1139 if (Cache[I].InstBroken() == false)
1140 break;
1141 }
1142
1143 if (Cache[I].InstBroken() == true)
1144 continue;
1145
1146 // Restart again.
1147 if (K == LastStop)
1148 return _error->Error("Internal Error, pkgProblemResolver::ResolveByKeep is looping on package %s.",I.Name());
1149 LastStop = K;
b2e465d6 1150 K = PList - 1;
0a8e3465 1151 }
6c139d6e
AL
1152
1153 return true;
1154}
1155 /*}}}*/
3b5421b4
AL
1156// ProblemResolver::InstallProtect - Install all protected packages /*{{{*/
1157// ---------------------------------------------------------------------
1158/* This is used to make sure protected packages are installed */
1159void pkgProblemResolver::InstallProtect()
1160{
1161 for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
1162 {
1163 if ((Flags[I->ID] & Protected) == Protected)
1164 {
1165 if ((Flags[I->ID] & ToRemove) == ToRemove)
1166 Cache.MarkDelete(I);
1167 else
1168 Cache.MarkInstall(I,false);
1169 }
1170 }
1171}
1172 /*}}}*/
b2e465d6
AL
1173
1174// PrioSortList - Sort a list of versions by priority /*{{{*/
1175// ---------------------------------------------------------------------
1176/* This is ment to be used in conjunction with AllTargets to get a list
1177 of versions ordered by preference. */
1178static pkgCache *PrioCache;
1179static int PrioComp(const void *A,const void *B)
1180{
1181 pkgCache::VerIterator L(*PrioCache,*(pkgCache::Version **)A);
1182 pkgCache::VerIterator R(*PrioCache,*(pkgCache::Version **)B);
1183
1184 if ((L.ParentPkg()->Flags & pkgCache::Flag::Essential) == pkgCache::Flag::Essential &&
1185 (L.ParentPkg()->Flags & pkgCache::Flag::Essential) != pkgCache::Flag::Essential)
1186 return 1;
1187 if ((L.ParentPkg()->Flags & pkgCache::Flag::Essential) != pkgCache::Flag::Essential &&
1188 (L.ParentPkg()->Flags & pkgCache::Flag::Essential) == pkgCache::Flag::Essential)
1189 return -1;
1190
1191 if (L->Priority != R->Priority)
1192 return L->Priority - R->Priority;
1193 return strcmp(L.ParentPkg().Name(),R.ParentPkg().Name());
1194}
1195void pkgPrioSortList(pkgCache &Cache,pkgCache::Version **List)
1196{
1197 unsigned long Count = 0;
1198 PrioCache = &Cache;
1199 for (pkgCache::Version **I = List; *I != 0; I++)
1200 Count++;
1201 qsort(List,Count,sizeof(*List),PrioComp);
1202}
1203 /*}}}*/