apt-mark.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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. /* DoHold - mark packages as hold by dpkg {{{*/
  148. static bool DoHold(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. bool const MarkHold = strcasecmp(CmdL.FileList[0],"hold") == 0;
  158. auto const part = std::stable_partition(pkgset.begin(), pkgset.end(),
  159. [](pkgCache::VerIterator const &V) { return V.ParentPkg()->SelectedState == pkgCache::State::Hold; });
  160. auto const doneBegin = MarkHold ? pkgset.begin() : part;
  161. auto const doneEnd = MarkHold ? part : pkgset.end();
  162. std::for_each(doneBegin, doneEnd, [&MarkHold](pkgCache::VerIterator const &V) {
  163. if (MarkHold == true)
  164. ioprintf(c1out, _("%s was already set on hold.\n"), V.ParentPkg().FullName(true).c_str());
  165. else
  166. ioprintf(c1out, _("%s was already not hold.\n"), V.ParentPkg().FullName(true).c_str());
  167. });
  168. if (doneBegin == pkgset.begin() && doneEnd == pkgset.end())
  169. return true;
  170. auto const changeBegin = MarkHold ? part : pkgset.begin();
  171. auto const changeEnd = MarkHold ? pkgset.end() : part;
  172. APT::StateChanges marks;
  173. std::move(changeBegin, changeEnd, std::back_inserter(MarkHold ? marks.Hold() : marks.Unhold()));
  174. pkgset.clear();
  175. bool success = true;
  176. if (_config->FindB("APT::Mark::Simulate", false) == false)
  177. {
  178. success = marks.Save();
  179. if (success == false)
  180. _error->Error(_("Executing dpkg failed. Are you root?"));
  181. }
  182. for (auto Ver : marks.Hold())
  183. ioprintf(c1out,_("%s set on hold.\n"), Ver.ParentPkg().FullName(true).c_str());
  184. for (auto Ver : marks.Unhold())
  185. ioprintf(c1out,_("Canceled hold on %s.\n"), Ver.ParentPkg().FullName(true).c_str());
  186. return success;
  187. }
  188. /*}}}*/
  189. /* ShowHold - show packages set on hold in dpkg status {{{*/
  190. static bool ShowHold(CommandLine &CmdL)
  191. {
  192. pkgCacheFile CacheFile;
  193. pkgCache *Cache = CacheFile.GetPkgCache();
  194. if (unlikely(Cache == NULL))
  195. return false;
  196. std::vector<string> packages;
  197. if (CmdL.FileList[1] == 0)
  198. {
  199. packages.reserve(50); // how many holds are realistic? I hope just a few…
  200. for (pkgCache::PkgIterator P = Cache->PkgBegin(); P.end() == false; ++P)
  201. if (P->SelectedState == pkgCache::State::Hold)
  202. packages.push_back(P.FullName(true));
  203. }
  204. else
  205. {
  206. APT::CacheSetHelper helper(false); // do not show errors
  207. APT::PackageSet pkgset = APT::PackageSet::FromCommandLine(CacheFile, CmdL.FileList + 1, helper);
  208. packages.reserve(pkgset.size());
  209. for (APT::PackageSet::const_iterator P = pkgset.begin(); P != pkgset.end(); ++P)
  210. if (P->SelectedState == pkgCache::State::Hold)
  211. packages.push_back(P.FullName(true));
  212. }
  213. std::sort(packages.begin(), packages.end());
  214. for (vector<string>::const_iterator I = packages.begin(); I != packages.end(); ++I)
  215. std::cout << *I << std::endl;
  216. return true;
  217. }
  218. /*}}}*/
  219. // ShowHelp - Show a help screen /*{{{*/
  220. // ---------------------------------------------------------------------
  221. /* */
  222. static bool ShowHelp(CommandLine &)
  223. {
  224. ioprintf(std::cout, "%s %s (%s)\n", PACKAGE, PACKAGE_VERSION, COMMON_ARCH);
  225. cout <<
  226. _("Usage: apt-mark [options] {auto|manual} pkg1 [pkg2 ...]\n"
  227. "\n"
  228. "apt-mark is a simple command line interface for marking packages\n"
  229. "as manually or automatically installed. It can also list marks.\n"
  230. "\n"
  231. "Commands:\n"
  232. " auto - Mark the given packages as automatically installed\n"
  233. " manual - Mark the given packages as manually installed\n"
  234. " hold - Mark a package as held back\n"
  235. " unhold - Unset a package set as held back\n"
  236. " showauto - Print the list of automatically installed packages\n"
  237. " showmanual - Print the list of manually installed packages\n"
  238. " showhold - Print the list of package on hold\n"
  239. "\n"
  240. "Options:\n"
  241. " -h This help text.\n"
  242. " -q Loggable output - no progress indicator\n"
  243. " -qq No output except for errors\n"
  244. " -s No-act. Just prints what would be done.\n"
  245. " -f read/write auto/manual marking in the given file\n"
  246. " -c=? Read this configuration file\n"
  247. " -o=? Set an arbitrary configuration option, eg -o dir::cache=/tmp\n"
  248. "See the apt-mark(8) and apt.conf(5) manual pages for more information.")
  249. << std::endl;
  250. return true;
  251. }
  252. /*}}}*/
  253. int main(int argc,const char *argv[]) /*{{{*/
  254. {
  255. CommandLine::Dispatch Cmds[] = {{"help",&ShowHelp},
  256. {"auto",&DoAuto},
  257. {"manual",&DoAuto},
  258. {"hold",&DoHold},
  259. {"unhold",&DoHold},
  260. {"showauto",&ShowAuto},
  261. {"showmanual",&ShowAuto},
  262. {"showhold",&ShowHold},
  263. // be nice and forgive the typo
  264. {"showholds",&ShowHold},
  265. // be nice and forgive it as it is technical right
  266. {"install",&DoHold},
  267. // obsolete commands for compatibility
  268. {"markauto", &DoMarkAuto},
  269. {"unmarkauto", &DoMarkAuto},
  270. {0,0}};
  271. std::vector<CommandLine::Args> Args = getCommandArgs("apt-mark", CommandLine::GetCommand(Cmds, argc, argv));
  272. // Set up gettext support
  273. setlocale(LC_ALL,"");
  274. textdomain(PACKAGE);
  275. CommandLine CmdL;
  276. ParseCommandLine(CmdL, Cmds, Args.data(), &_config, &_system, argc, argv, ShowHelp);
  277. InitOutput();
  278. // Match the operation
  279. CmdL.DispatchArg(Cmds);
  280. // Print any errors or warnings found during parsing
  281. bool const Errors = _error->PendingError();
  282. if (_config->FindI("quiet",0) > 0)
  283. _error->DumpErrors();
  284. else
  285. _error->DumpErrors(GlobalError::DEBUG);
  286. return Errors == true ? 100 : 0;
  287. }
  288. /*}}}*/