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/>. */
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"
40 #include "gl/localcharset.h"
43 #define _(msgid) gettext (msgid)
46 #if defined _WIN32 || defined __WIN32__
47 #define WIN32_LEAN_AND_MEAN /* avoid including junk */
50 #define Tunlink _wunlink
54 /* Shamelessly lifted and modified from the rpl_rename function in Gnulib */
56 Trename (TCHAR const *src, TCHAR const *dst)
60 /* MoveFileExW works if SRC is a directory without any flags, but
61 fails with MOVEFILE_REPLACE_EXISTING, so try without flags first.
62 Thankfully, MoveFileExW handles hard links correctly, even though
64 if (MoveFileExW (src, dst, 0))
67 /* Retry with MOVEFILE_REPLACE_EXISTING if the move failed
68 due to the destination already existing. */
69 error = GetLastError ();
70 if (error == ERROR_FILE_EXISTS || error == ERROR_ALREADY_EXISTS)
72 if (MoveFileExW (src, dst, MOVEFILE_REPLACE_EXISTING))
75 error = GetLastError ();
80 case ERROR_FILE_NOT_FOUND:
81 case ERROR_PATH_NOT_FOUND:
82 case ERROR_BAD_PATHNAME:
87 case ERROR_ACCESS_DENIED:
88 case ERROR_SHARING_VIOLATION:
92 case ERROR_OUTOFMEMORY:
96 case ERROR_CURRENT_DIRECTORY:
100 case ERROR_NOT_SAME_DEVICE:
104 case ERROR_WRITE_PROTECT:
108 case ERROR_WRITE_FAULT:
109 case ERROR_READ_FAULT:
110 case ERROR_GEN_FAILURE:
114 case ERROR_HANDLE_DISK_FULL:
115 case ERROR_DISK_FULL:
116 case ERROR_DISK_TOO_FRAGMENTED:
120 case ERROR_FILE_EXISTS:
121 case ERROR_ALREADY_EXISTS:
125 case ERROR_BUFFER_OVERFLOW:
126 case ERROR_FILENAME_EXCED_RANGE:
127 errno = ENAMETOOLONG;
130 case ERROR_INVALID_NAME:
131 case ERROR_DELETE_PENDING:
132 errno = EPERM; /* ? */
135 # ifndef ERROR_FILE_TOO_LARGE
136 /* This value is documented but not defined in all versions of windows.h. */
137 # define ERROR_FILE_TOO_LARGE 223
139 case ERROR_FILE_TOO_LARGE:
152 convert_to_filename_encoding (const char *s, size_t len, const char *current_encoding)
154 const char *enc = current_encoding;
155 if (NULL == enc || 0 == strcmp (enc, "Auto"))
156 enc = locale_charset ();
158 return (TCHAR *) recode_string ("UTF-16LE", enc, s, len);
164 #define Trename rename
165 #define Tunlink unlink
170 convert_to_filename_encoding (const char *s, size_t len UNUSED, const char *current_encoding UNUSED)
172 /* Non-windows systems don't care about the encoding.
173 The string is copied here, to be consistent with the w32 case. */
186 char *tmp_name_verbatim;
187 const char *file_name_verbatim;
190 static struct ll_list all_files = LL_INITIALIZER (all_files);
192 static void free_replace_file (struct replace_file *);
193 static void unlink_replace_files (void);
195 struct replace_file *
196 replace_file_start_fd (const struct file_handle *fh,
197 bool binary, mode_t permissions, int *fd)
199 static bool registered;
201 struct replace_file *rf;
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);
217 msg (ME, _("Opening %s for writing: %s."),
218 file_name, strerror (saved_errno));
223 rf = xzalloc (sizeof *rf);
224 rf->file_name = NULL;
225 rf->tmp_name = Tfile_name;
231 at_fatal_signal (unlink_replace_files);
234 block_fatal_signals ();
236 rf = xzalloc (sizeof *rf);
237 rf->file_name = Tfile_name;
238 rf->file_name_verbatim = file_name;
242 /* Generate unique temporary file name. */
243 free (rf->tmp_name_verbatim);
244 rf->tmp_name_verbatim = xasprintf ("%stmpXXXXXX", file_name);
245 if (gen_tempname (rf->tmp_name_verbatim, 0, 0600, GT_NOCREATE) < 0)
248 msg (ME, _("Creating temporary file to replace %s: %s."),
249 file_name, strerror (saved_errno));
253 rf->tmp_name = convert_to_filename_encoding (rf->tmp_name_verbatim, strlen (rf->tmp_name_verbatim), fh_get_file_name_encoding (fh));
255 /* Create file by that name. */
256 int flags = O_WRONLY | O_CREAT | O_EXCL | (binary ? O_BINARY : O_TEXT);
257 *fd = Topen (rf->tmp_name, flags, permissions);
263 msg (ME, _("Creating temporary file %s: %s."),
264 rf->tmp_name_verbatim, strerror (saved_errno));
270 /* Register file for deletion. */
271 ll_push_head (&all_files, &rf->ll);
272 unblock_fatal_signals ();
277 unblock_fatal_signals ();
278 free_replace_file (rf);
284 struct replace_file *
285 replace_file_start (const struct file_handle *fh, bool binary,
286 mode_t permissions, FILE **fp)
288 struct replace_file *rf;
292 rf = replace_file_start_fd (fh, binary, permissions, &fd);
296 /* Open file as stream. */
297 *fp = fdopen (fd, binary ? "wb" : "w");
301 msg (ME, _("Opening stream for temporary file %s: %s."),
302 rf->tmp_name_verbatim, strerror (error));
304 replace_file_abort (rf);
318 replace_file_commit (struct replace_file *rf)
322 if (rf->file_name != NULL)
326 block_fatal_signals ();
327 ok = Trename (rf->tmp_name, rf->file_name) == 0;
330 unblock_fatal_signals ();
333 msg (ME, _("Replacing %s by %s: %s."),
334 rf->file_name_verbatim, rf->tmp_name_verbatim, strerror (save_errno));
338 /* Special file: no temporary file to rename. */
340 free_replace_file (rf);
346 replace_file_abort (struct replace_file *rf)
350 if (rf->file_name != NULL)
354 block_fatal_signals ();
355 ok = Tunlink (rf->tmp_name) == 0;
358 unblock_fatal_signals ();
361 msg (ME, _("Removing %s: %s."), rf->tmp_name_verbatim, strerror (save_errno));
365 /* Special file: no temporary file to unlink. */
367 free_replace_file (rf);
373 free_replace_file (struct replace_file *rf)
375 free (rf->file_name);
377 free (rf->tmp_name_verbatim);
382 unlink_replace_files (void)
384 struct replace_file *rf;
386 block_fatal_signals ();
387 ll_for_each (rf, struct replace_file, ll, &all_files)
389 /* We don't free_replace_file(RF) because calling free is unsafe
390 from an asynchronous signal handler. */
391 Tunlink (rf->tmp_name);
393 unblock_fatal_signals ();