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 (int sig);
195 struct replace_file *
196 replace_file_start (const struct file_handle *fh, const char *mode,
197 mode_t permissions, FILE **fp)
199 static bool registered;
201 struct replace_file *rf;
203 int saved_errno = errno;
205 const char *file_name = fh_get_file_name (fh);
207 TCHAR * Tfile_name = convert_to_filename_encoding (file_name, strlen (file_name), fh_get_file_name_encoding (fh));
209 /* If FILE_NAME represents a special file, write to it directly
210 instead of trying to replace it. */
211 if (Tstat (Tfile_name, &s) == 0 && !S_ISREG (s.st_mode))
213 /* Open file descriptor. */
214 fd = Topen (Tfile_name, O_WRONLY);
218 msg (ME, _("Opening %s for writing: %s."),
219 file_name, strerror (saved_errno));
224 /* Open file as stream. */
225 *fp = fdopen (fd, mode);
229 msg (ME, _("Opening stream for %s: %s."),
230 file_name, strerror (saved_errno));
236 rf = xzalloc (sizeof *rf);
237 rf->file_name = NULL;
238 rf->tmp_name = Tfile_name;
244 at_fatal_signal (unlink_replace_files);
247 block_fatal_signals ();
249 rf = xzalloc (sizeof *rf);
250 rf->file_name = Tfile_name;
251 rf->file_name_verbatim = file_name;
255 /* Generate unique temporary file name. */
256 free (rf->tmp_name_verbatim);
257 rf->tmp_name_verbatim = xasprintf ("%stmpXXXXXX", file_name);
258 if (gen_tempname (rf->tmp_name_verbatim, 0, 0600, GT_NOCREATE) < 0)
261 msg (ME, _("Creating temporary file to replace %s: %s."),
262 file_name, strerror (saved_errno));
266 rf->tmp_name = convert_to_filename_encoding (rf->tmp_name_verbatim, strlen (rf->tmp_name_verbatim), fh_get_file_name_encoding (fh));
268 /* Create file by that name. */
269 fd = Topen (rf->tmp_name, O_WRONLY | O_CREAT | O_EXCL | O_BINARY, permissions);
275 msg (ME, _("Creating temporary file %s: %s."),
276 rf->tmp_name_verbatim, strerror (saved_errno));
282 /* Open file as stream. */
283 *fp = fdopen (fd, mode);
287 msg (ME, _("Opening stream for temporary file %s: %s."),
288 rf->tmp_name_verbatim, strerror (saved_errno));
290 Tunlink (rf->tmp_name);
294 /* Register file for deletion. */
295 ll_push_head (&all_files, &rf->ll);
296 unblock_fatal_signals ();
301 unblock_fatal_signals ();
302 free_replace_file (rf);
309 replace_file_commit (struct replace_file *rf)
313 if (rf->file_name != NULL)
317 block_fatal_signals ();
318 ok = Trename (rf->tmp_name, rf->file_name) == 0;
321 unblock_fatal_signals ();
324 msg (ME, _("Replacing %s by %s: %s."),
325 rf->file_name_verbatim, rf->tmp_name_verbatim, strerror (save_errno));
329 /* Special file: no temporary file to rename. */
331 free_replace_file (rf);
337 replace_file_abort (struct replace_file *rf)
341 if (rf->file_name != NULL)
345 block_fatal_signals ();
346 ok = Tunlink (rf->tmp_name) == 0;
349 unblock_fatal_signals ();
352 msg (ME, _("Removing %s: %s."), rf->tmp_name_verbatim, strerror (save_errno));
356 /* Special file: no temporary file to unlink. */
358 free_replace_file (rf);
364 free_replace_file (struct replace_file *rf)
366 free (rf->file_name);
368 free (rf->tmp_name_verbatim);
373 unlink_replace_files (int sig UNUSED)
375 struct replace_file *rf;
377 block_fatal_signals ();
378 ll_for_each (rf, struct replace_file, ll, &all_files)
380 /* We don't free_replace_file(RF) because calling free is unsafe
381 from an asynchronous signal handler. */
382 Tunlink (rf->tmp_name);
384 unblock_fatal_signals ();