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_gently()`, 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_gently()`. 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_gently()` 78 * return 0 on success. On failure they set `errno` appropriately and return 79 * -1. `delete` and `rename` (but not `close`) do their best to delete the 80 * temporary file before returning. 81 */ 82 83struct tempfile { 84struct tempfile *volatile next; 85volatilesig_atomic_t active; 86volatileint fd; 87FILE*volatile fp; 88volatile pid_t owner; 89char on_list; 90struct strbuf filename; 91}; 92 93/* 94 * Attempt to create a temporary file at the specified `path`. Return 95 * a file descriptor for writing to it, or -1 on error. It is an error 96 * if a file already exists at that path. 97 */ 98externintcreate_tempfile(struct tempfile *tempfile,const char*path); 99 100/* 101 * Register an existing file as a tempfile, meaning that it will be 102 * deleted when the program exits. The tempfile is considered closed, 103 * but it can be worked with like any other closed tempfile (for 104 * example, it can be opened using reopen_tempfile()). 105 */ 106externvoidregister_tempfile(struct tempfile *tempfile,const char*path); 107 108 109/* 110 * mks_tempfile functions 111 * 112 * The following functions attempt to create and open temporary files 113 * with names derived automatically from a template, in the manner of 114 * mkstemps(), and arrange for them to be deleted if the program ends 115 * before they are deleted explicitly. There is a whole family of such 116 * functions, named according to the following pattern: 117 * 118 * x?mks_tempfile_t?s?m?() 119 * 120 * The optional letters have the following meanings: 121 * 122 * x - die if the temporary file cannot be created. 123 * 124 * t - create the temporary file under $TMPDIR (as opposed to 125 * relative to the current directory). When these variants are 126 * used, template should be the pattern for the filename alone, 127 * without a path. 128 * 129 * s - template includes a suffix that is suffixlen characters long. 130 * 131 * m - the temporary file should be created with the specified mode 132 * (otherwise, the mode is set to 0600). 133 * 134 * None of these functions modify template. If the caller wants to 135 * know the (absolute) path of the file that was created, it can be 136 * read from tempfile->filename. 137 * 138 * On success, the functions return a file descriptor that is open for 139 * writing the temporary file. On errors, they return -1 and set errno 140 * appropriately (except for the "x" variants, which die() on errors). 141 */ 142 143/* See "mks_tempfile functions" above. */ 144externintmks_tempfile_sm(struct tempfile *tempfile, 145const char*template,int suffixlen,int mode); 146 147/* See "mks_tempfile functions" above. */ 148staticinlineintmks_tempfile_s(struct tempfile *tempfile, 149const char*template,int suffixlen) 150{ 151returnmks_tempfile_sm(tempfile,template, suffixlen,0600); 152} 153 154/* See "mks_tempfile functions" above. */ 155staticinlineintmks_tempfile_m(struct tempfile *tempfile, 156const char*template,int mode) 157{ 158returnmks_tempfile_sm(tempfile,template,0, mode); 159} 160 161/* See "mks_tempfile functions" above. */ 162staticinlineintmks_tempfile(struct tempfile *tempfile, 163const char*template) 164{ 165returnmks_tempfile_sm(tempfile,template,0,0600); 166} 167 168/* See "mks_tempfile functions" above. */ 169externintmks_tempfile_tsm(struct tempfile *tempfile, 170const char*template,int suffixlen,int mode); 171 172/* See "mks_tempfile functions" above. */ 173staticinlineintmks_tempfile_ts(struct tempfile *tempfile, 174const char*template,int suffixlen) 175{ 176returnmks_tempfile_tsm(tempfile,template, suffixlen,0600); 177} 178 179/* See "mks_tempfile functions" above. */ 180staticinlineintmks_tempfile_tm(struct tempfile *tempfile, 181const char*template,int mode) 182{ 183returnmks_tempfile_tsm(tempfile,template,0, mode); 184} 185 186/* See "mks_tempfile functions" above. */ 187staticinlineintmks_tempfile_t(struct tempfile *tempfile, 188const char*template) 189{ 190returnmks_tempfile_tsm(tempfile,template,0,0600); 191} 192 193/* See "mks_tempfile functions" above. */ 194externintxmks_tempfile_m(struct tempfile *tempfile, 195const char*template,int mode); 196 197/* See "mks_tempfile functions" above. */ 198staticinlineintxmks_tempfile(struct tempfile *tempfile, 199const char*template) 200{ 201returnxmks_tempfile_m(tempfile,template,0600); 202} 203 204/* 205 * Associate a stdio stream with the temporary file (which must still 206 * be open). Return `NULL` (*without* deleting the file) on error. The 207 * stream is closed automatically when `close_tempfile_gently()` is called or 208 * when the file is deleted or renamed. 209 */ 210externFILE*fdopen_tempfile(struct tempfile *tempfile,const char*mode); 211 212staticinlineintis_tempfile_active(struct tempfile *tempfile) 213{ 214return tempfile && tempfile->active; 215} 216 217/* 218 * Return the path of the lockfile. The return value is a pointer to a 219 * field within the lock_file object and should not be freed. 220 */ 221externconst char*get_tempfile_path(struct tempfile *tempfile); 222 223externintget_tempfile_fd(struct tempfile *tempfile); 224externFILE*get_tempfile_fp(struct tempfile *tempfile); 225 226/* 227 * If the temporary file is still open, close it (and the file pointer 228 * too, if it has been opened using `fdopen_tempfile()`) without 229 * deleting the file. Return 0 upon success. On failure to `close(2)`, 230 * return a negative value. Usually `delete_tempfile()` or `rename_tempfile()` 231 * should eventually be called regardless of whether `close_tempfile_gently()` 232 * succeeds. 233 */ 234externintclose_tempfile_gently(struct tempfile *tempfile); 235 236/* 237 * Re-open a temporary file that has been closed using 238 * `close_tempfile_gently()` but not yet deleted or renamed. This can be used 239 * to implement a sequence of operations like the following: 240 * 241 * * Create temporary file. 242 * 243 * * Write new contents to file, then `close_tempfile_gently()` to cause the 244 * contents to be written to disk. 245 * 246 * * Pass the name of the temporary file to another program to allow 247 * it (and nobody else) to inspect or even modify the file's 248 * contents. 249 * 250 * * `reopen_tempfile()` to reopen the temporary file. Make further 251 * updates to the contents. 252 * 253 * * `rename_tempfile()` to move the file to its permanent location. 254 */ 255externintreopen_tempfile(struct tempfile *tempfile); 256 257/* 258 * Close the file descriptor and/or file pointer and remove the 259 * temporary file associated with `tempfile`. It is a NOOP to call 260 * `delete_tempfile()` for a `tempfile` object that has already been 261 * deleted or renamed. 262 */ 263externvoiddelete_tempfile(struct tempfile *tempfile); 264 265/* 266 * Close the file descriptor and/or file pointer if they are still 267 * open, and atomically rename the temporary file to `path`. `path` 268 * must be on the same filesystem as the lock file. Return 0 on 269 * success. On failure, delete the temporary file and return -1, with 270 * `errno` set to the value from the failing call to `close(2)` or 271 * `rename(2)`. It is a bug to call `rename_tempfile()` for a 272 * `tempfile` object that is not currently active. 273 */ 274externintrename_tempfile(struct tempfile *tempfile,const char*path); 275 276#endif/* TEMPFILE_H */