Purge support
[ntk/apt.git] / apt-pkg / packagemanager.cc
CommitLineData
6c139d6e
AL
1// -*- mode: cpp; mode: fold -*-
2// Description /*{{{*/
d556d1a1 3// $Id: packagemanager.cc,v 1.18 1999/07/10 04:58:42 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);
281daf46
AL
63 FileNames[Pkg->ID] = string();
64
7a1b1f8b
AL
65 // Skip packages to erase
66 if (Cache[Pkg].Delete() == true)
03e39e59 67 continue;
d38b7b3d
AL
68
69 // Skip Packages that need configure only.
9dbb421f
AL
70 if (Pkg.State() == pkgCache::PkgIterator::NeedsConfigure &&
71 Cache[Pkg].Keep() == true)
d38b7b3d 72 continue;
281daf46
AL
73
74 // Skip already processed packages
75 if (List->IsNow(Pkg) == false)
76 continue;
77
7a1b1f8b
AL
78 new pkgAcqArchive(Owner,Sources,Recs,Cache[Pkg].InstVerIter(Cache),
79 FileNames[Pkg->ID]);
03e39e59 80 }
7a1b1f8b 81
03e39e59
AL
82 return true;
83}
84 /*}}}*/
6c139d6e
AL
85// PM::FixMissing - Keep all missing packages /*{{{*/
86// ---------------------------------------------------------------------
87/* This is called to correct the installation when packages could not
88 be downloaded. */
89bool pkgPackageManager::FixMissing()
90{
30e1eab5 91 pkgProblemResolver Resolve(Cache);
2fd65468
AL
92 List->SetFileList(FileNames);
93
9dbb421f 94 bool Bad = false;
6c139d6e
AL
95 for (PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
96 {
2fd65468 97 if (List->IsMissing(I) == false)
9dbb421f 98 continue;
2fd65468 99
9dbb421f
AL
100 // Okay, this file is missing and we need it. Mark it for keep
101 Bad = true;
6c139d6e
AL
102 Cache.MarkKeep(I);
103 }
104
9dbb421f
AL
105 if (Bad == false)
106 return true;
107
6c139d6e 108 // Now downgrade everything that is broken
30e1eab5 109 return Resolve.ResolveByKeep() == true && Cache.BrokenCount() == 0;
6c139d6e
AL
110}
111 /*}}}*/
112
7a1b1f8b
AL
113// PM::CreateOrderList - Create the ordering class /*{{{*/
114// ---------------------------------------------------------------------
115/* This populates the ordering list with all the packages that are
116 going to change. */
117bool pkgPackageManager::CreateOrderList()
118{
281daf46
AL
119 if (List != 0)
120 return true;
121
7a1b1f8b
AL
122 delete List;
123 List = new pkgOrderList(Cache);
124
b780bc1b 125 bool NoImmConfigure = _config->FindB("APT::Immediate-Configure",false);
079cc404 126
7a1b1f8b
AL
127 // Generate the list of affected packages and sort it
128 for (PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
129 {
138d4b3d
AL
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) &&
079cc404 133 NoImmConfigure == false)
7a1b1f8b
AL
134 {
135 List->Flag(I,pkgOrderList::Immediate);
d38b7b3d
AL
136
137 // Look for other packages to make immediate configurea
7a1b1f8b
AL
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);
d38b7b3d
AL
143
144 // And again with the current version.
7a1b1f8b
AL
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);
150 }
151
152 // Not interesting
153 if ((Cache[I].Keep() == true ||
154 Cache[I].InstVerIter(Cache) == I.CurrentVer()) &&
d556d1a1
AL
155 I.State() == pkgCache::PkgIterator::NeedsNothing &&
156 (I.Purge() != false || Cache[I].Mode != pkgDepCache::ModeDelete ||
157 (Cache[I].iFlags & pkgDepCache::Purge) != pkgDepCache::Purge))
7a1b1f8b
AL
158 continue;
159
160 // Append it to the list
138d4b3d 161 List->push_back(I);
7a1b1f8b
AL
162 }
163
164 return true;
165}
166 /*}}}*/
6c139d6e
AL
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] */
171bool pkgPackageManager::DepAlwaysTrue(DepIterator D)
172{
173 if (D.TargetPkg()->ProvidesList != 0)
174 return false;
175
176 if ((Cache[D] & pkgDepCache::DepInstall) != 0 &&
177 (Cache[D] & pkgDepCache::DepNow) != 0)
178 return true;
179 return false;
180}
181 /*}}}*/
182// PM::CheckRConflicts - Look for reverse conflicts /*{{{*/
183// ---------------------------------------------------------------------
184/* This looks over the reverses for a conflicts line that needs early
185 removal. */
186bool pkgPackageManager::CheckRConflicts(PkgIterator Pkg,DepIterator D,
187 const char *Ver)
188{
189 for (;D.end() == false; D++)
190 {
b50b2c97 191 if (D->Type != pkgCache::Dep::Conflicts)
6c139d6e
AL
192 continue;
193
194 if (D.ParentPkg() == Pkg)
195 continue;
196
197 if (pkgCheckDep(D.TargetVer(),Ver,D->CompareOp) == false)
198 continue;
199
200 if (List->IsNow(Pkg) == false)
201 continue;
202
203 if (EarlyRemove(D.ParentPkg()) == false)
204 return false;
205 }
206 return true;
207}
208 /*}}}*/
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. */
213bool pkgPackageManager::ConfigureAll()
214{
215 pkgOrderList OList(Cache);
216
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)
221 OList.push_back(*I);
222
223 if (OList.OrderConfigure() == false)
224 return false;
225
226 // Perform the configuring
227 for (pkgOrderList::iterator I = OList.begin(); I != OList.end(); I++)
228 {
229 PkgIterator Pkg(Cache,*I);
230
231 if (Configure(Pkg) == false)
232 return false;
233
234 List->Flag(Pkg,pkgOrderList::Configured,pkgOrderList::States);
235 }
236
237 return true;
238}
239 /*}}}*/
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. */
244bool pkgPackageManager::SmartConfigure(PkgIterator Pkg)
245{
246 pkgOrderList OList(Cache);
247
248 if (DepAdd(OList,Pkg) == false)
249 return false;
250
251 if (OList.OrderConfigure() == false)
252 return false;
253
254 // Perform the configuring
255 for (pkgOrderList::iterator I = OList.begin(); I != OList.end(); I++)
256 {
257 PkgIterator Pkg(Cache,*I);
258
259 if (Configure(Pkg) == false)
260 return false;
261
262 List->Flag(Pkg,pkgOrderList::Configured,pkgOrderList::States);
263 }
264
265 // Sanity Check
266 if (List->IsFlag(Pkg,pkgOrderList::Configured) == false)
267 return _error->Error("Internal error, could not immediate configure %s",Pkg.Name());
268
269 return true;
270}
271 /*}}}*/
272// PM::DepAdd - Add all dependents to the oder list /*{{{*/
273// ---------------------------------------------------------------------
274/* This recursively adds all dependents to the order list */
275bool pkgPackageManager::DepAdd(pkgOrderList &OList,PkgIterator Pkg,int Depth)
276{
277 if (OList.IsFlag(Pkg,pkgOrderList::Added) == true)
278 return true;
279 if (List->IsFlag(Pkg,pkgOrderList::Configured) == true)
280 return true;
281 if (List->IsFlag(Pkg,pkgOrderList::UnPacked) == false)
282 return false;
283
284
285 // Put the package on the list
286 OList.push_back(Pkg);
287 OList.Flag(Pkg,pkgOrderList::Added);
288 Depth++;
289
290 // Check the dependencies to see if they are all satisfied.
291 bool Bad = false;
292 for (DepIterator D = Cache[Pkg].InstVerIter(Cache).DependsList(); D.end() == false;)
293 {
b50b2c97 294 if (D->Type != pkgCache::Dep::Depends && D->Type != pkgCache::Dep::PreDepends)
6c139d6e
AL
295 {
296 D++;
297 continue;
298 }
299
300 // Grok or groups
301 Bad = true;
302 for (bool LastOR = true; D.end() == false && LastOR == true; D++)
303 {
b50b2c97 304 LastOR = (D->CompareOp & pkgCache::Dep::Or) == pkgCache::Dep::Or;
6c139d6e
AL
305
306 if (Bad == false)
307 continue;
308
309 Version **VList = D.AllTargets();
310 for (Version **I = VList; *I != 0 && Bad == true; I++)
311 {
312 VerIterator Ver(Cache,*I);
313 PkgIterator Pkg = Ver.ParentPkg();
314
315 // See if the current version is ok
316 if (Pkg.CurrentVer() == Ver && List->IsNow(Pkg) == true &&
317 Pkg.State() == PkgIterator::NeedsNothing)
318 {
319 Bad = false;
320 continue;
321 }
322
323 // Not the install version
324 if (Cache[Pkg].InstallVer != *I ||
325 (Cache[Pkg].Keep() == true && Pkg.State() == PkgIterator::NeedsNothing))
326 continue;
327 if (List->IsFlag(Pkg,pkgOrderList::UnPacked) == true)
328 Bad = !DepAdd(OList,Pkg,Depth);
329 if (List->IsFlag(Pkg,pkgOrderList::Configured) == true)
330 Bad = false;
331 }
332 delete [] VList;
333 }
334
335 if (Bad == true)
336 {
337 OList.Flag(Pkg,0,pkgOrderList::Added);
338 OList.pop_back();
339 Depth--;
340 return false;
341 }
342 }
343
344 Depth--;
345 return true;
346}
347 /*}}}*/
348// PM::EarlyRemove - Perform removal of packages before their time /*{{{*/
349// ---------------------------------------------------------------------
350/* This is called to deal with conflicts arising from unpacking */
351bool pkgPackageManager::EarlyRemove(PkgIterator Pkg)
352{
353 if (List->IsNow(Pkg) == false)
354 return true;
355
356 // Already removed it
357 if (List->IsFlag(Pkg,pkgOrderList::Removed) == true)
358 return true;
359
360 // Woops, it will not be re-installed!
361 if (List->IsFlag(Pkg,pkgOrderList::InList) == false)
362 return false;
363
364 bool Res = SmartRemove(Pkg);
365 if (Cache[Pkg].Delete() == false)
366 List->Flag(Pkg,pkgOrderList::Removed,pkgOrderList::States);
367
368 return Res;
369}
370 /*}}}*/
371// PM::SmartRemove - Removal Helper /*{{{*/
372// ---------------------------------------------------------------------
373/* */
374bool pkgPackageManager::SmartRemove(PkgIterator Pkg)
375{
376 if (List->IsNow(Pkg) == false)
377 return true;
378
379 List->Flag(Pkg,pkgOrderList::Configured,pkgOrderList::States);
fc4b5c9f 380 return Remove(Pkg,(Cache[Pkg].iFlags & pkgDepCache::Purge) == pkgDepCache::Purge);
6c139d6e
AL
381}
382 /*}}}*/
383// PM::SmartUnPack - Install helper /*{{{*/
384// ---------------------------------------------------------------------
385/* This performs the task of handling pre-depends. */
386bool pkgPackageManager::SmartUnPack(PkgIterator Pkg)
387{
388 // Check if it is already unpacked
389 if (Pkg.State() == pkgCache::PkgIterator::NeedsConfigure &&
390 Cache[Pkg].Keep() == true)
391 {
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");
396 return true;
397 }
981d20eb 398
6c139d6e
AL
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++)
403 {
b50b2c97 404 if (D->Type == pkgCache::Dep::PreDepends)
6c139d6e
AL
405 {
406 // Look for possible ok targets.
407 Version **VList = D.AllTargets();
408 bool Bad = true;
409 for (Version **I = VList; *I != 0 && Bad == true; I++)
410 {
411 VerIterator Ver(Cache,*I);
412 PkgIterator Pkg = Ver.ParentPkg();
413
414 // See if the current version is ok
415 if (Pkg.CurrentVer() == Ver && List->IsNow(Pkg) == true &&
416 Pkg.State() == PkgIterator::NeedsNothing)
417 {
418 Bad = false;
419 continue;
420 }
421 }
422
423 // Look for something that could be configured.
424 for (Version **I = VList; *I != 0 && Bad == true; I++)
425 {
426 VerIterator Ver(Cache,*I);
427 PkgIterator Pkg = Ver.ParentPkg();
428
429 // Not the install version
430 if (Cache[Pkg].InstallVer != *I ||
431 (Cache[Pkg].Keep() == true && Pkg.State() == PkgIterator::NeedsNothing))
432 continue;
433
434 Bad = !SmartConfigure(Pkg);
435 }
436
437 delete [] VList;
438
439 if (Bad == true)
440 return _error->Error("Internal Error, Couldn't configure a pre-depend");
441
442 continue;
443 }
444
b50b2c97 445 if (D->Type == pkgCache::Dep::Conflicts)
6c139d6e
AL
446 {
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++)
451 {
452 VerIterator Ver(Cache,*I);
453 PkgIterator Pkg = Ver.ParentPkg();
454
455 // See if the current version is conflicting
456 if (Pkg.CurrentVer() == Ver && List->IsNow(Pkg) == true)
457 {
458 if (EarlyRemove(Pkg) == false)
459 return _error->Error("Internal Error, Could not early remove %s",Pkg.Name());
460 }
461 }
462 delete [] VList;
463 }
464 }
465
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());
472
473 if (Install(Pkg,FileNames[Pkg->ID]) == false)
474 return false;
475
476 List->Flag(Pkg,pkgOrderList::UnPacked,pkgOrderList::States);
477
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");
482
483 return true;
484}
485 /*}}}*/
486// PM::OrderInstall - Installation ordering routine /*{{{*/
487// ---------------------------------------------------------------------
488/* */
281daf46 489pkgPackageManager::OrderResult pkgPackageManager::OrderInstall()
6c139d6e 490{
7a1b1f8b 491 if (CreateOrderList() == false)
281daf46
AL
492 return Failed;
493
494 Reset();
6c139d6e 495
30e1eab5
AL
496 if (Debug == true)
497 clog << "Begining to order" << endl;
6c139d6e 498
281daf46
AL
499 if (List->OrderUnpack(FileNames) == false)
500 {
501 _error->Error("Internal ordering error");
502 return Failed;
503 }
504
30e1eab5
AL
505 if (Debug == true)
506 clog << "Done ordering" << endl;
507
281daf46 508 bool DoneSomething = false;
6c139d6e
AL
509 for (pkgOrderList::iterator I = List->begin(); I != List->end(); I++)
510 {
511 PkgIterator Pkg(Cache,*I);
281daf46
AL
512
513 if (List->IsNow(Pkg) == false)
514 {
515 if (Debug == true)
516 clog << "Skipping already done " << Pkg.Name() << endl;
517 continue;
518 }
519
2fd65468 520 if (List->IsMissing(Pkg) == true)
281daf46
AL
521 {
522 if (Debug == true)
523 clog << "Sequence completed at" << Pkg.Name() << endl;
524 if (DoneSomething == false)
525 {
526 _error->Error("Internal Error, ordering was unable to handle the media swap");
527 return Failed;
528 }
529 return Incomplete;
530 }
6c139d6e
AL
531
532 // Sanity check
533 if (Cache[Pkg].Keep() == true && Pkg.State() == pkgCache::PkgIterator::NeedsNothing)
281daf46
AL
534 {
535 _error->Error("Internal Error, trying to manipulate a kept package");
536 return Failed;
537 }
6c139d6e
AL
538
539 // Perform a delete or an install
540 if (Cache[Pkg].Delete() == true)
541 {
542 if (SmartRemove(Pkg) == false)
281daf46 543 return Failed;
6c139d6e
AL
544 }
545 else
546 if (SmartUnPack(Pkg) == false)
281daf46
AL
547 return Failed;
548 DoneSomething = true;
6c139d6e
AL
549 }
550
551 // Final run through the configure phase
552 if (ConfigureAll() == false)
281daf46 553 return Failed;
6c139d6e
AL
554
555 // Sanity check
556 for (pkgOrderList::iterator I = List->begin(); I != List->end(); I++)
281daf46 557 {
6c139d6e 558 if (List->IsFlag(*I,pkgOrderList::Configured) == false)
281daf46
AL
559 {
560 _error->Error("Internal error, packages left unconfigured. %s",
561 PkgIterator(Cache,*I).Name());
562 return Failed;
563 }
564 }
565
566 return Completed;
6c139d6e
AL
567}
568 /*}}}*/
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.*/
281daf46 573pkgPackageManager::OrderResult pkgPackageManager::DoInstall()
6c139d6e 574{
281daf46
AL
575 OrderResult Res = OrderInstall();
576 if (Res != Failed)
577 if (Go() == false)
578 return Failed;
579 return Res;
6c139d6e
AL
580}
581 /*}}}*/