]>
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 /*{{{*/
17 #pragma implementation "apt-pkg/packagemanager.h"
20 #include <apt-pkg/packagemanager.h>
21 #include <apt-pkg/orderlist.h>
22 #include <apt-pkg/depcache.h>
23 #include <apt-pkg/error.h>
24 #include <apt-pkg/version.h>
25 #include <apt-pkg/acquire-item.h>
26 #include <apt-pkg/algorithms.h>
27 #include <apt-pkg/configuration.h>
28 #include <apt-pkg/sptr.h>
36 // PM::PackageManager - Constructor /*{{{*/
37 // ---------------------------------------------------------------------
39 pkgPackageManager::pkgPackageManager(pkgDepCache
*pCache
) : Cache(*pCache
)
41 FileNames
= new string
[Cache
.Head().PackageCount
];
43 Debug
= _config
->FindB("Debug::pkgPackageManager",false);
46 // PM::PackageManager - Destructor /*{{{*/
47 // ---------------------------------------------------------------------
49 pkgPackageManager::~pkgPackageManager()
55 // PM::GetArchives - Queue the archives for download /*{{{*/
56 // ---------------------------------------------------------------------
58 bool pkgPackageManager::GetArchives(pkgAcquire
*Owner
,pkgSourceList
*Sources
,
61 if (CreateOrderList() == false)
64 if (List
->OrderUnpack() == false)
65 return _error
->Error("Internal ordering error");
67 for (pkgOrderList::iterator I
= List
->begin(); I
!= List
->end(); I
++)
69 PkgIterator
Pkg(Cache
,*I
);
70 FileNames
[Pkg
->ID
] = string();
72 // Skip packages to erase
73 if (Cache
[Pkg
].Delete() == true)
76 // Skip Packages that need configure only.
77 if (Pkg
.State() == pkgCache::PkgIterator::NeedsConfigure
&&
78 Cache
[Pkg
].Keep() == true)
81 // Skip already processed packages
82 if (List
->IsNow(Pkg
) == false)
85 new pkgAcqArchive(Owner
,Sources
,Recs
,Cache
[Pkg
].InstVerIter(Cache
),
92 // PM::FixMissing - Keep all missing packages /*{{{*/
93 // ---------------------------------------------------------------------
94 /* This is called to correct the installation when packages could not
96 bool pkgPackageManager::FixMissing()
98 pkgDepCache::ActionGroup
group(Cache
);
99 pkgProblemResolver
Resolve(&Cache
);
100 List
->SetFileList(FileNames
);
103 for (PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
105 if (List
->IsMissing(I
) == false)
108 // Okay, this file is missing and we need it. Mark it for keep
110 Cache
.MarkKeep(I
, false, false);
113 // We have to empty the list otherwise it will not have the new changes
120 // Now downgrade everything that is broken
121 return Resolve
.ResolveByKeep() == true && Cache
.BrokenCount() == 0;
125 // PM::CreateOrderList - Create the ordering class /*{{{*/
126 // ---------------------------------------------------------------------
127 /* This populates the ordering list with all the packages that are
129 bool pkgPackageManager::CreateOrderList()
135 List
= new pkgOrderList(&Cache
);
137 bool NoImmConfigure
= !_config
->FindB("APT::Immediate-Configure",true);
139 // Generate the list of affected packages and sort it
140 for (PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
142 // Ignore no-version packages
143 if (I
->VersionList
== 0)
146 // Mark the package and its dependends for immediate configuration
147 if (((I
->Flags
& pkgCache::Flag::Essential
) == pkgCache::Flag::Essential
||
148 (I
->Flags
& pkgCache::Flag::Important
) == pkgCache::Flag::Important
) &&
149 NoImmConfigure
== false)
151 List
->Flag(I
,pkgOrderList::Immediate
);
153 // Look for other packages to make immediate configurea
154 if (Cache
[I
].InstallVer
!= 0)
155 for (DepIterator D
= Cache
[I
].InstVerIter(Cache
).DependsList();
156 D
.end() == false; D
++)
157 if (D
->Type
== pkgCache::Dep::Depends
|| D
->Type
== pkgCache::Dep::PreDepends
)
158 List
->Flag(D
.TargetPkg(),pkgOrderList::Immediate
);
160 // And again with the current version.
161 if (I
->CurrentVer
!= 0)
162 for (DepIterator D
= I
.CurrentVer().DependsList();
163 D
.end() == false; D
++)
164 if (D
->Type
== pkgCache::Dep::Depends
|| D
->Type
== pkgCache::Dep::PreDepends
)
165 List
->Flag(D
.TargetPkg(),pkgOrderList::Immediate
);
169 if ((Cache
[I
].Keep() == true ||
170 Cache
[I
].InstVerIter(Cache
) == I
.CurrentVer()) &&
171 I
.State() == pkgCache::PkgIterator::NeedsNothing
&&
172 (Cache
[I
].iFlags
& pkgDepCache::ReInstall
) != pkgDepCache::ReInstall
&&
173 (I
.Purge() != false || Cache
[I
].Mode
!= pkgDepCache::ModeDelete
||
174 (Cache
[I
].iFlags
& pkgDepCache::Purge
) != pkgDepCache::Purge
))
177 // Append it to the list
184 // PM::DepAlwaysTrue - Returns true if this dep is irrelevent /*{{{*/
185 // ---------------------------------------------------------------------
186 /* The restriction on provides is to eliminate the case when provides
187 are transitioning between valid states [ie exim to smail] */
188 bool pkgPackageManager::DepAlwaysTrue(DepIterator D
)
190 if (D
.TargetPkg()->ProvidesList
!= 0)
193 if ((Cache
[D
] & pkgDepCache::DepInstall
) != 0 &&
194 (Cache
[D
] & pkgDepCache::DepNow
) != 0)
199 // PM::CheckRConflicts - Look for reverse conflicts /*{{{*/
200 // ---------------------------------------------------------------------
201 /* This looks over the reverses for a conflicts line that needs early
203 bool pkgPackageManager::CheckRConflicts(PkgIterator Pkg
,DepIterator D
,
206 for (;D
.end() == false; D
++)
208 if (D
->Type
!= pkgCache::Dep::Conflicts
&&
209 D
->Type
!= pkgCache::Dep::Obsoletes
)
212 // The package hasnt been changed
213 if (List
->IsNow(Pkg
) == false)
216 // Ignore self conflicts, ignore conflicts from irrelevent versions
217 if (D
.ParentPkg() == Pkg
|| D
.ParentVer() != D
.ParentPkg().CurrentVer())
220 if (Cache
.VS().CheckDep(Ver
,D
->CompareOp
,D
.TargetVer()) == false)
223 if (EarlyRemove(D
.ParentPkg()) == false)
224 return _error
->Error("Reverse conflicts early remove for package '%s' failed",
230 // PM::ConfigureAll - Run the all out configuration /*{{{*/
231 // ---------------------------------------------------------------------
232 /* This configures every package. It is assumed they are all unpacked and
233 that the final configuration is valid. */
234 bool pkgPackageManager::ConfigureAll()
236 pkgOrderList
OList(&Cache
);
238 // Populate the order list
239 for (pkgOrderList::iterator I
= List
->begin(); I
!= List
->end(); I
++)
240 if (List
->IsFlag(pkgCache::PkgIterator(Cache
,*I
),
241 pkgOrderList::UnPacked
) == true)
244 if (OList
.OrderConfigure() == false)
247 // Perform the configuring
248 for (pkgOrderList::iterator I
= OList
.begin(); I
!= OList
.end(); I
++)
250 PkgIterator
Pkg(Cache
,*I
);
252 if (Configure(Pkg
) == false)
255 List
->Flag(Pkg
,pkgOrderList::Configured
,pkgOrderList::States
);
261 // PM::SmartConfigure - Perform immediate configuration of the pkg /*{{{*/
262 // ---------------------------------------------------------------------
263 /* This routine scheduals the configuration of the given package and all
264 of it's dependents. */
265 bool pkgPackageManager::SmartConfigure(PkgIterator Pkg
)
267 pkgOrderList
OList(&Cache
);
269 if (DepAdd(OList
,Pkg
) == false)
272 if (OList
.OrderConfigure() == false)
275 // Perform the configuring
276 for (pkgOrderList::iterator I
= OList
.begin(); I
!= OList
.end(); I
++)
278 PkgIterator
Pkg(Cache
,*I
);
280 if (Configure(Pkg
) == false)
283 List
->Flag(Pkg
,pkgOrderList::Configured
,pkgOrderList::States
);
287 if (List
->IsFlag(Pkg
,pkgOrderList::Configured
) == false)
288 return _error
->Error("Internal error, could not immediate configure %s",Pkg
.Name());
293 // PM::DepAdd - Add all dependents to the oder list /*{{{*/
294 // ---------------------------------------------------------------------
295 /* This recursively adds all dependents to the order list */
296 bool pkgPackageManager::DepAdd(pkgOrderList
&OList
,PkgIterator Pkg
,int Depth
)
298 if (OList
.IsFlag(Pkg
,pkgOrderList::Added
) == true)
300 if (List
->IsFlag(Pkg
,pkgOrderList::Configured
) == true)
302 if (List
->IsFlag(Pkg
,pkgOrderList::UnPacked
) == false)
305 // Put the package on the list
306 OList
.push_back(Pkg
);
307 OList
.Flag(Pkg
,pkgOrderList::Added
);
310 // Check the dependencies to see if they are all satisfied.
312 for (DepIterator D
= Cache
[Pkg
].InstVerIter(Cache
).DependsList(); D
.end() == false;)
314 if (D
->Type
!= pkgCache::Dep::Depends
&& D
->Type
!= pkgCache::Dep::PreDepends
)
322 for (bool LastOR
= true; D
.end() == false && LastOR
== true; D
++)
324 LastOR
= (D
->CompareOp
& pkgCache::Dep::Or
) == pkgCache::Dep::Or
;
329 SPtrArray
<Version
*> VList
= D
.AllTargets();
330 for (Version
**I
= VList
; *I
!= 0 && Bad
== true; I
++)
332 VerIterator
Ver(Cache
,*I
);
333 PkgIterator Pkg
= Ver
.ParentPkg();
335 // See if the current version is ok
336 if (Pkg
.CurrentVer() == Ver
&& List
->IsNow(Pkg
) == true &&
337 Pkg
.State() == PkgIterator::NeedsNothing
)
343 // Not the install version
344 if (Cache
[Pkg
].InstallVer
!= *I
||
345 (Cache
[Pkg
].Keep() == true && Pkg
.State() == PkgIterator::NeedsNothing
))
348 if (List
->IsFlag(Pkg
,pkgOrderList::UnPacked
) == true)
349 Bad
= !DepAdd(OList
,Pkg
,Depth
);
350 if (List
->IsFlag(Pkg
,pkgOrderList::Configured
) == true)
357 OList
.Flag(Pkg
,0,pkgOrderList::Added
);
368 // PM::EarlyRemove - Perform removal of packages before their time /*{{{*/
369 // ---------------------------------------------------------------------
370 /* This is called to deal with conflicts arising from unpacking */
371 bool pkgPackageManager::EarlyRemove(PkgIterator Pkg
)
373 if (List
->IsNow(Pkg
) == false)
376 // Already removed it
377 if (List
->IsFlag(Pkg
,pkgOrderList::Removed
) == true)
380 // Woops, it will not be re-installed!
381 if (List
->IsFlag(Pkg
,pkgOrderList::InList
) == false)
384 // Essential packages get special treatment
385 bool IsEssential
= false;
386 if ((Pkg
->Flags
& pkgCache::Flag::Essential
) != 0)
389 /* Check for packages that are the dependents of essential packages and
391 if (Pkg
->CurrentVer
!= 0)
393 for (DepIterator D
= Pkg
.RevDependsList(); D
.end() == false &&
394 IsEssential
== false; D
++)
395 if (D
->Type
== pkgCache::Dep::Depends
|| D
->Type
== pkgCache::Dep::PreDepends
)
396 if ((D
.ParentPkg()->Flags
& pkgCache::Flag::Essential
) != 0)
400 if (IsEssential
== true)
402 if (_config
->FindB("APT::Force-LoopBreak",false) == false)
403 return _error
->Error(_("This installation run will require temporarily "
404 "removing the essential package %s due to a "
405 "Conflicts/Pre-Depends loop. This is often bad, "
406 "but if you really want to do it, activate the "
407 "APT::Force-LoopBreak option."),Pkg
.Name());
410 bool Res
= SmartRemove(Pkg
);
411 if (Cache
[Pkg
].Delete() == false)
412 List
->Flag(Pkg
,pkgOrderList::Removed
,pkgOrderList::States
);
417 // PM::SmartRemove - Removal Helper /*{{{*/
418 // ---------------------------------------------------------------------
420 bool pkgPackageManager::SmartRemove(PkgIterator Pkg
)
422 if (List
->IsNow(Pkg
) == false)
425 List
->Flag(Pkg
,pkgOrderList::Configured
,pkgOrderList::States
);
426 return Remove(Pkg
,(Cache
[Pkg
].iFlags
& pkgDepCache::Purge
) == pkgDepCache::Purge
);
429 // PM::SmartUnPack - Install helper /*{{{*/
430 // ---------------------------------------------------------------------
431 /* This performs the task of handling pre-depends. */
432 bool pkgPackageManager::SmartUnPack(PkgIterator Pkg
)
434 // Check if it is already unpacked
435 if (Pkg
.State() == pkgCache::PkgIterator::NeedsConfigure
&&
436 Cache
[Pkg
].Keep() == true)
438 List
->Flag(Pkg
,pkgOrderList::UnPacked
,pkgOrderList::States
);
439 if (List
->IsFlag(Pkg
,pkgOrderList::Immediate
) == true)
440 if (SmartConfigure(Pkg
) == false)
441 return _error
->Error("Internal Error, Could not perform immediate configuration (1) on %s",Pkg
.Name());
445 /* See if this packages install version has any predependencies
446 that are not met by 'now' packages. */
447 for (DepIterator D
= Cache
[Pkg
].InstVerIter(Cache
).DependsList();
450 // Compute a single dependency element (glob or)
451 pkgCache::DepIterator Start
;
452 pkgCache::DepIterator End
;
455 while (End
->Type
== pkgCache::Dep::PreDepends
)
457 // Look for possible ok targets.
458 SPtrArray
<Version
*> VList
= Start
.AllTargets();
460 for (Version
**I
= VList
; *I
!= 0 && Bad
== true; I
++)
462 VerIterator
Ver(Cache
,*I
);
463 PkgIterator Pkg
= Ver
.ParentPkg();
465 // See if the current version is ok
466 if (Pkg
.CurrentVer() == Ver
&& List
->IsNow(Pkg
) == true &&
467 Pkg
.State() == PkgIterator::NeedsNothing
)
474 // Look for something that could be configured.
475 for (Version
**I
= VList
; *I
!= 0 && Bad
== true; I
++)
477 VerIterator
Ver(Cache
,*I
);
478 PkgIterator Pkg
= Ver
.ParentPkg();
480 // Not the install version
481 if (Cache
[Pkg
].InstallVer
!= *I
||
482 (Cache
[Pkg
].Keep() == true && Pkg
.State() == PkgIterator::NeedsNothing
))
485 Bad
= !SmartConfigure(Pkg
);
488 /* If this or element did not match then continue on to the
489 next or element until a matching element is found */
492 // This triggers if someone make a pre-depends/depend loop.
494 return _error
->Error("Couldn't configure pre-depend %s for %s, "
495 "probably a dependency cycle.",
496 End
.TargetPkg().Name(),Pkg
.Name());
503 if (End
->Type
== pkgCache::Dep::Conflicts
||
504 End
->Type
== pkgCache::Dep::Obsoletes
)
506 /* Look for conflicts. Two packages that are both in the install
507 state cannot conflict so we don't check.. */
508 SPtrArray
<Version
*> VList
= End
.AllTargets();
509 for (Version
**I
= VList
; *I
!= 0; I
++)
511 VerIterator
Ver(Cache
,*I
);
512 PkgIterator Pkg
= Ver
.ParentPkg();
514 // See if the current version is conflicting
515 if (Pkg
.CurrentVer() == Ver
&& List
->IsNow(Pkg
) == true)
517 if (EarlyRemove(Pkg
) == false)
518 return _error
->Error("Internal Error, Could not early remove %s",Pkg
.Name());
524 // Check for reverse conflicts.
525 if (CheckRConflicts(Pkg
,Pkg
.RevDependsList(),
526 Cache
[Pkg
].InstVerIter(Cache
).VerStr()) == false)
529 for (PrvIterator P
= Cache
[Pkg
].InstVerIter(Cache
).ProvidesList();
530 P
.end() == false; P
++)
531 CheckRConflicts(Pkg
,P
.ParentPkg().RevDependsList(),P
.ProvideVersion());
533 if (Install(Pkg
,FileNames
[Pkg
->ID
]) == false)
536 List
->Flag(Pkg
,pkgOrderList::UnPacked
,pkgOrderList::States
);
538 // Perform immedate configuration of the package.
539 if (List
->IsFlag(Pkg
,pkgOrderList::Immediate
) == true)
540 if (SmartConfigure(Pkg
) == false)
541 return _error
->Error("Internal Error, Could not perform immediate configuration (2) on %s",Pkg
.Name());
546 // PM::OrderInstall - Installation ordering routine /*{{{*/
547 // ---------------------------------------------------------------------
549 pkgPackageManager::OrderResult
pkgPackageManager::OrderInstall()
551 if (CreateOrderList() == false)
557 clog
<< "Begining to order" << endl
;
559 if (List
->OrderUnpack(FileNames
) == false)
561 _error
->Error("Internal ordering error");
566 clog
<< "Done ordering" << endl
;
568 bool DoneSomething
= false;
569 for (pkgOrderList::iterator I
= List
->begin(); I
!= List
->end(); I
++)
571 PkgIterator
Pkg(Cache
,*I
);
573 if (List
->IsNow(Pkg
) == false)
576 clog
<< "Skipping already done " << Pkg
.Name() << endl
;
580 if (List
->IsMissing(Pkg
) == true)
583 clog
<< "Sequence completed at " << Pkg
.Name() << endl
;
584 if (DoneSomething
== false)
586 _error
->Error("Internal Error, ordering was unable to handle the media swap");
593 if (Cache
[Pkg
].Keep() == true &&
594 Pkg
.State() == pkgCache::PkgIterator::NeedsNothing
&&
595 (Cache
[Pkg
].iFlags
& pkgDepCache::ReInstall
) != pkgDepCache::ReInstall
)
597 _error
->Error("Internal Error, trying to manipulate a kept package (%s)",Pkg
.Name());
601 // Perform a delete or an install
602 if (Cache
[Pkg
].Delete() == true)
604 if (SmartRemove(Pkg
) == false)
608 if (SmartUnPack(Pkg
) == false)
610 DoneSomething
= true;
613 // Final run through the configure phase
614 if (ConfigureAll() == false)
618 for (pkgOrderList::iterator I
= List
->begin(); I
!= List
->end(); I
++)
620 if (List
->IsFlag(*I
,pkgOrderList::Configured
) == false)
622 _error
->Error("Internal error, packages left unconfigured. %s",
623 PkgIterator(Cache
,*I
).Name());
631 // PM::DoInstall - Does the installation /*{{{*/
632 // ---------------------------------------------------------------------
633 /* This uses the filenames in FileNames and the information in the
634 DepCache to perform the installation of packages.*/
635 pkgPackageManager::OrderResult
pkgPackageManager::DoInstall(int statusFd
)
637 if(DoInstallPreFork() == Failed
)
640 return DoInstallPostFork(statusFd
);