/* * State diagram and cleanup * ------------------------- * * If the program exits while a temporary file is active, we want to * make sure that we remove it. This is done by remembering the active * temporary files in a linked list, `tempfile_list`. An `atexit(3)` * handler and a signal handler are registered, to clean up any active * temporary files. * * Because the signal handler can run at any time, `tempfile_list` and * the `tempfile` objects that comprise it must be kept in * self-consistent states at all times. * * The possible states of a `tempfile` object are as follows: * * - Inactive/unallocated. The only way to get a tempfile is via a creation * function like create_tempfile(). Once allocated, the tempfile is on the * global tempfile_list and considered active. * * - Active, file open (after `create_tempfile()` or * `reopen_tempfile()`). In this state: * * - the temporary file exists * - `filename` holds the filename of the temporary file * - `fd` holds a file descriptor open for writing to it * - `fp` holds a pointer to an open `FILE` object if and only if * `fdopen_tempfile()` has been called on the object * - `owner` holds the PID of the process that created the file * * - Active, file closed (after `close_tempfile_gently()`). Same * as the previous state, except that the temporary file is closed, * `fd` is -1, and `fp` is `NULL`. * * - Inactive (after `delete_tempfile()`, `rename_tempfile()`, or a * failed attempt to create a temporary file). The struct is removed from * the global tempfile_list and deallocated. * * A temporary file is owned by the process that created it. The * `tempfile` has an `owner` field that records the owner's PID. This * field is used to prevent a forked process from deleting a temporary * file created by its parent. */ #include "git-compat-util.h" #include "abspath.h" #include "path.h" #include "tempfile.h" #include "sigchain.h" static VOLATILE_LIST_HEAD(tempfile_list); static int remove_template_directory(struct tempfile *tempfile, int in_signal_handler) { … } static void remove_tempfiles(int in_signal_handler) { … } static void remove_tempfiles_on_exit(void) { … } static void remove_tempfiles_on_signal(int signo) { … } static struct tempfile *new_tempfile(void) { … } static void activate_tempfile(struct tempfile *tempfile) { … } static void deactivate_tempfile(struct tempfile *tempfile) { … } /* Make sure errno contains a meaningful value on error */ struct tempfile *create_tempfile_mode(const char *path, int mode) { … } struct tempfile *register_tempfile(const char *path) { … } struct tempfile *mks_tempfile_sm(const char *filename_template, int suffixlen, int mode) { … } struct tempfile *mks_tempfile_tsm(const char *filename_template, int suffixlen, int mode) { … } struct tempfile *mks_tempfile_dt(const char *directory_template, const char *filename) { … } struct tempfile *xmks_tempfile_m(const char *filename_template, int mode) { … } FILE *fdopen_tempfile(struct tempfile *tempfile, const char *mode) { … } const char *get_tempfile_path(struct tempfile *tempfile) { … } int get_tempfile_fd(struct tempfile *tempfile) { … } FILE *get_tempfile_fp(struct tempfile *tempfile) { … } int close_tempfile_gently(struct tempfile *tempfile) { … } int reopen_tempfile(struct tempfile *tempfile) { … } int rename_tempfile(struct tempfile **tempfile_p, const char *path) { … } int delete_tempfile(struct tempfile **tempfile_p) { … }