apt-mark.cc 17 KB

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