apt-mark.cc 13 KB

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