pspp-dev
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH 3/5] replace_file_start: Now takes a file handle instead of a fil


From: John Darrington
Subject: [PATCH 3/5] replace_file_start: Now takes a file handle instead of a file name
Date: Thu, 8 Oct 2015 18:30:25 +0200

---
 src/data/csv-file-writer.c         |    2 +-
 src/data/make-file.c               |    7 +++++--
 src/data/make-file.h               |    4 +++-
 src/data/por-file-writer.c         |    2 +-
 src/data/sys-file-writer.c         |    3 +--
 src/language/data-io/data-writer.c |    2 +-
 6 files changed, 12 insertions(+), 8 deletions(-)

diff --git a/src/data/csv-file-writer.c b/src/data/csv-file-writer.c
index 8e52df5..99fe413 100644
--- a/src/data/csv-file-writer.c
+++ b/src/data/csv-file-writer.c
@@ -154,7 +154,7 @@ csv_writer_open (struct file_handle *fh, const struct 
dictionary *dict,
     goto error;
 
   /* Create the file on disk. */
-  w->rf = replace_file_start (fh_get_file_name (fh), "w", 0666,
+  w->rf = replace_file_start (fh, "w", 0666,
                               &w->file, NULL);
   if (w->rf == NULL)
     {
diff --git a/src/data/make-file.c b/src/data/make-file.c
index 9e84306..be1a012 100644
--- a/src/data/make-file.c
+++ b/src/data/make-file.c
@@ -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))
diff --git a/src/data/make-file.h b/src/data/make-file.h
index 3861f35..2e424fe 100644
--- a/src/data/make-file.h
+++ b/src/data/make-file.h
@@ -21,6 +21,8 @@
 #include <stdio.h>
 #include <sys/types.h>
 
+struct file_handle;
+
 /* Prepares to atomically replace a (potentially) existing file
    by a new file, by creating a temporary file with the given
    PERMISSIONS bits in the same directory as *FILE_NAME.
@@ -37,7 +39,7 @@
 
    The caller is responsible for closing *FP, but *TMP_NAME is
    owned by the callee. */
-struct replace_file *replace_file_start (const char *file_name,
+struct replace_file *replace_file_start (const struct file_handle *fh,
                                          const char *mode, mode_t permissions,
                                          FILE **fp, char **tmp_name);
 
diff --git a/src/data/por-file-writer.c b/src/data/por-file-writer.c
index 778b8e4..7a3c971 100644
--- a/src/data/por-file-writer.c
+++ b/src/data/por-file-writer.c
@@ -153,7 +153,7 @@ pfm_open_writer (struct file_handle *fh, struct dictionary 
*dict,
   mode = 0444;
   if (opts.create_writeable)
     mode |= 0222;
-  w->rf = replace_file_start (fh_get_file_name (fh), "w", mode,
+  w->rf = replace_file_start (fh, "w", mode,
                               &w->file, NULL);
   if (w->rf == NULL)
     {
diff --git a/src/data/sys-file-writer.c b/src/data/sys-file-writer.c
index e0c6ead..0f97019 100644
--- a/src/data/sys-file-writer.c
+++ b/src/data/sys-file-writer.c
@@ -251,8 +251,7 @@ sfm_open_writer (struct file_handle *fh, struct dictionary 
*d,
   mode = 0444;
   if (opts.create_writeable)
     mode |= 0222;
-  w->rf = replace_file_start (fh_get_file_name (fh), "wb", mode,
-                              &w->file, NULL);
+  w->rf = replace_file_start (fh, "wb", mode, &w->file, NULL);
   if (w->rf == NULL)
     {
       msg (ME, _("Error opening `%s' for writing as a system file: %s."),
diff --git a/src/language/data-io/data-writer.c 
b/src/language/data-io/data-writer.c
index 5f87d00..8ae7251 100644
--- a/src/language/data-io/data-writer.c
+++ b/src/language/data-io/data-writer.c
@@ -92,7 +92,7 @@ dfm_open_writer (struct file_handle *fh, const char *encoding)
   w = xmalloc (sizeof *w);
   w->fh = fh_ref (fh);
   w->lock = lock;
-  w->rf = replace_file_start (fh_get_file_name (w->fh), "wb", 0666,
+  w->rf = replace_file_start (w->fh, "wb", 0666,
                               &w->file, NULL);
   w->encoding = xstrdup (encoding);
   w->line_ends = fh_get_line_ends (fh);
-- 
1.7.10.4




reply via email to

[Prev in Thread] Current Thread [Next in Thread]