apt-mark.cc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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 <errno.h>
  26. #include <fcntl.h>
  27. #include <stddef.h>
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include <sys/wait.h>
  32. #include <unistd.h>
  33. #include <algorithm>
  34. #include <fstream>
  35. #include <iostream>
  36. #include <string>
  37. #include <vector>
  38. #include <apti18n.h>
  39. /*}}}*/
  40. using namespace std;
  41. /* DoAuto - mark packages as automatically/manually installed {{{*/
  42. static bool DoAuto(CommandLine &CmdL)
  43. {
  44. pkgCacheFile CacheFile;
  45. pkgCache *Cache = CacheFile.GetPkgCache();
  46. pkgDepCache *DepCache = CacheFile.GetDepCache();
  47. if (unlikely(Cache == NULL || DepCache == NULL))
  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. pkgCache *Cache = CacheFile.GetPkgCache();
  88. pkgDepCache *DepCache = CacheFile.GetDepCache();
  89. if (unlikely(Cache == NULL || DepCache == NULL))
  90. return false;
  91. APT::PackageList pkgset = APT::PackageList::FromCommandLine(CacheFile, CmdL.FileList + 1);
  92. if (pkgset.empty() == true)
  93. return _error->Error(_("No packages found"));
  94. bool const MarkAuto = strcasecmp(CmdL.FileList[0],"markauto") == 0;
  95. bool const Verbose = _config->FindB("APT::MarkAuto::Verbose", false);
  96. int AutoMarkChanged = 0;
  97. for (APT::PackageList::const_iterator Pkg = pkgset.begin(); Pkg != pkgset.end(); ++Pkg)
  98. {
  99. if (Pkg->CurrentVer == 0 ||
  100. (((*DepCache)[Pkg].Flags & pkgCache::Flag::Auto) == pkgCache::Flag::Auto) == MarkAuto)
  101. continue;
  102. if (Verbose == true)
  103. ioprintf(c1out, "changing %s to %d\n", Pkg.Name(), (MarkAuto == false) ? 0 : 1);
  104. DepCache->MarkAuto(Pkg, MarkAuto);
  105. ++AutoMarkChanged;
  106. }
  107. if (AutoMarkChanged > 0 && _config->FindB("APT::Mark::Simulate", false) == false)
  108. return DepCache->writeStateFile(NULL);
  109. _error->Notice(_("This command is deprecated. Please use 'apt-mark auto' and 'apt-mark manual' instead."));
  110. return true;
  111. }
  112. /*}}}*/
  113. /* ShowAuto - show automatically installed packages (sorted) {{{*/
  114. static bool ShowAuto(CommandLine &CmdL)
  115. {
  116. pkgCacheFile CacheFile;
  117. pkgCache *Cache = CacheFile.GetPkgCache();
  118. pkgDepCache *DepCache = CacheFile.GetDepCache();
  119. if (unlikely(Cache == NULL || DepCache == NULL))
  120. return false;
  121. std::vector<string> packages;
  122. bool const ShowAuto = strcasecmp(CmdL.FileList[0],"showauto") == 0;
  123. if (CmdL.FileList[1] == 0)
  124. {
  125. packages.reserve(Cache->HeaderP->PackageCount / 3);
  126. for (pkgCache::PkgIterator P = Cache->PkgBegin(); P.end() == false; ++P)
  127. if (P->CurrentVer != 0 &&
  128. (((*DepCache)[P].Flags & pkgCache::Flag::Auto) == pkgCache::Flag::Auto) == ShowAuto)
  129. packages.push_back(P.FullName(true));
  130. }
  131. else
  132. {
  133. APT::CacheSetHelper helper(false); // do not show errors
  134. APT::PackageSet pkgset = APT::PackageSet::FromCommandLine(CacheFile, CmdL.FileList + 1, helper);
  135. packages.reserve(pkgset.size());
  136. for (APT::PackageSet::const_iterator P = pkgset.begin(); P != pkgset.end(); ++P)
  137. if (P->CurrentVer != 0 &&
  138. (((*DepCache)[P].Flags & pkgCache::Flag::Auto) == pkgCache::Flag::Auto) == ShowAuto)
  139. packages.push_back(P.FullName(true));
  140. }
  141. std::sort(packages.begin(), packages.end());
  142. for (vector<string>::const_iterator I = packages.begin(); I != packages.end(); ++I)
  143. std::cout << *I << std::endl;
  144. return true;
  145. }
  146. /*}}}*/
  147. // DoSelection - wrapping around dpkg selections /*{{{*/
  148. static bool DoSelection(CommandLine &CmdL)
  149. {
  150. pkgCacheFile CacheFile;
  151. pkgCache *Cache = CacheFile.GetPkgCache();
  152. if (unlikely(Cache == NULL))
  153. return false;
  154. APT::VersionVector pkgset = APT::VersionVector::FromCommandLine(CacheFile, CmdL.FileList + 1, APT::CacheSetHelper::INSTCAND);
  155. if (pkgset.empty() == true)
  156. return _error->Error(_("No packages found"));
  157. APT::StateChanges marks;
  158. if (strcasecmp(CmdL.FileList[0], "hold") == 0 || strcasecmp(CmdL.FileList[0], "unhold") == 0)
  159. {
  160. auto const part = std::stable_partition(pkgset.begin(), pkgset.end(),
  161. [](pkgCache::VerIterator const &V) { return V.ParentPkg()->SelectedState == pkgCache::State::Hold; });
  162. bool const MarkHold = strcasecmp(CmdL.FileList[0],"hold") == 0;
  163. auto const doneBegin = MarkHold ? pkgset.begin() : part;
  164. auto const doneEnd = MarkHold ? part : pkgset.end();
  165. std::for_each(doneBegin, doneEnd, [&MarkHold](pkgCache::VerIterator const &V) {
  166. if (MarkHold == true)
  167. ioprintf(c1out, _("%s was already set on hold.\n"), V.ParentPkg().FullName(true).c_str());
  168. else
  169. ioprintf(c1out, _("%s was already not hold.\n"), V.ParentPkg().FullName(true).c_str());
  170. });
  171. if (doneBegin == pkgset.begin() && doneEnd == pkgset.end())
  172. return true;
  173. auto const changeBegin = MarkHold ? part : pkgset.begin();
  174. auto const changeEnd = MarkHold ? pkgset.end() : part;
  175. std::move(changeBegin, changeEnd, std::back_inserter(MarkHold ? marks.Hold() : marks.Unhold()));
  176. }
  177. else
  178. {
  179. // FIXME: Maybe show a message for unchanged states here as well?
  180. if (strcasecmp(CmdL.FileList[0], "purge") == 0)
  181. std::swap(marks.Purge(), pkgset);
  182. else if (strcasecmp(CmdL.FileList[0], "deinstall") == 0 || strcasecmp(CmdL.FileList[0], "remove") == 0)
  183. std::swap(marks.Remove(), pkgset);
  184. else //if (strcasecmp(CmdL.FileList[0], "install") == 0)
  185. std::swap(marks.Install(), pkgset);
  186. }
  187. pkgset.clear();
  188. bool success = true;
  189. if (_config->FindB("APT::Mark::Simulate", false) == false)
  190. {
  191. success = marks.Save();
  192. if (success == false)
  193. _error->Error(_("Executing dpkg failed. Are you root?"));
  194. }
  195. for (auto Ver : marks.Hold())
  196. ioprintf(c1out,_("%s set on hold.\n"), Ver.ParentPkg().FullName(true).c_str());
  197. for (auto Ver : marks.Unhold())
  198. ioprintf(c1out,_("Canceled hold on %s.\n"), Ver.ParentPkg().FullName(true).c_str());
  199. for (auto Ver : marks.Purge())
  200. ioprintf(c1out,_("Selected %s for purge.\n"), Ver.ParentPkg().FullName(true).c_str());
  201. for (auto Ver : marks.Remove())
  202. ioprintf(c1out,_("Selected %s for removal.\n"), Ver.ParentPkg().FullName(true).c_str());
  203. for (auto Ver : marks.Install())
  204. ioprintf(c1out,_("Selected %s for installation.\n"), Ver.ParentPkg().FullName(true).c_str());
  205. return success;
  206. }
  207. /*}}}*/
  208. static bool ShowSelection(CommandLine &CmdL) /*{{{*/
  209. {
  210. pkgCacheFile CacheFile;
  211. pkgCache *Cache = CacheFile.GetPkgCache();
  212. if (unlikely(Cache == NULL))
  213. return false;
  214. pkgCache::State::PkgSelectedState selector;
  215. if (strncasecmp(CmdL.FileList[0], "showpurge", strlen("showpurge")) == 0)
  216. selector = pkgCache::State::Purge;
  217. else if (strncasecmp(CmdL.FileList[0], "showdeinstall", strlen("showdeinstall")) == 0 ||
  218. strncasecmp(CmdL.FileList[0], "showremove", strlen("showremove")) == 0)
  219. selector = pkgCache::State::DeInstall;
  220. else if (strncasecmp(CmdL.FileList[0], "showhold", strlen("showhold")) == 0)
  221. selector = pkgCache::State::Hold;
  222. else //if (strcasecmp(CmdL.FileList[0], "showinstall", strlen("showinstall")) == 0)
  223. selector = pkgCache::State::Install;
  224. std::vector<string> packages;
  225. if (CmdL.FileList[1] == 0)
  226. {
  227. packages.reserve(50); // how many holds are realistic? I hope just a few…
  228. for (pkgCache::PkgIterator P = Cache->PkgBegin(); P.end() == false; ++P)
  229. if (P->SelectedState == selector)
  230. packages.push_back(P.FullName(true));
  231. }
  232. else
  233. {
  234. APT::CacheSetHelper helper(false); // do not show errors
  235. APT::PackageSet pkgset = APT::PackageSet::FromCommandLine(CacheFile, CmdL.FileList + 1, helper);
  236. packages.reserve(pkgset.size());
  237. for (APT::PackageSet::const_iterator P = pkgset.begin(); P != pkgset.end(); ++P)
  238. if (P->SelectedState == selector)
  239. packages.push_back(P.FullName(true));
  240. }
  241. std::sort(packages.begin(), packages.end());
  242. for (vector<string>::const_iterator I = packages.begin(); I != packages.end(); ++I)
  243. std::cout << *I << std::endl;
  244. return true;
  245. }
  246. /*}}}*/
  247. // ShowHelp - Show a help screen /*{{{*/
  248. // ---------------------------------------------------------------------
  249. /* */
  250. static bool ShowHelp(CommandLine &)
  251. {
  252. ioprintf(std::cout, "%s %s (%s)\n", PACKAGE, PACKAGE_VERSION, COMMON_ARCH);
  253. cout <<
  254. _("Usage: apt-mark [options] {auto|manual} pkg1 [pkg2 ...]\n"
  255. "\n"
  256. "apt-mark is a simple command line interface for marking packages\n"
  257. "as manually or automatically installed. It can also list marks.\n"
  258. "\n"
  259. "Commands:\n"
  260. " auto - Mark the given packages as automatically installed\n"
  261. " manual - Mark the given packages as manually installed\n"
  262. " hold - Mark a package as held back\n"
  263. " unhold - Unset a package set as held back\n"
  264. " showauto - Print the list of automatically installed packages\n"
  265. " showmanual - Print the list of manually installed packages\n"
  266. " showhold - Print the list of package on hold\n"
  267. "\n"
  268. "Options:\n"
  269. " -h This help text.\n"
  270. " -q Loggable output - no progress indicator\n"
  271. " -qq No output except for errors\n"
  272. " -s No-act. Just prints what would be done.\n"
  273. " -f read/write auto/manual marking in the given file\n"
  274. " -c=? Read this configuration file\n"
  275. " -o=? Set an arbitrary configuration option, eg -o dir::cache=/tmp\n"
  276. "See the apt-mark(8) and apt.conf(5) manual pages for more information.")
  277. << std::endl;
  278. return true;
  279. }
  280. /*}}}*/
  281. int main(int argc,const char *argv[]) /*{{{*/
  282. {
  283. CommandLine::Dispatch Cmds[] = {{"help",&ShowHelp},
  284. {"auto",&DoAuto},
  285. {"manual",&DoAuto},
  286. {"hold",&DoSelection},
  287. {"unhold",&DoSelection},
  288. {"install",&DoSelection},
  289. {"remove",&DoSelection}, // dpkg uses deinstall, but we use remove everywhere else
  290. {"deinstall",&DoSelection},
  291. {"purge",&DoSelection},
  292. {"showauto",&ShowAuto},
  293. {"showmanual",&ShowAuto},
  294. {"showhold",&ShowSelection}, {"showholds",&ShowSelection},
  295. {"showinstall",&ShowSelection}, {"showinstalls",&ShowSelection},
  296. {"showdeinstall",&ShowSelection}, {"showdeinstalls",&ShowSelection},
  297. {"showremove",&ShowSelection}, {"showremoves",&ShowSelection},
  298. {"showpurge",&ShowSelection}, {"showpurges",&ShowSelection},
  299. // obsolete commands for compatibility
  300. {"markauto", &DoMarkAuto},
  301. {"unmarkauto", &DoMarkAuto},
  302. {0,0}};
  303. std::vector<CommandLine::Args> Args = getCommandArgs("apt-mark", CommandLine::GetCommand(Cmds, argc, argv));
  304. // Set up gettext support
  305. setlocale(LC_ALL,"");
  306. textdomain(PACKAGE);
  307. CommandLine CmdL;
  308. ParseCommandLine(CmdL, Cmds, Args.data(), &_config, &_system, argc, argv, ShowHelp);
  309. InitOutput();
  310. // Match the operation
  311. CmdL.DispatchArg(Cmds);
  312. // Print any errors or warnings found during parsing
  313. bool const Errors = _error->PendingError();
  314. if (_config->FindI("quiet",0) > 0)
  315. _error->DumpErrors();
  316. else
  317. _error->DumpErrors(GlobalError::DEBUG);
  318. return Errors == true ? 100 : 0;
  319. }
  320. /*}}}*/