]> git.saurik.com Git - apt.git/blame_incremental - apt-pkg/packagemanager.cc
Reverted 'bashisms' commit, it was wrong as $(...) is not a bashism.
[apt.git] / apt-pkg / packagemanager.cc
... / ...
CommitLineData
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 <apt-pkg/packagemanager.h>
17#include <apt-pkg/orderlist.h>
18#include <apt-pkg/depcache.h>
19#include <apt-pkg/error.h>
20#include <apt-pkg/version.h>
21#include <apt-pkg/acquire-item.h>
22#include <apt-pkg/algorithms.h>
23#include <apt-pkg/configuration.h>
24#include <apt-pkg/sptr.h>
25
26#include <apti18n.h>
27#include <iostream>
28#include <fcntl.h>
29
30using namespace std;
31
32// PM::PackageManager - Constructor /*{{{*/
33// ---------------------------------------------------------------------
34/* */
35pkgPackageManager::pkgPackageManager(pkgDepCache *pCache) : Cache(*pCache)
36{
37 FileNames = new string[Cache.Head().PackageCount];
38 List = 0;
39 Debug = _config->FindB("Debug::pkgPackageManager",false);
40}
41 /*}}}*/
42// PM::PackageManager - Destructor /*{{{*/
43// ---------------------------------------------------------------------
44/* */
45pkgPackageManager::~pkgPackageManager()
46{
47 delete List;
48 delete [] FileNames;
49}
50 /*}}}*/
51// PM::GetArchives - Queue the archives for download /*{{{*/
52// ---------------------------------------------------------------------
53/* */
54bool pkgPackageManager::GetArchives(pkgAcquire *Owner,pkgSourceList *Sources,
55 pkgRecords *Recs)
56{
57 if (CreateOrderList() == false)
58 return false;
59
60 if (List->OrderUnpack() == false)
61 return _error->Error("Internal ordering error");
62
63 for (pkgOrderList::iterator I = List->begin(); I != List->end(); I++)
64 {
65 PkgIterator Pkg(Cache,*I);
66 FileNames[Pkg->ID] = string();
67
68 // Skip packages to erase
69 if (Cache[Pkg].Delete() == true)
70 continue;
71
72 // Skip Packages that need configure only.
73 if (Pkg.State() == pkgCache::PkgIterator::NeedsConfigure &&
74 Cache[Pkg].Keep() == true)
75 continue;
76
77 // Skip already processed packages
78 if (List->IsNow(Pkg) == false)
79 continue;
80
81 new pkgAcqArchive(Owner,Sources,Recs,Cache[Pkg].InstVerIter(Cache),
82 FileNames[Pkg->ID]);
83 }
84
85 return true;
86}
87 /*}}}*/
88// PM::FixMissing - Keep all missing packages /*{{{*/
89// ---------------------------------------------------------------------
90/* This is called to correct the installation when packages could not
91 be downloaded. */
92bool pkgPackageManager::FixMissing()
93{
94 pkgDepCache::ActionGroup group(Cache);
95 pkgProblemResolver Resolve(&Cache);
96 List->SetFileList(FileNames);
97
98 bool Bad = false;
99 for (PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
100 {
101 if (List->IsMissing(I) == false)
102 continue;
103
104 // Okay, this file is missing and we need it. Mark it for keep
105 Bad = true;
106 Cache.MarkKeep(I, false, false);
107 }
108
109 // We have to empty the list otherwise it will not have the new changes
110 delete List;
111 List = 0;
112
113 if (Bad == false)
114 return true;
115
116 // Now downgrade everything that is broken
117 return Resolve.ResolveByKeep() == true && Cache.BrokenCount() == 0;
118}
119 /*}}}*/
120
121// PM::ImmediateAdd - Add the immediate flag recursivly /*{{{*/
122// ---------------------------------------------------------------------
123/* This adds the immediate flag to the pkg and recursively to the
124 dependendies
125 */
126void pkgPackageManager::ImmediateAdd(PkgIterator I, bool UseInstallVer)
127{
128 DepIterator D;
129
130 if(UseInstallVer)
131 {
132 if(Cache[I].InstallVer == 0)
133 return;
134 D = Cache[I].InstVerIter(Cache).DependsList();
135 } else {
136 if (I->CurrentVer == 0)
137 return;
138 D = I.CurrentVer().DependsList();
139 }
140
141 for ( /* nothing */ ; D.end() == false; D++)
142 if (D->Type == pkgCache::Dep::Depends || D->Type == pkgCache::Dep::PreDepends)
143 {
144 if(!List->IsFlag(D.TargetPkg(), pkgOrderList::Immediate))
145 {
146 if(Debug)
147 clog << "ImmediateAdd(): Adding Immediate flag to " << I.Name() << endl;
148 List->Flag(D.TargetPkg(),pkgOrderList::Immediate);
149 ImmediateAdd(D.TargetPkg(), UseInstallVer);
150 }
151 }
152 return;
153}
154 /*}}}*/
155
156// PM::CreateOrderList - Create the ordering class /*{{{*/
157// ---------------------------------------------------------------------
158/* This populates the ordering list with all the packages that are
159 going to change. */
160bool pkgPackageManager::CreateOrderList()
161{
162 if (List != 0)
163 return true;
164
165 delete List;
166 List = new pkgOrderList(&Cache);
167
168 bool NoImmConfigure = !_config->FindB("APT::Immediate-Configure",true);
169
170 // Generate the list of affected packages and sort it
171 for (PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
172 {
173 // Ignore no-version packages
174 if (I->VersionList == 0)
175 continue;
176
177 // Mark the package and its dependends for immediate configuration
178 if (((I->Flags & pkgCache::Flag::Essential) == pkgCache::Flag::Essential ||
179 (I->Flags & pkgCache::Flag::Important) == pkgCache::Flag::Important) &&
180 NoImmConfigure == false)
181 {
182 if(Debug)
183 clog << "CreateOrderList(): Adding Immediate flag for " << I.Name() << endl;
184 List->Flag(I,pkgOrderList::Immediate);
185
186 // Look for other install packages to make immediate configurea
187 ImmediateAdd(I, true);
188
189 // And again with the current version.
190 ImmediateAdd(I, false);
191 }
192
193 // Not interesting
194 if ((Cache[I].Keep() == true ||
195 Cache[I].InstVerIter(Cache) == I.CurrentVer()) &&
196 I.State() == pkgCache::PkgIterator::NeedsNothing &&
197 (Cache[I].iFlags & pkgDepCache::ReInstall) != pkgDepCache::ReInstall &&
198 (I.Purge() != false || Cache[I].Mode != pkgDepCache::ModeDelete ||
199 (Cache[I].iFlags & pkgDepCache::Purge) != pkgDepCache::Purge))
200 continue;
201
202 // Append it to the list
203 List->push_back(I);
204 }
205
206 return true;
207}
208 /*}}}*/
209// PM::DepAlwaysTrue - Returns true if this dep is irrelevent /*{{{*/
210// ---------------------------------------------------------------------
211/* The restriction on provides is to eliminate the case when provides
212 are transitioning between valid states [ie exim to smail] */
213bool pkgPackageManager::DepAlwaysTrue(DepIterator D)
214{
215 if (D.TargetPkg()->ProvidesList != 0)
216 return false;
217
218 if ((Cache[D] & pkgDepCache::DepInstall) != 0 &&
219 (Cache[D] & pkgDepCache::DepNow) != 0)
220 return true;
221 return false;
222}
223 /*}}}*/
224// PM::CheckRConflicts - Look for reverse conflicts /*{{{*/
225// ---------------------------------------------------------------------
226/* This looks over the reverses for a conflicts line that needs early
227 removal. */
228bool pkgPackageManager::CheckRConflicts(PkgIterator Pkg,DepIterator D,
229 const char *Ver)
230{
231 for (;D.end() == false; D++)
232 {
233 if (D->Type != pkgCache::Dep::Conflicts &&
234 D->Type != pkgCache::Dep::Obsoletes)
235 continue;
236
237 // The package hasnt been changed
238 if (List->IsNow(Pkg) == false)
239 continue;
240
241 // Ignore self conflicts, ignore conflicts from irrelevent versions
242 if (D.ParentPkg() == Pkg || D.ParentVer() != D.ParentPkg().CurrentVer())
243 continue;
244
245 if (Cache.VS().CheckDep(Ver,D->CompareOp,D.TargetVer()) == false)
246 continue;
247
248 if (EarlyRemove(D.ParentPkg()) == false)
249 return _error->Error("Reverse conflicts early remove for package '%s' failed",
250 Pkg.Name());
251 }
252 return true;
253}
254 /*}}}*/
255// PM::ConfigureAll - Run the all out configuration /*{{{*/
256// ---------------------------------------------------------------------
257/* This configures every package. It is assumed they are all unpacked and
258 that the final configuration is valid. */
259bool pkgPackageManager::ConfigureAll()
260{
261 pkgOrderList OList(&Cache);
262
263 // Populate the order list
264 for (pkgOrderList::iterator I = List->begin(); I != List->end(); I++)
265 if (List->IsFlag(pkgCache::PkgIterator(Cache,*I),
266 pkgOrderList::UnPacked) == true)
267 OList.push_back(*I);
268
269 if (OList.OrderConfigure() == false)
270 return false;
271
272 // Perform the configuring
273 for (pkgOrderList::iterator I = OList.begin(); I != OList.end(); I++)
274 {
275 PkgIterator Pkg(Cache,*I);
276
277 if (Configure(Pkg) == false)
278 return false;
279
280 List->Flag(Pkg,pkgOrderList::Configured,pkgOrderList::States);
281 }
282
283 return true;
284}
285 /*}}}*/
286// PM::SmartConfigure - Perform immediate configuration of the pkg /*{{{*/
287// ---------------------------------------------------------------------
288/* This routine scheduals the configuration of the given package and all
289 of it's dependents. */
290bool pkgPackageManager::SmartConfigure(PkgIterator Pkg)
291{
292 pkgOrderList OList(&Cache);
293
294 if (DepAdd(OList,Pkg) == false)
295 return false;
296
297 if (OList.OrderConfigure() == false)
298 return false;
299
300 // Perform the configuring
301 for (pkgOrderList::iterator I = OList.begin(); I != OList.end(); I++)
302 {
303 PkgIterator Pkg(Cache,*I);
304
305 if (Configure(Pkg) == false)
306 return false;
307
308 List->Flag(Pkg,pkgOrderList::Configured,pkgOrderList::States);
309 }
310
311 // Sanity Check
312 if (List->IsFlag(Pkg,pkgOrderList::Configured) == false)
313 return _error->Error("Internal error, could not immediate configure %s",Pkg.Name());
314
315 return true;
316}
317 /*}}}*/
318// PM::DepAdd - Add all dependents to the oder list /*{{{*/
319// ---------------------------------------------------------------------
320/* This recursively adds all dependents to the order list */
321bool pkgPackageManager::DepAdd(pkgOrderList &OList,PkgIterator Pkg,int Depth)
322{
323 if (OList.IsFlag(Pkg,pkgOrderList::Added) == true)
324 return true;
325 if (List->IsFlag(Pkg,pkgOrderList::Configured) == true)
326 return true;
327 if (List->IsFlag(Pkg,pkgOrderList::UnPacked) == false)
328 return false;
329
330 // Put the package on the list
331 OList.push_back(Pkg);
332 OList.Flag(Pkg,pkgOrderList::Added);
333 Depth++;
334
335 // Check the dependencies to see if they are all satisfied.
336 bool Bad = false;
337 for (DepIterator D = Cache[Pkg].InstVerIter(Cache).DependsList(); D.end() == false;)
338 {
339 if (D->Type != pkgCache::Dep::Depends && D->Type != pkgCache::Dep::PreDepends)
340 {
341 D++;
342 continue;
343 }
344
345 // Grok or groups
346 Bad = true;
347 for (bool LastOR = true; D.end() == false && LastOR == true; D++)
348 {
349 LastOR = (D->CompareOp & pkgCache::Dep::Or) == pkgCache::Dep::Or;
350
351 if (Bad == false)
352 continue;
353
354 SPtrArray<Version *> VList = D.AllTargets();
355 for (Version **I = VList; *I != 0 && Bad == true; I++)
356 {
357 VerIterator Ver(Cache,*I);
358 PkgIterator Pkg = Ver.ParentPkg();
359
360 // See if the current version is ok
361 if (Pkg.CurrentVer() == Ver && List->IsNow(Pkg) == true &&
362 Pkg.State() == PkgIterator::NeedsNothing)
363 {
364 Bad = false;
365 continue;
366 }
367
368 // Not the install version
369 if (Cache[Pkg].InstallVer != *I ||
370 (Cache[Pkg].Keep() == true && Pkg.State() == PkgIterator::NeedsNothing))
371 continue;
372
373 if (List->IsFlag(Pkg,pkgOrderList::UnPacked) == true)
374 Bad = !DepAdd(OList,Pkg,Depth);
375 if (List->IsFlag(Pkg,pkgOrderList::Configured) == true)
376 Bad = false;
377 }
378 }
379
380 if (Bad == true)
381 {
382 OList.Flag(Pkg,0,pkgOrderList::Added);
383 OList.pop_back();
384 Depth--;
385 return false;
386 }
387 }
388
389 Depth--;
390 return true;
391}
392 /*}}}*/
393// PM::EarlyRemove - Perform removal of packages before their time /*{{{*/
394// ---------------------------------------------------------------------
395/* This is called to deal with conflicts arising from unpacking */
396bool pkgPackageManager::EarlyRemove(PkgIterator Pkg)
397{
398 if (List->IsNow(Pkg) == false)
399 return true;
400
401 // Already removed it
402 if (List->IsFlag(Pkg,pkgOrderList::Removed) == true)
403 return true;
404
405 // Woops, it will not be re-installed!
406 if (List->IsFlag(Pkg,pkgOrderList::InList) == false)
407 return false;
408
409 // Essential packages get special treatment
410 bool IsEssential = false;
411 if ((Pkg->Flags & pkgCache::Flag::Essential) != 0)
412 IsEssential = true;
413
414 /* Check for packages that are the dependents of essential packages and
415 promote them too */
416 if (Pkg->CurrentVer != 0)
417 {
418 for (DepIterator D = Pkg.RevDependsList(); D.end() == false &&
419 IsEssential == false; D++)
420 if (D->Type == pkgCache::Dep::Depends || D->Type == pkgCache::Dep::PreDepends)
421 if ((D.ParentPkg()->Flags & pkgCache::Flag::Essential) != 0)
422 IsEssential = true;
423 }
424
425 if (IsEssential == true)
426 {
427 if (_config->FindB("APT::Force-LoopBreak",false) == false)
428 return _error->Error(_("This installation run will require temporarily "
429 "removing the essential package %s due to a "
430 "Conflicts/Pre-Depends loop. This is often bad, "
431 "but if you really want to do it, activate the "
432 "APT::Force-LoopBreak option."),Pkg.Name());
433 }
434
435 bool Res = SmartRemove(Pkg);
436 if (Cache[Pkg].Delete() == false)
437 List->Flag(Pkg,pkgOrderList::Removed,pkgOrderList::States);
438
439 return Res;
440}
441 /*}}}*/
442// PM::SmartRemove - Removal Helper /*{{{*/
443// ---------------------------------------------------------------------
444/* */
445bool pkgPackageManager::SmartRemove(PkgIterator Pkg)
446{
447 if (List->IsNow(Pkg) == false)
448 return true;
449
450 List->Flag(Pkg,pkgOrderList::Configured,pkgOrderList::States);
451 return Remove(Pkg,(Cache[Pkg].iFlags & pkgDepCache::Purge) == pkgDepCache::Purge);
452}
453 /*}}}*/
454// PM::SmartUnPack - Install helper /*{{{*/
455// ---------------------------------------------------------------------
456/* This performs the task of handling pre-depends. */
457bool pkgPackageManager::SmartUnPack(PkgIterator Pkg)
458{
459 // Check if it is already unpacked
460 if (Pkg.State() == pkgCache::PkgIterator::NeedsConfigure &&
461 Cache[Pkg].Keep() == true)
462 {
463 List->Flag(Pkg,pkgOrderList::UnPacked,pkgOrderList::States);
464 if (List->IsFlag(Pkg,pkgOrderList::Immediate) == true)
465 if (SmartConfigure(Pkg) == false)
466 return _error->Error("Internal Error, Could not perform immediate configuration (1) on %s",Pkg.Name());
467 return true;
468 }
469
470 /* See if this packages install version has any predependencies
471 that are not met by 'now' packages. */
472 for (DepIterator D = Cache[Pkg].InstVerIter(Cache).DependsList();
473 D.end() == false; )
474 {
475 // Compute a single dependency element (glob or)
476 pkgCache::DepIterator Start;
477 pkgCache::DepIterator End;
478 D.GlobOr(Start,End);
479
480 while (End->Type == pkgCache::Dep::PreDepends)
481 {
482 // Look for possible ok targets.
483 SPtrArray<Version *> VList = Start.AllTargets();
484 bool Bad = true;
485 for (Version **I = VList; *I != 0 && Bad == true; I++)
486 {
487 VerIterator Ver(Cache,*I);
488 PkgIterator Pkg = Ver.ParentPkg();
489
490 // See if the current version is ok
491 if (Pkg.CurrentVer() == Ver && List->IsNow(Pkg) == true &&
492 Pkg.State() == PkgIterator::NeedsNothing)
493 {
494 Bad = false;
495 continue;
496 }
497 }
498
499 // Look for something that could be configured.
500 for (Version **I = VList; *I != 0 && Bad == true; I++)
501 {
502 VerIterator Ver(Cache,*I);
503 PkgIterator Pkg = Ver.ParentPkg();
504
505 // Not the install version
506 if (Cache[Pkg].InstallVer != *I ||
507 (Cache[Pkg].Keep() == true && Pkg.State() == PkgIterator::NeedsNothing))
508 continue;
509
510 Bad = !SmartConfigure(Pkg);
511 }
512
513 /* If this or element did not match then continue on to the
514 next or element until a matching element is found */
515 if (Bad == true)
516 {
517 // This triggers if someone make a pre-depends/depend loop.
518 if (Start == End)
519 return _error->Error("Couldn't configure pre-depend %s for %s, "
520 "probably a dependency cycle.",
521 End.TargetPkg().Name(),Pkg.Name());
522 Start++;
523 }
524 else
525 break;
526 }
527
528 if (End->Type == pkgCache::Dep::Conflicts ||
529 End->Type == pkgCache::Dep::Obsoletes)
530 {
531 /* Look for conflicts. Two packages that are both in the install
532 state cannot conflict so we don't check.. */
533 SPtrArray<Version *> VList = End.AllTargets();
534 for (Version **I = VList; *I != 0; I++)
535 {
536 VerIterator Ver(Cache,*I);
537 PkgIterator Pkg = Ver.ParentPkg();
538
539 // See if the current version is conflicting
540 if (Pkg.CurrentVer() == Ver && List->IsNow(Pkg) == true)
541 {
542 if (EarlyRemove(Pkg) == false)
543 return _error->Error("Internal Error, Could not early remove %s",Pkg.Name());
544 }
545 }
546 }
547 }
548
549 // Check for reverse conflicts.
550 if (CheckRConflicts(Pkg,Pkg.RevDependsList(),
551 Cache[Pkg].InstVerIter(Cache).VerStr()) == false)
552 return false;
553
554 for (PrvIterator P = Cache[Pkg].InstVerIter(Cache).ProvidesList();
555 P.end() == false; P++)
556 CheckRConflicts(Pkg,P.ParentPkg().RevDependsList(),P.ProvideVersion());
557
558 if (Install(Pkg,FileNames[Pkg->ID]) == false)
559 return false;
560
561 List->Flag(Pkg,pkgOrderList::UnPacked,pkgOrderList::States);
562
563 // Perform immedate configuration of the package.
564 if (List->IsFlag(Pkg,pkgOrderList::Immediate) == true)
565 if (SmartConfigure(Pkg) == false)
566 return _error->Error("Internal Error, Could not perform immediate configuration (2) on %s",Pkg.Name());
567
568 return true;
569}
570 /*}}}*/
571// PM::OrderInstall - Installation ordering routine /*{{{*/
572// ---------------------------------------------------------------------
573/* */
574pkgPackageManager::OrderResult pkgPackageManager::OrderInstall()
575{
576 if (CreateOrderList() == false)
577 return Failed;
578
579 Reset();
580
581 if (Debug == true)
582 clog << "Begining to order" << endl;
583
584 if (List->OrderUnpack(FileNames) == false)
585 {
586 _error->Error("Internal ordering error");
587 return Failed;
588 }
589
590 if (Debug == true)
591 clog << "Done ordering" << endl;
592
593 bool DoneSomething = false;
594 for (pkgOrderList::iterator I = List->begin(); I != List->end(); I++)
595 {
596 PkgIterator Pkg(Cache,*I);
597
598 if (List->IsNow(Pkg) == false)
599 {
600 if (Debug == true)
601 clog << "Skipping already done " << Pkg.Name() << endl;
602 continue;
603 }
604
605 if (List->IsMissing(Pkg) == true)
606 {
607 if (Debug == true)
608 clog << "Sequence completed at " << Pkg.Name() << endl;
609 if (DoneSomething == false)
610 {
611 _error->Error("Internal Error, ordering was unable to handle the media swap");
612 return Failed;
613 }
614 return Incomplete;
615 }
616
617 // Sanity check
618 if (Cache[Pkg].Keep() == true &&
619 Pkg.State() == pkgCache::PkgIterator::NeedsNothing &&
620 (Cache[Pkg].iFlags & pkgDepCache::ReInstall) != pkgDepCache::ReInstall)
621 {
622 _error->Error("Internal Error, trying to manipulate a kept package (%s)",Pkg.Name());
623 return Failed;
624 }
625
626 // Perform a delete or an install
627 if (Cache[Pkg].Delete() == true)
628 {
629 if (SmartRemove(Pkg) == false)
630 return Failed;
631 }
632 else
633 if (SmartUnPack(Pkg) == false)
634 return Failed;
635 DoneSomething = true;
636 }
637
638 // Final run through the configure phase
639 if (ConfigureAll() == false)
640 return Failed;
641
642 // Sanity check
643 for (pkgOrderList::iterator I = List->begin(); I != List->end(); I++)
644 {
645 if (List->IsFlag(*I,pkgOrderList::Configured) == false)
646 {
647 _error->Error("Internal error, packages left unconfigured. %s",
648 PkgIterator(Cache,*I).Name());
649 return Failed;
650 }
651 }
652
653 return Completed;
654}
655 /*}}}*/
656// PM::DoInstallPostFork - Does install part that happens after the fork /*{{{*/
657// ---------------------------------------------------------------------
658pkgPackageManager::OrderResult
659pkgPackageManager::DoInstallPostFork(int statusFd)
660{
661 if(statusFd > 0)
662 // FIXME: use SetCloseExec here once it taught about throwing
663 // exceptions instead of doing _exit(100) on failure
664 fcntl(statusFd,F_SETFD,FD_CLOEXEC);
665 bool goResult = Go(statusFd);
666 if(goResult == false)
667 return Failed;
668
669 return Res;
670};
671
672// PM::DoInstall - Does the installation /*{{{*/
673// ---------------------------------------------------------------------
674/* This uses the filenames in FileNames and the information in the
675 DepCache to perform the installation of packages.*/
676pkgPackageManager::OrderResult pkgPackageManager::DoInstall(int statusFd)
677{
678 if(DoInstallPreFork() == Failed)
679 return Failed;
680
681 return DoInstallPostFork(statusFd);
682}
683 /*}}}*/