[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

gEDA-cvs: pcb.git: branch: master updated (035204bb2289055e186270937fb38b9f7880d14b)



The branch, master has been updated
       via  035204bb2289055e186270937fb38b9f7880d14b (commit)
      from  eebe6b36b19a8c0c8315fd08dbcac36a274170a4 (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.


=========
 Summary
=========

 configure.ac |    2 +
 src/action.c |  178 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 176 insertions(+), 4 deletions(-)


=================
 Commit Messages
=================

commit 035204bb2289055e186270937fb38b9f7880d14b
Author: Dan McMahill <dan@xxxxxxxxxxxx>
Commit: Dan McMahill <dan@xxxxxxxxxxxx>

    When mkdtemp() is available use it instead of tmpnam() for creating
    temp file names to be used by gnetlist or make.

:100644 100644 f8001e3... 9c14582... M	configure.ac
:100644 100644 f2b04a6... 34df05e... M	src/action.c

=========
 Changes
=========

commit 035204bb2289055e186270937fb38b9f7880d14b
Author: Dan McMahill <dan@xxxxxxxxxxxx>
Commit: Dan McMahill <dan@xxxxxxxxxxxx>

    When mkdtemp() is available use it instead of tmpnam() for creating
    temp file names to be used by gnetlist or make.

diff --git a/configure.ac b/configure.ac
index f8001e3..9c14582 100644
--- a/configure.ac
+++ b/configure.ac
@@ -621,6 +621,8 @@ AC_CHECK_FUNCS(getpwuid gethostname getcwd)
 AC_CHECK_FUNCS(random)
 AC_CHECK_FUNCS(stat)
 
+AC_CHECK_FUNCS(mkdtemp)
+
 # normally used for all file i/o
 AC_CHECK_FUNCS(popen)
 
diff --git a/src/action.c b/src/action.c
index f2b04a6..34df05e 100644
--- a/src/action.c
+++ b/src/action.c
@@ -71,6 +71,8 @@
 #include "rtree.h"
 #include "macro.h"
 
+#include <assert.h>
+
 #ifdef HAVE_LIBDMALLOC
 #include <dmalloc.h>
 #endif
@@ -7157,6 +7159,162 @@ pcb_spawnvp (char **argv)
   return 0;
 }
 
+/* 
+ * Creates a new temporary file name.  Hopefully the operating system
+ * provides a mkdtemp() function to securily create a temporary
+ * directory with mode 0700.  If so then that directory is created and
+ * the returned string is made up of the directory plus the name
+ * variable.  For example:
+ *
+ * tempfile_name_new ("myfile") might return
+ * "/var/tmp/pcb.123456/myfile".
+ *
+ * If mkdtemp() is not available then 'name' is ignored and the
+ * insecure tmpnam() function is used.
+ *  
+ * Files/names created with tempfile_name_new() should be unlinked
+ * with tempfile_unlink to make sure the temporary directory is also
+ * removed when mkdtemp() is used.
+ */
+static char *
+tempfile_name_new (char * name)
+{
+  char *tmpfile = NULL;
+#ifdef HAVE_MKDTEMP
+  char *tmpdir, *mytmpdir;
+  size_t len;
+#endif
+
+  assert ( name != NULL );
+
+#ifdef HAVE_MKDTEMP
+#define TEMPLATE "pcb.XXXXXXXX"
+    
+  
+  tmpdir = getenv ("TMPDIR");
+
+  /* FIXME -- what about win32? */
+  if (tmpdir == NULL) {
+    tmpdir = "/tmp";
+  }
+  
+  mytmpdir = (char *) malloc (sizeof(char) * 
+			      (strlen (tmpdir) + 
+			       1 +
+			       strlen (TEMPLATE) + 
+			       1));
+  if (mytmpdir == NULL) {
+    fprintf (stderr, "%s(): malloc failed()\n", __FUNCTION__);
+    exit (1);
+  }
+  
+  *mytmpdir = '\0';
+  (void)strcat (mytmpdir, tmpdir);
+  (void)strcat (mytmpdir, PCB_DIR_SEPARATOR_S);
+  (void)strcat (mytmpdir, TEMPLATE);
+  if (mkdtemp (mytmpdir) == NULL) {
+    fprintf (stderr, "%s():  mkdtemp (\"%s\") failed\n", __FUNCTION__, mytmpdir);
+    free (mytmpdir);
+    return NULL;
+  }
+
+
+  len = strlen (mytmpdir) + /* the temp directory name */
+    1 +                     /* the directory sep. */
+    strlen (name) +         /* the file name */
+    1                       /* the \0 termination */
+    ;
+
+  tmpfile = (char *) malloc (sizeof (char) * len);
+
+  *tmpfile = '\0';
+  (void)strcat (tmpfile, mytmpdir);
+  (void)strcat (tmpfile, PCB_DIR_SEPARATOR_S);
+  (void)strcat (tmpfile, name);
+  
+  free (mytmpdir);
+#undef TEMPLATE
+#else
+  /*
+   * tmpnam() uses a static buffer so strdup() the result right away
+   * in case someone decides to create multiple temp names.
+   */
+  tmpfile = strdup (tmpnam (NULL));
+#endif
+
+  return tmpfile;
+}
+
+/*
+ * Unlink a temporary file.  If we have mkdtemp() then our temp file
+ * lives in a temporary directory and we need to remove that directory
+ * too.
+ */
+static int
+tempfile_unlink (char * name)
+{
+  int rc;
+
+#ifdef HAVE_MKDTEMP
+  int e, rc2 = 0;
+  char *dname;
+
+  rc = unlink (name);
+  /* it is possible that the file was never created so it is OK if the
+     unlink fails */
+
+  /* now figure out the directory name to remove */
+  e = strlen (name) - 1;
+  while (e > 0 && name[e] != PCB_DIR_SEPARATOR_C) {e--;}
+  
+  dname = strdup (name);
+  dname[e] = '\0';
+
+  /* 
+   * at this point, e *should* point to the end of the directory part 
+   * but lets make sure.
+   */
+  if (e > 0) {
+    rc2 = rmdir (dname);
+    if (rc2 != 0) {
+      perror (dname);
+    }
+
+  } else {
+    fprintf (stderr, "%s():  Unable to determine temp directory name from the temp file\n", 
+	     __FUNCTION__);
+    fprintf (stderr, "%s():  \"%s\"\n", 
+	     __FUNCTION__, name);
+    rc2 = -1;
+  }
+
+  /* name was allocated with malloc */
+  free (dname);
+  free (name);
+
+  /*
+   * FIXME - should also return -1 if the temp file exists and was not
+   * removed.  
+   */
+  if (rc2 != 0) {
+    return -1;
+  }
+
+#else
+  rc =  unlink (name);
+
+  if (rc != 0) {
+    fprintf (stderr, "Failed to unlink \"%s\"\n", name);
+    free (name);
+    return rc;
+  }
+  free (name);
+
+#endif
+
+  return 0;
+}
+
 static int
 ActionImport (int argc, char **argv, int x, int y)
 {
@@ -7229,9 +7387,15 @@ ActionImport (int argc, char **argv, int x, int y)
 
   if (strcasecmp (mode, "gnetlist") == 0)
     {
-      char *tmpfile = tmpnam (NULL);
+      char *tmpfile = tempfile_name_new ("gnetlist_output");
       char **cmd;
       int i;
+
+      if (tmpfile == NULL) {
+	Message ("Could not create temp file");
+	return 1;
+      }
+
       cmd = (char **) malloc ((6 + nsources) * sizeof (char *));
       cmd[0] = "gnetlist";
       cmd[1] = "-g";
@@ -7253,16 +7417,22 @@ ActionImport (int argc, char **argv, int x, int y)
       ActionExecuteFile (1, cmd, 0, 0);
 
       free (cmd);
-      unlink (tmpfile);
+      tempfile_unlink (tmpfile);
     }
   else if (strcasecmp (mode, "make") == 0)
     {
-      char *tmpfile = tmpnam (NULL);
+      char *tmpfile = tempfile_name_new ("gnetlist_output");
       char **cmd;
       int i;
       char *srclist;
       int srclen;
 
+
+      if (tmpfile == NULL) {
+	Message ("Could not create temp file");
+	return 1;
+      }
+
       srclen = sizeof("SRCLIB=") + 2;
       for (i=0; i<nsources; i++)
 	srclen += strlen (sources[i]) + 2;
@@ -7304,7 +7474,7 @@ ActionImport (int argc, char **argv, int x, int y)
       free (cmd[3]);
       free (cmd[4]);
       free (cmd);
-      unlink (tmpfile);
+      tempfile_unlink (tmpfile);
     }
   else
     {




_______________________________________________
geda-cvs mailing list
geda-cvs@xxxxxxxxxxxxxx
http://www.seul.org/cgi-bin/mailman/listinfo/geda-cvs