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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
7044
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1 /* Temporary directories and temporary files with automatic cleanup.
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
2 Copyright (C) 2006 Free Software Foundation, Inc.
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3 Written by Bruno Haible <bruno@clisp.org>, 2006.
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5 This program is free software; you can redistribute it and/or modify
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
6 it under the terms of the GNU General Public License as published by
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
7 the Free Software Foundation; either version 2, or (at your option)
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
8 any later version.
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
9
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
10 This program is distributed in the hope that it will be useful,
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
13 GNU General Public License for more details.
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
14
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
15 You should have received a copy of the GNU General Public License
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
16 along with this program; if not, write to the Free Software Foundation,
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
17 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
18
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
19 #ifndef _CLEAN_TEMP_H
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
20 #define _CLEAN_TEMP_H
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
21
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
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
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
25
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
26 #ifdef __cplusplus
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
27 extern "C" {
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
28 #endif
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
29
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
30
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
31 /* Temporary directories and temporary files should be automatically removed
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
32 when the program exits either normally or through a fatal signal. We can't
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
33 rely on the "unlink before close" idiom, because it works only on Unix and
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
34 also - if no signal blocking is used - leaves a time window where a fatal
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
35 signal would not clean up the temporary file.
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
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
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
41 This module provides support for temporary directories and temporary files
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
42 inside these temporary directories. Temporary files without temporary
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
43 directories are not supported here. */
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
44
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
45 struct temp_dir
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
46 {
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
47 /* The absolute pathname of the directory. */
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
48 const char * const dir_name;
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
49 /* Whether errors during explicit cleanup are reported to standard error. */
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
50 bool cleanup_verbose;
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
51 /* More fields are present here, but not public. */
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
52 };
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
53
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
54 /* Create a temporary directory.
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
55 PREFIX is used as a prefix for the name of the temporary directory. It
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
56 should be short and still give an indication about the program.
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
57 PARENTDIR can be used to specify the parent directory; if NULL, a default
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
58 parent directory is used (either $TMPDIR or /tmp or similar).
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
59 CLEANUP_VERBOSE determines whether errors during explicit cleanup are
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
60 reported to standard error.
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
61 Return a fresh 'struct temp_dir' on success. Upon error, an error message
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
62 is shown and NULL is returned. */
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
63 extern struct temp_dir * create_temp_dir (const char *prefix,
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
64 const char *parentdir,
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
65 bool cleanup_verbose);
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
66
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
67 /* Register the given ABSOLUTE_FILE_NAME as being a file inside DIR, that
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
68 needs to be removed before DIR can be removed.
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
69 Should be called before the file ABSOLUTE_FILE_NAME is created. */
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
70 extern void register_temp_file (struct temp_dir *dir,
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
71 const char *absolute_file_name);
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
72
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
73 /* Unregister the given ABSOLUTE_FILE_NAME as being a file inside DIR, that
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
74 needs to be removed before DIR can be removed.
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
75 Should be called when the file ABSOLUTE_FILE_NAME could not be created. */
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
76 extern void unregister_temp_file (struct temp_dir *dir,
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
77 const char *absolute_file_name);
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
78
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
79 /* Register the given ABSOLUTE_DIR_NAME as being a subdirectory inside DIR,
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
80 that needs to be removed before DIR can be removed.
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
81 Should be called before the subdirectory ABSOLUTE_DIR_NAME is created. */
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
82 extern void register_temp_subdir (struct temp_dir *dir,
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
83 const char *absolute_dir_name);
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
84
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
85 /* Unregister the given ABSOLUTE_DIR_NAME as being a subdirectory inside DIR,
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
86 that needs to be removed before DIR can be removed.
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
87 Should be called when the subdirectory ABSOLUTE_DIR_NAME could not be
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
88 created. */
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
89 extern void unregister_temp_subdir (struct temp_dir *dir,
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
90 const char *absolute_dir_name);
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
91
7415
66b32599c835 Return an error indicator.
Bruno Haible <bruno@clisp.org>
parents: 7411
diff changeset
92 /* Remove the given ABSOLUTE_FILE_NAME and unregister it.
66b32599c835 Return an error indicator.
Bruno Haible <bruno@clisp.org>
parents: 7411
diff changeset
93 Return 0 upon success, or -1 if there was some problem. */
66b32599c835 Return an error indicator.
Bruno Haible <bruno@clisp.org>
parents: 7411
diff changeset
94 extern int cleanup_temp_file (struct temp_dir *dir,
66b32599c835 Return an error indicator.
Bruno Haible <bruno@clisp.org>
parents: 7411
diff changeset
95 const char *absolute_file_name);
7044
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
96
7415
66b32599c835 Return an error indicator.
Bruno Haible <bruno@clisp.org>
parents: 7411
diff changeset
97 /* Remove the given ABSOLUTE_DIR_NAME and unregister it.
66b32599c835 Return an error indicator.
Bruno Haible <bruno@clisp.org>
parents: 7411
diff changeset
98 Return 0 upon success, or -1 if there was some problem. */
66b32599c835 Return an error indicator.
Bruno Haible <bruno@clisp.org>
parents: 7411
diff changeset
99 extern int cleanup_temp_subdir (struct temp_dir *dir,
66b32599c835 Return an error indicator.
Bruno Haible <bruno@clisp.org>
parents: 7411
diff changeset
100 const char *absolute_dir_name);
7044
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
101
7415
66b32599c835 Return an error indicator.
Bruno Haible <bruno@clisp.org>
parents: 7411
diff changeset
102 /* Remove all registered files and subdirectories inside DIR.
66b32599c835 Return an error indicator.
Bruno Haible <bruno@clisp.org>
parents: 7411
diff changeset
103 Return 0 upon success, or -1 if there was some problem. */
66b32599c835 Return an error indicator.
Bruno Haible <bruno@clisp.org>
parents: 7411
diff changeset
104 extern int cleanup_temp_dir_contents (struct temp_dir *dir);
7044
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
105
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
106 /* Remove all registered files and subdirectories inside DIR and DIR itself.
7415
66b32599c835 Return an error indicator.
Bruno Haible <bruno@clisp.org>
parents: 7411
diff changeset
107 DIR cannot be used any more after this call.
66b32599c835 Return an error indicator.
Bruno Haible <bruno@clisp.org>
parents: 7411
diff changeset
108 Return 0 upon success, or -1 if there was some problem. */
66b32599c835 Return an error indicator.
Bruno Haible <bruno@clisp.org>
parents: 7411
diff changeset
109 extern int cleanup_temp_dir (struct temp_dir *dir);
7044
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
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
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
129
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
130 #ifdef __cplusplus
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
131 }
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
132 #endif
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
133
4165bec5f3a2 New module 'clean-temp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
134 #endif /* _CLEAN_TEMP_H */