From 40214686362e348692b4b6bd05ed5522189525c0 Mon Sep 17 00:00:00 2001 From: John Darrington Date: Wed, 7 Oct 2015 20:22:25 +0200 Subject: [PATCH] replace_file_start: Now takes a file handle instead of a file name --- 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 8e52df5075..99fe413510 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 9e84306cfb..be1a012749 100644 --- a/src/data/make-file.c +++ b/src/data/make-file.c @@ -28,6 +28,7 @@ #include #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 3861f35109..2e424fefdf 100644 --- a/src/data/make-file.h +++ b/src/data/make-file.h @@ -21,6 +21,8 @@ #include #include +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 778b8e402f..7a3c971dec 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 e0c6eade4b..0f970190d4 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 5f87d0060a..8ae7251339 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); -- 2.30.2