apt-mark.cc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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. pkgCache *Cache = CacheFile.GetPkgCache();
  47. pkgDepCache *DepCache = CacheFile.GetDepCache();
  48. if (unlikely(Cache == NULL || DepCache == NULL))
  49. return false;
  50. APT::PackageList pkgset = APT::PackageList::FromCommandLine(CacheFile, CmdL.FileList + 1);
  51. if (pkgset.empty() == true)
  52. return _error->Error(_("No packages found"));
  53. bool MarkAuto = strcasecmp(CmdL.FileList[0],"auto") == 0;
  54. int AutoMarkChanged = 0;
  55. for (APT::PackageList::const_iterator Pkg = pkgset.begin(); Pkg != pkgset.end(); ++Pkg)
  56. {
  57. if (Pkg->CurrentVer == 0)
  58. {
  59. ioprintf(c1out,_("%s can not be marked as it is not installed.\n"), Pkg.FullName(true).c_str());
  60. continue;
  61. }
  62. else if ((((*DepCache)[Pkg].Flags & pkgCache::Flag::Auto) == pkgCache::Flag::Auto) == MarkAuto)
  63. {
  64. if (MarkAuto == false)
  65. ioprintf(c1out,_("%s was already set to manually installed.\n"), Pkg.FullName(true).c_str());
  66. else
  67. ioprintf(c1out,_("%s was already set to automatically installed.\n"), Pkg.FullName(true).c_str());
  68. continue;
  69. }
  70. if (MarkAuto == false)
  71. ioprintf(c1out,_("%s set to manually installed.\n"), Pkg.FullName(true).c_str());
  72. else
  73. ioprintf(c1out,_("%s set to automatically installed.\n"), Pkg.FullName(true).c_str());
  74. DepCache->MarkAuto(Pkg, MarkAuto);
  75. ++AutoMarkChanged;
  76. }
  77. if (AutoMarkChanged > 0 && _config->FindB("APT::Mark::Simulate", false) == false)
  78. return DepCache->writeStateFile(NULL);
  79. return true;
  80. }
  81. /*}}}*/
  82. /* DoMarkAuto - mark packages as automatically/manually installed {{{*/
  83. /* Does the same as DoAuto but tries to do it exactly the same why as
  84. the python implementation did it so it can be a drop-in replacement */
  85. static bool DoMarkAuto(CommandLine &CmdL)
  86. {
  87. pkgCacheFile CacheFile;
  88. pkgCache *Cache = CacheFile.GetPkgCache();
  89. pkgDepCache *DepCache = CacheFile.GetDepCache();
  90. if (unlikely(Cache == NULL || DepCache == NULL))
  91. return false;
  92. APT::PackageList pkgset = APT::PackageList::FromCommandLine(CacheFile, CmdL.FileList + 1);
  93. if (pkgset.empty() == true)
  94. return _error->Error(_("No packages found"));
  95. bool const MarkAuto = strcasecmp(CmdL.FileList[0],"markauto") == 0;
  96. bool const Verbose = _config->FindB("APT::MarkAuto::Verbose", false);
  97. int AutoMarkChanged = 0;
  98. for (APT::PackageList::const_iterator Pkg = pkgset.begin(); Pkg != pkgset.end(); ++Pkg)
  99. {
  100. if (Pkg->CurrentVer == 0 ||
  101. (((*DepCache)[Pkg].Flags & pkgCache::Flag::Auto) == pkgCache::Flag::Auto) == MarkAuto)
  102. continue;
  103. if (Verbose == true)
  104. ioprintf(c1out, "changing %s to %d\n", Pkg.Name(), (MarkAuto == false) ? 0 : 1);
  105. DepCache->MarkAuto(Pkg, MarkAuto);
  106. ++AutoMarkChanged;
  107. }
  108. if (AutoMarkChanged > 0 && _config->FindB("APT::Mark::Simulate", false) == false)
  109. return DepCache->writeStateFile(NULL);
  110. _error->Notice(_("This command is deprecated. Please use 'apt-mark auto' and 'apt-mark manual' instead."));
  111. return true;
  112. }
  113. /*}}}*/
  114. /* ShowAuto - show automatically installed packages (sorted) {{{*/
  115. static bool ShowAuto(CommandLine &CmdL)
  116. {
  117. pkgCacheFile CacheFile;
  118. pkgCache *Cache = CacheFile.GetPkgCache();
  119. pkgDepCache *DepCache = CacheFile.GetDepCache();
  120. if (unlikely(Cache == NULL || DepCache == NULL))
  121. return false;
  122. std::vector<string> packages;
  123. bool const ShowAuto = strcasecmp(CmdL.FileList[0],"showauto") == 0;
  124. if (CmdL.FileList[1] == 0)
  125. {
  126. packages.reserve(Cache->HeaderP->PackageCount / 3);
  127. for (pkgCache::PkgIterator P = Cache->PkgBegin(); P.end() == false; ++P)
  128. if (P->CurrentVer != 0 &&
  129. (((*DepCache)[P].Flags & pkgCache::Flag::Auto) == pkgCache::Flag::Auto) == ShowAuto)
  130. packages.push_back(P.FullName(true));
  131. }
  132. else
  133. {
  134. APT::CacheSetHelper helper(false); // do not show errors
  135. APT::PackageSet pkgset = APT::PackageSet::FromCommandLine(CacheFile, CmdL.FileList + 1, helper);
  136. packages.reserve(pkgset.size());
  137. for (APT::PackageSet::const_iterator P = pkgset.begin(); P != pkgset.end(); ++P)
  138. if (P->CurrentVer != 0 &&
  139. (((*DepCache)[P].Flags & pkgCache::Flag::Auto) == pkgCache::Flag::Auto) == ShowAuto)
  140. packages.push_back(P.FullName(true));
  141. }
  142. std::sort(packages.begin(), packages.end());
  143. for (vector<string>::const_iterator I = packages.begin(); I != packages.end(); ++I)
  144. std::cout << *I << std::endl;
  145. return true;
  146. }
  147. /*}}}*/
  148. // DoSelection - wrapping around dpkg selections /*{{{*/
  149. static bool DoSelection(CommandLine &CmdL)
  150. {
  151. pkgCacheFile CacheFile;
  152. pkgCache *Cache = CacheFile.GetPkgCache();
  153. if (unlikely(Cache == NULL))
  154. return false;
  155. APT::VersionVector pkgset = APT::VersionVector::FromCommandLine(CacheFile, CmdL.FileList + 1, APT::CacheSetHelper::INSTCAND);
  156. if (pkgset.empty() == true)
  157. return _error->Error(_("No packages found"));
  158. APT::StateChanges marks;
  159. if (strcasecmp(CmdL.FileList[0], "hold") == 0 || strcasecmp(CmdL.FileList[0], "unhold") == 0)
  160. {
  161. auto const part = std::stable_partition(pkgset.begin(), pkgset.end(),
  162. [](pkgCache::VerIterator const &V) { return V.ParentPkg()->SelectedState == pkgCache::State::Hold; });
  163. bool const MarkHold = strcasecmp(CmdL.FileList[0],"hold") == 0;
  164. auto const doneBegin = MarkHold ? pkgset.begin() : part;
  165. auto const doneEnd = MarkHold ? part : pkgset.end();
  166. std::for_each(doneBegin, doneEnd, [&MarkHold](pkgCache::VerIterator const &V) {
  167. if (MarkHold == true)
  168. ioprintf(c1out, _("%s was already set on hold.\n"), V.ParentPkg().FullName(true).c_str());
  169. else
  170. ioprintf(c1out, _("%s was already not hold.\n"), V.ParentPkg().FullName(true).c_str());
  171. });
  172. if (doneBegin == pkgset.begin() && doneEnd == pkgset.end())
  173. return true;
  174. auto const changeBegin = MarkHold ? part : pkgset.begin();
  175. auto const changeEnd = MarkHold ? pkgset.end() : part;
  176. std::move(changeBegin, changeEnd, std::back_inserter(MarkHold ? marks.Hold() : marks.Unhold()));
  177. }
  178. else
  179. {
  180. // FIXME: Maybe show a message for unchanged states here as well?
  181. if (strcasecmp(CmdL.FileList[0], "purge") == 0)
  182. std::swap(marks.Purge(), pkgset);
  183. else if (strcasecmp(CmdL.FileList[0], "deinstall") == 0 || strcasecmp(CmdL.FileList[0], "remove") == 0)
  184. std::swap(marks.Remove(), pkgset);
  185. else //if (strcasecmp(CmdL.FileList[0], "install") == 0)
  186. std::swap(marks.Install(), pkgset);
  187. }
  188. pkgset.clear();
  189. bool success = true;
  190. if (_config->FindB("APT::Mark::Simulate", false) == false)
  191. {
  192. success = marks.Save();
  193. if (success == false)
  194. _error->Error(_("Executing dpkg failed. Are you root?"));
  195. }
  196. for (auto Ver : marks.Hold())
  197. ioprintf(c1out,_("%s set on hold.\n"), Ver.ParentPkg().FullName(true).c_str());
  198. for (auto Ver : marks.Unhold())
  199. ioprintf(c1out,_("Canceled hold on %s.\n"), Ver.ParentPkg().FullName(true).c_str());
  200. for (auto Ver : marks.Purge())
  201. ioprintf(c1out,_("Selected %s for purge.\n"), Ver.ParentPkg().FullName(true).c_str());
  202. for (auto Ver : marks.Remove())
  203. ioprintf(c1out,_("Selected %s for removal.\n"), Ver.ParentPkg().FullName(true).c_str());
  204. for (auto Ver : marks.Install())
  205. ioprintf(c1out,_("Selected %s for installation.\n"), Ver.ParentPkg().FullName(true).c_str());
  206. return success;
  207. }
  208. /*}}}*/
  209. static bool ShowSelection(CommandLine &CmdL) /*{{{*/
  210. {
  211. pkgCacheFile CacheFile;
  212. pkgCache *Cache = CacheFile.GetPkgCache();
  213. if (unlikely(Cache == NULL))
  214. return false;
  215. pkgCache::State::PkgSelectedState selector;
  216. if (strncasecmp(CmdL.FileList[0], "showpurge", strlen("showpurge")) == 0)
  217. selector = pkgCache::State::Purge;
  218. else if (strncasecmp(CmdL.FileList[0], "showdeinstall", strlen("showdeinstall")) == 0 ||
  219. strncasecmp(CmdL.FileList[0], "showremove", strlen("showremove")) == 0)
  220. selector = pkgCache::State::DeInstall;
  221. else if (strncasecmp(CmdL.FileList[0], "showhold", strlen("showhold")) == 0)
  222. selector = pkgCache::State::Hold;
  223. else //if (strcasecmp(CmdL.FileList[0], "showinstall", strlen("showinstall")) == 0)
  224. selector = pkgCache::State::Install;
  225. std::vector<string> packages;
  226. if (CmdL.FileList[1] == 0)
  227. {
  228. packages.reserve(50); // how many holds are realistic? I hope just a few…
  229. for (pkgCache::PkgIterator P = Cache->PkgBegin(); P.end() == false; ++P)
  230. if (P->SelectedState == selector)
  231. packages.push_back(P.FullName(true));
  232. }
  233. else
  234. {
  235. APT::CacheSetHelper helper(false); // do not show errors
  236. APT::PackageSet pkgset = APT::PackageSet::FromCommandLine(CacheFile, CmdL.FileList + 1, helper);
  237. packages.reserve(pkgset.size());
  238. for (APT::PackageSet::const_iterator P = pkgset.begin(); P != pkgset.end(); ++P)
  239. if (P->SelectedState == selector)
  240. packages.push_back(P.FullName(true));
  241. }
  242. std::sort(packages.begin(), packages.end());
  243. for (vector<string>::const_iterator I = packages.begin(); I != packages.end(); ++I)
  244. std::cout << *I << std::endl;
  245. return true;
  246. }
  247. /*}}}*/
  248. bool ShowHelp(CommandLine &, aptDispatchWithHelp const * Cmds) /*{{{*/
  249. {
  250. ioprintf(std::cout, "%s %s (%s)\n", PACKAGE, PACKAGE_VERSION, COMMON_ARCH);
  251. std::cout <<
  252. _("Usage: apt-mark [options] {auto|manual} pkg1 [pkg2 ...]\n"
  253. "\n"
  254. "apt-mark is a simple command line interface for marking packages\n"
  255. "as manually or automatically installed. It can also list marks.\n")
  256. << std::endl
  257. << _("Commands:") << std::endl;
  258. for (; Cmds->Handler != nullptr; ++Cmds)
  259. {
  260. if (Cmds->Help == nullptr)
  261. continue;
  262. std::cout << " " << Cmds->Match << " - " << Cmds->Help << std::endl;
  263. }
  264. std::cout << std::endl
  265. << _("Options:\n"
  266. " -h This help text.\n"
  267. " -q Loggable output - no progress indicator\n"
  268. " -qq No output except for errors\n"
  269. " -s No-act. Just prints what would be done.\n"
  270. " -f read/write auto/manual marking in the given file\n"
  271. " -c=? Read this configuration file\n"
  272. " -o=? Set an arbitrary configuration option, eg -o dir::cache=/tmp\n"
  273. "See the apt-mark(8) and apt.conf(5) manual pages for more information.")
  274. << std::endl;
  275. return true;
  276. }
  277. /*}}}*/
  278. std::vector<aptDispatchWithHelp> GetCommands() /*{{{*/
  279. {
  280. return {
  281. {"auto",&DoAuto, _("Mark the given packages as automatically installed")},
  282. {"manual",&DoAuto, _("Mark the given packages as manually installed")},
  283. {"hold",&DoSelection, _("Mark a package as held back")},
  284. {"unhold",&DoSelection, _("Unset a package set as held back")},
  285. {"install",&DoSelection, nullptr},
  286. {"remove",&DoSelection, nullptr}, // dpkg uses deinstall, but we use remove everywhere else
  287. {"deinstall",&DoSelection, nullptr},
  288. {"purge",&DoSelection, nullptr},
  289. {"showauto",&ShowAuto, _("Print the list of automatically installed packages")},
  290. {"showmanual",&ShowAuto, _("Print the list of manually installed packages")},
  291. {"showhold",&ShowSelection, _("Print the list of package on hold")}, {"showholds",&ShowSelection, nullptr},
  292. {"showinstall",&ShowSelection, nullptr}, {"showinstalls",&ShowSelection, nullptr},
  293. {"showdeinstall",&ShowSelection, nullptr}, {"showdeinstalls",&ShowSelection, nullptr},
  294. {"showremove",&ShowSelection, nullptr}, {"showremoves",&ShowSelection, nullptr},
  295. {"showpurge",&ShowSelection, nullptr}, {"showpurges",&ShowSelection, nullptr},
  296. // obsolete commands for compatibility
  297. {"markauto", &DoMarkAuto, nullptr},
  298. {"unmarkauto", &DoMarkAuto, nullptr},
  299. {nullptr, nullptr, nullptr}
  300. };
  301. }
  302. /*}}}*/
  303. int main(int argc,const char *argv[]) /*{{{*/
  304. {
  305. InitLocale();
  306. CommandLine CmdL;
  307. auto const Cmds = ParseCommandLine(CmdL, APT_CMD::APT_MARK, &_config, &_system, argc, argv);
  308. InitOutput();
  309. return DispatchCommandLine(CmdL, Cmds);
  310. }
  311. /*}}}*/