]>
git.saurik.com Git - apt.git/blob - apt-pkg/packagemanager.cc
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: packagemanager.cc,v 1.30 2003/04/27 03:04:15 doogie Exp $
4 /* ######################################################################
6 Package Manager - Abstacts the package manager
8 More work is needed in the area of transitioning provides, ie exim
9 replacing smail. This can cause interesing side effects.
11 Other cases involving conflicts+replaces should be tested.
13 ##################################################################### */
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>
32 // PM::PackageManager - Constructor /*{{{*/
33 // ---------------------------------------------------------------------
35 pkgPackageManager::pkgPackageManager(pkgDepCache
*pCache
) : Cache(*pCache
)
37 FileNames
= new string
[Cache
.Head().PackageCount
];
39 Debug
= _config
->FindB("Debug::pkgPackageManager",false);
42 // PM::PackageManager - Destructor /*{{{*/
43 // ---------------------------------------------------------------------
45 pkgPackageManager::~pkgPackageManager()
51 // PM::GetArchives - Queue the archives for download /*{{{*/
52 // ---------------------------------------------------------------------
54 bool pkgPackageManager::GetArchives(pkgAcquire
*Owner
,pkgSourceList
*Sources
,
57 if (CreateOrderList() == false)
61 _config
->FindB("PackageManager::UnpackAll",true) ?
62 List
->OrderUnpack() : List
->OrderCritical();
63 if (ordering
== false)
64 return _error
->Error("Internal ordering error");
66 for (pkgOrderList::iterator I
= List
->begin(); I
!= List
->end(); I
++)
68 PkgIterator
Pkg(Cache
,*I
);
69 FileNames
[Pkg
->ID
] = string();
71 // Skip packages to erase
72 if (Cache
[Pkg
].Delete() == true)
75 // Skip Packages that need configure only.
76 if (Pkg
.State() == pkgCache::PkgIterator::NeedsConfigure
&&
77 Cache
[Pkg
].Keep() == true)
80 // Skip already processed packages
81 if (List
->IsNow(Pkg
) == false)
84 if (pkgCache::VerIterator(Cache
, Cache
[Pkg
].CandidateVer
).Pseudo() == true)
87 new pkgAcqArchive(Owner
,Sources
,Recs
,Cache
[Pkg
].InstVerIter(Cache
),
94 // PM::FixMissing - Keep all missing packages /*{{{*/
95 // ---------------------------------------------------------------------
96 /* This is called to correct the installation when packages could not
98 bool pkgPackageManager::FixMissing()
100 pkgDepCache::ActionGroup
group(Cache
);
101 pkgProblemResolver
Resolve(&Cache
);
102 List
->SetFileList(FileNames
);
105 for (PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
107 if (List
->IsMissing(I
) == false)
110 // Okay, this file is missing and we need it. Mark it for keep
112 Cache
.MarkKeep(I
, false, false);
115 // We have to empty the list otherwise it will not have the new changes
122 // Now downgrade everything that is broken
123 return Resolve
.ResolveByKeep() == true && Cache
.BrokenCount() == 0;
126 // PM::ImmediateAdd - Add the immediate flag recursivly /*{{{*/
127 // ---------------------------------------------------------------------
128 /* This adds the immediate flag to the pkg and recursively to the
131 void pkgPackageManager::ImmediateAdd(PkgIterator I
, bool UseInstallVer
, unsigned const int &Depth
)
137 if(Cache
[I
].InstallVer
== 0)
139 D
= Cache
[I
].InstVerIter(Cache
).DependsList();
141 if (I
->CurrentVer
== 0)
143 D
= I
.CurrentVer().DependsList();
146 for ( /* nothing */ ; D
.end() == false; D
++)
147 if (D
->Type
== pkgCache::Dep::Depends
|| D
->Type
== pkgCache::Dep::PreDepends
)
149 if(!List
->IsFlag(D
.TargetPkg(), pkgOrderList::Immediate
))
152 clog
<< OutputInDepth(Depth
) << "ImmediateAdd(): Adding Immediate flag to " << D
.TargetPkg() << " cause of " << D
.DepType() << " " << I
.Name() << endl
;
153 List
->Flag(D
.TargetPkg(),pkgOrderList::Immediate
);
154 ImmediateAdd(D
.TargetPkg(), UseInstallVer
, Depth
+ 1);
160 // PM::CreateOrderList - Create the ordering class /*{{{*/
161 // ---------------------------------------------------------------------
162 /* This populates the ordering list with all the packages that are
164 bool pkgPackageManager::CreateOrderList()
170 List
= new pkgOrderList(&Cache
);
172 static bool const NoImmConfigure
= !_config
->FindB("APT::Immediate-Configure",true);
174 // Generate the list of affected packages and sort it
175 for (PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
177 // Ignore no-version packages
178 if (I
->VersionList
== 0)
181 // Mark the package and its dependends for immediate configuration
182 if (((I
->Flags
& pkgCache::Flag::Essential
) == pkgCache::Flag::Essential
||
183 (I
->Flags
& pkgCache::Flag::Important
) == pkgCache::Flag::Important
) &&
184 NoImmConfigure
== false)
187 clog
<< "CreateOrderList(): Adding Immediate flag for " << I
.Name() << endl
;
188 List
->Flag(I
,pkgOrderList::Immediate
);
190 // Look for other install packages to make immediate configurea
191 ImmediateAdd(I
, true);
193 // And again with the current version.
194 ImmediateAdd(I
, false);
198 if ((Cache
[I
].Keep() == true ||
199 Cache
[I
].InstVerIter(Cache
) == I
.CurrentVer()) &&
200 I
.State() == pkgCache::PkgIterator::NeedsNothing
&&
201 (Cache
[I
].iFlags
& pkgDepCache::ReInstall
) != pkgDepCache::ReInstall
&&
202 (I
.Purge() != false || Cache
[I
].Mode
!= pkgDepCache::ModeDelete
||
203 (Cache
[I
].iFlags
& pkgDepCache::Purge
) != pkgDepCache::Purge
))
206 // Append it to the list
213 // PM::DepAlwaysTrue - Returns true if this dep is irrelevent /*{{{*/
214 // ---------------------------------------------------------------------
215 /* The restriction on provides is to eliminate the case when provides
216 are transitioning between valid states [ie exim to smail] */
217 bool pkgPackageManager::DepAlwaysTrue(DepIterator D
)
219 if (D
.TargetPkg()->ProvidesList
!= 0)
222 if ((Cache
[D
] & pkgDepCache::DepInstall
) != 0 &&
223 (Cache
[D
] & pkgDepCache::DepNow
) != 0)
228 // PM::CheckRConflicts - Look for reverse conflicts /*{{{*/
229 // ---------------------------------------------------------------------
230 /* This looks over the reverses for a conflicts line that needs early
232 bool pkgPackageManager::CheckRConflicts(PkgIterator Pkg
,DepIterator D
,
235 for (;D
.end() == false; D
++)
237 if (D
->Type
!= pkgCache::Dep::Conflicts
&&
238 D
->Type
!= pkgCache::Dep::Obsoletes
)
241 // The package hasnt been changed
242 if (List
->IsNow(Pkg
) == false)
245 // Ignore self conflicts, ignore conflicts from irrelevent versions
246 if (D
.ParentPkg() == Pkg
|| D
.ParentVer() != D
.ParentPkg().CurrentVer())
249 if (Cache
.VS().CheckDep(Ver
,D
->CompareOp
,D
.TargetVer()) == false)
252 if (EarlyRemove(D
.ParentPkg()) == false)
253 return _error
->Error("Reverse conflicts early remove for package '%s' failed",
259 // PM::ConfigureAll - Run the all out configuration /*{{{*/
260 // ---------------------------------------------------------------------
261 /* This configures every package. It is assumed they are all unpacked and
262 that the final configuration is valid. */
263 bool pkgPackageManager::ConfigureAll()
265 pkgOrderList
OList(&Cache
);
267 // Populate the order list
268 for (pkgOrderList::iterator I
= List
->begin(); I
!= List
->end(); I
++)
269 if (List
->IsFlag(pkgCache::PkgIterator(Cache
,*I
),
270 pkgOrderList::UnPacked
) == true)
273 if (OList
.OrderConfigure() == false)
276 std::string
const conf
= _config
->Find("PackageManager::Configure","all");
277 bool const ConfigurePkgs
= (conf
== "all");
279 // Perform the configuring
280 for (pkgOrderList::iterator I
= OList
.begin(); I
!= OList
.end(); I
++)
282 PkgIterator
Pkg(Cache
,*I
);
284 if (ConfigurePkgs
== true &&
285 pkgCache::VerIterator(Cache
, Cache
[Pkg
].CandidateVer
).Pseudo() == false &&
286 Configure(Pkg
) == false)
289 List
->Flag(Pkg
,pkgOrderList::Configured
,pkgOrderList::States
);
295 // PM::SmartConfigure - Perform immediate configuration of the pkg /*{{{*/
296 // ---------------------------------------------------------------------
297 /* This routine scheduals the configuration of the given package and all
298 of it's dependents. */
299 bool pkgPackageManager::SmartConfigure(PkgIterator Pkg
)
302 clog
<< "SmartConfigure " << Pkg
.Name() << endl
;
304 pkgOrderList
OList(&Cache
);
306 if (DepAdd(OList
,Pkg
) == false)
309 static std::string
const conf
= _config
->Find("PackageManager::Configure","all");
310 static bool const ConfigurePkgs
= (conf
== "all" || conf
== "smart");
312 if (ConfigurePkgs
== true)
313 if (OList
.OrderConfigure() == false)
316 // Perform the configuring
317 for (pkgOrderList::iterator I
= OList
.begin(); I
!= OList
.end(); I
++)
319 PkgIterator
Pkg(Cache
,*I
);
321 if (ConfigurePkgs
== true &&
322 pkgCache::VerIterator(Cache
, Cache
[Pkg
].CandidateVer
).Pseudo() == false &&
323 Configure(Pkg
) == false)
326 List
->Flag(Pkg
,pkgOrderList::Configured
,pkgOrderList::States
);
330 if (List
->IsFlag(Pkg
,pkgOrderList::Configured
) == false)
331 return _error
->Error(_("Could not perform immediate configuration on '%s'."
332 "Please see man 5 apt.conf under APT::Immediate-Configure for details. (%d)"),Pkg
.Name(),1);
337 // PM::DepAdd - Add all dependents to the oder list /*{{{*/
338 // ---------------------------------------------------------------------
339 /* This recursively adds all dependents to the order list */
340 bool pkgPackageManager::DepAdd(pkgOrderList
&OList
,PkgIterator Pkg
,int Depth
)
342 if (OList
.IsFlag(Pkg
,pkgOrderList::Added
) == true)
344 if (List
->IsFlag(Pkg
,pkgOrderList::Configured
) == true)
346 if (List
->IsFlag(Pkg
,pkgOrderList::UnPacked
) == false)
350 std::clog
<< OutputInDepth(Depth
) << "DepAdd: " << Pkg
.Name() << std::endl
;
352 // Put the package on the list
353 OList
.push_back(Pkg
);
354 OList
.Flag(Pkg
,pkgOrderList::Added
);
357 // Check the dependencies to see if they are all satisfied.
359 for (DepIterator D
= Cache
[Pkg
].InstVerIter(Cache
).DependsList(); D
.end() == false;)
361 if (D
->Type
!= pkgCache::Dep::Depends
&& D
->Type
!= pkgCache::Dep::PreDepends
)
369 for (bool LastOR
= true; D
.end() == false && LastOR
== true; D
++)
371 LastOR
= (D
->CompareOp
& pkgCache::Dep::Or
) == pkgCache::Dep::Or
;
376 SPtrArray
<Version
*> VList
= D
.AllTargets();
377 for (Version
**I
= VList
; *I
!= 0 && Bad
== true; I
++)
379 VerIterator
Ver(Cache
,*I
);
380 PkgIterator Pkg
= Ver
.ParentPkg();
382 // See if the current version is ok
383 if (Pkg
.CurrentVer() == Ver
&& List
->IsNow(Pkg
) == true &&
384 Pkg
.State() == PkgIterator::NeedsNothing
)
390 // Not the install version
391 if (Cache
[Pkg
].InstallVer
!= *I
||
392 (Cache
[Pkg
].Keep() == true && Pkg
.State() == PkgIterator::NeedsNothing
))
395 if (List
->IsFlag(Pkg
,pkgOrderList::UnPacked
) == true)
396 Bad
= !DepAdd(OList
,Pkg
,Depth
);
397 if (List
->IsFlag(Pkg
,pkgOrderList::Configured
) == true)
405 std::clog
<< OutputInDepth(Depth
) << "DepAdd FAILS on: " << Pkg
.Name() << std::endl
;
406 OList
.Flag(Pkg
,0,pkgOrderList::Added
);
417 // PM::EarlyRemove - Perform removal of packages before their time /*{{{*/
418 // ---------------------------------------------------------------------
419 /* This is called to deal with conflicts arising from unpacking */
420 bool pkgPackageManager::EarlyRemove(PkgIterator Pkg
)
422 if (List
->IsNow(Pkg
) == false)
425 // Already removed it
426 if (List
->IsFlag(Pkg
,pkgOrderList::Removed
) == true)
429 // Woops, it will not be re-installed!
430 if (List
->IsFlag(Pkg
,pkgOrderList::InList
) == false)
433 // Essential packages get special treatment
434 bool IsEssential
= false;
435 if ((Pkg
->Flags
& pkgCache::Flag::Essential
) != 0)
438 /* Check for packages that are the dependents of essential packages and
440 if (Pkg
->CurrentVer
!= 0)
442 for (DepIterator D
= Pkg
.RevDependsList(); D
.end() == false &&
443 IsEssential
== false; D
++)
444 if (D
->Type
== pkgCache::Dep::Depends
|| D
->Type
== pkgCache::Dep::PreDepends
)
445 if ((D
.ParentPkg()->Flags
& pkgCache::Flag::Essential
) != 0)
449 if (IsEssential
== true)
451 if (_config
->FindB("APT::Force-LoopBreak",false) == false)
452 return _error
->Error(_("This installation run will require temporarily "
453 "removing the essential package %s due to a "
454 "Conflicts/Pre-Depends loop. This is often bad, "
455 "but if you really want to do it, activate the "
456 "APT::Force-LoopBreak option."),Pkg
.Name());
459 bool Res
= SmartRemove(Pkg
);
460 if (Cache
[Pkg
].Delete() == false)
461 List
->Flag(Pkg
,pkgOrderList::Removed
,pkgOrderList::States
);
466 // PM::SmartRemove - Removal Helper /*{{{*/
467 // ---------------------------------------------------------------------
469 bool pkgPackageManager::SmartRemove(PkgIterator Pkg
)
471 if (List
->IsNow(Pkg
) == false)
474 List
->Flag(Pkg
,pkgOrderList::Configured
,pkgOrderList::States
);
476 if (pkgCache::VerIterator(Cache
, Cache
[Pkg
].CandidateVer
).Pseudo() == false)
477 return Remove(Pkg
,(Cache
[Pkg
].iFlags
& pkgDepCache::Purge
) == pkgDepCache::Purge
);
479 return SmartRemove(Pkg
.Group().FindPkg("all"));
483 // PM::SmartUnPack - Install helper /*{{{*/
484 // ---------------------------------------------------------------------
485 /* This performs the task of handling pre-depends. */
486 bool pkgPackageManager::SmartUnPack(PkgIterator Pkg
)
488 // Check if it is already unpacked
489 if (Pkg
.State() == pkgCache::PkgIterator::NeedsConfigure
&&
490 Cache
[Pkg
].Keep() == true)
492 List
->Flag(Pkg
,pkgOrderList::UnPacked
,pkgOrderList::States
);
493 if (List
->IsFlag(Pkg
,pkgOrderList::Immediate
) == true)
494 if (SmartConfigure(Pkg
) == false)
495 return _error
->Error(_("Could not perform immediate configuration on already unpacked '%s'."
496 "Please see man 5 apt.conf under APT::Immediate-Configure for details."),Pkg
.Name());
500 /* See if this packages install version has any predependencies
501 that are not met by 'now' packages. */
502 for (DepIterator D
= Cache
[Pkg
].InstVerIter(Cache
).DependsList();
505 // Compute a single dependency element (glob or)
506 pkgCache::DepIterator Start
;
507 pkgCache::DepIterator End
;
510 while (End
->Type
== pkgCache::Dep::PreDepends
)
513 clog
<< "PreDepends order for " << Pkg
.Name() << std::endl
;
515 // Look for possible ok targets.
516 SPtrArray
<Version
*> VList
= Start
.AllTargets();
518 for (Version
**I
= VList
; *I
!= 0 && Bad
== true; I
++)
520 VerIterator
Ver(Cache
,*I
);
521 PkgIterator Pkg
= Ver
.ParentPkg();
523 // See if the current version is ok
524 if (Pkg
.CurrentVer() == Ver
&& List
->IsNow(Pkg
) == true &&
525 Pkg
.State() == PkgIterator::NeedsNothing
)
529 clog
<< "Found ok package " << Pkg
.Name() << endl
;
534 // Look for something that could be configured.
535 for (Version
**I
= VList
; *I
!= 0 && Bad
== true; I
++)
537 VerIterator
Ver(Cache
,*I
);
538 PkgIterator Pkg
= Ver
.ParentPkg();
540 // Not the install version
541 if (Cache
[Pkg
].InstallVer
!= *I
||
542 (Cache
[Pkg
].Keep() == true && Pkg
.State() == PkgIterator::NeedsNothing
))
546 clog
<< "Trying to SmartConfigure " << Pkg
.Name() << endl
;
547 Bad
= !SmartConfigure(Pkg
);
550 /* If this or element did not match then continue on to the
551 next or element until a matching element is found */
554 // This triggers if someone make a pre-depends/depend loop.
556 return _error
->Error("Couldn't configure pre-depend %s for %s, "
557 "probably a dependency cycle.",
558 End
.TargetPkg().Name(),Pkg
.Name());
565 if (End
->Type
== pkgCache::Dep::Conflicts
||
566 End
->Type
== pkgCache::Dep::Obsoletes
)
568 /* Look for conflicts. Two packages that are both in the install
569 state cannot conflict so we don't check.. */
570 SPtrArray
<Version
*> VList
= End
.AllTargets();
571 for (Version
**I
= VList
; *I
!= 0; I
++)
573 VerIterator
Ver(Cache
,*I
);
574 PkgIterator Pkg
= Ver
.ParentPkg();
576 // See if the current version is conflicting
577 if (Pkg
.CurrentVer() == Ver
&& List
->IsNow(Pkg
) == true)
579 if (EarlyRemove(Pkg
) == false)
580 return _error
->Error("Internal Error, Could not early remove %s",Pkg
.Name());
586 // Check for reverse conflicts.
587 if (CheckRConflicts(Pkg
,Pkg
.RevDependsList(),
588 Cache
[Pkg
].InstVerIter(Cache
).VerStr()) == false)
591 for (PrvIterator P
= Cache
[Pkg
].InstVerIter(Cache
).ProvidesList();
592 P
.end() == false; P
++)
593 CheckRConflicts(Pkg
,P
.ParentPkg().RevDependsList(),P
.ProvideVersion());
595 if (pkgCache::VerIterator(Cache
, Cache
[Pkg
].CandidateVer
).Pseudo() == false)
597 if(Install(Pkg
,FileNames
[Pkg
->ID
]) == false)
600 // Pseudo packages will not be unpacked - instead we will do this
601 // for the "real" package, but only once and if it is already
602 // configured we don't need to unpack it again…
603 PkgIterator
const P
= Pkg
.Group().FindPkg("all");
604 if (List
->IsFlag(P
,pkgOrderList::UnPacked
) != true &&
605 List
->IsFlag(P
,pkgOrderList::Configured
) != true &&
606 P
.State() != pkgCache::PkgIterator::NeedsNothing
) {
607 if (SmartUnPack(P
) == false)
611 List
->Flag(Pkg
,pkgOrderList::UnPacked
,pkgOrderList::States
);
613 // Perform immedate configuration of the package.
614 if (List
->IsFlag(Pkg
,pkgOrderList::Immediate
) == true)
615 if (SmartConfigure(Pkg
) == false)
616 return _error
->Error(_("Could not perform immediate configuration on '%s'."
617 "Please see man 5 apt.conf under APT::Immediate-Configure for details. (%d)"),Pkg
.Name(),2);
622 // PM::OrderInstall - Installation ordering routine /*{{{*/
623 // ---------------------------------------------------------------------
625 pkgPackageManager::OrderResult
pkgPackageManager::OrderInstall()
627 if (CreateOrderList() == false)
633 clog
<< "Beginning to order" << endl
;
635 bool const ordering
=
636 _config
->FindB("PackageManager::UnpackAll",true) ?
637 List
->OrderUnpack(FileNames
) : List
->OrderCritical();
638 if (ordering
== false)
640 _error
->Error("Internal ordering error");
645 clog
<< "Done ordering" << endl
;
647 bool DoneSomething
= false;
648 for (pkgOrderList::iterator I
= List
->begin(); I
!= List
->end(); I
++)
650 PkgIterator
Pkg(Cache
,*I
);
652 if (List
->IsNow(Pkg
) == false)
655 clog
<< "Skipping already done " << Pkg
.Name() << endl
;
659 if (List
->IsMissing(Pkg
) == true)
662 clog
<< "Sequence completed at " << Pkg
.Name() << endl
;
663 if (DoneSomething
== false)
665 _error
->Error("Internal Error, ordering was unable to handle the media swap");
672 if (Cache
[Pkg
].Keep() == true &&
673 Pkg
.State() == pkgCache::PkgIterator::NeedsNothing
&&
674 (Cache
[Pkg
].iFlags
& pkgDepCache::ReInstall
) != pkgDepCache::ReInstall
)
676 _error
->Error("Internal Error, trying to manipulate a kept package (%s)",Pkg
.Name());
680 // Perform a delete or an install
681 if (Cache
[Pkg
].Delete() == true)
683 if (SmartRemove(Pkg
) == false)
687 if (SmartUnPack(Pkg
) == false)
689 DoneSomething
= true;
692 // Final run through the configure phase
693 if (ConfigureAll() == false)
697 for (pkgOrderList::iterator I
= List
->begin(); I
!= List
->end(); I
++)
699 if (List
->IsFlag(*I
,pkgOrderList::Configured
) == false)
701 _error
->Error("Internal error, packages left unconfigured. %s",
702 PkgIterator(Cache
,*I
).Name());
710 // PM::DoInstallPostFork - Does install part that happens after the fork /*{{{*/
711 // ---------------------------------------------------------------------
712 pkgPackageManager::OrderResult
713 pkgPackageManager::DoInstallPostFork(int statusFd
)
716 // FIXME: use SetCloseExec here once it taught about throwing
717 // exceptions instead of doing _exit(100) on failure
718 fcntl(statusFd
,F_SETFD
,FD_CLOEXEC
);
719 bool goResult
= Go(statusFd
);
720 if(goResult
== false)
726 // PM::DoInstall - Does the installation /*{{{*/
727 // ---------------------------------------------------------------------
728 /* This uses the filenames in FileNames and the information in the
729 DepCache to perform the installation of packages.*/
730 pkgPackageManager::OrderResult
pkgPackageManager::DoInstall(int statusFd
)
732 if(DoInstallPreFork() == Failed
)
735 return DoInstallPostFork(statusFd
);