]>
git.saurik.com Git - apt.git/blob - apt-pkg/packagemanager.cc
99710469b8903325351fad995bdd4fc1c2a709f9
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: packagemanager.cc,v 1.25 2000/05/12 04:26:42 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"
19 #include <apt-pkg/packagemanager.h>
20 #include <apt-pkg/orderlist.h>
21 #include <apt-pkg/depcache.h>
22 #include <apt-pkg/error.h>
23 #include <apt-pkg/version.h>
24 #include <apt-pkg/acquire-item.h>
25 #include <apt-pkg/algorithms.h>
26 #include <apt-pkg/configuration.h>
29 // PM::PackageManager - Constructor /*{{{*/
30 // ---------------------------------------------------------------------
32 pkgPackageManager::pkgPackageManager(pkgDepCache
&Cache
) : Cache(Cache
)
34 FileNames
= new string
[Cache
.Head().PackageCount
];
36 Debug
= _config
->FindB("Debug::pkgPackageManager",false);
39 // PM::PackageManager - Destructor /*{{{*/
40 // ---------------------------------------------------------------------
42 pkgPackageManager::~pkgPackageManager()
48 // PM::GetArchives - Queue the archives for download /*{{{*/
49 // ---------------------------------------------------------------------
51 bool pkgPackageManager::GetArchives(pkgAcquire
*Owner
,pkgSourceList
*Sources
,
54 if (CreateOrderList() == false)
57 if (List
->OrderUnpack() == false)
58 return _error
->Error("Internal ordering error");
60 for (pkgOrderList::iterator I
= List
->begin(); I
!= List
->end(); I
++)
62 PkgIterator
Pkg(Cache
,*I
);
63 FileNames
[Pkg
->ID
] = string();
65 // Skip packages to erase
66 if (Cache
[Pkg
].Delete() == true)
69 // Skip Packages that need configure only.
70 if (Pkg
.State() == pkgCache::PkgIterator::NeedsConfigure
&&
71 Cache
[Pkg
].Keep() == true)
74 // Skip already processed packages
75 if (List
->IsNow(Pkg
) == false)
78 new pkgAcqArchive(Owner
,Sources
,Recs
,Cache
[Pkg
].InstVerIter(Cache
),
85 // PM::FixMissing - Keep all missing packages /*{{{*/
86 // ---------------------------------------------------------------------
87 /* This is called to correct the installation when packages could not
89 bool pkgPackageManager::FixMissing()
91 pkgProblemResolver
Resolve(Cache
);
92 List
->SetFileList(FileNames
);
95 for (PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
97 if (List
->IsMissing(I
) == false)
100 // Okay, this file is missing and we need it. Mark it for keep
105 // We have to empty the list otherwise it will not have the new changes
112 // Now downgrade everything that is broken
113 return Resolve
.ResolveByKeep() == true && Cache
.BrokenCount() == 0;
117 // PM::CreateOrderList - Create the ordering class /*{{{*/
118 // ---------------------------------------------------------------------
119 /* This populates the ordering list with all the packages that are
121 bool pkgPackageManager::CreateOrderList()
127 List
= new pkgOrderList(Cache
);
129 bool NoImmConfigure
= _config
->FindB("APT::Immediate-Configure",false);
131 // Generate the list of affected packages and sort it
132 for (PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
134 // Mark the package and its dependends for immediate configuration
135 if (((I
->Flags
& pkgCache::Flag::Essential
) == pkgCache::Flag::Essential
||
136 (I
->Flags
& pkgCache::Flag::Important
) == pkgCache::Flag::Important
) &&
137 NoImmConfigure
== false)
139 List
->Flag(I
,pkgOrderList::Immediate
);
141 // Look for other packages to make immediate configurea
142 if (Cache
[I
].InstallVer
!= 0)
143 for (DepIterator D
= Cache
[I
].InstVerIter(Cache
).DependsList();
144 D
.end() == false; D
++)
145 if (D
->Type
== pkgCache::Dep::Depends
|| D
->Type
== pkgCache::Dep::PreDepends
)
146 List
->Flag(D
.TargetPkg(),pkgOrderList::Immediate
);
148 // And again with the current version.
149 if (I
->CurrentVer
!= 0)
150 for (DepIterator D
= I
.CurrentVer().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
);
157 if ((Cache
[I
].Keep() == true ||
158 Cache
[I
].InstVerIter(Cache
) == I
.CurrentVer()) &&
159 I
.State() == pkgCache::PkgIterator::NeedsNothing
&&
160 (Cache
[I
].iFlags
& pkgDepCache::ReInstall
) != pkgDepCache::ReInstall
&&
161 (I
.Purge() != false || Cache
[I
].Mode
!= pkgDepCache::ModeDelete
||
162 (Cache
[I
].iFlags
& pkgDepCache::Purge
) != pkgDepCache::Purge
))
165 // Append it to the list
172 // PM::DepAlwaysTrue - Returns true if this dep is irrelevent /*{{{*/
173 // ---------------------------------------------------------------------
174 /* The restriction on provides is to eliminate the case when provides
175 are transitioning between valid states [ie exim to smail] */
176 bool pkgPackageManager::DepAlwaysTrue(DepIterator D
)
178 if (D
.TargetPkg()->ProvidesList
!= 0)
181 if ((Cache
[D
] & pkgDepCache::DepInstall
) != 0 &&
182 (Cache
[D
] & pkgDepCache::DepNow
) != 0)
187 // PM::CheckRConflicts - Look for reverse conflicts /*{{{*/
188 // ---------------------------------------------------------------------
189 /* This looks over the reverses for a conflicts line that needs early
191 bool pkgPackageManager::CheckRConflicts(PkgIterator Pkg
,DepIterator D
,
194 for (;D
.end() == false; D
++)
196 if (D
->Type
!= pkgCache::Dep::Conflicts
)
199 // The package hasnt been changed
200 if (List
->IsNow(Pkg
) == false)
203 // Ignore self conflicts, ignore conflicts from irrelevent versions
204 if (D
.ParentPkg() == Pkg
|| D
.ParentVer() != D
.ParentPkg().CurrentVer())
207 if (pkgCheckDep(D
.TargetVer(),Ver
,D
->CompareOp
) == false)
210 if (EarlyRemove(D
.ParentPkg()) == false)
211 return _error
->Error("Reverse conflicts early remove for package '%s' failed",
217 // PM::ConfigureAll - Run the all out configuration /*{{{*/
218 // ---------------------------------------------------------------------
219 /* This configures every package. It is assumed they are all unpacked and
220 that the final configuration is valid. */
221 bool pkgPackageManager::ConfigureAll()
223 pkgOrderList
OList(Cache
);
225 // Populate the order list
226 for (pkgOrderList::iterator I
= List
->begin(); I
!= List
->end(); I
++)
227 if (List
->IsFlag(pkgCache::PkgIterator(Cache
,*I
),
228 pkgOrderList::UnPacked
) == true)
231 if (OList
.OrderConfigure() == false)
234 // Perform the configuring
235 for (pkgOrderList::iterator I
= OList
.begin(); I
!= OList
.end(); I
++)
237 PkgIterator
Pkg(Cache
,*I
);
239 if (Configure(Pkg
) == false)
242 List
->Flag(Pkg
,pkgOrderList::Configured
,pkgOrderList::States
);
248 // PM::SmartConfigure - Perform immediate configuration of the pkg /*{{{*/
249 // ---------------------------------------------------------------------
250 /* This routine scheduals the configuration of the given package and all
251 of it's dependents. */
252 bool pkgPackageManager::SmartConfigure(PkgIterator Pkg
)
254 pkgOrderList
OList(Cache
);
256 if (DepAdd(OList
,Pkg
) == false)
259 if (OList
.OrderConfigure() == false)
262 // Perform the configuring
263 for (pkgOrderList::iterator I
= OList
.begin(); I
!= OList
.end(); I
++)
265 PkgIterator
Pkg(Cache
,*I
);
267 if (Configure(Pkg
) == false)
270 List
->Flag(Pkg
,pkgOrderList::Configured
,pkgOrderList::States
);
274 if (List
->IsFlag(Pkg
,pkgOrderList::Configured
) == false)
275 return _error
->Error("Internal error, could not immediate configure %s",Pkg
.Name());
280 // PM::DepAdd - Add all dependents to the oder list /*{{{*/
281 // ---------------------------------------------------------------------
282 /* This recursively adds all dependents to the order list */
283 bool pkgPackageManager::DepAdd(pkgOrderList
&OList
,PkgIterator Pkg
,int Depth
)
285 if (OList
.IsFlag(Pkg
,pkgOrderList::Added
) == true)
287 if (List
->IsFlag(Pkg
,pkgOrderList::Configured
) == true)
289 if (List
->IsFlag(Pkg
,pkgOrderList::UnPacked
) == false)
293 // Put the package on the list
294 OList
.push_back(Pkg
);
295 OList
.Flag(Pkg
,pkgOrderList::Added
);
298 // Check the dependencies to see if they are all satisfied.
300 for (DepIterator D
= Cache
[Pkg
].InstVerIter(Cache
).DependsList(); D
.end() == false;)
302 if (D
->Type
!= pkgCache::Dep::Depends
&& D
->Type
!= pkgCache::Dep::PreDepends
)
310 for (bool LastOR
= true; D
.end() == false && LastOR
== true; D
++)
312 LastOR
= (D
->CompareOp
& pkgCache::Dep::Or
) == pkgCache::Dep::Or
;
317 Version
**VList
= D
.AllTargets();
318 for (Version
**I
= VList
; *I
!= 0 && Bad
== true; I
++)
320 VerIterator
Ver(Cache
,*I
);
321 PkgIterator Pkg
= Ver
.ParentPkg();
323 // See if the current version is ok
324 if (Pkg
.CurrentVer() == Ver
&& List
->IsNow(Pkg
) == true &&
325 Pkg
.State() == PkgIterator::NeedsNothing
)
331 // Not the install version
332 if (Cache
[Pkg
].InstallVer
!= *I
||
333 (Cache
[Pkg
].Keep() == true && Pkg
.State() == PkgIterator::NeedsNothing
))
335 if (List
->IsFlag(Pkg
,pkgOrderList::UnPacked
) == true)
336 Bad
= !DepAdd(OList
,Pkg
,Depth
);
337 if (List
->IsFlag(Pkg
,pkgOrderList::Configured
) == true)
345 OList
.Flag(Pkg
,0,pkgOrderList::Added
);
356 // PM::EarlyRemove - Perform removal of packages before their time /*{{{*/
357 // ---------------------------------------------------------------------
358 /* This is called to deal with conflicts arising from unpacking */
359 bool pkgPackageManager::EarlyRemove(PkgIterator Pkg
)
361 if (List
->IsNow(Pkg
) == false)
364 // Already removed it
365 if (List
->IsFlag(Pkg
,pkgOrderList::Removed
) == true)
368 // Woops, it will not be re-installed!
369 if (List
->IsFlag(Pkg
,pkgOrderList::InList
) == false)
372 // Essential packages get special treatment
373 bool IsEssential
= false;
374 if ((Pkg
->Flags
& pkgCache::Flag::Essential
) != 0)
377 /* Check for packages that are the dependents of essential packages and
379 if (Pkg
->CurrentVer
!= 0)
381 for (DepIterator D
= Pkg
.RevDependsList(); D
.end() == false &&
382 IsEssential
== false; D
++)
383 if (D
->Type
== pkgCache::Dep::Depends
|| D
->Type
== pkgCache::Dep::PreDepends
)
384 if ((D
.ParentPkg()->Flags
& pkgCache::Flag::Essential
) != 0)
388 if (IsEssential
== true)
390 if (_config
->FindB("APT::Force-LoopBreak",false) == false)
391 return _error
->Error("This installation run will require temporarily "
392 "removing the essential package %s due to a "
393 "Conflicts/Pre-Depends loop. This is often bad, "
394 "but if you really want to do it, activate the "
395 "APT::Force-LoopBreak option.",Pkg
.Name());
398 bool Res
= SmartRemove(Pkg
);
399 if (Cache
[Pkg
].Delete() == false)
400 List
->Flag(Pkg
,pkgOrderList::Removed
,pkgOrderList::States
);
405 // PM::SmartRemove - Removal Helper /*{{{*/
406 // ---------------------------------------------------------------------
408 bool pkgPackageManager::SmartRemove(PkgIterator Pkg
)
410 if (List
->IsNow(Pkg
) == false)
413 List
->Flag(Pkg
,pkgOrderList::Configured
,pkgOrderList::States
);
414 return Remove(Pkg
,(Cache
[Pkg
].iFlags
& pkgDepCache::Purge
) == pkgDepCache::Purge
);
417 // PM::SmartUnPack - Install helper /*{{{*/
418 // ---------------------------------------------------------------------
419 /* This performs the task of handling pre-depends. */
420 bool pkgPackageManager::SmartUnPack(PkgIterator Pkg
)
422 // Check if it is already unpacked
423 if (Pkg
.State() == pkgCache::PkgIterator::NeedsConfigure
&&
424 Cache
[Pkg
].Keep() == true)
426 List
->Flag(Pkg
,pkgOrderList::UnPacked
,pkgOrderList::States
);
427 if (List
->IsFlag(Pkg
,pkgOrderList::Immediate
) == true)
428 if (SmartConfigure(Pkg
) == false)
429 return _error
->Error("Internal Error, Could not perform immediate configuration");
433 /* See if this packages install version has any predependencies
434 that are not met by 'now' packages. */
435 for (DepIterator D
= Cache
[Pkg
].InstVerIter(Cache
).DependsList();
438 // Compute a single dependency element (glob or)
439 pkgCache::DepIterator Start
;
440 pkgCache::DepIterator End
;
443 while (End
->Type
== pkgCache::Dep::PreDepends
)
445 // Look for possible ok targets.
446 Version
**VList
= Start
.AllTargets();
448 for (Version
**I
= VList
; *I
!= 0 && Bad
== true; I
++)
450 VerIterator
Ver(Cache
,*I
);
451 PkgIterator Pkg
= Ver
.ParentPkg();
453 // See if the current version is ok
454 if (Pkg
.CurrentVer() == Ver
&& List
->IsNow(Pkg
) == true &&
455 Pkg
.State() == PkgIterator::NeedsNothing
)
462 // Look for something that could be configured.
463 for (Version
**I
= VList
; *I
!= 0 && Bad
== true; I
++)
465 VerIterator
Ver(Cache
,*I
);
466 PkgIterator Pkg
= Ver
.ParentPkg();
468 // Not the install version
469 if (Cache
[Pkg
].InstallVer
!= *I
||
470 (Cache
[Pkg
].Keep() == true && Pkg
.State() == PkgIterator::NeedsNothing
))
473 Bad
= !SmartConfigure(Pkg
);
478 /* If this or element did not match then continue on to the
479 next or element until a matching element is found*/
483 return _error
->Error("Internal Error, Couldn't configure a pre-depend");
490 if (End
->Type
== pkgCache::Dep::Conflicts
)
492 /* Look for conflicts. Two packages that are both in the install
493 state cannot conflict so we don't check.. */
494 Version
**VList
= End
.AllTargets();
495 for (Version
**I
= VList
; *I
!= 0; I
++)
497 VerIterator
Ver(Cache
,*I
);
498 PkgIterator Pkg
= Ver
.ParentPkg();
500 // See if the current version is conflicting
501 if (Pkg
.CurrentVer() == Ver
&& List
->IsNow(Pkg
) == true)
503 if (EarlyRemove(Pkg
) == false)
504 return _error
->Error("Internal Error, Could not early remove %s",Pkg
.Name());
511 // Check for reverse conflicts.
512 if (CheckRConflicts(Pkg
,Pkg
.RevDependsList(),
513 Cache
[Pkg
].InstVerIter(Cache
).VerStr()) == false)
516 for (PrvIterator P
= Cache
[Pkg
].InstVerIter(Cache
).ProvidesList();
517 P
.end() == false; P
++)
518 CheckRConflicts(Pkg
,P
.ParentPkg().RevDependsList(),P
.ProvideVersion());
520 if (Install(Pkg
,FileNames
[Pkg
->ID
]) == false)
523 List
->Flag(Pkg
,pkgOrderList::UnPacked
,pkgOrderList::States
);
525 // Perform immedate configuration of the package.
526 if (List
->IsFlag(Pkg
,pkgOrderList::Immediate
) == true)
527 if (SmartConfigure(Pkg
) == false)
528 return _error
->Error("Internal Error, Could not perform immediate configuration");
533 // PM::OrderInstall - Installation ordering routine /*{{{*/
534 // ---------------------------------------------------------------------
536 pkgPackageManager::OrderResult
pkgPackageManager::OrderInstall()
538 if (CreateOrderList() == false)
544 clog
<< "Begining to order" << endl
;
546 if (List
->OrderUnpack(FileNames
) == false)
548 _error
->Error("Internal ordering error");
553 clog
<< "Done ordering" << endl
;
555 bool DoneSomething
= false;
556 for (pkgOrderList::iterator I
= List
->begin(); I
!= List
->end(); I
++)
558 PkgIterator
Pkg(Cache
,*I
);
560 if (List
->IsNow(Pkg
) == false)
563 clog
<< "Skipping already done " << Pkg
.Name() << endl
;
567 if (List
->IsMissing(Pkg
) == true)
570 clog
<< "Sequence completed at " << Pkg
.Name() << endl
;
571 if (DoneSomething
== false)
573 _error
->Error("Internal Error, ordering was unable to handle the media swap");
580 if (Cache
[Pkg
].Keep() == true &&
581 Pkg
.State() == pkgCache::PkgIterator::NeedsNothing
&&
582 (Cache
[Pkg
].iFlags
& pkgDepCache::ReInstall
) != pkgDepCache::ReInstall
)
584 _error
->Error("Internal Error, trying to manipulate a kept package");
588 // Perform a delete or an install
589 if (Cache
[Pkg
].Delete() == true)
591 if (SmartRemove(Pkg
) == false)
595 if (SmartUnPack(Pkg
) == false)
597 DoneSomething
= true;
600 // Final run through the configure phase
601 if (ConfigureAll() == false)
605 for (pkgOrderList::iterator I
= List
->begin(); I
!= List
->end(); I
++)
607 if (List
->IsFlag(*I
,pkgOrderList::Configured
) == false)
609 _error
->Error("Internal error, packages left unconfigured. %s",
610 PkgIterator(Cache
,*I
).Name());
618 // PM::DoInstall - Does the installation /*{{{*/
619 // ---------------------------------------------------------------------
620 /* This uses the filenames in FileNames and the information in the
621 DepCache to perform the installation of packages.*/
622 pkgPackageManager::OrderResult
pkgPackageManager::DoInstall()
624 OrderResult Res
= OrderInstall();