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