Independent Samples T-Test Dialog: Fix Crash
[pspp.git] / src / data / make-file.c
blob0090c09c43362584e7f8cdb1891766f2f72e7c58
1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2004, 2010, 2015 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/>. */
17 #include <config.h>
19 #include "data/make-file.h"
20 #include "libpspp/i18n.h"
22 #include <errno.h>
23 #include <fcntl.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <sys/stat.h>
28 #include <unistd.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"
39 #include "gl/localcharset.h"
41 #include "gettext.h"
42 #define _(msgid) gettext (msgid)
45 #if defined _WIN32 || defined __WIN32__
46 #define WIN32_LEAN_AND_MEAN /* avoid including junk */
47 #include <windows.h>
48 #define TS_stat _stat
49 #define Tunlink _wunlink
50 #define Topen _wopen
51 #define Tstat _wstat
53 /* Shamelessly lifted and modified from the rpl_rename function in Gnulib */
54 static int
55 Trename (TCHAR const *src, TCHAR const *dst)
57 int error;
59 /* MoveFileExW works if SRC is a directory without any flags, but
60 fails with MOVEFILE_REPLACE_EXISTING, so try without flags first.
61 Thankfully, MoveFileExW handles hard links correctly, even though
62 rename() does not. */
63 if (MoveFileExW (src, dst, 0))
64 return 0;
66 /* Retry with MOVEFILE_REPLACE_EXISTING if the move failed
67 due to the destination already existing. */
68 error = GetLastError ();
69 if (error == ERROR_FILE_EXISTS || error == ERROR_ALREADY_EXISTS)
71 if (MoveFileExW (src, dst, MOVEFILE_REPLACE_EXISTING))
72 return 0;
74 error = GetLastError ();
77 switch (error)
79 case ERROR_FILE_NOT_FOUND:
80 case ERROR_PATH_NOT_FOUND:
81 case ERROR_BAD_PATHNAME:
82 case ERROR_DIRECTORY:
83 errno = ENOENT;
84 break;
86 case ERROR_ACCESS_DENIED:
87 case ERROR_SHARING_VIOLATION:
88 errno = EACCES;
89 break;
91 case ERROR_OUTOFMEMORY:
92 errno = ENOMEM;
93 break;
95 case ERROR_CURRENT_DIRECTORY:
96 errno = EBUSY;
97 break;
99 case ERROR_NOT_SAME_DEVICE:
100 errno = EXDEV;
101 break;
103 case ERROR_WRITE_PROTECT:
104 errno = EROFS;
105 break;
107 case ERROR_WRITE_FAULT:
108 case ERROR_READ_FAULT:
109 case ERROR_GEN_FAILURE:
110 errno = EIO;
111 break;
113 case ERROR_HANDLE_DISK_FULL:
114 case ERROR_DISK_FULL:
115 case ERROR_DISK_TOO_FRAGMENTED:
116 errno = ENOSPC;
117 break;
119 case ERROR_FILE_EXISTS:
120 case ERROR_ALREADY_EXISTS:
121 errno = EEXIST;
122 break;
124 case ERROR_BUFFER_OVERFLOW:
125 case ERROR_FILENAME_EXCED_RANGE:
126 errno = ENAMETOOLONG;
127 break;
129 case ERROR_INVALID_NAME:
130 case ERROR_DELETE_PENDING:
131 errno = EPERM; /* ? */
132 break;
134 # ifndef ERROR_FILE_TOO_LARGE
135 /* This value is documented but not defined in all versions of windows.h. */
136 # define ERROR_FILE_TOO_LARGE 223
137 # endif
138 case ERROR_FILE_TOO_LARGE:
139 errno = EFBIG;
140 break;
142 default:
143 errno = EINVAL;
144 break;
147 return -1;
150 TCHAR *
151 convert_to_filename_encoding (const char *s, size_t len, const char *current_encoding)
153 const char *enc = current_encoding;
154 if (NULL == enc || 0 == strcmp (enc, "Auto"))
155 enc = locale_charset ();
157 return (TCHAR *) recode_string ("UTF-16LE", enc, s, len);
161 #else
162 #define TS_stat stat
163 #define Trename rename
164 #define Tunlink unlink
165 #define Topen open
166 #define Tstat stat
168 TCHAR *
169 convert_to_filename_encoding (const char *s, size_t len UNUSED, const char *current_encoding UNUSED)
171 /* Non-windows systems don't care about the encoding.
172 The string is copied here, to be consistent with the w32 case. */
173 return xstrdup (s);
176 #endif
179 struct replace_file
181 struct ll ll;
182 TCHAR *file_name;
183 TCHAR *tmp_name;
185 char *tmp_name_verbatim;
186 const char *file_name_verbatim;
189 static struct ll_list all_files = LL_INITIALIZER (all_files);
191 static void free_replace_file (struct replace_file *);
192 static void unlink_replace_files (int sig);
194 struct replace_file *
195 replace_file_start (const struct file_handle *fh, const char *mode,
196 mode_t permissions, FILE **fp)
198 static bool registered;
199 struct TS_stat s;
200 struct replace_file *rf;
201 int fd;
202 int saved_errno = errno;
204 const char *file_name = fh_get_file_name (fh);
206 TCHAR * Tfile_name = convert_to_filename_encoding (file_name, strlen (file_name), fh_get_file_name_encoding (fh));
208 /* If FILE_NAME represents a special file, write to it directly
209 instead of trying to replace it. */
210 if (Tstat (Tfile_name, &s) == 0 && !S_ISREG (s.st_mode))
212 /* Open file descriptor. */
213 fd = Topen (Tfile_name, O_WRONLY);
214 if (fd < 0)
216 saved_errno = errno;
217 msg (ME, _("Opening %s for writing: %s."),
218 file_name, strerror (saved_errno));
219 free (Tfile_name);
220 return NULL;
223 /* Open file as stream. */
224 *fp = fdopen (fd, mode);
225 if (*fp == NULL)
227 saved_errno = errno;
228 msg (ME, _("Opening stream for %s: %s."),
229 file_name, strerror (saved_errno));
230 close (fd);
231 free (Tfile_name);
232 return NULL;
235 rf = xzalloc (sizeof *rf);
236 rf->file_name = NULL;
237 rf->tmp_name = Tfile_name;
238 return rf;
241 if (!registered)
243 at_fatal_signal (unlink_replace_files);
244 registered = true;
246 block_fatal_signals ();
248 rf = xzalloc (sizeof *rf);
249 rf->file_name = Tfile_name;
250 rf->file_name_verbatim = file_name;
252 for (;;)
254 /* Generate unique temporary file name. */
255 free (rf->tmp_name_verbatim);
256 rf->tmp_name_verbatim = xasprintf ("%stmpXXXXXX", file_name);
257 if (gen_tempname (rf->tmp_name_verbatim, 0, 0600, GT_NOCREATE) < 0)
259 saved_errno = errno;
260 msg (ME, _("Creating temporary file to replace %s: %s."),
261 file_name, strerror (saved_errno));
262 goto error;
265 rf->tmp_name = convert_to_filename_encoding (rf->tmp_name_verbatim, strlen (rf->tmp_name_verbatim), fh_get_file_name_encoding (fh));
267 /* Create file by that name. */
268 bool binary = strchr (mode, 'b') != NULL;
269 fd = Topen (rf->tmp_name,
270 O_WRONLY | O_CREAT | O_EXCL | (binary ? O_BINARY : O_TEXT),
271 permissions);
272 if (fd >= 0)
273 break;
274 if (errno != EEXIST)
276 saved_errno = errno;
277 msg (ME, _("Creating temporary file %s: %s."),
278 rf->tmp_name_verbatim, strerror (saved_errno));
279 goto error;
284 /* Open file as stream. */
285 *fp = fdopen (fd, mode);
286 if (*fp == NULL)
288 saved_errno = errno;
289 msg (ME, _("Opening stream for temporary file %s: %s."),
290 rf->tmp_name_verbatim, strerror (saved_errno));
291 close (fd);
292 Tunlink (rf->tmp_name);
293 goto error;
296 /* Register file for deletion. */
297 ll_push_head (&all_files, &rf->ll);
298 unblock_fatal_signals ();
300 return rf;
302 error:
303 unblock_fatal_signals ();
304 free_replace_file (rf);
305 *fp = NULL;
306 errno = saved_errno;
307 return NULL;
310 bool
311 replace_file_commit (struct replace_file *rf)
313 bool ok = true;
315 if (rf->file_name != NULL)
317 int save_errno;
319 block_fatal_signals ();
320 ok = Trename (rf->tmp_name, rf->file_name) == 0;
321 save_errno = errno;
322 ll_remove (&rf->ll);
323 unblock_fatal_signals ();
325 if (!ok)
326 msg (ME, _("Replacing %s by %s: %s."),
327 rf->file_name_verbatim, rf->tmp_name_verbatim, strerror (save_errno));
329 else
331 /* Special file: no temporary file to rename. */
333 free_replace_file (rf);
335 return ok;
338 bool
339 replace_file_abort (struct replace_file *rf)
341 bool ok = true;
343 if (rf->file_name != NULL)
345 int save_errno;
347 block_fatal_signals ();
348 ok = Tunlink (rf->tmp_name) == 0;
349 save_errno = errno;
350 ll_remove (&rf->ll);
351 unblock_fatal_signals ();
353 if (!ok)
354 msg (ME, _("Removing %s: %s."), rf->tmp_name_verbatim, strerror (save_errno));
356 else
358 /* Special file: no temporary file to unlink. */
360 free_replace_file (rf);
362 return ok;
365 static void
366 free_replace_file (struct replace_file *rf)
368 free (rf->file_name);
369 free (rf->tmp_name);
370 free (rf->tmp_name_verbatim);
371 free (rf);
374 static void
375 unlink_replace_files (int sig UNUSED)
377 struct replace_file *rf;
379 block_fatal_signals ();
380 ll_for_each (rf, struct replace_file, ll, &all_files)
382 /* We don't free_replace_file(RF) because calling free is unsafe
383 from an asynchronous signal handler. */
384 Tunlink (rf->tmp_name);
386 unblock_fatal_signals ();