]> git.saurik.com Git - apt.git/blob - apt-pkg/packagemanager.cc
DropPrivs: Do not use an invalid return check for setgroups()
[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(), &D) == 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::NonLoopingSmart - helper to avoid loops while calling Smart methods /*{{{*/
318 // -----------------------------------------------------------------------
319 /* ensures that a loop of the form A depends B, B depends A (and similar)
320 is not leading us down into infinite recursion segfault land */
321 bool pkgPackageManager::NonLoopingSmart(SmartAction const action, pkgCache::PkgIterator &Pkg,
322 pkgCache::PkgIterator DepPkg, int const Depth, bool const PkgLoop,
323 bool * const Bad, bool * const Changed)
324 {
325 if (PkgLoop == false)
326 List->Flag(Pkg,pkgOrderList::Loop);
327 bool success = false;
328 switch(action)
329 {
330 case UNPACK_IMMEDIATE: success = SmartUnPack(DepPkg, true, Depth + 1); break;
331 case UNPACK: success = SmartUnPack(DepPkg, false, Depth + 1); break;
332 case CONFIGURE: success = SmartConfigure(DepPkg, Depth + 1); break;
333 }
334 if (PkgLoop == false)
335 List->RmFlag(Pkg,pkgOrderList::Loop);
336
337 if (success == false)
338 return false;
339
340 if (Bad != NULL)
341 *Bad = false;
342 if (Changed != NULL && List->IsFlag(DepPkg,pkgOrderList::Loop) == false)
343 *Changed = true;
344 return true;
345 }
346 /*}}}*/
347 // PM::SmartConfigure - Perform immediate configuration of the pkg /*{{{*/
348 // ---------------------------------------------------------------------
349 /* This function tries to put the system in a state where Pkg can be configured.
350 This involves checking each of Pkg's dependencies and unpacking and
351 configuring packages where needed. */
352 bool pkgPackageManager::SmartConfigure(PkgIterator Pkg, int const Depth)
353 {
354 // If this is true, only check and correct and dependencies without the Loop flag
355 bool const PkgLoop = List->IsFlag(Pkg,pkgOrderList::Loop);
356
357 if (Debug) {
358 VerIterator InstallVer = VerIterator(Cache,Cache[Pkg].InstallVer);
359 clog << OutputInDepth(Depth) << "SmartConfigure " << Pkg.FullName() << " (" << InstallVer.VerStr() << ")";
360 if (PkgLoop)
361 clog << " (Only Correct Dependencies)";
362 clog << endl;
363 }
364
365 VerIterator const instVer = Cache[Pkg].InstVerIter(Cache);
366
367 /* Because of the ordered list, most dependencies should be unpacked,
368 however if there is a loop (A depends on B, B depends on A) this will not
369 be the case, so check for dependencies before configuring. */
370 bool Bad = false, Changed = false;
371 const unsigned int max_loops = _config->FindI("APT::pkgPackageManager::MaxLoopCount", 5000);
372 unsigned int i=0;
373 std::list<DepIterator> needConfigure;
374 do
375 {
376 // Check each dependency and see if anything needs to be done
377 // so that it can be configured
378 Changed = false;
379 for (DepIterator D = instVer.DependsList(); D.end() == false; )
380 {
381 // Compute a single dependency element (glob or)
382 pkgCache::DepIterator Start, End;
383 D.GlobOr(Start,End);
384
385 if (End->Type != pkgCache::Dep::Depends)
386 continue;
387 Bad = true;
388
389 // the first pass checks if we its all good, i.e. if we have
390 // to do anything at all
391 for (DepIterator Cur = Start; true; ++Cur)
392 {
393 SPtrArray<Version *> VList = Cur.AllTargets();
394
395 for (Version **I = VList; *I != 0; ++I)
396 {
397 VerIterator Ver(Cache,*I);
398 PkgIterator DepPkg = Ver.ParentPkg();
399
400 // Check if the current version of the package is available and will satisfy this dependency
401 if (DepPkg.CurrentVer() == Ver && List->IsNow(DepPkg) == true &&
402 List->IsFlag(DepPkg,pkgOrderList::Removed) == false &&
403 DepPkg.State() == PkgIterator::NeedsNothing)
404 {
405 Bad = false;
406 break;
407 }
408
409 // Check if the version that is going to be installed will satisfy the dependency
410 if (Cache[DepPkg].InstallVer != *I || List->IsNow(DepPkg) == false)
411 continue;
412
413 if (PkgLoop == true)
414 {
415 if (Debug)
416 std::clog << OutputInDepth(Depth) << "Package " << Pkg << " loops in SmartConfigure" << std::endl;
417 Bad = false;
418 }
419 }
420
421 if (Cur == End || Bad == false)
422 break;
423 }
424
425 // this dependency is in a good state, so we can stop
426 if (Bad == false)
427 {
428 if (Debug)
429 std::clog << OutputInDepth(Depth) << "Found ok dep " << D.TargetPkg() << std::endl;
430 continue;
431 }
432
433 // Check for dependencies that have not been unpacked,
434 // probably due to loops.
435 for (DepIterator Cur = Start; true; ++Cur)
436 {
437 SPtrArray<Version *> VList = Cur.AllTargets();
438
439 for (Version **I = VList; *I != 0; ++I)
440 {
441 VerIterator Ver(Cache,*I);
442 PkgIterator DepPkg = Ver.ParentPkg();
443
444 // Check if the current version of the package is available and will satisfy this dependency
445 if (DepPkg.CurrentVer() == Ver && List->IsNow(DepPkg) == true &&
446 List->IsFlag(DepPkg,pkgOrderList::Removed) == false &&
447 DepPkg.State() == PkgIterator::NeedsNothing)
448 continue;
449
450 // Check if the version that is going to be installed will satisfy the dependency
451 if (Cache[DepPkg].InstallVer != *I || List->IsNow(DepPkg) == false)
452 continue;
453
454 if (PkgLoop == true)
455 {
456 if (Debug)
457 std::clog << OutputInDepth(Depth) << "Package " << Pkg << " loops in SmartConfigure" << std::endl;
458 Bad = false;
459 }
460 else
461 {
462 if (Debug)
463 clog << OutputInDepth(Depth) << "Unpacking " << DepPkg.FullName() << " to avoid loop " << Cur << endl;
464 if (NonLoopingSmart(UNPACK_IMMEDIATE, Pkg, DepPkg, Depth, PkgLoop, &Bad, &Changed) == false)
465 return false;
466 }
467 // at this point we either unpacked a Dep or we are in a loop,
468 // no need to unpack a second one
469 break;
470 }
471
472 if (Cur == End || Bad == false)
473 break;
474 }
475
476 if (Bad == false)
477 continue;
478
479 needConfigure.push_back(Start);
480 }
481 if (i++ > max_loops)
482 return _error->Error("Internal error: MaxLoopCount reached in SmartUnPack (1) for %s, aborting", Pkg.FullName().c_str());
483 } while (Changed == true);
484
485 // now go over anything that needs configuring
486 Bad = false, Changed = false, i = 0;
487 do
488 {
489 Changed = false;
490 for (std::list<DepIterator>::const_iterator D = needConfigure.begin(); D != needConfigure.end(); ++D)
491 {
492 // Compute a single dependency element (glob or) without modifying D
493 pkgCache::DepIterator Start, End;
494 {
495 pkgCache::DepIterator Discard = *D;
496 Discard.GlobOr(Start,End);
497 }
498
499 if (End->Type != pkgCache::Dep::Depends)
500 continue;
501 Bad = true;
502
503 // Search for dependencies which are unpacked but aren't configured yet (maybe loops)
504 for (DepIterator Cur = Start; true; ++Cur)
505 {
506 SPtrArray<Version *> VList = Cur.AllTargets();
507
508 for (Version **I = VList; *I != 0; ++I)
509 {
510 VerIterator Ver(Cache,*I);
511 PkgIterator DepPkg = Ver.ParentPkg();
512
513 // Check if the version that is going to be installed will satisfy the dependency
514 if (Cache[DepPkg].InstallVer != *I)
515 continue;
516
517 if (List->IsFlag(DepPkg,pkgOrderList::UnPacked))
518 {
519 if (List->IsFlag(DepPkg,pkgOrderList::Loop) && PkgLoop)
520 {
521 // This dependency has already been dealt with by another SmartConfigure on Pkg
522 Bad = false;
523 break;
524 }
525 if (Debug)
526 std::clog << OutputInDepth(Depth) << "Configure already unpacked " << DepPkg << std::endl;
527 if (NonLoopingSmart(CONFIGURE, Pkg, DepPkg, Depth, PkgLoop, &Bad, &Changed) == false)
528 return false;
529 break;
530
531 }
532 else if (List->IsFlag(DepPkg,pkgOrderList::Configured))
533 {
534 Bad = false;
535 break;
536 }
537 }
538 if (Cur == End || Bad == false)
539 break;
540 }
541
542
543 if (Bad == true && Changed == false && Debug == true)
544 std::clog << OutputInDepth(Depth) << "Could not satisfy " << *D << std::endl;
545 }
546 if (i++ > max_loops)
547 return _error->Error("Internal error: MaxLoopCount reached in SmartUnPack (2) for %s, aborting", Pkg.FullName().c_str());
548 } while (Changed == true);
549
550 if (Bad == true)
551 return _error->Error(_("Could not configure '%s'. "),Pkg.FullName().c_str());
552
553 if (PkgLoop) return true;
554
555 static std::string const conf = _config->Find("PackageManager::Configure","all");
556 static bool const ConfigurePkgs = (conf == "all" || conf == "smart");
557
558 if (List->IsFlag(Pkg,pkgOrderList::Configured))
559 return _error->Error("Internal configure error on '%s'.", Pkg.FullName().c_str());
560
561 if (ConfigurePkgs == true && Configure(Pkg) == false)
562 return false;
563
564 List->Flag(Pkg,pkgOrderList::Configured,pkgOrderList::States);
565
566 if ((Cache[Pkg].InstVerIter(Cache)->MultiArch & pkgCache::Version::Same) == pkgCache::Version::Same)
567 for (PkgIterator P = Pkg.Group().PackageList();
568 P.end() == false; P = Pkg.Group().NextPkg(P))
569 {
570 if (Pkg == P || List->IsFlag(P,pkgOrderList::Configured) == true ||
571 List->IsFlag(P,pkgOrderList::UnPacked) == false ||
572 Cache[P].InstallVer == 0 || (P.CurrentVer() == Cache[P].InstallVer &&
573 (Cache[Pkg].iFlags & pkgDepCache::ReInstall) != pkgDepCache::ReInstall))
574 continue;
575 if (SmartConfigure(P, (Depth +1)) == false)
576 return false;
577 }
578
579 // Sanity Check
580 if (List->IsFlag(Pkg,pkgOrderList::Configured) == false)
581 return _error->Error(_("Could not configure '%s'. "),Pkg.FullName().c_str());
582
583 return true;
584 }
585 /*}}}*/
586 // PM::EarlyRemove - Perform removal of packages before their time /*{{{*/
587 // ---------------------------------------------------------------------
588 /* This is called to deal with conflicts arising from unpacking */
589 bool pkgPackageManager::EarlyRemove(PkgIterator Pkg)
590 {
591 return EarlyRemove(Pkg, NULL);
592 }
593 bool pkgPackageManager::EarlyRemove(PkgIterator Pkg, DepIterator const * const Dep)
594 {
595 if (List->IsNow(Pkg) == false)
596 return true;
597
598 // Already removed it
599 if (List->IsFlag(Pkg,pkgOrderList::Removed) == true)
600 return true;
601
602 // Woops, it will not be re-installed!
603 if (List->IsFlag(Pkg,pkgOrderList::InList) == false)
604 return false;
605
606 // these breaks on M-A:same packages can be dealt with. They 'loop' by design
607 if (Dep != NULL && (*Dep)->Type == pkgCache::Dep::DpkgBreaks && Dep->IsMultiArchImplicit() == true)
608 return true;
609
610 // Essential packages get special treatment
611 bool IsEssential = false;
612 if ((Pkg->Flags & pkgCache::Flag::Essential) != 0 ||
613 (Pkg->Flags & pkgCache::Flag::Important) != 0)
614 IsEssential = true;
615
616 /* Check for packages that are the dependents of essential packages and
617 promote them too */
618 if (Pkg->CurrentVer != 0)
619 {
620 for (pkgCache::DepIterator D = Pkg.RevDependsList(); D.end() == false &&
621 IsEssential == false; ++D)
622 if (D->Type == pkgCache::Dep::Depends || D->Type == pkgCache::Dep::PreDepends)
623 if ((D.ParentPkg()->Flags & pkgCache::Flag::Essential) != 0 ||
624 (D.ParentPkg()->Flags & pkgCache::Flag::Important) != 0)
625 IsEssential = true;
626 }
627
628 if (IsEssential == true)
629 {
630 if (_config->FindB("APT::Force-LoopBreak",false) == false)
631 return _error->Error(_("This installation run will require temporarily "
632 "removing the essential package %s due to a "
633 "Conflicts/Pre-Depends loop. This is often bad, "
634 "but if you really want to do it, activate the "
635 "APT::Force-LoopBreak option."),Pkg.FullName().c_str());
636 }
637 // dpkg will auto-deconfigure it, no need for the big remove hammer
638 else if (Dep != NULL && (*Dep)->Type == pkgCache::Dep::DpkgBreaks)
639 return true;
640
641 bool Res = SmartRemove(Pkg);
642 if (Cache[Pkg].Delete() == false)
643 List->Flag(Pkg,pkgOrderList::Removed,pkgOrderList::States);
644
645 return Res;
646 }
647 /*}}}*/
648 // PM::SmartRemove - Removal Helper /*{{{*/
649 // ---------------------------------------------------------------------
650 /* */
651 bool pkgPackageManager::SmartRemove(PkgIterator Pkg)
652 {
653 if (List->IsNow(Pkg) == false)
654 return true;
655
656 List->Flag(Pkg,pkgOrderList::Configured,pkgOrderList::States);
657
658 return Remove(Pkg,(Cache[Pkg].iFlags & pkgDepCache::Purge) == pkgDepCache::Purge);
659 }
660 /*}}}*/
661 // PM::SmartUnPack - Install helper /*{{{*/
662 // ---------------------------------------------------------------------
663 /* This puts the system in a state where it can Unpack Pkg, if Pkg is already
664 unpacked, or when it has been unpacked, if Immediate==true it configures it. */
665 bool pkgPackageManager::SmartUnPack(PkgIterator Pkg)
666 {
667 return SmartUnPack(Pkg, true, 0);
668 }
669 bool pkgPackageManager::SmartUnPack(PkgIterator Pkg, bool const Immediate, int const Depth)
670 {
671 bool PkgLoop = List->IsFlag(Pkg,pkgOrderList::Loop);
672
673 if (Debug) {
674 clog << OutputInDepth(Depth) << "SmartUnPack " << Pkg.FullName();
675 VerIterator InstallVer = VerIterator(Cache,Cache[Pkg].InstallVer);
676 if (Pkg.CurrentVer() == 0)
677 clog << " (install version " << InstallVer.VerStr() << ")";
678 else
679 clog << " (replace version " << Pkg.CurrentVer().VerStr() << " with " << InstallVer.VerStr() << ")";
680 if (PkgLoop)
681 clog << " (Only Perform PreUnpack Checks)";
682 if (Immediate)
683 clog << " immediately";
684 clog << endl;
685 }
686
687 VerIterator const instVer = Cache[Pkg].InstVerIter(Cache);
688
689 /* PreUnpack Checks: This loop checks and attempts to rectify any problems that would prevent the package being unpacked.
690 It addresses: PreDepends, Conflicts, Obsoletes and Breaks (DpkgBreaks). Any resolutions that do not require it should
691 avoid configuration (calling SmartUnpack with Immediate=true), this is because when unpacking some packages with
692 complex dependency structures, trying to configure some packages while breaking the loops can complicate things.
693 This will be either dealt with if the package is configured as a dependency of Pkg (if and when Pkg is configured),
694 or by the ConfigureAll call at the end of the for loop in OrderInstall. */
695 bool SomethingBad = false, Changed = false;
696 bool couldBeTemporaryRemoved = Depth != 0 && List->IsFlag(Pkg,pkgOrderList::Removed) == false;
697 const unsigned int max_loops = _config->FindI("APT::pkgPackageManager::MaxLoopCount", 5000);
698 unsigned int i = 0;
699 do
700 {
701 Changed = false;
702 for (DepIterator D = instVer.DependsList(); D.end() == false; )
703 {
704 // Compute a single dependency element (glob or)
705 pkgCache::DepIterator Start, End;
706 D.GlobOr(Start,End);
707
708 if (End->Type == pkgCache::Dep::PreDepends)
709 {
710 bool Bad = true;
711 if (Debug)
712 clog << OutputInDepth(Depth) << "PreDepends order for " << Pkg.FullName() << std::endl;
713
714 // Look for easy targets: packages that are already okay
715 for (DepIterator Cur = Start; Bad == true; ++Cur)
716 {
717 SPtrArray<Version *> VList = Cur.AllTargets();
718 for (Version **I = VList; *I != 0; ++I)
719 {
720 VerIterator Ver(Cache,*I);
721 PkgIterator Pkg = Ver.ParentPkg();
722
723 // See if the current version is ok
724 if (Pkg.CurrentVer() == Ver && List->IsNow(Pkg) == true &&
725 Pkg.State() == PkgIterator::NeedsNothing)
726 {
727 Bad = false;
728 if (Debug)
729 clog << OutputInDepth(Depth) << "Found ok package " << Pkg.FullName() << endl;
730 break;
731 }
732 }
733 if (Cur == End)
734 break;
735 }
736
737 // Look for something that could be configured.
738 for (DepIterator Cur = Start; Bad == true && Cur.end() == false; ++Cur)
739 {
740 SPtrArray<Version *> VList = Cur.AllTargets();
741 for (Version **I = VList; *I != 0; ++I)
742 {
743 VerIterator Ver(Cache,*I);
744 PkgIterator DepPkg = Ver.ParentPkg();
745
746 // Not the install version
747 if (Cache[DepPkg].InstallVer != *I ||
748 (Cache[DepPkg].Keep() == true && DepPkg.State() == PkgIterator::NeedsNothing))
749 continue;
750
751 if (List->IsFlag(DepPkg,pkgOrderList::Configured))
752 {
753 Bad = false;
754 break;
755 }
756
757 // check if it needs unpack or if if configure is enough
758 if (List->IsFlag(DepPkg,pkgOrderList::UnPacked) == false)
759 {
760 if (Debug)
761 clog << OutputInDepth(Depth) << "Trying to SmartUnpack " << DepPkg.FullName() << endl;
762 if (NonLoopingSmart(UNPACK_IMMEDIATE, Pkg, DepPkg, Depth, PkgLoop, &Bad, &Changed) == false)
763 return false;
764 }
765 else
766 {
767 if (Debug)
768 clog << OutputInDepth(Depth) << "Trying to SmartConfigure " << DepPkg.FullName() << endl;
769 if (NonLoopingSmart(CONFIGURE, Pkg, DepPkg, Depth, PkgLoop, &Bad, &Changed) == false)
770 return false;
771 }
772 break;
773 }
774 }
775
776 if (Bad == true)
777 SomethingBad = true;
778 }
779 else if (End->Type == pkgCache::Dep::Conflicts ||
780 End->Type == pkgCache::Dep::Obsoletes ||
781 End->Type == pkgCache::Dep::DpkgBreaks)
782 {
783 SPtrArray<Version *> VList = End.AllTargets();
784 for (Version **I = VList; *I != 0; ++I)
785 {
786 VerIterator Ver(Cache,*I);
787 PkgIterator ConflictPkg = Ver.ParentPkg();
788 if (ConflictPkg.CurrentVer() != Ver)
789 {
790 if (Debug)
791 std::clog << OutputInDepth(Depth) << "Ignore not-installed version " << Ver.VerStr() << " of " << ConflictPkg.FullName() << " for " << End << std::endl;
792 continue;
793 }
794
795 if (List->IsNow(ConflictPkg) == false)
796 {
797 if (Debug)
798 std::clog << OutputInDepth(Depth) << "Ignore already dealt-with version " << Ver.VerStr() << " of " << ConflictPkg.FullName() << " for " << End << std::endl;
799 continue;
800 }
801
802 if (List->IsFlag(ConflictPkg,pkgOrderList::Removed) == true)
803 {
804 if (Debug)
805 clog << OutputInDepth(Depth) << "Ignoring " << End << " as " << ConflictPkg.FullName() << "was temporarily removed" << endl;
806 continue;
807 }
808
809 if (List->IsFlag(ConflictPkg,pkgOrderList::Loop) && PkgLoop)
810 {
811 if (End->Type == pkgCache::Dep::DpkgBreaks && End.IsMultiArchImplicit() == true)
812 {
813 if (Debug)
814 clog << OutputInDepth(Depth) << "Because dependency is MultiArchImplicit we ignored looping on: " << ConflictPkg << endl;
815 continue;
816 }
817 if (Debug)
818 {
819 if (End->Type == pkgCache::Dep::DpkgBreaks)
820 clog << OutputInDepth(Depth) << "Because of breaks knot, deconfigure " << ConflictPkg.FullName() << " temporarily" << endl;
821 else
822 clog << OutputInDepth(Depth) << "Because of conflict knot, removing " << ConflictPkg.FullName() << " temporarily" << endl;
823 }
824 if (EarlyRemove(ConflictPkg, &End) == false)
825 return _error->Error("Internal Error, Could not early remove %s (2)",ConflictPkg.FullName().c_str());
826 SomethingBad = true;
827 continue;
828 }
829
830 if (Cache[ConflictPkg].Delete() == false)
831 {
832 if (Debug)
833 {
834 clog << OutputInDepth(Depth) << "Unpacking " << ConflictPkg.FullName() << " to avoid " << End;
835 if (PkgLoop == true)
836 clog << " (Looping)";
837 clog << std::endl;
838 }
839 // we would like to avoid temporary removals and all that at best via a simple unpack
840 _error->PushToStack();
841 if (NonLoopingSmart(UNPACK, Pkg, ConflictPkg, Depth, PkgLoop, NULL, &Changed) == false)
842 {
843 // but if it fails ignore this failure and look for alternative ways of solving
844 if (Debug)
845 {
846 clog << OutputInDepth(Depth) << "Avoidance unpack of " << ConflictPkg.FullName() << " failed for " << End << std::endl;
847 _error->DumpErrors(std::clog);
848 }
849 _error->RevertToStack();
850 // ignorance can only happen if a) one of the offenders is already gone
851 if (List->IsFlag(ConflictPkg,pkgOrderList::Removed) == true)
852 {
853 if (Debug)
854 clog << OutputInDepth(Depth) << "But " << ConflictPkg.FullName() << " was temporarily removed in the meantime to satisfy " << End << endl;
855 }
856 else if (List->IsFlag(Pkg,pkgOrderList::Removed) == true)
857 {
858 if (Debug)
859 clog << OutputInDepth(Depth) << "But " << Pkg.FullName() << " was temporarily removed in the meantime to satisfy " << End << endl;
860 }
861 // or b) we can make one go (removal or dpkg auto-deconfigure)
862 else
863 {
864 if (Debug)
865 clog << OutputInDepth(Depth) << "So temprorary remove/deconfigure " << ConflictPkg.FullName() << " to satisfy " << End << endl;
866 if (EarlyRemove(ConflictPkg, &End) == false)
867 return _error->Error("Internal Error, Could not early remove %s (2)",ConflictPkg.FullName().c_str());
868 }
869 }
870 else
871 _error->MergeWithStack();
872 }
873 else
874 {
875 if (Debug)
876 clog << OutputInDepth(Depth) << "Removing " << ConflictPkg.FullName() << " now to avoid " << End << endl;
877 // no earlyremove() here as user has already agreed to the permanent removal
878 if (SmartRemove(Pkg) == false)
879 return _error->Error("Internal Error, Could not early remove %s (1)",ConflictPkg.FullName().c_str());
880 }
881 }
882 }
883 }
884 if (i++ > max_loops)
885 return _error->Error("Internal error: APT::pkgPackageManager::MaxLoopCount reached in SmartConfigure for %s, aborting", Pkg.FullName().c_str());
886 } while (Changed == true);
887
888 if (SomethingBad == true)
889 return _error->Error("Couldn't configure %s, probably a dependency cycle.", Pkg.FullName().c_str());
890
891 if (couldBeTemporaryRemoved == true && List->IsFlag(Pkg,pkgOrderList::Removed) == true)
892 {
893 if (Debug)
894 std::clog << OutputInDepth(Depth) << "Prevent unpack as " << Pkg << " is currently temporarily removed" << std::endl;
895 return true;
896 }
897
898 // Check for reverse conflicts.
899 if (CheckRConflicts(Pkg,Pkg.RevDependsList(),
900 instVer.VerStr()) == false)
901 return false;
902
903 for (PrvIterator P = instVer.ProvidesList();
904 P.end() == false; ++P)
905 if (Pkg->Group != P.OwnerPkg()->Group)
906 CheckRConflicts(Pkg,P.ParentPkg().RevDependsList(),P.ProvideVersion());
907
908 if (PkgLoop)
909 return true;
910
911 List->Flag(Pkg,pkgOrderList::UnPacked,pkgOrderList::States);
912
913 if (Immediate == true && (instVer->MultiArch & pkgCache::Version::Same) == pkgCache::Version::Same)
914 {
915 /* Do lockstep M-A:same unpacking in two phases:
916 First unpack all installed architectures, then the not installed.
917 This way we avoid that M-A: enabled packages are installed before
918 their older non-M-A enabled packages are replaced by newer versions */
919 bool const installed = Pkg->CurrentVer != 0;
920 if (installed == true &&
921 (instVer != Pkg.CurrentVer() ||
922 ((Cache[Pkg].iFlags & pkgDepCache::ReInstall) == pkgDepCache::ReInstall)) &&
923 Install(Pkg,FileNames[Pkg->ID]) == false)
924 return false;
925 for (PkgIterator P = Pkg.Group().PackageList();
926 P.end() == false; P = Pkg.Group().NextPkg(P))
927 {
928 if (P->CurrentVer == 0 || P == Pkg || List->IsFlag(P,pkgOrderList::UnPacked) == true ||
929 Cache[P].InstallVer == 0 || (P.CurrentVer() == Cache[P].InstallVer &&
930 (Cache[Pkg].iFlags & pkgDepCache::ReInstall) != pkgDepCache::ReInstall))
931 continue;
932 if (SmartUnPack(P, false, Depth + 1) == false)
933 return false;
934 }
935 if (installed == false && Install(Pkg,FileNames[Pkg->ID]) == false)
936 return false;
937 for (PkgIterator P = Pkg.Group().PackageList();
938 P.end() == false; P = Pkg.Group().NextPkg(P))
939 {
940 if (P->CurrentVer != 0 || P == Pkg || List->IsFlag(P,pkgOrderList::UnPacked) == true ||
941 List->IsFlag(P,pkgOrderList::Configured) == true ||
942 Cache[P].InstallVer == 0 || (P.CurrentVer() == Cache[P].InstallVer &&
943 (Cache[Pkg].iFlags & pkgDepCache::ReInstall) != pkgDepCache::ReInstall))
944 continue;
945 if (SmartUnPack(P, false, Depth + 1) == false)
946 return false;
947 }
948 }
949 // packages which are already unpacked don't need to be unpacked again
950 else if ((instVer != Pkg.CurrentVer() ||
951 ((Cache[Pkg].iFlags & pkgDepCache::ReInstall) == pkgDepCache::ReInstall)) &&
952 Install(Pkg,FileNames[Pkg->ID]) == false)
953 return false;
954
955 if (Immediate == true) {
956 // Perform immedate configuration of the package.
957 if (SmartConfigure(Pkg, Depth + 1) == false)
958 _error->Error(_("Could not perform immediate configuration on '%s'. "
959 "Please see man 5 apt.conf under APT::Immediate-Configure for details. (%d)"),Pkg.FullName().c_str(),2);
960 }
961
962 return true;
963 }
964 /*}}}*/
965 // PM::OrderInstall - Installation ordering routine /*{{{*/
966 // ---------------------------------------------------------------------
967 /* */
968 pkgPackageManager::OrderResult pkgPackageManager::OrderInstall()
969 {
970 if (CreateOrderList() == false)
971 return Failed;
972
973 Reset();
974
975 if (Debug == true)
976 clog << "Beginning to order" << endl;
977
978 bool const ordering =
979 _config->FindB("PackageManager::UnpackAll",true) ?
980 List->OrderUnpack(FileNames) : List->OrderCritical();
981 if (ordering == false)
982 {
983 _error->Error("Internal ordering error");
984 return Failed;
985 }
986
987 if (Debug == true)
988 clog << "Done ordering" << endl;
989
990 bool DoneSomething = false;
991 for (pkgOrderList::iterator I = List->begin(); I != List->end(); ++I)
992 {
993 PkgIterator Pkg(Cache,*I);
994
995 if (List->IsNow(Pkg) == false)
996 {
997 if (Debug == true)
998 clog << "Skipping already done " << Pkg.FullName() << endl;
999 continue;
1000 }
1001
1002 if (List->IsMissing(Pkg) == true)
1003 {
1004 if (Debug == true)
1005 clog << "Sequence completed at " << Pkg.FullName() << endl;
1006 if (DoneSomething == false)
1007 {
1008 _error->Error("Internal Error, ordering was unable to handle the media swap");
1009 return Failed;
1010 }
1011 return Incomplete;
1012 }
1013
1014 // Sanity check
1015 if (Cache[Pkg].Keep() == true &&
1016 Pkg.State() == pkgCache::PkgIterator::NeedsNothing &&
1017 (Cache[Pkg].iFlags & pkgDepCache::ReInstall) != pkgDepCache::ReInstall)
1018 {
1019 _error->Error("Internal Error, trying to manipulate a kept package (%s)",Pkg.FullName().c_str());
1020 return Failed;
1021 }
1022
1023 // Perform a delete or an install
1024 if (Cache[Pkg].Delete() == true)
1025 {
1026 if (SmartRemove(Pkg) == false)
1027 return Failed;
1028 }
1029 else
1030 if (SmartUnPack(Pkg,List->IsFlag(Pkg,pkgOrderList::Immediate),0) == false)
1031 return Failed;
1032 DoneSomething = true;
1033
1034 if (ImmConfigureAll) {
1035 /* ConfigureAll here to pick up and packages left unconfigured because they were unpacked in the
1036 "PreUnpack Checks" section */
1037 if (!ConfigureAll())
1038 return Failed;
1039 }
1040 }
1041
1042 // Final run through the configure phase
1043 if (ConfigureAll() == false)
1044 return Failed;
1045
1046 // Sanity check
1047 for (pkgOrderList::iterator I = List->begin(); I != List->end(); ++I)
1048 {
1049 if (List->IsFlag(*I,pkgOrderList::Configured) == false)
1050 {
1051 _error->Error("Internal error, packages left unconfigured. %s",
1052 PkgIterator(Cache,*I).FullName().c_str());
1053 return Failed;
1054 }
1055 }
1056
1057 return Completed;
1058 }
1059 // PM::DoInstallPostFork - compat /*{{{*/
1060 // ---------------------------------------------------------------------
1061 /*}}}*/
1062 #if (APT_PKG_MAJOR >= 4 && APT_PKG_MINOR >= 13)
1063 pkgPackageManager::OrderResult
1064 pkgPackageManager::DoInstallPostFork(int statusFd)
1065 {
1066 APT::Progress::PackageManager *progress = new
1067 APT::Progress::PackageManagerProgressFd(statusFd);
1068 pkgPackageManager::OrderResult res = DoInstallPostFork(progress);
1069 delete progress;
1070 return res;
1071 }
1072 /*}}}*/
1073 // PM::DoInstallPostFork - Does install part that happens after the fork /*{{{*/
1074 // ---------------------------------------------------------------------
1075 pkgPackageManager::OrderResult
1076 pkgPackageManager::DoInstallPostFork(APT::Progress::PackageManager *progress)
1077 {
1078 bool goResult = Go(progress);
1079 if(goResult == false)
1080 return Failed;
1081
1082 return Res;
1083 };
1084 #else
1085 pkgPackageManager::OrderResult
1086 pkgPackageManager::DoInstallPostFork(int statusFd)
1087 {
1088 bool goResult = Go(statusFd);
1089 if(goResult == false)
1090 return Failed;
1091
1092 return Res;
1093 }
1094 #endif
1095 /*}}}*/
1096 // PM::DoInstall - Does the installation /*{{{*/
1097 // ---------------------------------------------------------------------
1098 /* compat */
1099 #if (APT_PKG_MAJOR >= 4 && APT_PKG_MINOR >= 13)
1100 pkgPackageManager::OrderResult
1101 pkgPackageManager::DoInstall(int statusFd)
1102 {
1103 APT::Progress::PackageManager *progress = new
1104 APT::Progress::PackageManagerProgressFd(statusFd);
1105 OrderResult res = DoInstall(progress);
1106 delete progress;
1107 return res;
1108 }
1109 #else
1110 pkgPackageManager::OrderResult pkgPackageManager::DoInstall(int statusFd)
1111 {
1112 if(DoInstallPreFork() == Failed)
1113 return Failed;
1114
1115 return DoInstallPostFork(statusFd);
1116 }
1117 #endif
1118 /*}}}*/
1119 // PM::DoInstall - Does the installation /*{{{*/
1120 // ---------------------------------------------------------------------
1121 /* This uses the filenames in FileNames and the information in the
1122 DepCache to perform the installation of packages.*/
1123 #if (APT_PKG_MAJOR >= 4 && APT_PKG_MINOR >= 13)
1124 pkgPackageManager::OrderResult
1125 pkgPackageManager::DoInstall(APT::Progress::PackageManager *progress)
1126 {
1127 if(DoInstallPreFork() == Failed)
1128 return Failed;
1129
1130 return DoInstallPostFork(progress);
1131 }
1132 #endif
1133 /*}}}*/