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