RFA: pex: abstract out temp file name generation

Jim Blandy jimb@codesourcery.com
Mon Apr 10 21:58:00 GMT 2006


This is preparation for a subsequent patch implement pex_input_file
and pex_input_pipe.

src/libiberty/ChangeLog:
2006-04-10  Jim Blandy  <jimb@codesourcery.com>

	* pex-common.c (temp_file): New function, containing guts of
	pex-style temporary file name generation.
	(pex_run): Use it.

Index: src/libiberty/pex-common.c
===================================================================
--- src.orig/libiberty/pex-common.c	2006-04-10 14:32:02.765350000 -0700
+++ src/libiberty/pex-common.c	2006-04-10 14:32:30.585727000 -0700
@@ -91,6 +91,56 @@
   obj->remove[obj->remove_count - 1] = add;
 }
 
+/* Generate a temporary file name based on OBJ, FLAGS, and NAME.
+   Return NULL if we were unable to reserve a temporary filename.
+
+   If non-NULL, the result is either allocated with malloc, or the
+   same pointer as NAME.  */
+static char *
+temp_file (struct pex_obj *obj, int flags, char *name)
+{
+  if (name == NULL)
+    {
+      if (obj->tempbase == NULL)
+        {
+          name = make_temp_file (NULL);
+        }
+      else
+        {
+          int len = strlen (obj->tempbase);
+          int out;
+
+          if (len >= 6
+              && strcmp (obj->tempbase + len - 6, "XXXXXX") == 0)
+            name = xstrdup (obj->tempbase);
+          else
+            name = concat (obj->tempbase, "XXXXXX", NULL);
+
+          out = mkstemps (name, 0);
+          if (out < 0)
+            {
+              free (name);
+              return NULL;
+            }
+
+          /* This isn't obj->funcs->close because we got the
+             descriptor from mkstemps, not from a function in
+             obj->funcs.  Calling close here is just like what
+             make_temp_file does.  */
+          close (out);
+        }
+    }
+  else if ((flags & PEX_SUFFIX) != 0)
+    {
+      if (obj->tempbase == NULL)
+        name = make_temp_file (name);
+      else
+        name = concat (obj->tempbase, name, NULL);
+    }
+
+  return name;
+}
+
 /* Run a program.  */
 
 const char *
@@ -161,49 +211,16 @@
     }
   else if ((obj->flags & PEX_USE_PIPES) == 0)
     {
-      if (outname == NULL)
-	{
-	  if (obj->tempbase == NULL)
-	    {
-	      outname = make_temp_file (NULL);
-	      outname_allocated = 1;
-	    }
-	  else
-	    {
-	      int len = strlen (obj->tempbase);
-
-	      if (len >= 6
-		  && strcmp (obj->tempbase + len - 6, "XXXXXX") == 0)
-		outname = xstrdup (obj->tempbase);
-	      else
-		outname = concat (obj->tempbase, "XXXXXX", NULL);
-
-	      outname_allocated = 1;
-
-	      out = mkstemps (outname, 0);
-	      if (out < 0)
-		{
-		  *err = 0;
-		  errmsg = "could not create temporary output file";
-		  goto error_exit;
-		}
-
-	      /* This isn't obj->funcs->close because we got the
-		 descriptor from mkstemps, not from a function in
-		 obj->funcs.  Calling close here is just like what
-		 make_temp_file does.  */
-	      close (out);
-	      out = -1;
-	    }
-	}
-      else if ((flags & PEX_SUFFIX) != 0)
-	{
-	  if (obj->tempbase == NULL)
-	    outname = make_temp_file (outname);
-	  else
-	    outname = concat (obj->tempbase, outname, NULL);
-	  outname_allocated = 1;
-	}
+      outname = temp_file (obj, flags, outname);
+      if (! outname)
+        {
+          *err = 0;
+          errmsg = "could not create temporary file";
+          goto error_exit;
+        }
+
+      if (outname != orig_outname)
+        outname_allocated = 1;
 
       if ((obj->flags & PEX_SAVE_TEMPS) == 0)
 	{



More information about the Gcc-patches mailing list