apt-mark.cc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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 <apt-pkg/cachefile.h>
  9. #include <apt-pkg/cacheset.h>
  10. #include <apt-pkg/cmndline.h>
  11. #include <apt-pkg/error.h>
  12. #include <apt-pkg/init.h>
  13. #include <apt-pkg/strutl.h>
  14. #include <config.h>
  15. #include <apti18n.h>
  16. #include <algorithm>
  17. /*}}}*/
  18. using namespace std;
  19. ostream c0out(0);
  20. ostream c1out(0);
  21. ostream c2out(0);
  22. ofstream devnull("/dev/null");
  23. /* DoAuto - mark packages as automatically/manually installed {{{*/
  24. bool DoAuto(CommandLine &CmdL)
  25. {
  26. pkgCacheFile CacheFile;
  27. pkgCache *Cache = CacheFile.GetPkgCache();
  28. pkgDepCache *DepCache = CacheFile.GetDepCache();
  29. if (unlikely(Cache == NULL || DepCache == NULL))
  30. return false;
  31. APT::PackageSet pkgset = APT::PackageSet::FromCommandLine(CacheFile, CmdL.FileList + 1);
  32. if (pkgset.empty() == true)
  33. return _error->Error(_("No packages found"));
  34. bool MarkAuto = strcasecmp(CmdL.FileList[0],"auto") == 0;
  35. int AutoMarkChanged = 0;
  36. for (APT::PackageSet::const_iterator Pkg = pkgset.begin(); Pkg != pkgset.end(); ++Pkg)
  37. {
  38. if (Pkg->CurrentVer == 0)
  39. {
  40. ioprintf(c1out,_("%s can not be marked as it is not installed.\n"), Pkg.FullName(true).c_str());
  41. continue;
  42. }
  43. else if ((((*DepCache)[Pkg].Flags & pkgCache::Flag::Auto) == pkgCache::Flag::Auto) == MarkAuto)
  44. {
  45. if (MarkAuto == false)
  46. ioprintf(c1out,_("%s was already set to manually installed.\n"), Pkg.FullName(true).c_str());
  47. else
  48. ioprintf(c1out,_("%s was already set to automatically installed.\n"), Pkg.FullName(true).c_str());
  49. continue;
  50. }
  51. if (MarkAuto == false)
  52. ioprintf(c1out,_("%s set to manually installed.\n"), Pkg.FullName(true).c_str());
  53. else
  54. ioprintf(c1out,_("%s set to automatically installed.\n"), Pkg.FullName(true).c_str());
  55. DepCache->MarkAuto(Pkg, MarkAuto);
  56. ++AutoMarkChanged;
  57. }
  58. if (AutoMarkChanged > 0 && _config->FindB("APT::Mark::Simulate", false) == false)
  59. return DepCache->writeStateFile(NULL);
  60. return true;
  61. }
  62. /*}}}*/
  63. /* DoMarkAuto - mark packages as automatically/manually installed {{{*/
  64. /* Does the same as DoAuto but tries to do it exactly the same why as
  65. the python implementation did it so it can be a drop-in replacement */
  66. bool DoMarkAuto(CommandLine &CmdL)
  67. {
  68. pkgCacheFile CacheFile;
  69. pkgCache *Cache = CacheFile.GetPkgCache();
  70. pkgDepCache *DepCache = CacheFile.GetDepCache();
  71. if (unlikely(Cache == NULL || DepCache == NULL))
  72. return false;
  73. APT::PackageSet pkgset = APT::PackageSet::FromCommandLine(CacheFile, CmdL.FileList + 1);
  74. if (pkgset.empty() == true)
  75. return _error->Error(_("No packages found"));
  76. bool const MarkAuto = strcasecmp(CmdL.FileList[0],"markauto") == 0;
  77. bool const Verbose = _config->FindB("APT::MarkAuto::Verbose", false);
  78. int AutoMarkChanged = 0;
  79. for (APT::PackageSet::const_iterator Pkg = pkgset.begin(); Pkg != pkgset.end(); ++Pkg)
  80. {
  81. if (Pkg->CurrentVer == 0 ||
  82. (((*DepCache)[Pkg].Flags & pkgCache::Flag::Auto) == pkgCache::Flag::Auto) == MarkAuto)
  83. continue;
  84. if (Verbose == true)
  85. ioprintf(c1out, "changing %s to %d\n", Pkg.Name(), (MarkAuto == false) ? 0 : 1);
  86. DepCache->MarkAuto(Pkg, MarkAuto);
  87. ++AutoMarkChanged;
  88. }
  89. if (AutoMarkChanged > 0 && _config->FindB("APT::Mark::Simulate", false) == false)
  90. return DepCache->writeStateFile(NULL);
  91. _error->Notice(_("This command is deprecated. Please use 'apt-mark auto' and 'apt-mark manual' instead."));
  92. return true;
  93. }
  94. /*}}}*/
  95. /* ShowAuto - show automatically installed packages (sorted) {{{*/
  96. bool ShowAuto(CommandLine &CmdL)
  97. {
  98. pkgCacheFile CacheFile;
  99. pkgCache *Cache = CacheFile.GetPkgCache();
  100. pkgDepCache *DepCache = CacheFile.GetDepCache();
  101. if (unlikely(Cache == NULL || DepCache == NULL))
  102. return false;
  103. std::vector<string> packages;
  104. bool const ShowAuto = strcasecmp(CmdL.FileList[0],"showauto") == 0;
  105. if (CmdL.FileList[1] == 0)
  106. {
  107. packages.reserve(Cache->HeaderP->PackageCount / 3);
  108. for (pkgCache::PkgIterator P = Cache->PkgBegin(); P.end() == false; ++P)
  109. if (P->CurrentVer != 0 &&
  110. (((*DepCache)[P].Flags & pkgCache::Flag::Auto) == pkgCache::Flag::Auto) == ShowAuto)
  111. packages.push_back(P.FullName(true));
  112. }
  113. else
  114. {
  115. APT::CacheSetHelper helper(false); // do not show errors
  116. APT::PackageSet pkgset = APT::PackageSet::FromCommandLine(CacheFile, CmdL.FileList + 1, helper);
  117. packages.reserve(pkgset.size());
  118. for (APT::PackageSet::const_iterator P = pkgset.begin(); P != pkgset.end(); ++P)
  119. if (P->CurrentVer != 0 &&
  120. (((*DepCache)[P].Flags & pkgCache::Flag::Auto) == pkgCache::Flag::Auto) == ShowAuto)
  121. packages.push_back(P.FullName(true));
  122. }
  123. std::sort(packages.begin(), packages.end());
  124. for (vector<string>::const_iterator I = packages.begin(); I != packages.end(); ++I)
  125. std::cout << *I << std::endl;
  126. return true;
  127. }
  128. /*}}}*/
  129. /* DoHold - mark packages as hold by dpkg {{{*/
  130. bool DoHold(CommandLine &CmdL)
  131. {
  132. pkgCacheFile CacheFile;
  133. pkgCache *Cache = CacheFile.GetPkgCache();
  134. if (unlikely(Cache == NULL))
  135. return false;
  136. APT::PackageSet pkgset = APT::PackageSet::FromCommandLine(CacheFile, CmdL.FileList + 1);
  137. if (pkgset.empty() == true)
  138. return _error->Error(_("No packages found"));
  139. bool const MarkHold = strcasecmp(CmdL.FileList[0],"hold") == 0;
  140. for (APT::PackageSet::iterator Pkg = pkgset.begin(); Pkg != pkgset.end(); ++Pkg)
  141. {
  142. if ((Pkg->SelectedState == pkgCache::State::Hold) == MarkHold)
  143. {
  144. if (MarkHold == true)
  145. ioprintf(c1out,_("%s was already set on hold.\n"), Pkg.FullName(true).c_str());
  146. else
  147. ioprintf(c1out,_("%s was already not hold.\n"), Pkg.FullName(true).c_str());
  148. pkgset.erase(Pkg);
  149. continue;
  150. }
  151. }
  152. if (pkgset.empty() == true)
  153. return true;
  154. if (_config->FindB("APT::Mark::Simulate", false) == true)
  155. {
  156. for (APT::PackageSet::iterator Pkg = pkgset.begin(); Pkg != pkgset.end(); ++Pkg)
  157. {
  158. if (MarkHold == false)
  159. ioprintf(c1out,_("%s set on hold.\n"), Pkg.FullName(true).c_str());
  160. else
  161. ioprintf(c1out,_("Canceled hold on %s.\n"), Pkg.FullName(true).c_str());
  162. }
  163. return true;
  164. }
  165. string dpkgcall = _config->Find("Dir::Bin::dpkg", "dpkg");
  166. std::vector<string> const dpkgoptions = _config->FindVector("DPkg::options");
  167. for (std::vector<string>::const_iterator o = dpkgoptions.begin();
  168. o != dpkgoptions.end(); ++o)
  169. dpkgcall.append(" ").append(*o);
  170. dpkgcall.append(" --set-selections");
  171. FILE *dpkg = popen(dpkgcall.c_str(), "w");
  172. if (dpkg == NULL)
  173. return _error->Errno("DoHold", "fdopen on dpkg stdin failed");
  174. for (APT::PackageSet::iterator Pkg = pkgset.begin(); Pkg != pkgset.end(); ++Pkg)
  175. {
  176. if (MarkHold == true)
  177. {
  178. fprintf(dpkg, "%s hold\n", Pkg.FullName(true).c_str());
  179. ioprintf(c1out,_("%s set on hold.\n"), Pkg.FullName(true).c_str());
  180. }
  181. else
  182. {
  183. fprintf(dpkg, "%s install\n", Pkg.FullName(true).c_str());
  184. ioprintf(c1out,_("Canceled hold on %s.\n"), Pkg.FullName(true).c_str());
  185. }
  186. }
  187. int const status = pclose(dpkg);
  188. if (status == -1)
  189. return _error->Errno("DoHold", "dpkg execution failed in the end");
  190. if (WIFEXITED(status) == false || WEXITSTATUS(status) != 0)
  191. return _error->Error(_("Executing dpkg failed. Are you root?"));
  192. return true;
  193. }
  194. /*}}}*/
  195. /* ShowHold - show packages set on hold in dpkg status {{{*/
  196. bool ShowHold(CommandLine &CmdL)
  197. {
  198. pkgCacheFile CacheFile;
  199. pkgCache *Cache = CacheFile.GetPkgCache();
  200. if (unlikely(Cache == NULL))
  201. return false;
  202. std::vector<string> packages;
  203. if (CmdL.FileList[1] == 0)
  204. {
  205. packages.reserve(50); // how many holds are realistic? I hope just a few…
  206. for (pkgCache::PkgIterator P = Cache->PkgBegin(); P.end() == false; ++P)
  207. if (P->SelectedState == pkgCache::State::Hold)
  208. packages.push_back(P.FullName(true));
  209. }
  210. else
  211. {
  212. APT::CacheSetHelper helper(false); // do not show errors
  213. APT::PackageSet pkgset = APT::PackageSet::FromCommandLine(CacheFile, CmdL.FileList + 1, helper);
  214. packages.reserve(pkgset.size());
  215. for (APT::PackageSet::const_iterator P = pkgset.begin(); P != pkgset.end(); ++P)
  216. if (P->SelectedState == pkgCache::State::Hold)
  217. packages.push_back(P.FullName(true));
  218. }
  219. std::sort(packages.begin(), packages.end());
  220. for (vector<string>::const_iterator I = packages.begin(); I != packages.end(); ++I)
  221. std::cout << *I << std::endl;
  222. return true;
  223. }
  224. /*}}}*/
  225. // ShowHelp - Show a help screen /*{{{*/
  226. // ---------------------------------------------------------------------
  227. /* */
  228. bool ShowHelp(CommandLine &CmdL)
  229. {
  230. ioprintf(cout,_("%s %s for %s compiled on %s %s\n"),PACKAGE,VERSION,
  231. COMMON_ARCH,__DATE__,__TIME__);
  232. cout <<
  233. _("Usage: apt-mark [options] {auto|manual} pkg1 [pkg2 ...]\n"
  234. "\n"
  235. "apt-mark is a simple command line interface for marking packages\n"
  236. "as manual or automatical installed. It can also list marks.\n"
  237. "\n"
  238. "Commands:\n"
  239. " auto - Mark the given packages as automatically installed\n"
  240. " manual - Mark the given packages as manually installed\n"
  241. "\n"
  242. "Options:\n"
  243. " -h This help text.\n"
  244. " -q Loggable output - no progress indicator\n"
  245. " -qq No output except for errors\n"
  246. " -s No-act. Just prints what would be done.\n"
  247. " -f read/write auto/manual marking in the given file\n"
  248. " -c=? Read this configuration file\n"
  249. " -o=? Set an arbitrary configuration option, eg -o dir::cache=/tmp\n"
  250. "See the apt-mark(8) and apt.conf(5) manual pages for more information.")
  251. << std::endl;
  252. return true;
  253. }
  254. /*}}}*/
  255. int main(int argc,const char *argv[]) /*{{{*/
  256. {
  257. CommandLine::Args Args[] = {
  258. {'h',"help","help",0},
  259. {0,"version","version",0},
  260. {'q',"quiet","quiet",CommandLine::IntLevel},
  261. {'q',"silent","quiet",CommandLine::IntLevel},
  262. {'v',"verbose","APT::MarkAuto::Verbose",0},
  263. {'s',"simulate","APT::Mark::Simulate",0},
  264. {'s',"just-print","APT::Mark::Simulate",0},
  265. {'s',"recon","APT::Mark::Simulate",0},
  266. {'s',"dry-run","APT::Mark::Simulate",0},
  267. {'s',"no-act","APT::Mark::Simulate",0},
  268. {'f',"file","Dir::State::extended_states",CommandLine::HasArg},
  269. {'c',"config-file",0,CommandLine::ConfigFile},
  270. {'o',"option",0,CommandLine::ArbItem},
  271. {0,0,0,0}};
  272. CommandLine::Dispatch Cmds[] = {{"help",&ShowHelp},
  273. {"auto",&DoAuto},
  274. {"manual",&DoAuto},
  275. {"hold",&DoHold},
  276. {"unhold",&DoHold},
  277. {"showauto",&ShowAuto},
  278. {"showmanual",&ShowAuto},
  279. {"showhold",&ShowHold},
  280. // be nice and forgive the typo
  281. {"showholds",&ShowHold},
  282. // be nice and forgive it as it is technical right
  283. {"install",&DoHold},
  284. // obsolete commands for compatibility
  285. {"markauto", &DoMarkAuto},
  286. {"unmarkauto", &DoMarkAuto},
  287. {0,0}};
  288. // Set up gettext support
  289. setlocale(LC_ALL,"");
  290. textdomain(PACKAGE);
  291. // Parse the command line and initialize the package library
  292. CommandLine CmdL(Args,_config);
  293. if (pkgInitConfig(*_config) == false ||
  294. CmdL.Parse(argc,argv) == false ||
  295. pkgInitSystem(*_config,_system) == false)
  296. {
  297. if (_config->FindB("version") == true)
  298. ShowHelp(CmdL);
  299. _error->DumpErrors();
  300. return 100;
  301. }
  302. // See if the help should be shown
  303. if (_config->FindB("help") == true ||
  304. _config->FindB("version") == true ||
  305. CmdL.FileSize() == 0)
  306. {
  307. ShowHelp(CmdL);
  308. return 0;
  309. }
  310. // Deal with stdout not being a tty
  311. if (!isatty(STDOUT_FILENO) && _config->FindI("quiet", -1) == -1)
  312. _config->Set("quiet","1");
  313. // Setup the output streams
  314. c0out.rdbuf(cout.rdbuf());
  315. c1out.rdbuf(cout.rdbuf());
  316. c2out.rdbuf(cout.rdbuf());
  317. if (_config->FindI("quiet",0) > 0)
  318. c0out.rdbuf(devnull.rdbuf());
  319. if (_config->FindI("quiet",0) > 1)
  320. c1out.rdbuf(devnull.rdbuf());
  321. // Match the operation
  322. CmdL.DispatchArg(Cmds);
  323. // Print any errors or warnings found during parsing
  324. bool const Errors = _error->PendingError();
  325. if (_config->FindI("quiet",0) > 0)
  326. _error->DumpErrors();
  327. else
  328. _error->DumpErrors(GlobalError::DEBUG);
  329. return Errors == true ? 100 : 0;
  330. }
  331. /*}}}*/