]> git.saurik.com Git - apt.git/blob - apt-pkg/packagemanager.cc
* merged with mainline
[apt.git] / apt-pkg / packagemanager.cc
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 // $Id: packagemanager.cc,v 1.30 2003/04/27 03:04:15 doogie Exp $
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__
17 #pragma implementation "apt-pkg/packagemanager.h"
18 #endif
19
20 #include <apt-pkg/packagemanager.h>
21 #include <apt-pkg/orderlist.h>
22 #include <apt-pkg/depcache.h>
23 #include <apt-pkg/error.h>
24 #include <apt-pkg/version.h>
25 #include <apt-pkg/acquire-item.h>
26 #include <apt-pkg/algorithms.h>
27 #include <apt-pkg/configuration.h>
28 #include <apt-pkg/sptr.h>
29
30 #include <apti18n.h>
31 #include <iostream>
32 /*}}}*/
33
34 using namespace std;
35
36 // PM::PackageManager - Constructor /*{{{*/
37 // ---------------------------------------------------------------------
38 /* */
39 pkgPackageManager::pkgPackageManager(pkgDepCache *pCache) : Cache(*pCache)
40 {
41 FileNames = new string[Cache.Head().PackageCount];
42 List = 0;
43 Debug = _config->FindB("Debug::pkgPackageManager",false);
44 }
45 /*}}}*/
46 // PM::PackageManager - Destructor /*{{{*/
47 // ---------------------------------------------------------------------
48 /* */
49 pkgPackageManager::~pkgPackageManager()
50 {
51 delete List;
52 delete [] FileNames;
53 }
54 /*}}}*/
55 // PM::GetArchives - Queue the archives for download /*{{{*/
56 // ---------------------------------------------------------------------
57 /* */
58 bool pkgPackageManager::GetArchives(pkgAcquire *Owner,pkgSourceList *Sources,
59 pkgRecords *Recs)
60 {
61 if (CreateOrderList() == false)
62 return false;
63
64 if (List->OrderUnpack() == false)
65 return _error->Error("Internal ordering error");
66
67 for (pkgOrderList::iterator I = List->begin(); I != List->end(); I++)
68 {
69 PkgIterator Pkg(Cache,*I);
70 FileNames[Pkg->ID] = string();
71
72 // Skip packages to erase
73 if (Cache[Pkg].Delete() == true)
74 continue;
75
76 // Skip Packages that need configure only.
77 if (Pkg.State() == pkgCache::PkgIterator::NeedsConfigure &&
78 Cache[Pkg].Keep() == true)
79 continue;
80
81 // Skip already processed packages
82 if (List->IsNow(Pkg) == false)
83 continue;
84
85 new pkgAcqArchive(Owner,Sources,Recs,Cache[Pkg].InstVerIter(Cache),
86 FileNames[Pkg->ID]);
87 }
88
89 return true;
90 }
91 /*}}}*/
92 // PM::FixMissing - Keep all missing packages /*{{{*/
93 // ---------------------------------------------------------------------
94 /* This is called to correct the installation when packages could not
95 be downloaded. */
96 bool pkgPackageManager::FixMissing()
97 {
98 pkgProblemResolver Resolve(&Cache);
99 List->SetFileList(FileNames);
100
101 bool Bad = false;
102 for (PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
103 {
104 if (List->IsMissing(I) == false)
105 continue;
106
107 // Okay, this file is missing and we need it. Mark it for keep
108 Bad = true;
109 Cache.MarkKeep(I);
110 }
111
112 // We have to empty the list otherwise it will not have the new changes
113 delete List;
114 List = 0;
115
116 if (Bad == false)
117 return true;
118
119 // Now downgrade everything that is broken
120 return Resolve.ResolveByKeep() == true && Cache.BrokenCount() == 0;
121 }
122 /*}}}*/
123
124 // PM::CreateOrderList - Create the ordering class /*{{{*/
125 // ---------------------------------------------------------------------
126 /* This populates the ordering list with all the packages that are
127 going to change. */
128 bool pkgPackageManager::CreateOrderList()
129 {
130 if (List != 0)
131 return true;
132
133 delete List;
134 List = new pkgOrderList(&Cache);
135
136 bool NoImmConfigure = !_config->FindB("APT::Immediate-Configure",true);
137
138 // Generate the list of affected packages and sort it
139 for (PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
140 {
141 // Ignore no-version packages
142 if (I->VersionList == 0)
143 continue;
144
145 // Mark the package and its dependends for immediate configuration
146 if (((I->Flags & pkgCache::Flag::Essential) == pkgCache::Flag::Essential ||
147 (I->Flags & pkgCache::Flag::Important) == pkgCache::Flag::Important) &&
148 NoImmConfigure == false)
149 {
150 List->Flag(I,pkgOrderList::Immediate);
151
152 // Look for other packages to make immediate configurea
153 if (Cache[I].InstallVer != 0)
154 for (DepIterator D = Cache[I].InstVerIter(Cache).DependsList();
155 D.end() == false; D++)
156 if (D->Type == pkgCache::Dep::Depends || D->Type == pkgCache::Dep::PreDepends)
157 List->Flag(D.TargetPkg(),pkgOrderList::Immediate);
158
159 // And again with the current version.
160 if (I->CurrentVer != 0)
161 for (DepIterator D = I.CurrentVer().DependsList();
162 D.end() == false; D++)
163 if (D->Type == pkgCache::Dep::Depends || D->Type == pkgCache::Dep::PreDepends)
164 List->Flag(D.TargetPkg(),pkgOrderList::Immediate);
165 }
166
167 // Not interesting
168 if ((Cache[I].Keep() == true ||
169 Cache[I].InstVerIter(Cache) == I.CurrentVer()) &&
170 I.State() == pkgCache::PkgIterator::NeedsNothing &&
171 (Cache[I].iFlags & pkgDepCache::ReInstall) != pkgDepCache::ReInstall &&
172 (I.Purge() != false || Cache[I].Mode != pkgDepCache::ModeDelete ||
173 (Cache[I].iFlags & pkgDepCache::Purge) != pkgDepCache::Purge))
174 continue;
175
176 // Append it to the list
177 List->push_back(I);
178 }
179
180 return true;
181 }
182 /*}}}*/
183 // PM::DepAlwaysTrue - Returns true if this dep is irrelevent /*{{{*/
184 // ---------------------------------------------------------------------
185 /* The restriction on provides is to eliminate the case when provides
186 are transitioning between valid states [ie exim to smail] */
187 bool pkgPackageManager::DepAlwaysTrue(DepIterator D)
188 {
189 if (D.TargetPkg()->ProvidesList != 0)
190 return false;
191
192 if ((Cache[D] & pkgDepCache::DepInstall) != 0 &&
193 (Cache[D] & pkgDepCache::DepNow) != 0)
194 return true;
195 return false;
196 }
197 /*}}}*/
198 // PM::CheckRConflicts - Look for reverse conflicts /*{{{*/
199 // ---------------------------------------------------------------------
200 /* This looks over the reverses for a conflicts line that needs early
201 removal. */
202 bool pkgPackageManager::CheckRConflicts(PkgIterator Pkg,DepIterator D,
203 const char *Ver)
204 {
205 for (;D.end() == false; D++)
206 {
207 if (D->Type != pkgCache::Dep::Conflicts &&
208 D->Type != pkgCache::Dep::Obsoletes)
209 continue;
210
211 // The package hasnt been changed
212 if (List->IsNow(Pkg) == false)
213 continue;
214
215 // Ignore self conflicts, ignore conflicts from irrelevent versions
216 if (D.ParentPkg() == Pkg || D.ParentVer() != D.ParentPkg().CurrentVer())
217 continue;
218
219 if (Cache.VS().CheckDep(Ver,D->CompareOp,D.TargetVer()) == false)
220 continue;
221
222 if (EarlyRemove(D.ParentPkg()) == false)
223 return _error->Error("Reverse conflicts early remove for package '%s' failed",
224 Pkg.Name());
225 }
226 return true;
227 }
228 /*}}}*/
229 // PM::ConfigureAll - Run the all out configuration /*{{{*/
230 // ---------------------------------------------------------------------
231 /* This configures every package. It is assumed they are all unpacked and
232 that the final configuration is valid. */
233 bool pkgPackageManager::ConfigureAll()
234 {
235 pkgOrderList OList(&Cache);
236
237 // Populate the order list
238 for (pkgOrderList::iterator I = List->begin(); I != List->end(); I++)
239 if (List->IsFlag(pkgCache::PkgIterator(Cache,*I),
240 pkgOrderList::UnPacked) == true)
241 OList.push_back(*I);
242
243 if (OList.OrderConfigure() == false)
244 return false;
245
246 // Perform the configuring
247 for (pkgOrderList::iterator I = OList.begin(); I != OList.end(); I++)
248 {
249 PkgIterator Pkg(Cache,*I);
250
251 if (Configure(Pkg) == false)
252 return false;
253
254 List->Flag(Pkg,pkgOrderList::Configured,pkgOrderList::States);
255 }
256
257 return true;
258 }
259 /*}}}*/
260 // PM::SmartConfigure - Perform immediate configuration of the pkg /*{{{*/
261 // ---------------------------------------------------------------------
262 /* This routine scheduals the configuration of the given package and all
263 of it's dependents. */
264 bool pkgPackageManager::SmartConfigure(PkgIterator Pkg)
265 {
266 pkgOrderList OList(&Cache);
267
268 if (DepAdd(OList,Pkg) == false)
269 return false;
270
271 if (OList.OrderConfigure() == false)
272 return false;
273
274 // Perform the configuring
275 for (pkgOrderList::iterator I = OList.begin(); I != OList.end(); I++)
276 {
277 PkgIterator Pkg(Cache,*I);
278
279 if (Configure(Pkg) == false)
280 return false;
281
282 List->Flag(Pkg,pkgOrderList::Configured,pkgOrderList::States);
283 }
284
285 // Sanity Check
286 if (List->IsFlag(Pkg,pkgOrderList::Configured) == false)
287 return _error->Error("Internal error, could not immediate configure %s",Pkg.Name());
288
289 return true;
290 }
291 /*}}}*/
292 // PM::DepAdd - Add all dependents to the oder list /*{{{*/
293 // ---------------------------------------------------------------------
294 /* This recursively adds all dependents to the order list */
295 bool pkgPackageManager::DepAdd(pkgOrderList &OList,PkgIterator Pkg,int Depth)
296 {
297 if (OList.IsFlag(Pkg,pkgOrderList::Added) == true)
298 return true;
299 if (List->IsFlag(Pkg,pkgOrderList::Configured) == true)
300 return true;
301 if (List->IsFlag(Pkg,pkgOrderList::UnPacked) == false)
302 return false;
303
304 // Put the package on the list
305 OList.push_back(Pkg);
306 OList.Flag(Pkg,pkgOrderList::Added);
307 Depth++;
308
309 // Check the dependencies to see if they are all satisfied.
310 bool Bad = false;
311 for (DepIterator D = Cache[Pkg].InstVerIter(Cache).DependsList(); D.end() == false;)
312 {
313 if (D->Type != pkgCache::Dep::Depends && D->Type != pkgCache::Dep::PreDepends)
314 {
315 D++;
316 continue;
317 }
318
319 // Grok or groups
320 Bad = true;
321 for (bool LastOR = true; D.end() == false && LastOR == true; D++)
322 {
323 LastOR = (D->CompareOp & pkgCache::Dep::Or) == pkgCache::Dep::Or;
324
325 if (Bad == false)
326 continue;
327
328 SPtrArray<Version *> VList = D.AllTargets();
329 for (Version **I = VList; *I != 0 && Bad == true; I++)
330 {
331 VerIterator Ver(Cache,*I);
332 PkgIterator Pkg = Ver.ParentPkg();
333
334 // See if the current version is ok
335 if (Pkg.CurrentVer() == Ver && List->IsNow(Pkg) == true &&
336 Pkg.State() == PkgIterator::NeedsNothing)
337 {
338 Bad = false;
339 continue;
340 }
341
342 // Not the install version
343 if (Cache[Pkg].InstallVer != *I ||
344 (Cache[Pkg].Keep() == true && Pkg.State() == PkgIterator::NeedsNothing))
345 continue;
346
347 if (List->IsFlag(Pkg,pkgOrderList::UnPacked) == true)
348 Bad = !DepAdd(OList,Pkg,Depth);
349 if (List->IsFlag(Pkg,pkgOrderList::Configured) == true)
350 Bad = false;
351 }
352 }
353
354 if (Bad == true)
355 {
356 OList.Flag(Pkg,0,pkgOrderList::Added);
357 OList.pop_back();
358 Depth--;
359 return false;
360 }
361 }
362
363 Depth--;
364 return true;
365 }
366 /*}}}*/
367 // PM::EarlyRemove - Perform removal of packages before their time /*{{{*/
368 // ---------------------------------------------------------------------
369 /* This is called to deal with conflicts arising from unpacking */
370 bool pkgPackageManager::EarlyRemove(PkgIterator Pkg)
371 {
372 if (List->IsNow(Pkg) == false)
373 return true;
374
375 // Already removed it
376 if (List->IsFlag(Pkg,pkgOrderList::Removed) == true)
377 return true;
378
379 // Woops, it will not be re-installed!
380 if (List->IsFlag(Pkg,pkgOrderList::InList) == false)
381 return false;
382
383 // Essential packages get special treatment
384 bool IsEssential = false;
385 if ((Pkg->Flags & pkgCache::Flag::Essential) != 0)
386 IsEssential = true;
387
388 /* Check for packages that are the dependents of essential packages and
389 promote them too */
390 if (Pkg->CurrentVer != 0)
391 {
392 for (DepIterator D = Pkg.RevDependsList(); D.end() == false &&
393 IsEssential == false; D++)
394 if (D->Type == pkgCache::Dep::Depends || D->Type == pkgCache::Dep::PreDepends)
395 if ((D.ParentPkg()->Flags & pkgCache::Flag::Essential) != 0)
396 IsEssential = true;
397 }
398
399 if (IsEssential == true)
400 {
401 if (_config->FindB("APT::Force-LoopBreak",false) == false)
402 return _error->Error(_("This installation run will require temporarily "
403 "removing the essential package %s due to a "
404 "Conflicts/Pre-Depends loop. This is often bad, "
405 "but if you really want to do it, activate the "
406 "APT::Force-LoopBreak option."),Pkg.Name());
407 }
408
409 bool Res = SmartRemove(Pkg);
410 if (Cache[Pkg].Delete() == false)
411 List->Flag(Pkg,pkgOrderList::Removed,pkgOrderList::States);
412
413 return Res;
414 }
415 /*}}}*/
416 // PM::SmartRemove - Removal Helper /*{{{*/
417 // ---------------------------------------------------------------------
418 /* */
419 bool pkgPackageManager::SmartRemove(PkgIterator Pkg)
420 {
421 if (List->IsNow(Pkg) == false)
422 return true;
423
424 List->Flag(Pkg,pkgOrderList::Configured,pkgOrderList::States);
425 return Remove(Pkg,(Cache[Pkg].iFlags & pkgDepCache::Purge) == pkgDepCache::Purge);
426 }
427 /*}}}*/
428 // PM::SmartUnPack - Install helper /*{{{*/
429 // ---------------------------------------------------------------------
430 /* This performs the task of handling pre-depends. */
431 bool pkgPackageManager::SmartUnPack(PkgIterator Pkg)
432 {
433 // Check if it is already unpacked
434 if (Pkg.State() == pkgCache::PkgIterator::NeedsConfigure &&
435 Cache[Pkg].Keep() == true)
436 {
437 List->Flag(Pkg,pkgOrderList::UnPacked,pkgOrderList::States);
438 if (List->IsFlag(Pkg,pkgOrderList::Immediate) == true)
439 if (SmartConfigure(Pkg) == false)
440 return _error->Error("Internal Error, Could not perform immediate configuration (1) on %s",Pkg.Name());
441 return true;
442 }
443
444 /* See if this packages install version has any predependencies
445 that are not met by 'now' packages. */
446 for (DepIterator D = Cache[Pkg].InstVerIter(Cache).DependsList();
447 D.end() == false; )
448 {
449 // Compute a single dependency element (glob or)
450 pkgCache::DepIterator Start;
451 pkgCache::DepIterator End;
452 D.GlobOr(Start,End);
453
454 while (End->Type == pkgCache::Dep::PreDepends)
455 {
456 // Look for possible ok targets.
457 SPtrArray<Version *> VList = Start.AllTargets();
458 bool Bad = true;
459 for (Version **I = VList; *I != 0 && Bad == true; I++)
460 {
461 VerIterator Ver(Cache,*I);
462 PkgIterator Pkg = Ver.ParentPkg();
463
464 // See if the current version is ok
465 if (Pkg.CurrentVer() == Ver && List->IsNow(Pkg) == true &&
466 Pkg.State() == PkgIterator::NeedsNothing)
467 {
468 Bad = false;
469 continue;
470 }
471 }
472
473 // Look for something that could be configured.
474 for (Version **I = VList; *I != 0 && Bad == true; I++)
475 {
476 VerIterator Ver(Cache,*I);
477 PkgIterator Pkg = Ver.ParentPkg();
478
479 // Not the install version
480 if (Cache[Pkg].InstallVer != *I ||
481 (Cache[Pkg].Keep() == true && Pkg.State() == PkgIterator::NeedsNothing))
482 continue;
483
484 Bad = !SmartConfigure(Pkg);
485 }
486
487 /* If this or element did not match then continue on to the
488 next or element until a matching element is found */
489 if (Bad == true)
490 {
491 // This triggers if someone make a pre-depends/depend loop.
492 if (Start == End)
493 return _error->Error("Couldn't configure pre-depend %s for %s, "
494 "probably a dependency cycle.",
495 End.TargetPkg().Name(),Pkg.Name());
496 Start++;
497 }
498 else
499 break;
500 }
501
502 if (End->Type == pkgCache::Dep::Conflicts ||
503 End->Type == pkgCache::Dep::Obsoletes)
504 {
505 /* Look for conflicts. Two packages that are both in the install
506 state cannot conflict so we don't check.. */
507 SPtrArray<Version *> VList = End.AllTargets();
508 for (Version **I = VList; *I != 0; I++)
509 {
510 VerIterator Ver(Cache,*I);
511 PkgIterator Pkg = Ver.ParentPkg();
512
513 // See if the current version is conflicting
514 if (Pkg.CurrentVer() == Ver && List->IsNow(Pkg) == true)
515 {
516 if (EarlyRemove(Pkg) == false)
517 return _error->Error("Internal Error, Could not early remove %s",Pkg.Name());
518 }
519 }
520 }
521 }
522
523 // Check for reverse conflicts.
524 if (CheckRConflicts(Pkg,Pkg.RevDependsList(),
525 Cache[Pkg].InstVerIter(Cache).VerStr()) == false)
526 return false;
527
528 for (PrvIterator P = Cache[Pkg].InstVerIter(Cache).ProvidesList();
529 P.end() == false; P++)
530 CheckRConflicts(Pkg,P.ParentPkg().RevDependsList(),P.ProvideVersion());
531
532 if (Install(Pkg,FileNames[Pkg->ID]) == false)
533 return false;
534
535 List->Flag(Pkg,pkgOrderList::UnPacked,pkgOrderList::States);
536
537 // Perform immedate configuration of the package.
538 if (List->IsFlag(Pkg,pkgOrderList::Immediate) == true)
539 if (SmartConfigure(Pkg) == false)
540 return _error->Error("Internal Error, Could not perform immediate configuration (2) on %s",Pkg.Name());
541
542 return true;
543 }
544 /*}}}*/
545 // PM::OrderInstall - Installation ordering routine /*{{{*/
546 // ---------------------------------------------------------------------
547 /* */
548 pkgPackageManager::OrderResult pkgPackageManager::OrderInstall()
549 {
550 if (CreateOrderList() == false)
551 return Failed;
552
553 Reset();
554
555 if (Debug == true)
556 clog << "Begining to order" << endl;
557
558 if (List->OrderUnpack(FileNames) == false)
559 {
560 _error->Error("Internal ordering error");
561 return Failed;
562 }
563
564 if (Debug == true)
565 clog << "Done ordering" << endl;
566
567 bool DoneSomething = false;
568 for (pkgOrderList::iterator I = List->begin(); I != List->end(); I++)
569 {
570 PkgIterator Pkg(Cache,*I);
571
572 if (List->IsNow(Pkg) == false)
573 {
574 if (Debug == true)
575 clog << "Skipping already done " << Pkg.Name() << endl;
576 continue;
577 }
578
579 if (List->IsMissing(Pkg) == true)
580 {
581 if (Debug == true)
582 clog << "Sequence completed at " << Pkg.Name() << endl;
583 if (DoneSomething == false)
584 {
585 _error->Error("Internal Error, ordering was unable to handle the media swap");
586 return Failed;
587 }
588 return Incomplete;
589 }
590
591 // Sanity check
592 if (Cache[Pkg].Keep() == true &&
593 Pkg.State() == pkgCache::PkgIterator::NeedsNothing &&
594 (Cache[Pkg].iFlags & pkgDepCache::ReInstall) != pkgDepCache::ReInstall)
595 {
596 _error->Error("Internal Error, trying to manipulate a kept package (%s)",Pkg.Name());
597 return Failed;
598 }
599
600 // Perform a delete or an install
601 if (Cache[Pkg].Delete() == true)
602 {
603 if (SmartRemove(Pkg) == false)
604 return Failed;
605 }
606 else
607 if (SmartUnPack(Pkg) == false)
608 return Failed;
609 DoneSomething = true;
610 }
611
612 // Final run through the configure phase
613 if (ConfigureAll() == false)
614 return Failed;
615
616 // Sanity check
617 for (pkgOrderList::iterator I = List->begin(); I != List->end(); I++)
618 {
619 if (List->IsFlag(*I,pkgOrderList::Configured) == false)
620 {
621 _error->Error("Internal error, packages left unconfigured. %s",
622 PkgIterator(Cache,*I).Name());
623 return Failed;
624 }
625 }
626
627 return Completed;
628 }
629 /*}}}*/
630 // PM::DoInstall - Does the installation /*{{{*/
631 // ---------------------------------------------------------------------
632 /* This uses the filenames in FileNames and the information in the
633 DepCache to perform the installation of packages.*/
634 pkgPackageManager::OrderResult pkgPackageManager::DoInstall(int status_fd)
635 {
636 OrderResult Res = OrderInstall();
637 if(Debug)
638 std::clog << "OrderInstall() returned: " << Res << std::endl;
639 if (Res != Failed)
640 if (Go(status_fd) == false)
641 return Failed;
642 return Res;
643 }
644 /*}}}*/