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