]> git.saurik.com Git - apt.git/blobdiff - cmdline/apt-mark.cc
use pkgAcquire::GetLock instead of own code
[apt.git] / cmdline / apt-mark.cc
index 2a093c55a3b1384fd0da169189355965719cdc67..860982f9e5d7b83d3e8c6ae007d1db57e8f2c2b9 100644 (file)
 #include <apt-pkg/cacheset.h>
 #include <apt-pkg/cmndline.h>
 #include <apt-pkg/error.h>
+#include <apt-pkg/fileutl.h>
 #include <apt-pkg/init.h>
-#include <apt-pkg/strutl.h>
 #include <apt-pkg/pkgsystem.h>
-#include <apt-pkg/fileutl.h>
+#include <apt-pkg/strutl.h>
+#include <apt-pkg/cacheiterators.h>
+#include <apt-pkg/configuration.h>
+#include <apt-pkg/depcache.h>
+#include <apt-pkg/macros.h>
+#include <apt-pkg/pkgcache.h>
+
+#include <apt-private/private-cmndline.h>
+#include <apt-private/private-output.h>
 
-#include <algorithm>
 #include <errno.h>
-#include <unistd.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <sys/wait.h>
 #include <fcntl.h>
+#include <stddef.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/wait.h>
+#include <unistd.h>
+#include <algorithm>
+#include <fstream>
+#include <iostream>
+#include <string>
+#include <vector>
 
 #include <apti18n.h>
                                                                        /*}}}*/
 using namespace std;
 
-ostream c0out(0);
-ostream c1out(0);
-ostream c2out(0);
-ofstream devnull("/dev/null");
 /* DoAuto - mark packages as automatically/manually installed          {{{*/
-bool DoAuto(CommandLine &CmdL)
+static bool DoAuto(CommandLine &CmdL)
 {
    pkgCacheFile CacheFile;
    pkgCache *Cache = CacheFile.GetPkgCache();
@@ -80,7 +90,7 @@ bool DoAuto(CommandLine &CmdL)
 /* DoMarkAuto - mark packages as automatically/manually installed      {{{*/
 /* Does the same as DoAuto but tries to do it exactly the same why as
    the python implementation did it so it can be a drop-in replacement */
-bool DoMarkAuto(CommandLine &CmdL)
+static bool DoMarkAuto(CommandLine &CmdL)
 {
    pkgCacheFile CacheFile;
    pkgCache *Cache = CacheFile.GetPkgCache();
@@ -117,7 +127,7 @@ bool DoMarkAuto(CommandLine &CmdL)
 }
                                                                        /*}}}*/
 /* ShowAuto - show automatically installed packages (sorted)           {{{*/
-bool ShowAuto(CommandLine &CmdL)
+static bool ShowAuto(CommandLine &CmdL)
 {
    pkgCacheFile CacheFile;
    pkgCache *Cache = CacheFile.GetPkgCache();
@@ -157,7 +167,7 @@ bool ShowAuto(CommandLine &CmdL)
 }
                                                                        /*}}}*/
 /* DoHold - mark packages as hold by dpkg                              {{{*/
-bool DoHold(CommandLine &CmdL)
+static bool DoHold(CommandLine &CmdL)
 {
    pkgCacheFile CacheFile;
    pkgCache *Cache = CacheFile.GetPkgCache();
@@ -202,13 +212,13 @@ bool DoHold(CommandLine &CmdL)
    if (dpkgAssertMultiArch == 0)
    {
       std::string const chrootDir = _config->FindDir("DPkg::Chroot-Directory");
-      if (chrootDir != "/" && chroot(chrootDir.c_str()) != 0)
-        _error->WarningE("getArchitecture", "Couldn't chroot into %s for dpkg --assert-multi-arch", chrootDir.c_str());
       // redirect everything to the ultimate sink as we only need the exit-status
       int const nullfd = open("/dev/null", O_RDONLY);
       dup2(nullfd, STDIN_FILENO);
       dup2(nullfd, STDOUT_FILENO);
       dup2(nullfd, STDERR_FILENO);
+      if (chrootDir != "/" && chroot(chrootDir.c_str()) != 0 && chdir("/") != 0)
+        _error->WarningE("getArchitecture", "Couldn't chroot into %s for dpkg --assert-multi-arch", chrootDir.c_str());
       execvp(Args[0], (char**) &Args[0]);
       _error->WarningE("dpkgGo", "Can't detect if dpkg supports multi-arch!");
       _exit(2);
@@ -265,10 +275,64 @@ bool DoHold(CommandLine &CmdL)
    }
 
    Args.erase(Args.begin() + BaseArgs, Args.end());
-   Args.push_back("--set-selections");
+   Args.push_back("--merge-avail");
+   Args.push_back("-");
    Args.push_back(NULL);
 
    int external[2] = {-1, -1};
+   if (pipe(external) != 0)
+      return _error->WarningE("DoHold", "Can't create IPC pipe for dpkg --merge-avail");
+
+   pid_t dpkgMergeAvail = ExecFork();
+   if (dpkgMergeAvail == 0)
+   {
+      close(external[1]);
+      std::string const chrootDir = _config->FindDir("DPkg::Chroot-Directory");
+      if (chrootDir != "/" && chroot(chrootDir.c_str()) != 0 && chdir("/") != 0)
+        _error->WarningE("getArchitecture", "Couldn't chroot into %s for dpkg --merge-avail", chrootDir.c_str());
+      dup2(external[0], STDIN_FILENO);
+      int const nullfd = open("/dev/null", O_RDONLY);
+      dup2(nullfd, STDOUT_FILENO);
+      execvp(Args[0], (char**) &Args[0]);
+      _error->WarningE("dpkgGo", "Can't get dpkg --merge-avail running!");
+      _exit(2);
+   }
+
+   FILE* dpkg = fdopen(external[1], "w");
+   for (APT::PackageList::iterator Pkg = pkgset.begin(); Pkg != pkgset.end(); ++Pkg)
+   {
+      if (Pkg->CurrentVer != 0)
+        continue;
+      char const * Arch;
+      if (Pkg->VersionList != 0)
+        Arch = Pkg.VersionList().Arch();
+      else
+        Arch = Pkg.Arch();
+      fprintf(dpkg, "Package: %s\nVersion: 0~\nArchitecture: %s\nMaintainer: Dummy Example <dummy@example.org>\n"
+           "Description: dummy package record\n A record is needed to put a package on hold, so here it is.\n\n", Pkg.Name(), Arch);
+   }
+   fclose(dpkg);
+
+   if (dpkgMergeAvail > 0)
+   {
+      int Status = 0;
+      while (waitpid(dpkgMergeAvail, &Status, 0) != dpkgMergeAvail)
+      {
+        if (errno == EINTR)
+           continue;
+        _error->WarningE("dpkgGo", _("Waited for %s but it wasn't there"), "dpkg --merge-avail");
+        break;
+      }
+      if (WIFEXITED(Status) == false || WEXITSTATUS(Status) != 0)
+        return _error->Error(_("Executing dpkg failed. Are you root?"));
+   }
+
+   Args.erase(Args.begin() + BaseArgs, Args.end());
+   Args.push_back("--set-selections");
+   Args.push_back(NULL);
+
+   external[0] = -1;
+   external[1] = -1;
    if (pipe(external) != 0)
       return _error->WarningE("DoHold", "Can't create IPC pipe for dpkg --set-selections");
 
@@ -277,28 +341,37 @@ bool DoHold(CommandLine &CmdL)
    {
       close(external[1]);
       std::string const chrootDir = _config->FindDir("DPkg::Chroot-Directory");
-      if (chrootDir != "/" && chroot(chrootDir.c_str()) != 0)
+      if (chrootDir != "/" && chroot(chrootDir.c_str()) != 0 && chdir("/") != 0)
         _error->WarningE("getArchitecture", "Couldn't chroot into %s for dpkg --set-selections", chrootDir.c_str());
-      int const nullfd = open("/dev/null", O_RDONLY);
       dup2(external[0], STDIN_FILENO);
-      dup2(nullfd, STDOUT_FILENO);
-      dup2(nullfd, STDERR_FILENO);
       execvp(Args[0], (char**) &Args[0]);
-      _error->WarningE("dpkgGo", "Can't detect if dpkg supports multi-arch!");
+      _error->WarningE("dpkgGo", "Can't get dpkg --set-selections running!");
       _exit(2);
    }
 
-   FILE* dpkg = fdopen(external[1], "w");
+   dpkg = fdopen(external[1], "w");
    for (APT::PackageList::iterator Pkg = pkgset.begin(); Pkg != pkgset.end(); ++Pkg)
    {
+      if (dpkgMultiArch == false)
+        fprintf(dpkg, "%s", Pkg.FullName(true).c_str());
+      else
+      {
+        if (Pkg->CurrentVer != 0)
+           fprintf(dpkg, "%s:%s", Pkg.Name(), Pkg.CurrentVer().Arch());
+        else if (Pkg.VersionList().end() == false)
+           fprintf(dpkg, "%s:%s", Pkg.Name(), Pkg.VersionList().Arch());
+        else
+           fprintf(dpkg, "%s", Pkg.FullName(false).c_str());
+      }
+
       if (MarkHold == true)
       {
-        fprintf(dpkg, "%s hold\n", Pkg.FullName(!dpkgMultiArch).c_str());
+        fprintf(dpkg, " hold\n");
         ioprintf(c1out,_("%s set on hold.\n"), Pkg.FullName(true).c_str());
       }
       else
       {
-        fprintf(dpkg, "%s install\n", Pkg.FullName(!dpkgMultiArch).c_str());
+        fprintf(dpkg, " install\n");
         ioprintf(c1out,_("Canceled hold on %s.\n"), Pkg.FullName(true).c_str());
       }
    }
@@ -321,7 +394,7 @@ bool DoHold(CommandLine &CmdL)
 }
                                                                        /*}}}*/
 /* ShowHold - show packages set on hold in dpkg status                 {{{*/
-bool ShowHold(CommandLine &CmdL)
+static bool ShowHold(CommandLine &CmdL)
 {
    pkgCacheFile CacheFile;
    pkgCache *Cache = CacheFile.GetPkgCache();
@@ -358,7 +431,7 @@ bool ShowHold(CommandLine &CmdL)
 // ShowHelp - Show a help screen                                       /*{{{*/
 // ---------------------------------------------------------------------
 /* */
-bool ShowHelp(CommandLine &CmdL)
+static bool ShowHelp(CommandLine &)
 {
    ioprintf(cout,_("%s %s for %s compiled on %s %s\n"),PACKAGE,PACKAGE_VERSION,
            COMMON_ARCH,__DATE__,__TIME__);
@@ -372,6 +445,11 @@ bool ShowHelp(CommandLine &CmdL)
       "Commands:\n"
       "   auto - Mark the given packages as automatically installed\n"
       "   manual - Mark the given packages as manually installed\n"
+      "   hold - Mark a package as held back\n"
+      "   unhold - Unset a package set as held back\n"
+      "   showauto - Print the list of automatically installed packages\n"
+      "   showmanual - Print the list of manually installed packages\n"
+      "   showhold - Print the list of package on hold\n"
       "\n"
       "Options:\n"
       "  -h  This help text.\n"
@@ -388,21 +466,6 @@ bool ShowHelp(CommandLine &CmdL)
                                                                        /*}}}*/
 int main(int argc,const char *argv[])                                  /*{{{*/
 {
-   CommandLine::Args Args[] = {
-      {'h',"help","help",0},
-      {0,"version","version",0},
-      {'q',"quiet","quiet",CommandLine::IntLevel},
-      {'q',"silent","quiet",CommandLine::IntLevel},
-      {'v',"verbose","APT::MarkAuto::Verbose",0},
-      {'s',"simulate","APT::Mark::Simulate",0},
-      {'s',"just-print","APT::Mark::Simulate",0},
-      {'s',"recon","APT::Mark::Simulate",0},
-      {'s',"dry-run","APT::Mark::Simulate",0},
-      {'s',"no-act","APT::Mark::Simulate",0},
-      {'f',"file","Dir::State::extended_states",CommandLine::HasArg},
-      {'c',"config-file",0,CommandLine::ConfigFile},
-      {'o',"option",0,CommandLine::ArbItem},
-      {0,0,0,0}};
    CommandLine::Dispatch Cmds[] = {{"help",&ShowHelp},
                                   {"auto",&DoAuto},
                                   {"manual",&DoAuto},
@@ -420,43 +483,16 @@ int main(int argc,const char *argv[])                                     /*{{{*/
                                   {"unmarkauto", &DoMarkAuto},
                                    {0,0}};
 
+   std::vector<CommandLine::Args> Args = getCommandArgs("apt-mark", CommandLine::GetCommand(Cmds, argc, argv));
+
    // Set up gettext support
    setlocale(LC_ALL,"");
    textdomain(PACKAGE);
 
-   // Parse the command line and initialize the package library
-   CommandLine CmdL(Args,_config);
-   if (pkgInitConfig(*_config) == false ||
-       CmdL.Parse(argc,argv) == false ||
-       pkgInitSystem(*_config,_system) == false)
-   {
-      if (_config->FindB("version") == true)
-        ShowHelp(CmdL);
-      _error->DumpErrors();
-      return 100;
-   }
-
-   // See if the help should be shown
-   if (_config->FindB("help") == true ||
-       _config->FindB("version") == true ||
-       CmdL.FileSize() == 0)
-   {
-      ShowHelp(CmdL);
-      return 0;
-   }
+   CommandLine CmdL;
+   ParseCommandLine(CmdL, Cmds, Args.data(), &_config, &_system, argc, argv, ShowHelp);
 
-   // Deal with stdout not being a tty
-   if (!isatty(STDOUT_FILENO) && _config->FindI("quiet", -1) == -1)
-      _config->Set("quiet","1");
-
-   // Setup the output streams
-   c0out.rdbuf(cout.rdbuf());
-   c1out.rdbuf(cout.rdbuf());
-   c2out.rdbuf(cout.rdbuf());
-   if (_config->FindI("quiet",0) > 0)
-      c0out.rdbuf(devnull.rdbuf());
-   if (_config->FindI("quiet",0) > 1)
-      c1out.rdbuf(devnull.rdbuf());
+   InitOutput();
 
    // Match the operation
    CmdL.DispatchArg(Cmds);