X-Git-Url: https://git.saurik.com/apt.git/blobdiff_plain/e75aa33384d52635fba502bed628bc68f9cb5066..e7e10e47476606e3b2274cf66b1e8ea74b236757:/cmdline/apt-mark.cc diff --git a/cmdline/apt-mark.cc b/cmdline/apt-mark.cc index 9a628d714..0a5bb164a 100644 --- a/cmdline/apt-mark.cc +++ b/cmdline/apt-mark.cc @@ -11,23 +11,41 @@ #include #include #include +#include #include -#include #include - -#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include #include +#include +#include +#include +#include +#include #include /*}}}*/ 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(); @@ -35,14 +53,14 @@ bool DoAuto(CommandLine &CmdL) if (unlikely(Cache == NULL || DepCache == NULL)) return false; - APT::PackageSet pkgset = APT::PackageSet::FromCommandLine(CacheFile, CmdL.FileList + 1); + APT::PackageList pkgset = APT::PackageList::FromCommandLine(CacheFile, CmdL.FileList + 1); if (pkgset.empty() == true) return _error->Error(_("No packages found")); bool MarkAuto = strcasecmp(CmdL.FileList[0],"auto") == 0; int AutoMarkChanged = 0; - for (APT::PackageSet::const_iterator Pkg = pkgset.begin(); Pkg != pkgset.end(); ++Pkg) + for (APT::PackageList::const_iterator Pkg = pkgset.begin(); Pkg != pkgset.end(); ++Pkg) { if (Pkg->CurrentVer == 0) { @@ -74,7 +92,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(); @@ -82,7 +100,7 @@ bool DoMarkAuto(CommandLine &CmdL) if (unlikely(Cache == NULL || DepCache == NULL)) return false; - APT::PackageSet pkgset = APT::PackageSet::FromCommandLine(CacheFile, CmdL.FileList + 1); + APT::PackageList pkgset = APT::PackageList::FromCommandLine(CacheFile, CmdL.FileList + 1); if (pkgset.empty() == true) return _error->Error(_("No packages found")); @@ -90,7 +108,7 @@ bool DoMarkAuto(CommandLine &CmdL) bool const Verbose = _config->FindB("APT::MarkAuto::Verbose", false); int AutoMarkChanged = 0; - for (APT::PackageSet::const_iterator Pkg = pkgset.begin(); Pkg != pkgset.end(); ++Pkg) + for (APT::PackageList::const_iterator Pkg = pkgset.begin(); Pkg != pkgset.end(); ++Pkg) { if (Pkg->CurrentVer == 0 || (((*DepCache)[Pkg].Flags & pkgCache::Flag::Auto) == pkgCache::Flag::Auto) == MarkAuto) @@ -111,7 +129,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(); @@ -150,95 +168,98 @@ bool ShowAuto(CommandLine &CmdL) return true; } /*}}}*/ -/* DoHold - mark packages as hold by dpkg {{{*/ -bool DoHold(CommandLine &CmdL) +// DoSelection - wrapping around dpkg selections /*{{{*/ +static bool DoSelection(CommandLine &CmdL) { pkgCacheFile CacheFile; pkgCache *Cache = CacheFile.GetPkgCache(); if (unlikely(Cache == NULL)) return false; - APT::PackageSet pkgset = APT::PackageSet::FromCommandLine(CacheFile, CmdL.FileList + 1); + 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; - - for (APT::PackageSet::iterator Pkg = pkgset.begin(); Pkg != pkgset.end(); ++Pkg) + APT::StateChanges marks; + if (strcasecmp(CmdL.FileList[0], "hold") == 0 || strcasecmp(CmdL.FileList[0], "unhold") == 0) { - if ((Pkg->SelectedState == pkgCache::State::Hold) == MarkHold) - { - if (MarkHold == true) - ioprintf(c1out,_("%s was already set on hold.\n"), Pkg.FullName(true).c_str()); - else - ioprintf(c1out,_("%s was already not hold.\n"), Pkg.FullName(true).c_str()); - pkgset.erase(Pkg); - continue; - } + 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; + + 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())); } - - if (pkgset.empty() == true) - return true; - - if (_config->FindB("APT::Mark::Simulate", false) == true) + else { - for (APT::PackageSet::iterator Pkg = pkgset.begin(); Pkg != pkgset.end(); ++Pkg) - { - if (MarkHold == false) - ioprintf(c1out,_("%s set on hold.\n"), Pkg.FullName(true).c_str()); - else - ioprintf(c1out,_("Canceled hold on %s.\n"), Pkg.FullName(true).c_str()); - } - return true; + // 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); } + pkgset.clear(); - string dpkgcall = _config->Find("Dir::Bin::dpkg", "dpkg"); - std::vector const dpkgoptions = _config->FindVector("DPkg::options"); - for (std::vector::const_iterator o = dpkgoptions.begin(); - o != dpkgoptions.end(); ++o) - dpkgcall.append(" ").append(*o); - dpkgcall.append(" --set-selections"); - FILE *dpkg = popen(dpkgcall.c_str(), "w"); - if (dpkg == NULL) - return _error->Errno("DoHold", "fdopen on dpkg stdin failed"); - - for (APT::PackageSet::iterator Pkg = pkgset.begin(); Pkg != pkgset.end(); ++Pkg) + bool success = true; + if (_config->FindB("APT::Mark::Simulate", false) == false) { - if (MarkHold == true) - { - fprintf(dpkg, "%s hold\n", Pkg.FullName(true).c_str()); - ioprintf(c1out,_("%s set on hold.\n"), Pkg.FullName(true).c_str()); - } - else - { - fprintf(dpkg, "%s install\n", Pkg.FullName(true).c_str()); - ioprintf(c1out,_("Canceled hold on %s.\n"), Pkg.FullName(true).c_str()); - } + success = marks.Save(); + if (success == false) + _error->Error(_("Executing dpkg failed. Are you root?")); } - - int const status = pclose(dpkg); - if (status == -1) - return _error->Errno("DoHold", "dpkg execution failed in the end"); - if (WIFEXITED(status) == false || WEXITSTATUS(status) != 0) - return _error->Error(_("Executing dpkg failed. Are you root?")); - return true; + 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 {{{*/ -bool ShowHold(CommandLine &CmdL) +static bool ShowSelection(CommandLine &CmdL) /*{{{*/ { pkgCacheFile CacheFile; pkgCache *Cache = CacheFile.GetPkgCache(); if (unlikely(Cache == NULL)) 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 @@ -247,7 +268,7 @@ 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)); } @@ -260,24 +281,27 @@ bool ShowHold(CommandLine &CmdL) } /*}}}*/ // ShowHelp - Show a help screen /*{{{*/ -// --------------------------------------------------------------------- -/* */ -bool ShowHelp(CommandLine &CmdL) +static bool ShowHelp(CommandLine &, CommandLine::DispatchWithHelp const * Cmds) { - ioprintf(cout,_("%s %s for %s compiled on %s %s\n"),PACKAGE,VERSION, - COMMON_ARCH,__DATE__,__TIME__); + 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 manual or automatical 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" - "\n" - "Options:\n" + "as manually or automatically installed. It can also list marks.\n") + << std::endl + << _("Commands:") << std::endl; + + for (; Cmds->Handler != nullptr; ++Cmds) + { + if (Cmds->Help == nullptr) + continue; + std::cout << " " << Cmds->Match << " - " << Cmds->Help << std::endl; + } + + std::cout << std::endl + << _("Options:\n" " -h This help text.\n" " -q Loggable output - no progress indicator\n" " -qq No output except for errors\n" @@ -292,85 +316,35 @@ 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}, - {"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}}; - - // 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; - } - - // 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()); - - // 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; + InitLocale(); + + CommandLine::DispatchWithHelp Cmds[] = { + {"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} + }; + + CommandLine CmdL; + ParseCommandLine(CmdL, Cmds, "apt-mark", &_config, &_system, argc, argv, ShowHelp); + + InitOutput(); + + return DispatchCommandLine(CmdL, Cmds); } /*}}}*/