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"
20 #include "libpspp/i18n.h"
31 #include "data/file-name.h"
32 #include "data/file-handle-def.h"
33 #include "libpspp/ll.h"
34 #include "libpspp/message.h"
36 #include "gl/fatal-signal.h"
37 #include "gl/tempname.h"
38 #include "gl/xalloc.h"
39 #include "gl/xvasprintf.h"
42 #define _(msgid) gettext (msgid)
51 static struct ll_list all_files = LL_INITIALIZER (all_files);
53 static void free_replace_file (struct replace_file *);
54 static void unlink_replace_files (void);
57 replace_file_start (const struct file_handle *fh, const char *mode,
58 mode_t permissions, FILE **fp)
60 static bool registered;
62 struct replace_file *rf;
64 int saved_errno = errno;
66 const char *file_name = fh_get_file_name (fh);
68 /* If FILE_NAME represents a special file, write to it directly
69 instead of trying to replace it. */
70 if (stat (file_name, &s) == 0 && !S_ISREG (s.st_mode))
72 /* Open file descriptor. */
73 fd = open (file_name, O_WRONLY);
77 msg (ME, _("Opening %s for writing: %s."),
78 file_name, strerror (saved_errno));
82 /* Open file as stream. */
83 *fp = fdopen (fd, mode);
87 msg (ME, _("Opening stream for %s: %s."),
88 file_name, strerror (saved_errno));
93 rf = xmalloc (sizeof *rf);
95 rf->tmp_name = xstrdup (file_name);
101 at_fatal_signal (unlink_replace_files);
104 block_fatal_signals ();
106 rf = xmalloc (sizeof *rf);
107 rf->file_name = xstrdup (file_name);
110 /* Generate unique temporary file name. */
111 rf->tmp_name = xasprintf ("%stmpXXXXXX", file_name);
112 if (gen_tempname (rf->tmp_name, 0, 0600, GT_NOCREATE) < 0)
115 msg (ME, _("Creating temporary file to replace %s: %s."),
116 rf->file_name, strerror (saved_errno));
120 /* Create file by that name. */
121 fd = open (rf->tmp_name, O_WRONLY | O_CREAT | O_EXCL | O_BINARY, permissions);
127 msg (ME, _("Creating temporary file %s: %s."),
128 rf->tmp_name, strerror (saved_errno));
135 /* Open file as stream. */
136 *fp = fdopen (fd, mode);
140 msg (ME, _("Opening stream for temporary file %s: %s."),
141 rf->tmp_name, strerror (saved_errno));
143 unlink (rf->tmp_name);
147 /* Register file for deletion. */
148 ll_push_head (&all_files, &rf->ll);
149 unblock_fatal_signals ();
154 unblock_fatal_signals ();
155 free_replace_file (rf);
162 replace_file_commit (struct replace_file *rf)
166 if (rf->file_name != NULL)
170 block_fatal_signals ();
171 ok = rename (rf->tmp_name, rf->file_name) == 0;
174 unblock_fatal_signals ();
177 msg (ME, _("Replacing %s by %s: %s."),
178 rf->tmp_name, rf->file_name, strerror (save_errno));
182 /* Special file: no temporary file to rename. */
184 free_replace_file (rf);
190 replace_file_abort (struct replace_file *rf)
194 if (rf->file_name != NULL)
198 block_fatal_signals ();
199 ok = unlink (rf->tmp_name) == 0;
202 unblock_fatal_signals ();
205 msg (ME, _("Removing %s: %s."), rf->tmp_name, strerror (save_errno));
209 /* Special file: no temporary file to unlink. */
211 free_replace_file (rf);
217 free_replace_file (struct replace_file *rf)
219 free (rf->file_name);
225 unlink_replace_files (void)
227 struct replace_file *rf;
229 block_fatal_signals ();
230 ll_for_each (rf, struct replace_file, ll, &all_files)
232 /* We don't free_replace_file(RF) because calling free is unsafe
233 from an asynchronous signal handler. */
234 unlink (rf->tmp_name);
236 unblock_fatal_signals ();