X-Git-Url: https://git.saurik.com/apt.git/blobdiff_plain/c1553e72c6728572b06dc32395a994d5bfdc8e23..baec76f5f0f9fcbd71f6e2afaa7fc85543bd624c:/cmdline/apt-mark.cc diff --git a/cmdline/apt-mark.cc b/cmdline/apt-mark.cc index ef3a0d72f..0cba31e70 100644 --- a/cmdline/apt-mark.cc +++ b/cmdline/apt-mark.cc @@ -15,6 +15,7 @@ #include #include #include +#include #include #include #include @@ -23,6 +24,7 @@ #include #include +#include #include #include @@ -46,9 +48,8 @@ using namespace std; static bool DoAuto(CommandLine &CmdL) { pkgCacheFile CacheFile; - pkgCache *Cache = CacheFile.GetPkgCache(); - pkgDepCache *DepCache = CacheFile.GetDepCache(); - if (unlikely(Cache == NULL || DepCache == NULL)) + pkgDepCache * const DepCache = CacheFile.GetDepCache(); + if (unlikely(DepCache == nullptr)) return false; APT::PackageList pkgset = APT::PackageList::FromCommandLine(CacheFile, CmdL.FileList + 1); @@ -93,9 +94,8 @@ static bool DoAuto(CommandLine &CmdL) static bool DoMarkAuto(CommandLine &CmdL) { pkgCacheFile CacheFile; - pkgCache *Cache = CacheFile.GetPkgCache(); - pkgDepCache *DepCache = CacheFile.GetDepCache(); - if (unlikely(Cache == NULL || DepCache == NULL)) + pkgDepCache * const DepCache = CacheFile.GetDepCache(); + if (unlikely(DepCache == nullptr)) return false; APT::PackageList pkgset = APT::PackageList::FromCommandLine(CacheFile, CmdL.FileList + 1); @@ -130,9 +130,8 @@ static bool DoMarkAuto(CommandLine &CmdL) static bool ShowAuto(CommandLine &CmdL) { pkgCacheFile CacheFile; - pkgCache *Cache = CacheFile.GetPkgCache(); - pkgDepCache *DepCache = CacheFile.GetDepCache(); - if (unlikely(Cache == NULL || DepCache == NULL)) + pkgDepCache * const DepCache = CacheFile.GetDepCache(); + if (unlikely(DepCache == nullptr)) return false; std::vector packages; @@ -141,8 +140,8 @@ static bool ShowAuto(CommandLine &CmdL) if (CmdL.FileList[1] == 0) { - packages.reserve(Cache->HeaderP->PackageCount / 3); - for (pkgCache::PkgIterator P = Cache->PkgBegin(); P.end() == false; ++P) + packages.reserve(DepCache->Head().PackageCount / 3); + for (pkgCache::PkgIterator P = DepCache->PkgBegin(); P.end() == false; ++P) if (P->CurrentVer != 0 && (((*DepCache)[P].Flags & pkgCache::Flag::Auto) == pkgCache::Flag::Auto) == ShowAuto) packages.push_back(P.FullName(true)); @@ -166,205 +165,98 @@ static bool ShowAuto(CommandLine &CmdL) return true; } /*}}}*/ -/* DoHold - mark packages as hold by dpkg {{{*/ -static bool DoHold(CommandLine &CmdL) +// DoSelection - wrapping around dpkg selections /*{{{*/ +static bool DoSelection(CommandLine &CmdL) { pkgCacheFile CacheFile; - pkgCache *Cache = CacheFile.GetPkgCache(); - if (unlikely(Cache == NULL)) + pkgCache * const Cache = CacheFile.GetPkgCache(); + if (unlikely(Cache == nullptr)) return false; APT::VersionVector pkgset = APT::VersionVector::FromCommandLine(CacheFile, CmdL.FileList + 1, APT::CacheSetHelper::INSTCAND); if (pkgset.empty() == true) return _error->Error(_("No packages found")); - bool const MarkHold = strcasecmp(CmdL.FileList[0],"hold") == 0; - - auto const part = std::stable_partition(pkgset.begin(), pkgset.end(), - [](pkgCache::VerIterator const &V) { return V.ParentPkg()->SelectedState == pkgCache::State::Hold; }); - - auto const doneBegin = MarkHold ? pkgset.begin() : part; - auto const doneEnd = MarkHold ? part : pkgset.end(); - auto const changeBegin = MarkHold ? part : pkgset.begin(); - auto const changeEnd = MarkHold ? pkgset.end() : part; - - std::for_each(doneBegin, doneEnd, [&MarkHold](pkgCache::VerIterator const &V) { - if (MarkHold == true) - ioprintf(c1out, _("%s was already set on hold.\n"), V.ParentPkg().FullName(true).c_str()); - else - ioprintf(c1out, _("%s was already not hold.\n"), V.ParentPkg().FullName(true).c_str()); - }); - - if (doneBegin == pkgset.begin() && doneEnd == pkgset.end()) - return true; - - if (_config->FindB("APT::Mark::Simulate", false) == true) + APT::StateChanges marks; + if (strcasecmp(CmdL.FileList[0], "hold") == 0 || strcasecmp(CmdL.FileList[0], "unhold") == 0) { - std::for_each(changeBegin, changeEnd, [&MarkHold](pkgCache::VerIterator const &V) { - if (MarkHold == false) - ioprintf(c1out, _("%s set on hold.\n"), V.ParentPkg().FullName(true).c_str()); - else - ioprintf(c1out, _("Canceled hold on %s.\n"), V.ParentPkg().FullName(true).c_str()); - }); - return true; - } - - // Generate the base argument list for dpkg - std::vector Args; - string Tmp = _config->Find("Dir::Bin::dpkg","dpkg"); - { - string const dpkgChrootDir = _config->FindDir("DPkg::Chroot-Directory", "/"); - size_t dpkgChrootLen = dpkgChrootDir.length(); - if (dpkgChrootDir != "/" && Tmp.find(dpkgChrootDir) == 0) - { - if (dpkgChrootDir[dpkgChrootLen - 1] == '/') - --dpkgChrootLen; - Tmp = Tmp.substr(dpkgChrootLen); - } - } - Args.push_back(Tmp.c_str()); - - // Stick in any custom dpkg options - Configuration::Item const *Opts = _config->Tree("DPkg::Options"); - if (Opts != 0) - { - Opts = Opts->Child; - for (; Opts != 0; Opts = Opts->Next) - { - if (Opts->Value.empty() == true) - continue; - Args.push_back(Opts->Value.c_str()); - } - } - - APT::VersionVector keepoffset; - std::copy_if(changeBegin, changeEnd, std::back_inserter(keepoffset), - [](pkgCache::VerIterator const &V) { return V.ParentPkg()->CurrentVer == 0; }); - - if (keepoffset.empty() == false) - { - size_t const BaseArgs = Args.size(); - Args.push_back("--merge-avail"); - // FIXME: supported only since 1.17.7 in dpkg - 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 (auto const &V: keepoffset) - fprintf(dpkg, "Package: %s\nVersion: 0~\nArchitecture: %s\nMaintainer: Dummy Example \n" - "Description: dummy package record\n A record is needed to put a package on hold, so here it is.\n\n", V.ParentPkg().Name(), V.Arch()); - fclose(dpkg); - keepoffset.clear(); - - 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); - - int external[2] = {-1, -1}; - if (pipe(external) != 0) - return _error->WarningE("DoHold", "Can't create IPC pipe for dpkg --set-selections"); + auto const part = std::stable_partition(pkgset.begin(), pkgset.end(), + [](pkgCache::VerIterator const &V) { return V.ParentPkg()->SelectedState == pkgCache::State::Hold; }); + + bool const MarkHold = strcasecmp(CmdL.FileList[0],"hold") == 0; + auto const doneBegin = MarkHold ? pkgset.begin() : part; + auto const doneEnd = MarkHold ? part : pkgset.end(); + std::for_each(doneBegin, doneEnd, [&MarkHold](pkgCache::VerIterator const &V) { + if (MarkHold == true) + ioprintf(c1out, _("%s was already set on hold.\n"), V.ParentPkg().FullName(true).c_str()); + else + ioprintf(c1out, _("%s was already not hold.\n"), V.ParentPkg().FullName(true).c_str()); + }); + + if (doneBegin == pkgset.begin() && doneEnd == pkgset.end()) + return true; - pid_t dpkgSelection = ExecFork(); - if (dpkgSelection == 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 --set-selections", chrootDir.c_str()); - dup2(external[0], STDIN_FILENO); - execvp(Args[0], (char**) &Args[0]); - _error->WarningE("dpkgGo", "Can't get dpkg --set-selections running!"); - _exit(2); + auto const changeBegin = MarkHold ? part : pkgset.begin(); + auto const changeEnd = MarkHold ? pkgset.end() : part; + std::move(changeBegin, changeEnd, std::back_inserter(MarkHold ? marks.Hold() : marks.Unhold())); } - - bool const dpkgMultiArch = _system->MultiArchSupported(); - FILE* dpkg = fdopen(external[1], "w"); - for (auto Ver = changeBegin; Ver != changeEnd; ++Ver) + else { - pkgCache::PkgIterator P = Ver.ParentPkg(); - if (dpkgMultiArch == false) - fprintf(dpkg, "%s", P.FullName(true).c_str()); - else - fprintf(dpkg, "%s:%s", P.Name(), Ver.Arch()); - - if (MarkHold == true) - { - fprintf(dpkg, " hold\n"); - ioprintf(c1out,_("%s set on hold.\n"), P.FullName(true).c_str()); - } - else - { - fprintf(dpkg, " install\n"); - ioprintf(c1out,_("Canceled hold on %s.\n"), P.FullName(true).c_str()); - } + // FIXME: Maybe show a message for unchanged states here as well? + if (strcasecmp(CmdL.FileList[0], "purge") == 0) + std::swap(marks.Purge(), pkgset); + else if (strcasecmp(CmdL.FileList[0], "deinstall") == 0 || strcasecmp(CmdL.FileList[0], "remove") == 0) + std::swap(marks.Remove(), pkgset); + else //if (strcasecmp(CmdL.FileList[0], "install") == 0) + std::swap(marks.Install(), pkgset); } - fclose(dpkg); + pkgset.clear(); - if (dpkgSelection > 0) + bool success = true; + if (_config->FindB("APT::Mark::Simulate", false) == false) { - int Status = 0; - while (waitpid(dpkgSelection, &Status, 0) != dpkgSelection) - { - if (errno == EINTR) - continue; - _error->WarningE("dpkgGo", _("Waited for %s but it wasn't there"), "dpkg --set-selection"); - break; - } - if (WIFEXITED(Status) == true && WEXITSTATUS(Status) == 0) - return true; + success = marks.Save(); + if (success == false) + _error->Error(_("Executing dpkg failed. Are you root?")); } - return _error->Error(_("Executing dpkg failed. Are you root?")); + for (auto Ver : marks.Hold()) + ioprintf(c1out,_("%s set on hold.\n"), Ver.ParentPkg().FullName(true).c_str()); + for (auto Ver : marks.Unhold()) + ioprintf(c1out,_("Canceled hold on %s.\n"), Ver.ParentPkg().FullName(true).c_str()); + for (auto Ver : marks.Purge()) + ioprintf(c1out,_("Selected %s for purge.\n"), Ver.ParentPkg().FullName(true).c_str()); + for (auto Ver : marks.Remove()) + ioprintf(c1out,_("Selected %s for removal.\n"), Ver.ParentPkg().FullName(true).c_str()); + for (auto Ver : marks.Install()) + ioprintf(c1out,_("Selected %s for installation.\n"), Ver.ParentPkg().FullName(true).c_str()); + return success; } /*}}}*/ -/* ShowHold - show packages set on hold in dpkg status {{{*/ -static bool ShowHold(CommandLine &CmdL) +static bool ShowSelection(CommandLine &CmdL) /*{{{*/ { pkgCacheFile CacheFile; - pkgCache *Cache = CacheFile.GetPkgCache(); - if (unlikely(Cache == NULL)) + pkgCache * const Cache = CacheFile.GetPkgCache(); + if (unlikely(Cache == nullptr)) return false; + pkgCache::State::PkgSelectedState selector; + if (strncasecmp(CmdL.FileList[0], "showpurge", strlen("showpurge")) == 0) + selector = pkgCache::State::Purge; + else if (strncasecmp(CmdL.FileList[0], "showdeinstall", strlen("showdeinstall")) == 0 || + strncasecmp(CmdL.FileList[0], "showremove", strlen("showremove")) == 0) + selector = pkgCache::State::DeInstall; + else if (strncasecmp(CmdL.FileList[0], "showhold", strlen("showhold")) == 0) + selector = pkgCache::State::Hold; + else //if (strcasecmp(CmdL.FileList[0], "showinstall", strlen("showinstall")) == 0) + selector = pkgCache::State::Install; + std::vector packages; if (CmdL.FileList[1] == 0) { packages.reserve(50); // how many holds are realistic? I hope just a few… for (pkgCache::PkgIterator P = Cache->PkgBegin(); P.end() == false; ++P) - if (P->SelectedState == pkgCache::State::Hold) + if (P->SelectedState == selector) packages.push_back(P.FullName(true)); } else @@ -373,7 +265,7 @@ static bool ShowHold(CommandLine &CmdL) APT::PackageSet pkgset = APT::PackageSet::FromCommandLine(CacheFile, CmdL.FileList + 1, helper); packages.reserve(pkgset.size()); for (APT::PackageSet::const_iterator P = pkgset.begin(); P != pkgset.end(); ++P) - if (P->SelectedState == pkgCache::State::Hold) + if (P->SelectedState == selector) packages.push_back(P.FullName(true)); } @@ -385,80 +277,50 @@ static bool ShowHold(CommandLine &CmdL) return true; } /*}}}*/ -// ShowHelp - Show a help screen /*{{{*/ -// --------------------------------------------------------------------- -/* */ -static bool ShowHelp(CommandLine &) +static bool ShowHelp(CommandLine &) /*{{{*/ { - ioprintf(std::cout, "%s %s (%s)\n", PACKAGE, PACKAGE_VERSION, COMMON_ARCH); - - cout << + std::cout << _("Usage: apt-mark [options] {auto|manual} pkg1 [pkg2 ...]\n" "\n" "apt-mark is a simple command line interface for marking packages\n" - "as manually or automatically installed. It can also list marks.\n" - "\n" - "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" - " -q Loggable output - no progress indicator\n" - " -qq No output except for errors\n" - " -s No-act. Just prints what would be done.\n" - " -f read/write auto/manual marking in the given file\n" - " -c=? Read this configuration file\n" - " -o=? Set an arbitrary configuration option, eg -o dir::cache=/tmp\n" - "See the apt-mark(8) and apt.conf(5) manual pages for more information.") - << std::endl; + "as manually or automatically installed. It can also be used to\n" + "manipulate the dpkg(1) selection states of packages, and to list\n" + "all packages with or without a certain marking.\n"); return true; } /*}}}*/ +static std::vector GetCommands() /*{{{*/ +{ + return { + {"auto",&DoAuto, _("Mark the given packages as automatically installed")}, + {"manual",&DoAuto, _("Mark the given packages as manually installed")}, + {"hold",&DoSelection, _("Mark a package as held back")}, + {"unhold",&DoSelection, _("Unset a package set as held back")}, + {"install",&DoSelection, nullptr}, + {"remove",&DoSelection, nullptr}, // dpkg uses deinstall, but we use remove everywhere else + {"deinstall",&DoSelection, nullptr}, + {"purge",&DoSelection, nullptr}, + {"showauto",&ShowAuto, _("Print the list of automatically installed packages")}, + {"showmanual",&ShowAuto, _("Print the list of manually installed packages")}, + {"showhold",&ShowSelection, _("Print the list of package on hold")}, {"showholds",&ShowSelection, nullptr}, + {"showinstall",&ShowSelection, nullptr}, {"showinstalls",&ShowSelection, nullptr}, + {"showdeinstall",&ShowSelection, nullptr}, {"showdeinstalls",&ShowSelection, nullptr}, + {"showremove",&ShowSelection, nullptr}, {"showremoves",&ShowSelection, nullptr}, + {"showpurge",&ShowSelection, nullptr}, {"showpurges",&ShowSelection, nullptr}, + // obsolete commands for compatibility + {"markauto", &DoMarkAuto, nullptr}, + {"unmarkauto", &DoMarkAuto, nullptr}, + {nullptr, nullptr, nullptr} + }; +} + /*}}}*/ int main(int argc,const char *argv[]) /*{{{*/ { - CommandLine::Dispatch Cmds[] = {{"help",&ShowHelp}, - {"auto",&DoAuto}, - {"manual",&DoAuto}, - {"hold",&DoHold}, - {"unhold",&DoHold}, - {"showauto",&ShowAuto}, - {"showmanual",&ShowAuto}, - {"showhold",&ShowHold}, - // be nice and forgive the typo - {"showholds",&ShowHold}, - // be nice and forgive it as it is technical right - {"install",&DoHold}, - // obsolete commands for compatibility - {"markauto", &DoMarkAuto}, - {"unmarkauto", &DoMarkAuto}, - {0,0}}; - - std::vector Args = getCommandArgs("apt-mark", CommandLine::GetCommand(Cmds, argc, argv)); - - // Set up gettext support - setlocale(LC_ALL,""); - textdomain(PACKAGE); - CommandLine CmdL; - ParseCommandLine(CmdL, Cmds, Args.data(), &_config, &_system, argc, argv, ShowHelp); + auto const Cmds = ParseCommandLine(CmdL, APT_CMD::APT_MARK, &_config, &_system, argc, argv, &ShowHelp, &GetCommands); InitOutput(); - // Match the operation - CmdL.DispatchArg(Cmds); - - // Print any errors or warnings found during parsing - bool const Errors = _error->PendingError(); - if (_config->FindI("quiet",0) > 0) - _error->DumpErrors(); - else - _error->DumpErrors(GlobalError::DEBUG); - return Errors == true ? 100 : 0; + return DispatchCommandLine(CmdL, Cmds); } /*}}}*/