Patch #6258.
[pspp-builds.git] / src / language / data-io / data-writer.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-2004, 2006 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
16
17 #include <config.h>
18
19 #include <language/data-io/data-writer.h>
20
21 #include <assert.h>
22 #include <errno.h>
23 #include <stdlib.h>
24 #include <sys/stat.h>
25
26 #include <data/file-name.h>
27 #include <data/make-file.h>
28 #include <language/data-io/file-handle.h>
29 #include <libpspp/assertion.h>
30 #include <libpspp/message.h>
31 #include <libpspp/str.h>
32
33 #include "minmax.h"
34 #include "xalloc.h"
35
36 #include "gettext.h"
37 #define _(msgid) gettext (msgid)
38 #define N_(msgid) (msgid)
39
40 /* Data file writer. */
41 struct dfm_writer
42   {
43     struct file_handle *fh;     /* File handle. */
44     struct fh_lock *lock;       /* Exclusive access to file. */
45     FILE *file;                 /* Associated file. */
46     struct replace_file *rf;    /* Atomic file replacement support. */
47   };
48
49 /* Opens a file handle for writing as a data file. */
50 struct dfm_writer *
51 dfm_open_writer (struct file_handle *fh)
52 {
53   struct dfm_writer *w;
54   struct fh_lock *lock;
55
56   lock = fh_lock (fh, FH_REF_FILE, N_("data file"), FH_ACC_WRITE, false);
57   if (lock == NULL)
58     return NULL;
59
60   w = fh_lock_get_aux (lock);
61   if (w != NULL)
62     return w;
63
64   w = xmalloc (sizeof *w);
65   w->fh = fh_ref (fh);
66   w->lock = lock;
67   w->rf = replace_file_start (fh_get_file_name (w->fh), "wb",
68                               (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP
69                                | S_IROTH | S_IWOTH), &w->file, NULL);
70   if (w->rf == NULL)
71     {
72       msg (ME, _("An error occurred while opening \"%s\" for writing "
73                  "as a data file: %s."),
74            fh_get_file_name (w->fh), strerror (errno));
75       dfm_close_writer (w);
76       return NULL;
77     }
78   fh_lock_set_aux (lock, w);
79
80   return w;
81 }
82
83 /* Returns false if an I/O error occurred on WRITER, true otherwise. */
84 bool
85 dfm_write_error (const struct dfm_writer *writer)
86 {
87   return ferror (writer->file);
88 }
89
90 /* Writes record REC (which need not be null-terminated) having
91    length LEN to the file corresponding to HANDLE.  Adds any
92    needed formatting, such as a trailing new-line.  Returns true
93    on success, false on failure. */
94 bool
95 dfm_put_record (struct dfm_writer *w, const char *rec, size_t len)
96 {
97   assert (w != NULL);
98
99   if (dfm_write_error (w))
100     return false;
101
102   switch (fh_get_mode (w->fh))
103     {
104     case FH_MODE_TEXT:
105       fwrite (rec, len, 1, w->file);
106       putc ('\n', w->file);
107       break;
108
109     case FH_MODE_BINARY:
110       {
111         size_t record_width = fh_get_record_width (w->fh);
112         size_t write_bytes = MIN (len, record_width);
113         size_t pad_bytes = record_width - write_bytes;
114         fwrite (rec, write_bytes, 1, w->file);
115         while (pad_bytes > 0)
116           {
117             static const char spaces[32] = "                                ";
118             size_t chunk = MIN (pad_bytes, sizeof spaces);
119             fwrite (spaces, chunk, 1, w->file);
120             pad_bytes -= chunk;
121           }
122       }
123       break;
124
125     default:
126       NOT_REACHED ();
127     }
128
129   return !dfm_write_error (w);
130 }
131
132 /* Closes data file writer W. */
133 bool
134 dfm_close_writer (struct dfm_writer *w)
135 {
136   bool ok;
137
138   if (w == NULL)
139     return true;
140   if (fh_unlock (w->lock))
141     return true;
142
143   ok = true;
144   if (w->file != NULL)
145     {
146       const char *file_name = fh_get_file_name (w->fh);
147       ok = !dfm_write_error (w) && !fn_close (file_name, w->file);
148
149       if (!ok)
150         msg (ME, _("I/O error occurred writing data file \"%s\"."), file_name);
151
152       if (ok ? !replace_file_commit (w->rf) : !replace_file_abort (w->rf))
153         ok = false;
154     }
155   fh_unref (w->fh);
156   free (w);
157
158   return ok;
159 }