]> git.saurik.com Git - apt.git/blob - apt-pkg/packagemanager.cc
Merge remote-tracking branch 'mvo/bugfix/update-progress-reporting' into debian/exper...
[apt.git] / apt-pkg / packagemanager.cc
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 // $Id: packagemanager.cc,v 1.30 2003/04/27 03:04:15 doogie Exp $
4 /* ######################################################################
5
6 Package Manager - Abstacts the package manager
7
8 More work is needed in the area of transitioning provides, ie exim
9 replacing smail. This can cause interesing side effects.
10
11 Other cases involving conflicts+replaces should be tested.
12
13 ##################################################################### */
14 /*}}}*/
15 // Include Files /*{{{*/
16 #include <config.h>
17
18 #include <apt-pkg/packagemanager.h>
19 #include <apt-pkg/orderlist.h>
20 #include <apt-pkg/depcache.h>
21 #include <apt-pkg/error.h>
22 #include <apt-pkg/version.h>
23 #include <apt-pkg/acquire-item.h>
24 #include <apt-pkg/algorithms.h>
25 #include <apt-pkg/configuration.h>
26 #include <apt-pkg/sptr.h>
27 #include <apt-pkg/macros.h>
28 #include <apt-pkg/pkgcache.h>
29 #include <apt-pkg/cacheiterators.h>
30 #include <apt-pkg/strutl.h>
31 #include <apt-pkg/install-progress.h>
32
33 #include <stddef.h>
34 #include <list>
35 #include <string>
36 #include <iostream>
37
38 #include <apti18n.h>
39 /*}}}*/
40 using namespace std;
41
42 bool pkgPackageManager::SigINTStop = false;
43
44 // PM::PackageManager - Constructor /*{{{*/
45 // ---------------------------------------------------------------------
46 /* */
47 pkgPackageManager::pkgPackageManager(pkgDepCache *pCache) : Cache(*pCache),
48 List(NULL), Res(Incomplete)
49 {
50 FileNames = new string[Cache.Head().PackageCount];
51 Debug = _config->FindB("Debug::pkgPackageManager",false);
52 NoImmConfigure = !_config->FindB("APT::Immediate-Configure",true);
53 ImmConfigureAll = _config->FindB("APT::Immediate-Configure-All",false);
54 }
55 /*}}}*/
56 // PM::PackageManager - Destructor /*{{{*/
57 // ---------------------------------------------------------------------
58 /* */
59 pkgPackageManager::~pkgPackageManager()
60 {
61 delete List;
62 delete [] FileNames;
63 }
64 /*}}}*/
65 // PM::GetArchives - Queue the archives for download /*{{{*/
66 // ---------------------------------------------------------------------
67 /* */
68 bool pkgPackageManager::GetArchives(pkgAcquire *Owner,pkgSourceList *Sources,
69 pkgRecords *Recs)
70 {
71 if (CreateOrderList() == false)
72 return false;
73
74 bool const ordering =
75 _config->FindB("PackageManager::UnpackAll",true) ?
76 List->OrderUnpack() : List->OrderCritical();
77 if (ordering == false)
78 return _error->Error("Internal ordering error");
79
80 for (pkgOrderList::iterator I = List->begin(); I != List->end(); ++I)
81 {
82 PkgIterator Pkg(Cache,*I);
83 FileNames[Pkg->ID] = string();
84
85 // Skip packages to erase
86 if (Cache[Pkg].Delete() == true)
87 continue;
88
89 // Skip Packages that need configure only.
90 if (Pkg.State() == pkgCache::PkgIterator::NeedsConfigure &&
91 Cache[Pkg].Keep() == true)
92 continue;
93
94 // Skip already processed packages
95 if (List->IsNow(Pkg) == false)
96 continue;
97
98 new pkgAcqArchive(Owner,Sources,Recs,Cache[Pkg].InstVerIter(Cache),
99 FileNames[Pkg->ID]);
100 }
101
102 return true;
103 }
104 /*}}}*/
105 // PM::FixMissing - Keep all missing packages /*{{{*/
106 // ---------------------------------------------------------------------
107 /* This is called to correct the installation when packages could not
108 be downloaded. */
109 bool pkgPackageManager::FixMissing()
110 {
111 pkgDepCache::ActionGroup group(Cache);
112 pkgProblemResolver Resolve(&Cache);
113 List->SetFileList(FileNames);
114
115 bool Bad = false;
116 for (PkgIterator I = Cache.PkgBegin(); I.end() == false; ++I)
117 {
118 if (List->IsMissing(I) == false)
119 continue;
120
121 // Okay, this file is missing and we need it. Mark it for keep
122 Bad = true;
123 Cache.MarkKeep(I, false, false);
124 }
125
126 // We have to empty the list otherwise it will not have the new changes
127 delete List;
128 List = 0;
129
130 if (Bad == false)
131 return true;
132
133 // Now downgrade everything that is broken
134 return Resolve.ResolveByKeep() == true && Cache.BrokenCount() == 0;
135 }
136 /*}}}*/
137 // PM::ImmediateAdd - Add the immediate flag recursivly /*{{{*/
138 // ---------------------------------------------------------------------
139 /* This adds the immediate flag to the pkg and recursively to the
140 dependendies
141 */
142 void pkgPackageManager::ImmediateAdd(PkgIterator I, bool UseInstallVer, unsigned const int &Depth)
143 {
144 DepIterator D;
145
146 if(UseInstallVer)
147 {
148 if(Cache[I].InstallVer == 0)
149 return;
150 D = Cache[I].InstVerIter(Cache).DependsList();
151 } else {
152 if (I->CurrentVer == 0)
153 return;
154 D = I.CurrentVer().DependsList();
155 }
156
157 for ( /* nothing */ ; D.end() == false; ++D)
158 if (D->Type == pkgCache::Dep::Depends || D->Type == pkgCache::Dep::PreDepends)
159 {
160 if(!List->IsFlag(D.TargetPkg(), pkgOrderList::Immediate))
161 {
162 if(Debug)
163 clog << OutputInDepth(Depth) << "ImmediateAdd(): Adding Immediate flag to " << D.TargetPkg() << " cause of " << D.DepType() << " " << I.FullName() << endl;
164 List->Flag(D.TargetPkg(),pkgOrderList::Immediate);
165 ImmediateAdd(D.TargetPkg(), UseInstallVer, Depth + 1);
166 }
167 }
168 return;
169 }
170 /*}}}*/
171 // PM::CreateOrderList - Create the ordering class /*{{{*/
172 // ---------------------------------------------------------------------
173 /* This populates the ordering list with all the packages that are
174 going to change. */
175 bool pkgPackageManager::CreateOrderList()
176 {
177 if (List != 0)
178 return true;
179
180 delete List;
181 List = new pkgOrderList(&Cache);
182
183 if (Debug && ImmConfigureAll)
184 clog << "CreateOrderList(): Adding Immediate flag for all packages because of APT::Immediate-Configure-All" << endl;
185
186 // Generate the list of affected packages and sort it
187 for (PkgIterator I = Cache.PkgBegin(); I.end() == false; ++I)
188 {
189 // Ignore no-version packages
190 if (I->VersionList == 0)
191 continue;
192
193 // Mark the package and its dependends for immediate configuration
194 if ((((I->Flags & pkgCache::Flag::Essential) == pkgCache::Flag::Essential) &&
195 NoImmConfigure == false) || ImmConfigureAll)
196 {
197 if(Debug && !ImmConfigureAll)
198 clog << "CreateOrderList(): Adding Immediate flag for " << I.FullName() << endl;
199 List->Flag(I,pkgOrderList::Immediate);
200
201 if (!ImmConfigureAll) {
202 // Look for other install packages to make immediate configurea
203 ImmediateAdd(I, true);
204
205 // And again with the current version.
206 ImmediateAdd(I, false);
207 }
208 }
209
210 // Not interesting
211 if ((Cache[I].Keep() == true ||
212 Cache[I].InstVerIter(Cache) == I.CurrentVer()) &&
213 I.State() == pkgCache::PkgIterator::NeedsNothing &&
214 (Cache[I].iFlags & pkgDepCache::ReInstall) != pkgDepCache::ReInstall &&
215 (I.Purge() != false || Cache[I].Mode != pkgDepCache::ModeDelete ||
216 (Cache[I].iFlags & pkgDepCache::Purge) != pkgDepCache::Purge))
217 continue;
218
219 // Append it to the list
220 List->push_back(I);
221 }
222
223 return true;
224 }
225 /*}}}*/
226 // PM::DepAlwaysTrue - Returns true if this dep is irrelevant /*{{{*/
227 // ---------------------------------------------------------------------
228 /* The restriction on provides is to eliminate the case when provides
229 are transitioning between valid states [ie exim to smail] */
230 bool pkgPackageManager::DepAlwaysTrue(DepIterator D)
231 {
232 if (D.TargetPkg()->ProvidesList != 0)
233 return false;
234
235 if ((Cache[D] & pkgDepCache::DepInstall) != 0 &&
236 (Cache[D] & pkgDepCache::DepNow) != 0)
237 return true;
238 return false;
239 }
240 /*}}}*/
241 // PM::CheckRConflicts - Look for reverse conflicts /*{{{*/
242 // ---------------------------------------------------------------------
243 /* This looks over the reverses for a conflicts line that needs early
244 removal. */
245 bool pkgPackageManager::CheckRConflicts(PkgIterator Pkg,DepIterator D,
246 const char *Ver)
247 {
248 for (;D.end() == false; ++D)
249 {
250 if (D->Type != pkgCache::Dep::Conflicts &&
251 D->Type != pkgCache::Dep::Obsoletes)
252 continue;
253
254 // The package hasn't been changed
255 if (List->IsNow(Pkg) == false)
256 continue;
257
258 // Ignore self conflicts, ignore conflicts from irrelevant versions
259 if (D.IsIgnorable(Pkg) || D.ParentVer() != D.ParentPkg().CurrentVer())
260 continue;
261
262 if (Cache.VS().CheckDep(Ver,D->CompareOp,D.TargetVer()) == false)
263 continue;
264
265 if (EarlyRemove(D.ParentPkg()) == false)
266 return _error->Error("Reverse conflicts early remove for package '%s' failed",
267 Pkg.FullName().c_str());
268 }
269 return true;
270 }
271 /*}}}*/
272 // PM::ConfigureAll - Run the all out configuration /*{{{*/
273 // ---------------------------------------------------------------------
274 /* This configures every package. It is assumed they are all unpacked and
275 that the final configuration is valid. This is also used to catch packages
276 that have not been configured when using ImmConfigureAll */
277 bool pkgPackageManager::ConfigureAll()
278 {
279 pkgOrderList OList(&Cache);
280
281 // Populate the order list
282 for (pkgOrderList::iterator I = List->begin(); I != List->end(); ++I)
283 if (List->IsFlag(pkgCache::PkgIterator(Cache,*I),
284 pkgOrderList::UnPacked) == true)
285 OList.push_back(*I);
286
287 if (OList.OrderConfigure() == false)
288 return false;
289
290 std::string const conf = _config->Find("PackageManager::Configure","all");
291 bool const ConfigurePkgs = (conf == "all");
292
293 // Perform the configuring
294 for (pkgOrderList::iterator I = OList.begin(); I != OList.end(); ++I)
295 {
296 PkgIterator Pkg(Cache,*I);
297
298 /* Check if the package has been configured, this can happen if SmartConfigure
299 calls its self */
300 if (List->IsFlag(Pkg,pkgOrderList::Configured)) continue;
301
302 if (ConfigurePkgs == true && SmartConfigure(Pkg, 0) == false) {
303 if (ImmConfigureAll)
304 _error->Error(_("Could not perform immediate configuration on '%s'. "
305 "Please see man 5 apt.conf under APT::Immediate-Configure for details. (%d)"),Pkg.FullName().c_str(),1);
306 else
307 _error->Error("Internal error, packages left unconfigured. %s",Pkg.FullName().c_str());
308 return false;
309 }
310
311 List->Flag(Pkg,pkgOrderList::Configured,pkgOrderList::States);
312 }
313
314 return true;
315 }
316 /*}}}*/
317 // PM::SmartConfigure - Perform immediate configuration of the pkg /*{{{*/
318 // ---------------------------------------------------------------------
319 /* This function tries to put the system in a state where Pkg can be configured.
320 This involves checking each of Pkg's dependanies and unpacking and
321 configuring packages where needed.
322
323 Note on failure: This method can fail, without causing any problems.
324 This can happen when using Immediate-Configure-All, SmartUnPack may call
325 SmartConfigure, it may fail because of a complex dependency situation, but
326 a error will only be reported if ConfigureAll fails. This is why some of the
327 messages this function reports on failure (return false;) as just warnings
328 only shown when debuging*/
329 bool pkgPackageManager::SmartConfigure(PkgIterator Pkg, int const Depth)
330 {
331 // If this is true, only check and correct and dependencies without the Loop flag
332 bool const PkgLoop = List->IsFlag(Pkg,pkgOrderList::Loop);
333
334 if (Debug) {
335 VerIterator InstallVer = VerIterator(Cache,Cache[Pkg].InstallVer);
336 clog << OutputInDepth(Depth) << "SmartConfigure " << Pkg.FullName() << " (" << InstallVer.VerStr() << ")";
337 if (PkgLoop)
338 clog << " (Only Correct Dependencies)";
339 clog << endl;
340 }
341
342 VerIterator const instVer = Cache[Pkg].InstVerIter(Cache);
343
344 /* Because of the ordered list, most dependencies should be unpacked,
345 however if there is a loop (A depends on B, B depends on A) this will not
346 be the case, so check for dependencies before configuring. */
347 bool Bad = false, Changed = false;
348 const unsigned int max_loops = _config->FindI("APT::pkgPackageManager::MaxLoopCount", 5000);
349 unsigned int i=0;
350 std::list<DepIterator> needConfigure;
351 do
352 {
353 Changed = false;
354 for (DepIterator D = instVer.DependsList(); D.end() == false; )
355 {
356 // Compute a single dependency element (glob or)
357 pkgCache::DepIterator Start, End;
358 D.GlobOr(Start,End);
359
360 if (End->Type != pkgCache::Dep::Depends)
361 continue;
362 Bad = true;
363
364 // Check for dependencies that have not been unpacked, probably due to loops.
365 for (DepIterator Cur = Start; true; ++Cur)
366 {
367 SPtrArray<Version *> VList = Cur.AllTargets();
368
369 for (Version **I = VList; *I != 0; ++I)
370 {
371 VerIterator Ver(Cache,*I);
372 PkgIterator DepPkg = Ver.ParentPkg();
373
374 // Check if the current version of the package is available and will satisfy this dependency
375 if (DepPkg.CurrentVer() == Ver && List->IsNow(DepPkg) == true &&
376 List->IsFlag(DepPkg,pkgOrderList::Removed) == false &&
377 DepPkg.State() == PkgIterator::NeedsNothing)
378 {
379 Bad = false;
380 break;
381 }
382
383 // Check if the version that is going to be installed will satisfy the dependency
384 if (Cache[DepPkg].InstallVer != *I || List->IsNow(DepPkg) == false)
385 continue;
386
387 if (PkgLoop == true)
388 {
389 if (Debug)
390 std::clog << OutputInDepth(Depth) << "Package " << Pkg << " loops in SmartConfigure" << std::endl;
391 Bad = false;
392 break;
393 }
394 else
395 {
396 if (Debug)
397 clog << OutputInDepth(Depth) << "Unpacking " << DepPkg.FullName() << " to avoid loop " << Cur << endl;
398 if (PkgLoop == false)
399 List->Flag(Pkg,pkgOrderList::Loop);
400 if (SmartUnPack(DepPkg, true, Depth + 1) == true)
401 {
402 Bad = false;
403 if (List->IsFlag(DepPkg,pkgOrderList::Loop) == false)
404 Changed = true;
405 }
406 if (PkgLoop == false)
407 List->RmFlag(Pkg,pkgOrderList::Loop);
408 if (Bad == false)
409 break;
410 }
411 }
412
413 if (Cur == End || Bad == false)
414 break;
415 }
416
417 if (Bad == false)
418 continue;
419
420 needConfigure.push_back(Start);
421 }
422 if (i++ > max_loops)
423 return _error->Error("Internal error: MaxLoopCount reached in SmartUnPack (1) for %s, aborting", Pkg.FullName().c_str());
424 } while (Changed == true);
425
426 Bad = false, Changed = false, i = 0;
427 do
428 {
429 Changed = false;
430 for (std::list<DepIterator>::const_iterator D = needConfigure.begin(); D != needConfigure.end(); ++D)
431 {
432 // Compute a single dependency element (glob or) without modifying D
433 pkgCache::DepIterator Start, End;
434 {
435 pkgCache::DepIterator Discard = *D;
436 Discard.GlobOr(Start,End);
437 }
438
439 if (End->Type != pkgCache::Dep::Depends)
440 continue;
441 Bad = true;
442
443 // Search for dependencies which are unpacked but aren't configured yet (maybe loops)
444 for (DepIterator Cur = Start; true; ++Cur)
445 {
446 SPtrArray<Version *> VList = Cur.AllTargets();
447
448 for (Version **I = VList; *I != 0; ++I)
449 {
450 VerIterator Ver(Cache,*I);
451 PkgIterator DepPkg = Ver.ParentPkg();
452
453 // Check if the version that is going to be installed will satisfy the dependency
454 if (Cache[DepPkg].InstallVer != *I)
455 continue;
456
457 if (List->IsFlag(DepPkg,pkgOrderList::UnPacked))
458 {
459 if (List->IsFlag(DepPkg,pkgOrderList::Loop) && PkgLoop)
460 {
461 // This dependency has already been dealt with by another SmartConfigure on Pkg
462 Bad = false;
463 break;
464 }
465 /* Check for a loop to prevent one forming
466 If A depends on B and B depends on A, SmartConfigure will
467 just hop between them if this is not checked. Dont remove the
468 loop flag after finishing however as loop is already set.
469 This means that there is another SmartConfigure call for this
470 package and it will remove the loop flag */
471 if (PkgLoop == false)
472 List->Flag(Pkg,pkgOrderList::Loop);
473 if (SmartConfigure(DepPkg, Depth + 1) == true)
474 {
475 Bad = false;
476 if (List->IsFlag(DepPkg,pkgOrderList::Loop) == false)
477 Changed = true;
478 }
479 if (PkgLoop == false)
480 List->RmFlag(Pkg,pkgOrderList::Loop);
481 // If SmartConfigure was succesfull, Bad is false, so break
482 if (Bad == false)
483 break;
484 }
485 else if (List->IsFlag(DepPkg,pkgOrderList::Configured))
486 {
487 Bad = false;
488 break;
489 }
490 }
491 if (Cur == End || Bad == false)
492 break;
493 }
494
495
496 if (Bad == true && Changed == false && Debug == true)
497 std::clog << OutputInDepth(Depth) << "Could not satisfy " << *D << std::endl;
498 }
499 if (i++ > max_loops)
500 return _error->Error("Internal error: MaxLoopCount reached in SmartUnPack (2) for %s, aborting", Pkg.FullName().c_str());
501 } while (Changed == true);
502
503 if (Bad) {
504 if (Debug)
505 _error->Warning(_("Could not configure '%s'. "),Pkg.FullName().c_str());
506 return false;
507 }
508
509 if (PkgLoop) return true;
510
511 static std::string const conf = _config->Find("PackageManager::Configure","all");
512 static bool const ConfigurePkgs = (conf == "all" || conf == "smart");
513
514 if (List->IsFlag(Pkg,pkgOrderList::Configured))
515 return _error->Error("Internal configure error on '%s'.", Pkg.FullName().c_str());
516
517 if (ConfigurePkgs == true && Configure(Pkg) == false)
518 return false;
519
520 List->Flag(Pkg,pkgOrderList::Configured,pkgOrderList::States);
521
522 if ((Cache[Pkg].InstVerIter(Cache)->MultiArch & pkgCache::Version::Same) == pkgCache::Version::Same)
523 for (PkgIterator P = Pkg.Group().PackageList();
524 P.end() == false; P = Pkg.Group().NextPkg(P))
525 {
526 if (Pkg == P || List->IsFlag(P,pkgOrderList::Configured) == true ||
527 List->IsFlag(P,pkgOrderList::UnPacked) == false ||
528 Cache[P].InstallVer == 0 || (P.CurrentVer() == Cache[P].InstallVer &&
529 (Cache[Pkg].iFlags & pkgDepCache::ReInstall) != pkgDepCache::ReInstall))
530 continue;
531 SmartConfigure(P, (Depth +1));
532 }
533
534 // Sanity Check
535 if (List->IsFlag(Pkg,pkgOrderList::Configured) == false)
536 return _error->Error(_("Could not configure '%s'. "),Pkg.FullName().c_str());
537
538 return true;
539 }
540 /*}}}*/
541 // PM::EarlyRemove - Perform removal of packages before their time /*{{{*/
542 // ---------------------------------------------------------------------
543 /* This is called to deal with conflicts arising from unpacking */
544 bool pkgPackageManager::EarlyRemove(PkgIterator Pkg)
545 {
546 if (List->IsNow(Pkg) == false)
547 return true;
548
549 // Already removed it
550 if (List->IsFlag(Pkg,pkgOrderList::Removed) == true)
551 return true;
552
553 // Woops, it will not be re-installed!
554 if (List->IsFlag(Pkg,pkgOrderList::InList) == false)
555 return false;
556
557 // Essential packages get special treatment
558 bool IsEssential = false;
559 if ((Pkg->Flags & pkgCache::Flag::Essential) != 0 ||
560 (Pkg->Flags & pkgCache::Flag::Important) != 0)
561 IsEssential = true;
562
563 /* Check for packages that are the dependents of essential packages and
564 promote them too */
565 if (Pkg->CurrentVer != 0)
566 {
567 for (DepIterator D = Pkg.RevDependsList(); D.end() == false &&
568 IsEssential == false; ++D)
569 if (D->Type == pkgCache::Dep::Depends || D->Type == pkgCache::Dep::PreDepends)
570 if ((D.ParentPkg()->Flags & pkgCache::Flag::Essential) != 0 ||
571 (D.ParentPkg()->Flags & pkgCache::Flag::Important) != 0)
572 IsEssential = true;
573 }
574
575 if (IsEssential == true)
576 {
577 if (_config->FindB("APT::Force-LoopBreak",false) == false)
578 return _error->Error(_("This installation run will require temporarily "
579 "removing the essential package %s due to a "
580 "Conflicts/Pre-Depends loop. This is often bad, "
581 "but if you really want to do it, activate the "
582 "APT::Force-LoopBreak option."),Pkg.FullName().c_str());
583 }
584
585 bool Res = SmartRemove(Pkg);
586 if (Cache[Pkg].Delete() == false)
587 List->Flag(Pkg,pkgOrderList::Removed,pkgOrderList::States);
588
589 return Res;
590 }
591 /*}}}*/
592 // PM::SmartRemove - Removal Helper /*{{{*/
593 // ---------------------------------------------------------------------
594 /* */
595 bool pkgPackageManager::SmartRemove(PkgIterator Pkg)
596 {
597 if (List->IsNow(Pkg) == false)
598 return true;
599
600 List->Flag(Pkg,pkgOrderList::Configured,pkgOrderList::States);
601
602 return Remove(Pkg,(Cache[Pkg].iFlags & pkgDepCache::Purge) == pkgDepCache::Purge);
603 }
604 /*}}}*/
605 // PM::SmartUnPack - Install helper /*{{{*/
606 // ---------------------------------------------------------------------
607 /* This puts the system in a state where it can Unpack Pkg, if Pkg is already
608 unpacked, or when it has been unpacked, if Immediate==true it configures it. */
609 bool pkgPackageManager::SmartUnPack(PkgIterator Pkg)
610 {
611 return SmartUnPack(Pkg, true, 0);
612 }
613 bool pkgPackageManager::SmartUnPack(PkgIterator Pkg, bool const Immediate, int const Depth)
614 {
615 bool PkgLoop = List->IsFlag(Pkg,pkgOrderList::Loop);
616
617 if (Debug) {
618 clog << OutputInDepth(Depth) << "SmartUnPack " << Pkg.FullName();
619 VerIterator InstallVer = VerIterator(Cache,Cache[Pkg].InstallVer);
620 if (Pkg.CurrentVer() == 0)
621 clog << " (install version " << InstallVer.VerStr() << ")";
622 else
623 clog << " (replace version " << Pkg.CurrentVer().VerStr() << " with " << InstallVer.VerStr() << ")";
624 if (PkgLoop)
625 clog << " (Only Perform PreUnpack Checks)";
626 if (Immediate)
627 clog << " immediately";
628 clog << endl;
629 }
630
631 VerIterator const instVer = Cache[Pkg].InstVerIter(Cache);
632
633 /* PreUnpack Checks: This loop checks and attempts to rectify and problems that would prevent the package being unpacked.
634 It addresses: PreDepends, Conflicts, Obsoletes and Breaks (DpkgBreaks). Any resolutions that do not require it should
635 avoid configuration (calling SmartUnpack with Immediate=true), this is because when unpacking some packages with
636 complex dependency structures, trying to configure some packages while breaking the loops can complicate things .
637 This will be either dealt with if the package is configured as a dependency of Pkg (if and when Pkg is configured),
638 or by the ConfigureAll call at the end of the for loop in OrderInstall. */
639 bool Changed = false;
640 const unsigned int max_loops = _config->FindI("APT::pkgPackageManager::MaxLoopCount", 5000);
641 unsigned int i = 0;
642 do
643 {
644 Changed = false;
645 for (DepIterator D = instVer.DependsList(); D.end() == false; )
646 {
647 // Compute a single dependency element (glob or)
648 pkgCache::DepIterator Start, End;
649 D.GlobOr(Start,End);
650
651 if (End->Type == pkgCache::Dep::PreDepends)
652 {
653 bool Bad = true;
654 if (Debug)
655 clog << OutputInDepth(Depth) << "PreDepends order for " << Pkg.FullName() << std::endl;
656
657 // Look for easy targets: packages that are already okay
658 for (DepIterator Cur = Start; Bad == true; ++Cur)
659 {
660 SPtrArray<Version *> VList = Cur.AllTargets();
661 for (Version **I = VList; *I != 0; ++I)
662 {
663 VerIterator Ver(Cache,*I);
664 PkgIterator Pkg = Ver.ParentPkg();
665
666 // See if the current version is ok
667 if (Pkg.CurrentVer() == Ver && List->IsNow(Pkg) == true &&
668 Pkg.State() == PkgIterator::NeedsNothing)
669 {
670 Bad = false;
671 if (Debug)
672 clog << OutputInDepth(Depth) << "Found ok package " << Pkg.FullName() << endl;
673 break;
674 }
675 }
676 if (Cur == End)
677 break;
678 }
679
680 // Look for something that could be configured.
681 for (DepIterator Cur = Start; Bad == true && Cur.end() == false; ++Cur)
682 {
683 SPtrArray<Version *> VList = Cur.AllTargets();
684 for (Version **I = VList; *I != 0; ++I)
685 {
686 VerIterator Ver(Cache,*I);
687 PkgIterator Pkg = Ver.ParentPkg();
688
689 // Not the install version
690 if (Cache[Pkg].InstallVer != *I ||
691 (Cache[Pkg].Keep() == true && Pkg.State() == PkgIterator::NeedsNothing))
692 continue;
693
694 if (List->IsFlag(Pkg,pkgOrderList::Configured))
695 {
696 Bad = false;
697 break;
698 }
699
700 // check if it needs unpack or if if configure is enough
701 if (List->IsFlag(Pkg,pkgOrderList::UnPacked) == false)
702 {
703 if (Debug)
704 clog << OutputInDepth(Depth) << "Trying to SmartUnpack " << Pkg.FullName() << endl;
705 // SmartUnpack with the ImmediateFlag to ensure its really ready
706 if (SmartUnPack(Pkg, true, Depth + 1) == true)
707 {
708 Bad = false;
709 if (List->IsFlag(Pkg,pkgOrderList::Loop) == false)
710 Changed = true;
711 break;
712 }
713 }
714 else
715 {
716 if (Debug)
717 clog << OutputInDepth(Depth) << "Trying to SmartConfigure " << Pkg.FullName() << endl;
718 if (SmartConfigure(Pkg, Depth + 1) == true)
719 {
720 Bad = false;
721 if (List->IsFlag(Pkg,pkgOrderList::Loop) == false)
722 Changed = true;
723 break;
724 }
725 }
726 }
727 }
728
729 if (Bad == true)
730 {
731 if (Start == End)
732 return _error->Error("Couldn't configure pre-depend %s for %s, "
733 "probably a dependency cycle.",
734 End.TargetPkg().FullName().c_str(),Pkg.FullName().c_str());
735 }
736 else
737 continue;
738 }
739 else if (End->Type == pkgCache::Dep::Conflicts ||
740 End->Type == pkgCache::Dep::Obsoletes)
741 {
742 /* Look for conflicts. Two packages that are both in the install
743 state cannot conflict so we don't check.. */
744 SPtrArray<Version *> VList = End.AllTargets();
745 for (Version **I = VList; *I != 0; I++)
746 {
747 VerIterator Ver(Cache,*I);
748 PkgIterator ConflictPkg = Ver.ParentPkg();
749 VerIterator InstallVer(Cache,Cache[ConflictPkg].InstallVer);
750
751 // See if the current version is conflicting
752 if (ConflictPkg.CurrentVer() == Ver && List->IsNow(ConflictPkg))
753 {
754 if (Debug)
755 clog << OutputInDepth(Depth) << Pkg.FullName() << " conflicts with " << ConflictPkg.FullName() << endl;
756 /* If a loop is not present or has not yet been detected, attempt to unpack packages
757 to resolve this conflict. If there is a loop present, remove packages to resolve this conflict */
758 if (List->IsFlag(ConflictPkg,pkgOrderList::Loop) == false)
759 {
760 if (Cache[ConflictPkg].Keep() == 0 && Cache[ConflictPkg].InstallVer != 0)
761 {
762 if (Debug)
763 clog << OutputInDepth(Depth) << OutputInDepth(Depth) << "Unpacking " << ConflictPkg.FullName() << " to prevent conflict" << endl;
764 List->Flag(Pkg,pkgOrderList::Loop);
765 if (SmartUnPack(ConflictPkg,false, Depth + 1) == true)
766 if (List->IsFlag(ConflictPkg,pkgOrderList::Loop) == false)
767 Changed = true;
768 // Remove loop to allow it to be used later if needed
769 List->RmFlag(Pkg,pkgOrderList::Loop);
770 }
771 else if (EarlyRemove(ConflictPkg) == false)
772 return _error->Error("Internal Error, Could not early remove %s (1)",ConflictPkg.FullName().c_str());
773 }
774 else if (List->IsFlag(ConflictPkg,pkgOrderList::Removed) == false)
775 {
776 if (Debug)
777 clog << OutputInDepth(Depth) << "Because of conficts knot, removing " << ConflictPkg.FullName() << " to conflict violation" << endl;
778 if (EarlyRemove(ConflictPkg) == false)
779 return _error->Error("Internal Error, Could not early remove %s (2)",ConflictPkg.FullName().c_str());
780 }
781 }
782 }
783 }
784 else if (End->Type == pkgCache::Dep::DpkgBreaks)
785 {
786 SPtrArray<Version *> VList = End.AllTargets();
787 for (Version **I = VList; *I != 0; ++I)
788 {
789 VerIterator Ver(Cache,*I);
790 PkgIterator BrokenPkg = Ver.ParentPkg();
791 if (BrokenPkg.CurrentVer() != Ver)
792 {
793 if (Debug)
794 std::clog << OutputInDepth(Depth) << " Ignore not-installed version " << Ver.VerStr() << " of " << Pkg.FullName() << " for " << End << std::endl;
795 continue;
796 }
797
798 // Check if it needs to be unpacked
799 if (List->IsFlag(BrokenPkg,pkgOrderList::InList) && Cache[BrokenPkg].Delete() == false &&
800 List->IsNow(BrokenPkg))
801 {
802 if (List->IsFlag(BrokenPkg,pkgOrderList::Loop) && PkgLoop)
803 {
804 // This dependency has already been dealt with by another SmartUnPack on Pkg
805 break;
806 }
807 else
808 {
809 // Found a break, so see if we can unpack the package to avoid it
810 // but do not set loop if another SmartUnPack already deals with it
811 // Also, avoid it if the package we would unpack pre-depends on this one
812 VerIterator InstallVer(Cache,Cache[BrokenPkg].InstallVer);
813 bool circle = false;
814 for (pkgCache::DepIterator D = InstallVer.DependsList(); D.end() == false; ++D)
815 {
816 if (D->Type != pkgCache::Dep::PreDepends)
817 continue;
818 SPtrArray<Version *> VL = D.AllTargets();
819 for (Version **I = VL; *I != 0; ++I)
820 {
821 VerIterator V(Cache,*I);
822 PkgIterator P = V.ParentPkg();
823 // we are checking for installation as an easy 'protection' against or-groups and (unchosen) providers
824 if (P != Pkg || (P.CurrentVer() != V && Cache[P].InstallVer != V))
825 continue;
826 circle = true;
827 break;
828 }
829 if (circle == true)
830 break;
831 }
832 if (circle == true)
833 {
834 if (Debug)
835 clog << OutputInDepth(Depth) << " Avoiding " << End << " avoided as " << BrokenPkg.FullName() << " has a pre-depends on " << Pkg.FullName() << std::endl;
836 continue;
837 }
838 else
839 {
840 if (Debug)
841 {
842 clog << OutputInDepth(Depth) << " Unpacking " << BrokenPkg.FullName() << " to avoid " << End;
843 if (PkgLoop == true)
844 clog << " (Looping)";
845 clog << std::endl;
846 }
847 if (PkgLoop == false)
848 List->Flag(Pkg,pkgOrderList::Loop);
849 if (SmartUnPack(BrokenPkg, false, Depth + 1) == true)
850 {
851 if (List->IsFlag(BrokenPkg,pkgOrderList::Loop) == false)
852 Changed = true;
853 }
854 if (PkgLoop == false)
855 List->RmFlag(Pkg,pkgOrderList::Loop);
856 }
857 }
858 }
859 // Check if a package needs to be removed
860 else if (Cache[BrokenPkg].Delete() == true && List->IsFlag(BrokenPkg,pkgOrderList::Configured) == false)
861 {
862 if (Debug)
863 clog << OutputInDepth(Depth) << " Removing " << BrokenPkg.FullName() << " to avoid " << End << endl;
864 SmartRemove(BrokenPkg);
865 }
866 }
867 }
868 }
869 if (i++ > max_loops)
870 return _error->Error("Internal error: APT::pkgPackageManager::MaxLoopCount reached in SmartConfigure for %s, aborting", Pkg.FullName().c_str());
871 } while (Changed == true);
872
873 // Check for reverse conflicts.
874 if (CheckRConflicts(Pkg,Pkg.RevDependsList(),
875 instVer.VerStr()) == false)
876 return false;
877
878 for (PrvIterator P = instVer.ProvidesList();
879 P.end() == false; ++P)
880 if (Pkg->Group != P.OwnerPkg()->Group)
881 CheckRConflicts(Pkg,P.ParentPkg().RevDependsList(),P.ProvideVersion());
882
883 if (PkgLoop)
884 return true;
885
886 List->Flag(Pkg,pkgOrderList::UnPacked,pkgOrderList::States);
887
888 if (Immediate == true && (instVer->MultiArch & pkgCache::Version::Same) == pkgCache::Version::Same)
889 {
890 /* Do lockstep M-A:same unpacking in two phases:
891 First unpack all installed architectures, then the not installed.
892 This way we avoid that M-A: enabled packages are installed before
893 their older non-M-A enabled packages are replaced by newer versions */
894 bool const installed = Pkg->CurrentVer != 0;
895 if (installed == true &&
896 (instVer != Pkg.CurrentVer() ||
897 ((Cache[Pkg].iFlags & pkgDepCache::ReInstall) == pkgDepCache::ReInstall)) &&
898 Install(Pkg,FileNames[Pkg->ID]) == false)
899 return false;
900 for (PkgIterator P = Pkg.Group().PackageList();
901 P.end() == false; P = Pkg.Group().NextPkg(P))
902 {
903 if (P->CurrentVer == 0 || P == Pkg || List->IsFlag(P,pkgOrderList::UnPacked) == true ||
904 Cache[P].InstallVer == 0 || (P.CurrentVer() == Cache[P].InstallVer &&
905 (Cache[Pkg].iFlags & pkgDepCache::ReInstall) != pkgDepCache::ReInstall))
906 continue;
907 if (SmartUnPack(P, false, Depth + 1) == false)
908 return false;
909 }
910 if (installed == false && Install(Pkg,FileNames[Pkg->ID]) == false)
911 return false;
912 for (PkgIterator P = Pkg.Group().PackageList();
913 P.end() == false; P = Pkg.Group().NextPkg(P))
914 {
915 if (P->CurrentVer != 0 || P == Pkg || List->IsFlag(P,pkgOrderList::UnPacked) == true ||
916 List->IsFlag(P,pkgOrderList::Configured) == true ||
917 Cache[P].InstallVer == 0 || (P.CurrentVer() == Cache[P].InstallVer &&
918 (Cache[Pkg].iFlags & pkgDepCache::ReInstall) != pkgDepCache::ReInstall))
919 continue;
920 if (SmartUnPack(P, false, Depth + 1) == false)
921 return false;
922 }
923 }
924 // packages which are already unpacked don't need to be unpacked again
925 else if ((instVer != Pkg.CurrentVer() ||
926 ((Cache[Pkg].iFlags & pkgDepCache::ReInstall) == pkgDepCache::ReInstall)) &&
927 Install(Pkg,FileNames[Pkg->ID]) == false)
928 return false;
929
930 if (Immediate == true) {
931 // Perform immedate configuration of the package.
932 if (SmartConfigure(Pkg, Depth + 1) == false)
933 _error->Warning(_("Could not perform immediate configuration on '%s'. "
934 "Please see man 5 apt.conf under APT::Immediate-Configure for details. (%d)"),Pkg.FullName().c_str(),2);
935 }
936
937 return true;
938 }
939 /*}}}*/
940 // PM::OrderInstall - Installation ordering routine /*{{{*/
941 // ---------------------------------------------------------------------
942 /* */
943 pkgPackageManager::OrderResult pkgPackageManager::OrderInstall()
944 {
945 if (CreateOrderList() == false)
946 return Failed;
947
948 Reset();
949
950 if (Debug == true)
951 clog << "Beginning to order" << endl;
952
953 bool const ordering =
954 _config->FindB("PackageManager::UnpackAll",true) ?
955 List->OrderUnpack(FileNames) : List->OrderCritical();
956 if (ordering == false)
957 {
958 _error->Error("Internal ordering error");
959 return Failed;
960 }
961
962 if (Debug == true)
963 clog << "Done ordering" << endl;
964
965 bool DoneSomething = false;
966 for (pkgOrderList::iterator I = List->begin(); I != List->end(); ++I)
967 {
968 PkgIterator Pkg(Cache,*I);
969
970 if (List->IsNow(Pkg) == false)
971 {
972 if (Debug == true)
973 clog << "Skipping already done " << Pkg.FullName() << endl;
974 continue;
975 }
976
977 if (List->IsMissing(Pkg) == true)
978 {
979 if (Debug == true)
980 clog << "Sequence completed at " << Pkg.FullName() << endl;
981 if (DoneSomething == false)
982 {
983 _error->Error("Internal Error, ordering was unable to handle the media swap");
984 return Failed;
985 }
986 return Incomplete;
987 }
988
989 // Sanity check
990 if (Cache[Pkg].Keep() == true &&
991 Pkg.State() == pkgCache::PkgIterator::NeedsNothing &&
992 (Cache[Pkg].iFlags & pkgDepCache::ReInstall) != pkgDepCache::ReInstall)
993 {
994 _error->Error("Internal Error, trying to manipulate a kept package (%s)",Pkg.FullName().c_str());
995 return Failed;
996 }
997
998 // Perform a delete or an install
999 if (Cache[Pkg].Delete() == true)
1000 {
1001 if (SmartRemove(Pkg) == false)
1002 return Failed;
1003 }
1004 else
1005 if (SmartUnPack(Pkg,List->IsFlag(Pkg,pkgOrderList::Immediate),0) == false)
1006 return Failed;
1007 DoneSomething = true;
1008
1009 if (ImmConfigureAll) {
1010 /* ConfigureAll here to pick up and packages left unconfigured because they were unpacked in the
1011 "PreUnpack Checks" section */
1012 if (!ConfigureAll())
1013 return Failed;
1014 }
1015 }
1016
1017 // Final run through the configure phase
1018 if (ConfigureAll() == false)
1019 return Failed;
1020
1021 // Sanity check
1022 for (pkgOrderList::iterator I = List->begin(); I != List->end(); ++I)
1023 {
1024 if (List->IsFlag(*I,pkgOrderList::Configured) == false)
1025 {
1026 _error->Error("Internal error, packages left unconfigured. %s",
1027 PkgIterator(Cache,*I).FullName().c_str());
1028 return Failed;
1029 }
1030 }
1031
1032 return Completed;
1033 }
1034 // PM::DoInstallPostFork - compat /*{{{*/
1035 // ---------------------------------------------------------------------
1036 /*}}}*/
1037 #if (APT_PKG_MAJOR >= 4 && APT_PKG_MINOR >= 13)
1038 pkgPackageManager::OrderResult
1039 pkgPackageManager::DoInstallPostFork(int statusFd)
1040 {
1041 APT::Progress::PackageManager *progress = new
1042 APT::Progress::PackageManagerProgressFd(statusFd);
1043 pkgPackageManager::OrderResult res = DoInstallPostFork(progress);
1044 delete progress;
1045 return res;
1046 }
1047 /*}}}*/
1048 // PM::DoInstallPostFork - Does install part that happens after the fork /*{{{*/
1049 // ---------------------------------------------------------------------
1050 pkgPackageManager::OrderResult
1051 pkgPackageManager::DoInstallPostFork(APT::Progress::PackageManager *progress)
1052 {
1053 bool goResult = Go(progress);
1054 if(goResult == false)
1055 return Failed;
1056
1057 return Res;
1058 };
1059 #else
1060 pkgPackageManager::OrderResult
1061 pkgPackageManager::DoInstallPostFork(int statusFd)
1062 {
1063 bool goResult = Go(statusFd);
1064 if(goResult == false)
1065 return Failed;
1066
1067 return Res;
1068 }
1069 #endif
1070 /*}}}*/
1071 // PM::DoInstall - Does the installation /*{{{*/
1072 // ---------------------------------------------------------------------
1073 /* compat */
1074 #if (APT_PKG_MAJOR >= 4 && APT_PKG_MINOR >= 13)
1075 pkgPackageManager::OrderResult
1076 pkgPackageManager::DoInstall(int statusFd)
1077 {
1078 APT::Progress::PackageManager *progress = new
1079 APT::Progress::PackageManagerProgressFd(statusFd);
1080 OrderResult res = DoInstall(progress);
1081 delete progress;
1082 return res;
1083 }
1084 #else
1085 pkgPackageManager::OrderResult pkgPackageManager::DoInstall(int statusFd)
1086 {
1087 if(DoInstallPreFork() == Failed)
1088 return Failed;
1089
1090 return DoInstallPostFork(statusFd);
1091 }
1092 #endif
1093 /*}}}*/
1094 // PM::DoInstall - Does the installation /*{{{*/
1095 // ---------------------------------------------------------------------
1096 /* This uses the filenames in FileNames and the information in the
1097 DepCache to perform the installation of packages.*/
1098 #if (APT_PKG_MAJOR >= 4 && APT_PKG_MINOR >= 13)
1099 pkgPackageManager::OrderResult
1100 pkgPackageManager::DoInstall(APT::Progress::PackageManager *progress)
1101 {
1102 if(DoInstallPreFork() == Failed)
1103 return Failed;
1104
1105 return DoInstallPostFork(progress);
1106 }
1107 #endif
1108 /*}}}*/