Mercurial > hg > octave-jordi > gnulib-hg
annotate lib/clean-temp.h @ 7417:fa9e9b096831
* clean-temp.h (close_stream_temp): New declaration.
* clean-temp.c (includes): Pull in headers according to what
other modules are in use.
(close_stream_temp) [GNULIB_CLOSE_STREAM]: New function.
author | Eric Blake <ebb9@byu.net> |
---|---|
date | Fri, 06 Oct 2006 23:13:53 +0000 |
parents | 66b32599c835 |
children | fd5a9db5be5b |
rev | line source |
---|---|
7044 | 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> | |
7411
67235cf9199a
Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents:
7044
diff
changeset
|
23 #include <stdio.h> |
67235cf9199a
Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents:
7044
diff
changeset
|
24 #include <sys/types.h> |
7044 | 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 | |
7411
67235cf9199a
Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents:
7044
diff
changeset
|
37 Also, open file descriptors need to be closed before the temporary files |
67235cf9199a
Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents:
7044
diff
changeset
|
38 and the temporary directories can be removed, because only on Unix |
67235cf9199a
Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents:
7044
diff
changeset
|
39 (excluding Cygwin) one can remove directories containing open files. |
67235cf9199a
Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents:
7044
diff
changeset
|
40 |
7044 | 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 | |
7415 | 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); | |
7044 | 96 |
7415 | 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); | |
7044 | 101 |
7415 | 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); | |
7044 | 105 |
106 /* Remove all registered files and subdirectories inside DIR and DIR itself. | |
7415 | 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); | |
7044 | 110 |
7411
67235cf9199a
Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents:
7044
diff
changeset
|
111 /* Open a temporary file in a temporary directory. |
67235cf9199a
Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents:
7044
diff
changeset
|
112 Registers the resulting file descriptor to be closed. */ |
67235cf9199a
Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents:
7044
diff
changeset
|
113 extern int open_temp (const char *file_name, int flags, mode_t mode); |
67235cf9199a
Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents:
7044
diff
changeset
|
114 extern FILE * fopen_temp (const char *file_name, const char *mode); |
67235cf9199a
Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents:
7044
diff
changeset
|
115 |
67235cf9199a
Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents:
7044
diff
changeset
|
116 /* Close a temporary file in a temporary directory. |
67235cf9199a
Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents:
7044
diff
changeset
|
117 Unregisters the previously registered file descriptor. */ |
67235cf9199a
Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents:
7044
diff
changeset
|
118 extern int close_temp (int fd); |
67235cf9199a
Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents:
7044
diff
changeset
|
119 extern int fclose_temp (FILE *fp); |
67235cf9199a
Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents:
7044
diff
changeset
|
120 |
67235cf9199a
Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents:
7044
diff
changeset
|
121 /* Like fwriteerror. |
67235cf9199a
Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents:
7044
diff
changeset
|
122 Unregisters the previously registered file descriptor. */ |
67235cf9199a
Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents:
7044
diff
changeset
|
123 extern int fwriteerror_temp (FILE *fp); |
67235cf9199a
Have clean-temp register file open descriptors to temporary files.
Bruno Haible <bruno@clisp.org>
parents:
7044
diff
changeset
|
124 |
7417
fa9e9b096831
* clean-temp.h (close_stream_temp): New declaration.
Eric Blake <ebb9@byu.net>
parents:
7415
diff
changeset
|
125 /* Like close_stream. |
fa9e9b096831
* clean-temp.h (close_stream_temp): New declaration.
Eric Blake <ebb9@byu.net>
parents:
7415
diff
changeset
|
126 Unregisters the previously registered file descriptor. */ |
fa9e9b096831
* clean-temp.h (close_stream_temp): New declaration.
Eric Blake <ebb9@byu.net>
parents:
7415
diff
changeset
|
127 extern int close_stream_temp (FILE *fp); |
fa9e9b096831
* clean-temp.h (close_stream_temp): New declaration.
Eric Blake <ebb9@byu.net>
parents:
7415
diff
changeset
|
128 |
7044 | 129 |
130 #ifdef __cplusplus | |
131 } | |
132 #endif | |
133 | |
134 #endif /* _CLEAN_TEMP_H */ |