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