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