Fixed weird to-configure inconsitency and added apt-cac...
[ntk/apt.git] / apt-pkg / packagemanager.cc
CommitLineData
6c139d6e
AL
1// -*- mode: cpp; mode: fold -*-
2// Description /*{{{*/
9dbb421f 3// $Id: packagemanager.cc,v 1.13 1999/02/19 07:56:07 jgg Exp $
6c139d6e
AL
4/* ######################################################################
5
6 Package Manager - Abstacts the package manager
7
8 More work is needed in the area of transitioning provides, ie exim
9 replacing smail. This can cause interesing side effects.
10
11 Other cases involving conflicts+replaces should be tested.
12
13 ##################################################################### */
14 /*}}}*/
15// Include Files /*{{{*/
16#ifdef __GNUG__
094a497d 17#pragma implementation "apt-pkg/packagemanager.h"
6c139d6e 18#endif
094a497d
AL
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>
03e39e59 24#include <apt-pkg/acquire-item.h>
30e1eab5
AL
25#include <apt-pkg/algorithms.h>
26#include <apt-pkg/configuration.h>
6c139d6e
AL
27 /*}}}*/
28
29// PM::PackageManager - Constructor /*{{{*/
30// ---------------------------------------------------------------------
31/* */
32pkgPackageManager::pkgPackageManager(pkgDepCache &Cache) : Cache(Cache)
33{
34 FileNames = new string[Cache.Head().PackageCount];
35 List = 0;
30e1eab5 36 Debug = _config->FindB("Debug::pkgPackageManager",false);
6c139d6e
AL
37}
38 /*}}}*/
39// PM::PackageManager - Destructor /*{{{*/
40// ---------------------------------------------------------------------
41/* */
42pkgPackageManager::~pkgPackageManager()
43{
44 delete List;
45 delete [] FileNames;
46}
47 /*}}}*/
03e39e59
AL
48// PM::GetArchives - Queue the archives for download /*{{{*/
49// ---------------------------------------------------------------------
50/* */
51bool pkgPackageManager::GetArchives(pkgAcquire *Owner,pkgSourceList *Sources,
52 pkgRecords *Recs)
53{
7a1b1f8b
AL
54 if (CreateOrderList() == false)
55 return false;
56
57 if (List->OrderUnpack() == false)
58 return _error->Error("Internal ordering error");
59
60 for (pkgOrderList::iterator I = List->begin(); I != List->end(); I++)
61 {
62 PkgIterator Pkg(Cache,*I);
63
64 // Skip packages to erase
65 if (Cache[Pkg].Delete() == true)
03e39e59 66 continue;
d38b7b3d
AL
67
68 // Skip Packages that need configure only.
9dbb421f
AL
69 if (Pkg.State() == pkgCache::PkgIterator::NeedsConfigure &&
70 Cache[Pkg].Keep() == true)
d38b7b3d 71 continue;
03e39e59 72
7a1b1f8b
AL
73 new pkgAcqArchive(Owner,Sources,Recs,Cache[Pkg].InstVerIter(Cache),
74 FileNames[Pkg->ID]);
03e39e59 75 }
7a1b1f8b 76
03e39e59
AL
77 return true;
78}
79 /*}}}*/
6c139d6e
AL
80// PM::FixMissing - Keep all missing packages /*{{{*/
81// ---------------------------------------------------------------------
82/* This is called to correct the installation when packages could not
83 be downloaded. */
84bool pkgPackageManager::FixMissing()
85{
30e1eab5 86 pkgProblemResolver Resolve(Cache);
9dbb421f
AL
87
88 bool Bad = false;
6c139d6e
AL
89 for (PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
90 {
9dbb421f
AL
91 // These don't need files
92 if (Cache[I].Keep() == true)
6c139d6e 93 continue;
9dbb421f 94 if (Cache[I].Delete() == true)
6c139d6e 95 continue;
9dbb421f
AL
96
97 // We have a filename
98 if (FileNames[I->ID].empty() == false)
99 continue;
100
101 // Skip Packages that need configure only.
102 if (I.State() == pkgCache::PkgIterator::NeedsConfigure &&
103 Cache[I].Keep() == true)
104 continue;
105
106 // Okay, this file is missing and we need it. Mark it for keep
107 Bad = true;
6c139d6e
AL
108 Cache.MarkKeep(I);
109 }
110
9dbb421f
AL
111 if (Bad == false)
112 return true;
113
6c139d6e 114 // Now downgrade everything that is broken
30e1eab5 115 return Resolve.ResolveByKeep() == true && Cache.BrokenCount() == 0;
6c139d6e
AL
116}
117 /*}}}*/
118
7a1b1f8b
AL
119// PM::CreateOrderList - Create the ordering class /*{{{*/
120// ---------------------------------------------------------------------
121/* This populates the ordering list with all the packages that are
122 going to change. */
123bool pkgPackageManager::CreateOrderList()
124{
125 delete List;
126 List = new pkgOrderList(Cache);
127
b780bc1b 128 bool NoImmConfigure = _config->FindB("APT::Immediate-Configure",false);
079cc404 129
7a1b1f8b
AL
130 // Generate the list of affected packages and sort it
131 for (PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
132 {
d38b7b3d 133 // Mark the package for immediate configuration
079cc404
AL
134 if ((I->Flags & pkgCache::Flag::Essential) == pkgCache::Flag::Essential &&
135 NoImmConfigure == false)
7a1b1f8b
AL
136 {
137 List->Flag(I,pkgOrderList::Immediate);
d38b7b3d
AL
138
139 // Look for other packages to make immediate configurea
7a1b1f8b
AL
140 if (Cache[I].InstallVer != 0)
141 for (DepIterator D = Cache[I].InstVerIter(Cache).DependsList();
142 D.end() == false; D++)
143 if (D->Type == pkgCache::Dep::Depends || D->Type == pkgCache::Dep::PreDepends)
144 List->Flag(D.TargetPkg(),pkgOrderList::Immediate);
d38b7b3d
AL
145
146 // And again with the current version.
7a1b1f8b
AL
147 if (I->CurrentVer != 0)
148 for (DepIterator D = I.CurrentVer().DependsList();
149 D.end() == false; D++)
150 if (D->Type == pkgCache::Dep::Depends || D->Type == pkgCache::Dep::PreDepends)
151 List->Flag(D.TargetPkg(),pkgOrderList::Immediate);
152 }
153
154 // Not interesting
155 if ((Cache[I].Keep() == true ||
156 Cache[I].InstVerIter(Cache) == I.CurrentVer()) &&
157 I.State() == pkgCache::PkgIterator::NeedsNothing)
158 continue;
159
160 // Append it to the list
161 List->push_back(I);
162
163 if ((I->Flags & pkgCache::Flag::ImmediateConf) == pkgCache::Flag::ImmediateConf)
164 List->Flag(I,pkgOrderList::Immediate);
165 }
166
167 return true;
168}
169 /*}}}*/
6c139d6e
AL
170// PM::DepAlwaysTrue - Returns true if this dep is irrelevent /*{{{*/
171// ---------------------------------------------------------------------
172/* The restriction on provides is to eliminate the case when provides
173 are transitioning between valid states [ie exim to smail] */
174bool pkgPackageManager::DepAlwaysTrue(DepIterator D)
175{
176 if (D.TargetPkg()->ProvidesList != 0)
177 return false;
178
179 if ((Cache[D] & pkgDepCache::DepInstall) != 0 &&
180 (Cache[D] & pkgDepCache::DepNow) != 0)
181 return true;
182 return false;
183}
184 /*}}}*/
185// PM::CheckRConflicts - Look for reverse conflicts /*{{{*/
186// ---------------------------------------------------------------------
187/* This looks over the reverses for a conflicts line that needs early
188 removal. */
189bool pkgPackageManager::CheckRConflicts(PkgIterator Pkg,DepIterator D,
190 const char *Ver)
191{
192 for (;D.end() == false; D++)
193 {
b50b2c97 194 if (D->Type != pkgCache::Dep::Conflicts)
6c139d6e
AL
195 continue;
196
197 if (D.ParentPkg() == Pkg)
198 continue;
199
200 if (pkgCheckDep(D.TargetVer(),Ver,D->CompareOp) == false)
201 continue;
202
203 if (List->IsNow(Pkg) == false)
204 continue;
205
206 if (EarlyRemove(D.ParentPkg()) == false)
207 return false;
208 }
209 return true;
210}
211 /*}}}*/
212// PM::ConfigureAll - Run the all out configuration /*{{{*/
213// ---------------------------------------------------------------------
214/* This configures every package. It is assumed they are all unpacked and
215 that the final configuration is valid. */
216bool pkgPackageManager::ConfigureAll()
217{
218 pkgOrderList OList(Cache);
219
220 // Populate the order list
221 for (pkgOrderList::iterator I = List->begin(); I != List->end(); I++)
222 if (List->IsFlag(pkgCache::PkgIterator(Cache,*I),
223 pkgOrderList::UnPacked) == true)
224 OList.push_back(*I);
225
226 if (OList.OrderConfigure() == false)
227 return false;
228
229 // Perform the configuring
230 for (pkgOrderList::iterator I = OList.begin(); I != OList.end(); I++)
231 {
232 PkgIterator Pkg(Cache,*I);
233
234 if (Configure(Pkg) == false)
235 return false;
236
237 List->Flag(Pkg,pkgOrderList::Configured,pkgOrderList::States);
238 }
239
240 return true;
241}
242 /*}}}*/
243// PM::SmartConfigure - Perform immediate configuration of the pkg /*{{{*/
244// ---------------------------------------------------------------------
245/* This routine scheduals the configuration of the given package and all
246 of it's dependents. */
247bool pkgPackageManager::SmartConfigure(PkgIterator Pkg)
248{
249 pkgOrderList OList(Cache);
250
251 if (DepAdd(OList,Pkg) == false)
252 return false;
253
254 if (OList.OrderConfigure() == false)
255 return false;
256
257 // Perform the configuring
258 for (pkgOrderList::iterator I = OList.begin(); I != OList.end(); I++)
259 {
260 PkgIterator Pkg(Cache,*I);
261
262 if (Configure(Pkg) == false)
263 return false;
264
265 List->Flag(Pkg,pkgOrderList::Configured,pkgOrderList::States);
266 }
267
268 // Sanity Check
269 if (List->IsFlag(Pkg,pkgOrderList::Configured) == false)
270 return _error->Error("Internal error, could not immediate configure %s",Pkg.Name());
271
272 return true;
273}
274 /*}}}*/
275// PM::DepAdd - Add all dependents to the oder list /*{{{*/
276// ---------------------------------------------------------------------
277/* This recursively adds all dependents to the order list */
278bool pkgPackageManager::DepAdd(pkgOrderList &OList,PkgIterator Pkg,int Depth)
279{
280 if (OList.IsFlag(Pkg,pkgOrderList::Added) == true)
281 return true;
282 if (List->IsFlag(Pkg,pkgOrderList::Configured) == true)
283 return true;
284 if (List->IsFlag(Pkg,pkgOrderList::UnPacked) == false)
285 return false;
286
287
288 // Put the package on the list
289 OList.push_back(Pkg);
290 OList.Flag(Pkg,pkgOrderList::Added);
291 Depth++;
292
293 // Check the dependencies to see if they are all satisfied.
294 bool Bad = false;
295 for (DepIterator D = Cache[Pkg].InstVerIter(Cache).DependsList(); D.end() == false;)
296 {
b50b2c97 297 if (D->Type != pkgCache::Dep::Depends && D->Type != pkgCache::Dep::PreDepends)
6c139d6e
AL
298 {
299 D++;
300 continue;
301 }
302
303 // Grok or groups
304 Bad = true;
305 for (bool LastOR = true; D.end() == false && LastOR == true; D++)
306 {
b50b2c97 307 LastOR = (D->CompareOp & pkgCache::Dep::Or) == pkgCache::Dep::Or;
6c139d6e
AL
308
309 if (Bad == false)
310 continue;
311
312 Version **VList = D.AllTargets();
313 for (Version **I = VList; *I != 0 && Bad == true; I++)
314 {
315 VerIterator Ver(Cache,*I);
316 PkgIterator Pkg = Ver.ParentPkg();
317
318 // See if the current version is ok
319 if (Pkg.CurrentVer() == Ver && List->IsNow(Pkg) == true &&
320 Pkg.State() == PkgIterator::NeedsNothing)
321 {
322 Bad = false;
323 continue;
324 }
325
326 // Not the install version
327 if (Cache[Pkg].InstallVer != *I ||
328 (Cache[Pkg].Keep() == true && Pkg.State() == PkgIterator::NeedsNothing))
329 continue;
330 if (List->IsFlag(Pkg,pkgOrderList::UnPacked) == true)
331 Bad = !DepAdd(OList,Pkg,Depth);
332 if (List->IsFlag(Pkg,pkgOrderList::Configured) == true)
333 Bad = false;
334 }
335 delete [] VList;
336 }
337
338 if (Bad == true)
339 {
340 OList.Flag(Pkg,0,pkgOrderList::Added);
341 OList.pop_back();
342 Depth--;
343 return false;
344 }
345 }
346
347 Depth--;
348 return true;
349}
350 /*}}}*/
351// PM::EarlyRemove - Perform removal of packages before their time /*{{{*/
352// ---------------------------------------------------------------------
353/* This is called to deal with conflicts arising from unpacking */
354bool pkgPackageManager::EarlyRemove(PkgIterator Pkg)
355{
356 if (List->IsNow(Pkg) == false)
357 return true;
358
359 // Already removed it
360 if (List->IsFlag(Pkg,pkgOrderList::Removed) == true)
361 return true;
362
363 // Woops, it will not be re-installed!
364 if (List->IsFlag(Pkg,pkgOrderList::InList) == false)
365 return false;
366
367 bool Res = SmartRemove(Pkg);
368 if (Cache[Pkg].Delete() == false)
369 List->Flag(Pkg,pkgOrderList::Removed,pkgOrderList::States);
370
371 return Res;
372}
373 /*}}}*/
374// PM::SmartRemove - Removal Helper /*{{{*/
375// ---------------------------------------------------------------------
376/* */
377bool pkgPackageManager::SmartRemove(PkgIterator Pkg)
378{
379 if (List->IsNow(Pkg) == false)
380 return true;
381
382 List->Flag(Pkg,pkgOrderList::Configured,pkgOrderList::States);
383 return Remove(Pkg);
384}
385 /*}}}*/
386// PM::SmartUnPack - Install helper /*{{{*/
387// ---------------------------------------------------------------------
388/* This performs the task of handling pre-depends. */
389bool pkgPackageManager::SmartUnPack(PkgIterator Pkg)
390{
391 // Check if it is already unpacked
392 if (Pkg.State() == pkgCache::PkgIterator::NeedsConfigure &&
393 Cache[Pkg].Keep() == true)
394 {
395 List->Flag(Pkg,pkgOrderList::UnPacked,pkgOrderList::States);
396 if (List->IsFlag(Pkg,pkgOrderList::Immediate) == true)
397 if (SmartConfigure(Pkg) == false)
398 return _error->Error("Internal Error, Could not perform immediate configuraton");
399 return true;
400 }
981d20eb 401
6c139d6e
AL
402 /* See if this packages install version has any predependencies
403 that are not met by 'now' packages. */
404 for (DepIterator D = Cache[Pkg].InstVerIter(Cache).DependsList();
405 D.end() == false; D++)
406 {
b50b2c97 407 if (D->Type == pkgCache::Dep::PreDepends)
6c139d6e
AL
408 {
409 // Look for possible ok targets.
410 Version **VList = D.AllTargets();
411 bool Bad = true;
412 for (Version **I = VList; *I != 0 && Bad == true; I++)
413 {
414 VerIterator Ver(Cache,*I);
415 PkgIterator Pkg = Ver.ParentPkg();
416
417 // See if the current version is ok
418 if (Pkg.CurrentVer() == Ver && List->IsNow(Pkg) == true &&
419 Pkg.State() == PkgIterator::NeedsNothing)
420 {
421 Bad = false;
422 continue;
423 }
424 }
425
426 // Look for something that could be configured.
427 for (Version **I = VList; *I != 0 && Bad == true; I++)
428 {
429 VerIterator Ver(Cache,*I);
430 PkgIterator Pkg = Ver.ParentPkg();
431
432 // Not the install version
433 if (Cache[Pkg].InstallVer != *I ||
434 (Cache[Pkg].Keep() == true && Pkg.State() == PkgIterator::NeedsNothing))
435 continue;
436
437 Bad = !SmartConfigure(Pkg);
438 }
439
440 delete [] VList;
441
442 if (Bad == true)
443 return _error->Error("Internal Error, Couldn't configure a pre-depend");
444
445 continue;
446 }
447
b50b2c97 448 if (D->Type == pkgCache::Dep::Conflicts)
6c139d6e
AL
449 {
450 /* Look for conflicts. Two packages that are both in the install
451 state cannot conflict so we don't check.. */
452 Version **VList = D.AllTargets();
453 for (Version **I = VList; *I != 0; I++)
454 {
455 VerIterator Ver(Cache,*I);
456 PkgIterator Pkg = Ver.ParentPkg();
457
458 // See if the current version is conflicting
459 if (Pkg.CurrentVer() == Ver && List->IsNow(Pkg) == true)
460 {
461 if (EarlyRemove(Pkg) == false)
462 return _error->Error("Internal Error, Could not early remove %s",Pkg.Name());
463 }
464 }
465 delete [] VList;
466 }
467 }
468
469 // Check for reverse conflicts.
470 CheckRConflicts(Pkg,Pkg.RevDependsList(),
471 Cache[Pkg].InstVerIter(Cache).VerStr());
472 for (PrvIterator P = Cache[Pkg].InstVerIter(Cache).ProvidesList();
473 P.end() == false; P++)
474 CheckRConflicts(Pkg,P.ParentPkg().RevDependsList(),P.ProvideVersion());
475
476 if (Install(Pkg,FileNames[Pkg->ID]) == false)
477 return false;
478
479 List->Flag(Pkg,pkgOrderList::UnPacked,pkgOrderList::States);
480
481 // Perform immedate configuration of the package.
482 if (List->IsFlag(Pkg,pkgOrderList::Immediate) == true)
483 if (SmartConfigure(Pkg) == false)
484 return _error->Error("Internal Error, Could not perform immediate configuraton");
485
486 return true;
487}
488 /*}}}*/
489// PM::OrderInstall - Installation ordering routine /*{{{*/
490// ---------------------------------------------------------------------
491/* */
492bool pkgPackageManager::OrderInstall()
493{
7a1b1f8b
AL
494 if (CreateOrderList() == false)
495 return false;
6c139d6e 496
30e1eab5
AL
497 if (Debug == true)
498 clog << "Begining to order" << endl;
6c139d6e
AL
499
500 if (List->OrderUnpack() == false)
501 return _error->Error("Internal ordering error");
502
30e1eab5
AL
503 if (Debug == true)
504 clog << "Done ordering" << endl;
505
6c139d6e
AL
506 for (pkgOrderList::iterator I = List->begin(); I != List->end(); I++)
507 {
508 PkgIterator Pkg(Cache,*I);
509
510 // Sanity check
511 if (Cache[Pkg].Keep() == true && Pkg.State() == pkgCache::PkgIterator::NeedsNothing)
512 return _error->Error("Internal Error, trying to manipulate a kept package");
513
514 // Perform a delete or an install
515 if (Cache[Pkg].Delete() == true)
516 {
517 if (SmartRemove(Pkg) == false)
518 return false;
519 }
520 else
521 if (SmartUnPack(Pkg) == false)
522 return false;
523 }
524
525 // Final run through the configure phase
526 if (ConfigureAll() == false)
527 return false;
528
529 // Sanity check
530 for (pkgOrderList::iterator I = List->begin(); I != List->end(); I++)
531 if (List->IsFlag(*I,pkgOrderList::Configured) == false)
532 return _error->Error("Internal error, packages left unconfigured. %s",
533 PkgIterator(Cache,*I).Name());
534
535 return true;
536}
537 /*}}}*/
538// PM::DoInstall - Does the installation /*{{{*/
539// ---------------------------------------------------------------------
540/* This uses the filenames in FileNames and the information in the
541 DepCache to perform the installation of packages.*/
542bool pkgPackageManager::DoInstall()
543{
544 return OrderInstall() && Go();
545}
546 /*}}}*/