]>
git.saurik.com Git - apt.git/blob - apt-pkg/packagemanager.cc
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: packagemanager.cc,v 1.18 1999/07/10 04:58: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
108 // Now downgrade everything that is broken
109 return Resolve
.ResolveByKeep() == true && Cache
.BrokenCount() == 0;
113 // PM::CreateOrderList - Create the ordering class /*{{{*/
114 // ---------------------------------------------------------------------
115 /* This populates the ordering list with all the packages that are
117 bool pkgPackageManager::CreateOrderList()
123 List
= new pkgOrderList(Cache
);
125 bool NoImmConfigure
= _config
->FindB("APT::Immediate-Configure",false);
127 // Generate the list of affected packages and sort it
128 for (PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
130 // Mark the package and its dependends for immediate configuration
131 if (((I
->Flags
& pkgCache::Flag::Essential
) == pkgCache::Flag::Essential
||
132 (I
->Flags
& pkgCache::Flag::Important
) == pkgCache::Flag::Important
) &&
133 NoImmConfigure
== false)
135 List
->Flag(I
,pkgOrderList::Immediate
);
137 // Look for other packages to make immediate configurea
138 if (Cache
[I
].InstallVer
!= 0)
139 for (DepIterator D
= Cache
[I
].InstVerIter(Cache
).DependsList();
140 D
.end() == false; D
++)
141 if (D
->Type
== pkgCache::Dep::Depends
|| D
->Type
== pkgCache::Dep::PreDepends
)
142 List
->Flag(D
.TargetPkg(),pkgOrderList::Immediate
);
144 // And again with the current version.
145 if (I
->CurrentVer
!= 0)
146 for (DepIterator D
= I
.CurrentVer().DependsList();
147 D
.end() == false; D
++)
148 if (D
->Type
== pkgCache::Dep::Depends
|| D
->Type
== pkgCache::Dep::PreDepends
)
149 List
->Flag(D
.TargetPkg(),pkgOrderList::Immediate
);
153 if ((Cache
[I
].Keep() == true ||
154 Cache
[I
].InstVerIter(Cache
) == I
.CurrentVer()) &&
155 I
.State() == pkgCache::PkgIterator::NeedsNothing
&&
156 (I
.Purge() != false || Cache
[I
].Mode
!= pkgDepCache::ModeDelete
||
157 (Cache
[I
].iFlags
& pkgDepCache::Purge
) != pkgDepCache::Purge
))
160 // Append it to the list
167 // PM::DepAlwaysTrue - Returns true if this dep is irrelevent /*{{{*/
168 // ---------------------------------------------------------------------
169 /* The restriction on provides is to eliminate the case when provides
170 are transitioning between valid states [ie exim to smail] */
171 bool pkgPackageManager::DepAlwaysTrue(DepIterator D
)
173 if (D
.TargetPkg()->ProvidesList
!= 0)
176 if ((Cache
[D
] & pkgDepCache::DepInstall
) != 0 &&
177 (Cache
[D
] & pkgDepCache::DepNow
) != 0)
182 // PM::CheckRConflicts - Look for reverse conflicts /*{{{*/
183 // ---------------------------------------------------------------------
184 /* This looks over the reverses for a conflicts line that needs early
186 bool pkgPackageManager::CheckRConflicts(PkgIterator Pkg
,DepIterator D
,
189 for (;D
.end() == false; D
++)
191 if (D
->Type
!= pkgCache::Dep::Conflicts
)
194 if (D
.ParentPkg() == Pkg
)
197 if (pkgCheckDep(D
.TargetVer(),Ver
,D
->CompareOp
) == false)
200 if (List
->IsNow(Pkg
) == false)
203 if (EarlyRemove(D
.ParentPkg()) == false)
209 // PM::ConfigureAll - Run the all out configuration /*{{{*/
210 // ---------------------------------------------------------------------
211 /* This configures every package. It is assumed they are all unpacked and
212 that the final configuration is valid. */
213 bool pkgPackageManager::ConfigureAll()
215 pkgOrderList
OList(Cache
);
217 // Populate the order list
218 for (pkgOrderList::iterator I
= List
->begin(); I
!= List
->end(); I
++)
219 if (List
->IsFlag(pkgCache::PkgIterator(Cache
,*I
),
220 pkgOrderList::UnPacked
) == true)
223 if (OList
.OrderConfigure() == false)
226 // Perform the configuring
227 for (pkgOrderList::iterator I
= OList
.begin(); I
!= OList
.end(); I
++)
229 PkgIterator
Pkg(Cache
,*I
);
231 if (Configure(Pkg
) == false)
234 List
->Flag(Pkg
,pkgOrderList::Configured
,pkgOrderList::States
);
240 // PM::SmartConfigure - Perform immediate configuration of the pkg /*{{{*/
241 // ---------------------------------------------------------------------
242 /* This routine scheduals the configuration of the given package and all
243 of it's dependents. */
244 bool pkgPackageManager::SmartConfigure(PkgIterator Pkg
)
246 pkgOrderList
OList(Cache
);
248 if (DepAdd(OList
,Pkg
) == false)
251 if (OList
.OrderConfigure() == false)
254 // Perform the configuring
255 for (pkgOrderList::iterator I
= OList
.begin(); I
!= OList
.end(); I
++)
257 PkgIterator
Pkg(Cache
,*I
);
259 if (Configure(Pkg
) == false)
262 List
->Flag(Pkg
,pkgOrderList::Configured
,pkgOrderList::States
);
266 if (List
->IsFlag(Pkg
,pkgOrderList::Configured
) == false)
267 return _error
->Error("Internal error, could not immediate configure %s",Pkg
.Name());
272 // PM::DepAdd - Add all dependents to the oder list /*{{{*/
273 // ---------------------------------------------------------------------
274 /* This recursively adds all dependents to the order list */
275 bool pkgPackageManager::DepAdd(pkgOrderList
&OList
,PkgIterator Pkg
,int Depth
)
277 if (OList
.IsFlag(Pkg
,pkgOrderList::Added
) == true)
279 if (List
->IsFlag(Pkg
,pkgOrderList::Configured
) == true)
281 if (List
->IsFlag(Pkg
,pkgOrderList::UnPacked
) == false)
285 // Put the package on the list
286 OList
.push_back(Pkg
);
287 OList
.Flag(Pkg
,pkgOrderList::Added
);
290 // Check the dependencies to see if they are all satisfied.
292 for (DepIterator D
= Cache
[Pkg
].InstVerIter(Cache
).DependsList(); D
.end() == false;)
294 if (D
->Type
!= pkgCache::Dep::Depends
&& D
->Type
!= pkgCache::Dep::PreDepends
)
302 for (bool LastOR
= true; D
.end() == false && LastOR
== true; D
++)
304 LastOR
= (D
->CompareOp
& pkgCache::Dep::Or
) == pkgCache::Dep::Or
;
309 Version
**VList
= D
.AllTargets();
310 for (Version
**I
= VList
; *I
!= 0 && Bad
== true; I
++)
312 VerIterator
Ver(Cache
,*I
);
313 PkgIterator Pkg
= Ver
.ParentPkg();
315 // See if the current version is ok
316 if (Pkg
.CurrentVer() == Ver
&& List
->IsNow(Pkg
) == true &&
317 Pkg
.State() == PkgIterator::NeedsNothing
)
323 // Not the install version
324 if (Cache
[Pkg
].InstallVer
!= *I
||
325 (Cache
[Pkg
].Keep() == true && Pkg
.State() == PkgIterator::NeedsNothing
))
327 if (List
->IsFlag(Pkg
,pkgOrderList::UnPacked
) == true)
328 Bad
= !DepAdd(OList
,Pkg
,Depth
);
329 if (List
->IsFlag(Pkg
,pkgOrderList::Configured
) == true)
337 OList
.Flag(Pkg
,0,pkgOrderList::Added
);
348 // PM::EarlyRemove - Perform removal of packages before their time /*{{{*/
349 // ---------------------------------------------------------------------
350 /* This is called to deal with conflicts arising from unpacking */
351 bool pkgPackageManager::EarlyRemove(PkgIterator Pkg
)
353 if (List
->IsNow(Pkg
) == false)
356 // Already removed it
357 if (List
->IsFlag(Pkg
,pkgOrderList::Removed
) == true)
360 // Woops, it will not be re-installed!
361 if (List
->IsFlag(Pkg
,pkgOrderList::InList
) == false)
364 bool Res
= SmartRemove(Pkg
);
365 if (Cache
[Pkg
].Delete() == false)
366 List
->Flag(Pkg
,pkgOrderList::Removed
,pkgOrderList::States
);
371 // PM::SmartRemove - Removal Helper /*{{{*/
372 // ---------------------------------------------------------------------
374 bool pkgPackageManager::SmartRemove(PkgIterator Pkg
)
376 if (List
->IsNow(Pkg
) == false)
379 List
->Flag(Pkg
,pkgOrderList::Configured
,pkgOrderList::States
);
380 return Remove(Pkg
,(Cache
[Pkg
].iFlags
& pkgDepCache::Purge
) == pkgDepCache::Purge
);
383 // PM::SmartUnPack - Install helper /*{{{*/
384 // ---------------------------------------------------------------------
385 /* This performs the task of handling pre-depends. */
386 bool pkgPackageManager::SmartUnPack(PkgIterator Pkg
)
388 // Check if it is already unpacked
389 if (Pkg
.State() == pkgCache::PkgIterator::NeedsConfigure
&&
390 Cache
[Pkg
].Keep() == true)
392 List
->Flag(Pkg
,pkgOrderList::UnPacked
,pkgOrderList::States
);
393 if (List
->IsFlag(Pkg
,pkgOrderList::Immediate
) == true)
394 if (SmartConfigure(Pkg
) == false)
395 return _error
->Error("Internal Error, Could not perform immediate configuraton");
399 /* See if this packages install version has any predependencies
400 that are not met by 'now' packages. */
401 for (DepIterator D
= Cache
[Pkg
].InstVerIter(Cache
).DependsList();
402 D
.end() == false; D
++)
404 if (D
->Type
== pkgCache::Dep::PreDepends
)
406 // Look for possible ok targets.
407 Version
**VList
= D
.AllTargets();
409 for (Version
**I
= VList
; *I
!= 0 && Bad
== true; I
++)
411 VerIterator
Ver(Cache
,*I
);
412 PkgIterator Pkg
= Ver
.ParentPkg();
414 // See if the current version is ok
415 if (Pkg
.CurrentVer() == Ver
&& List
->IsNow(Pkg
) == true &&
416 Pkg
.State() == PkgIterator::NeedsNothing
)
423 // Look for something that could be configured.
424 for (Version
**I
= VList
; *I
!= 0 && Bad
== true; I
++)
426 VerIterator
Ver(Cache
,*I
);
427 PkgIterator Pkg
= Ver
.ParentPkg();
429 // Not the install version
430 if (Cache
[Pkg
].InstallVer
!= *I
||
431 (Cache
[Pkg
].Keep() == true && Pkg
.State() == PkgIterator::NeedsNothing
))
434 Bad
= !SmartConfigure(Pkg
);
440 return _error
->Error("Internal Error, Couldn't configure a pre-depend");
445 if (D
->Type
== pkgCache::Dep::Conflicts
)
447 /* Look for conflicts. Two packages that are both in the install
448 state cannot conflict so we don't check.. */
449 Version
**VList
= D
.AllTargets();
450 for (Version
**I
= VList
; *I
!= 0; I
++)
452 VerIterator
Ver(Cache
,*I
);
453 PkgIterator Pkg
= Ver
.ParentPkg();
455 // See if the current version is conflicting
456 if (Pkg
.CurrentVer() == Ver
&& List
->IsNow(Pkg
) == true)
458 if (EarlyRemove(Pkg
) == false)
459 return _error
->Error("Internal Error, Could not early remove %s",Pkg
.Name());
466 // Check for reverse conflicts.
467 CheckRConflicts(Pkg
,Pkg
.RevDependsList(),
468 Cache
[Pkg
].InstVerIter(Cache
).VerStr());
469 for (PrvIterator P
= Cache
[Pkg
].InstVerIter(Cache
).ProvidesList();
470 P
.end() == false; P
++)
471 CheckRConflicts(Pkg
,P
.ParentPkg().RevDependsList(),P
.ProvideVersion());
473 if (Install(Pkg
,FileNames
[Pkg
->ID
]) == false)
476 List
->Flag(Pkg
,pkgOrderList::UnPacked
,pkgOrderList::States
);
478 // Perform immedate configuration of the package.
479 if (List
->IsFlag(Pkg
,pkgOrderList::Immediate
) == true)
480 if (SmartConfigure(Pkg
) == false)
481 return _error
->Error("Internal Error, Could not perform immediate configuraton");
486 // PM::OrderInstall - Installation ordering routine /*{{{*/
487 // ---------------------------------------------------------------------
489 pkgPackageManager::OrderResult
pkgPackageManager::OrderInstall()
491 if (CreateOrderList() == false)
497 clog
<< "Begining to order" << endl
;
499 if (List
->OrderUnpack(FileNames
) == false)
501 _error
->Error("Internal ordering error");
506 clog
<< "Done ordering" << endl
;
508 bool DoneSomething
= false;
509 for (pkgOrderList::iterator I
= List
->begin(); I
!= List
->end(); I
++)
511 PkgIterator
Pkg(Cache
,*I
);
513 if (List
->IsNow(Pkg
) == false)
516 clog
<< "Skipping already done " << Pkg
.Name() << endl
;
520 if (List
->IsMissing(Pkg
) == true)
523 clog
<< "Sequence completed at" << Pkg
.Name() << endl
;
524 if (DoneSomething
== false)
526 _error
->Error("Internal Error, ordering was unable to handle the media swap");
533 if (Cache
[Pkg
].Keep() == true && Pkg
.State() == pkgCache::PkgIterator::NeedsNothing
)
535 _error
->Error("Internal Error, trying to manipulate a kept package");
539 // Perform a delete or an install
540 if (Cache
[Pkg
].Delete() == true)
542 if (SmartRemove(Pkg
) == false)
546 if (SmartUnPack(Pkg
) == false)
548 DoneSomething
= true;
551 // Final run through the configure phase
552 if (ConfigureAll() == false)
556 for (pkgOrderList::iterator I
= List
->begin(); I
!= List
->end(); I
++)
558 if (List
->IsFlag(*I
,pkgOrderList::Configured
) == false)
560 _error
->Error("Internal error, packages left unconfigured. %s",
561 PkgIterator(Cache
,*I
).Name());
569 // PM::DoInstall - Does the installation /*{{{*/
570 // ---------------------------------------------------------------------
571 /* This uses the filenames in FileNames and the information in the
572 DepCache to perform the installation of packages.*/
573 pkgPackageManager::OrderResult
pkgPackageManager::DoInstall()
575 OrderResult Res
= OrderInstall();