1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2004 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/>. */
25 #include <data/file-name.h>
26 #include <data/make-file.h>
27 #include <libpspp/ll.h>
28 #include <libpspp/message.h>
30 #include "fatal-signal.h"
35 #define _(msgid) gettext (msgid)
37 /* Non ansi compilers may set this */
39 #define P_tmpdir "/tmp"
42 /* Creates a temporary file and stores its name in *FILE_NAME and
43 a file descriptor for it in *FD. Returns success. Caller is
44 responsible for freeing *FILE_NAME. */
46 make_temp_file (int *fd, char **file_name)
48 const char *parent_dir;
50 assert (file_name != NULL);
53 if (getenv ("TMPDIR") != NULL)
54 parent_dir = getenv ("TMPDIR");
56 parent_dir = P_tmpdir;
58 *file_name = xmalloc (strlen (parent_dir) + 32);
59 sprintf (*file_name, "%s/psppXXXXXX", parent_dir);
60 *fd = mkstemp (*file_name);
63 msg (ME, _("%s: Creating temporary file: %s."),
64 *file_name, strerror (errno));
73 /* Creates a temporary file and stores its name in *FILE_NAME and
74 a file stream for it in *FP. Returns success. Caller is
75 responsible for freeing *FILE_NAME and for closing *FP */
77 make_unique_file_stream (FILE **fp, char **file_name)
79 static int serial = 0;
80 const char *parent_dir;
84 Need to check for pre-existing file name.
85 Need also to pass in the directory instead of using /tmp
88 assert (file_name != NULL);
91 if (getenv ("TMPDIR") != NULL)
92 parent_dir = getenv ("TMPDIR");
94 parent_dir = P_tmpdir;
96 *file_name = xmalloc (strlen (parent_dir) + 32);
99 sprintf (*file_name, "%s/pspp%d.png", parent_dir, serial++);
101 *fp = fopen(*file_name, "w");
105 msg (ME, _("%s: Creating file: %s."), *file_name, strerror (errno));
121 static struct ll_list all_files = LL_INITIALIZER (all_files);
123 static void free_replace_file (struct replace_file *);
124 static void unlink_replace_files (void);
126 struct replace_file *
127 replace_file_start (const char *file_name, const char *mode,
128 mode_t permissions, FILE **fp, char **tmp_name)
130 static bool registered;
132 struct replace_file *rf;
135 /* If FILE_NAME represents a special file, write to it directly
136 instead of trying to replace it. */
137 if (stat (file_name, &s) == 0 && !S_ISREG (s.st_mode))
139 /* Open file descriptor. */
140 fd = open (file_name, O_WRONLY);
143 msg (ME, _("Opening %s for writing: %s."),
144 file_name, strerror (errno));
148 /* Open file as stream. */
149 *fp = fdopen (fd, mode);
152 msg (ME, _("Opening stream for %s: %s."),
153 file_name, strerror (errno));
158 rf = xmalloc (sizeof *rf);
159 rf->file_name = NULL;
160 rf->tmp_name = xstrdup (file_name);
161 if (tmp_name != NULL)
162 *tmp_name = rf->tmp_name;
168 at_fatal_signal (unlink_replace_files);
171 block_fatal_signals ();
173 rf = xmalloc (sizeof *rf);
174 rf->file_name = xstrdup (file_name);
177 /* Generate unique temporary file name. */
178 rf->tmp_name = xasprintf ("%s.tmpXXXXXX", file_name);
179 if (gen_tempname (rf->tmp_name, GT_NOCREATE) < 0)
181 msg (ME, _("Creating temporary file to replace %s: %s."),
182 rf->file_name, strerror (errno));
186 /* Create file by that name. */
187 fd = open (rf->tmp_name, O_WRONLY | O_CREAT | O_EXCL, permissions);
192 msg (ME, _("Creating temporary file %s: %s."),
193 rf->tmp_name, strerror (errno));
200 /* Open file as stream. */
201 *fp = fdopen (fd, mode);
204 msg (ME, _("Opening stream for temporary file %s: %s."),
205 rf->tmp_name, strerror (errno));
207 unlink (rf->tmp_name);
211 /* Register file for deletion. */
212 ll_push_head (&all_files, &rf->ll);
213 unblock_fatal_signals ();
215 if (tmp_name != NULL)
216 *tmp_name = rf->tmp_name;
221 unblock_fatal_signals ();
222 free_replace_file (rf);
224 if (tmp_name != NULL)
230 replace_file_commit (struct replace_file *rf)
234 if (rf->file_name != NULL)
238 block_fatal_signals ();
239 ok = rename (rf->tmp_name, rf->file_name) == 0;
242 unblock_fatal_signals ();
245 msg (ME, _("Replacing %s by %s: %s."),
246 rf->tmp_name, rf->file_name, strerror (save_errno));
250 /* Special file: no temporary file to rename. */
252 free_replace_file (rf);
258 replace_file_abort (struct replace_file *rf)
262 if (rf->file_name != NULL)
266 block_fatal_signals ();
267 ok = unlink (rf->tmp_name) == 0;
270 unblock_fatal_signals ();
273 msg (ME, _("Removing %s: %s."), rf->tmp_name, strerror (save_errno));
277 /* Special file: no temporary file to unlink. */
279 free_replace_file (rf);
285 free_replace_file (struct replace_file *rf)
287 free (rf->file_name);
293 unlink_replace_files (void)
295 struct replace_file *rf;
297 block_fatal_signals ();
298 ll_for_each (rf, struct replace_file, ll, &all_files)
300 /* We don't free_replace_file(RF) because calling free is unsafe
301 from an asynchronous signal handler. */
302 unlink (rf->tmp_name);
305 unblock_fatal_signals ();