[Pinfo-devel] r153 - pinfo/branches/cxx/src

Nathanael Nerode neroden-guest at costa.debian.org
Sat Sep 3 18:58:30 UTC 2005


Author: neroden-guest
Date: 2005-09-03 18:58:29 +0000 (Sat, 03 Sep 2005)
New Revision: 153

Added:
   pinfo/branches/cxx/src/tmpfiles.cxx
   pinfo/branches/cxx/src/tmpfiles.h
Modified:
   pinfo/branches/cxx/src/Makefile.am
   pinfo/branches/cxx/src/datatypes.cxx
   pinfo/branches/cxx/src/datatypes.h
   pinfo/branches/cxx/src/filehandling_functions.cxx
   pinfo/branches/cxx/src/manual.cxx
   pinfo/branches/cxx/src/pinfo.cxx
   pinfo/branches/cxx/src/utils.cxx
Log:
Overhaul temp file usage.  A lot.
Now it creates its own directory (read/write only by the user) in /tmp,
and puts all files underneath that, setting the umask so they're read/write
only by the user as well.



Modified: pinfo/branches/cxx/src/Makefile.am
===================================================================
--- pinfo/branches/cxx/src/Makefile.am	2005-09-02 12:01:08 UTC (rev 152)
+++ pinfo/branches/cxx/src/Makefile.am	2005-09-03 18:58:29 UTC (rev 153)
@@ -32,6 +32,8 @@
 		initializelinks.h \
 		printinfo.cxx \
 		printinfo.h \
+		tmpfiles.cxx \
+		tmpfiles.h \
 		localestuff.h \
 		rcsid.h
 

Modified: pinfo/branches/cxx/src/datatypes.cxx
===================================================================
--- pinfo/branches/cxx/src/datatypes.cxx	2005-09-02 12:01:08 UTC (rev 152)
+++ pinfo/branches/cxx/src/datatypes.cxx	2005-09-03 18:58:29 UTC (rev 153)
@@ -40,9 +40,6 @@
 string ignoredmacros = "";
 string rcfile = "";
 
-char *tmpfilename1 = 0;
-char *tmpfilename2 = 0;
-
 SearchAgain searchagain;
 
 vector<HyperObject> hyperobjects;

Modified: pinfo/branches/cxx/src/datatypes.h
===================================================================
--- pinfo/branches/cxx/src/datatypes.h	2005-09-02 12:01:08 UTC (rev 152)
+++ pinfo/branches/cxx/src/datatypes.h	2005-09-03 18:58:29 UTC (rev 153)
@@ -114,11 +114,6 @@
 /* a user specified rc file */
 extern std::string rcfile;
 
-/* temporary filename */
-extern char *tmpfilename1;
-/* second tmp filename--needed by regexp search, etc */
-extern char *tmpfilename2;
-
 /* a structure for "search again" feature */
 extern SearchAgain searchagain;
 

Modified: pinfo/branches/cxx/src/filehandling_functions.cxx
===================================================================
--- pinfo/branches/cxx/src/filehandling_functions.cxx	2005-09-02 12:01:08 UTC (rev 152)
+++ pinfo/branches/cxx/src/filehandling_functions.cxx	2005-09-03 18:58:29 UTC (rev 153)
@@ -22,6 +22,7 @@
 
 #include "common_includes.h"
 #include "utils.h"
+#include "tmpfiles.h"
 #include <string>
 using std::string;
 #include <vector>
@@ -534,19 +535,17 @@
 opendirfile(int number)
 {
 	FILE *id = NULL;
-	char *tmpfilename;
+	string tmpfilename;
 	int dir_found = 0;
 	int dircount = 0;
 	struct stat status;
 
 	if (number == 0)		/* initialize tmp filename for file 1 */
 	{
-		if (tmpfilename1)
+		if (tmpfilename1 != "")
 		{
-			unlink(tmpfilename1);	/* erase old tmpfile */
-			free(tmpfilename1);
+			unlink(tmpfilename1.c_str());	/* erase old tmpfile */
 		}
-		tmpfilename1 = tempnam("/tmp", NULL);
 		tmpfilename = tmpfilename1;	/* later we will refere only to tmp1 */
 	}
 
@@ -580,14 +579,13 @@
 				id = fopen(bufstr_with_suffix.c_str(), "r");
 				if (id != NULL) {
 					fclose(id);
-					/* FIXME: Insecure temp file usage */
 					string command_string = suffixes[j].command;
 					command_string += " ";
 					command_string += bufstr_with_suffix;
 					command_string += ">> ";
 					command_string += tmpfilename;
 					system(command_string.c_str());
-					lstat(tmpfilename, &status);
+					lstat(tmpfilename.c_str(), &status);
 					fileendentries[dircount] = status.st_size;
 					dircount++;
 					dir_found = 1;
@@ -597,7 +595,7 @@
 		}
 	}
 	if (dir_found)
-		id = fopen(tmpfilename, "r");
+		id = fopen(tmpfilename.c_str(), "r");
 	/*
 	 * Filter the concatenated dir pages to exclude hidden parts of info
 	 * entries
@@ -617,7 +615,7 @@
 		fseek(id, 0, SEEK_SET);
 		fread(tmp, 1, filelen, id);
 		fclose(id);
-		id = fopen(tmpfilename, "w");
+		id = fopen(tmpfilename.c_str(), "w");
 		for (i = 0; i < filelen; i++)
 		{
 			if (tmp[i] == INFO_TAG)
@@ -639,7 +637,7 @@
 		fputc(INFO_TAG, id);
 		fputc('\n', id);
 		fclose(id);
-		id = fopen(tmpfilename, "r");
+		id = fopen(tmpfilename.c_str(), "r");
 		xfree(tmp);
 
 		xfree(fileendentries);
@@ -662,31 +660,24 @@
 openinfo(const string filename, int number)
 {
 	FILE *id = NULL;
-	char *tmpfilename;
+	string tmpfilename;
 
 	if (filename == "dir")
 	{
 		return opendirfile(number);
 	}
 
-	if (number == 0)		/* initialize tmp filename for file 1 */
-	{
-		if (tmpfilename1)
+	if (number == 0) { /* initialize tmp filename for file 1 */
+		if (tmpfilename1 != "")
 		{
-			unlink(tmpfilename1);	/* erase old tmpfile */
-			free(tmpfilename1);
+			unlink(tmpfilename1.c_str());	/* erase old tmpfile */
 		}
-		tmpfilename1 = tempnam("/tmp", NULL);
 		tmpfilename = tmpfilename1;	/* later we will refere only to tmp1 */
-	}
-	else /* initialize tmp filename for file 2 */
-	{
-		if (tmpfilename2)
+	} else { /* initialize tmp filename for file 2 */
+		if (tmpfilename2 != "")
 		{
-			unlink(tmpfilename2);	/* erase old tmpfile */
-			free(tmpfilename2);
+			unlink(tmpfilename2.c_str());	/* erase old tmpfile */
 		}
-		tmpfilename2 = tempnam("/tmp", NULL);
 		tmpfilename = tmpfilename2;	/* later we will refere only to tmp2 */
 	}
 
@@ -723,7 +714,6 @@
 				/* Set global filenameprefix to the dirname of the found file */
 				dirname(buf_with_suffix, filenameprefix);
 
-				/* FIXME: Insecure temp file usage */
 				string command_string = suffixes[j].command;
 				command_string += ' ';
 				command_string += buf_with_suffix;
@@ -731,7 +721,7 @@
 				command_string += tmpfilename;
 				system(command_string.c_str());
 
-				id = fopen(tmpfilename, "r");
+				id = fopen(tmpfilename.c_str(), "r");
 				if (id)
 				{
 					return id;

Modified: pinfo/branches/cxx/src/manual.cxx
===================================================================
--- pinfo/branches/cxx/src/manual.cxx	2005-09-02 12:01:08 UTC (rev 152)
+++ pinfo/branches/cxx/src/manual.cxx	2005-09-03 18:58:29 UTC (rev 153)
@@ -20,6 +20,7 @@
  *  USA
  ***************************************************************************/
 #include "common_includes.h"
+#include "tmpfiles.h"
 
 RCSID("$Id$")
 
@@ -246,15 +247,11 @@
 	FILE *id;
 
 	string manualname_string; /* Filled by construct_manualname */
-	char *raw_tempfilename = 0;
-	char *apropos_tempfilename = 0;
 
-	if (tmpfilename1)
+	if (tmpfilename1 != "")
 	{
-		unlink(tmpfilename1);
-		xfree(tmpfilename1);
+		unlink(tmpfilename1.c_str());
 	}
-	tmpfilename1 = tempnam("/tmp", NULL);
 
 #ifdef getmaxyx
 	init_curses();
@@ -288,11 +285,11 @@
 		int cmd_result;
 		cmd_result = system(cmd_string.c_str());
 		if (cmd_result != 0) {
-			unlink(tmpfilename1);
+			unlink(tmpfilename1.c_str());
 			printf(_("Error: No manual page found\n"));
 			plain_apropos = 1; /* Fallback */
 		} else {
-			id = fopen(tmpfilename1, "r");
+			id = fopen(tmpfilename1.c_str(), "r");
 		}
 	}
 	if (plain_apropos) {
@@ -301,17 +298,18 @@
 			return 1;
 		}
 		printf(_("Calling apropos \n"));
-		apropos_tempfilename = tempnam("/tmp", NULL);
+		apropos_tmpfilename = tmpdirname;
+		apropos_tmpfilename += "/apropos_result";
 		string cmd_string = "apropos ";
 		cmd_string += name;
 		cmd_string += " > ";
-		cmd_string += apropos_tempfilename;
+		cmd_string += apropos_tmpfilename;
 		if (system(cmd_string.c_str()) != 0) {
 			printf(_("Nothing appropriate\n"));
-			unlink(apropos_tempfilename);
+			unlink(apropos_tmpfilename.c_str());
 			return 1;
 		} else {
-			id = fopen(apropos_tempfilename, "r");
+			id = fopen(apropos_tmpfilename.c_str(), "r");
 		}
 	}
 
@@ -341,12 +339,10 @@
 		/* -1 is quit key */
 		if (return_value != -1)
 		{
-			if (tmpfilename2)
+			if (tmpfilename2 != "")
 			{
-				unlink(tmpfilename2);
-				xfree(tmpfilename2);
+				unlink(tmpfilename2.c_str());
 			}
-			tmpfilename2 = tempnam("/tmp", NULL);
 
 			bool historical = false;
 			string cmd_string = "man ";
@@ -354,9 +350,9 @@
 			cmd_string += " ";
 			if (return_value == -2) {
 				/* key_back was pressed */
-				if ( (manualhistory.size() - 2) == 0 && apropos_tempfilename)
+				if ( (manualhistory.size() - 2) == 0 && apropos_tmpfilename != "")
 				{
-					id = fopen(apropos_tempfilename, "r");
+					id = fopen(apropos_tmpfilename.c_str(), "r");
 					loadmanual(id);
 					fclose(id);
 					continue;
@@ -385,7 +381,7 @@
 			cmd_string += " > ";
 			cmd_string += tmpfilename2;
 			system(cmd_string.c_str());
-			stat(tmpfilename2, &statbuf);
+			stat(tmpfilename2.c_str(), &statbuf);
 			if (statbuf.st_size > 0) {
 				string cmd_string = "mv ";
 				cmd_string += tmpfilename2;
@@ -394,7 +390,7 @@
 				/* create tmp file containing man page */
 				system(cmd_string.c_str());
 				/* open man page */
-				id = fopen(tmpfilename1, "r");
+				id = fopen(tmpfilename1.c_str(), "r");
 				if (id != NULL) {
 					manhistory my_hist;
 					/* now we create history entry for new page */
@@ -419,11 +415,8 @@
 			}
 		}
 	} while (return_value != -1);
-	if (apropos_tempfilename)
-		unlink(apropos_tempfilename);
-	/* we were using temporary */
-	if (raw_tempfilename)
-		unlink(raw_tempfilename);
+	if (apropos_tmpfilename != "")
+		unlink(apropos_tmpfilename.c_str());
 	/* raw-manpage for scanning */
 	return 0;
 }

Modified: pinfo/branches/cxx/src/pinfo.cxx
===================================================================
--- pinfo/branches/cxx/src/pinfo.cxx	2005-09-02 12:01:08 UTC (rev 152)
+++ pinfo/branches/cxx/src/pinfo.cxx	2005-09-03 18:58:29 UTC (rev 153)
@@ -23,6 +23,7 @@
 #include "common_includes.h"
 #include "signal_handler.h"
 #include "utils.h"
+#include "tmpfiles.h"
 
 #include <string>
 using std::string;
@@ -205,6 +206,12 @@
 
 	/* take care of SIGSEGV, SIGTERM, SIGINT */
 	install_signal_handlers();
+
+	/* Open the temporary dir right after installing the signal handlers
+	 * (which delete it on control-c)
+	 */
+	mktmpdir();
+
 	searchagain.type = 0;
 	searchagain.search = 0;
 	initlocale();

Added: pinfo/branches/cxx/src/tmpfiles.cxx
===================================================================
--- pinfo/branches/cxx/src/tmpfiles.cxx	2005-09-02 12:01:08 UTC (rev 152)
+++ pinfo/branches/cxx/src/tmpfiles.cxx	2005-09-03 18:58:29 UTC (rev 153)
@@ -0,0 +1,103 @@
+/***************************************************************************
+ *  Pinfo is a ncurses based lynx style info documentation browser
+ *
+ *  Copyright 2005  Nathanael Nerode <neroden at gcc.gnu.org>
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of version 2 of the GNU General Public License as
+ *  published by the Free Software Foundation.
+ *
+ *  This program is distributed in the hope that it will be useful, but
+ *  WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ *  General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
+ *  USA
+ ***************************************************************************/
+
+#include <string>
+using std::string;
+
+#include <iostream>
+using std::cerr;
+#include <sstream>
+using std::ostringstream;
+
+
+/* For mkdir, umask */
+#include <sys/stat.h>
+
+/* For getpid, mkdir, umask */
+#include <sys/types.h>
+
+/* For getpid */
+#include <unistd.h>
+
+/* For errno */
+#include <errno.h>
+
+/* For strerror */
+#include <string.h>
+
+string tmpdirname;
+string tmpfilename1;
+string tmpfilename2;
+string apropos_tmpfilename;
+
+int mktmpdir () {
+	ostringstream tmpdirname_stream;
+	tmpdirname_stream << "/tmp/pinfo";
+	pid_t my_pid = getpid();
+	tmpdirname_stream << my_pid;
+	tmpdirname = tmpdirname_stream.str();
+	if (tmpdirname == "/tmp/pinfo") {
+		cerr << "Unexpected error: Couldn't get temporary directory name.\n";
+		exit(1);
+	}
+
+	int result;
+	result = mkdir(tmpdirname.c_str(), 0700);
+	if (result != 0) {
+		int errval = errno;
+		cerr << "Error: couldn't make temporary directory " << tmpdirname
+		     << " (" << strerror(errval) << ")\n";
+		exit(1);
+	}
+
+	tmpfilename1 = tmpdirname;
+	tmpfilename1 += "/current-file";
+	tmpfilename2 = tmpdirname;
+	tmpfilename2 += "/cross-file";
+
+	/* FIXME: This belongs elsewhere. */
+	/* Most of our temp files are created by calls to system().
+	 * We need to set the umask so that it will be inherited.
+	 */
+	umask(077);
+}
+
+int rmtmpfiles () {
+	int result;
+
+	result = unlink(tmpfilename1.c_str());
+	/* It might not exist; this is sloppy.  FIXME later. */
+
+	result = unlink(tmpfilename2.c_str());
+	/* It might not exist; this is sloppy.  FIXME later. */
+
+	if (apropos_tmpfilename != "") {
+		result = unlink(apropos_tmpfilename.c_str());
+		/* It might not exist; this is sloppy.  FIXME later. */
+	}
+
+	result = rmdir(tmpdirname.c_str());
+	if (result != 0) {
+		int errval = errno;
+		cerr << "Error: couldn't remove temporary directory " << tmpdirname
+		     << " (" << strerror(errval) << ")\n";
+	}
+	tmpdirname = "";
+}

Added: pinfo/branches/cxx/src/tmpfiles.h
===================================================================
--- pinfo/branches/cxx/src/tmpfiles.h	2005-09-02 12:01:08 UTC (rev 152)
+++ pinfo/branches/cxx/src/tmpfiles.h	2005-09-03 18:58:29 UTC (rev 153)
@@ -0,0 +1,34 @@
+/***************************************************************************
+ *  Pinfo is a ncurses based lynx style info documentation browser
+ *
+ *  Copyright 2005  Nathanael Nerode <neroden at gcc.gnu.org>
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of version 2 of the GNU General Public License as
+ *  published by the Free Software Foundation.
+ *
+ *  This program is distributed in the hope that it will be useful, but
+ *  WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ *  General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
+ *  USA
+ ***************************************************************************/
+
+#ifndef __TMPFILES_H
+#define __TMPFILES_H
+
+#include <string>
+
+extern std::string tmpdirname;
+extern std::string tmpfilename1;
+extern std::string tmpfilename2;
+extern std::string apropos_tmpfilename;
+
+int mktmpdir(void);
+int rmtmpfiles(void);
+
+#endif /* _TMPFILES_H */

Modified: pinfo/branches/cxx/src/utils.cxx
===================================================================
--- pinfo/branches/cxx/src/utils.cxx	2005-09-02 12:01:08 UTC (rev 152)
+++ pinfo/branches/cxx/src/utils.cxx	2005-09-03 18:58:29 UTC (rev 153)
@@ -21,6 +21,7 @@
  ***************************************************************************/
 
 #include "common_includes.h"
+#include "tmpfiles.h"
 #include <string>
 using std::string;
 #include <vector>
@@ -297,16 +298,7 @@
 		system("clear");
 	else
 		printf("\n");
-	if (tmpfilename1)
-	{
-		unlink(tmpfilename1);
-		xfree(tmpfilename1);
-	}
-	if (tmpfilename2)
-	{
-		unlink(tmpfilename2);
-		xfree(tmpfilename2);
-	}
+	rmtmpfiles();
 }
 
 /*




More information about the Pinfo-devel mailing list