1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2004, 2010 Free Software Foundation, Inc.
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.
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.
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/>. */
19 #include "data/make-file.h"
30 #include "data/file-name.h"
31 #include "data/file-handle-def.h"
32 #include "libpspp/ll.h"
33 #include "libpspp/message.h"
35 #include "gl/fatal-signal.h"
36 #include "gl/tempname.h"
37 #include "gl/xalloc.h"
38 #include "gl/xvasprintf.h"
41 #define _(msgid) gettext (msgid)
50 static struct ll_list all_files = LL_INITIALIZER (all_files);
52 static void free_replace_file (struct replace_file *);
53 static void unlink_replace_files (void);
56 replace_file_start (const struct file_handle *fh, const char *mode,
57 mode_t permissions, FILE **fp, char **tmp_name)
59 static bool registered;
61 struct replace_file *rf;
63 int saved_errno = errno;
65 const char *file_name = fh_get_file_name (fh);
67 /* If FILE_NAME represents a special file, write to it directly
68 instead of trying to replace it. */
69 if (stat (file_name, &s) == 0 && !S_ISREG (s.st_mode))
71 /* Open file descriptor. */
72 fd = open (file_name, O_WRONLY);
76 msg (ME, _("Opening %s for writing: %s."),
77 file_name, strerror (saved_errno));
81 /* Open file as stream. */
82 *fp = fdopen (fd, mode);
86 msg (ME, _("Opening stream for %s: %s."),
87 file_name, strerror (saved_errno));
92 rf = xmalloc (sizeof *rf);
94 rf->tmp_name = xstrdup (file_name);
96 *tmp_name = rf->tmp_name;
102 at_fatal_signal (unlink_replace_files);
105 block_fatal_signals ();
107 rf = xmalloc (sizeof *rf);
108 rf->file_name = xstrdup (file_name);
111 /* Generate unique temporary file name. */
112 rf->tmp_name = xasprintf ("%s.tmpXXXXXX", file_name);
113 if (gen_tempname (rf->tmp_name, 0, 0600, GT_NOCREATE) < 0)
116 msg (ME, _("Creating temporary file to replace %s: %s."),
117 rf->file_name, strerror (saved_errno));
121 /* Create file by that name. */
122 fd = open (rf->tmp_name, O_WRONLY | O_CREAT | O_EXCL | O_BINARY, permissions);
128 msg (ME, _("Creating temporary file %s: %s."),
129 rf->tmp_name, strerror (saved_errno));
136 /* Open file as stream. */
137 *fp = fdopen (fd, mode);
141 msg (ME, _("Opening stream for temporary file %s: %s."),
142 rf->tmp_name, strerror (saved_errno));
144 unlink (rf->tmp_name);
148 /* Register file for deletion. */
149 ll_push_head (&all_files, &rf->ll);
150 unblock_fatal_signals ();
152 if (tmp_name != NULL)
153 *tmp_name = rf->tmp_name;
158 unblock_fatal_signals ();
159 free_replace_file (rf);
161 if (tmp_name != NULL)
168 replace_file_commit (struct replace_file *rf)
172 if (rf->file_name != NULL)
176 block_fatal_signals ();
177 ok = rename (rf->tmp_name, rf->file_name) == 0;
180 unblock_fatal_signals ();
183 msg (ME, _("Replacing %s by %s: %s."),
184 rf->tmp_name, rf->file_name, strerror (save_errno));
188 /* Special file: no temporary file to rename. */
190 free_replace_file (rf);
196 replace_file_abort (struct replace_file *rf)
200 if (rf->file_name != NULL)
204 block_fatal_signals ();
205 ok = unlink (rf->tmp_name) == 0;
208 unblock_fatal_signals ();
211 msg (ME, _("Removing %s: %s."), rf->tmp_name, strerror (save_errno));
215 /* Special file: no temporary file to unlink. */
217 free_replace_file (rf);
223 free_replace_file (struct replace_file *rf)
225 free (rf->file_name);
231 unlink_replace_files (void)
233 struct replace_file *rf;
235 block_fatal_signals ();
236 ll_for_each (rf, struct replace_file, ll, &all_files)
238 /* We don't free_replace_file(RF) because calling free is unsafe
239 from an asynchronous signal handler. */
240 unlink (rf->tmp_name);
242 unblock_fatal_signals ();