bcc229f9f3569ceeb30c356b04c8122ef6afbc9b
   1#ifndef TEMPFILE_H
   2#define TEMPFILE_H
   3
   4/*
   5 * Handle temporary files.
   6 *
   7 * The tempfile API allows temporary files to be created, deleted, and
   8 * atomically renamed. Temporary files that are still active when the
   9 * program ends are cleaned up automatically. Lockfiles (see
  10 * "lockfile.h") are built on top of this API.
  11 *
  12 *
  13 * Calling sequence
  14 * ----------------
  15 *
  16 * The caller:
  17 *
  18 * * Allocates a `struct tempfile` either as a static variable or on
  19 *   the heap, initialized to zeros. Once you use the structure to
  20 *   call `create_tempfile()`, it belongs to the tempfile subsystem
  21 *   and its storage must remain valid throughout the life of the
  22 *   program (i.e. you cannot use an on-stack variable to hold this
  23 *   structure).
  24 *
  25 * * Attempts to create a temporary file by calling
  26 *   `create_tempfile()`.
  27 *
  28 * * Writes new content to the file by either:
  29 *
  30 *   * writing to the file descriptor returned by `create_tempfile()`
  31 *     (also available via `tempfile->fd`).
  32 *
  33 *   * calling `fdopen_tempfile()` to get a `FILE` pointer for the
  34 *     open file and writing to the file using stdio.
  35 *
  36 * When finished writing, the caller can:
  37 *
  38 * * Close the file descriptor and remove the temporary file by
  39 *   calling `delete_tempfile()`.
  40 *
  41 * * Close the temporary file and rename it atomically to a specified
  42 *   filename by calling `rename_tempfile()`. This relinquishes
  43 *   control of the file.
  44 *
  45 * * Close the file descriptor without removing or renaming the
  46 *   temporary file by calling `close_tempfile()`, and later call
  47 *   `delete_tempfile()` or `rename_tempfile()`.
  48 *
  49 * Even after the temporary file is renamed or deleted, the `tempfile`
  50 * object must not be freed or altered by the caller. However, it may
  51 * be reused; just pass it to another call of `create_tempfile()`.
  52 *
  53 * If the program exits before `rename_tempfile()` or
  54 * `delete_tempfile()` is called, an `atexit(3)` handler will close
  55 * and remove the temporary file.
  56 *
  57 * If you need to close the file descriptor yourself, do so by calling
  58 * `close_tempfile()`. You should never call `close(2)` or `fclose(3)`
  59 * yourself, otherwise the `struct tempfile` structure would still
  60 * think that the file descriptor needs to be closed, and a later
  61 * cleanup would result in duplicate calls to `close(2)`. Worse yet,
  62 * if you close and then later open another file descriptor for a
  63 * completely different purpose, then the unrelated file descriptor
  64 * might get closed.
  65 *
  66 *
  67 * Error handling
  68 * --------------
  69 *
  70 * `create_tempfile()` returns a file descriptor on success or -1 on
  71 * failure. On errors, `errno` describes the reason for failure.
  72 *
  73 * `delete_tempfile()`, `rename_tempfile()`, and `close_tempfile()`
  74 * return 0 on success. On failure they set `errno` appropriately, do
  75 * their best to delete the temporary file, and return -1.
  76 */
  77
  78struct tempfile {
  79        struct tempfile *volatile next;
  80        volatile sig_atomic_t active;
  81        volatile int fd;
  82        FILE *volatile fp;
  83        volatile pid_t owner;
  84        char on_list;
  85        struct strbuf filename;
  86};
  87
  88/*
  89 * Attempt to create a temporary file at the specified `path`. Return
  90 * a file descriptor for writing to it, or -1 on error. It is an error
  91 * if a file already exists at that path.
  92 */
  93extern int create_tempfile(struct tempfile *tempfile, const char *path);
  94
  95/*
  96 * Associate a stdio stream with the temporary file (which must still
  97 * be open). Return `NULL` (*without* deleting the file) on error. The
  98 * stream is closed automatically when `close_tempfile()` is called or
  99 * when the file is deleted or renamed.
 100 */
 101extern FILE *fdopen_tempfile(struct tempfile *tempfile, const char *mode);
 102
 103static inline int is_tempfile_active(struct tempfile *tempfile)
 104{
 105        return tempfile->active;
 106}
 107
 108/*
 109 * Return the path of the lockfile. The return value is a pointer to a
 110 * field within the lock_file object and should not be freed.
 111 */
 112extern const char *get_tempfile_path(struct tempfile *tempfile);
 113
 114extern int get_tempfile_fd(struct tempfile *tempfile);
 115extern FILE *get_tempfile_fp(struct tempfile *tempfile);
 116
 117/*
 118 * If the temporary file is still open, close it (and the file pointer
 119 * too, if it has been opened using `fdopen_tempfile()`) without
 120 * deleting the file. Return 0 upon success. On failure to `close(2)`,
 121 * return a negative value and delete the file. Usually
 122 * `delete_tempfile()` or `rename_tempfile()` should eventually be
 123 * called if `close_tempfile()` succeeds.
 124 */
 125extern int close_tempfile(struct tempfile *tempfile);
 126
 127/*
 128 * Re-open a temporary file that has been closed using
 129 * `close_tempfile()` but not yet deleted or renamed. This can be used
 130 * to implement a sequence of operations like the following:
 131 *
 132 * * Create temporary file.
 133 *
 134 * * Write new contents to file, then `close_tempfile()` to cause the
 135 *   contents to be written to disk.
 136 *
 137 * * Pass the name of the temporary file to another program to allow
 138 *   it (and nobody else) to inspect or even modify the file's
 139 *   contents.
 140 *
 141 * * `reopen_tempfile()` to reopen the temporary file. Make further
 142 *   updates to the contents.
 143 *
 144 * * `rename_tempfile()` to move the file to its permanent location.
 145 */
 146extern int reopen_tempfile(struct tempfile *tempfile);
 147
 148/*
 149 * Close the file descriptor and/or file pointer and remove the
 150 * temporary file associated with `tempfile`. It is a NOOP to call
 151 * `delete_tempfile()` for a `tempfile` object that has already been
 152 * deleted or renamed.
 153 */
 154extern void delete_tempfile(struct tempfile *tempfile);
 155
 156/*
 157 * Close the file descriptor and/or file pointer if they are still
 158 * open, and atomically rename the temporary file to `path`. `path`
 159 * must be on the same filesystem as the lock file. Return 0 on
 160 * success. On failure, delete the temporary file and return -1, with
 161 * `errno` set to the value from the failing call to `close(2)` or
 162 * `rename(2)`. It is a bug to call `rename_tempfile()` for a
 163 * `tempfile` object that is not currently active.
 164 */
 165extern int rename_tempfile(struct tempfile *tempfile, const char *path);
 166
 167#endif /* TEMPFILE_H */