Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/git/blob/master/tempfile.c # git [Ported from github.com/git/git] Sync with Git 2.56.1 Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `git / tempfile.c` 386 lines ยท 9838 bytes Raw Blame History 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 * - Inactive/unallocated. The only way to get a tempfile is via a creation 18 * function like create_tempfile(). Once allocated, the tempfile is on the 19 * global tempfile_list and considered active. 20 * 21 * - Active, file open (after `create_tempfile()` or 22 * `reopen_tempfile()`). In this state: 23 * 24 * - the temporary file exists 25 * - `filename` holds the filename of the temporary file 26 * - `fd` holds a file descriptor open for writing to it 27 * - `fp` holds a pointer to an open `FILE` object if and only if 28 * `fdopen_tempfile()` has been called on the object 29 * - `owner` holds the PID of the process that created the file 30 * 31 * - Active, file closed (after `close_tempfile_gently()`). Same 32 * as the previous state, except that the temporary file is closed, 33 * `fd` is -1, and `fp` is `NULL`. 34 * 35 * - Inactive (after `delete_tempfile()`, `rename_tempfile()`, or a 36 * failed attempt to create a temporary file). The struct is removed from 37 * the global tempfile_list and deallocated. 38 * 39 * A temporary file is owned by the process that created it. The 40 * `tempfile` has an `owner` field that records the owner's PID. This 41 * field is used to prevent a forked process from deleting a temporary 42 * file created by its parent. 43 */ 44 45 #include "git-compat-util.h" 46 #include "abspath.h" 47 #include "path.h" 48 #include "tempfile.h" 49 #include "sigchain.h" 50 51 static VOLATILE_LIST_HEAD(tempfile_list); 52 53 static int remove_template_directory(struct tempfile *tempfile, 54 int in_signal_handler) 55 { 56 if (tempfile->directory) { 57 if (in_signal_handler) 58 return rmdir(tempfile->directory); 59 else 60 return rmdir_or_warn(tempfile->directory); 61 } 62 63 return 0; 64 } 65 66 static void remove_tempfiles(int in_signal_handler) 67 { 68 pid_t me = getpid(); 69 volatile struct volatile_list_head *pos; 70 71 list_for_each(pos, &tempfile_list) { 72 struct tempfile *p = list_entry(pos, struct tempfile, list); 73 74 if (!is_tempfile_active(p) || p->owner != me) 75 continue; 76 77 if (p->fd >= 0) 78 close(p->fd); 79 80 if (in_signal_handler) 81 unlink(p->filename.buf); 82 else 83 unlink_or_warn(p->filename.buf); 84 remove_template_directory(p, in_signal_handler); 85 } 86 } 87 88 static void remove_tempfiles_on_exit(void) 89 { 90 remove_tempfiles(0); 91 } 92 93 static void remove_tempfiles_on_signal(int signo) 94 { 95 remove_tempfiles(1); 96 sigchain_pop(signo); 97 raise(signo); 98 } 99 100 static struct tempfile *new_tempfile(void) 101 { 102 struct tempfile *tempfile = xmalloc(sizeof(*tempfile)); 103 tempfile->fd = -1; 104 tempfile->fp = NULL; 105 tempfile->owner = 0; 106 INIT_LIST_HEAD(&tempfile->list); 107 strbuf_init(&tempfile->filename, 0); 108 tempfile->directory = NULL; 109 return tempfile; 110 } 111 112 static void activate_tempfile(struct tempfile *tempfile) 113 { 114 static int initialized; 115 116 if (!initialized) { 117 sigchain_push_common(remove_tempfiles_on_signal); 118 atexit(remove_tempfiles_on_exit); 119 initialized = 1; 120 } 121 122 volatile_list_add(&tempfile->list, &tempfile_list); 123 tempfile->owner = getpid(); 124 } 125 126 static void deactivate_tempfile(struct tempfile *tempfile) 127 { 128 volatile_list_del(&tempfile->list); 129 strbuf_release(&tempfile->filename); 130 free(tempfile->directory); 131 free(tempfile); 132 } 133 134 /* Make sure errno contains a meaningful value on error */ 135 struct tempfile *repo_create_tempfile_mode(struct repository *r, 136 const char *path, int mode) 137 { 138 struct tempfile *tempfile = new_tempfile(); 139 140 strbuf_add_absolute_path(&tempfile->filename, path); 141 tempfile->fd = open(tempfile->filename.buf, 142 O_RDWR | O_CREAT | O_EXCL | O_CLOEXEC, mode); 143 if (O_CLOEXEC && tempfile->fd < 0 && errno == EINVAL) 144 /* Try again w/o O_CLOEXEC: the kernel might not support it */ 145 tempfile->fd = open(tempfile->filename.buf, 146 O_RDWR | O_CREAT | O_EXCL, mode); 147 if (tempfile->fd < 0) { 148 deactivate_tempfile(tempfile); 149 return NULL; 150 } 151 activate_tempfile(tempfile); 152 if (adjust_shared_perm(r, tempfile->filename.buf)) { 153 int save_errno = errno; 154 error("cannot fix permission bits on %s", tempfile->filename.buf); 155 delete_tempfile(&tempfile); 156 errno = save_errno; 157 return NULL; 158 } 159 160 return tempfile; 161 } 162 163 struct tempfile *register_tempfile(const char *path) 164 { 165 struct tempfile *tempfile = new_tempfile(); 166 strbuf_add_absolute_path(&tempfile->filename, path); 167 activate_tempfile(tempfile); 168 return tempfile; 169 } 170 171 struct tempfile *mks_tempfile_sm(const char *filename_template, int suffixlen, int mode) 172 { 173 struct tempfile *tempfile = new_tempfile(); 174 175 strbuf_add_absolute_path(&tempfile->filename, filename_template); 176 tempfile->fd = git_mkstemps_mode(tempfile->filename.buf, suffixlen, mode); 177 if (tempfile->fd < 0) { 178 deactivate_tempfile(tempfile); 179 return NULL; 180 } 181 activate_tempfile(tempfile); 182 return tempfile; 183 } 184 185 struct tempfile *mks_tempfile_tsm(const char *filename_template, int suffixlen, int mode) 186 { 187 struct tempfile *tempfile = new_tempfile(); 188 const char *tmpdir; 189 190 tmpdir = getenv("TMPDIR"); 191 if (!tmpdir) 192 tmpdir = "/tmp"; 193 194 strbuf_addf(&tempfile->filename, "%s/%s", tmpdir, filename_template); 195 tempfile->fd = git_mkstemps_mode(tempfile->filename.buf, suffixlen, mode); 196 if (tempfile->fd < 0) { 197 deactivate_tempfile(tempfile); 198 return NULL; 199 } 200 activate_tempfile(tempfile); 201 return tempfile; 202 } 203 204 struct tempfile *mks_tempfile_dt(const char *directory_template, 205 const char *filename) 206 { 207 struct tempfile *tempfile; 208 const char *tmpdir; 209 struct strbuf sb = STRBUF_INIT; 210 int fd; 211 size_t directorylen; 212 213 if (!ends_with(directory_template, "XXXXXX")) { 214 errno = EINVAL; 215 return NULL; 216 } 217 218 tmpdir = getenv("TMPDIR"); 219 if (!tmpdir) 220 tmpdir = "/tmp"; 221 222 strbuf_addf(&sb, "%s/%s", tmpdir, directory_template); 223 directorylen = sb.len; 224 if (!mkdtemp(sb.buf)) { 225 int orig_errno = errno; 226 strbuf_release(&sb); 227 errno = orig_errno; 228 return NULL; 229 } 230 231 strbuf_addf(&sb, "/%s", filename); 232 fd = open(sb.buf, O_CREAT | O_EXCL | O_RDWR, 0600); 233 if (fd < 0) { 234 int orig_errno = errno; 235 strbuf_setlen(&sb, directorylen); 236 rmdir(sb.buf); 237 strbuf_release(&sb); 238 errno = orig_errno; 239 return NULL; 240 } 241 242 tempfile = new_tempfile(); 243 strbuf_swap(&tempfile->filename, &sb); 244 tempfile->directory = xmemdupz(tempfile->filename.buf, directorylen); 245 tempfile->fd = fd; 246 activate_tempfile(tempfile); 247 return tempfile; 248 } 249 250 struct tempfile *xmks_tempfile_m(const char *filename_template, int mode) 251 { 252 struct tempfile *tempfile; 253 struct strbuf full_template = STRBUF_INIT; 254 255 strbuf_add_absolute_path(&full_template, filename_template); 256 tempfile = mks_tempfile_m(full_template.buf, mode); 257 if (!tempfile) 258 die_errno("Unable to create temporary file '%s'", 259 full_template.buf); 260 261 strbuf_release(&full_template); 262 return tempfile; 263 } 264 265 FILE *fdopen_tempfile(struct tempfile *tempfile, const char *mode) 266 { 267 if (!is_tempfile_active(tempfile)) 268 BUG("fdopen_tempfile() called for inactive object"); 269 if (tempfile->fp) 270 BUG("fdopen_tempfile() called for open object"); 271 272 tempfile->fp = fdopen(tempfile->fd, mode); 273 return tempfile->fp; 274 } 275 276 const char *get_tempfile_path(struct tempfile *tempfile) 277 { 278 if (!is_tempfile_active(tempfile)) 279 BUG("get_tempfile_path() called for inactive object"); 280 return tempfile->filename.buf; 281 } 282 283 int get_tempfile_fd(struct tempfile *tempfile) 284 { 285 if (!is_tempfile_active(tempfile)) 286 BUG("get_tempfile_fd() called for inactive object"); 287 return tempfile->fd; 288 } 289 290 FILE *get_tempfile_fp(struct tempfile *tempfile) 291 { 292 if (!is_tempfile_active(tempfile)) 293 BUG("get_tempfile_fp() called for inactive object"); 294 return tempfile->fp; 295 } 296 297 int close_tempfile_gently(struct tempfile *tempfile) 298 { 299 int fd; 300 FILE *fp; 301 int err; 302 303 if (!is_tempfile_active(tempfile) || tempfile->fd < 0) 304 return 0; 305 306 fd = tempfile->fd; 307 fp = tempfile->fp; 308 tempfile->fd = -1; 309 if (fp) { 310 tempfile->fp = NULL; 311 if (ferror(fp)) { 312 err = -1; 313 if (!fclose(fp)) 314 errno = EIO; 315 } else { 316 err = fclose(fp); 317 } 318 } else { 319 err = close(fd); 320 } 321 322 return err ? -1 : 0; 323 } 324 325 int reopen_tempfile(struct tempfile *tempfile) 326 { 327 if (!is_tempfile_active(tempfile)) 328 BUG("reopen_tempfile called for an inactive object"); 329 if (0 <= tempfile->fd) 330 BUG("reopen_tempfile called for an open object"); 331 tempfile->fd = open(tempfile->filename.buf, O_WRONLY|O_TRUNC); 332 return tempfile->fd; 333 } 334 335 int rename_tempfile(struct tempfile **tempfile_p, const char *path) 336 { 337 struct tempfile *tempfile = *tempfile_p; 338 339 if (!is_tempfile_active(tempfile)) 340 BUG("rename_tempfile called for inactive object"); 341 342 if (close_tempfile_gently(tempfile)) { 343 delete_tempfile(tempfile_p); 344 return -1; 345 } 346 347 if (rename(tempfile->filename.buf, path)) { 348 int save_errno = errno; 349 delete_tempfile(tempfile_p); 350 errno = save_errno; 351 return -1; 352 } 353 354 deactivate_tempfile(tempfile); 355 *tempfile_p = NULL; 356 return 0; 357 } 358 359 int delete_tempfile(struct tempfile **tempfile_p) 360 { 361 struct tempfile *tempfile = *tempfile_p; 362 int err = 0; 363 364 if (!is_tempfile_active(tempfile)) 365 return 0; 366 367 err |= close_tempfile_gently(tempfile); 368 err |= unlink_or_warn(tempfile->filename.buf); 369 err |= remove_template_directory(tempfile, 0); 370 deactivate_tempfile(tempfile); 371 *tempfile_p = NULL; 372 373 return err ? -1 : 0; 374 } 375 376 void reassign_tempfile_ownership(pid_t from, pid_t to) 377 { 378 volatile struct volatile_list_head *pos; 379 380 list_for_each(pos, &tempfile_list) { 381 struct tempfile *p = list_entry(pos, struct tempfile, list); 382 383 if (is_tempfile_active(p) && p->owner == from) 384 p->owner = to; 385 } 386 }