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"
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"
42 #define _(msgid) gettext (msgid)
45 #if defined _WIN32 || defined __WIN32__
46 #define WIN32_LEAN_AND_MEAN /* avoid including junk */
49 #define Tunlink _wunlink
53 /* Shamelessly lifted and modified from the rpl_rename function in Gnulib */
55 Trename (TCHAR const *src, TCHAR const *dst)
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
63 if (MoveFileExW (src, dst, 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))
74 error = GetLastError ();
79 case ERROR_FILE_NOT_FOUND:
80 case ERROR_PATH_NOT_FOUND:
81 case ERROR_BAD_PATHNAME:
86 case ERROR_ACCESS_DENIED:
87 case ERROR_SHARING_VIOLATION:
91 case ERROR_OUTOFMEMORY:
95 case ERROR_CURRENT_DIRECTORY:
99 case ERROR_NOT_SAME_DEVICE:
103 case ERROR_WRITE_PROTECT:
107 case ERROR_WRITE_FAULT:
108 case ERROR_READ_FAULT:
109 case ERROR_GEN_FAILURE:
113 case ERROR_HANDLE_DISK_FULL:
114 case ERROR_DISK_FULL:
115 case ERROR_DISK_TOO_FRAGMENTED:
119 case ERROR_FILE_EXISTS:
120 case ERROR_ALREADY_EXISTS:
124 case ERROR_BUFFER_OVERFLOW:
125 case ERROR_FILENAME_EXCED_RANGE:
126 errno = ENAMETOOLONG;
129 case ERROR_INVALID_NAME:
130 case ERROR_DELETE_PENDING:
131 errno = EPERM; /* ? */
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
138 case ERROR_FILE_TOO_LARGE:
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);
163 #define Trename rename
164 #define Tunlink unlink
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. */
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;
200 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 /* Open file as stream. */
224 *fp = fdopen (fd, mode);
228 msg (ME, _("Opening stream for %s: %s."),
229 file_name, strerror (saved_errno));
235 rf = xzalloc (sizeof *rf);
236 rf->file_name = NULL;
237 rf->tmp_name = Tfile_name;
243 at_fatal_signal (unlink_replace_files);
246 block_fatal_signals ();
248 rf = xzalloc (sizeof *rf);
249 rf->file_name = Tfile_name;
250 rf->file_name_verbatim = file_name;
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)
260 msg (ME, _("Creating temporary file to replace %s: %s."),
261 file_name, strerror (saved_errno));
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),
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 (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 ();