X-Git-Url: https://git.saurik.com/apt.git/blobdiff_plain/c3753d1de25737aed66d6ca14d0473042b7c55a7..40ebab6ac9dcbbebd3c3e6d70eee0d6e3ab36520:/cmdline/apt-dump-solver.cc

diff --git a/cmdline/apt-dump-solver.cc b/cmdline/apt-dump-solver.cc
index aa16b1271..0de248e75 100644
--- a/cmdline/apt-dump-solver.cc
+++ b/cmdline/apt-dump-solver.cc
@@ -9,23 +9,26 @@
 // Include Files							/*{{{*/
 #include <apt-pkg/edsp.h>
 
-#include <config.h>
-
+#include <string.h>
+#include <unistd.h>
 #include <cstdio>
 #include <iostream>
+#include <sstream>
+
+#include <config.h>
 									/*}}}*/
 
 // ShowHelp - Show a help screen					/*{{{*/
 // ---------------------------------------------------------------------
 /* */
-bool ShowHelp() {
-
+static bool ShowHelp() {
+	ioprintf(std::cout, "%s %s (%s)\n", PACKAGE, PACKAGE_VERSION, COMMON_ARCH);
 	std::cout <<
-		PACKAGE " " PACKAGE_VERSION " for " COMMON_ARCH " compiled on " __DATE__ " " __TIME__ << std::endl <<
-		"Usage: apt-dump-resolver\n"
+		"Usage: apt-dump-solver\n"
 		"\n"
-		"apt-dump-resolver is a dummy solver who just dumps its input to the\n"
-		"file /tmp/dump.edsp and exists with a proper EDSP error.\n"
+		"apt-dump-solver is a dummy solver who just dumps its input to the\n"
+		"file specified in the environment variable APT_EDSP_DUMP_FILENAME and\n"
+		"exists with a proper EDSP error.\n"
 		"\n"
 		"                       This dump has lost Super Cow Powers.\n";
 	return true;
@@ -33,19 +36,41 @@ bool ShowHelp() {
 									/*}}}*/
 int main(int argc,const char *argv[])					/*{{{*/
 {
+	// we really don't need anything
+	DropPrivileges();
+
 	if (argc > 1 && (strcmp(argv[1], "--help") == 0 || strcmp(argv[1],"-h") == 0 ||
 	    strcmp(argv[1],"-v") == 0 || strcmp(argv[1],"--version") == 0)) {
 		ShowHelp();
 		return 0;
 	}
 
-	FILE* input = fdopen(STDIN_FILENO, "r");
-	FILE* output = fopen("/tmp/dump.edsp", "w");
-	char buffer[400];
-	while (fgets(buffer, sizeof(buffer), input) != NULL)
-		fputs(buffer, output);
-	fclose(output);
-	fclose(input);
+	FileFd stdoutfd;
+	if (stdoutfd.OpenDescriptor(STDOUT_FILENO, FileFd::WriteOnly | FileFd::BufferedWrite, true) == false)
+	   return 1;
+
+	char const * const filename = getenv("APT_EDSP_DUMP_FILENAME");
+	if (filename == NULL || strlen(filename) == 0)
+	{
+	   EDSP::WriteError("ERR_NO_FILENAME", "You have to set the environment variable APT_EDSP_DUMP_FILENAME\n"
+		 "to a valid filename to store the dump of EDSP solver input in.\n"
+		 "For example with: export APT_EDSP_DUMP_FILENAME=/tmp/dump.edsp", stdoutfd);
+	   return 0;
+	}
+
+	RemoveFile(argv[0], filename);
+	FileFd input, output;
+	if (input.OpenDescriptor(STDIN_FILENO, FileFd::ReadOnly) == false ||
+	      output.Open(filename, FileFd::WriteOnly | FileFd::Create | FileFd::Exclusive, FileFd::Extension, 0600) == false ||
+	      CopyFile(input, output) == false || input.Close() == false || output.Close() == false)
+	{
+	   std::ostringstream out;
+	   out << "Writing EDSP solver input to file '" << filename << "' failed!\n";
+	   _error->DumpErrors(out);
+	   EDSP::WriteError("ERR_WRITE_ERROR", out.str(), stdoutfd);
+	   return 0;
+	}
 
-	EDSP::WriteError("ERR_JUST_DUMPING", "I am too dumb, i can just dump!\nPlease use one of my friends instead!", stdout);
+	EDSP::WriteError("ERR_JUST_DUMPING", "I am too dumb, i can just dump!\nPlease use one of my friends instead!", stdoutfd);
+	return 0;
 }