X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Fdata%2Fsys-file-writer.c;fp=src%2Fdata%2Fsys-file-writer.c;h=21d89a0d696c991f588a5cd3877e20a47f4504a0;hb=e1fb96f07a06f3133f54702ed8706493989789fe;hp=d58fd835beaa3a8ade0d750a6909d4aed80c1d3e;hpb=a0f88151da33f2537632898156c32a7009178d50;p=pspp-builds.git diff --git a/src/data/sys-file-writer.c b/src/data/sys-file-writer.c index d58fd835..21d89a0d 100644 --- a/src/data/sys-file-writer.c +++ b/src/data/sys-file-writer.c @@ -21,12 +21,10 @@ #include #include -#include #include #include #include #include -#include #include #include @@ -42,6 +40,7 @@ #include #include #include +#include #include #include #include @@ -152,7 +151,7 @@ sfm_open_writer (struct file_handle *fh, struct dictionary *d, { struct sfm_writer *w = NULL; mode_t mode; - int fd; + FILE *file; int idx; int i; @@ -164,22 +163,27 @@ sfm_open_writer (struct file_handle *fh, struct dictionary *d, opts.version = 3; } - /* Create file. */ + /* Open file handle as an exclusive writer. */ + if (!fh_open (fh, FH_REF_FILE, "system file", "we")) + return NULL; + + /* Create the file on disk. */ mode = S_IRUSR | S_IRGRP | S_IROTH; if (opts.create_writeable) mode |= S_IWUSR | S_IWGRP | S_IWOTH; - fd = open (fh_get_file_name (fh), O_WRONLY | O_CREAT | O_TRUNC, mode); - if (fd < 0) - goto open_error; - - /* Open file handle. */ - if (!fh_open (fh, FH_REF_FILE, "system file", "we")) - goto error; + file = create_stream (fh_get_file_name (fh), "w", mode); + if (file == NULL) + { + msg (ME, _("Error opening \"%s\" for writing as a system file: %s."), + fh_get_file_name (fh), strerror (errno)); + fh_close (fh, "system file", "we"); + return NULL; + } /* Create and initialize writer. */ w = xmalloc (sizeof *w); w->fh = fh; - w->file = fdopen (fd, "w"); + w->file = file; w->compress = opts.compress; w->case_cnt = 0; @@ -193,13 +197,6 @@ sfm_open_writer (struct file_handle *fh, struct dictionary *d, w->segment_cnt = sfm_dictionary_to_sfm_vars (d, &w->sfm_vars, &w->sfm_var_cnt); - /* Check that file create succeeded. */ - if (w->file == NULL) - { - close (fd); - goto open_error; - } - /* Write the file header. */ write_header (w, d); @@ -236,19 +233,13 @@ sfm_open_writer (struct file_handle *fh, struct dictionary *d, write_int (w, 0); if (write_error (w)) - goto error; + { + close_writer (w); + return NULL; + } return casewriter_create (dict_get_next_value_idx (d), &sys_file_casewriter_class, w); - - error: - close_writer (w); - return NULL; - - open_error: - msg (ME, _("Error opening \"%s\" for writing as a system file: %s."), - fh_get_file_name (fh), strerror (errno)); - goto error; } /* Returns value of X truncated to two least-significant digits. */