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