apt-get.cc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: apt-get.cc,v 1.156 2004/08/28 01:05:16 mdz Exp $
  4. /* ######################################################################
  5. apt-get - Cover for dpkg
  6. This is an allout cover for dpkg implementing a safer front end. It is
  7. based largely on libapt-pkg.
  8. The syntax is different,
  9. apt-get [opt] command [things]
  10. Where command is:
  11. update - Resyncronize the package files from their sources
  12. upgrade - Smart-Download the newest versions of all packages
  13. dselect-upgrade - Follows dselect's changes to the Status: field
  14. and installes new and removes old packages
  15. dist-upgrade - Powerful upgrader designed to handle the issues with
  16. a new distribution.
  17. install - Download and install a given package (by name, not by .deb)
  18. check - Update the package cache and check for broken packages
  19. clean - Erase the .debs downloaded to /var/cache/apt/archives and
  20. the partial dir too
  21. ##################################################################### */
  22. /*}}}*/
  23. // Include Files /*{{{*/
  24. #include <config.h>
  25. #include <apt-pkg/acquire-item.h>
  26. #include <apt-pkg/algorithms.h>
  27. #include <apt-pkg/aptconfiguration.h>
  28. #include <apt-pkg/cachefile.h>
  29. #include <apt-pkg/cacheset.h>
  30. #include <apt-pkg/clean.h>
  31. #include <apt-pkg/cmndline.h>
  32. #include <apt-pkg/debmetaindex.h>
  33. #include <apt-pkg/depcache.h>
  34. #include <apt-pkg/error.h>
  35. #include <apt-pkg/fileutl.h>
  36. #include <apt-pkg/indexfile.h>
  37. #include <apt-pkg/init.h>
  38. #include <apt-pkg/md5.h>
  39. #include <apt-pkg/metaindex.h>
  40. #include <apt-pkg/pkgrecords.h>
  41. #include <apt-pkg/pkgsystem.h>
  42. #include <apt-pkg/progress.h>
  43. #include <apt-pkg/sourcelist.h>
  44. #include <apt-pkg/srcrecords.h>
  45. #include <apt-pkg/strutl.h>
  46. #include <apt-pkg/version.h>
  47. #include <apt-pkg/acquire.h>
  48. #include <apt-pkg/configuration.h>
  49. #include <apt-pkg/macros.h>
  50. #include <apt-pkg/pkgcache.h>
  51. #include <apt-pkg/cacheiterators.h>
  52. #include <apt-pkg/upgrade.h>
  53. #include <apt-pkg/sptr.h>
  54. #include <apt-private/acqprogress.h>
  55. #include <apt-private/private-cacheset.h>
  56. #include <apt-private/private-cachefile.h>
  57. #include <apt-private/private-cmndline.h>
  58. #include <apt-private/private-download.h>
  59. #include <apt-private/private-install.h>
  60. #include <apt-private/private-main.h>
  61. #include <apt-private/private-moo.h>
  62. #include <apt-private/private-output.h>
  63. #include <apt-private/private-update.h>
  64. #include <apt-private/private-upgrade.h>
  65. #include <apt-private/private-utils.h>
  66. #include <apt-private/private-source.h>
  67. #include <errno.h>
  68. #include <signal.h>
  69. #include <stddef.h>
  70. #include <stdio.h>
  71. #include <stdlib.h>
  72. #include <string.h>
  73. #include <sys/ioctl.h>
  74. #include <sys/stat.h>
  75. #include <unistd.h>
  76. #include <pwd.h>
  77. #include <grp.h>
  78. #include <algorithm>
  79. #include <fstream>
  80. #include <iostream>
  81. #include <sstream>
  82. #include <set>
  83. #include <string>
  84. #include <vector>
  85. #include <apti18n.h>
  86. /*}}}*/
  87. using namespace std;
  88. /* mark packages as automatically/manually installed. {{{*/
  89. static bool DoMarkAuto(CommandLine &CmdL)
  90. {
  91. bool Action = true;
  92. int AutoMarkChanged = 0;
  93. OpTextProgress progress;
  94. CacheFile Cache;
  95. if (Cache.Open() == false)
  96. return false;
  97. if (strcasecmp(CmdL.FileList[0],"markauto") == 0)
  98. Action = true;
  99. else if (strcasecmp(CmdL.FileList[0],"unmarkauto") == 0)
  100. Action = false;
  101. for (const char **I = CmdL.FileList + 1; *I != 0; I++)
  102. {
  103. const char *S = *I;
  104. // Locate the package
  105. pkgCache::PkgIterator Pkg = Cache->FindPkg(S);
  106. if (Pkg.end() == true) {
  107. return _error->Error(_("Couldn't find package %s"),S);
  108. }
  109. else
  110. {
  111. if (!Action)
  112. ioprintf(c1out,_("%s set to manually installed.\n"), Pkg.Name());
  113. else
  114. ioprintf(c1out,_("%s set to automatically installed.\n"),
  115. Pkg.Name());
  116. Cache->MarkAuto(Pkg,Action);
  117. AutoMarkChanged++;
  118. }
  119. }
  120. _error->Notice(_("This command is deprecated. Please use 'apt-mark auto' and 'apt-mark manual' instead."));
  121. if (AutoMarkChanged && ! _config->FindB("APT::Get::Simulate",false))
  122. return Cache->writeStateFile(NULL);
  123. return false;
  124. }
  125. /*}}}*/
  126. // DoDSelectUpgrade - Do an upgrade by following dselects selections /*{{{*/
  127. // ---------------------------------------------------------------------
  128. /* Follows dselect's selections */
  129. static bool DoDSelectUpgrade(CommandLine &)
  130. {
  131. CacheFile Cache;
  132. if (Cache.OpenForInstall() == false || Cache.CheckDeps() == false)
  133. return false;
  134. pkgDepCache::ActionGroup group(Cache);
  135. // Install everything with the install flag set
  136. pkgCache::PkgIterator I = Cache->PkgBegin();
  137. for (;I.end() != true; ++I)
  138. {
  139. /* Install the package only if it is a new install, the autoupgrader
  140. will deal with the rest */
  141. if (I->SelectedState == pkgCache::State::Install)
  142. Cache->MarkInstall(I,false);
  143. }
  144. /* Now install their deps too, if we do this above then order of
  145. the status file is significant for | groups */
  146. for (I = Cache->PkgBegin();I.end() != true; ++I)
  147. {
  148. /* Install the package only if it is a new install, the autoupgrader
  149. will deal with the rest */
  150. if (I->SelectedState == pkgCache::State::Install)
  151. Cache->MarkInstall(I,true);
  152. }
  153. // Apply erasures now, they override everything else.
  154. for (I = Cache->PkgBegin();I.end() != true; ++I)
  155. {
  156. // Remove packages
  157. if (I->SelectedState == pkgCache::State::DeInstall ||
  158. I->SelectedState == pkgCache::State::Purge)
  159. Cache->MarkDelete(I,I->SelectedState == pkgCache::State::Purge);
  160. }
  161. /* Resolve any problems that dselect created, allupgrade cannot handle
  162. such things. We do so quite aggressively too.. */
  163. if (Cache->BrokenCount() != 0)
  164. {
  165. pkgProblemResolver Fix(Cache);
  166. // Hold back held packages.
  167. if (_config->FindB("APT::Ignore-Hold",false) == false)
  168. {
  169. for (pkgCache::PkgIterator I = Cache->PkgBegin(); I.end() == false; ++I)
  170. {
  171. if (I->SelectedState == pkgCache::State::Hold)
  172. {
  173. Fix.Protect(I);
  174. Cache->MarkKeep(I);
  175. }
  176. }
  177. }
  178. if (Fix.Resolve() == false)
  179. {
  180. ShowBroken(c1out,Cache,false);
  181. return _error->Error(_("Internal error, problem resolver broke stuff"));
  182. }
  183. }
  184. // Now upgrade everything
  185. if (APT::Upgrade::Upgrade(Cache, APT::Upgrade::FORBID_REMOVE_PACKAGES | APT::Upgrade::FORBID_INSTALL_NEW_PACKAGES) == false)
  186. {
  187. ShowBroken(c1out,Cache,false);
  188. return _error->Error(_("Internal error, problem resolver broke stuff"));
  189. }
  190. return InstallPackages(Cache,false);
  191. }
  192. /*}}}*/
  193. // DoCheck - Perform the check operation /*{{{*/
  194. // ---------------------------------------------------------------------
  195. /* Opening automatically checks the system, this command is mostly used
  196. for debugging */
  197. static bool DoCheck(CommandLine &)
  198. {
  199. CacheFile Cache;
  200. Cache.Open();
  201. Cache.CheckDeps();
  202. return true;
  203. }
  204. /*}}}*/
  205. // DoIndexTargets - Lists all IndexTargets /*{{{*/
  206. static std::string format_key(std::string key)
  207. {
  208. // deb822 is case-insensitive, but the human eye prefers candy
  209. std::transform(key.begin(), key.end(), key.begin(), ::tolower);
  210. key[0] = ::toupper(key[0]);
  211. size_t found = key.find("_uri");
  212. if (found != std::string::npos)
  213. key.replace(found, 4, "-URI");
  214. while ((found = key.find('_')) != std::string::npos)
  215. {
  216. key[found] = '-';
  217. key[found + 1] = ::toupper(key[found + 1]);
  218. }
  219. return key;
  220. }
  221. static bool DoIndexTargets(CommandLine &CmdL)
  222. {
  223. pkgCacheFile CacheFile;
  224. pkgSourceList *SrcList = CacheFile.GetSourceList();
  225. pkgCache *Cache = CacheFile.GetPkgCache();
  226. if (SrcList == nullptr || Cache == nullptr)
  227. return false;
  228. std::string const Format = _config->Find("APT::Get::IndexTargets::Format");
  229. bool const ReleaseInfo = _config->FindB("APT::Get::IndexTargets::ReleaseInfo", true);
  230. bool Filtered = CmdL.FileSize() > 1;
  231. for (pkgSourceList::const_iterator S = SrcList->begin(); S != SrcList->end(); ++S)
  232. {
  233. std::vector<IndexTarget> const targets = (*S)->GetIndexTargets();
  234. std::map<std::string, string> AddOptions;
  235. if (ReleaseInfo)
  236. {
  237. AddOptions.insert(std::make_pair("TRUSTED", ((*S)->IsTrusted() ? "yes" : "no")));
  238. pkgCache::RlsFileIterator const RlsFile = (*S)->FindInCache(*Cache, false);
  239. if (RlsFile.end())
  240. continue;
  241. #define APT_RELEASE(X,Y) if (RlsFile.Y() != NULL) AddOptions.insert(std::make_pair(X, RlsFile.Y()))
  242. APT_RELEASE("CODENAME", Codename);
  243. APT_RELEASE("SUITE", Archive);
  244. APT_RELEASE("VERSION", Version);
  245. APT_RELEASE("ORIGIN", Origin);
  246. APT_RELEASE("LABEL", Label);
  247. #undef APT_RELEASE
  248. }
  249. for (std::vector<IndexTarget>::const_iterator T = targets.begin(); T != targets.end(); ++T)
  250. {
  251. std::string filename = T->Option(ReleaseInfo ? IndexTarget::EXISTING_FILENAME : IndexTarget::FILENAME);
  252. if (filename.empty())
  253. continue;
  254. std::ostringstream stanza;
  255. if (Filtered || Format.empty())
  256. {
  257. stanza << "MetaKey: " << T->MetaKey << "\n"
  258. << "ShortDesc: " << T->ShortDesc << "\n"
  259. << "Description: " << T->Description << "\n"
  260. << "URI: " << T->URI << "\n"
  261. << "Filename: " << filename << "\n"
  262. << "Optional: " << (T->IsOptional ? "yes" : "no") << "\n"
  263. << "KeepCompressed: " << (T->KeepCompressed ? "yes" : "no") << "\n";
  264. for (std::map<std::string,std::string>::const_iterator O = AddOptions.begin(); O != AddOptions.end(); ++O)
  265. stanza << format_key(O->first) << ": " << O->second << "\n";
  266. for (std::map<std::string,std::string>::const_iterator O = T->Options.begin(); O != T->Options.end(); ++O)
  267. {
  268. if (O->first == "PDIFFS")
  269. stanza << "PDiffs: " << O->second << "\n";
  270. else if (O->first == "COMPRESSIONTYPES")
  271. stanza << "CompressionTypes: " << O->second << "\n";
  272. else if (O->first == "KEEPCOMPRESSEDAS")
  273. stanza << "KeepCompressedAs: " << O->second << "\n";
  274. else if (O->first == "DEFAULTENABLED")
  275. stanza << "DefaultEnabled: " << O->second << "\n";
  276. else
  277. stanza << format_key(O->first) << ": " << O->second << "\n";
  278. }
  279. stanza << "\n";
  280. if (Filtered)
  281. {
  282. // that is a bit crude, but good enough for now
  283. bool found = true;
  284. std::string haystack = std::string("\n") + stanza.str() + "\n";
  285. std::transform(haystack.begin(), haystack.end(), haystack.begin(), ::tolower);
  286. size_t const filesize = CmdL.FileSize() - 1;
  287. for (size_t i = 0; i != filesize; ++i)
  288. {
  289. std::string needle = std::string("\n") + CmdL.FileList[i + 1] + "\n";
  290. std::transform(needle.begin(), needle.end(), needle.begin(), ::tolower);
  291. if (haystack.find(needle) != std::string::npos)
  292. continue;
  293. found = false;
  294. break;
  295. }
  296. if (found == false)
  297. continue;
  298. }
  299. }
  300. if (Format.empty())
  301. cout << stanza.str();
  302. else
  303. {
  304. std::string out = SubstVar(Format, "$(FILENAME)", filename);
  305. out = T->Format(out);
  306. for (std::map<std::string,std::string>::const_iterator O = AddOptions.begin(); O != AddOptions.end(); ++O)
  307. out = SubstVar(out, std::string("$(") + O->first + ")", O->second);
  308. cout << out << std::endl;
  309. }
  310. }
  311. }
  312. return true;
  313. }
  314. /*}}}*/
  315. static bool ShowHelp(CommandLine &) /*{{{*/
  316. {
  317. if (_config->FindB("version") == true)
  318. {
  319. cout << _("Supported modules:") << endl;
  320. for (unsigned I = 0; I != pkgVersioningSystem::GlobalListLen; I++)
  321. {
  322. pkgVersioningSystem *VS = pkgVersioningSystem::GlobalList[I];
  323. if (_system != 0 && _system->VS == VS)
  324. cout << '*';
  325. else
  326. cout << ' ';
  327. cout << "Ver: " << VS->Label << endl;
  328. /* Print out all the packaging systems that will work with
  329. this VS */
  330. for (unsigned J = 0; J != pkgSystem::GlobalListLen; J++)
  331. {
  332. pkgSystem *Sys = pkgSystem::GlobalList[J];
  333. if (_system == Sys)
  334. cout << '*';
  335. else
  336. cout << ' ';
  337. if (Sys->VS->TestCompatibility(*VS) == true)
  338. cout << "Pkg: " << Sys->Label << " (Priority " << Sys->Score(*_config) << ")" << endl;
  339. }
  340. }
  341. for (unsigned I = 0; I != pkgSourceList::Type::GlobalListLen; I++)
  342. {
  343. pkgSourceList::Type *Type = pkgSourceList::Type::GlobalList[I];
  344. cout << " S.L: '" << Type->Name << "' " << Type->Label << endl;
  345. }
  346. for (unsigned I = 0; I != pkgIndexFile::Type::GlobalListLen; I++)
  347. {
  348. pkgIndexFile::Type *Type = pkgIndexFile::Type::GlobalList[I];
  349. cout << " Idx: " << Type->Label << endl;
  350. }
  351. return true;
  352. }
  353. std::cout <<
  354. _("Usage: apt-get [options] command\n"
  355. " apt-get [options] install|remove pkg1 [pkg2 ...]\n"
  356. " apt-get [options] source pkg1 [pkg2 ...]\n"
  357. "\n"
  358. "apt-get is a command line interface for retrieval of packages\n"
  359. "and information about them from authenticated sources and\n"
  360. "for installation, upgrade and removal of packages together\n"
  361. "with their dependencies.\n");
  362. return true;
  363. }
  364. /*}}}*/
  365. static std::vector<aptDispatchWithHelp> GetCommands() /*{{{*/
  366. {
  367. return {
  368. {"update", &DoUpdate, _("Retrieve new lists of packages")},
  369. {"upgrade", &DoUpgrade, _("Perform an upgrade")},
  370. {"install", &DoInstall, _("Install new packages (pkg is libc6 not libc6.deb)")},
  371. {"remove", &DoInstall, _("Remove packages")},
  372. {"purge", &DoInstall, _("Remove packages and config files")},
  373. {"autoremove", &DoInstall, _("Remove automatically all unused packages")},
  374. {"auto-remove", &DoInstall, nullptr},
  375. {"markauto", &DoMarkAuto, nullptr},
  376. {"unmarkauto", &DoMarkAuto, nullptr},
  377. {"dist-upgrade", &DoDistUpgrade, _("Distribution upgrade, see apt-get(8)")},
  378. {"full-upgrade", &DoDistUpgrade, nullptr},
  379. {"dselect-upgrade", &DoDSelectUpgrade, _("Follow dselect selections")},
  380. {"build-dep", &DoBuildDep, _("Configure build-dependencies for source packages")},
  381. {"clean", &DoClean, _("Erase downloaded archive files")},
  382. {"autoclean", &DoAutoClean, _("Erase old downloaded archive files")},
  383. {"auto-clean", &DoAutoClean, nullptr},
  384. {"check", &DoCheck, _("Verify that there are no broken dependencies")},
  385. {"source", &DoSource, _("Download source archives")},
  386. {"download", &DoDownload, _("Download the binary package into the current directory")},
  387. {"changelog", &DoChangelog, _("Download and display the changelog for the given package")},
  388. {"indextargets", &DoIndexTargets, nullptr},
  389. {"moo", &DoMoo, nullptr},
  390. {nullptr, nullptr, nullptr}
  391. };
  392. }
  393. /*}}}*/
  394. int main(int argc,const char *argv[]) /*{{{*/
  395. {
  396. // Parse the command line and initialize the package library
  397. CommandLine CmdL;
  398. auto const Cmds = ParseCommandLine(CmdL, APT_CMD::APT_GET, &_config, &_system, argc, argv, &ShowHelp, &GetCommands);
  399. InitSignals();
  400. InitOutput();
  401. CheckIfSimulateMode(CmdL);
  402. return DispatchCommandLine(CmdL, Cmds);
  403. }
  404. /*}}}*/