1 1.1 christos /* Temporary directories and temporary files with automatic cleanup. 2 1.1 christos Copyright (C) 2006 Free Software Foundation, Inc. 3 1.1 christos Written by Bruno Haible <bruno (at) clisp.org>, 2006. 4 1.1 christos 5 1.1 christos This program is free software; you can redistribute it and/or modify 6 1.1 christos it under the terms of the GNU General Public License as published by 7 1.1 christos the Free Software Foundation; either version 2, or (at your option) 8 1.1 christos any later version. 9 1.1 christos 10 1.1 christos This program is distributed in the hope that it will be useful, 11 1.1 christos but WITHOUT ANY WARRANTY; without even the implied warranty of 12 1.1 christos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 1.1 christos GNU General Public License for more details. 14 1.1 christos 15 1.1 christos You should have received a copy of the GNU General Public License 16 1.1 christos along with this program; if not, write to the Free Software Foundation, 17 1.1 christos Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ 18 1.1 christos 19 1.1 christos #ifndef _CLEAN_TEMP_H 20 1.1 christos #define _CLEAN_TEMP_H 21 1.1 christos 22 1.1 christos #include <stdbool.h> 23 1.1 christos #include <stdio.h> 24 1.1 christos #include <sys/types.h> 25 1.1 christos 26 1.1 christos #ifdef __cplusplus 27 1.1 christos extern "C" { 28 1.1 christos #endif 29 1.1 christos 30 1.1 christos 31 1.1 christos /* Temporary directories and temporary files should be automatically removed 32 1.1 christos when the program exits either normally or through a fatal signal. We can't 33 1.1 christos rely on the "unlink before close" idiom, because it works only on Unix and 34 1.1 christos also - if no signal blocking is used - leaves a time window where a fatal 35 1.1 christos signal would not clean up the temporary file. 36 1.1 christos 37 1.1 christos Also, open file descriptors need to be closed before the temporary files 38 1.1 christos and the temporary directories can be removed, because only on Unix 39 1.1 christos (excluding Cygwin) can one remove directories containing open files. 40 1.1 christos 41 1.1 christos This module provides support for temporary directories and temporary files 42 1.1 christos inside these temporary directories. Temporary files without temporary 43 1.1 christos directories are not supported here. */ 44 1.1 christos 45 1.1 christos struct temp_dir 46 1.1 christos { 47 1.1 christos /* The absolute pathname of the directory. */ 48 1.1 christos const char * const dir_name; 49 1.1 christos /* Whether errors during explicit cleanup are reported to standard error. */ 50 1.1 christos bool cleanup_verbose; 51 1.1 christos /* More fields are present here, but not public. */ 52 1.1 christos }; 53 1.1 christos 54 1.1 christos /* Create a temporary directory. 55 1.1 christos PREFIX is used as a prefix for the name of the temporary directory. It 56 1.1 christos should be short and still give an indication about the program. 57 1.1 christos PARENTDIR can be used to specify the parent directory; if NULL, a default 58 1.1 christos parent directory is used (either $TMPDIR or /tmp or similar). 59 1.1 christos CLEANUP_VERBOSE determines whether errors during explicit cleanup are 60 1.1 christos reported to standard error. 61 1.1 christos Return a fresh 'struct temp_dir' on success. Upon error, an error message 62 1.1 christos is shown and NULL is returned. */ 63 1.1 christos extern struct temp_dir * create_temp_dir (const char *prefix, 64 1.1 christos const char *parentdir, 65 1.1 christos bool cleanup_verbose); 66 1.1 christos 67 1.1 christos /* Register the given ABSOLUTE_FILE_NAME as being a file inside DIR, that 68 1.1 christos needs to be removed before DIR can be removed. 69 1.1 christos Should be called before the file ABSOLUTE_FILE_NAME is created. */ 70 1.1 christos extern void register_temp_file (struct temp_dir *dir, 71 1.1 christos const char *absolute_file_name); 72 1.1 christos 73 1.1 christos /* Unregister the given ABSOLUTE_FILE_NAME as being a file inside DIR, that 74 1.1 christos needs to be removed before DIR can be removed. 75 1.1 christos Should be called when the file ABSOLUTE_FILE_NAME could not be created. */ 76 1.1 christos extern void unregister_temp_file (struct temp_dir *dir, 77 1.1 christos const char *absolute_file_name); 78 1.1 christos 79 1.1 christos /* Register the given ABSOLUTE_DIR_NAME as being a subdirectory inside DIR, 80 1.1 christos that needs to be removed before DIR can be removed. 81 1.1 christos Should be called before the subdirectory ABSOLUTE_DIR_NAME is created. */ 82 1.1 christos extern void register_temp_subdir (struct temp_dir *dir, 83 1.1 christos const char *absolute_dir_name); 84 1.1 christos 85 1.1 christos /* Unregister the given ABSOLUTE_DIR_NAME as being a subdirectory inside DIR, 86 1.1 christos that needs to be removed before DIR can be removed. 87 1.1 christos Should be called when the subdirectory ABSOLUTE_DIR_NAME could not be 88 1.1 christos created. */ 89 1.1 christos extern void unregister_temp_subdir (struct temp_dir *dir, 90 1.1 christos const char *absolute_dir_name); 91 1.1 christos 92 1.1 christos /* Remove the given ABSOLUTE_FILE_NAME and unregister it. 93 1.1 christos Return 0 upon success, or -1 if there was some problem. */ 94 1.1 christos extern int cleanup_temp_file (struct temp_dir *dir, 95 1.1 christos const char *absolute_file_name); 96 1.1 christos 97 1.1 christos /* Remove the given ABSOLUTE_DIR_NAME and unregister it. 98 1.1 christos Return 0 upon success, or -1 if there was some problem. */ 99 1.1 christos extern int cleanup_temp_subdir (struct temp_dir *dir, 100 1.1 christos const char *absolute_dir_name); 101 1.1 christos 102 1.1 christos /* Remove all registered files and subdirectories inside DIR. 103 1.1 christos Return 0 upon success, or -1 if there was some problem. */ 104 1.1 christos extern int cleanup_temp_dir_contents (struct temp_dir *dir); 105 1.1 christos 106 1.1 christos /* Remove all registered files and subdirectories inside DIR and DIR itself. 107 1.1 christos DIR cannot be used any more after this call. 108 1.1 christos Return 0 upon success, or -1 if there was some problem. */ 109 1.1 christos extern int cleanup_temp_dir (struct temp_dir *dir); 110 1.1 christos 111 1.1 christos /* Open a temporary file in a temporary directory. 112 1.1 christos Registers the resulting file descriptor to be closed. */ 113 1.1 christos extern int open_temp (const char *file_name, int flags, mode_t mode); 114 1.1 christos extern FILE * fopen_temp (const char *file_name, const char *mode); 115 1.1 christos 116 1.1 christos /* Close a temporary file in a temporary directory. 117 1.1 christos Unregisters the previously registered file descriptor. */ 118 1.1 christos extern int close_temp (int fd); 119 1.1 christos extern int fclose_temp (FILE *fp); 120 1.1 christos 121 1.1 christos /* Like fwriteerror. 122 1.1 christos Unregisters the previously registered file descriptor. */ 123 1.1 christos extern int fwriteerror_temp (FILE *fp); 124 1.1 christos 125 1.1 christos /* Like close_stream. 126 1.1 christos Unregisters the previously registered file descriptor. */ 127 1.1 christos extern int close_stream_temp (FILE *fp); 128 1.1 christos 129 1.1 christos 130 1.1 christos #ifdef __cplusplus 131 1.1 christos } 132 1.1 christos #endif 133 1.1 christos 134 1.1 christos #endif /* _CLEAN_TEMP_H */ 135