Michael Haggerty | 1a9d15d | 2015-08-10 11:47:41 +0200 | [diff] [blame] | 1 | /* |
| 2 | * State diagram and cleanup |
| 3 | * ------------------------- |
| 4 | * |
| 5 | * If the program exits while a temporary file is active, we want to |
| 6 | * make sure that we remove it. This is done by remembering the active |
| 7 | * temporary files in a linked list, `tempfile_list`. An `atexit(3)` |
| 8 | * handler and a signal handler are registered, to clean up any active |
| 9 | * temporary files. |
| 10 | * |
| 11 | * Because the signal handler can run at any time, `tempfile_list` and |
| 12 | * the `tempfile` objects that comprise it must be kept in |
| 13 | * self-consistent states at all times. |
| 14 | * |
| 15 | * The possible states of a `tempfile` object are as follows: |
| 16 | * |
| 17 | * - Uninitialized. In this state the object's `on_list` field must be |
| 18 | * zero but the rest of its contents need not be initialized. As |
| 19 | * soon as the object is used in any way, it is irrevocably |
| 20 | * registered in `tempfile_list`, and `on_list` is set. |
| 21 | * |
| 22 | * - Active, file open (after `create_tempfile()` or |
| 23 | * `reopen_tempfile()`). In this state: |
| 24 | * |
| 25 | * - the temporary file exists |
| 26 | * - `active` is set |
| 27 | * - `filename` holds the filename of the temporary file |
| 28 | * - `fd` holds a file descriptor open for writing to it |
| 29 | * - `fp` holds a pointer to an open `FILE` object if and only if |
| 30 | * `fdopen_tempfile()` has been called on the object |
| 31 | * - `owner` holds the PID of the process that created the file |
| 32 | * |
Jeff King | 49bd0fc | 2017-09-05 08:14:30 -0400 | [diff] [blame] | 33 | * - Active, file closed (after `close_tempfile_gently()`). Same |
Michael Haggerty | 1a9d15d | 2015-08-10 11:47:41 +0200 | [diff] [blame] | 34 | * as the previous state, except that the temporary file is closed, |
| 35 | * `fd` is -1, and `fp` is `NULL`. |
| 36 | * |
Jeff King | 49bd0fc | 2017-09-05 08:14:30 -0400 | [diff] [blame] | 37 | * - Inactive (after `delete_tempfile()`, `rename_tempfile()`, or a |
| 38 | * failed attempt to create a temporary file). In this state: |
Michael Haggerty | 1a9d15d | 2015-08-10 11:47:41 +0200 | [diff] [blame] | 39 | * |
| 40 | * - `active` is unset |
| 41 | * - `filename` is empty (usually, though there are transitory |
| 42 | * states in which this condition doesn't hold). Client code should |
| 43 | * *not* rely on the filename being empty in this state. |
| 44 | * - `fd` is -1 and `fp` is `NULL` |
Jeff King | 422a21c | 2017-09-05 08:15:04 -0400 | [diff] [blame] | 45 | * - the object is removed from `tempfile_list` (but could be used again) |
Michael Haggerty | 1a9d15d | 2015-08-10 11:47:41 +0200 | [diff] [blame] | 46 | * |
| 47 | * A temporary file is owned by the process that created it. The |
| 48 | * `tempfile` has an `owner` field that records the owner's PID. This |
| 49 | * field is used to prevent a forked process from deleting a temporary |
| 50 | * file created by its parent. |
| 51 | */ |
| 52 | |
| 53 | #include "cache.h" |
| 54 | #include "tempfile.h" |
| 55 | #include "sigchain.h" |
| 56 | |
Jeff King | 24d8218 | 2017-09-05 08:15:00 -0400 | [diff] [blame] | 57 | static VOLATILE_LIST_HEAD(tempfile_list); |
Michael Haggerty | 1a9d15d | 2015-08-10 11:47:41 +0200 | [diff] [blame] | 58 | |
Jeff King | 6b93506 | 2017-09-05 08:14:53 -0400 | [diff] [blame] | 59 | static void remove_tempfiles(int in_signal_handler) |
Michael Haggerty | 1a9d15d | 2015-08-10 11:47:41 +0200 | [diff] [blame] | 60 | { |
| 61 | pid_t me = getpid(); |
Jeff King | 24d8218 | 2017-09-05 08:15:00 -0400 | [diff] [blame] | 62 | volatile struct volatile_list_head *pos; |
Michael Haggerty | 1a9d15d | 2015-08-10 11:47:41 +0200 | [diff] [blame] | 63 | |
Jeff King | 24d8218 | 2017-09-05 08:15:00 -0400 | [diff] [blame] | 64 | list_for_each(pos, &tempfile_list) { |
| 65 | struct tempfile *p = list_entry(pos, struct tempfile, list); |
| 66 | |
Jeff King | 6b93506 | 2017-09-05 08:14:53 -0400 | [diff] [blame] | 67 | if (!is_tempfile_active(p) || p->owner != me) |
| 68 | continue; |
| 69 | |
| 70 | if (p->fd >= 0) |
| 71 | close(p->fd); |
| 72 | |
| 73 | if (in_signal_handler) |
| 74 | unlink(p->filename.buf); |
| 75 | else |
| 76 | unlink_or_warn(p->filename.buf); |
| 77 | |
| 78 | p->active = 0; |
Michael Haggerty | 1a9d15d | 2015-08-10 11:47:41 +0200 | [diff] [blame] | 79 | } |
| 80 | } |
| 81 | |
| 82 | static void remove_tempfiles_on_exit(void) |
| 83 | { |
| 84 | remove_tempfiles(0); |
| 85 | } |
| 86 | |
| 87 | static void remove_tempfiles_on_signal(int signo) |
| 88 | { |
| 89 | remove_tempfiles(1); |
| 90 | sigchain_pop(signo); |
| 91 | raise(signo); |
| 92 | } |
| 93 | |
Jeff King | 076aa2c | 2017-09-05 08:15:08 -0400 | [diff] [blame] | 94 | static struct tempfile *new_tempfile(void) |
Michael Haggerty | 1a9d15d | 2015-08-10 11:47:41 +0200 | [diff] [blame] | 95 | { |
Jeff King | 076aa2c | 2017-09-05 08:15:08 -0400 | [diff] [blame] | 96 | struct tempfile *tempfile = xmalloc(sizeof(*tempfile)); |
Jeff King | 422a21c | 2017-09-05 08:15:04 -0400 | [diff] [blame] | 97 | tempfile->fd = -1; |
| 98 | tempfile->fp = NULL; |
| 99 | tempfile->active = 0; |
| 100 | tempfile->owner = 0; |
| 101 | INIT_LIST_HEAD(&tempfile->list); |
| 102 | strbuf_init(&tempfile->filename, 0); |
Jeff King | 076aa2c | 2017-09-05 08:15:08 -0400 | [diff] [blame] | 103 | return tempfile; |
Michael Haggerty | 7eba6ce | 2015-08-10 11:47:42 +0200 | [diff] [blame] | 104 | } |
| 105 | |
Jeff King | 2933ebb | 2017-09-05 08:14:47 -0400 | [diff] [blame] | 106 | static void activate_tempfile(struct tempfile *tempfile) |
| 107 | { |
Jeff King | 422a21c | 2017-09-05 08:15:04 -0400 | [diff] [blame] | 108 | static int initialized; |
| 109 | |
| 110 | if (is_tempfile_active(tempfile)) |
| 111 | BUG("activate_tempfile called for active object"); |
| 112 | |
| 113 | if (!initialized) { |
| 114 | sigchain_push_common(remove_tempfiles_on_signal); |
| 115 | atexit(remove_tempfiles_on_exit); |
| 116 | initialized = 1; |
| 117 | } |
| 118 | |
| 119 | volatile_list_add(&tempfile->list, &tempfile_list); |
Jeff King | 2933ebb | 2017-09-05 08:14:47 -0400 | [diff] [blame] | 120 | tempfile->owner = getpid(); |
| 121 | tempfile->active = 1; |
| 122 | } |
| 123 | |
Jeff King | b5f4dcb | 2017-09-05 08:14:50 -0400 | [diff] [blame] | 124 | static void deactivate_tempfile(struct tempfile *tempfile) |
| 125 | { |
| 126 | tempfile->active = 0; |
Jeff King | 102cf7a | 2017-09-05 08:14:56 -0400 | [diff] [blame] | 127 | strbuf_release(&tempfile->filename); |
Jeff King | 422a21c | 2017-09-05 08:15:04 -0400 | [diff] [blame] | 128 | volatile_list_del(&tempfile->list); |
Jeff King | 076aa2c | 2017-09-05 08:15:08 -0400 | [diff] [blame] | 129 | free(tempfile); |
Jeff King | b5f4dcb | 2017-09-05 08:14:50 -0400 | [diff] [blame] | 130 | } |
| 131 | |
Michael Haggerty | 7eba6ce | 2015-08-10 11:47:42 +0200 | [diff] [blame] | 132 | /* Make sure errno contains a meaningful value on error */ |
Taylor Blau | bef0413 | 2020-04-27 10:27:54 -0600 | [diff] [blame] | 133 | struct tempfile *create_tempfile_mode(const char *path, int mode) |
Michael Haggerty | 7eba6ce | 2015-08-10 11:47:42 +0200 | [diff] [blame] | 134 | { |
Jeff King | 076aa2c | 2017-09-05 08:15:08 -0400 | [diff] [blame] | 135 | struct tempfile *tempfile = new_tempfile(); |
Michael Haggerty | 1a9d15d | 2015-08-10 11:47:41 +0200 | [diff] [blame] | 136 | |
| 137 | strbuf_add_absolute_path(&tempfile->filename, path); |
Ben Wijen | 05d1ed6 | 2016-08-22 14:47:55 +0200 | [diff] [blame] | 138 | tempfile->fd = open(tempfile->filename.buf, |
Taylor Blau | bef0413 | 2020-04-27 10:27:54 -0600 | [diff] [blame] | 139 | O_RDWR | O_CREAT | O_EXCL | O_CLOEXEC, mode); |
Ben Wijen | 05d1ed6 | 2016-08-22 14:47:55 +0200 | [diff] [blame] | 140 | if (O_CLOEXEC && tempfile->fd < 0 && errno == EINVAL) |
| 141 | /* Try again w/o O_CLOEXEC: the kernel might not support it */ |
| 142 | tempfile->fd = open(tempfile->filename.buf, |
Taylor Blau | bef0413 | 2020-04-27 10:27:54 -0600 | [diff] [blame] | 143 | O_RDWR | O_CREAT | O_EXCL, mode); |
Michael Haggerty | 1a9d15d | 2015-08-10 11:47:41 +0200 | [diff] [blame] | 144 | if (tempfile->fd < 0) { |
Jeff King | b5f4dcb | 2017-09-05 08:14:50 -0400 | [diff] [blame] | 145 | deactivate_tempfile(tempfile); |
Jeff King | 076aa2c | 2017-09-05 08:15:08 -0400 | [diff] [blame] | 146 | return NULL; |
Michael Haggerty | 1a9d15d | 2015-08-10 11:47:41 +0200 | [diff] [blame] | 147 | } |
Jeff King | 2933ebb | 2017-09-05 08:14:47 -0400 | [diff] [blame] | 148 | activate_tempfile(tempfile); |
Michael Haggerty | 1a9d15d | 2015-08-10 11:47:41 +0200 | [diff] [blame] | 149 | if (adjust_shared_perm(tempfile->filename.buf)) { |
| 150 | int save_errno = errno; |
| 151 | error("cannot fix permission bits on %s", tempfile->filename.buf); |
Jeff King | 076aa2c | 2017-09-05 08:15:08 -0400 | [diff] [blame] | 152 | delete_tempfile(&tempfile); |
Michael Haggerty | 1a9d15d | 2015-08-10 11:47:41 +0200 | [diff] [blame] | 153 | errno = save_errno; |
Jeff King | 076aa2c | 2017-09-05 08:15:08 -0400 | [diff] [blame] | 154 | return NULL; |
Michael Haggerty | 1a9d15d | 2015-08-10 11:47:41 +0200 | [diff] [blame] | 155 | } |
Jeff King | 076aa2c | 2017-09-05 08:15:08 -0400 | [diff] [blame] | 156 | |
| 157 | return tempfile; |
Michael Haggerty | 1a9d15d | 2015-08-10 11:47:41 +0200 | [diff] [blame] | 158 | } |
| 159 | |
Jeff King | 076aa2c | 2017-09-05 08:15:08 -0400 | [diff] [blame] | 160 | struct tempfile *register_tempfile(const char *path) |
Michael Haggerty | 9939715 | 2015-08-10 11:47:44 +0200 | [diff] [blame] | 161 | { |
Jeff King | 076aa2c | 2017-09-05 08:15:08 -0400 | [diff] [blame] | 162 | struct tempfile *tempfile = new_tempfile(); |
Michael Haggerty | 9939715 | 2015-08-10 11:47:44 +0200 | [diff] [blame] | 163 | strbuf_add_absolute_path(&tempfile->filename, path); |
Jeff King | 2933ebb | 2017-09-05 08:14:47 -0400 | [diff] [blame] | 164 | activate_tempfile(tempfile); |
Jeff King | 076aa2c | 2017-09-05 08:15:08 -0400 | [diff] [blame] | 165 | return tempfile; |
Michael Haggerty | 9939715 | 2015-08-10 11:47:44 +0200 | [diff] [blame] | 166 | } |
| 167 | |
Brandon Williams | ea8ace4 | 2018-02-14 10:59:57 -0800 | [diff] [blame] | 168 | struct tempfile *mks_tempfile_sm(const char *filename_template, int suffixlen, int mode) |
Michael Haggerty | 354ab11 | 2015-08-10 11:47:43 +0200 | [diff] [blame] | 169 | { |
Jeff King | 076aa2c | 2017-09-05 08:15:08 -0400 | [diff] [blame] | 170 | struct tempfile *tempfile = new_tempfile(); |
Michael Haggerty | 354ab11 | 2015-08-10 11:47:43 +0200 | [diff] [blame] | 171 | |
Brandon Williams | ea8ace4 | 2018-02-14 10:59:57 -0800 | [diff] [blame] | 172 | strbuf_add_absolute_path(&tempfile->filename, filename_template); |
Michael Haggerty | 354ab11 | 2015-08-10 11:47:43 +0200 | [diff] [blame] | 173 | tempfile->fd = git_mkstemps_mode(tempfile->filename.buf, suffixlen, mode); |
| 174 | if (tempfile->fd < 0) { |
Jeff King | b5f4dcb | 2017-09-05 08:14:50 -0400 | [diff] [blame] | 175 | deactivate_tempfile(tempfile); |
Jeff King | 076aa2c | 2017-09-05 08:15:08 -0400 | [diff] [blame] | 176 | return NULL; |
Michael Haggerty | 354ab11 | 2015-08-10 11:47:43 +0200 | [diff] [blame] | 177 | } |
Jeff King | 2933ebb | 2017-09-05 08:14:47 -0400 | [diff] [blame] | 178 | activate_tempfile(tempfile); |
Jeff King | 076aa2c | 2017-09-05 08:15:08 -0400 | [diff] [blame] | 179 | return tempfile; |
Michael Haggerty | 354ab11 | 2015-08-10 11:47:43 +0200 | [diff] [blame] | 180 | } |
| 181 | |
Brandon Williams | ea8ace4 | 2018-02-14 10:59:57 -0800 | [diff] [blame] | 182 | struct tempfile *mks_tempfile_tsm(const char *filename_template, int suffixlen, int mode) |
Michael Haggerty | 354ab11 | 2015-08-10 11:47:43 +0200 | [diff] [blame] | 183 | { |
Jeff King | 076aa2c | 2017-09-05 08:15:08 -0400 | [diff] [blame] | 184 | struct tempfile *tempfile = new_tempfile(); |
Michael Haggerty | 354ab11 | 2015-08-10 11:47:43 +0200 | [diff] [blame] | 185 | const char *tmpdir; |
| 186 | |
Michael Haggerty | 354ab11 | 2015-08-10 11:47:43 +0200 | [diff] [blame] | 187 | tmpdir = getenv("TMPDIR"); |
| 188 | if (!tmpdir) |
| 189 | tmpdir = "/tmp"; |
| 190 | |
Brandon Williams | ea8ace4 | 2018-02-14 10:59:57 -0800 | [diff] [blame] | 191 | strbuf_addf(&tempfile->filename, "%s/%s", tmpdir, filename_template); |
Michael Haggerty | 354ab11 | 2015-08-10 11:47:43 +0200 | [diff] [blame] | 192 | tempfile->fd = git_mkstemps_mode(tempfile->filename.buf, suffixlen, mode); |
| 193 | if (tempfile->fd < 0) { |
Jeff King | b5f4dcb | 2017-09-05 08:14:50 -0400 | [diff] [blame] | 194 | deactivate_tempfile(tempfile); |
Jeff King | 076aa2c | 2017-09-05 08:15:08 -0400 | [diff] [blame] | 195 | return NULL; |
Michael Haggerty | 354ab11 | 2015-08-10 11:47:43 +0200 | [diff] [blame] | 196 | } |
Jeff King | 2933ebb | 2017-09-05 08:14:47 -0400 | [diff] [blame] | 197 | activate_tempfile(tempfile); |
Jeff King | 076aa2c | 2017-09-05 08:15:08 -0400 | [diff] [blame] | 198 | return tempfile; |
Michael Haggerty | 354ab11 | 2015-08-10 11:47:43 +0200 | [diff] [blame] | 199 | } |
| 200 | |
Brandon Williams | ea8ace4 | 2018-02-14 10:59:57 -0800 | [diff] [blame] | 201 | struct tempfile *xmks_tempfile_m(const char *filename_template, int mode) |
Michael Haggerty | 354ab11 | 2015-08-10 11:47:43 +0200 | [diff] [blame] | 202 | { |
Jeff King | 076aa2c | 2017-09-05 08:15:08 -0400 | [diff] [blame] | 203 | struct tempfile *tempfile; |
Michael Haggerty | 354ab11 | 2015-08-10 11:47:43 +0200 | [diff] [blame] | 204 | struct strbuf full_template = STRBUF_INIT; |
| 205 | |
Brandon Williams | ea8ace4 | 2018-02-14 10:59:57 -0800 | [diff] [blame] | 206 | strbuf_add_absolute_path(&full_template, filename_template); |
Jeff King | 076aa2c | 2017-09-05 08:15:08 -0400 | [diff] [blame] | 207 | tempfile = mks_tempfile_m(full_template.buf, mode); |
| 208 | if (!tempfile) |
Michael Haggerty | 354ab11 | 2015-08-10 11:47:43 +0200 | [diff] [blame] | 209 | die_errno("Unable to create temporary file '%s'", |
| 210 | full_template.buf); |
| 211 | |
| 212 | strbuf_release(&full_template); |
Jeff King | 076aa2c | 2017-09-05 08:15:08 -0400 | [diff] [blame] | 213 | return tempfile; |
Michael Haggerty | 354ab11 | 2015-08-10 11:47:43 +0200 | [diff] [blame] | 214 | } |
| 215 | |
Michael Haggerty | 1a9d15d | 2015-08-10 11:47:41 +0200 | [diff] [blame] | 216 | FILE *fdopen_tempfile(struct tempfile *tempfile, const char *mode) |
| 217 | { |
Jeff King | e6fc267 | 2017-09-05 08:14:36 -0400 | [diff] [blame] | 218 | if (!is_tempfile_active(tempfile)) |
Jeff King | 9b028aa | 2017-09-05 08:14:43 -0400 | [diff] [blame] | 219 | BUG("fdopen_tempfile() called for inactive object"); |
Michael Haggerty | 1a9d15d | 2015-08-10 11:47:41 +0200 | [diff] [blame] | 220 | if (tempfile->fp) |
Jeff King | 9b028aa | 2017-09-05 08:14:43 -0400 | [diff] [blame] | 221 | BUG("fdopen_tempfile() called for open object"); |
Michael Haggerty | 1a9d15d | 2015-08-10 11:47:41 +0200 | [diff] [blame] | 222 | |
| 223 | tempfile->fp = fdopen(tempfile->fd, mode); |
| 224 | return tempfile->fp; |
| 225 | } |
| 226 | |
| 227 | const char *get_tempfile_path(struct tempfile *tempfile) |
| 228 | { |
Jeff King | e6fc267 | 2017-09-05 08:14:36 -0400 | [diff] [blame] | 229 | if (!is_tempfile_active(tempfile)) |
Jeff King | 9b028aa | 2017-09-05 08:14:43 -0400 | [diff] [blame] | 230 | BUG("get_tempfile_path() called for inactive object"); |
Michael Haggerty | 1a9d15d | 2015-08-10 11:47:41 +0200 | [diff] [blame] | 231 | return tempfile->filename.buf; |
| 232 | } |
| 233 | |
| 234 | int get_tempfile_fd(struct tempfile *tempfile) |
| 235 | { |
Jeff King | e6fc267 | 2017-09-05 08:14:36 -0400 | [diff] [blame] | 236 | if (!is_tempfile_active(tempfile)) |
Jeff King | 9b028aa | 2017-09-05 08:14:43 -0400 | [diff] [blame] | 237 | BUG("get_tempfile_fd() called for inactive object"); |
Michael Haggerty | 1a9d15d | 2015-08-10 11:47:41 +0200 | [diff] [blame] | 238 | return tempfile->fd; |
| 239 | } |
| 240 | |
| 241 | FILE *get_tempfile_fp(struct tempfile *tempfile) |
| 242 | { |
Jeff King | e6fc267 | 2017-09-05 08:14:36 -0400 | [diff] [blame] | 243 | if (!is_tempfile_active(tempfile)) |
Jeff King | 9b028aa | 2017-09-05 08:14:43 -0400 | [diff] [blame] | 244 | BUG("get_tempfile_fp() called for inactive object"); |
Michael Haggerty | 1a9d15d | 2015-08-10 11:47:41 +0200 | [diff] [blame] | 245 | return tempfile->fp; |
| 246 | } |
| 247 | |
Jeff King | 49bd0fc | 2017-09-05 08:14:30 -0400 | [diff] [blame] | 248 | int close_tempfile_gently(struct tempfile *tempfile) |
Michael Haggerty | 1a9d15d | 2015-08-10 11:47:41 +0200 | [diff] [blame] | 249 | { |
Jeff King | f5b4dc7 | 2017-09-05 08:14:40 -0400 | [diff] [blame] | 250 | int fd; |
| 251 | FILE *fp; |
Michael Haggerty | 1a9d15d | 2015-08-10 11:47:41 +0200 | [diff] [blame] | 252 | int err; |
| 253 | |
Jeff King | f5b4dc7 | 2017-09-05 08:14:40 -0400 | [diff] [blame] | 254 | if (!is_tempfile_active(tempfile) || tempfile->fd < 0) |
Michael Haggerty | 1a9d15d | 2015-08-10 11:47:41 +0200 | [diff] [blame] | 255 | return 0; |
| 256 | |
Jeff King | f5b4dc7 | 2017-09-05 08:14:40 -0400 | [diff] [blame] | 257 | fd = tempfile->fd; |
| 258 | fp = tempfile->fp; |
Michael Haggerty | 1a9d15d | 2015-08-10 11:47:41 +0200 | [diff] [blame] | 259 | tempfile->fd = -1; |
| 260 | if (fp) { |
| 261 | tempfile->fp = NULL; |
Jeff King | 7e8c935 | 2017-02-17 16:07:49 -0500 | [diff] [blame] | 262 | if (ferror(fp)) { |
| 263 | err = -1; |
| 264 | if (!fclose(fp)) |
| 265 | errno = EIO; |
| 266 | } else { |
| 267 | err = fclose(fp); |
| 268 | } |
Michael Haggerty | 1a9d15d | 2015-08-10 11:47:41 +0200 | [diff] [blame] | 269 | } else { |
| 270 | err = close(fd); |
| 271 | } |
| 272 | |
Jeff King | 49bd0fc | 2017-09-05 08:14:30 -0400 | [diff] [blame] | 273 | return err ? -1 : 0; |
Michael Haggerty | 1a9d15d | 2015-08-10 11:47:41 +0200 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | int reopen_tempfile(struct tempfile *tempfile) |
| 277 | { |
Jeff King | e6fc267 | 2017-09-05 08:14:36 -0400 | [diff] [blame] | 278 | if (!is_tempfile_active(tempfile)) |
Jeff King | 9b028aa | 2017-09-05 08:14:43 -0400 | [diff] [blame] | 279 | BUG("reopen_tempfile called for an inactive object"); |
Jeff King | f5b4dc7 | 2017-09-05 08:14:40 -0400 | [diff] [blame] | 280 | if (0 <= tempfile->fd) |
Jeff King | 9b028aa | 2017-09-05 08:14:43 -0400 | [diff] [blame] | 281 | BUG("reopen_tempfile called for an open object"); |
Jeff King | 6c003d6 | 2018-09-04 19:36:43 -0400 | [diff] [blame] | 282 | tempfile->fd = open(tempfile->filename.buf, O_WRONLY|O_TRUNC); |
Michael Haggerty | 1a9d15d | 2015-08-10 11:47:41 +0200 | [diff] [blame] | 283 | return tempfile->fd; |
| 284 | } |
| 285 | |
Jeff King | 076aa2c | 2017-09-05 08:15:08 -0400 | [diff] [blame] | 286 | int rename_tempfile(struct tempfile **tempfile_p, const char *path) |
Michael Haggerty | 1a9d15d | 2015-08-10 11:47:41 +0200 | [diff] [blame] | 287 | { |
Jeff King | 076aa2c | 2017-09-05 08:15:08 -0400 | [diff] [blame] | 288 | struct tempfile *tempfile = *tempfile_p; |
| 289 | |
Jeff King | e6fc267 | 2017-09-05 08:14:36 -0400 | [diff] [blame] | 290 | if (!is_tempfile_active(tempfile)) |
Jeff King | 9b028aa | 2017-09-05 08:14:43 -0400 | [diff] [blame] | 291 | BUG("rename_tempfile called for inactive object"); |
Michael Haggerty | 1a9d15d | 2015-08-10 11:47:41 +0200 | [diff] [blame] | 292 | |
Jeff King | 49bd0fc | 2017-09-05 08:14:30 -0400 | [diff] [blame] | 293 | if (close_tempfile_gently(tempfile)) { |
Jeff King | 076aa2c | 2017-09-05 08:15:08 -0400 | [diff] [blame] | 294 | delete_tempfile(tempfile_p); |
Michael Haggerty | 1a9d15d | 2015-08-10 11:47:41 +0200 | [diff] [blame] | 295 | return -1; |
Jeff King | 49bd0fc | 2017-09-05 08:14:30 -0400 | [diff] [blame] | 296 | } |
Michael Haggerty | 1a9d15d | 2015-08-10 11:47:41 +0200 | [diff] [blame] | 297 | |
| 298 | if (rename(tempfile->filename.buf, path)) { |
| 299 | int save_errno = errno; |
Jeff King | 076aa2c | 2017-09-05 08:15:08 -0400 | [diff] [blame] | 300 | delete_tempfile(tempfile_p); |
Michael Haggerty | 1a9d15d | 2015-08-10 11:47:41 +0200 | [diff] [blame] | 301 | errno = save_errno; |
| 302 | return -1; |
| 303 | } |
| 304 | |
Jeff King | b5f4dcb | 2017-09-05 08:14:50 -0400 | [diff] [blame] | 305 | deactivate_tempfile(tempfile); |
Jeff King | 076aa2c | 2017-09-05 08:15:08 -0400 | [diff] [blame] | 306 | *tempfile_p = NULL; |
Michael Haggerty | 1a9d15d | 2015-08-10 11:47:41 +0200 | [diff] [blame] | 307 | return 0; |
| 308 | } |
| 309 | |
Jeff King | 076aa2c | 2017-09-05 08:15:08 -0400 | [diff] [blame] | 310 | void delete_tempfile(struct tempfile **tempfile_p) |
Michael Haggerty | 1a9d15d | 2015-08-10 11:47:41 +0200 | [diff] [blame] | 311 | { |
Jeff King | 076aa2c | 2017-09-05 08:15:08 -0400 | [diff] [blame] | 312 | struct tempfile *tempfile = *tempfile_p; |
| 313 | |
Jeff King | e6fc267 | 2017-09-05 08:14:36 -0400 | [diff] [blame] | 314 | if (!is_tempfile_active(tempfile)) |
Michael Haggerty | 1a9d15d | 2015-08-10 11:47:41 +0200 | [diff] [blame] | 315 | return; |
| 316 | |
Jeff King | 49bd0fc | 2017-09-05 08:14:30 -0400 | [diff] [blame] | 317 | close_tempfile_gently(tempfile); |
| 318 | unlink_or_warn(tempfile->filename.buf); |
Jeff King | b5f4dcb | 2017-09-05 08:14:50 -0400 | [diff] [blame] | 319 | deactivate_tempfile(tempfile); |
Jeff King | 076aa2c | 2017-09-05 08:15:08 -0400 | [diff] [blame] | 320 | *tempfile_p = NULL; |
Michael Haggerty | 1a9d15d | 2015-08-10 11:47:41 +0200 | [diff] [blame] | 321 | } |