]>
git.saurik.com Git - apt.git/blob - apt-pkg/packagemanager.cc
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: packagemanager.cc,v 1.15 1999/07/03 03:10:35 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
);
94 for (PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
96 // These don't need files
97 if (Cache
[I
].Keep() == true)
99 if (Cache
[I
].Delete() == true)
102 // We have a filename
103 if (FileNames
[I
->ID
].empty() == false)
106 // Skip Packages that need configure only.
107 if (I
.State() == pkgCache::PkgIterator::NeedsConfigure
&&
108 Cache
[I
].Keep() == true)
111 // Okay, this file is missing and we need it. Mark it for keep
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
)
169 // Append it to the list
176 // PM::DepAlwaysTrue - Returns true if this dep is irrelevent /*{{{*/
177 // ---------------------------------------------------------------------
178 /* The restriction on provides is to eliminate the case when provides
179 are transitioning between valid states [ie exim to smail] */
180 bool pkgPackageManager::DepAlwaysTrue(DepIterator D
)
182 if (D
.TargetPkg()->ProvidesList
!= 0)
185 if ((Cache
[D
] & pkgDepCache::DepInstall
) != 0 &&
186 (Cache
[D
] & pkgDepCache::DepNow
) != 0)
191 // PM::CheckRConflicts - Look for reverse conflicts /*{{{*/
192 // ---------------------------------------------------------------------
193 /* This looks over the reverses for a conflicts line that needs early
195 bool pkgPackageManager::CheckRConflicts(PkgIterator Pkg
,DepIterator D
,
198 for (;D
.end() == false; D
++)
200 if (D
->Type
!= pkgCache::Dep::Conflicts
)
203 if (D
.ParentPkg() == Pkg
)
206 if (pkgCheckDep(D
.TargetVer(),Ver
,D
->CompareOp
) == false)
209 if (List
->IsNow(Pkg
) == false)
212 if (EarlyRemove(D
.ParentPkg()) == false)
218 // PM::ConfigureAll - Run the all out configuration /*{{{*/
219 // ---------------------------------------------------------------------
220 /* This configures every package. It is assumed they are all unpacked and
221 that the final configuration is valid. */
222 bool pkgPackageManager::ConfigureAll()
224 pkgOrderList
OList(Cache
);
226 // Populate the order list
227 for (pkgOrderList::iterator I
= List
->begin(); I
!= List
->end(); I
++)
228 if (List
->IsFlag(pkgCache::PkgIterator(Cache
,*I
),
229 pkgOrderList::UnPacked
) == true)
232 if (OList
.OrderConfigure() == false)
235 // Perform the configuring
236 for (pkgOrderList::iterator I
= OList
.begin(); I
!= OList
.end(); I
++)
238 PkgIterator
Pkg(Cache
,*I
);
240 if (Configure(Pkg
) == false)
243 List
->Flag(Pkg
,pkgOrderList::Configured
,pkgOrderList::States
);
249 // PM::SmartConfigure - Perform immediate configuration of the pkg /*{{{*/
250 // ---------------------------------------------------------------------
251 /* This routine scheduals the configuration of the given package and all
252 of it's dependents. */
253 bool pkgPackageManager::SmartConfigure(PkgIterator Pkg
)
255 pkgOrderList
OList(Cache
);
257 if (DepAdd(OList
,Pkg
) == false)
260 if (OList
.OrderConfigure() == false)
263 // Perform the configuring
264 for (pkgOrderList::iterator I
= OList
.begin(); I
!= OList
.end(); I
++)
266 PkgIterator
Pkg(Cache
,*I
);
268 if (Configure(Pkg
) == false)
271 List
->Flag(Pkg
,pkgOrderList::Configured
,pkgOrderList::States
);
275 if (List
->IsFlag(Pkg
,pkgOrderList::Configured
) == false)
276 return _error
->Error("Internal error, could not immediate configure %s",Pkg
.Name());
281 // PM::DepAdd - Add all dependents to the oder list /*{{{*/
282 // ---------------------------------------------------------------------
283 /* This recursively adds all dependents to the order list */
284 bool pkgPackageManager::DepAdd(pkgOrderList
&OList
,PkgIterator Pkg
,int Depth
)
286 if (OList
.IsFlag(Pkg
,pkgOrderList::Added
) == true)
288 if (List
->IsFlag(Pkg
,pkgOrderList::Configured
) == true)
290 if (List
->IsFlag(Pkg
,pkgOrderList::UnPacked
) == false)
294 // Put the package on the list
295 OList
.push_back(Pkg
);
296 OList
.Flag(Pkg
,pkgOrderList::Added
);
299 // Check the dependencies to see if they are all satisfied.
301 for (DepIterator D
= Cache
[Pkg
].InstVerIter(Cache
).DependsList(); D
.end() == false;)
303 if (D
->Type
!= pkgCache::Dep::Depends
&& D
->Type
!= pkgCache::Dep::PreDepends
)
311 for (bool LastOR
= true; D
.end() == false && LastOR
== true; D
++)
313 LastOR
= (D
->CompareOp
& pkgCache::Dep::Or
) == pkgCache::Dep::Or
;
318 Version
**VList
= D
.AllTargets();
319 for (Version
**I
= VList
; *I
!= 0 && Bad
== true; I
++)
321 VerIterator
Ver(Cache
,*I
);
322 PkgIterator Pkg
= Ver
.ParentPkg();
324 // See if the current version is ok
325 if (Pkg
.CurrentVer() == Ver
&& List
->IsNow(Pkg
) == true &&
326 Pkg
.State() == PkgIterator::NeedsNothing
)
332 // Not the install version
333 if (Cache
[Pkg
].InstallVer
!= *I
||
334 (Cache
[Pkg
].Keep() == true && Pkg
.State() == PkgIterator::NeedsNothing
))
336 if (List
->IsFlag(Pkg
,pkgOrderList::UnPacked
) == true)
337 Bad
= !DepAdd(OList
,Pkg
,Depth
);
338 if (List
->IsFlag(Pkg
,pkgOrderList::Configured
) == true)
346 OList
.Flag(Pkg
,0,pkgOrderList::Added
);
357 // PM::EarlyRemove - Perform removal of packages before their time /*{{{*/
358 // ---------------------------------------------------------------------
359 /* This is called to deal with conflicts arising from unpacking */
360 bool pkgPackageManager::EarlyRemove(PkgIterator Pkg
)
362 if (List
->IsNow(Pkg
) == false)
365 // Already removed it
366 if (List
->IsFlag(Pkg
,pkgOrderList::Removed
) == true)
369 // Woops, it will not be re-installed!
370 if (List
->IsFlag(Pkg
,pkgOrderList::InList
) == false)
373 bool Res
= SmartRemove(Pkg
);
374 if (Cache
[Pkg
].Delete() == false)
375 List
->Flag(Pkg
,pkgOrderList::Removed
,pkgOrderList::States
);
380 // PM::SmartRemove - Removal Helper /*{{{*/
381 // ---------------------------------------------------------------------
383 bool pkgPackageManager::SmartRemove(PkgIterator Pkg
)
385 if (List
->IsNow(Pkg
) == false)
388 List
->Flag(Pkg
,pkgOrderList::Configured
,pkgOrderList::States
);
392 // PM::SmartUnPack - Install helper /*{{{*/
393 // ---------------------------------------------------------------------
394 /* This performs the task of handling pre-depends. */
395 bool pkgPackageManager::SmartUnPack(PkgIterator Pkg
)
397 // Check if it is already unpacked
398 if (Pkg
.State() == pkgCache::PkgIterator::NeedsConfigure
&&
399 Cache
[Pkg
].Keep() == true)
401 List
->Flag(Pkg
,pkgOrderList::UnPacked
,pkgOrderList::States
);
402 if (List
->IsFlag(Pkg
,pkgOrderList::Immediate
) == true)
403 if (SmartConfigure(Pkg
) == false)
404 return _error
->Error("Internal Error, Could not perform immediate configuraton");
408 /* See if this packages install version has any predependencies
409 that are not met by 'now' packages. */
410 for (DepIterator D
= Cache
[Pkg
].InstVerIter(Cache
).DependsList();
411 D
.end() == false; D
++)
413 if (D
->Type
== pkgCache::Dep::PreDepends
)
415 // Look for possible ok targets.
416 Version
**VList
= D
.AllTargets();
418 for (Version
**I
= VList
; *I
!= 0 && Bad
== true; I
++)
420 VerIterator
Ver(Cache
,*I
);
421 PkgIterator Pkg
= Ver
.ParentPkg();
423 // See if the current version is ok
424 if (Pkg
.CurrentVer() == Ver
&& List
->IsNow(Pkg
) == true &&
425 Pkg
.State() == PkgIterator::NeedsNothing
)
432 // Look for something that could be configured.
433 for (Version
**I
= VList
; *I
!= 0 && Bad
== true; I
++)
435 VerIterator
Ver(Cache
,*I
);
436 PkgIterator Pkg
= Ver
.ParentPkg();
438 // Not the install version
439 if (Cache
[Pkg
].InstallVer
!= *I
||
440 (Cache
[Pkg
].Keep() == true && Pkg
.State() == PkgIterator::NeedsNothing
))
443 Bad
= !SmartConfigure(Pkg
);
449 return _error
->Error("Internal Error, Couldn't configure a pre-depend");
454 if (D
->Type
== pkgCache::Dep::Conflicts
)
456 /* Look for conflicts. Two packages that are both in the install
457 state cannot conflict so we don't check.. */
458 Version
**VList
= D
.AllTargets();
459 for (Version
**I
= VList
; *I
!= 0; I
++)
461 VerIterator
Ver(Cache
,*I
);
462 PkgIterator Pkg
= Ver
.ParentPkg();
464 // See if the current version is conflicting
465 if (Pkg
.CurrentVer() == Ver
&& List
->IsNow(Pkg
) == true)
467 if (EarlyRemove(Pkg
) == false)
468 return _error
->Error("Internal Error, Could not early remove %s",Pkg
.Name());
475 // Check for reverse conflicts.
476 CheckRConflicts(Pkg
,Pkg
.RevDependsList(),
477 Cache
[Pkg
].InstVerIter(Cache
).VerStr());
478 for (PrvIterator P
= Cache
[Pkg
].InstVerIter(Cache
).ProvidesList();
479 P
.end() == false; P
++)
480 CheckRConflicts(Pkg
,P
.ParentPkg().RevDependsList(),P
.ProvideVersion());
482 if (Install(Pkg
,FileNames
[Pkg
->ID
]) == false)
485 List
->Flag(Pkg
,pkgOrderList::UnPacked
,pkgOrderList::States
);
487 // Perform immedate configuration of the package.
488 if (List
->IsFlag(Pkg
,pkgOrderList::Immediate
) == true)
489 if (SmartConfigure(Pkg
) == false)
490 return _error
->Error("Internal Error, Could not perform immediate configuraton");
495 // PM::OrderInstall - Installation ordering routine /*{{{*/
496 // ---------------------------------------------------------------------
498 pkgPackageManager::OrderResult
pkgPackageManager::OrderInstall()
500 if (CreateOrderList() == false)
506 clog
<< "Begining to order" << endl
;
508 if (List
->OrderUnpack(FileNames
) == false)
510 _error
->Error("Internal ordering error");
515 clog
<< "Done ordering" << endl
;
517 bool DoneSomething
= false;
518 for (pkgOrderList::iterator I
= List
->begin(); I
!= List
->end(); I
++)
520 PkgIterator
Pkg(Cache
,*I
);
522 if (List
->IsNow(Pkg
) == false)
525 clog
<< "Skipping already done " << Pkg
.Name() << endl
;
529 if (Cache
[Pkg
].Delete() == false && FileNames
[Pkg
->ID
].empty() == true)
532 clog
<< "Sequence completed at" << Pkg
.Name() << endl
;
533 if (DoneSomething
== false)
535 _error
->Error("Internal Error, ordering was unable to handle the media swap");
542 if (Cache
[Pkg
].Keep() == true && Pkg
.State() == pkgCache::PkgIterator::NeedsNothing
)
544 _error
->Error("Internal Error, trying to manipulate a kept package");
548 // Perform a delete or an install
549 if (Cache
[Pkg
].Delete() == true)
551 if (SmartRemove(Pkg
) == false)
555 if (SmartUnPack(Pkg
) == false)
557 DoneSomething
= true;
560 // Final run through the configure phase
561 if (ConfigureAll() == false)
565 for (pkgOrderList::iterator I
= List
->begin(); I
!= List
->end(); I
++)
567 if (List
->IsFlag(*I
,pkgOrderList::Configured
) == false)
569 _error
->Error("Internal error, packages left unconfigured. %s",
570 PkgIterator(Cache
,*I
).Name());
578 // PM::DoInstall - Does the installation /*{{{*/
579 // ---------------------------------------------------------------------
580 /* This uses the filenames in FileNames and the information in the
581 DepCache to perform the installation of packages.*/
582 pkgPackageManager::OrderResult
pkgPackageManager::DoInstall()
584 OrderResult Res
= OrderInstall();