X-Git-Url: https://git.saurik.com/apt.git/blobdiff_plain/89d88ac3ef3f82fdfeac6d8d231deddeeb0f02e9..2853201580944cb4e5a2a8a810e800e89e9a0475:/cmdline/apt-extracttemplates.cc

diff --git a/cmdline/apt-extracttemplates.cc b/cmdline/apt-extracttemplates.cc
index d5c1a3208..8f77425e4 100644
--- a/cmdline/apt-extracttemplates.cc
+++ b/cmdline/apt-extracttemplates.cc
@@ -18,43 +18,45 @@
 #include <apt-pkg/init.h>
 #include <apt-pkg/cmndline.h>
 #include <apt-pkg/pkgcache.h>
+#include <apt-pkg/cacheiterators.h>
 #include <apt-pkg/configuration.h>
-#include <apt-pkg/progress.h>
 #include <apt-pkg/sourcelist.h>
 #include <apt-pkg/pkgcachegen.h>
 #include <apt-pkg/version.h>
 #include <apt-pkg/tagfile.h>
-#include <apt-pkg/extracttar.h>
-#include <apt-pkg/arfile.h>
+#include <apt-pkg/debfile.h>
 #include <apt-pkg/deblistparser.h>
 #include <apt-pkg/error.h>
 #include <apt-pkg/strutl.h>
 #include <apt-pkg/fileutl.h>
 #include <apt-pkg/pkgsystem.h>
+#include <apt-pkg/dirstream.h>
+#include <apt-pkg/mmap.h>
 
+#include <apt-private/private-cmndline.h>
+#include <apt-private/private-main.h>
+
+#include <iostream>
 #include <stdio.h>
 #include <string.h>
-#include <stdlib.h>
 #include <unistd.h>
-#include <fstream>
+#include <stdlib.h>
 
-#include <locale.h>
-#include <apti18n.h>
 #include "apt-extracttemplates.h"
+
+#include <apti18n.h>
 									/*}}}*/
 
 using namespace std;
 
-#define TMPDIR		"/tmp"
-
 pkgCache *DebFile::Cache = 0;
 
 // DebFile::DebFile - Construct the DebFile object			/*{{{*/
 // ---------------------------------------------------------------------
 /* */
 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)
 {
 }
 									/*}}}*/
@@ -91,18 +93,9 @@ string DebFile::GetInstalledVer(const string &package)
 /* */
 bool DebFile::Go()
 {
-	ARArchive AR(File);
-	if (_error->PendingError() == true)
-		return false;
-		
-	const ARArchive::Member *Member = AR.FindMember("control.tar.gz");
-	if (Member == 0)
-		return _error->Error(_("%s not a valid DEB package."),File.Name().c_str());
-	
-	if (File.Seek(Member->Start) == false)
-		return false;
-	ExtractTar Tar(File, Member->Size,"gzip");
-	return Tar.Go(*this);
+	debDebFile Deb(File);
+
+	return Deb.ExtractTarMember(*this, "control.tar");
 }
 									/*}}}*/
 // DebFile::DoItem examine element in package and mark			/*{{{*/
@@ -113,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;
 	}
@@ -147,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)
 	{
@@ -172,9 +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);
@@ -219,57 +215,42 @@ bool DebFile::ParseInfo()
 	return true;
 }
 									/*}}}*/
-// ShowHelp - show a short help text					/*{{{*/
-// ---------------------------------------------------------------------
-/* */
-int ShowHelp(void)
+static bool ShowHelp(CommandLine &)					/*{{{*/
 {
-   	ioprintf(cout,_("%s %s for %s compiled on %s %s\n"),PACKAGE,VERSION,
-	    COMMON_ARCH,__DATE__,__TIME__);
-
-	if (_config->FindB("version") == true) 
-		return 0;
-
-	cout << 
+	cout <<
 		_("Usage: apt-extracttemplates file1 [file2 ...]\n"
 		"\n"
-		"apt-extracttemplates is a tool to extract config and template info\n"
-		"from debian packages\n"
-		"\n"
-		"Options:\n"
-	        "  -h   This help text\n"
-		"  -t   Set the temp dir\n"
-		"  -c=? Read this configuration file\n"
-		"  -o=? Set an arbitrary configuration option, eg -o dir::cache=/tmp\n");
-	return 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	/*{{{*/
 // ---------------------------------------------------------------------
 /* */
-string WriteFile(const char *package, const char *prefix, const char *data)
+static string WriteFile(const char *package, const char *prefix, const char *data)
 {
 	char fn[512];
-	static int i;
-	const char *tempdir = NULL;
-
-        tempdir = getenv("TMPDIR");
-        if (tempdir == NULL)
-             tempdir = TMPDIR;
 
-	snprintf(fn, sizeof(fn), "%s/%s.%s.%u%d",
-                 _config->Find("APT::ExtractTemplates::TempDir", tempdir).c_str(),
-                 package, prefix, getpid(), i++);
+        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 = "";
-
-	if (!f.Open(fn, FileFd::WriteTemp, 0600))
+        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;
@@ -278,7 +259,7 @@ string WriteFile(const char *package, const char *prefix, const char *data)
 // WriteConfig - write out the config data from a debian package file	/*{{{*/
 // ---------------------------------------------------------------------
 /* */
-void WriteConfig(const DebFile &file)
+static void WriteConfig(const DebFile &file)
 {
 	string templatefile = WriteFile(file.Package.c_str(), "template", file.Template);
 	string configscript = WriteFile(file.Package.c_str(), "config", file.Config);
@@ -292,7 +273,7 @@ void WriteConfig(const DebFile &file)
 // InitCache - initialize the package cache				/*{{{*/
 // ---------------------------------------------------------------------
 /* */
-bool Go(CommandLine &CmdL)
+static bool Go(CommandLine &CmdL)
 {	
 	// Initialize the apt cache
 	MMap *Map = 0;
@@ -349,46 +330,22 @@ bool Go(CommandLine &CmdL)
 	return !_error->PendingError();
 }
 									/*}}}*/
+static std::vector<aptDispatchWithHelp> GetCommands()			/*{{{*/
+{
+   return {
+	{nullptr, nullptr, nullptr}
+   };
+}
+									/*}}}*/
 int main(int argc, const char **argv)					/*{{{*/
 {
-	CommandLine::Args Args[] = {
-		{'h',"help","help",0},
-		{'v',"version","version",0},
-		{'t',"tempdir","APT::ExtractTemplates::TempDir",CommandLine::HasArg},
-		{'c',"config-file",0,CommandLine::ConfigFile},
-		{'o',"option",0,CommandLine::ArbItem},
-		{0,0,0,0}};
+	InitLocale();
 
-	// Set up gettext support
-	setlocale(LC_ALL,"");
-	textdomain(PACKAGE);
+	CommandLine CmdL;
+	auto const Cmds = ParseCommandLine(CmdL, APT_CMD::APT_EXTRACTTEMPLATES, &_config, &_system, argc, argv, &ShowHelp, &GetCommands);
 
-	// 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)
-	{
-		_error->DumpErrors();
-		return 100;
-	}
-	
-	// See if the help should be shown
-	if (_config->FindB("help") == true ||
-	    CmdL.FileSize() == 0)
-		return ShowHelp();
-	
 	Go(CmdL);
 
-	// Print any errors or warnings found during operation
-	if (_error->empty() == false)
-	{
-		// This goes to stderr..
-		bool Errors = _error->PendingError();
-		_error->DumpErrors();
-		return Errors == true?100:0;
-	}
-	
-	return 0;
+	return DispatchCommandLine(CmdL, {});
 }
 									/*}}}*/