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