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 */
51 #define Tunlink _wunlink
55 /* Shamelessly lifted and modified from the rpl_rename function in Gnulib */
57 Trename (TCHAR const *src, TCHAR const *dst)
61 /* MoveFileExW works if SRC is a directory without any flags, but
62 fails with MOVEFILE_REPLACE_EXISTING, so try without flags first.
63 Thankfully, MoveFileExW handles hard links correctly, even though
65 if (MoveFileExW (src, dst, 0))
68 /* Retry with MOVEFILE_REPLACE_EXISTING if the move failed
69 due to the destination already existing. */
70 error = GetLastError ();
71 if (error == ERROR_FILE_EXISTS || error == ERROR_ALREADY_EXISTS)
73 if (MoveFileExW (src, dst, MOVEFILE_REPLACE_EXISTING))
76 error = GetLastError ();
81 case ERROR_FILE_NOT_FOUND:
82 case ERROR_PATH_NOT_FOUND:
83 case ERROR_BAD_PATHNAME:
88 case ERROR_ACCESS_DENIED:
89 case ERROR_SHARING_VIOLATION:
93 case ERROR_OUTOFMEMORY:
97 case ERROR_CURRENT_DIRECTORY:
101 case ERROR_NOT_SAME_DEVICE:
105 case ERROR_WRITE_PROTECT:
109 case ERROR_WRITE_FAULT:
110 case ERROR_READ_FAULT:
111 case ERROR_GEN_FAILURE:
115 case ERROR_HANDLE_DISK_FULL:
116 case ERROR_DISK_FULL:
117 case ERROR_DISK_TOO_FRAGMENTED:
121 case ERROR_FILE_EXISTS:
122 case ERROR_ALREADY_EXISTS:
126 case ERROR_BUFFER_OVERFLOW:
127 case ERROR_FILENAME_EXCED_RANGE:
128 errno = ENAMETOOLONG;
131 case ERROR_INVALID_NAME:
132 case ERROR_DELETE_PENDING:
133 errno = EPERM; /* ? */
136 # ifndef ERROR_FILE_TOO_LARGE
137 /* This value is documented but not defined in all versions of windows.h. */
138 # define ERROR_FILE_TOO_LARGE 223
140 case ERROR_FILE_TOO_LARGE:
153 convert_to_filename_encoding (const char *s, size_t len, const char *current_encoding)
155 const char *enc = current_encoding;
156 if (NULL == enc || 0 == strcmp (enc, "Auto"))
157 enc = locale_charset ();
159 return (TCHAR *) recode_string ("UTF-16LE", enc, s, len);
166 #define Trename rename
167 #define Tunlink unlink
172 convert_to_filename_encoding (const char *s, size_t len UNUSED, const char *current_encoding UNUSED)
174 /* Non-windows systems don't care about the encoding.
175 The string is copied here, to be consistent with the w32 case. */
188 char *tmp_name_verbatim;
189 const char *file_name_verbatim;
192 static struct ll_list all_files = LL_INITIALIZER (all_files);
194 static void free_replace_file (struct replace_file *);
195 static void unlink_replace_files (void);
197 struct replace_file *
198 replace_file_start (const struct file_handle *fh, const char *mode,
199 mode_t permissions, FILE **fp)
201 static bool registered;
203 struct replace_file *rf;
205 int saved_errno = errno;
207 const char *file_name = fh_get_file_name (fh);
209 TCHAR * Tfile_name = convert_to_filename_encoding (file_name, strlen (file_name), fh_get_file_name_encoding (fh));
211 /* If FILE_NAME represents a special file, write to it directly
212 instead of trying to replace it. */
213 if (Tstat (Tfile_name, &s) == 0 && !S_ISREG (s.st_mode))
215 /* Open file descriptor. */
216 fd = Topen (Tfile_name, O_WRONLY);
220 msg (ME, _("Opening %s for writing: %s."),
221 file_name, strerror (saved_errno));
226 /* Open file as stream. */
227 *fp = fdopen (fd, mode);
231 msg (ME, _("Opening stream for %s: %s."),
232 file_name, strerror (saved_errno));
238 rf = xzalloc (sizeof *rf);
239 rf->file_name = NULL;
240 rf->tmp_name = Tfile_name;
246 at_fatal_signal (unlink_replace_files);
249 block_fatal_signals ();
251 rf = xzalloc (sizeof *rf);
252 rf->file_name = Tfile_name;
253 rf->file_name_verbatim = file_name;
257 /* Generate unique temporary file name. */
258 free (rf->tmp_name_verbatim);
259 rf->tmp_name_verbatim = xasprintf ("%stmpXXXXXX", file_name);
260 if (gen_tempname (rf->tmp_name_verbatim, 0, 0600, GT_NOCREATE) < 0)
263 msg (ME, _("Creating temporary file to replace %s: %s."),
264 file_name, strerror (saved_errno));
268 rf->tmp_name = convert_to_filename_encoding (rf->tmp_name_verbatim, strlen (rf->tmp_name_verbatim), fh_get_file_name_encoding (fh));
270 /* Create file by that name. */
271 fd = Topen (rf->tmp_name, O_WRONLY | O_CREAT | O_EXCL | O_BINARY, permissions);
277 msg (ME, _("Creating temporary file %s: %s."),
278 rf->tmp_name_verbatim, strerror (saved_errno));
284 /* Open file as stream. */
285 *fp = fdopen (fd, mode);
289 msg (ME, _("Opening stream for temporary file %s: %s."),
290 rf->tmp_name_verbatim, strerror (saved_errno));
292 Tunlink (rf->tmp_name);
296 /* Register file for deletion. */
297 ll_push_head (&all_files, &rf->ll);
298 unblock_fatal_signals ();
303 unblock_fatal_signals ();
304 free_replace_file (rf);
311 replace_file_commit (struct replace_file *rf)
315 if (rf->file_name != NULL)
319 block_fatal_signals ();
320 ok = Trename (rf->tmp_name, rf->file_name) == 0;
323 unblock_fatal_signals ();
326 msg (ME, _("Replacing %s by %s: %s."),
327 rf->file_name_verbatim, rf->tmp_name_verbatim, strerror (save_errno));
331 /* Special file: no temporary file to rename. */
333 free_replace_file (rf);
339 replace_file_abort (struct replace_file *rf)
343 if (rf->file_name != NULL)
347 block_fatal_signals ();
348 ok = Tunlink (rf->tmp_name) == 0;
351 unblock_fatal_signals ();
354 msg (ME, _("Removing %s: %s."), rf->tmp_name_verbatim, strerror (save_errno));
358 /* Special file: no temporary file to unlink. */
360 free_replace_file (rf);
366 free_replace_file (struct replace_file *rf)
368 free (rf->file_name);
370 free (rf->tmp_name_verbatim);
375 unlink_replace_files (void)
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 ();