clean-temp.h 6.2 KB

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