apt-mark.cc 16 KB

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