Change pattern of tmpfiles from <name>.tmpXXXXXX to <name>tmpXXXXXX
[pspp] / src / data / make-file.c
index 9e84306cfb04a36d6482a3f740a03290ac9cd960..c8f3238a80cdc2f7a7c5d9479cc72c2bbf191df5 100644 (file)
@@ -17,6 +17,7 @@
 #include <config.h>
 
 #include "data/make-file.h"
+#include "libpspp/i18n.h"
 
 #include <assert.h>
 #include <errno.h>
@@ -28,6 +29,7 @@
 #include <unistd.h>
 
 #include "data/file-name.h"
+#include "data/file-handle-def.h"
 #include "libpspp/ll.h"
 #include "libpspp/message.h"
 
@@ -45,15 +47,15 @@ struct replace_file
     char *file_name;
     char *tmp_name;
   };
-
 static struct ll_list all_files = LL_INITIALIZER (all_files);
 
 static void free_replace_file (struct replace_file *);
 static void unlink_replace_files (void);
 
 struct replace_file *
-replace_file_start (const char *file_name, const char *mode,
-                    mode_t permissions, FILE **fp, char **tmp_name)
+replace_file_start (const struct file_handle *fh, const char *mode,
+                    mode_t permissions, FILE **fp)
 {
   static bool registered;
   struct stat s;
@@ -61,6 +63,8 @@ replace_file_start (const char *file_name, const char *mode,
   int fd;
   int saved_errno = errno;
 
+  const char *file_name = fh_get_file_name (fh);
+
   /* If FILE_NAME represents a special file, write to it directly
      instead of trying to replace it. */
   if (stat (file_name, &s) == 0 && !S_ISREG (s.st_mode))
@@ -89,8 +93,6 @@ replace_file_start (const char *file_name, const char *mode,
       rf = xmalloc (sizeof *rf);
       rf->file_name = NULL;
       rf->tmp_name = xstrdup (file_name);
-      if (tmp_name != NULL)
-        *tmp_name = rf->tmp_name;
       return rf;
     }
 
@@ -106,7 +108,7 @@ replace_file_start (const char *file_name, const char *mode,
   for (;;)
     {
       /* Generate unique temporary file name. */
-      rf->tmp_name = xasprintf ("%s.tmpXXXXXX", file_name);
+      rf->tmp_name = xasprintf ("%stmpXXXXXX", file_name);
       if (gen_tempname (rf->tmp_name, 0, 0600, GT_NOCREATE) < 0)
         {
          saved_errno = errno;
@@ -146,17 +148,12 @@ replace_file_start (const char *file_name, const char *mode,
   ll_push_head (&all_files, &rf->ll);
   unblock_fatal_signals ();
 
-  if (tmp_name != NULL)
-    *tmp_name = rf->tmp_name;
-
   return rf;
 
 error:
   unblock_fatal_signals ();
   free_replace_file (rf);
   *fp = NULL;
-  if (tmp_name != NULL)
-    *tmp_name = NULL;
   errno = saved_errno;
   return NULL;
 }