You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

794 lines
27 KiB

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