replace_file_start: Now takes a file handle instead of a file name
[pspp] / src / data / make-file.c
index e4520f411eba6c3c897317117a5801f2b5014065..be1a0127493586d42262c022d705c0c2e2657d2d 100644 (file)
@@ -28,6 +28,7 @@
 #include <unistd.h>
 
 #include "data/file-name.h"
+#include "data/file-handle-def.h"
 #include "libpspp/ll.h"
 #include "libpspp/message.h"
 
@@ -45,14 +46,14 @@ 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,
+replace_file_start (const struct file_handle *fh, const char *mode,
                     mode_t permissions, FILE **fp, char **tmp_name)
 {
   static bool registered;
@@ -61,6 +62,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))
@@ -72,7 +75,6 @@ replace_file_start (const char *file_name, const char *mode,
          saved_errno = errno;     
           msg (ME, _("Opening %s for writing: %s."),
                file_name, strerror (saved_errno));
-         errno = saved_errno;
           return NULL;
         }
 
@@ -84,7 +86,6 @@ replace_file_start (const char *file_name, const char *mode,
          msg (ME, _("Opening stream for %s: %s."),
                file_name, strerror (saved_errno));
           close (fd);
-         errno = saved_errno;
           return NULL;
         }
 
@@ -111,9 +112,9 @@ replace_file_start (const char *file_name, const char *mode,
       rf->tmp_name = xasprintf ("%s.tmpXXXXXX", file_name);
       if (gen_tempname (rf->tmp_name, 0, 0600, GT_NOCREATE) < 0)
         {
-          msg (ME, _("Creating temporary file to replace %s: %s."),
-               rf->file_name, strerror (errno));
          saved_errno = errno;
+          msg (ME, _("Creating temporary file to replace %s: %s."),
+               rf->file_name, strerror (saved_errno));
           goto error;
         }
 
@@ -123,9 +124,9 @@ replace_file_start (const char *file_name, const char *mode,
         break;
       if (errno != EEXIST)
         {
-          msg (ME, _("Creating temporary file %s: %s."),
-               rf->tmp_name, strerror (errno));
          saved_errno = errno;
+          msg (ME, _("Creating temporary file %s: %s."),
+               rf->tmp_name, strerror (saved_errno));
           goto error;
         }
       free (rf->tmp_name);
@@ -136,11 +137,11 @@ replace_file_start (const char *file_name, const char *mode,
   *fp = fdopen (fd, mode);
   if (*fp == NULL)
     {
+      saved_errno = errno;
       msg (ME, _("Opening stream for temporary file %s: %s."),
-           rf->tmp_name, strerror (errno));
+           rf->tmp_name, strerror (saved_errno));
       close (fd);
       unlink (rf->tmp_name);
-      saved_errno = errno;
       goto error;
     }