tempfile.hon commit Merge branch 'jc/utf8-fprintf' into maint (127c13a)
   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 *   Note that the file descriptor returned by create_tempfile()
  37 *   is marked O_CLOEXEC, so the new contents must be written by
  38 *   the current process, not any spawned one.
  39 *
  40 * When finished writing, the caller can:
  41 *
  42 * * Close the file descriptor and remove the temporary file by
  43 *   calling `delete_tempfile()`.
  44 *
  45 * * Close the temporary file and rename it atomically to a specified
  46 *   filename by calling `rename_tempfile()`. This relinquishes
  47 *   control of the file.
  48 *
  49 * * Close the file descriptor without removing or renaming the
  50 *   temporary file by calling `close_tempfile()`, and later call
  51 *   `delete_tempfile()` or `rename_tempfile()`.
  52 *
  53 * Even after the temporary file is renamed or deleted, the `tempfile`
  54 * object must not be freed or altered by the caller. However, it may
  55 * be reused; just pass it to another call of `create_tempfile()`.
  56 *
  57 * If the program exits before `rename_tempfile()` or
  58 * `delete_tempfile()` is called, an `atexit(3)` handler will close
  59 * and remove the temporary file.
  60 *
  61 * If you need to close the file descriptor yourself, do so by calling
  62 * `close_tempfile()`. You should never call `close(2)` or `fclose(3)`
  63 * yourself, otherwise the `struct tempfile` structure would still
  64 * think that the file descriptor needs to be closed, and a later
  65 * cleanup would result in duplicate calls to `close(2)`. Worse yet,
  66 * if you close and then later open another file descriptor for a
  67 * completely different purpose, then the unrelated file descriptor
  68 * might get closed.
  69 *
  70 *
  71 * Error handling
  72 * --------------
  73 *
  74 * `create_tempfile()` returns a file descriptor on success or -1 on
  75 * failure. On errors, `errno` describes the reason for failure.
  76 *
  77 * `delete_tempfile()`, `rename_tempfile()`, and `close_tempfile()`
  78 * return 0 on success. On failure they set `errno` appropriately, do
  79 * their best to delete the temporary file, and return -1.
  80 */
  81
  82struct tempfile {
  83        struct tempfile *volatile next;
  84        volatile sig_atomic_t active;
  85        volatile int fd;
  86        FILE *volatile fp;
  87        volatile pid_t owner;
  88        char on_list;
  89        struct strbuf filename;
  90};
  91
  92/*
  93 * Attempt to create a temporary file at the specified `path`. Return
  94 * a file descriptor for writing to it, or -1 on error. It is an error
  95 * if a file already exists at that path.
  96 */
  97extern int create_tempfile(struct tempfile *tempfile, const char *path);
  98
  99/*
 100 * Register an existing file as a tempfile, meaning that it will be
 101 * deleted when the program exits. The tempfile is considered closed,
 102 * but it can be worked with like any other closed tempfile (for
 103 * example, it can be opened using reopen_tempfile()).
 104 */
 105extern void register_tempfile(struct tempfile *tempfile, const char *path);
 106
 107
 108/*
 109 * mks_tempfile functions
 110 *
 111 * The following functions attempt to create and open temporary files
 112 * with names derived automatically from a template, in the manner of
 113 * mkstemps(), and arrange for them to be deleted if the program ends
 114 * before they are deleted explicitly. There is a whole family of such
 115 * functions, named according to the following pattern:
 116 *
 117 *     x?mks_tempfile_t?s?m?()
 118 *
 119 * The optional letters have the following meanings:
 120 *
 121 *   x - die if the temporary file cannot be created.
 122 *
 123 *   t - create the temporary file under $TMPDIR (as opposed to
 124 *       relative to the current directory). When these variants are
 125 *       used, template should be the pattern for the filename alone,
 126 *       without a path.
 127 *
 128 *   s - template includes a suffix that is suffixlen characters long.
 129 *
 130 *   m - the temporary file should be created with the specified mode
 131 *       (otherwise, the mode is set to 0600).
 132 *
 133 * None of these functions modify template. If the caller wants to
 134 * know the (absolute) path of the file that was created, it can be
 135 * read from tempfile->filename.
 136 *
 137 * On success, the functions return a file descriptor that is open for
 138 * writing the temporary file. On errors, they return -1 and set errno
 139 * appropriately (except for the "x" variants, which die() on errors).
 140 */
 141
 142/* See "mks_tempfile functions" above. */
 143extern int mks_tempfile_sm(struct tempfile *tempfile,
 144                           const char *template, int suffixlen, int mode);
 145
 146/* See "mks_tempfile functions" above. */
 147static inline int mks_tempfile_s(struct tempfile *tempfile,
 148                                 const char *template, int suffixlen)
 149{
 150        return mks_tempfile_sm(tempfile, template, suffixlen, 0600);
 151}
 152
 153/* See "mks_tempfile functions" above. */
 154static inline int mks_tempfile_m(struct tempfile *tempfile,
 155                                 const char *template, int mode)
 156{
 157        return mks_tempfile_sm(tempfile, template, 0, mode);
 158}
 159
 160/* See "mks_tempfile functions" above. */
 161static inline int mks_tempfile(struct tempfile *tempfile,
 162                               const char *template)
 163{
 164        return mks_tempfile_sm(tempfile, template, 0, 0600);
 165}
 166
 167/* See "mks_tempfile functions" above. */
 168extern int mks_tempfile_tsm(struct tempfile *tempfile,
 169                            const char *template, int suffixlen, int mode);
 170
 171/* See "mks_tempfile functions" above. */
 172static inline int mks_tempfile_ts(struct tempfile *tempfile,
 173                                  const char *template, int suffixlen)
 174{
 175        return mks_tempfile_tsm(tempfile, template, suffixlen, 0600);
 176}
 177
 178/* See "mks_tempfile functions" above. */
 179static inline int mks_tempfile_tm(struct tempfile *tempfile,
 180                                  const char *template, int mode)
 181{
 182        return mks_tempfile_tsm(tempfile, template, 0, mode);
 183}
 184
 185/* See "mks_tempfile functions" above. */
 186static inline int mks_tempfile_t(struct tempfile *tempfile,
 187                                 const char *template)
 188{
 189        return mks_tempfile_tsm(tempfile, template, 0, 0600);
 190}
 191
 192/* See "mks_tempfile functions" above. */
 193extern int xmks_tempfile_m(struct tempfile *tempfile,
 194                           const char *template, int mode);
 195
 196/* See "mks_tempfile functions" above. */
 197static inline int xmks_tempfile(struct tempfile *tempfile,
 198                                const char *template)
 199{
 200        return xmks_tempfile_m(tempfile, template, 0600);
 201}
 202
 203/*
 204 * Associate a stdio stream with the temporary file (which must still
 205 * be open). Return `NULL` (*without* deleting the file) on error. The
 206 * stream is closed automatically when `close_tempfile()` is called or
 207 * when the file is deleted or renamed.
 208 */
 209extern FILE *fdopen_tempfile(struct tempfile *tempfile, const char *mode);
 210
 211static inline int is_tempfile_active(struct tempfile *tempfile)
 212{
 213        return tempfile->active;
 214}
 215
 216/*
 217 * Return the path of the lockfile. The return value is a pointer to a
 218 * field within the lock_file object and should not be freed.
 219 */
 220extern const char *get_tempfile_path(struct tempfile *tempfile);
 221
 222extern int get_tempfile_fd(struct tempfile *tempfile);
 223extern FILE *get_tempfile_fp(struct tempfile *tempfile);
 224
 225/*
 226 * If the temporary file is still open, close it (and the file pointer
 227 * too, if it has been opened using `fdopen_tempfile()`) without
 228 * deleting the file. Return 0 upon success. On failure to `close(2)`,
 229 * return a negative value and delete the file. Usually
 230 * `delete_tempfile()` or `rename_tempfile()` should eventually be
 231 * called if `close_tempfile()` succeeds.
 232 */
 233extern int close_tempfile(struct tempfile *tempfile);
 234
 235/*
 236 * Re-open a temporary file that has been closed using
 237 * `close_tempfile()` but not yet deleted or renamed. This can be used
 238 * to implement a sequence of operations like the following:
 239 *
 240 * * Create temporary file.
 241 *
 242 * * Write new contents to file, then `close_tempfile()` to cause the
 243 *   contents to be written to disk.
 244 *
 245 * * Pass the name of the temporary file to another program to allow
 246 *   it (and nobody else) to inspect or even modify the file's
 247 *   contents.
 248 *
 249 * * `reopen_tempfile()` to reopen the temporary file. Make further
 250 *   updates to the contents.
 251 *
 252 * * `rename_tempfile()` to move the file to its permanent location.
 253 */
 254extern int reopen_tempfile(struct tempfile *tempfile);
 255
 256/*
 257 * Close the file descriptor and/or file pointer and remove the
 258 * temporary file associated with `tempfile`. It is a NOOP to call
 259 * `delete_tempfile()` for a `tempfile` object that has already been
 260 * deleted or renamed.
 261 */
 262extern void delete_tempfile(struct tempfile *tempfile);
 263
 264/*
 265 * Close the file descriptor and/or file pointer if they are still
 266 * open, and atomically rename the temporary file to `path`. `path`
 267 * must be on the same filesystem as the lock file. Return 0 on
 268 * success. On failure, delete the temporary file and return -1, with
 269 * `errno` set to the value from the failing call to `close(2)` or
 270 * `rename(2)`. It is a bug to call `rename_tempfile()` for a
 271 * `tempfile` object that is not currently active.
 272 */
 273extern int rename_tempfile(struct tempfile *tempfile, const char *path);
 274
 275#endif /* TEMPFILE_H */