]>
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::CreateOrderList - Create the ordering class /*{{{*/
122 // ---------------------------------------------------------------------
123 /* This populates the ordering list with all the packages that are
125 bool pkgPackageManager::CreateOrderList()
131 List
= new pkgOrderList(&Cache
);
133 bool NoImmConfigure
= !_config
->FindB("APT::Immediate-Configure",true);
135 // Generate the list of affected packages and sort it
136 for (PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
138 // Ignore no-version packages
139 if (I
->VersionList
== 0)
142 // Mark the package and its dependends for immediate configuration
143 if (((I
->Flags
& pkgCache::Flag::Essential
) == pkgCache::Flag::Essential
||
144 (I
->Flags
& pkgCache::Flag::Important
) == pkgCache::Flag::Important
) &&
145 NoImmConfigure
== false)
147 List
->Flag(I
,pkgOrderList::Immediate
);
149 // Look for other packages to make immediate configurea
150 if (Cache
[I
].InstallVer
!= 0)
151 for (DepIterator D
= Cache
[I
].InstVerIter(Cache
).DependsList();
152 D
.end() == false; D
++)
153 if (D
->Type
== pkgCache::Dep::Depends
|| D
->Type
== pkgCache::Dep::PreDepends
)
154 List
->Flag(D
.TargetPkg(),pkgOrderList::Immediate
);
156 // And again with the current version.
157 if (I
->CurrentVer
!= 0)
158 for (DepIterator D
= I
.CurrentVer().DependsList();
159 D
.end() == false; D
++)
160 if (D
->Type
== pkgCache::Dep::Depends
|| D
->Type
== pkgCache::Dep::PreDepends
)
161 List
->Flag(D
.TargetPkg(),pkgOrderList::Immediate
);
165 if ((Cache
[I
].Keep() == true ||
166 Cache
[I
].InstVerIter(Cache
) == I
.CurrentVer()) &&
167 I
.State() == pkgCache::PkgIterator::NeedsNothing
&&
168 (Cache
[I
].iFlags
& pkgDepCache::ReInstall
) != pkgDepCache::ReInstall
&&
169 (I
.Purge() != false || Cache
[I
].Mode
!= pkgDepCache::ModeDelete
||
170 (Cache
[I
].iFlags
& pkgDepCache::Purge
) != pkgDepCache::Purge
))
173 // Append it to the list
180 // PM::DepAlwaysTrue - Returns true if this dep is irrelevent /*{{{*/
181 // ---------------------------------------------------------------------
182 /* The restriction on provides is to eliminate the case when provides
183 are transitioning between valid states [ie exim to smail] */
184 bool pkgPackageManager::DepAlwaysTrue(DepIterator D
)
186 if (D
.TargetPkg()->ProvidesList
!= 0)
189 if ((Cache
[D
] & pkgDepCache::DepInstall
) != 0 &&
190 (Cache
[D
] & pkgDepCache::DepNow
) != 0)
195 // PM::CheckRConflicts - Look for reverse conflicts /*{{{*/
196 // ---------------------------------------------------------------------
197 /* This looks over the reverses for a conflicts line that needs early
199 bool pkgPackageManager::CheckRConflicts(PkgIterator Pkg
,DepIterator D
,
202 for (;D
.end() == false; D
++)
204 if (D
->Type
!= pkgCache::Dep::Conflicts
&&
205 D
->Type
!= pkgCache::Dep::Obsoletes
)
208 // The package hasnt been changed
209 if (List
->IsNow(Pkg
) == false)
212 // Ignore self conflicts, ignore conflicts from irrelevent versions
213 if (D
.ParentPkg() == Pkg
|| D
.ParentVer() != D
.ParentPkg().CurrentVer())
216 if (Cache
.VS().CheckDep(Ver
,D
->CompareOp
,D
.TargetVer()) == false)
219 if (EarlyRemove(D
.ParentPkg()) == false)
220 return _error
->Error("Reverse conflicts early remove for package '%s' failed",
226 // PM::ConfigureAll - Run the all out configuration /*{{{*/
227 // ---------------------------------------------------------------------
228 /* This configures every package. It is assumed they are all unpacked and
229 that the final configuration is valid. */
230 bool pkgPackageManager::ConfigureAll()
232 pkgOrderList
OList(&Cache
);
234 // Populate the order list
235 for (pkgOrderList::iterator I
= List
->begin(); I
!= List
->end(); I
++)
236 if (List
->IsFlag(pkgCache::PkgIterator(Cache
,*I
),
237 pkgOrderList::UnPacked
) == true)
240 if (OList
.OrderConfigure() == false)
243 // Perform the configuring
244 for (pkgOrderList::iterator I
= OList
.begin(); I
!= OList
.end(); I
++)
246 PkgIterator
Pkg(Cache
,*I
);
248 if (Configure(Pkg
) == false)
251 List
->Flag(Pkg
,pkgOrderList::Configured
,pkgOrderList::States
);
257 // PM::SmartConfigure - Perform immediate configuration of the pkg /*{{{*/
258 // ---------------------------------------------------------------------
259 /* This routine scheduals the configuration of the given package and all
260 of it's dependents. */
261 bool pkgPackageManager::SmartConfigure(PkgIterator Pkg
)
263 pkgOrderList
OList(&Cache
);
265 if (DepAdd(OList
,Pkg
) == false)
268 if (OList
.OrderConfigure() == false)
271 // Perform the configuring
272 for (pkgOrderList::iterator I
= OList
.begin(); I
!= OList
.end(); I
++)
274 PkgIterator
Pkg(Cache
,*I
);
276 if (Configure(Pkg
) == false)
279 List
->Flag(Pkg
,pkgOrderList::Configured
,pkgOrderList::States
);
283 if (List
->IsFlag(Pkg
,pkgOrderList::Configured
) == false)
284 return _error
->Error("Internal error, could not immediate configure %s",Pkg
.Name());
289 // PM::DepAdd - Add all dependents to the oder list /*{{{*/
290 // ---------------------------------------------------------------------
291 /* This recursively adds all dependents to the order list */
292 bool pkgPackageManager::DepAdd(pkgOrderList
&OList
,PkgIterator Pkg
,int Depth
)
294 if (OList
.IsFlag(Pkg
,pkgOrderList::Added
) == true)
296 if (List
->IsFlag(Pkg
,pkgOrderList::Configured
) == true)
298 if (List
->IsFlag(Pkg
,pkgOrderList::UnPacked
) == false)
301 // Put the package on the list
302 OList
.push_back(Pkg
);
303 OList
.Flag(Pkg
,pkgOrderList::Added
);
306 // Check the dependencies to see if they are all satisfied.
308 for (DepIterator D
= Cache
[Pkg
].InstVerIter(Cache
).DependsList(); D
.end() == false;)
310 if (D
->Type
!= pkgCache::Dep::Depends
&& D
->Type
!= pkgCache::Dep::PreDepends
)
318 for (bool LastOR
= true; D
.end() == false && LastOR
== true; D
++)
320 LastOR
= (D
->CompareOp
& pkgCache::Dep::Or
) == pkgCache::Dep::Or
;
325 SPtrArray
<Version
*> VList
= D
.AllTargets();
326 for (Version
**I
= VList
; *I
!= 0 && Bad
== true; I
++)
328 VerIterator
Ver(Cache
,*I
);
329 PkgIterator Pkg
= Ver
.ParentPkg();
331 // See if the current version is ok
332 if (Pkg
.CurrentVer() == Ver
&& List
->IsNow(Pkg
) == true &&
333 Pkg
.State() == PkgIterator::NeedsNothing
)
339 // Not the install version
340 if (Cache
[Pkg
].InstallVer
!= *I
||
341 (Cache
[Pkg
].Keep() == true && Pkg
.State() == PkgIterator::NeedsNothing
))
344 if (List
->IsFlag(Pkg
,pkgOrderList::UnPacked
) == true)
345 Bad
= !DepAdd(OList
,Pkg
,Depth
);
346 if (List
->IsFlag(Pkg
,pkgOrderList::Configured
) == true)
353 OList
.Flag(Pkg
,0,pkgOrderList::Added
);
364 // PM::EarlyRemove - Perform removal of packages before their time /*{{{*/
365 // ---------------------------------------------------------------------
366 /* This is called to deal with conflicts arising from unpacking */
367 bool pkgPackageManager::EarlyRemove(PkgIterator Pkg
)
369 if (List
->IsNow(Pkg
) == false)
372 // Already removed it
373 if (List
->IsFlag(Pkg
,pkgOrderList::Removed
) == true)
376 // Woops, it will not be re-installed!
377 if (List
->IsFlag(Pkg
,pkgOrderList::InList
) == false)
380 // Essential packages get special treatment
381 bool IsEssential
= false;
382 if ((Pkg
->Flags
& pkgCache::Flag::Essential
) != 0)
385 /* Check for packages that are the dependents of essential packages and
387 if (Pkg
->CurrentVer
!= 0)
389 for (DepIterator D
= Pkg
.RevDependsList(); D
.end() == false &&
390 IsEssential
== false; D
++)
391 if (D
->Type
== pkgCache::Dep::Depends
|| D
->Type
== pkgCache::Dep::PreDepends
)
392 if ((D
.ParentPkg()->Flags
& pkgCache::Flag::Essential
) != 0)
396 if (IsEssential
== true)
398 if (_config
->FindB("APT::Force-LoopBreak",false) == false)
399 return _error
->Error(_("This installation run will require temporarily "
400 "removing the essential package %s due to a "
401 "Conflicts/Pre-Depends loop. This is often bad, "
402 "but if you really want to do it, activate the "
403 "APT::Force-LoopBreak option."),Pkg
.Name());
406 bool Res
= SmartRemove(Pkg
);
407 if (Cache
[Pkg
].Delete() == false)
408 List
->Flag(Pkg
,pkgOrderList::Removed
,pkgOrderList::States
);
413 // PM::SmartRemove - Removal Helper /*{{{*/
414 // ---------------------------------------------------------------------
416 bool pkgPackageManager::SmartRemove(PkgIterator Pkg
)
418 if (List
->IsNow(Pkg
) == false)
421 List
->Flag(Pkg
,pkgOrderList::Configured
,pkgOrderList::States
);
422 return Remove(Pkg
,(Cache
[Pkg
].iFlags
& pkgDepCache::Purge
) == pkgDepCache::Purge
);
425 // PM::SmartUnPack - Install helper /*{{{*/
426 // ---------------------------------------------------------------------
427 /* This performs the task of handling pre-depends. */
428 bool pkgPackageManager::SmartUnPack(PkgIterator Pkg
)
430 // Check if it is already unpacked
431 if (Pkg
.State() == pkgCache::PkgIterator::NeedsConfigure
&&
432 Cache
[Pkg
].Keep() == true)
434 List
->Flag(Pkg
,pkgOrderList::UnPacked
,pkgOrderList::States
);
435 if (List
->IsFlag(Pkg
,pkgOrderList::Immediate
) == true)
436 if (SmartConfigure(Pkg
) == false)
437 return _error
->Error("Internal Error, Could not perform immediate configuration (1) on %s",Pkg
.Name());
441 /* See if this packages install version has any predependencies
442 that are not met by 'now' packages. */
443 for (DepIterator D
= Cache
[Pkg
].InstVerIter(Cache
).DependsList();
446 // Compute a single dependency element (glob or)
447 pkgCache::DepIterator Start
;
448 pkgCache::DepIterator End
;
451 while (End
->Type
== pkgCache::Dep::PreDepends
)
453 // Look for possible ok targets.
454 SPtrArray
<Version
*> VList
= Start
.AllTargets();
456 for (Version
**I
= VList
; *I
!= 0 && Bad
== true; I
++)
458 VerIterator
Ver(Cache
,*I
);
459 PkgIterator Pkg
= Ver
.ParentPkg();
461 // See if the current version is ok
462 if (Pkg
.CurrentVer() == Ver
&& List
->IsNow(Pkg
) == true &&
463 Pkg
.State() == PkgIterator::NeedsNothing
)
470 // Look for something that could be configured.
471 for (Version
**I
= VList
; *I
!= 0 && Bad
== true; I
++)
473 VerIterator
Ver(Cache
,*I
);
474 PkgIterator Pkg
= Ver
.ParentPkg();
476 // Not the install version
477 if (Cache
[Pkg
].InstallVer
!= *I
||
478 (Cache
[Pkg
].Keep() == true && Pkg
.State() == PkgIterator::NeedsNothing
))
481 Bad
= !SmartConfigure(Pkg
);
484 /* If this or element did not match then continue on to the
485 next or element until a matching element is found */
488 // This triggers if someone make a pre-depends/depend loop.
490 return _error
->Error("Couldn't configure pre-depend %s for %s, "
491 "probably a dependency cycle.",
492 End
.TargetPkg().Name(),Pkg
.Name());
499 if (End
->Type
== pkgCache::Dep::Conflicts
||
500 End
->Type
== pkgCache::Dep::Obsoletes
)
502 /* Look for conflicts. Two packages that are both in the install
503 state cannot conflict so we don't check.. */
504 SPtrArray
<Version
*> VList
= End
.AllTargets();
505 for (Version
**I
= VList
; *I
!= 0; I
++)
507 VerIterator
Ver(Cache
,*I
);
508 PkgIterator Pkg
= Ver
.ParentPkg();
510 // See if the current version is conflicting
511 if (Pkg
.CurrentVer() == Ver
&& List
->IsNow(Pkg
) == true)
513 if (EarlyRemove(Pkg
) == false)
514 return _error
->Error("Internal Error, Could not early remove %s",Pkg
.Name());
520 // Check for reverse conflicts.
521 if (CheckRConflicts(Pkg
,Pkg
.RevDependsList(),
522 Cache
[Pkg
].InstVerIter(Cache
).VerStr()) == false)
525 for (PrvIterator P
= Cache
[Pkg
].InstVerIter(Cache
).ProvidesList();
526 P
.end() == false; P
++)
527 CheckRConflicts(Pkg
,P
.ParentPkg().RevDependsList(),P
.ProvideVersion());
529 if (Install(Pkg
,FileNames
[Pkg
->ID
]) == false)
532 List
->Flag(Pkg
,pkgOrderList::UnPacked
,pkgOrderList::States
);
534 // Perform immedate configuration of the package.
535 if (List
->IsFlag(Pkg
,pkgOrderList::Immediate
) == true)
536 if (SmartConfigure(Pkg
) == false)
537 return _error
->Error("Internal Error, Could not perform immediate configuration (2) on %s",Pkg
.Name());
542 // PM::OrderInstall - Installation ordering routine /*{{{*/
543 // ---------------------------------------------------------------------
545 pkgPackageManager::OrderResult
pkgPackageManager::OrderInstall()
547 if (CreateOrderList() == false)
553 clog
<< "Begining to order" << endl
;
555 if (List
->OrderUnpack(FileNames
) == false)
557 _error
->Error("Internal ordering error");
562 clog
<< "Done ordering" << endl
;
564 bool DoneSomething
= false;
565 for (pkgOrderList::iterator I
= List
->begin(); I
!= List
->end(); I
++)
567 PkgIterator
Pkg(Cache
,*I
);
569 if (List
->IsNow(Pkg
) == false)
572 clog
<< "Skipping already done " << Pkg
.Name() << endl
;
576 if (List
->IsMissing(Pkg
) == true)
579 clog
<< "Sequence completed at " << Pkg
.Name() << endl
;
580 if (DoneSomething
== false)
582 _error
->Error("Internal Error, ordering was unable to handle the media swap");
589 if (Cache
[Pkg
].Keep() == true &&
590 Pkg
.State() == pkgCache::PkgIterator::NeedsNothing
&&
591 (Cache
[Pkg
].iFlags
& pkgDepCache::ReInstall
) != pkgDepCache::ReInstall
)
593 _error
->Error("Internal Error, trying to manipulate a kept package (%s)",Pkg
.Name());
597 // Perform a delete or an install
598 if (Cache
[Pkg
].Delete() == true)
600 if (SmartRemove(Pkg
) == false)
604 if (SmartUnPack(Pkg
) == false)
606 DoneSomething
= true;
609 // Final run through the configure phase
610 if (ConfigureAll() == false)
614 for (pkgOrderList::iterator I
= List
->begin(); I
!= List
->end(); I
++)
616 if (List
->IsFlag(*I
,pkgOrderList::Configured
) == false)
618 _error
->Error("Internal error, packages left unconfigured. %s",
619 PkgIterator(Cache
,*I
).Name());
627 // PM::DoInstall - Does the installation /*{{{*/
628 // ---------------------------------------------------------------------
629 /* This uses the filenames in FileNames and the information in the
630 DepCache to perform the installation of packages.*/
631 pkgPackageManager::OrderResult
pkgPackageManager::DoInstall(int statusFd
)
633 if(DoInstallPreFork() == Failed
)
636 return DoInstallPostFork(statusFd
);