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