]>
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)
60 if (List
->OrderUnpack() == false)
61 return _error
->Error("Internal ordering error");
63 for (pkgOrderList::iterator I
= List
->begin(); I
!= List
->end(); I
++)
65 PkgIterator
Pkg(Cache
,*I
);
66 FileNames
[Pkg
->ID
] = string();
68 // Skip packages to erase
69 if (Cache
[Pkg
].Delete() == true)
72 // Skip Packages that need configure only.
73 if (Pkg
.State() == pkgCache::PkgIterator::NeedsConfigure
&&
74 Cache
[Pkg
].Keep() == true)
77 // Skip already processed packages
78 if (List
->IsNow(Pkg
) == false)
81 new pkgAcqArchive(Owner
,Sources
,Recs
,Cache
[Pkg
].InstVerIter(Cache
),
88 // PM::FixMissing - Keep all missing packages /*{{{*/
89 // ---------------------------------------------------------------------
90 /* This is called to correct the installation when packages could not
92 bool pkgPackageManager::FixMissing()
94 pkgDepCache::ActionGroup
group(Cache
);
95 pkgProblemResolver
Resolve(&Cache
);
96 List
->SetFileList(FileNames
);
99 for (PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
101 if (List
->IsMissing(I
) == false)
104 // Okay, this file is missing and we need it. Mark it for keep
106 Cache
.MarkKeep(I
, false, false);
109 // We have to empty the list otherwise it will not have the new changes
116 // Now downgrade everything that is broken
117 return Resolve
.ResolveByKeep() == true && Cache
.BrokenCount() == 0;
121 // PM::ImmediateAdd - Add the immediate flag recursivly /*{{{*/
122 // ---------------------------------------------------------------------
123 /* This adds the immediate flag to the pkg and recursively to the
126 void pkgPackageManager::ImmediateAdd(PkgIterator I
, bool UseInstallVer
)
132 if(Cache
[I
].InstallVer
== 0)
134 D
= Cache
[I
].InstVerIter(Cache
).DependsList();
136 if (I
->CurrentVer
== 0)
138 D
= I
.CurrentVer().DependsList();
141 for ( /* nothing */ ; D
.end() == false; D
++)
142 if (D
->Type
== pkgCache::Dep::Depends
|| D
->Type
== pkgCache::Dep::PreDepends
)
144 if(!List
->IsFlag(D
.TargetPkg(), pkgOrderList::Immediate
))
147 clog
<< "ImmediateAdd(): Adding Immediate flag to " << I
.Name() << endl
;
148 List
->Flag(D
.TargetPkg(),pkgOrderList::Immediate
);
149 ImmediateAdd(D
.TargetPkg(), UseInstallVer
);
156 // PM::CreateOrderList - Create the ordering class /*{{{*/
157 // ---------------------------------------------------------------------
158 /* This populates the ordering list with all the packages that are
160 bool pkgPackageManager::CreateOrderList()
166 List
= new pkgOrderList(&Cache
);
168 bool NoImmConfigure
= !_config
->FindB("APT::Immediate-Configure",true);
170 // Generate the list of affected packages and sort it
171 for (PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
173 // Ignore no-version packages
174 if (I
->VersionList
== 0)
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)
183 clog
<< "CreateOrderList(): Adding Immediate flag for " << I
.Name() << endl
;
184 List
->Flag(I
,pkgOrderList::Immediate
);
186 // Look for other install packages to make immediate configurea
187 ImmediateAdd(I
, true);
189 // And again with the current version.
190 ImmediateAdd(I
, false);
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
))
202 // Append it to the list
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] */
213 bool pkgPackageManager::DepAlwaysTrue(DepIterator D
)
215 if (D
.TargetPkg()->ProvidesList
!= 0)
218 if ((Cache
[D
] & pkgDepCache::DepInstall
) != 0 &&
219 (Cache
[D
] & pkgDepCache::DepNow
) != 0)
224 // PM::CheckRConflicts - Look for reverse conflicts /*{{{*/
225 // ---------------------------------------------------------------------
226 /* This looks over the reverses for a conflicts line that needs early
228 bool pkgPackageManager::CheckRConflicts(PkgIterator Pkg
,DepIterator D
,
231 for (;D
.end() == false; D
++)
233 if (D
->Type
!= pkgCache::Dep::Conflicts
&&
234 D
->Type
!= pkgCache::Dep::Obsoletes
)
237 // The package hasnt been changed
238 if (List
->IsNow(Pkg
) == false)
241 // Ignore self conflicts, ignore conflicts from irrelevent versions
242 if (D
.ParentPkg() == Pkg
|| D
.ParentVer() != D
.ParentPkg().CurrentVer())
245 if (Cache
.VS().CheckDep(Ver
,D
->CompareOp
,D
.TargetVer()) == false)
248 if (EarlyRemove(D
.ParentPkg()) == false)
249 return _error
->Error("Reverse conflicts early remove for package '%s' failed",
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. */
259 bool pkgPackageManager::ConfigureAll()
261 pkgOrderList
OList(&Cache
);
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)
269 if (OList
.OrderConfigure() == false)
272 // Perform the configuring
273 for (pkgOrderList::iterator I
= OList
.begin(); I
!= OList
.end(); I
++)
275 PkgIterator
Pkg(Cache
,*I
);
277 if (Configure(Pkg
) == false)
280 List
->Flag(Pkg
,pkgOrderList::Configured
,pkgOrderList::States
);
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. */
290 bool pkgPackageManager::SmartConfigure(PkgIterator Pkg
)
292 pkgOrderList
OList(&Cache
);
294 if (DepAdd(OList
,Pkg
) == false)
297 if (OList
.OrderConfigure() == false)
300 // Perform the configuring
301 for (pkgOrderList::iterator I
= OList
.begin(); I
!= OList
.end(); I
++)
303 PkgIterator
Pkg(Cache
,*I
);
305 if (Configure(Pkg
) == false)
308 List
->Flag(Pkg
,pkgOrderList::Configured
,pkgOrderList::States
);
312 if (List
->IsFlag(Pkg
,pkgOrderList::Configured
) == false)
313 return _error
->Error("Internal error, could not immediate configure %s",Pkg
.Name());
318 // PM::DepAdd - Add all dependents to the oder list /*{{{*/
319 // ---------------------------------------------------------------------
320 /* This recursively adds all dependents to the order list */
321 bool pkgPackageManager::DepAdd(pkgOrderList
&OList
,PkgIterator Pkg
,int Depth
)
323 if (OList
.IsFlag(Pkg
,pkgOrderList::Added
) == true)
325 if (List
->IsFlag(Pkg
,pkgOrderList::Configured
) == true)
327 if (List
->IsFlag(Pkg
,pkgOrderList::UnPacked
) == false)
330 // Put the package on the list
331 OList
.push_back(Pkg
);
332 OList
.Flag(Pkg
,pkgOrderList::Added
);
335 // Check the dependencies to see if they are all satisfied.
337 for (DepIterator D
= Cache
[Pkg
].InstVerIter(Cache
).DependsList(); D
.end() == false;)
339 if (D
->Type
!= pkgCache::Dep::Depends
&& D
->Type
!= pkgCache::Dep::PreDepends
)
347 for (bool LastOR
= true; D
.end() == false && LastOR
== true; D
++)
349 LastOR
= (D
->CompareOp
& pkgCache::Dep::Or
) == pkgCache::Dep::Or
;
354 SPtrArray
<Version
*> VList
= D
.AllTargets();
355 for (Version
**I
= VList
; *I
!= 0 && Bad
== true; I
++)
357 VerIterator
Ver(Cache
,*I
);
358 PkgIterator Pkg
= Ver
.ParentPkg();
360 // See if the current version is ok
361 if (Pkg
.CurrentVer() == Ver
&& List
->IsNow(Pkg
) == true &&
362 Pkg
.State() == PkgIterator::NeedsNothing
)
368 // Not the install version
369 if (Cache
[Pkg
].InstallVer
!= *I
||
370 (Cache
[Pkg
].Keep() == true && Pkg
.State() == PkgIterator::NeedsNothing
))
373 if (List
->IsFlag(Pkg
,pkgOrderList::UnPacked
) == true)
374 Bad
= !DepAdd(OList
,Pkg
,Depth
);
375 if (List
->IsFlag(Pkg
,pkgOrderList::Configured
) == true)
382 OList
.Flag(Pkg
,0,pkgOrderList::Added
);
393 // PM::EarlyRemove - Perform removal of packages before their time /*{{{*/
394 // ---------------------------------------------------------------------
395 /* This is called to deal with conflicts arising from unpacking */
396 bool pkgPackageManager::EarlyRemove(PkgIterator Pkg
)
398 if (List
->IsNow(Pkg
) == false)
401 // Already removed it
402 if (List
->IsFlag(Pkg
,pkgOrderList::Removed
) == true)
405 // Woops, it will not be re-installed!
406 if (List
->IsFlag(Pkg
,pkgOrderList::InList
) == false)
409 // Essential packages get special treatment
410 bool IsEssential
= false;
411 if ((Pkg
->Flags
& pkgCache::Flag::Essential
) != 0)
414 /* Check for packages that are the dependents of essential packages and
416 if (Pkg
->CurrentVer
!= 0)
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)
425 if (IsEssential
== true)
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());
435 bool Res
= SmartRemove(Pkg
);
436 if (Cache
[Pkg
].Delete() == false)
437 List
->Flag(Pkg
,pkgOrderList::Removed
,pkgOrderList::States
);
442 // PM::SmartRemove - Removal Helper /*{{{*/
443 // ---------------------------------------------------------------------
445 bool pkgPackageManager::SmartRemove(PkgIterator Pkg
)
447 if (List
->IsNow(Pkg
) == false)
450 List
->Flag(Pkg
,pkgOrderList::Configured
,pkgOrderList::States
);
451 return Remove(Pkg
,(Cache
[Pkg
].iFlags
& pkgDepCache::Purge
) == pkgDepCache::Purge
);
454 // PM::SmartUnPack - Install helper /*{{{*/
455 // ---------------------------------------------------------------------
456 /* This performs the task of handling pre-depends. */
457 bool pkgPackageManager::SmartUnPack(PkgIterator Pkg
)
459 // Check if it is already unpacked
460 if (Pkg
.State() == pkgCache::PkgIterator::NeedsConfigure
&&
461 Cache
[Pkg
].Keep() == true)
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());
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();
475 // Compute a single dependency element (glob or)
476 pkgCache::DepIterator Start
;
477 pkgCache::DepIterator End
;
480 while (End
->Type
== pkgCache::Dep::PreDepends
)
482 // Look for possible ok targets.
483 SPtrArray
<Version
*> VList
= Start
.AllTargets();
485 for (Version
**I
= VList
; *I
!= 0 && Bad
== true; I
++)
487 VerIterator
Ver(Cache
,*I
);
488 PkgIterator Pkg
= Ver
.ParentPkg();
490 // See if the current version is ok
491 if (Pkg
.CurrentVer() == Ver
&& List
->IsNow(Pkg
) == true &&
492 Pkg
.State() == PkgIterator::NeedsNothing
)
499 // Look for something that could be configured.
500 for (Version
**I
= VList
; *I
!= 0 && Bad
== true; I
++)
502 VerIterator
Ver(Cache
,*I
);
503 PkgIterator Pkg
= Ver
.ParentPkg();
505 // Not the install version
506 if (Cache
[Pkg
].InstallVer
!= *I
||
507 (Cache
[Pkg
].Keep() == true && Pkg
.State() == PkgIterator::NeedsNothing
))
510 Bad
= !SmartConfigure(Pkg
);
513 /* If this or element did not match then continue on to the
514 next or element until a matching element is found */
517 // This triggers if someone make a pre-depends/depend loop.
519 return _error
->Error("Couldn't configure pre-depend %s for %s, "
520 "probably a dependency cycle.",
521 End
.TargetPkg().Name(),Pkg
.Name());
528 if (End
->Type
== pkgCache::Dep::Conflicts
||
529 End
->Type
== pkgCache::Dep::Obsoletes
)
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
++)
536 VerIterator
Ver(Cache
,*I
);
537 PkgIterator Pkg
= Ver
.ParentPkg();
539 // See if the current version is conflicting
540 if (Pkg
.CurrentVer() == Ver
&& List
->IsNow(Pkg
) == true)
542 if (EarlyRemove(Pkg
) == false)
543 return _error
->Error("Internal Error, Could not early remove %s",Pkg
.Name());
549 // Check for reverse conflicts.
550 if (CheckRConflicts(Pkg
,Pkg
.RevDependsList(),
551 Cache
[Pkg
].InstVerIter(Cache
).VerStr()) == false)
554 for (PrvIterator P
= Cache
[Pkg
].InstVerIter(Cache
).ProvidesList();
555 P
.end() == false; P
++)
556 CheckRConflicts(Pkg
,P
.ParentPkg().RevDependsList(),P
.ProvideVersion());
558 if (Install(Pkg
,FileNames
[Pkg
->ID
]) == false)
561 List
->Flag(Pkg
,pkgOrderList::UnPacked
,pkgOrderList::States
);
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());
571 // PM::OrderInstall - Installation ordering routine /*{{{*/
572 // ---------------------------------------------------------------------
574 pkgPackageManager::OrderResult
pkgPackageManager::OrderInstall()
576 if (CreateOrderList() == false)
582 clog
<< "Begining to order" << endl
;
584 if (List
->OrderUnpack(FileNames
) == false)
586 _error
->Error("Internal ordering error");
591 clog
<< "Done ordering" << endl
;
593 bool DoneSomething
= false;
594 for (pkgOrderList::iterator I
= List
->begin(); I
!= List
->end(); I
++)
596 PkgIterator
Pkg(Cache
,*I
);
598 if (List
->IsNow(Pkg
) == false)
601 clog
<< "Skipping already done " << Pkg
.Name() << endl
;
605 if (List
->IsMissing(Pkg
) == true)
608 clog
<< "Sequence completed at " << Pkg
.Name() << endl
;
609 if (DoneSomething
== false)
611 _error
->Error("Internal Error, ordering was unable to handle the media swap");
618 if (Cache
[Pkg
].Keep() == true &&
619 Pkg
.State() == pkgCache::PkgIterator::NeedsNothing
&&
620 (Cache
[Pkg
].iFlags
& pkgDepCache::ReInstall
) != pkgDepCache::ReInstall
)
622 _error
->Error("Internal Error, trying to manipulate a kept package (%s)",Pkg
.Name());
626 // Perform a delete or an install
627 if (Cache
[Pkg
].Delete() == true)
629 if (SmartRemove(Pkg
) == false)
633 if (SmartUnPack(Pkg
) == false)
635 DoneSomething
= true;
638 // Final run through the configure phase
639 if (ConfigureAll() == false)
643 for (pkgOrderList::iterator I
= List
->begin(); I
!= List
->end(); I
++)
645 if (List
->IsFlag(*I
,pkgOrderList::Configured
) == false)
647 _error
->Error("Internal error, packages left unconfigured. %s",
648 PkgIterator(Cache
,*I
).Name());
656 // PM::DoInstallPostFork - Does install part that happens after the fork /*{{{*/
657 // ---------------------------------------------------------------------
658 pkgPackageManager::OrderResult
659 pkgPackageManager::DoInstallPostFork(int statusFd
)
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)
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.*/
676 pkgPackageManager::OrderResult
pkgPackageManager::DoInstall(int statusFd
)
678 if(DoInstallPreFork() == Failed
)
681 return DoInstallPostFork(statusFd
);