- msg (ME, _("%s: Creating temporary file: %s."),
- *filename, strerror (errno));
- free (*filename);
- *filename = NULL;
- return 0;
+ at_fatal_signal (unlink_replace_files);
+ registered = true;
+ }
+ block_fatal_signals ();
+
+ rf = xmalloc (sizeof *rf);
+ rf->file_name = xstrdup (file_name);
+ for (;;)
+ {
+ /* Generate unique temporary file name. */
+ rf->tmp_name = xasprintf ("%stmpXXXXXX", file_name);
+ if (gen_tempname (rf->tmp_name, 0, 0600, GT_NOCREATE) < 0)
+ {
+ saved_errno = errno;
+ msg (ME, _("Creating temporary file to replace %s: %s."),
+ rf->file_name, strerror (saved_errno));
+ goto error;
+ }
+
+ /* Create file by that name. */
+ fd = open (rf->tmp_name, O_WRONLY | O_CREAT | O_EXCL | O_BINARY, permissions);
+ if (fd >= 0)
+ break;
+ if (errno != EEXIST)
+ {
+ saved_errno = errno;
+ msg (ME, _("Creating temporary file %s: %s."),
+ rf->tmp_name, strerror (saved_errno));
+ goto error;
+ }
+ free (rf->tmp_name);