X-Git-Url: https://git.saurik.com/apt.git/blobdiff_plain/234edfd074bd8a356494d5d8df8774cf1b096fd8..ef1e4dfd2853abb2e8900c26c1c17bf053863e85:/cmdline/apt-extracttemplates.cc diff --git a/cmdline/apt-extracttemplates.cc b/cmdline/apt-extracttemplates.cc index ecba44ade..a4468b989 100644 --- a/cmdline/apt-extracttemplates.cc +++ b/cmdline/apt-extracttemplates.cc @@ -1,6 +1,6 @@ // -*- mode: cpp; mode: fold -*- // Description /*{{{*/ -// $Id: apt-extracttemplates.cc,v 1.2 2001/02/25 04:53:59 tausq Exp $ +// $Id: apt-extracttemplates.cc,v 1.15 2003/07/26 00:00:11 mdz Exp $ /* ###################################################################### APT Extract Templates - Program to extract debconf config and template @@ -13,38 +13,41 @@ ##################################################################### */ /*}}}*/ // Include Files /*{{{*/ +#include + #include -#if APT_PKG_MAJOR >= 3 -#define APT_COMPATIBILITY 986 -#include -#endif +#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 -#define _GNU_SOURCE -#include -#include -#include +#include -#include -#include #include "apt-extracttemplates.h" -#define TMPDIR "/var/lib/debconf/" +#include + /*}}}*/ + +using namespace std; pkgCache *DebFile::Cache = 0; @@ -52,8 +55,8 @@ pkgCache *DebFile::Cache = 0; // --------------------------------------------------------------------- /* */ DebFile::DebFile(const char *debfile) - : File(debfile, FileFd::ReadOnly), Control(0), DepOp(0), PreDepOp(0), - Config(0), Template(0), Which(None) + : File(debfile, FileFd::ReadOnly), Control(NULL), ControlLen(0), + DepOp(0), PreDepOp(0), Config(0), Template(0), Which(None) { } /*}}}*/ @@ -70,21 +73,19 @@ DebFile::~DebFile() // DebFile::GetInstalledVer - Find out the installed version of a pkg /*{{{*/ // --------------------------------------------------------------------- /* */ -char *DebFile::GetInstalledVer(const string &package) +string DebFile::GetInstalledVer(const string &package) { - char *ver = 0; - pkgCache::PkgIterator Pkg = Cache->FindPkg(package); if (Pkg.end() == false) { pkgCache::VerIterator V = Pkg.CurrentVer(); - if (V.end() == false) + if (V.end() == false) { - ver = strdup(V.VerStr()); + return V.VerStr(); } } - return ver; + return string(); } /*}}}*/ // DebFile::Go - Start extracting a debian package /*{{{*/ @@ -92,21 +93,9 @@ char *DebFile::GetInstalledVer(const string &package) /* */ bool DebFile::Go() { - ARArchive AR(File); - const ARArchive::Member *Member = AR.FindMember("control.tar.gz"); - if (Member == 0) - { - fprintf(stderr, _("This is not a valid DEB package.\n")); - return false; - } - - if (File.Seek(Member->Start) == false) - { - return false; - } + debDebFile Deb(File); - ExtractTar Tar(File, Member->Size); - return Tar.Go(*this); + return Deb.ExtractTarMember(*this, "control.tar"); } /*}}}*/ // DebFile::DoItem examine element in package and mark /*{{{*/ @@ -117,10 +106,12 @@ bool DebFile::DoItem(Item &I, int &Fd) if (strcmp(I.Name, "control") == 0) { delete [] Control; - Control = new char[I.Size+1]; - Control[I.Size] = 0; + Control = new char[I.Size+3]; + Control[I.Size] = '\n'; + Control[I.Size + 1] = '\n'; + Control[I.Size + 2] = '\0'; Which = IsControl; - ControlLen = I.Size; + ControlLen = I.Size + 3; // make it call the Process method below. this is so evil Fd = -2; } @@ -142,6 +133,7 @@ bool DebFile::DoItem(Item &I, int &Fd) } else { + // Ignore it Fd = -1; } return true; @@ -150,8 +142,8 @@ bool DebFile::DoItem(Item &I, int &Fd) // DebFile::Process examine element in package and copy /*{{{*/ // --------------------------------------------------------------------- /* */ -bool DebFile::Process(Item &I, const unsigned char *data, - unsigned long size, unsigned long pos) +bool DebFile::Process(Item &/*I*/, const unsigned char *data, + unsigned long long size, unsigned long long pos) { switch (Which) { @@ -175,8 +167,10 @@ bool DebFile::Process(Item &I, const unsigned char *data, bool DebFile::ParseInfo() { if (Control == NULL) return false; + pkgTagSection Section; - Section.Scan(Control, ControlLen); + if (Section.Scan(Control, ControlLen) == false) + return false; Package = Section.FindS("Package"); Version = GetInstalledVer(Package); @@ -221,127 +215,108 @@ bool DebFile::ParseInfo() return true; } /*}}}*/ -// ShowHelp - show a short help text /*{{{*/ -// --------------------------------------------------------------------- -/* */ -void ShowHelp(void) +static bool ShowHelp(CommandLine &) /*{{{*/ { - ioprintf(cout,_("%s %s for %s %s compiled on %s %s\n"),PACKAGE,VERSION, - COMMON_OS,COMMON_CPU,__DATE__,__TIME__); - - fprintf(stderr, + cout << _("Usage: apt-extracttemplates file1 [file2 ...]\n" "\n" - "apt-extracttemplates is a tool to extract config and template info\n" - "from debian packages\n")); - exit(0); + "apt-extracttemplates is used to extract config and template files\n" + "from debian packages. It is used mainly by debconf(1) to prompt for\n" + "configuration questions before installation of packages.\n"); + return true; } /*}}}*/ // WriteFile - write the contents of the passed string to a file /*{{{*/ // --------------------------------------------------------------------- /* */ -char *WriteFile(const char *prefix, const char *data) +static string WriteFile(const char *package, const char *prefix, const char *data) { char fn[512]; - static int i; - snprintf(fn, sizeof(fn), "%s%s.%u%d", TMPDIR, prefix, getpid(), i++); - ofstream ofs(fn); - if (!ofs) return NULL; - ofs << (data ? data : ""); - ofs.close(); - return strdup(fn); + std::string tempdir = GetTempDir(); + snprintf(fn, sizeof(fn), "%s/%s.%s.XXXXXX", + _config->Find("APT::ExtractTemplates::TempDir", + tempdir.c_str()).c_str(), + package, prefix); + FileFd f; + if (data == NULL) + data = ""; + int fd = mkstemp(fn); + if (fd < 0) { + _error->Errno("ofstream::ofstream",_("Unable to mkstemp %s"),fn); + return string(); + } + if (!f.OpenDescriptor(fd, FileFd::WriteOnly, FileFd::None, true)) + { + _error->Errno("ofstream::ofstream",_("Unable to write to %s"),fn); + return string(); + } + f.Write(data, strlen(data)); + f.Close(); + return fn; } /*}}}*/ // WriteConfig - write out the config data from a debian package file /*{{{*/ // --------------------------------------------------------------------- /* */ -void WriteConfig(const DebFile &file) +static void WriteConfig(const DebFile &file) { - char *templatefile = WriteFile("template", file.Template); - char *configscript = WriteFile("config", file.Config); + string templatefile = WriteFile(file.Package.c_str(), "template", file.Template); + string configscript = WriteFile(file.Package.c_str(), "config", file.Config); - if (templatefile == 0 || configscript == 0) - { - fprintf(stderr, _("Cannot write config script or templates\n")); + if (templatefile.empty() == true || configscript.empty() == true) return; - } cout << file.Package << " " << file.Version << " " << templatefile << " " << configscript << endl; - - free(templatefile); - free(configscript); } /*}}}*/ // InitCache - initialize the package cache /*{{{*/ // --------------------------------------------------------------------- /* */ -int InitCache(MMap *&Map, pkgCache *&Cache) -{ +static bool Go(CommandLine &CmdL) +{ // Initialize the apt cache - if (pkgInitConfig(*_config) == false || pkgInitSystem(*_config, _system) == false) - { - _error->DumpErrors(); - return -1; - } + MMap *Map = 0; pkgSourceList List; List.ReadMainList(); - OpProgress Prog; - pkgMakeStatusCache(List,Prog,&Map,true); - Cache = new pkgCache(Map); - return 0; -} - /*}}}*/ - -int main(int argc, char **argv, char **env) -{ - int idx = 0; - char **debs = 0; - int numdebs = 0; - MMap *Map = 0; - const char *debconfver = NULL; - - // Initialize the package cache - if (InitCache(Map, DebFile::Cache) < 0 || Map == 0 || DebFile::Cache == 0) - { - fprintf(stderr, _("Cannot initialize APT cache\n")); - return 100; - } + pkgCacheGenerator::MakeStatusCache(List,NULL,&Map,true); + if (Map == 0) + return false; + DebFile::Cache = new pkgCache(Map); + if (_error->PendingError() == true) + return false; // Find out what version of debconf is currently installed - if ((debconfver = DebFile::GetInstalledVer("debconf")) == NULL) - { - fprintf(stderr, _("Cannot get debconf version. Is debconf installed?\n")); - return 1; - } + string debconfver = DebFile::GetInstalledVer("debconf"); + if (debconfver.empty() == true) + return _error->Error( _("Cannot get debconf version. Is debconf installed?")); // Process each package passsed in - numdebs = argc - 1; - debs = new char *[numdebs]; - memcpy(debs, &argv[1], sizeof(char *) * numdebs); - - if (numdebs < 1) ShowHelp(); - - for (idx = 0; idx < numdebs; idx++) + for (unsigned int I = 0; I != CmdL.FileSize(); I++) { - DebFile file(debs[idx]); - if (file.Go() == false) + // Will pick up the errors later.. + DebFile file(CmdL.FileList[I]); + if (file.Go() == false) { - fprintf(stderr, _("Cannot read %s\n"), debs[idx]); + _error->Error("Prior errors apply to %s",CmdL.FileList[I]); continue; } + // Does the package have templates? if (file.Template != 0 && file.ParseInfo() == true) { // Check to make sure debconf dependencies are // satisfied + // cout << "Check " << file.DepVer << ',' << debconfver << endl; if (file.DepVer != "" && - pkgCheckDep(file.DepVer.c_str(), - debconfver, file.DepOp) == false) + DebFile::Cache->VS->CheckDep(debconfver.c_str(), + file.DepOp,file.DepVer.c_str() + ) == false) continue; if (file.PreDepVer != "" && - pkgCheckDep(file.PreDepVer.c_str(), - debconfver, file.PreDepOp) == false) + DebFile::Cache->VS->CheckDep(debconfver.c_str(), + file.PreDepOp,file.PreDepVer.c_str() + ) == false) continue; WriteConfig(file); @@ -351,6 +326,24 @@ int main(int argc, char **argv, char **env) delete Map; delete DebFile::Cache; + + return !_error->PendingError(); +} + /*}}}*/ +static std::vector GetCommands() /*{{{*/ +{ + return { + {nullptr, nullptr, nullptr} + }; +} + /*}}}*/ +int main(int argc, const char **argv) /*{{{*/ +{ + CommandLine CmdL; + auto const Cmds = ParseCommandLine(CmdL, APT_CMD::APT_EXTRACTTEMPLATES, &_config, &_system, argc, argv, &ShowHelp, &GetCommands); + + Go(CmdL); - return 0; + return DispatchCommandLine(CmdL, {}); } + /*}}}*/