apt-mark.cc 12 KB

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