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