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