]>
git.saurik.com Git - apt.git/blob - apt-pkg/packagemanager.cc
d5b8eafb4b19f19b0e3995e0070eba46a3ab8cad
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: packagemanager.cc,v 1.27 2001/05/07 05:49:43 jgg 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 pkgProblemResolver
Resolve(&Cache
);
99 List
->SetFileList(FileNames
);
102 for (PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
104 if (List
->IsMissing(I
) == false)
107 // Okay, this file is missing and we need it. Mark it for keep
112 // We have to empty the list otherwise it will not have the new changes
119 // Now downgrade everything that is broken
120 return Resolve
.ResolveByKeep() == true && Cache
.BrokenCount() == 0;
124 // PM::CreateOrderList - Create the ordering class /*{{{*/
125 // ---------------------------------------------------------------------
126 /* This populates the ordering list with all the packages that are
128 bool pkgPackageManager::CreateOrderList()
134 List
= new pkgOrderList(&Cache
);
136 bool NoImmConfigure
= _config
->FindB("APT::Immediate-Configure",false);
138 // Generate the list of affected packages and sort it
139 for (PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
141 // Mark the package and its dependends for immediate configuration
142 if (((I
->Flags
& pkgCache::Flag::Essential
) == pkgCache::Flag::Essential
||
143 (I
->Flags
& pkgCache::Flag::Important
) == pkgCache::Flag::Important
) &&
144 NoImmConfigure
== false)
146 List
->Flag(I
,pkgOrderList::Immediate
);
148 // Look for other packages to make immediate configurea
149 if (Cache
[I
].InstallVer
!= 0)
150 for (DepIterator D
= Cache
[I
].InstVerIter(Cache
).DependsList();
151 D
.end() == false; D
++)
152 if (D
->Type
== pkgCache::Dep::Depends
|| D
->Type
== pkgCache::Dep::PreDepends
)
153 List
->Flag(D
.TargetPkg(),pkgOrderList::Immediate
);
155 // And again with the current version.
156 if (I
->CurrentVer
!= 0)
157 for (DepIterator D
= I
.CurrentVer().DependsList();
158 D
.end() == false; D
++)
159 if (D
->Type
== pkgCache::Dep::Depends
|| D
->Type
== pkgCache::Dep::PreDepends
)
160 List
->Flag(D
.TargetPkg(),pkgOrderList::Immediate
);
164 if ((Cache
[I
].Keep() == true ||
165 Cache
[I
].InstVerIter(Cache
) == I
.CurrentVer()) &&
166 I
.State() == pkgCache::PkgIterator::NeedsNothing
&&
167 (Cache
[I
].iFlags
& pkgDepCache::ReInstall
) != pkgDepCache::ReInstall
&&
168 (I
.Purge() != false || Cache
[I
].Mode
!= pkgDepCache::ModeDelete
||
169 (Cache
[I
].iFlags
& pkgDepCache::Purge
) != pkgDepCache::Purge
))
172 // Append it to the list
179 // PM::DepAlwaysTrue - Returns true if this dep is irrelevent /*{{{*/
180 // ---------------------------------------------------------------------
181 /* The restriction on provides is to eliminate the case when provides
182 are transitioning between valid states [ie exim to smail] */
183 bool pkgPackageManager::DepAlwaysTrue(DepIterator D
)
185 if (D
.TargetPkg()->ProvidesList
!= 0)
188 if ((Cache
[D
] & pkgDepCache::DepInstall
) != 0 &&
189 (Cache
[D
] & pkgDepCache::DepNow
) != 0)
194 // PM::CheckRConflicts - Look for reverse conflicts /*{{{*/
195 // ---------------------------------------------------------------------
196 /* This looks over the reverses for a conflicts line that needs early
198 bool pkgPackageManager::CheckRConflicts(PkgIterator Pkg
,DepIterator D
,
201 for (;D
.end() == false; D
++)
203 if (D
->Type
!= pkgCache::Dep::Conflicts
&&
204 D
->Type
!= pkgCache::Dep::Obsoletes
)
207 // The package hasnt been changed
208 if (List
->IsNow(Pkg
) == false)
211 // Ignore self conflicts, ignore conflicts from irrelevent versions
212 if (D
.ParentPkg() == Pkg
|| D
.ParentVer() != D
.ParentPkg().CurrentVer())
215 if (Cache
.VS().CheckDep(Ver
,D
->CompareOp
,D
.TargetVer()) == false)
218 if (EarlyRemove(D
.ParentPkg()) == false)
219 return _error
->Error("Reverse conflicts early remove for package '%s' failed",
225 // PM::ConfigureAll - Run the all out configuration /*{{{*/
226 // ---------------------------------------------------------------------
227 /* This configures every package. It is assumed they are all unpacked and
228 that the final configuration is valid. */
229 bool pkgPackageManager::ConfigureAll()
231 pkgOrderList
OList(&Cache
);
233 // Populate the order list
234 for (pkgOrderList::iterator I
= List
->begin(); I
!= List
->end(); I
++)
235 if (List
->IsFlag(pkgCache::PkgIterator(Cache
,*I
),
236 pkgOrderList::UnPacked
) == true)
239 if (OList
.OrderConfigure() == false)
242 // Perform the configuring
243 for (pkgOrderList::iterator I
= OList
.begin(); I
!= OList
.end(); I
++)
245 PkgIterator
Pkg(Cache
,*I
);
247 if (Configure(Pkg
) == false)
250 List
->Flag(Pkg
,pkgOrderList::Configured
,pkgOrderList::States
);
256 // PM::SmartConfigure - Perform immediate configuration of the pkg /*{{{*/
257 // ---------------------------------------------------------------------
258 /* This routine scheduals the configuration of the given package and all
259 of it's dependents. */
260 bool pkgPackageManager::SmartConfigure(PkgIterator Pkg
)
262 pkgOrderList
OList(&Cache
);
264 if (DepAdd(OList
,Pkg
) == false)
267 if (OList
.OrderConfigure() == false)
270 // Perform the configuring
271 for (pkgOrderList::iterator I
= OList
.begin(); I
!= OList
.end(); I
++)
273 PkgIterator
Pkg(Cache
,*I
);
275 if (Configure(Pkg
) == false)
278 List
->Flag(Pkg
,pkgOrderList::Configured
,pkgOrderList::States
);
282 if (List
->IsFlag(Pkg
,pkgOrderList::Configured
) == false)
283 return _error
->Error("Internal error, could not immediate configure %s",Pkg
.Name());
288 // PM::DepAdd - Add all dependents to the oder list /*{{{*/
289 // ---------------------------------------------------------------------
290 /* This recursively adds all dependents to the order list */
291 bool pkgPackageManager::DepAdd(pkgOrderList
&OList
,PkgIterator Pkg
,int Depth
)
293 if (OList
.IsFlag(Pkg
,pkgOrderList::Added
) == true)
295 if (List
->IsFlag(Pkg
,pkgOrderList::Configured
) == true)
297 if (List
->IsFlag(Pkg
,pkgOrderList::UnPacked
) == false)
300 // Put the package on the list
301 OList
.push_back(Pkg
);
302 OList
.Flag(Pkg
,pkgOrderList::Added
);
305 // Check the dependencies to see if they are all satisfied.
307 for (DepIterator D
= Cache
[Pkg
].InstVerIter(Cache
).DependsList(); D
.end() == false;)
309 if (D
->Type
!= pkgCache::Dep::Depends
&& D
->Type
!= pkgCache::Dep::PreDepends
)
317 for (bool LastOR
= true; D
.end() == false && LastOR
== true; D
++)
319 LastOR
= (D
->CompareOp
& pkgCache::Dep::Or
) == pkgCache::Dep::Or
;
324 SPtrArray
<Version
*> VList
= D
.AllTargets();
325 for (Version
**I
= VList
; *I
!= 0 && Bad
== true; I
++)
327 VerIterator
Ver(Cache
,*I
);
328 PkgIterator Pkg
= Ver
.ParentPkg();
330 // See if the current version is ok
331 if (Pkg
.CurrentVer() == Ver
&& List
->IsNow(Pkg
) == true &&
332 Pkg
.State() == PkgIterator::NeedsNothing
)
338 // Not the install version
339 if (Cache
[Pkg
].InstallVer
!= *I
||
340 (Cache
[Pkg
].Keep() == true && Pkg
.State() == PkgIterator::NeedsNothing
))
343 if (List
->IsFlag(Pkg
,pkgOrderList::UnPacked
) == true)
344 Bad
= !DepAdd(OList
,Pkg
,Depth
);
345 if (List
->IsFlag(Pkg
,pkgOrderList::Configured
) == true)
352 OList
.Flag(Pkg
,0,pkgOrderList::Added
);
363 // PM::EarlyRemove - Perform removal of packages before their time /*{{{*/
364 // ---------------------------------------------------------------------
365 /* This is called to deal with conflicts arising from unpacking */
366 bool pkgPackageManager::EarlyRemove(PkgIterator Pkg
)
368 if (List
->IsNow(Pkg
) == false)
371 // Already removed it
372 if (List
->IsFlag(Pkg
,pkgOrderList::Removed
) == true)
375 // Woops, it will not be re-installed!
376 if (List
->IsFlag(Pkg
,pkgOrderList::InList
) == false)
379 // Essential packages get special treatment
380 bool IsEssential
= false;
381 if ((Pkg
->Flags
& pkgCache::Flag::Essential
) != 0)
384 /* Check for packages that are the dependents of essential packages and
386 if (Pkg
->CurrentVer
!= 0)
388 for (DepIterator D
= Pkg
.RevDependsList(); D
.end() == false &&
389 IsEssential
== false; D
++)
390 if (D
->Type
== pkgCache::Dep::Depends
|| D
->Type
== pkgCache::Dep::PreDepends
)
391 if ((D
.ParentPkg()->Flags
& pkgCache::Flag::Essential
) != 0)
395 if (IsEssential
== true)
397 if (_config
->FindB("APT::Force-LoopBreak",false) == false)
398 return _error
->Error(_("This installation run will require temporarily "
399 "removing the essential package %s due to a "
400 "Conflicts/Pre-Depends loop. This is often bad, "
401 "but if you really want to do it, activate the "
402 "APT::Force-LoopBreak option."),Pkg
.Name());
405 bool Res
= SmartRemove(Pkg
);
406 if (Cache
[Pkg
].Delete() == false)
407 List
->Flag(Pkg
,pkgOrderList::Removed
,pkgOrderList::States
);
412 // PM::SmartRemove - Removal Helper /*{{{*/
413 // ---------------------------------------------------------------------
415 bool pkgPackageManager::SmartRemove(PkgIterator Pkg
)
417 if (List
->IsNow(Pkg
) == false)
420 List
->Flag(Pkg
,pkgOrderList::Configured
,pkgOrderList::States
);
421 return Remove(Pkg
,(Cache
[Pkg
].iFlags
& pkgDepCache::Purge
) == pkgDepCache::Purge
);
424 // PM::SmartUnPack - Install helper /*{{{*/
425 // ---------------------------------------------------------------------
426 /* This performs the task of handling pre-depends. */
427 bool pkgPackageManager::SmartUnPack(PkgIterator Pkg
)
429 // Check if it is already unpacked
430 if (Pkg
.State() == pkgCache::PkgIterator::NeedsConfigure
&&
431 Cache
[Pkg
].Keep() == true)
433 List
->Flag(Pkg
,pkgOrderList::UnPacked
,pkgOrderList::States
);
434 if (List
->IsFlag(Pkg
,pkgOrderList::Immediate
) == true)
435 if (SmartConfigure(Pkg
) == false)
436 return _error
->Error("Internal Error, Could not perform immediate configuration (1) on %s",Pkg
.Name());
440 /* See if this packages install version has any predependencies
441 that are not met by 'now' packages. */
442 for (DepIterator D
= Cache
[Pkg
].InstVerIter(Cache
).DependsList();
445 // Compute a single dependency element (glob or)
446 pkgCache::DepIterator Start
;
447 pkgCache::DepIterator End
;
450 while (End
->Type
== pkgCache::Dep::PreDepends
)
452 // Look for possible ok targets.
453 SPtrArray
<Version
*> VList
= Start
.AllTargets();
455 for (Version
**I
= VList
; *I
!= 0 && Bad
== true; I
++)
457 VerIterator
Ver(Cache
,*I
);
458 PkgIterator Pkg
= Ver
.ParentPkg();
460 // See if the current version is ok
461 if (Pkg
.CurrentVer() == Ver
&& List
->IsNow(Pkg
) == true &&
462 Pkg
.State() == PkgIterator::NeedsNothing
)
469 // Look for something that could be configured.
470 for (Version
**I
= VList
; *I
!= 0 && Bad
== true; I
++)
472 VerIterator
Ver(Cache
,*I
);
473 PkgIterator Pkg
= Ver
.ParentPkg();
475 // Not the install version
476 if (Cache
[Pkg
].InstallVer
!= *I
||
477 (Cache
[Pkg
].Keep() == true && Pkg
.State() == PkgIterator::NeedsNothing
))
480 Bad
= !SmartConfigure(Pkg
);
483 /* If this or element did not match then continue on to the
484 next or element until a matching element is found*/
488 return _error
->Error("Internal Error, Couldn't configure a pre-depend");
495 if (End
->Type
== pkgCache::Dep::Conflicts
||
496 End
->Type
== pkgCache::Dep::Obsoletes
)
498 /* Look for conflicts. Two packages that are both in the install
499 state cannot conflict so we don't check.. */
500 SPtrArray
<Version
*> VList
= End
.AllTargets();
501 for (Version
**I
= VList
; *I
!= 0; I
++)
503 VerIterator
Ver(Cache
,*I
);
504 PkgIterator Pkg
= Ver
.ParentPkg();
506 // See if the current version is conflicting
507 if (Pkg
.CurrentVer() == Ver
&& List
->IsNow(Pkg
) == true)
509 if (EarlyRemove(Pkg
) == false)
510 return _error
->Error("Internal Error, Could not early remove %s",Pkg
.Name());
516 // Check for reverse conflicts.
517 if (CheckRConflicts(Pkg
,Pkg
.RevDependsList(),
518 Cache
[Pkg
].InstVerIter(Cache
).VerStr()) == false)
521 for (PrvIterator P
= Cache
[Pkg
].InstVerIter(Cache
).ProvidesList();
522 P
.end() == false; P
++)
523 CheckRConflicts(Pkg
,P
.ParentPkg().RevDependsList(),P
.ProvideVersion());
525 if (Install(Pkg
,FileNames
[Pkg
->ID
]) == false)
528 List
->Flag(Pkg
,pkgOrderList::UnPacked
,pkgOrderList::States
);
530 // Perform immedate configuration of the package.
531 if (List
->IsFlag(Pkg
,pkgOrderList::Immediate
) == true)
532 if (SmartConfigure(Pkg
) == false)
533 return _error
->Error("Internal Error, Could not perform immediate configuration (2) on %s",Pkg
.Name());
538 // PM::OrderInstall - Installation ordering routine /*{{{*/
539 // ---------------------------------------------------------------------
541 pkgPackageManager::OrderResult
pkgPackageManager::OrderInstall()
543 if (CreateOrderList() == false)
549 clog
<< "Begining to order" << endl
;
551 if (List
->OrderUnpack(FileNames
) == false)
553 _error
->Error("Internal ordering error");
558 clog
<< "Done ordering" << endl
;
560 bool DoneSomething
= false;
561 for (pkgOrderList::iterator I
= List
->begin(); I
!= List
->end(); I
++)
563 PkgIterator
Pkg(Cache
,*I
);
565 if (List
->IsNow(Pkg
) == false)
568 clog
<< "Skipping already done " << Pkg
.Name() << endl
;
572 if (List
->IsMissing(Pkg
) == true)
575 clog
<< "Sequence completed at " << Pkg
.Name() << endl
;
576 if (DoneSomething
== false)
578 _error
->Error("Internal Error, ordering was unable to handle the media swap");
585 if (Cache
[Pkg
].Keep() == true &&
586 Pkg
.State() == pkgCache::PkgIterator::NeedsNothing
&&
587 (Cache
[Pkg
].iFlags
& pkgDepCache::ReInstall
) != pkgDepCache::ReInstall
)
589 _error
->Error("Internal Error, trying to manipulate a kept package");
593 // Perform a delete or an install
594 if (Cache
[Pkg
].Delete() == true)
596 if (SmartRemove(Pkg
) == false)
600 if (SmartUnPack(Pkg
) == false)
602 DoneSomething
= true;
605 // Final run through the configure phase
606 if (ConfigureAll() == false)
610 for (pkgOrderList::iterator I
= List
->begin(); I
!= List
->end(); I
++)
612 if (List
->IsFlag(*I
,pkgOrderList::Configured
) == false)
614 _error
->Error("Internal error, packages left unconfigured. %s",
615 PkgIterator(Cache
,*I
).Name());
623 // PM::DoInstall - Does the installation /*{{{*/
624 // ---------------------------------------------------------------------
625 /* This uses the filenames in FileNames and the information in the
626 DepCache to perform the installation of packages.*/
627 pkgPackageManager::OrderResult
pkgPackageManager::DoInstall()
629 OrderResult Res
= OrderInstall();