New module 'clean-temp'.
[pspp] / lib / clean-temp.h
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
24 #ifdef __cplusplus
25 extern "C" {
26 #endif
27
28
29 /* Temporary directories and temporary files should be automatically removed
30    when the program exits either normally or through a fatal signal.  We can't
31    rely on the "unlink before close" idiom, because it works only on Unix and
32    also - if no signal blocking is used - leaves a time window where a fatal
33    signal would not clean up the temporary file.
34
35    This module provides support for temporary directories and temporary files
36    inside these temporary directories.  Temporary files without temporary
37    directories are not supported here.  */
38
39 struct temp_dir
40 {
41   /* The absolute pathname of the directory.  */
42   const char * const dir_name;
43   /* Whether errors during explicit cleanup are reported to standard error.  */
44   bool cleanup_verbose;
45   /* More fields are present here, but not public.  */
46 };
47
48 /* Create a temporary directory.
49    PREFIX is used as a prefix for the name of the temporary directory. It
50    should be short and still give an indication about the program.
51    PARENTDIR can be used to specify the parent directory; if NULL, a default
52    parent directory is used (either $TMPDIR or /tmp or similar).
53    CLEANUP_VERBOSE determines whether errors during explicit cleanup are
54    reported to standard error.
55    Return a fresh 'struct temp_dir' on success.  Upon error, an error message
56    is shown and NULL is returned.  */
57 extern struct temp_dir * create_temp_dir (const char *prefix,
58                                           const char *parentdir,
59                                           bool cleanup_verbose);
60
61 /* Register the given ABSOLUTE_FILE_NAME as being a file inside DIR, that
62    needs to be removed before DIR can be removed.
63    Should be called before the file ABSOLUTE_FILE_NAME is created.  */
64 extern void register_temp_file (struct temp_dir *dir,
65                                 const char *absolute_file_name);
66
67 /* Unregister 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 when the file ABSOLUTE_FILE_NAME could not be created.  */
70 extern void unregister_temp_file (struct temp_dir *dir,
71                                   const char *absolute_file_name);
72
73 /* Register the given ABSOLUTE_DIR_NAME as being a subdirectory inside DIR,
74    that needs to be removed before DIR can be removed.
75    Should be called before the subdirectory ABSOLUTE_DIR_NAME is created.  */
76 extern void register_temp_subdir (struct temp_dir *dir,
77                                   const char *absolute_dir_name);
78
79 /* Unregister 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 when the subdirectory ABSOLUTE_DIR_NAME could not be
82    created.  */
83 extern void unregister_temp_subdir (struct temp_dir *dir,
84                                     const char *absolute_dir_name);
85
86 /* Remove the given ABSOLUTE_FILE_NAME and unregister it.  */
87 extern void cleanup_temp_file (struct temp_dir *dir,
88                                const char *absolute_file_name);
89
90 /* Remove the given ABSOLUTE_DIR_NAME and unregister it.  */
91 extern void cleanup_temp_subdir (struct temp_dir *dir,
92                                  const char *absolute_dir_name);
93
94 /* Remove all registered files and subdirectories inside DIR.  */
95 extern void cleanup_temp_dir_contents (struct temp_dir *dir);
96
97 /* Remove all registered files and subdirectories inside DIR and DIR itself.
98    DIR cannot be used any more after this call.  */
99 extern void cleanup_temp_dir (struct temp_dir *dir);
100
101
102 #ifdef __cplusplus
103 }
104 #endif
105
106 #endif /* _CLEAN_TEMP_H */