apt-mark.cc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  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 <apt-pkg/fileutl.h>
  17. #include <algorithm>
  18. #include <errno.h>
  19. #include <unistd.h>
  20. #include <sys/types.h>
  21. #include <sys/stat.h>
  22. #include <sys/wait.h>
  23. #include <fcntl.h>
  24. #include <apti18n.h>
  25. /*}}}*/
  26. using namespace std;
  27. ostream c0out(0);
  28. ostream c1out(0);
  29. ostream c2out(0);
  30. ofstream devnull("/dev/null");
  31. /* DoAuto - mark packages as automatically/manually installed {{{*/
  32. bool DoAuto(CommandLine &CmdL)
  33. {
  34. pkgCacheFile CacheFile;
  35. pkgCache *Cache = CacheFile.GetPkgCache();
  36. pkgDepCache *DepCache = CacheFile.GetDepCache();
  37. if (unlikely(Cache == NULL || DepCache == NULL))
  38. return false;
  39. APT::PackageList pkgset = APT::PackageList::FromCommandLine(CacheFile, CmdL.FileList + 1);
  40. if (pkgset.empty() == true)
  41. return _error->Error(_("No packages found"));
  42. bool MarkAuto = strcasecmp(CmdL.FileList[0],"auto") == 0;
  43. int AutoMarkChanged = 0;
  44. for (APT::PackageList::const_iterator Pkg = pkgset.begin(); Pkg != pkgset.end(); ++Pkg)
  45. {
  46. if (Pkg->CurrentVer == 0)
  47. {
  48. ioprintf(c1out,_("%s can not be marked as it is not installed.\n"), Pkg.FullName(true).c_str());
  49. continue;
  50. }
  51. else if ((((*DepCache)[Pkg].Flags & pkgCache::Flag::Auto) == pkgCache::Flag::Auto) == MarkAuto)
  52. {
  53. if (MarkAuto == false)
  54. ioprintf(c1out,_("%s was already set to manually installed.\n"), Pkg.FullName(true).c_str());
  55. else
  56. ioprintf(c1out,_("%s was already set to automatically installed.\n"), Pkg.FullName(true).c_str());
  57. continue;
  58. }
  59. if (MarkAuto == false)
  60. ioprintf(c1out,_("%s set to manually installed.\n"), Pkg.FullName(true).c_str());
  61. else
  62. ioprintf(c1out,_("%s set to automatically installed.\n"), Pkg.FullName(true).c_str());
  63. DepCache->MarkAuto(Pkg, MarkAuto);
  64. ++AutoMarkChanged;
  65. }
  66. if (AutoMarkChanged > 0 && _config->FindB("APT::Mark::Simulate", false) == false)
  67. return DepCache->writeStateFile(NULL);
  68. return true;
  69. }
  70. /*}}}*/
  71. /* DoMarkAuto - mark packages as automatically/manually installed {{{*/
  72. /* Does the same as DoAuto but tries to do it exactly the same why as
  73. the python implementation did it so it can be a drop-in replacement */
  74. bool DoMarkAuto(CommandLine &CmdL)
  75. {
  76. pkgCacheFile CacheFile;
  77. pkgCache *Cache = CacheFile.GetPkgCache();
  78. pkgDepCache *DepCache = CacheFile.GetDepCache();
  79. if (unlikely(Cache == NULL || DepCache == NULL))
  80. return false;
  81. APT::PackageList pkgset = APT::PackageList::FromCommandLine(CacheFile, CmdL.FileList + 1);
  82. if (pkgset.empty() == true)
  83. return _error->Error(_("No packages found"));
  84. bool const MarkAuto = strcasecmp(CmdL.FileList[0],"markauto") == 0;
  85. bool const Verbose = _config->FindB("APT::MarkAuto::Verbose", false);
  86. int AutoMarkChanged = 0;
  87. for (APT::PackageList::const_iterator Pkg = pkgset.begin(); Pkg != pkgset.end(); ++Pkg)
  88. {
  89. if (Pkg->CurrentVer == 0 ||
  90. (((*DepCache)[Pkg].Flags & pkgCache::Flag::Auto) == pkgCache::Flag::Auto) == MarkAuto)
  91. continue;
  92. if (Verbose == true)
  93. ioprintf(c1out, "changing %s to %d\n", Pkg.Name(), (MarkAuto == false) ? 0 : 1);
  94. DepCache->MarkAuto(Pkg, MarkAuto);
  95. ++AutoMarkChanged;
  96. }
  97. if (AutoMarkChanged > 0 && _config->FindB("APT::Mark::Simulate", false) == false)
  98. return DepCache->writeStateFile(NULL);
  99. _error->Notice(_("This command is deprecated. Please use 'apt-mark auto' and 'apt-mark manual' instead."));
  100. return true;
  101. }
  102. /*}}}*/
  103. /* ShowAuto - show automatically installed packages (sorted) {{{*/
  104. bool ShowAuto(CommandLine &CmdL)
  105. {
  106. pkgCacheFile CacheFile;
  107. pkgCache *Cache = CacheFile.GetPkgCache();
  108. pkgDepCache *DepCache = CacheFile.GetDepCache();
  109. if (unlikely(Cache == NULL || DepCache == NULL))
  110. return false;
  111. std::vector<string> packages;
  112. bool const ShowAuto = strcasecmp(CmdL.FileList[0],"showauto") == 0;
  113. if (CmdL.FileList[1] == 0)
  114. {
  115. packages.reserve(Cache->HeaderP->PackageCount / 3);
  116. for (pkgCache::PkgIterator P = Cache->PkgBegin(); P.end() == false; ++P)
  117. if (P->CurrentVer != 0 &&
  118. (((*DepCache)[P].Flags & pkgCache::Flag::Auto) == pkgCache::Flag::Auto) == ShowAuto)
  119. packages.push_back(P.FullName(true));
  120. }
  121. else
  122. {
  123. APT::CacheSetHelper helper(false); // do not show errors
  124. APT::PackageSet pkgset = APT::PackageSet::FromCommandLine(CacheFile, CmdL.FileList + 1, helper);
  125. packages.reserve(pkgset.size());
  126. for (APT::PackageSet::const_iterator P = pkgset.begin(); P != pkgset.end(); ++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. std::sort(packages.begin(), packages.end());
  132. for (vector<string>::const_iterator I = packages.begin(); I != packages.end(); ++I)
  133. std::cout << *I << std::endl;
  134. return true;
  135. }
  136. /*}}}*/
  137. /* DoHold - mark packages as hold by dpkg {{{*/
  138. bool DoHold(CommandLine &CmdL)
  139. {
  140. pkgCacheFile CacheFile;
  141. pkgCache *Cache = CacheFile.GetPkgCache();
  142. if (unlikely(Cache == NULL))
  143. return false;
  144. // Generate the base argument list for dpkg
  145. std::vector<const char *> Args;
  146. string Tmp = _config->Find("Dir::Bin::dpkg","dpkg");
  147. {
  148. string const dpkgChrootDir = _config->FindDir("DPkg::Chroot-Directory", "/");
  149. size_t dpkgChrootLen = dpkgChrootDir.length();
  150. if (dpkgChrootDir != "/" && Tmp.find(dpkgChrootDir) == 0)
  151. {
  152. if (dpkgChrootDir[dpkgChrootLen - 1] == '/')
  153. --dpkgChrootLen;
  154. Tmp = Tmp.substr(dpkgChrootLen);
  155. }
  156. }
  157. Args.push_back(Tmp.c_str());
  158. // Stick in any custom dpkg options
  159. Configuration::Item const *Opts = _config->Tree("DPkg::Options");
  160. if (Opts != 0)
  161. {
  162. Opts = Opts->Child;
  163. for (; Opts != 0; Opts = Opts->Next)
  164. {
  165. if (Opts->Value.empty() == true)
  166. continue;
  167. Args.push_back(Opts->Value.c_str());
  168. }
  169. }
  170. size_t const BaseArgs = Args.size();
  171. // we need to detect if we can qualify packages with the architecture or not
  172. Args.push_back("--assert-multi-arch");
  173. Args.push_back(NULL);
  174. pid_t dpkgAssertMultiArch = ExecFork();
  175. if (dpkgAssertMultiArch == 0)
  176. {
  177. std::string const chrootDir = _config->FindDir("DPkg::Chroot-Directory");
  178. if (chrootDir != "/" && chroot(chrootDir.c_str()) != 0)
  179. _error->WarningE("getArchitecture", "Couldn't chroot into %s for dpkg --assert-multi-arch", chrootDir.c_str());
  180. // redirect everything to the ultimate sink as we only need the exit-status
  181. int const nullfd = open("/dev/null", O_RDONLY);
  182. dup2(nullfd, STDIN_FILENO);
  183. dup2(nullfd, STDOUT_FILENO);
  184. dup2(nullfd, STDERR_FILENO);
  185. execvp(Args[0], (char**) &Args[0]);
  186. _error->WarningE("dpkgGo", "Can't detect if dpkg supports multi-arch!");
  187. _exit(2);
  188. }
  189. APT::PackageList pkgset = APT::PackageList::FromCommandLine(CacheFile, CmdL.FileList + 1);
  190. if (pkgset.empty() == true)
  191. return _error->Error(_("No packages found"));
  192. bool const MarkHold = strcasecmp(CmdL.FileList[0],"hold") == 0;
  193. for (APT::PackageList::iterator Pkg = pkgset.begin(); Pkg != pkgset.end();)
  194. {
  195. if ((Pkg->SelectedState == pkgCache::State::Hold) == MarkHold)
  196. {
  197. if (MarkHold == true)
  198. ioprintf(c1out,_("%s was already set on hold.\n"), Pkg.FullName(true).c_str());
  199. else
  200. ioprintf(c1out,_("%s was already not hold.\n"), Pkg.FullName(true).c_str());
  201. Pkg = pkgset.erase(Pkg, true);
  202. }
  203. else
  204. ++Pkg;
  205. }
  206. bool dpkgMultiArch = false;
  207. if (dpkgAssertMultiArch > 0)
  208. {
  209. int Status = 0;
  210. while (waitpid(dpkgAssertMultiArch, &Status, 0) != dpkgAssertMultiArch)
  211. {
  212. if (errno == EINTR)
  213. continue;
  214. _error->WarningE("dpkgGo", _("Waited for %s but it wasn't there"), "dpkg --assert-multi-arch");
  215. break;
  216. }
  217. if (WIFEXITED(Status) == true && WEXITSTATUS(Status) == 0)
  218. dpkgMultiArch = true;
  219. }
  220. if (pkgset.empty() == true)
  221. return true;
  222. if (_config->FindB("APT::Mark::Simulate", false) == true)
  223. {
  224. for (APT::PackageList::iterator Pkg = pkgset.begin(); Pkg != pkgset.end(); ++Pkg)
  225. {
  226. if (MarkHold == false)
  227. ioprintf(c1out,_("%s set on hold.\n"), Pkg.FullName(true).c_str());
  228. else
  229. ioprintf(c1out,_("Canceled hold on %s.\n"), Pkg.FullName(true).c_str());
  230. }
  231. return true;
  232. }
  233. Args.erase(Args.begin() + BaseArgs, Args.end());
  234. Args.push_back("--set-selections");
  235. Args.push_back(NULL);
  236. int external[2] = {-1, -1};
  237. if (pipe(external) != 0)
  238. return _error->WarningE("DoHold", "Can't create IPC pipe for dpkg --set-selections");
  239. pid_t dpkgSelection = ExecFork();
  240. if (dpkgSelection == 0)
  241. {
  242. close(external[1]);
  243. std::string const chrootDir = _config->FindDir("DPkg::Chroot-Directory");
  244. if (chrootDir != "/" && chroot(chrootDir.c_str()) != 0)
  245. _error->WarningE("getArchitecture", "Couldn't chroot into %s for dpkg --set-selections", chrootDir.c_str());
  246. int const nullfd = open("/dev/null", O_RDONLY);
  247. dup2(external[0], STDIN_FILENO);
  248. dup2(nullfd, STDOUT_FILENO);
  249. dup2(nullfd, STDERR_FILENO);
  250. execvp(Args[0], (char**) &Args[0]);
  251. _error->WarningE("dpkgGo", "Can't detect if dpkg supports multi-arch!");
  252. _exit(2);
  253. }
  254. FILE* dpkg = fdopen(external[1], "w");
  255. for (APT::PackageList::iterator Pkg = pkgset.begin(); Pkg != pkgset.end(); ++Pkg)
  256. {
  257. if (MarkHold == true)
  258. {
  259. fprintf(dpkg, "%s hold\n", Pkg.FullName(!dpkgMultiArch).c_str());
  260. ioprintf(c1out,_("%s set on hold.\n"), Pkg.FullName(true).c_str());
  261. }
  262. else
  263. {
  264. fprintf(dpkg, "%s install\n", Pkg.FullName(!dpkgMultiArch).c_str());
  265. ioprintf(c1out,_("Canceled hold on %s.\n"), Pkg.FullName(true).c_str());
  266. }
  267. }
  268. fclose(dpkg);
  269. if (dpkgSelection > 0)
  270. {
  271. int Status = 0;
  272. while (waitpid(dpkgSelection, &Status, 0) != dpkgSelection)
  273. {
  274. if (errno == EINTR)
  275. continue;
  276. _error->WarningE("dpkgGo", _("Waited for %s but it wasn't there"), "dpkg --set-selection");
  277. break;
  278. }
  279. if (WIFEXITED(Status) == true && WEXITSTATUS(Status) == 0)
  280. return true;
  281. }
  282. return _error->Error(_("Executing dpkg failed. Are you root?"));
  283. }
  284. /*}}}*/
  285. /* ShowHold - show packages set on hold in dpkg status {{{*/
  286. bool ShowHold(CommandLine &CmdL)
  287. {
  288. pkgCacheFile CacheFile;
  289. pkgCache *Cache = CacheFile.GetPkgCache();
  290. if (unlikely(Cache == NULL))
  291. return false;
  292. std::vector<string> packages;
  293. if (CmdL.FileList[1] == 0)
  294. {
  295. packages.reserve(50); // how many holds are realistic? I hope just a few…
  296. for (pkgCache::PkgIterator P = Cache->PkgBegin(); P.end() == false; ++P)
  297. if (P->SelectedState == pkgCache::State::Hold)
  298. packages.push_back(P.FullName(true));
  299. }
  300. else
  301. {
  302. APT::CacheSetHelper helper(false); // do not show errors
  303. APT::PackageSet pkgset = APT::PackageSet::FromCommandLine(CacheFile, CmdL.FileList + 1, helper);
  304. packages.reserve(pkgset.size());
  305. for (APT::PackageSet::const_iterator P = pkgset.begin(); P != pkgset.end(); ++P)
  306. if (P->SelectedState == pkgCache::State::Hold)
  307. packages.push_back(P.FullName(true));
  308. }
  309. std::sort(packages.begin(), packages.end());
  310. for (vector<string>::const_iterator I = packages.begin(); I != packages.end(); ++I)
  311. std::cout << *I << std::endl;
  312. return true;
  313. }
  314. /*}}}*/
  315. // ShowHelp - Show a help screen /*{{{*/
  316. // ---------------------------------------------------------------------
  317. /* */
  318. bool ShowHelp(CommandLine &CmdL)
  319. {
  320. ioprintf(cout,_("%s %s for %s compiled on %s %s\n"),PACKAGE,PACKAGE_VERSION,
  321. COMMON_ARCH,__DATE__,__TIME__);
  322. cout <<
  323. _("Usage: apt-mark [options] {auto|manual} pkg1 [pkg2 ...]\n"
  324. "\n"
  325. "apt-mark is a simple command line interface for marking packages\n"
  326. "as manual or automatical installed. It can also list marks.\n"
  327. "\n"
  328. "Commands:\n"
  329. " auto - Mark the given packages as automatically installed\n"
  330. " manual - Mark the given packages as manually installed\n"
  331. "\n"
  332. "Options:\n"
  333. " -h This help text.\n"
  334. " -q Loggable output - no progress indicator\n"
  335. " -qq No output except for errors\n"
  336. " -s No-act. Just prints what would be done.\n"
  337. " -f read/write auto/manual marking in the given file\n"
  338. " -c=? Read this configuration file\n"
  339. " -o=? Set an arbitrary configuration option, eg -o dir::cache=/tmp\n"
  340. "See the apt-mark(8) and apt.conf(5) manual pages for more information.")
  341. << std::endl;
  342. return true;
  343. }
  344. /*}}}*/
  345. int main(int argc,const char *argv[]) /*{{{*/
  346. {
  347. CommandLine::Args Args[] = {
  348. {'h',"help","help",0},
  349. {0,"version","version",0},
  350. {'q',"quiet","quiet",CommandLine::IntLevel},
  351. {'q',"silent","quiet",CommandLine::IntLevel},
  352. {'v',"verbose","APT::MarkAuto::Verbose",0},
  353. {'s',"simulate","APT::Mark::Simulate",0},
  354. {'s',"just-print","APT::Mark::Simulate",0},
  355. {'s',"recon","APT::Mark::Simulate",0},
  356. {'s',"dry-run","APT::Mark::Simulate",0},
  357. {'s',"no-act","APT::Mark::Simulate",0},
  358. {'f',"file","Dir::State::extended_states",CommandLine::HasArg},
  359. {'c',"config-file",0,CommandLine::ConfigFile},
  360. {'o',"option",0,CommandLine::ArbItem},
  361. {0,0,0,0}};
  362. CommandLine::Dispatch Cmds[] = {{"help",&ShowHelp},
  363. {"auto",&DoAuto},
  364. {"manual",&DoAuto},
  365. {"hold",&DoHold},
  366. {"unhold",&DoHold},
  367. {"showauto",&ShowAuto},
  368. {"showmanual",&ShowAuto},
  369. {"showhold",&ShowHold},
  370. // be nice and forgive the typo
  371. {"showholds",&ShowHold},
  372. // be nice and forgive it as it is technical right
  373. {"install",&DoHold},
  374. // obsolete commands for compatibility
  375. {"markauto", &DoMarkAuto},
  376. {"unmarkauto", &DoMarkAuto},
  377. {0,0}};
  378. // Set up gettext support
  379. setlocale(LC_ALL,"");
  380. textdomain(PACKAGE);
  381. // Parse the command line and initialize the package library
  382. CommandLine CmdL(Args,_config);
  383. if (pkgInitConfig(*_config) == false ||
  384. CmdL.Parse(argc,argv) == false ||
  385. pkgInitSystem(*_config,_system) == false)
  386. {
  387. if (_config->FindB("version") == true)
  388. ShowHelp(CmdL);
  389. _error->DumpErrors();
  390. return 100;
  391. }
  392. // See if the help should be shown
  393. if (_config->FindB("help") == true ||
  394. _config->FindB("version") == true ||
  395. CmdL.FileSize() == 0)
  396. {
  397. ShowHelp(CmdL);
  398. return 0;
  399. }
  400. // Deal with stdout not being a tty
  401. if (!isatty(STDOUT_FILENO) && _config->FindI("quiet", -1) == -1)
  402. _config->Set("quiet","1");
  403. // Setup the output streams
  404. c0out.rdbuf(cout.rdbuf());
  405. c1out.rdbuf(cout.rdbuf());
  406. c2out.rdbuf(cout.rdbuf());
  407. if (_config->FindI("quiet",0) > 0)
  408. c0out.rdbuf(devnull.rdbuf());
  409. if (_config->FindI("quiet",0) > 1)
  410. c1out.rdbuf(devnull.rdbuf());
  411. // Match the operation
  412. CmdL.DispatchArg(Cmds);
  413. // Print any errors or warnings found during parsing
  414. bool const Errors = _error->PendingError();
  415. if (_config->FindI("quiet",0) > 0)
  416. _error->DumpErrors();
  417. else
  418. _error->DumpErrors(GlobalError::DEBUG);
  419. return Errors == true ? 100 : 0;
  420. }
  421. /*}}}*/