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