apt-mark.cc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. /* #####################################################################
  4. apt-mark - show and change auto-installed bit information
  5. ##################################################################### */
  6. /*}}}*/
  7. // Include Files /*{{{*/
  8. #include <config.h>
  9. #include <apt-pkg/cachefile.h>
  10. #include <apt-pkg/cacheset.h>
  11. #include <apt-pkg/cmndline.h>
  12. #include <apt-pkg/error.h>
  13. #include <apt-pkg/fileutl.h>
  14. #include <apt-pkg/init.h>
  15. #include <apt-pkg/pkgsystem.h>
  16. #include <apt-pkg/strutl.h>
  17. #include <apt-pkg/statechanges.h>
  18. #include <apt-pkg/cacheiterators.h>
  19. #include <apt-pkg/configuration.h>
  20. #include <apt-pkg/depcache.h>
  21. #include <apt-pkg/macros.h>
  22. #include <apt-pkg/pkgcache.h>
  23. #include <apt-private/private-cmndline.h>
  24. #include <apt-private/private-output.h>
  25. #include <apt-private/private-main.h>
  26. #include <errno.h>
  27. #include <fcntl.h>
  28. #include <stddef.h>
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include <sys/wait.h>
  33. #include <unistd.h>
  34. #include <algorithm>
  35. #include <fstream>
  36. #include <iostream>
  37. #include <string>
  38. #include <vector>
  39. #include <apti18n.h>
  40. /*}}}*/
  41. using namespace std;
  42. /* DoAuto - mark packages as automatically/manually installed {{{*/
  43. static bool DoAuto(CommandLine &CmdL)
  44. {
  45. pkgCacheFile CacheFile;
  46. pkgDepCache * const DepCache = CacheFile.GetDepCache();
  47. if (unlikely(DepCache == nullptr))
  48. return false;
  49. APT::PackageList pkgset = APT::PackageList::FromCommandLine(CacheFile, CmdL.FileList + 1);
  50. if (pkgset.empty() == true)
  51. return _error->Error(_("No packages found"));
  52. bool MarkAuto = strcasecmp(CmdL.FileList[0],"auto") == 0;
  53. int AutoMarkChanged = 0;
  54. for (APT::PackageList::const_iterator Pkg = pkgset.begin(); Pkg != pkgset.end(); ++Pkg)
  55. {
  56. if (Pkg->CurrentVer == 0)
  57. {
  58. ioprintf(c1out,_("%s can not be marked as it is not installed.\n"), Pkg.FullName(true).c_str());
  59. continue;
  60. }
  61. else if ((((*DepCache)[Pkg].Flags & pkgCache::Flag::Auto) == pkgCache::Flag::Auto) == MarkAuto)
  62. {
  63. if (MarkAuto == false)
  64. ioprintf(c1out,_("%s was already set to manually installed.\n"), Pkg.FullName(true).c_str());
  65. else
  66. ioprintf(c1out,_("%s was already set to automatically installed.\n"), Pkg.FullName(true).c_str());
  67. continue;
  68. }
  69. if (MarkAuto == false)
  70. ioprintf(c1out,_("%s set to manually installed.\n"), Pkg.FullName(true).c_str());
  71. else
  72. ioprintf(c1out,_("%s set to automatically installed.\n"), Pkg.FullName(true).c_str());
  73. DepCache->MarkAuto(Pkg, MarkAuto);
  74. ++AutoMarkChanged;
  75. }
  76. if (AutoMarkChanged > 0 && _config->FindB("APT::Mark::Simulate", false) == false)
  77. return DepCache->writeStateFile(NULL);
  78. return true;
  79. }
  80. /*}}}*/
  81. /* DoMarkAuto - mark packages as automatically/manually installed {{{*/
  82. /* Does the same as DoAuto but tries to do it exactly the same why as
  83. the python implementation did it so it can be a drop-in replacement */
  84. static bool DoMarkAuto(CommandLine &CmdL)
  85. {
  86. pkgCacheFile CacheFile;
  87. pkgDepCache * const DepCache = CacheFile.GetDepCache();
  88. if (unlikely(DepCache == nullptr))
  89. return false;
  90. APT::PackageList pkgset = APT::PackageList::FromCommandLine(CacheFile, CmdL.FileList + 1);
  91. if (pkgset.empty() == true)
  92. return _error->Error(_("No packages found"));
  93. bool const MarkAuto = strcasecmp(CmdL.FileList[0],"markauto") == 0;
  94. bool const Verbose = _config->FindB("APT::MarkAuto::Verbose", false);
  95. int AutoMarkChanged = 0;
  96. for (APT::PackageList::const_iterator Pkg = pkgset.begin(); Pkg != pkgset.end(); ++Pkg)
  97. {
  98. if (Pkg->CurrentVer == 0 ||
  99. (((*DepCache)[Pkg].Flags & pkgCache::Flag::Auto) == pkgCache::Flag::Auto) == MarkAuto)
  100. continue;
  101. if (Verbose == true)
  102. ioprintf(c1out, "changing %s to %d\n", Pkg.Name(), (MarkAuto == false) ? 0 : 1);
  103. DepCache->MarkAuto(Pkg, MarkAuto);
  104. ++AutoMarkChanged;
  105. }
  106. if (AutoMarkChanged > 0 && _config->FindB("APT::Mark::Simulate", false) == false)
  107. return DepCache->writeStateFile(NULL);
  108. _error->Notice(_("This command is deprecated. Please use 'apt-mark auto' and 'apt-mark manual' instead."));
  109. return true;
  110. }
  111. /*}}}*/
  112. /* ShowAuto - show automatically installed packages (sorted) {{{*/
  113. static bool ShowAuto(CommandLine &CmdL)
  114. {
  115. pkgCacheFile CacheFile;
  116. pkgDepCache * const DepCache = CacheFile.GetDepCache();
  117. if (unlikely(DepCache == nullptr))
  118. return false;
  119. std::vector<string> packages;
  120. bool const ShowAuto = strcasecmp(CmdL.FileList[0],"showauto") == 0;
  121. if (CmdL.FileList[1] == 0)
  122. {
  123. packages.reserve(DepCache->Head().PackageCount / 3);
  124. for (pkgCache::PkgIterator P = DepCache->PkgBegin(); P.end() == false; ++P)
  125. if (P->CurrentVer != 0 &&
  126. (((*DepCache)[P].Flags & pkgCache::Flag::Auto) == pkgCache::Flag::Auto) == ShowAuto)
  127. packages.push_back(P.FullName(true));
  128. }
  129. else
  130. {
  131. APT::CacheSetHelper helper(false); // do not show errors
  132. APT::PackageSet pkgset = APT::PackageSet::FromCommandLine(CacheFile, CmdL.FileList + 1, helper);
  133. packages.reserve(pkgset.size());
  134. for (APT::PackageSet::const_iterator P = pkgset.begin(); P != pkgset.end(); ++P)
  135. if (P->CurrentVer != 0 &&
  136. (((*DepCache)[P].Flags & pkgCache::Flag::Auto) == pkgCache::Flag::Auto) == ShowAuto)
  137. packages.push_back(P.FullName(true));
  138. }
  139. std::sort(packages.begin(), packages.end());
  140. for (vector<string>::const_iterator I = packages.begin(); I != packages.end(); ++I)
  141. std::cout << *I << std::endl;
  142. return true;
  143. }
  144. /*}}}*/
  145. // DoSelection - wrapping around dpkg selections /*{{{*/
  146. static bool DoSelection(CommandLine &CmdL)
  147. {
  148. pkgCacheFile CacheFile;
  149. pkgCache * const Cache = CacheFile.GetPkgCache();
  150. if (unlikely(Cache == nullptr))
  151. return false;
  152. APT::VersionVector pkgset = APT::VersionVector::FromCommandLine(CacheFile, CmdL.FileList + 1, APT::CacheSetHelper::INSTCAND);
  153. if (pkgset.empty() == true)
  154. return _error->Error(_("No packages found"));
  155. APT::StateChanges marks;
  156. if (strcasecmp(CmdL.FileList[0], "hold") == 0 || strcasecmp(CmdL.FileList[0], "unhold") == 0)
  157. {
  158. auto const part = std::stable_partition(pkgset.begin(), pkgset.end(),
  159. [](pkgCache::VerIterator const &V) { return V.ParentPkg()->SelectedState == pkgCache::State::Hold; });
  160. bool const MarkHold = strcasecmp(CmdL.FileList[0],"hold") == 0;
  161. auto const doneBegin = MarkHold ? pkgset.begin() : part;
  162. auto const doneEnd = MarkHold ? part : pkgset.end();
  163. std::for_each(doneBegin, doneEnd, [&MarkHold](pkgCache::VerIterator const &V) {
  164. if (MarkHold == true)
  165. ioprintf(c1out, _("%s was already set on hold.\n"), V.ParentPkg().FullName(true).c_str());
  166. else
  167. ioprintf(c1out, _("%s was already not hold.\n"), V.ParentPkg().FullName(true).c_str());
  168. });
  169. if (doneBegin == pkgset.begin() && doneEnd == pkgset.end())
  170. return true;
  171. auto const changeBegin = MarkHold ? part : pkgset.begin();
  172. auto const changeEnd = MarkHold ? pkgset.end() : part;
  173. std::move(changeBegin, changeEnd, std::back_inserter(MarkHold ? marks.Hold() : marks.Unhold()));
  174. }
  175. else
  176. {
  177. // FIXME: Maybe show a message for unchanged states here as well?
  178. if (strcasecmp(CmdL.FileList[0], "purge") == 0)
  179. std::swap(marks.Purge(), pkgset);
  180. else if (strcasecmp(CmdL.FileList[0], "deinstall") == 0 || strcasecmp(CmdL.FileList[0], "remove") == 0)
  181. std::swap(marks.Remove(), pkgset);
  182. else //if (strcasecmp(CmdL.FileList[0], "install") == 0)
  183. std::swap(marks.Install(), pkgset);
  184. }
  185. pkgset.clear();
  186. bool success = true;
  187. if (_config->FindB("APT::Mark::Simulate", false) == false)
  188. {
  189. success = marks.Save();
  190. if (success == false)
  191. _error->Error(_("Executing dpkg failed. Are you root?"));
  192. }
  193. for (auto Ver : marks.Hold())
  194. ioprintf(c1out,_("%s set on hold.\n"), Ver.ParentPkg().FullName(true).c_str());
  195. for (auto Ver : marks.Unhold())
  196. ioprintf(c1out,_("Canceled hold on %s.\n"), Ver.ParentPkg().FullName(true).c_str());
  197. for (auto Ver : marks.Purge())
  198. ioprintf(c1out,_("Selected %s for purge.\n"), Ver.ParentPkg().FullName(true).c_str());
  199. for (auto Ver : marks.Remove())
  200. ioprintf(c1out,_("Selected %s for removal.\n"), Ver.ParentPkg().FullName(true).c_str());
  201. for (auto Ver : marks.Install())
  202. ioprintf(c1out,_("Selected %s for installation.\n"), Ver.ParentPkg().FullName(true).c_str());
  203. return success;
  204. }
  205. /*}}}*/
  206. static bool ShowSelection(CommandLine &CmdL) /*{{{*/
  207. {
  208. pkgCacheFile CacheFile;
  209. pkgCache * const Cache = CacheFile.GetPkgCache();
  210. if (unlikely(Cache == nullptr))
  211. return false;
  212. pkgCache::State::PkgSelectedState selector;
  213. if (strncasecmp(CmdL.FileList[0], "showpurge", strlen("showpurge")) == 0)
  214. selector = pkgCache::State::Purge;
  215. else if (strncasecmp(CmdL.FileList[0], "showdeinstall", strlen("showdeinstall")) == 0 ||
  216. strncasecmp(CmdL.FileList[0], "showremove", strlen("showremove")) == 0)
  217. selector = pkgCache::State::DeInstall;
  218. else if (strncasecmp(CmdL.FileList[0], "showhold", strlen("showhold")) == 0)
  219. selector = pkgCache::State::Hold;
  220. else //if (strcasecmp(CmdL.FileList[0], "showinstall", strlen("showinstall")) == 0)
  221. selector = pkgCache::State::Install;
  222. std::vector<string> packages;
  223. if (CmdL.FileList[1] == 0)
  224. {
  225. packages.reserve(50); // how many holds are realistic? I hope just a few…
  226. for (pkgCache::PkgIterator P = Cache->PkgBegin(); P.end() == false; ++P)
  227. if (P->SelectedState == selector)
  228. packages.push_back(P.FullName(true));
  229. }
  230. else
  231. {
  232. APT::CacheSetHelper helper(false); // do not show errors
  233. APT::PackageSet pkgset = APT::PackageSet::FromCommandLine(CacheFile, CmdL.FileList + 1, helper);
  234. packages.reserve(pkgset.size());
  235. for (APT::PackageSet::const_iterator P = pkgset.begin(); P != pkgset.end(); ++P)
  236. if (P->SelectedState == selector)
  237. packages.push_back(P.FullName(true));
  238. }
  239. std::sort(packages.begin(), packages.end());
  240. for (vector<string>::const_iterator I = packages.begin(); I != packages.end(); ++I)
  241. std::cout << *I << std::endl;
  242. return true;
  243. }
  244. /*}}}*/
  245. static bool ShowHelp(CommandLine &) /*{{{*/
  246. {
  247. std::cout <<
  248. _("Usage: apt-mark [options] {auto|manual} pkg1 [pkg2 ...]\n"
  249. "\n"
  250. "apt-mark is a simple command line interface for marking packages\n"
  251. "as manually or automatically installed. It can also be used to\n"
  252. "manipulate the dpkg(1) selection states of packages, and to list\n"
  253. "all packages with or without a certain marking.\n");
  254. return true;
  255. }
  256. /*}}}*/
  257. static std::vector<aptDispatchWithHelp> GetCommands() /*{{{*/
  258. {
  259. return {
  260. {"auto",&DoAuto, _("Mark the given packages as automatically installed")},
  261. {"manual",&DoAuto, _("Mark the given packages as manually installed")},
  262. {"hold",&DoSelection, _("Mark a package as held back")},
  263. {"unhold",&DoSelection, _("Unset a package set as held back")},
  264. {"install",&DoSelection, nullptr},
  265. {"remove",&DoSelection, nullptr}, // dpkg uses deinstall, but we use remove everywhere else
  266. {"deinstall",&DoSelection, nullptr},
  267. {"purge",&DoSelection, nullptr},
  268. {"showauto",&ShowAuto, _("Print the list of automatically installed packages")},
  269. {"showmanual",&ShowAuto, _("Print the list of manually installed packages")},
  270. {"showhold",&ShowSelection, _("Print the list of package on hold")}, {"showholds",&ShowSelection, nullptr},
  271. {"showinstall",&ShowSelection, nullptr}, {"showinstalls",&ShowSelection, nullptr},
  272. {"showdeinstall",&ShowSelection, nullptr}, {"showdeinstalls",&ShowSelection, nullptr},
  273. {"showremove",&ShowSelection, nullptr}, {"showremoves",&ShowSelection, nullptr},
  274. {"showpurge",&ShowSelection, nullptr}, {"showpurges",&ShowSelection, nullptr},
  275. // obsolete commands for compatibility
  276. {"markauto", &DoMarkAuto, nullptr},
  277. {"unmarkauto", &DoMarkAuto, nullptr},
  278. {nullptr, nullptr, nullptr}
  279. };
  280. }
  281. /*}}}*/
  282. int main(int argc,const char *argv[]) /*{{{*/
  283. {
  284. InitLocale();
  285. CommandLine CmdL;
  286. auto const Cmds = ParseCommandLine(CmdL, APT_CMD::APT_MARK, &_config, &_system, argc, argv, &ShowHelp, &GetCommands);
  287. InitOutput();
  288. return DispatchCommandLine(CmdL, Cmds);
  289. }
  290. /*}}}*/