]>
git.saurik.com Git - apt.git/blob - apt-pkg/packagemanager.cc
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: packagemanager.cc,v 1.14 1999/02/21 08:38:53 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
);
64 // Skip packages to erase
65 if (Cache
[Pkg
].Delete() == true)
68 // Skip Packages that need configure only.
69 if (Pkg
.State() == pkgCache::PkgIterator::NeedsConfigure
&&
70 Cache
[Pkg
].Keep() == true)
73 new pkgAcqArchive(Owner
,Sources
,Recs
,Cache
[Pkg
].InstVerIter(Cache
),
80 // PM::FixMissing - Keep all missing packages /*{{{*/
81 // ---------------------------------------------------------------------
82 /* This is called to correct the installation when packages could not
84 bool pkgPackageManager::FixMissing()
86 pkgProblemResolver
Resolve(Cache
);
89 for (PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
91 // These don't need files
92 if (Cache
[I
].Keep() == true)
94 if (Cache
[I
].Delete() == true)
98 if (FileNames
[I
->ID
].empty() == false)
101 // Skip Packages that need configure only.
102 if (I
.State() == pkgCache::PkgIterator::NeedsConfigure
&&
103 Cache
[I
].Keep() == true)
106 // Okay, this file is missing and we need it. Mark it for keep
114 // Now downgrade everything that is broken
115 return Resolve
.ResolveByKeep() == true && Cache
.BrokenCount() == 0;
119 // PM::CreateOrderList - Create the ordering class /*{{{*/
120 // ---------------------------------------------------------------------
121 /* This populates the ordering list with all the packages that are
123 bool pkgPackageManager::CreateOrderList()
126 List
= new pkgOrderList(Cache
);
128 bool NoImmConfigure
= _config
->FindB("APT::Immediate-Configure",false);
130 // Generate the list of affected packages and sort it
131 for (PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
133 // Mark the package and its dependends for immediate configuration
134 if (((I
->Flags
& pkgCache::Flag::Essential
) == pkgCache::Flag::Essential
||
135 (I
->Flags
& pkgCache::Flag::Important
) == pkgCache::Flag::Important
) &&
136 NoImmConfigure
== false)
138 List
->Flag(I
,pkgOrderList::Immediate
);
140 // Look for other packages to make immediate configurea
141 if (Cache
[I
].InstallVer
!= 0)
142 for (DepIterator D
= Cache
[I
].InstVerIter(Cache
).DependsList();
143 D
.end() == false; D
++)
144 if (D
->Type
== pkgCache::Dep::Depends
|| D
->Type
== pkgCache::Dep::PreDepends
)
145 List
->Flag(D
.TargetPkg(),pkgOrderList::Immediate
);
147 // And again with the current version.
148 if (I
->CurrentVer
!= 0)
149 for (DepIterator D
= I
.CurrentVer().DependsList();
150 D
.end() == false; D
++)
151 if (D
->Type
== pkgCache::Dep::Depends
|| D
->Type
== pkgCache::Dep::PreDepends
)
152 List
->Flag(D
.TargetPkg(),pkgOrderList::Immediate
);
156 if ((Cache
[I
].Keep() == true ||
157 Cache
[I
].InstVerIter(Cache
) == I
.CurrentVer()) &&
158 I
.State() == pkgCache::PkgIterator::NeedsNothing
)
161 // Append it to the list
168 // PM::DepAlwaysTrue - Returns true if this dep is irrelevent /*{{{*/
169 // ---------------------------------------------------------------------
170 /* The restriction on provides is to eliminate the case when provides
171 are transitioning between valid states [ie exim to smail] */
172 bool pkgPackageManager::DepAlwaysTrue(DepIterator D
)
174 if (D
.TargetPkg()->ProvidesList
!= 0)
177 if ((Cache
[D
] & pkgDepCache::DepInstall
) != 0 &&
178 (Cache
[D
] & pkgDepCache::DepNow
) != 0)
183 // PM::CheckRConflicts - Look for reverse conflicts /*{{{*/
184 // ---------------------------------------------------------------------
185 /* This looks over the reverses for a conflicts line that needs early
187 bool pkgPackageManager::CheckRConflicts(PkgIterator Pkg
,DepIterator D
,
190 for (;D
.end() == false; D
++)
192 if (D
->Type
!= pkgCache::Dep::Conflicts
)
195 if (D
.ParentPkg() == Pkg
)
198 if (pkgCheckDep(D
.TargetVer(),Ver
,D
->CompareOp
) == false)
201 if (List
->IsNow(Pkg
) == false)
204 if (EarlyRemove(D
.ParentPkg()) == false)
210 // PM::ConfigureAll - Run the all out configuration /*{{{*/
211 // ---------------------------------------------------------------------
212 /* This configures every package. It is assumed they are all unpacked and
213 that the final configuration is valid. */
214 bool pkgPackageManager::ConfigureAll()
216 pkgOrderList
OList(Cache
);
218 // Populate the order list
219 for (pkgOrderList::iterator I
= List
->begin(); I
!= List
->end(); I
++)
220 if (List
->IsFlag(pkgCache::PkgIterator(Cache
,*I
),
221 pkgOrderList::UnPacked
) == true)
224 if (OList
.OrderConfigure() == false)
227 // Perform the configuring
228 for (pkgOrderList::iterator I
= OList
.begin(); I
!= OList
.end(); I
++)
230 PkgIterator
Pkg(Cache
,*I
);
232 if (Configure(Pkg
) == false)
235 List
->Flag(Pkg
,pkgOrderList::Configured
,pkgOrderList::States
);
241 // PM::SmartConfigure - Perform immediate configuration of the pkg /*{{{*/
242 // ---------------------------------------------------------------------
243 /* This routine scheduals the configuration of the given package and all
244 of it's dependents. */
245 bool pkgPackageManager::SmartConfigure(PkgIterator Pkg
)
247 pkgOrderList
OList(Cache
);
249 if (DepAdd(OList
,Pkg
) == false)
252 if (OList
.OrderConfigure() == false)
255 // Perform the configuring
256 for (pkgOrderList::iterator I
= OList
.begin(); I
!= OList
.end(); I
++)
258 PkgIterator
Pkg(Cache
,*I
);
260 if (Configure(Pkg
) == false)
263 List
->Flag(Pkg
,pkgOrderList::Configured
,pkgOrderList::States
);
267 if (List
->IsFlag(Pkg
,pkgOrderList::Configured
) == false)
268 return _error
->Error("Internal error, could not immediate configure %s",Pkg
.Name());
273 // PM::DepAdd - Add all dependents to the oder list /*{{{*/
274 // ---------------------------------------------------------------------
275 /* This recursively adds all dependents to the order list */
276 bool pkgPackageManager::DepAdd(pkgOrderList
&OList
,PkgIterator Pkg
,int Depth
)
278 if (OList
.IsFlag(Pkg
,pkgOrderList::Added
) == true)
280 if (List
->IsFlag(Pkg
,pkgOrderList::Configured
) == true)
282 if (List
->IsFlag(Pkg
,pkgOrderList::UnPacked
) == false)
286 // Put the package on the list
287 OList
.push_back(Pkg
);
288 OList
.Flag(Pkg
,pkgOrderList::Added
);
291 // Check the dependencies to see if they are all satisfied.
293 for (DepIterator D
= Cache
[Pkg
].InstVerIter(Cache
).DependsList(); D
.end() == false;)
295 if (D
->Type
!= pkgCache::Dep::Depends
&& D
->Type
!= pkgCache::Dep::PreDepends
)
303 for (bool LastOR
= true; D
.end() == false && LastOR
== true; D
++)
305 LastOR
= (D
->CompareOp
& pkgCache::Dep::Or
) == pkgCache::Dep::Or
;
310 Version
**VList
= D
.AllTargets();
311 for (Version
**I
= VList
; *I
!= 0 && Bad
== true; I
++)
313 VerIterator
Ver(Cache
,*I
);
314 PkgIterator Pkg
= Ver
.ParentPkg();
316 // See if the current version is ok
317 if (Pkg
.CurrentVer() == Ver
&& List
->IsNow(Pkg
) == true &&
318 Pkg
.State() == PkgIterator::NeedsNothing
)
324 // Not the install version
325 if (Cache
[Pkg
].InstallVer
!= *I
||
326 (Cache
[Pkg
].Keep() == true && Pkg
.State() == PkgIterator::NeedsNothing
))
328 if (List
->IsFlag(Pkg
,pkgOrderList::UnPacked
) == true)
329 Bad
= !DepAdd(OList
,Pkg
,Depth
);
330 if (List
->IsFlag(Pkg
,pkgOrderList::Configured
) == true)
338 OList
.Flag(Pkg
,0,pkgOrderList::Added
);
349 // PM::EarlyRemove - Perform removal of packages before their time /*{{{*/
350 // ---------------------------------------------------------------------
351 /* This is called to deal with conflicts arising from unpacking */
352 bool pkgPackageManager::EarlyRemove(PkgIterator Pkg
)
354 if (List
->IsNow(Pkg
) == false)
357 // Already removed it
358 if (List
->IsFlag(Pkg
,pkgOrderList::Removed
) == true)
361 // Woops, it will not be re-installed!
362 if (List
->IsFlag(Pkg
,pkgOrderList::InList
) == false)
365 bool Res
= SmartRemove(Pkg
);
366 if (Cache
[Pkg
].Delete() == false)
367 List
->Flag(Pkg
,pkgOrderList::Removed
,pkgOrderList::States
);
372 // PM::SmartRemove - Removal Helper /*{{{*/
373 // ---------------------------------------------------------------------
375 bool pkgPackageManager::SmartRemove(PkgIterator Pkg
)
377 if (List
->IsNow(Pkg
) == false)
380 List
->Flag(Pkg
,pkgOrderList::Configured
,pkgOrderList::States
);
384 // PM::SmartUnPack - Install helper /*{{{*/
385 // ---------------------------------------------------------------------
386 /* This performs the task of handling pre-depends. */
387 bool pkgPackageManager::SmartUnPack(PkgIterator Pkg
)
389 // Check if it is already unpacked
390 if (Pkg
.State() == pkgCache::PkgIterator::NeedsConfigure
&&
391 Cache
[Pkg
].Keep() == true)
393 List
->Flag(Pkg
,pkgOrderList::UnPacked
,pkgOrderList::States
);
394 if (List
->IsFlag(Pkg
,pkgOrderList::Immediate
) == true)
395 if (SmartConfigure(Pkg
) == false)
396 return _error
->Error("Internal Error, Could not perform immediate configuraton");
400 /* See if this packages install version has any predependencies
401 that are not met by 'now' packages. */
402 for (DepIterator D
= Cache
[Pkg
].InstVerIter(Cache
).DependsList();
403 D
.end() == false; D
++)
405 if (D
->Type
== pkgCache::Dep::PreDepends
)
407 // Look for possible ok targets.
408 Version
**VList
= D
.AllTargets();
410 for (Version
**I
= VList
; *I
!= 0 && Bad
== true; I
++)
412 VerIterator
Ver(Cache
,*I
);
413 PkgIterator Pkg
= Ver
.ParentPkg();
415 // See if the current version is ok
416 if (Pkg
.CurrentVer() == Ver
&& List
->IsNow(Pkg
) == true &&
417 Pkg
.State() == PkgIterator::NeedsNothing
)
424 // Look for something that could be configured.
425 for (Version
**I
= VList
; *I
!= 0 && Bad
== true; I
++)
427 VerIterator
Ver(Cache
,*I
);
428 PkgIterator Pkg
= Ver
.ParentPkg();
430 // Not the install version
431 if (Cache
[Pkg
].InstallVer
!= *I
||
432 (Cache
[Pkg
].Keep() == true && Pkg
.State() == PkgIterator::NeedsNothing
))
435 Bad
= !SmartConfigure(Pkg
);
441 return _error
->Error("Internal Error, Couldn't configure a pre-depend");
446 if (D
->Type
== pkgCache::Dep::Conflicts
)
448 /* Look for conflicts. Two packages that are both in the install
449 state cannot conflict so we don't check.. */
450 Version
**VList
= D
.AllTargets();
451 for (Version
**I
= VList
; *I
!= 0; I
++)
453 VerIterator
Ver(Cache
,*I
);
454 PkgIterator Pkg
= Ver
.ParentPkg();
456 // See if the current version is conflicting
457 if (Pkg
.CurrentVer() == Ver
&& List
->IsNow(Pkg
) == true)
459 if (EarlyRemove(Pkg
) == false)
460 return _error
->Error("Internal Error, Could not early remove %s",Pkg
.Name());
467 // Check for reverse conflicts.
468 CheckRConflicts(Pkg
,Pkg
.RevDependsList(),
469 Cache
[Pkg
].InstVerIter(Cache
).VerStr());
470 for (PrvIterator P
= Cache
[Pkg
].InstVerIter(Cache
).ProvidesList();
471 P
.end() == false; P
++)
472 CheckRConflicts(Pkg
,P
.ParentPkg().RevDependsList(),P
.ProvideVersion());
474 if (Install(Pkg
,FileNames
[Pkg
->ID
]) == false)
477 List
->Flag(Pkg
,pkgOrderList::UnPacked
,pkgOrderList::States
);
479 // Perform immedate configuration of the package.
480 if (List
->IsFlag(Pkg
,pkgOrderList::Immediate
) == true)
481 if (SmartConfigure(Pkg
) == false)
482 return _error
->Error("Internal Error, Could not perform immediate configuraton");
487 // PM::OrderInstall - Installation ordering routine /*{{{*/
488 // ---------------------------------------------------------------------
490 bool pkgPackageManager::OrderInstall()
492 if (CreateOrderList() == false)
496 clog
<< "Begining to order" << endl
;
498 if (List
->OrderUnpack() == false)
499 return _error
->Error("Internal ordering error");
502 clog
<< "Done ordering" << endl
;
504 for (pkgOrderList::iterator I
= List
->begin(); I
!= List
->end(); I
++)
506 PkgIterator
Pkg(Cache
,*I
);
509 if (Cache
[Pkg
].Keep() == true && Pkg
.State() == pkgCache::PkgIterator::NeedsNothing
)
510 return _error
->Error("Internal Error, trying to manipulate a kept package");
512 // Perform a delete or an install
513 if (Cache
[Pkg
].Delete() == true)
515 if (SmartRemove(Pkg
) == false)
519 if (SmartUnPack(Pkg
) == false)
523 // Final run through the configure phase
524 if (ConfigureAll() == false)
528 for (pkgOrderList::iterator I
= List
->begin(); I
!= List
->end(); I
++)
529 if (List
->IsFlag(*I
,pkgOrderList::Configured
) == false)
530 return _error
->Error("Internal error, packages left unconfigured. %s",
531 PkgIterator(Cache
,*I
).Name());
536 // PM::DoInstall - Does the installation /*{{{*/
537 // ---------------------------------------------------------------------
538 /* This uses the filenames in FileNames and the information in the
539 DepCache to perform the installation of packages.*/
540 bool pkgPackageManager::DoInstall()
542 return OrderInstall() && Go();