re-add APT::Keep-Fds:: for the dpkg status-fd in dpkgpm.cc as we always need this
[ntk/apt.git] / apt-private / private-install.cc
1 // Include Files /*{{{*/
2 #include <config.h>
3
4 #include <apt-pkg/aptconfiguration.h>
5 #include <apt-pkg/error.h>
6 #include <apt-pkg/cmndline.h>
7 #include <apt-pkg/init.h>
8 #include <apt-pkg/depcache.h>
9 #include <apt-pkg/sourcelist.h>
10 #include <apt-pkg/algorithms.h>
11 #include <apt-pkg/acquire-item.h>
12 #include <apt-pkg/strutl.h>
13 #include <apt-pkg/fileutl.h>
14 #include <apt-pkg/clean.h>
15 #include <apt-pkg/srcrecords.h>
16 #include <apt-pkg/version.h>
17 #include <apt-pkg/cachefile.h>
18 #include <apt-pkg/cacheset.h>
19 #include <apt-pkg/sptr.h>
20 #include <apt-pkg/md5.h>
21 #include <apt-pkg/versionmatch.h>
22 #include <apt-pkg/progress.h>
23 #include <apt-pkg/pkgsystem.h>
24 #include <apt-pkg/pkgrecords.h>
25 #include <apt-pkg/indexfile.h>
26 #include <apt-pkg/iprogress.h>
27
28 #include <set>
29 #include <locale.h>
30 #include <langinfo.h>
31 #include <fstream>
32 #include <termios.h>
33 #include <sys/ioctl.h>
34 #include <sys/stat.h>
35 #include <sys/statfs.h>
36 #include <sys/statvfs.h>
37 #include <signal.h>
38 #include <unistd.h>
39 #include <stdio.h>
40 #include <errno.h>
41 #include <regex.h>
42 #include <sys/wait.h>
43 #include <sstream>
44
45 #include "private-install.h"
46 #include "private-download.h"
47 #include "private-cachefile.h"
48 #include "private-output.h"
49 #include "private-cacheset.h"
50 #include "acqprogress.h"
51
52 #include <apti18n.h>
53 /*}}}*/
54
55 // InstallPackages - Actually download and install the packages /*{{{*/
56 // ---------------------------------------------------------------------
57 /* This displays the informative messages describing what is going to
58 happen and then calls the download routines */
59 bool InstallPackages(CacheFile &Cache,bool ShwKept,bool Ask, bool Safety)
60 {
61 if (_config->FindB("APT::Get::Purge",false) == true)
62 {
63 pkgCache::PkgIterator I = Cache->PkgBegin();
64 for (; I.end() == false; ++I)
65 {
66 if (I.Purge() == false && Cache[I].Mode == pkgDepCache::ModeDelete)
67 Cache->MarkDelete(I,true);
68 }
69 }
70
71 bool Fail = false;
72 bool Essential = false;
73
74 // Show all the various warning indicators
75 ShowDel(c1out,Cache);
76 ShowNew(c1out,Cache);
77 if (ShwKept == true)
78 ShowKept(c1out,Cache);
79 Fail |= !ShowHold(c1out,Cache);
80 if (_config->FindB("APT::Get::Show-Upgraded",true) == true)
81 ShowUpgraded(c1out,Cache);
82 Fail |= !ShowDowngraded(c1out,Cache);
83 if (_config->FindB("APT::Get::Download-Only",false) == false)
84 Essential = !ShowEssential(c1out,Cache);
85 Fail |= Essential;
86 Stats(c1out,Cache);
87
88 // Sanity check
89 if (Cache->BrokenCount() != 0)
90 {
91 ShowBroken(c1out,Cache,false);
92 return _error->Error(_("Internal error, InstallPackages was called with broken packages!"));
93 }
94
95 if (Cache->DelCount() == 0 && Cache->InstCount() == 0 &&
96 Cache->BadCount() == 0)
97 return true;
98
99 // No remove flag
100 if (Cache->DelCount() != 0 && _config->FindB("APT::Get::Remove",true) == false)
101 return _error->Error(_("Packages need to be removed but remove is disabled."));
102
103 // Run the simulator ..
104 if (_config->FindB("APT::Get::Simulate") == true)
105 {
106 pkgSimulate PM(Cache);
107
108 int status_fd = _config->FindI("APT::Status-Fd",-1);
109 APT::Progress::PackageManager *progress = NULL;
110 if (status_fd > 0)
111 progress = new APT::Progress::PackageManagerProgressFd(status_fd);
112 pkgPackageManager::OrderResult Res = PM.DoInstall(progress);
113 delete progress;
114
115 if (Res == pkgPackageManager::Failed)
116 return false;
117 if (Res != pkgPackageManager::Completed)
118 return _error->Error(_("Internal error, Ordering didn't finish"));
119 return true;
120 }
121
122 // Create the text record parser
123 pkgRecords Recs(Cache);
124 if (_error->PendingError() == true)
125 return false;
126
127 // Create the download object
128 pkgAcquire Fetcher;
129 AcqTextStatus Stat(ScreenWidth,_config->FindI("quiet",0));
130 if (_config->FindB("APT::Get::Print-URIs", false) == true)
131 {
132 // force a hashsum for compatibility reasons
133 _config->CndSet("Acquire::ForceHash", "md5sum");
134 }
135 else if (Fetcher.Setup(&Stat, _config->FindDir("Dir::Cache::Archives")) == false)
136 return false;
137
138 // Read the source list
139 if (Cache.BuildSourceList() == false)
140 return false;
141 pkgSourceList *List = Cache.GetSourceList();
142
143 // Create the package manager and prepare to download
144 SPtr<pkgPackageManager> PM= _system->CreatePM(Cache);
145 if (PM->GetArchives(&Fetcher,List,&Recs) == false ||
146 _error->PendingError() == true)
147 return false;
148
149 // Display statistics
150 unsigned long long FetchBytes = Fetcher.FetchNeeded();
151 unsigned long long FetchPBytes = Fetcher.PartialPresent();
152 unsigned long long DebBytes = Fetcher.TotalNeeded();
153 if (DebBytes != Cache->DebSize())
154 {
155 c0out << DebBytes << ',' << Cache->DebSize() << std::endl;
156 c0out << _("How odd.. The sizes didn't match, email apt@packages.debian.org") << std::endl;
157 }
158
159 // Number of bytes
160 if (DebBytes != FetchBytes)
161 //TRANSLATOR: The required space between number and unit is already included
162 // in the replacement strings, so %sB will be correctly translate in e.g. 1,5 MB
163 ioprintf(c1out,_("Need to get %sB/%sB of archives.\n"),
164 SizeToStr(FetchBytes).c_str(),SizeToStr(DebBytes).c_str());
165 else if (DebBytes != 0)
166 //TRANSLATOR: The required space between number and unit is already included
167 // in the replacement string, so %sB will be correctly translate in e.g. 1,5 MB
168 ioprintf(c1out,_("Need to get %sB of archives.\n"),
169 SizeToStr(DebBytes).c_str());
170
171 // Size delta
172 if (Cache->UsrSize() >= 0)
173 //TRANSLATOR: The required space between number and unit is already included
174 // in the replacement string, so %sB will be correctly translate in e.g. 1,5 MB
175 ioprintf(c1out,_("After this operation, %sB of additional disk space will be used.\n"),
176 SizeToStr(Cache->UsrSize()).c_str());
177 else
178 //TRANSLATOR: The required space between number and unit is already included
179 // in the replacement string, so %sB will be correctly translate in e.g. 1,5 MB
180 ioprintf(c1out,_("After this operation, %sB disk space will be freed.\n"),
181 SizeToStr(-1*Cache->UsrSize()).c_str());
182
183 if (_error->PendingError() == true)
184 return false;
185
186 /* Check for enough free space, but only if we are actually going to
187 download */
188 if (_config->FindB("APT::Get::Print-URIs") == false &&
189 _config->FindB("APT::Get::Download",true) == true)
190 {
191 struct statvfs Buf;
192 std::string OutputDir = _config->FindDir("Dir::Cache::Archives");
193 if (statvfs(OutputDir.c_str(),&Buf) != 0) {
194 if (errno == EOVERFLOW)
195 return _error->WarningE("statvfs",_("Couldn't determine free space in %s"),
196 OutputDir.c_str());
197 else
198 return _error->Errno("statvfs",_("Couldn't determine free space in %s"),
199 OutputDir.c_str());
200 } else if (unsigned(Buf.f_bfree) < (FetchBytes - FetchPBytes)/Buf.f_bsize)
201 {
202 struct statfs Stat;
203 if (statfs(OutputDir.c_str(),&Stat) != 0
204 #if HAVE_STRUCT_STATFS_F_TYPE
205 || unsigned(Stat.f_type) != RAMFS_MAGIC
206 #endif
207 )
208 return _error->Error(_("You don't have enough free space in %s."),
209 OutputDir.c_str());
210 }
211 }
212
213 // Fail safe check
214 if (_config->FindI("quiet",0) >= 2 ||
215 _config->FindB("APT::Get::Assume-Yes",false) == true)
216 {
217 if (Fail == true && _config->FindB("APT::Get::Force-Yes",false) == false)
218 return _error->Error(_("There are problems and -y was used without --force-yes"));
219 }
220
221 if (Essential == true && Safety == true)
222 {
223 if (_config->FindB("APT::Get::Trivial-Only",false) == true)
224 return _error->Error(_("Trivial Only specified but this is not a trivial operation."));
225
226 // TRANSLATOR: This string needs to be typed by the user as a confirmation, so be
227 // careful with hard to type or special characters (like non-breaking spaces)
228 const char *Prompt = _("Yes, do as I say!");
229 ioprintf(c2out,
230 _("You are about to do something potentially harmful.\n"
231 "To continue type in the phrase '%s'\n"
232 " ?] "),Prompt);
233 c2out << std::flush;
234 if (AnalPrompt(Prompt) == false)
235 {
236 c2out << _("Abort.") << std::endl;
237 exit(1);
238 }
239 }
240 else
241 {
242 // Prompt to continue
243 if (Ask == true || Fail == true)
244 {
245 if (_config->FindB("APT::Get::Trivial-Only",false) == true)
246 return _error->Error(_("Trivial Only specified but this is not a trivial operation."));
247
248 if (_config->FindI("quiet",0) < 2 &&
249 _config->FindB("APT::Get::Assume-Yes",false) == false)
250 {
251 c2out << _("Do you want to continue?") << std::flush;
252 if (YnPrompt() == false)
253 {
254 c2out << _("Abort.") << std::endl;
255 exit(1);
256 }
257 }
258 }
259 }
260
261 // Just print out the uris an exit if the --print-uris flag was used
262 if (_config->FindB("APT::Get::Print-URIs") == true)
263 {
264 pkgAcquire::UriIterator I = Fetcher.UriBegin();
265 for (; I != Fetcher.UriEnd(); ++I)
266 std::cout << '\'' << I->URI << "' " << flNotDir(I->Owner->DestFile) << ' ' <<
267 I->Owner->FileSize << ' ' << I->Owner->HashSum() << std::endl;
268 return true;
269 }
270
271 if (!CheckAuth(Fetcher, true))
272 return false;
273
274 /* Unlock the dpkg lock if we are not going to be doing an install
275 after. */
276 if (_config->FindB("APT::Get::Download-Only",false) == true)
277 _system->UnLock();
278
279 // Run it
280 while (1)
281 {
282 bool Transient = false;
283 if (_config->FindB("APT::Get::Download",true) == false)
284 {
285 for (pkgAcquire::ItemIterator I = Fetcher.ItemsBegin(); I < Fetcher.ItemsEnd();)
286 {
287 if ((*I)->Local == true)
288 {
289 ++I;
290 continue;
291 }
292
293 // Close the item and check if it was found in cache
294 (*I)->Finished();
295 if ((*I)->Complete == false)
296 Transient = true;
297
298 // Clear it out of the fetch list
299 delete *I;
300 I = Fetcher.ItemsBegin();
301 }
302 }
303
304 bool Failed = false;
305 if (AcquireRun(Fetcher, 0, &Failed, &Transient) == false)
306 return false;
307
308 /* If we are in no download mode and missing files and there were
309 'failures' then the user must specify -m. Furthermore, there
310 is no such thing as a transient error in no-download mode! */
311 if (Transient == true &&
312 _config->FindB("APT::Get::Download",true) == false)
313 {
314 Transient = false;
315 Failed = true;
316 }
317
318 if (_config->FindB("APT::Get::Download-Only",false) == true)
319 {
320 if (Failed == true && _config->FindB("APT::Get::Fix-Missing",false) == false)
321 return _error->Error(_("Some files failed to download"));
322 c1out << _("Download complete and in download only mode") << std::endl;
323 return true;
324 }
325
326 if (Failed == true && _config->FindB("APT::Get::Fix-Missing",false) == false)
327 {
328 return _error->Error(_("Unable to fetch some archives, maybe run apt-get update or try with --fix-missing?"));
329 }
330
331 if (Transient == true && Failed == true)
332 return _error->Error(_("--fix-missing and media swapping is not currently supported"));
333
334 // Try to deal with missing package files
335 if (Failed == true && PM->FixMissing() == false)
336 {
337 c2out << _("Unable to correct missing packages.") << std::endl;
338 return _error->Error(_("Aborting install."));
339 }
340
341 _system->UnLock();
342
343 // FIXME: make this a factory
344 // select the right progress
345 int status_fd = _config->FindI("APT::Status-Fd",-1);
346
347 APT::Progress::PackageManager *progress = NULL;
348 if (status_fd > 0)
349 progress = new APT::Progress::PackageManagerProgressFd(status_fd);
350 else if(_config->FindB("Dpkg::Progress-Fancy", false) == true)
351 progress = new APT::Progress::PackageManagerFancy();
352 else if (_config->FindB("Dpkg::Progress",
353 _config->FindB("DpkgPM::Progress", false)) == true)
354 progress = new APT::Progress::PackageManagerText();
355 else
356 progress = new APT::Progress::PackageManager();
357 pkgPackageManager::OrderResult Res = PM->DoInstall(progress);
358 delete progress;
359
360 if (Res == pkgPackageManager::Failed || _error->PendingError() == true)
361 return false;
362 if (Res == pkgPackageManager::Completed)
363 break;
364
365 // Reload the fetcher object and loop again for media swapping
366 Fetcher.Shutdown();
367 if (PM->GetArchives(&Fetcher,List,&Recs) == false)
368 return false;
369
370 _system->Lock();
371 }
372
373 std::set<std::string> const disappearedPkgs = PM->GetDisappearedPackages();
374 if (disappearedPkgs.empty() == true)
375 return true;
376
377 std::string disappear;
378 for (std::set<std::string>::const_iterator d = disappearedPkgs.begin();
379 d != disappearedPkgs.end(); ++d)
380 disappear.append(*d).append(" ");
381
382 ShowList(c1out, P_("The following package disappeared from your system as\n"
383 "all files have been overwritten by other packages:",
384 "The following packages disappeared from your system as\n"
385 "all files have been overwritten by other packages:", disappearedPkgs.size()), disappear, "");
386 c0out << _("Note: This is done automatically and on purpose by dpkg.") << std::endl;
387
388 return true;
389 }
390 /*}}}*/
391 // DoAutomaticRemove - Remove all automatic unused packages /*{{{*/
392 // ---------------------------------------------------------------------
393 /* Remove unused automatic packages */
394 bool DoAutomaticRemove(CacheFile &Cache)
395 {
396 bool Debug = _config->FindI("Debug::pkgAutoRemove",false);
397 bool doAutoRemove = _config->FindB("APT::Get::AutomaticRemove", false);
398 bool hideAutoRemove = _config->FindB("APT::Get::HideAutoRemove");
399
400 pkgDepCache::ActionGroup group(*Cache);
401 if(Debug)
402 std::cout << "DoAutomaticRemove()" << std::endl;
403
404 if (doAutoRemove == true &&
405 _config->FindB("APT::Get::Remove",true) == false)
406 {
407 c1out << _("We are not supposed to delete stuff, can't start "
408 "AutoRemover") << std::endl;
409 return false;
410 }
411
412 bool purgePkgs = _config->FindB("APT::Get::Purge", false);
413 bool smallList = (hideAutoRemove == false &&
414 strcasecmp(_config->Find("APT::Get::HideAutoRemove","").c_str(),"small") == 0);
415
416 unsigned long autoRemoveCount = 0;
417 APT::PackageSet tooMuch;
418 APT::PackageList autoRemoveList;
419 // look over the cache to see what can be removed
420 for (unsigned J = 0; J < Cache->Head().PackageCount; ++J)
421 {
422 pkgCache::PkgIterator Pkg(Cache,Cache.List[J]);
423 if (Cache[Pkg].Garbage)
424 {
425 if(Pkg.CurrentVer() != 0 || Cache[Pkg].Install())
426 if(Debug)
427 std::cout << "We could delete %s" << Pkg.FullName(true).c_str() << std::endl;
428
429 if (doAutoRemove)
430 {
431 if(Pkg.CurrentVer() != 0 &&
432 Pkg->CurrentState != pkgCache::State::ConfigFiles)
433 Cache->MarkDelete(Pkg, purgePkgs, 0, false);
434 else
435 Cache->MarkKeep(Pkg, false, false);
436 }
437 else
438 {
439 if (hideAutoRemove == false && Cache[Pkg].Delete() == false)
440 autoRemoveList.insert(Pkg);
441 // if the package is a new install and already garbage we don't need to
442 // install it in the first place, so nuke it instead of show it
443 if (Cache[Pkg].Install() == true && Pkg.CurrentVer() == 0)
444 {
445 if (Pkg.CandVersion() != 0)
446 tooMuch.insert(Pkg);
447 Cache->MarkDelete(Pkg, false, 0, false);
448 }
449 // only show stuff in the list that is not yet marked for removal
450 else if(hideAutoRemove == false && Cache[Pkg].Delete() == false)
451 ++autoRemoveCount;
452 }
453 }
454 }
455
456 // we could have removed a new dependency of a garbage package,
457 // so check if a reverse depends is broken and if so install it again.
458 if (tooMuch.empty() == false && (Cache->BrokenCount() != 0 || Cache->PolicyBrokenCount() != 0))
459 {
460 bool Changed;
461 do {
462 Changed = false;
463 for (APT::PackageSet::const_iterator Pkg = tooMuch.begin();
464 Pkg != tooMuch.end(); ++Pkg)
465 {
466 APT::PackageSet too;
467 too.insert(*Pkg);
468 for (pkgCache::PrvIterator Prv = Cache[Pkg].CandidateVerIter(Cache).ProvidesList();
469 Prv.end() == false; ++Prv)
470 too.insert(Prv.ParentPkg());
471 for (APT::PackageSet::const_iterator P = too.begin(); P != too.end(); ++P)
472 {
473 for (pkgCache::DepIterator R = P.RevDependsList();
474 R.end() == false; ++R)
475 {
476 if (R.IsNegative() == true ||
477 Cache->IsImportantDep(R) == false)
478 continue;
479 pkgCache::PkgIterator N = R.ParentPkg();
480 if (N.end() == true || (N->CurrentVer == 0 && (*Cache)[N].Install() == false))
481 continue;
482 if (Debug == true)
483 std::clog << "Save " << Pkg << " as another installed garbage package depends on it" << std::endl;
484 Cache->MarkInstall(Pkg, false, 0, false);
485 if (hideAutoRemove == false)
486 ++autoRemoveCount;
487 tooMuch.erase(Pkg);
488 Changed = true;
489 break;
490 }
491 if (Changed == true)
492 break;
493 }
494 if (Changed == true)
495 break;
496 }
497 } while (Changed == true);
498 }
499
500 std::string autoremovelist, autoremoveversions;
501 if (smallList == false && autoRemoveCount != 0)
502 {
503 for (APT::PackageList::const_iterator Pkg = autoRemoveList.begin(); Pkg != autoRemoveList.end(); ++Pkg)
504 {
505 if (Cache[Pkg].Garbage == false)
506 continue;
507 autoremovelist += Pkg.FullName(true) + " ";
508 autoremoveversions += std::string(Cache[Pkg].CandVersion) + "\n";
509 }
510 }
511
512 // Now see if we had destroyed anything (if we had done anything)
513 if (Cache->BrokenCount() != 0)
514 {
515 c1out << _("Hmm, seems like the AutoRemover destroyed something which really\n"
516 "shouldn't happen. Please file a bug report against apt.") << std::endl;
517 c1out << std::endl;
518 c1out << _("The following information may help to resolve the situation:") << std::endl;
519 c1out << std::endl;
520 ShowBroken(c1out,Cache,false);
521
522 return _error->Error(_("Internal Error, AutoRemover broke stuff"));
523 }
524
525 // if we don't remove them, we should show them!
526 if (doAutoRemove == false && (autoremovelist.empty() == false || autoRemoveCount != 0))
527 {
528 if (smallList == false)
529 ShowList(c1out, P_("The following package was automatically installed and is no longer required:",
530 "The following packages were automatically installed and are no longer required:",
531 autoRemoveCount), autoremovelist, autoremoveversions);
532 else
533 ioprintf(c1out, P_("%lu package was automatically installed and is no longer required.\n",
534 "%lu packages were automatically installed and are no longer required.\n", autoRemoveCount), autoRemoveCount);
535 c1out << P_("Use 'apt-get autoremove' to remove it.", "Use 'apt-get autoremove' to remove them.", autoRemoveCount) << std::endl;
536 }
537 return true;
538 }
539 /*}}}*/
540 // DoCacheManipulationFromCommandLine /*{{{*/
541 static const unsigned short MOD_REMOVE = 1;
542 static const unsigned short MOD_INSTALL = 2;
543
544 bool DoCacheManipulationFromCommandLine(CommandLine &CmdL, CacheFile &Cache)
545 {
546 std::map<unsigned short, APT::VersionSet> verset;
547 return DoCacheManipulationFromCommandLine(CmdL, Cache, verset);
548 }
549 bool DoCacheManipulationFromCommandLine(CommandLine &CmdL, CacheFile &Cache,
550 std::map<unsigned short, APT::VersionSet> &verset)
551 {
552
553 // Enter the special broken fixing mode if the user specified arguments
554 bool BrokenFix = false;
555 if (Cache->BrokenCount() != 0)
556 BrokenFix = true;
557
558 SPtr<pkgProblemResolver> Fix;
559 if (_config->FindB("APT::Get::CallResolver", true) == true)
560 Fix = new pkgProblemResolver(Cache);
561
562 unsigned short fallback = MOD_INSTALL;
563 if (strcasecmp(CmdL.FileList[0],"remove") == 0)
564 fallback = MOD_REMOVE;
565 else if (strcasecmp(CmdL.FileList[0], "purge") == 0)
566 {
567 _config->Set("APT::Get::Purge", true);
568 fallback = MOD_REMOVE;
569 }
570 else if (strcasecmp(CmdL.FileList[0], "autoremove") == 0)
571 {
572 _config->Set("APT::Get::AutomaticRemove", "true");
573 fallback = MOD_REMOVE;
574 }
575
576 std::list<APT::VersionSet::Modifier> mods;
577 mods.push_back(APT::VersionSet::Modifier(MOD_INSTALL, "+",
578 APT::VersionSet::Modifier::POSTFIX, APT::VersionSet::CANDIDATE));
579 mods.push_back(APT::VersionSet::Modifier(MOD_REMOVE, "-",
580 APT::VersionSet::Modifier::POSTFIX, APT::VersionSet::NEWEST));
581 CacheSetHelperAPTGet helper(c0out);
582 verset = APT::VersionSet::GroupedFromCommandLine(Cache,
583 CmdL.FileList + 1, mods, fallback, helper);
584
585 if (_error->PendingError() == true)
586 {
587 helper.showVirtualPackageErrors(Cache);
588 return false;
589 }
590
591
592 TryToInstall InstallAction(Cache, Fix, BrokenFix);
593 TryToRemove RemoveAction(Cache, Fix);
594
595 // new scope for the ActionGroup
596 {
597 pkgDepCache::ActionGroup group(Cache);
598 unsigned short const order[] = { MOD_REMOVE, MOD_INSTALL, 0 };
599
600 for (unsigned short i = 0; order[i] != 0; ++i)
601 {
602 if (order[i] == MOD_INSTALL)
603 InstallAction = std::for_each(verset[MOD_INSTALL].begin(), verset[MOD_INSTALL].end(), InstallAction);
604 else if (order[i] == MOD_REMOVE)
605 RemoveAction = std::for_each(verset[MOD_REMOVE].begin(), verset[MOD_REMOVE].end(), RemoveAction);
606 }
607
608 if (Fix != NULL && _config->FindB("APT::Get::AutoSolving", true) == true)
609 {
610 for (unsigned short i = 0; order[i] != 0; ++i)
611 {
612 if (order[i] != MOD_INSTALL)
613 continue;
614 InstallAction.propergateReleaseCandiateSwitching(helper.selectedByRelease, c0out);
615 InstallAction.doAutoInstall();
616 }
617 }
618
619 if (_error->PendingError() == true)
620 {
621 return false;
622 }
623
624 /* If we are in the Broken fixing mode we do not attempt to fix the
625 problems. This is if the user invoked install without -f and gave
626 packages */
627 if (BrokenFix == true && Cache->BrokenCount() != 0)
628 {
629 c1out << _("You might want to run 'apt-get -f install' to correct these:") << std::endl;
630 ShowBroken(c1out,Cache,false);
631 return _error->Error(_("Unmet dependencies. Try 'apt-get -f install' with no packages (or specify a solution)."));
632 }
633
634 if (Fix != NULL)
635 {
636 // Call the scored problem resolver
637 Fix->Resolve(true);
638 }
639
640 // Now we check the state of the packages,
641 if (Cache->BrokenCount() != 0)
642 {
643 c1out <<
644 _("Some packages could not be installed. This may mean that you have\n"
645 "requested an impossible situation or if you are using the unstable\n"
646 "distribution that some required packages have not yet been created\n"
647 "or been moved out of Incoming.") << std::endl;
648 /*
649 if (Packages == 1)
650 {
651 c1out << std::endl;
652 c1out <<
653 _("Since you only requested a single operation it is extremely likely that\n"
654 "the package is simply not installable and a bug report against\n"
655 "that package should be filed.") << std::endl;
656 }
657 */
658
659 c1out << _("The following information may help to resolve the situation:") << std::endl;
660 c1out << std::endl;
661 ShowBroken(c1out,Cache,false);
662 if (_error->PendingError() == true)
663 return false;
664 else
665 return _error->Error(_("Broken packages"));
666 }
667 }
668 if (!DoAutomaticRemove(Cache))
669 return false;
670
671 // if nothing changed in the cache, but only the automark information
672 // we write the StateFile here, otherwise it will be written in
673 // cache.commit()
674 if (InstallAction.AutoMarkChanged > 0 &&
675 Cache->DelCount() == 0 && Cache->InstCount() == 0 &&
676 Cache->BadCount() == 0 &&
677 _config->FindB("APT::Get::Simulate",false) == false)
678 Cache->writeStateFile(NULL);
679
680 return true;
681 }
682 /*}}}*/
683 // DoInstall - Install packages from the command line /*{{{*/
684 // ---------------------------------------------------------------------
685 /* Install named packages */
686 bool DoInstall(CommandLine &CmdL)
687 {
688 CacheFile Cache;
689 if (Cache.OpenForInstall() == false ||
690 Cache.CheckDeps(CmdL.FileSize() != 1) == false)
691 return false;
692
693 std::map<unsigned short, APT::VersionSet> verset;
694
695 if(!DoCacheManipulationFromCommandLine(CmdL, Cache, verset))
696 return false;
697
698 /* Print out a list of packages that are going to be installed extra
699 to what the user asked */
700 if (Cache->InstCount() != verset[MOD_INSTALL].size())
701 {
702 std::string List;
703 std::string VersionsList;
704 for (unsigned J = 0; J < Cache->Head().PackageCount; J++)
705 {
706 pkgCache::PkgIterator I(Cache,Cache.List[J]);
707 if ((*Cache)[I].Install() == false)
708 continue;
709 pkgCache::VerIterator Cand = Cache[I].CandidateVerIter(Cache);
710
711 if (verset[MOD_INSTALL].find(Cand) != verset[MOD_INSTALL].end())
712 continue;
713
714 List += I.FullName(true) + " ";
715 VersionsList += std::string(Cache[I].CandVersion) + "\n";
716 }
717
718 ShowList(c1out,_("The following extra packages will be installed:"),List,VersionsList);
719 }
720
721 /* Print out a list of suggested and recommended packages */
722 {
723 std::string SuggestsList, RecommendsList;
724 std::string SuggestsVersions, RecommendsVersions;
725 for (unsigned J = 0; J < Cache->Head().PackageCount; J++)
726 {
727 pkgCache::PkgIterator Pkg(Cache,Cache.List[J]);
728
729 /* Just look at the ones we want to install */
730 if ((*Cache)[Pkg].Install() == false)
731 continue;
732
733 // get the recommends/suggests for the candidate ver
734 pkgCache::VerIterator CV = (*Cache)[Pkg].CandidateVerIter(*Cache);
735 for (pkgCache::DepIterator D = CV.DependsList(); D.end() == false; )
736 {
737 pkgCache::DepIterator Start;
738 pkgCache::DepIterator End;
739 D.GlobOr(Start,End); // advances D
740
741 // FIXME: we really should display a or-group as a or-group to the user
742 // the problem is that ShowList is incapable of doing this
743 std::string RecommendsOrList,RecommendsOrVersions;
744 std::string SuggestsOrList,SuggestsOrVersions;
745 bool foundInstalledInOrGroup = false;
746 for(;;)
747 {
748 /* Skip if package is installed already, or is about to be */
749 std::string target = Start.TargetPkg().FullName(true) + " ";
750 pkgCache::PkgIterator const TarPkg = Start.TargetPkg();
751 if (TarPkg->SelectedState == pkgCache::State::Install ||
752 TarPkg->SelectedState == pkgCache::State::Hold ||
753 Cache[Start.TargetPkg()].Install())
754 {
755 foundInstalledInOrGroup=true;
756 break;
757 }
758
759 /* Skip if we already saw it */
760 if (int(SuggestsList.find(target)) != -1 || int(RecommendsList.find(target)) != -1)
761 {
762 foundInstalledInOrGroup=true;
763 break;
764 }
765
766 // this is a dep on a virtual pkg, check if any package that provides it
767 // should be installed
768 if(Start.TargetPkg().ProvidesList() != 0)
769 {
770 pkgCache::PrvIterator I = Start.TargetPkg().ProvidesList();
771 for (; I.end() == false; ++I)
772 {
773 pkgCache::PkgIterator Pkg = I.OwnerPkg();
774 if (Cache[Pkg].CandidateVerIter(Cache) == I.OwnerVer() &&
775 Pkg.CurrentVer() != 0)
776 foundInstalledInOrGroup=true;
777 }
778 }
779
780 if (Start->Type == pkgCache::Dep::Suggests)
781 {
782 SuggestsOrList += target;
783 SuggestsOrVersions += std::string(Cache[Start.TargetPkg()].CandVersion) + "\n";
784 }
785
786 if (Start->Type == pkgCache::Dep::Recommends)
787 {
788 RecommendsOrList += target;
789 RecommendsOrVersions += std::string(Cache[Start.TargetPkg()].CandVersion) + "\n";
790 }
791
792 if (Start >= End)
793 break;
794 ++Start;
795 }
796
797 if(foundInstalledInOrGroup == false)
798 {
799 RecommendsList += RecommendsOrList;
800 RecommendsVersions += RecommendsOrVersions;
801 SuggestsList += SuggestsOrList;
802 SuggestsVersions += SuggestsOrVersions;
803 }
804
805 }
806 }
807
808 ShowList(c1out,_("Suggested packages:"),SuggestsList,SuggestsVersions);
809 ShowList(c1out,_("Recommended packages:"),RecommendsList,RecommendsVersions);
810
811 }
812
813 // See if we need to prompt
814 // FIXME: check if really the packages in the set are going to be installed
815 if (Cache->InstCount() == verset[MOD_INSTALL].size() && Cache->DelCount() == 0)
816 return InstallPackages(Cache,false,false);
817
818 return InstallPackages(Cache,false);
819 }
820 /*}}}*/