apt-mark.cc 16 KB

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