apt-mark.cc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  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(); ++Pkg)
  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. pkgset.erase(Pkg);
  202. continue;
  203. }
  204. }
  205. bool dpkgMultiArch = false;
  206. if (dpkgAssertMultiArch > 0)
  207. {
  208. int Status = 0;
  209. while (waitpid(dpkgAssertMultiArch, &Status, 0) != dpkgAssertMultiArch)
  210. {
  211. if (errno == EINTR)
  212. continue;
  213. _error->WarningE("dpkgGo", _("Waited for %s but it wasn't there"), "dpkg --assert-multi-arch");
  214. break;
  215. }
  216. if (WIFEXITED(Status) == true && WEXITSTATUS(Status) == 0)
  217. dpkgMultiArch = true;
  218. }
  219. if (pkgset.empty() == true)
  220. return true;
  221. if (_config->FindB("APT::Mark::Simulate", false) == true)
  222. {
  223. for (APT::PackageList::iterator Pkg = pkgset.begin(); Pkg != pkgset.end(); ++Pkg)
  224. {
  225. if (MarkHold == false)
  226. ioprintf(c1out,_("%s set on hold.\n"), Pkg.FullName(true).c_str());
  227. else
  228. ioprintf(c1out,_("Canceled hold on %s.\n"), Pkg.FullName(true).c_str());
  229. }
  230. return true;
  231. }
  232. Args.erase(Args.begin() + BaseArgs, Args.end());
  233. Args.push_back("--set-selections");
  234. Args.push_back(NULL);
  235. int external[2] = {-1, -1};
  236. if (pipe(external) != 0)
  237. return _error->WarningE("DoHold", "Can't create IPC pipe for dpkg --set-selections");
  238. pid_t dpkgSelection = ExecFork();
  239. if (dpkgSelection == 0)
  240. {
  241. close(external[1]);
  242. std::string const chrootDir = _config->FindDir("DPkg::Chroot-Directory");
  243. if (chrootDir != "/" && chroot(chrootDir.c_str()) != 0)
  244. _error->WarningE("getArchitecture", "Couldn't chroot into %s for dpkg --set-selections", chrootDir.c_str());
  245. int const nullfd = open("/dev/null", O_RDONLY);
  246. dup2(nullfd, STDIN_FILENO);
  247. dup2(external[0], STDOUT_FILENO);
  248. dup2(nullfd, STDERR_FILENO);
  249. execvp(Args[0], (char**) &Args[0]);
  250. _error->WarningE("dpkgGo", "Can't detect if dpkg supports multi-arch!");
  251. _exit(2);
  252. }
  253. FILE* dpkg = fdopen(external[1], "w");
  254. for (APT::PackageList::iterator Pkg = pkgset.begin(); Pkg != pkgset.end(); ++Pkg)
  255. {
  256. if (MarkHold == true)
  257. {
  258. fprintf(dpkg, "%s hold\n", Pkg.FullName(!dpkgMultiArch).c_str());
  259. ioprintf(c1out,_("%s set on hold.\n"), Pkg.FullName(true).c_str());
  260. }
  261. else
  262. {
  263. fprintf(dpkg, "%s install\n", Pkg.FullName(!dpkgMultiArch).c_str());
  264. ioprintf(c1out,_("Canceled hold on %s.\n"), Pkg.FullName(true).c_str());
  265. }
  266. }
  267. fclose(dpkg);
  268. if (dpkgSelection > 0)
  269. {
  270. int Status = 0;
  271. while (waitpid(dpkgSelection, &Status, 0) != dpkgSelection)
  272. {
  273. if (errno == EINTR)
  274. continue;
  275. _error->WarningE("dpkgGo", _("Waited for %s but it wasn't there"), "dpkg --set-selection");
  276. break;
  277. }
  278. if (WIFEXITED(Status) == true && WEXITSTATUS(Status) == 0)
  279. return true;
  280. }
  281. return _error->Error(_("Executing dpkg failed. Are you root?"));
  282. }
  283. /*}}}*/
  284. /* ShowHold - show packages set on hold in dpkg status {{{*/
  285. bool ShowHold(CommandLine &CmdL)
  286. {
  287. pkgCacheFile CacheFile;
  288. pkgCache *Cache = CacheFile.GetPkgCache();
  289. if (unlikely(Cache == NULL))
  290. return false;
  291. std::vector<string> packages;
  292. if (CmdL.FileList[1] == 0)
  293. {
  294. packages.reserve(50); // how many holds are realistic? I hope just a few…
  295. for (pkgCache::PkgIterator P = Cache->PkgBegin(); P.end() == false; ++P)
  296. if (P->SelectedState == pkgCache::State::Hold)
  297. packages.push_back(P.FullName(true));
  298. }
  299. else
  300. {
  301. APT::CacheSetHelper helper(false); // do not show errors
  302. APT::PackageSet pkgset = APT::PackageSet::FromCommandLine(CacheFile, CmdL.FileList + 1, helper);
  303. packages.reserve(pkgset.size());
  304. for (APT::PackageSet::const_iterator P = pkgset.begin(); P != pkgset.end(); ++P)
  305. if (P->SelectedState == pkgCache::State::Hold)
  306. packages.push_back(P.FullName(true));
  307. }
  308. std::sort(packages.begin(), packages.end());
  309. for (vector<string>::const_iterator I = packages.begin(); I != packages.end(); ++I)
  310. std::cout << *I << std::endl;
  311. return true;
  312. }
  313. /*}}}*/
  314. // ShowHelp - Show a help screen /*{{{*/
  315. // ---------------------------------------------------------------------
  316. /* */
  317. bool ShowHelp(CommandLine &CmdL)
  318. {
  319. ioprintf(cout,_("%s %s for %s compiled on %s %s\n"),PACKAGE,VERSION,
  320. COMMON_ARCH,__DATE__,__TIME__);
  321. cout <<
  322. _("Usage: apt-mark [options] {auto|manual} pkg1 [pkg2 ...]\n"
  323. "\n"
  324. "apt-mark is a simple command line interface for marking packages\n"
  325. "as manual or automatical installed. It can also list marks.\n"
  326. "\n"
  327. "Commands:\n"
  328. " auto - Mark the given packages as automatically installed\n"
  329. " manual - Mark the given packages as manually installed\n"
  330. "\n"
  331. "Options:\n"
  332. " -h This help text.\n"
  333. " -q Loggable output - no progress indicator\n"
  334. " -qq No output except for errors\n"
  335. " -s No-act. Just prints what would be done.\n"
  336. " -f read/write auto/manual marking in the given file\n"
  337. " -c=? Read this configuration file\n"
  338. " -o=? Set an arbitrary configuration option, eg -o dir::cache=/tmp\n"
  339. "See the apt-mark(8) and apt.conf(5) manual pages for more information.")
  340. << std::endl;
  341. return true;
  342. }
  343. /*}}}*/
  344. int main(int argc,const char *argv[]) /*{{{*/
  345. {
  346. CommandLine::Args Args[] = {
  347. {'h',"help","help",0},
  348. {0,"version","version",0},
  349. {'q',"quiet","quiet",CommandLine::IntLevel},
  350. {'q',"silent","quiet",CommandLine::IntLevel},
  351. {'v',"verbose","APT::MarkAuto::Verbose",0},
  352. {'s',"simulate","APT::Mark::Simulate",0},
  353. {'s',"just-print","APT::Mark::Simulate",0},
  354. {'s',"recon","APT::Mark::Simulate",0},
  355. {'s',"dry-run","APT::Mark::Simulate",0},
  356. {'s',"no-act","APT::Mark::Simulate",0},
  357. {'f',"file","Dir::State::extended_states",CommandLine::HasArg},
  358. {'c',"config-file",0,CommandLine::ConfigFile},
  359. {'o',"option",0,CommandLine::ArbItem},
  360. {0,0,0,0}};
  361. CommandLine::Dispatch Cmds[] = {{"help",&ShowHelp},
  362. {"auto",&DoAuto},
  363. {"manual",&DoAuto},
  364. {"hold",&DoHold},
  365. {"unhold",&DoHold},
  366. {"showauto",&ShowAuto},
  367. {"showmanual",&ShowAuto},
  368. {"showhold",&ShowHold},
  369. // be nice and forgive the typo
  370. {"showholds",&ShowHold},
  371. // be nice and forgive it as it is technical right
  372. {"install",&DoHold},
  373. // obsolete commands for compatibility
  374. {"markauto", &DoMarkAuto},
  375. {"unmarkauto", &DoMarkAuto},
  376. {0,0}};
  377. // Set up gettext support
  378. setlocale(LC_ALL,"");
  379. textdomain(PACKAGE);
  380. // Parse the command line and initialize the package library
  381. CommandLine CmdL(Args,_config);
  382. if (pkgInitConfig(*_config) == false ||
  383. CmdL.Parse(argc,argv) == false ||
  384. pkgInitSystem(*_config,_system) == false)
  385. {
  386. if (_config->FindB("version") == true)
  387. ShowHelp(CmdL);
  388. _error->DumpErrors();
  389. return 100;
  390. }
  391. // See if the help should be shown
  392. if (_config->FindB("help") == true ||
  393. _config->FindB("version") == true ||
  394. CmdL.FileSize() == 0)
  395. {
  396. ShowHelp(CmdL);
  397. return 0;
  398. }
  399. // Deal with stdout not being a tty
  400. if (!isatty(STDOUT_FILENO) && _config->FindI("quiet", -1) == -1)
  401. _config->Set("quiet","1");
  402. // Setup the output streams
  403. c0out.rdbuf(cout.rdbuf());
  404. c1out.rdbuf(cout.rdbuf());
  405. c2out.rdbuf(cout.rdbuf());
  406. if (_config->FindI("quiet",0) > 0)
  407. c0out.rdbuf(devnull.rdbuf());
  408. if (_config->FindI("quiet",0) > 1)
  409. c1out.rdbuf(devnull.rdbuf());
  410. // Match the operation
  411. CmdL.DispatchArg(Cmds);
  412. // Print any errors or warnings found during parsing
  413. bool const Errors = _error->PendingError();
  414. if (_config->FindI("quiet",0) > 0)
  415. _error->DumpErrors();
  416. else
  417. _error->DumpErrors(GlobalError::DEBUG);
  418. return Errors == true ? 100 : 0;
  419. }
  420. /*}}}*/