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