/* PSPP - a program for statistical analysis.
- Copyright (C) 2004 Free Software Foundation, Inc.
+ Copyright (C) 2004, 2010 Free Software Foundation, Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
-#ifndef MKFILE_H
-#define MKFILE_H
+#ifndef MAKE_FILE_H
+#define MAKE_FILE_H
+#include <stdbool.h>
+#include <stdio.h>
+#include <sys/types.h>
-/* Creates a temporary file and stores its name in *FILE_NAME and
- a file descriptor for it in *FD. Returns success. Caller is
- responsible for freeing *FILE_NAME. */
-int make_temp_file (int *fd, char **file_name);
+struct file_handle;
+/* Prepares to atomically replace a (potentially) existing file by a new file,
+ by creating a temporary file with the given PERMISSIONS bits.
-/* Creates a temporary file and stores its name in *FILE_NAME and
- a file stream for it in *FP. Returns success. Caller is
- responsible for freeing *FILE_NAME. */
-int make_unique_file_stream (FILE **fp, char **file_name) ;
+ Special files are an exception: they are not atomically
+ replaced but simply opened for writing.
+
+ If successful, returns a ticket that can be used to commit or abort the file
+ replacement. If neither action is taken, program termination via signal
+ will abort. Depending on the function, stores a file descriptor in *FD or a
+ stream in *FP for the newly opened file. The descriptor or stream is opened
+ for writing a binary file if BINARY is true, otherwise a text file (this
+ disctinction only matters on Windows).
+
+ On error, returns NULL and stores NULL in *FP or -1 in *FD.
+
+ The caller is responsible for closing *FP or *FD. */
+
+struct replace_file *replace_file_start (const struct file_handle *fh,
+ bool binary, mode_t permissions,
+ FILE **fp);
+struct replace_file *replace_file_start_fd (const struct file_handle *fh,
+ bool binary,
+ mode_t permissions, int *fd);
+
+/* Commits or aborts the replacement of a (potentially) existing
+ file by a new file, using the ticket returned by
+ replace_file_start. Returns success. */
+bool replace_file_commit (struct replace_file *);
+bool replace_file_abort (struct replace_file *);
#endif /* make-file.h */