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