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