X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Fdata%2Fpor-file-writer.c;h=fad89bf7682cbf77a0b7f6a8b270a4ee64c98ede;hb=51d8c9b54d65bd0aa3944b8fb9d4460875048e14;hp=f3a1101cf9801aeae0d90c7d8c36f0f33b178066;hpb=e1fb96f07a06f3133f54702ed8706493989789fe;p=pspp-builds.git diff --git a/src/data/por-file-writer.c b/src/data/por-file-writer.c index f3a1101c..fad89bf7 100644 --- a/src/data/por-file-writer.c +++ b/src/data/por-file-writer.c @@ -33,21 +33,23 @@ #include #include #include +#include #include #include #include #include -#include #include -#include #include #include #include #include +#include "xalloc.h" + #include "gettext.h" #define _(msgid) gettext (msgid) +#define N_(msgid) (msgid) /* Maximum width of a variable in a portable file. */ #define MAX_POR_WIDTH 255 @@ -56,7 +58,9 @@ struct pfm_writer { struct file_handle *fh; /* File handle. */ + struct fh_lock *lock; /* Lock on file handle. */ FILE *file; /* File stream. */ + struct replace_file *rf; /* Ticket for replacing output file. */ int lc; /* Number of characters on this line so far. */ @@ -108,32 +112,14 @@ pfm_open_writer (struct file_handle *fh, struct dictionary *dict, { struct pfm_writer *w = NULL; mode_t mode; - FILE *file; size_t i; - /* Open file handle. */ - if (!fh_open (fh, FH_REF_FILE, "portable file", "we")) - return NULL; - - /* Create file. */ - mode = S_IRUSR | S_IRGRP | S_IROTH; - if (opts.create_writeable) - mode |= S_IWUSR | S_IWGRP | S_IWOTH; - file = create_stream (fh_get_file_name (fh), "w", mode); - if (file == NULL) - { - fh_close (fh, "portable file", "we"); - msg (ME, _("An error occurred while opening \"%s\" for writing " - "as a portable file: %s."), - fh_get_file_name (fh), strerror (errno)); - return NULL; - } - /* Initialize data structures. */ w = xmalloc (sizeof *w); - w->fh = fh; - w->file = file; - + w->fh = fh_ref (fh); + w->lock = NULL; + w->file = NULL; + w->rf = NULL; w->lc = 0; w->var_cnt = 0; w->vars = NULL; @@ -156,6 +142,26 @@ pfm_open_writer (struct file_handle *fh, struct dictionary *dict, w->digits = DBL_DIG; } + /* Lock file. */ + /* TRANSLATORS: this fragment will be interpolated into + messages in fh_lock() that identify types of files. */ + w->lock = fh_lock (fh, FH_REF_FILE, N_("portable file"), FH_ACC_WRITE, true); + if (w->lock == NULL) + goto error; + + /* Create file. */ + mode = S_IRUSR | S_IRGRP | S_IROTH; + if (opts.create_writeable) + mode |= S_IWUSR | S_IWGRP | S_IWOTH; + w->rf = replace_file_start (fh_get_file_name (fh), "w", mode, + &w->file, NULL); + if (w->rf == NULL) + { + msg (ME, _("Error opening \"%s\" for writing as a portable file: %s."), + fh_get_file_name (fh), strerror (errno)); + goto error; + } + /* Write file header. */ write_header (w); write_version_data (w); @@ -165,12 +171,13 @@ pfm_open_writer (struct file_handle *fh, struct dictionary *dict, write_documents (w, dict); buf_write (w, "F", 1); if (ferror (w->file)) - { - close_writer (w); - return NULL; - } + goto error; return casewriter_create (dict_get_next_value_idx (dict), &por_file_casewriter_class, w); + +error: + close_writer (w); + return NULL; } /* Write NBYTES starting at BUF to the portable file represented by @@ -491,9 +498,13 @@ close_writer (struct pfm_writer *w) if (!ok) msg (ME, _("An I/O error occurred writing portable file \"%s\"."), fh_get_file_name (w->fh)); + + if (ok ? !replace_file_commit (w->rf) : !replace_file_abort (w->rf)) + ok = false; } - fh_close (w->fh, "portable file", "we"); + fh_unlock (w->lock); + fh_unref (w->fh); free (w->vars); free (w);