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 "libpspp/ll.h"
32 #include "libpspp/message.h"
34 #include "gl/fatal-signal.h"
35 #include "gl/tempname.h"
36 #include "gl/xalloc.h"
37 #include "gl/xvasprintf.h"
40 #define _(msgid) gettext (msgid)
49 static struct ll_list all_files = LL_INITIALIZER (all_files);
51 static void free_replace_file (struct replace_file *);
52 static void unlink_replace_files (void);
55 replace_file_start (const char *file_name, const char *mode,
56 mode_t permissions, FILE **fp, char **tmp_name)
58 static bool registered;
60 struct replace_file *rf;
63 /* If FILE_NAME represents a special file, write to it directly
64 instead of trying to replace it. */
65 if (stat (file_name, &s) == 0 && !S_ISREG (s.st_mode))
67 /* Open file descriptor. */
68 fd = open (file_name, O_WRONLY);
71 msg (ME, _("Opening %s for writing: %s."),
72 file_name, strerror (errno));
76 /* Open file as stream. */
77 *fp = fdopen (fd, mode);
80 msg (ME, _("Opening stream for %s: %s."),
81 file_name, strerror (errno));
86 rf = xmalloc (sizeof *rf);
88 rf->tmp_name = xstrdup (file_name);
90 *tmp_name = rf->tmp_name;
96 at_fatal_signal (unlink_replace_files);
99 block_fatal_signals ();
101 rf = xmalloc (sizeof *rf);
102 rf->file_name = xstrdup (file_name);
105 /* Generate unique temporary file name. */
106 rf->tmp_name = xasprintf ("%s.tmpXXXXXX", file_name);
107 if (gen_tempname (rf->tmp_name, 0, 0600, GT_NOCREATE) < 0)
109 msg (ME, _("Creating temporary file to replace %s: %s."),
110 rf->file_name, strerror (errno));
114 /* Create file by that name. */
115 fd = open (rf->tmp_name, O_WRONLY | O_CREAT | O_EXCL | O_BINARY, permissions);
120 msg (ME, _("Creating temporary file %s: %s."),
121 rf->tmp_name, strerror (errno));
128 /* Open file as stream. */
129 *fp = fdopen (fd, mode);
132 msg (ME, _("Opening stream for temporary file %s: %s."),
133 rf->tmp_name, strerror (errno));
135 unlink (rf->tmp_name);
139 /* Register file for deletion. */
140 ll_push_head (&all_files, &rf->ll);
141 unblock_fatal_signals ();
143 if (tmp_name != NULL)
144 *tmp_name = rf->tmp_name;
149 unblock_fatal_signals ();
150 free_replace_file (rf);
152 if (tmp_name != NULL)
158 replace_file_commit (struct replace_file *rf)
162 if (rf->file_name != NULL)
166 block_fatal_signals ();
167 ok = rename (rf->tmp_name, rf->file_name) == 0;
170 unblock_fatal_signals ();
173 msg (ME, _("Replacing %s by %s: %s."),
174 rf->tmp_name, rf->file_name, strerror (save_errno));
178 /* Special file: no temporary file to rename. */
180 free_replace_file (rf);
186 replace_file_abort (struct replace_file *rf)
190 if (rf->file_name != NULL)
194 block_fatal_signals ();
195 ok = unlink (rf->tmp_name) == 0;
198 unblock_fatal_signals ();
201 msg (ME, _("Removing %s: %s."), rf->tmp_name, strerror (save_errno));
205 /* Special file: no temporary file to unlink. */
207 free_replace_file (rf);
213 free_replace_file (struct replace_file *rf)
215 free (rf->file_name);
221 unlink_replace_files (void)
223 struct replace_file *rf;
225 block_fatal_signals ();
226 ll_for_each (rf, struct replace_file, ll, &all_files)
228 /* We don't free_replace_file(RF) because calling free is unsafe
229 from an asynchronous signal handler. */
230 unlink (rf->tmp_name);
232 unblock_fatal_signals ();