apt-mark.cc 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. // ShowHelp - Show a help screen /*{{{*/
  130. // ---------------------------------------------------------------------
  131. /* */
  132. bool ShowHelp(CommandLine &CmdL)
  133. {
  134. ioprintf(cout,_("%s %s for %s compiled on %s %s\n"),PACKAGE,VERSION,
  135. COMMON_ARCH,__DATE__,__TIME__);
  136. cout <<
  137. _("Usage: apt-mark [options] {auto|manual} pkg1 [pkg2 ...]\n"
  138. "\n"
  139. "apt-mark is a simple command line interface for marking packages\n"
  140. "as manual or automatical installed. It can also list marks.\n"
  141. "\n"
  142. "Commands:\n"
  143. " auto - Mark the given packages as automatically installed\n"
  144. " manual - Mark the given packages as manually installed\n"
  145. "\n"
  146. "Options:\n"
  147. " -h This help text.\n"
  148. " -q Loggable output - no progress indicator\n"
  149. " -qq No output except for errors\n"
  150. " -s No-act. Just prints what would be done.\n"
  151. " -f read/write auto/manual marking in the given file\n"
  152. " -c=? Read this configuration file\n"
  153. " -o=? Set an arbitrary configuration option, eg -o dir::cache=/tmp\n"
  154. "See the apt-mark(8) and apt.conf(5) manual pages for more information.")
  155. << std::endl;
  156. return true;
  157. }
  158. /*}}}*/
  159. int main(int argc,const char *argv[]) /*{{{*/
  160. {
  161. CommandLine::Args Args[] = {
  162. {'h',"help","help",0},
  163. {0,"version","version",0},
  164. {'q',"quiet","quiet",CommandLine::IntLevel},
  165. {'q',"silent","quiet",CommandLine::IntLevel},
  166. {'v',"verbose","APT::MarkAuto::Verbose",0},
  167. {'s',"simulate","APT::Mark::Simulate",0},
  168. {'s',"just-print","APT::Mark::Simulate",0},
  169. {'s',"recon","APT::Mark::Simulate",0},
  170. {'s',"dry-run","APT::Mark::Simulate",0},
  171. {'s',"no-act","APT::Mark::Simulate",0},
  172. {'f',"file","Dir::State::extended_states",CommandLine::HasArg},
  173. {'c',"config-file",0,CommandLine::ConfigFile},
  174. {'o',"option",0,CommandLine::ArbItem},
  175. {0,0,0,0}};
  176. CommandLine::Dispatch Cmds[] = {{"help",&ShowHelp},
  177. {"auto",&DoAuto},
  178. {"manual",&DoAuto},
  179. {"showauto",&ShowAuto},
  180. {"showmanual",&ShowAuto},
  181. // obsolete commands for compatibility
  182. {"markauto", &DoMarkAuto},
  183. {"unmarkauto", &DoMarkAuto},
  184. {0,0}};
  185. // Set up gettext support
  186. setlocale(LC_ALL,"");
  187. textdomain(PACKAGE);
  188. // Parse the command line and initialize the package library
  189. CommandLine CmdL(Args,_config);
  190. if (pkgInitConfig(*_config) == false ||
  191. CmdL.Parse(argc,argv) == false ||
  192. pkgInitSystem(*_config,_system) == false)
  193. {
  194. if (_config->FindB("version") == true)
  195. ShowHelp(CmdL);
  196. _error->DumpErrors();
  197. return 100;
  198. }
  199. // See if the help should be shown
  200. if (_config->FindB("help") == true ||
  201. _config->FindB("version") == true ||
  202. CmdL.FileSize() == 0)
  203. {
  204. ShowHelp(CmdL);
  205. return 0;
  206. }
  207. // Deal with stdout not being a tty
  208. if (!isatty(STDOUT_FILENO) && _config->FindI("quiet", -1) == -1)
  209. _config->Set("quiet","1");
  210. // Setup the output streams
  211. c0out.rdbuf(cout.rdbuf());
  212. c1out.rdbuf(cout.rdbuf());
  213. c2out.rdbuf(cout.rdbuf());
  214. if (_config->FindI("quiet",0) > 0)
  215. c0out.rdbuf(devnull.rdbuf());
  216. if (_config->FindI("quiet",0) > 1)
  217. c1out.rdbuf(devnull.rdbuf());
  218. // Match the operation
  219. CmdL.DispatchArg(Cmds);
  220. // Print any errors or warnings found during parsing
  221. bool const Errors = _error->PendingError();
  222. if (_config->FindI("quiet",0) > 0)
  223. _error->DumpErrors();
  224. else
  225. _error->DumpErrors(GlobalError::DEBUG);
  226. return Errors == true ? 100 : 0;
  227. }
  228. /*}}}*/