+2021-03-22 Bruno Haible <bruno@clisp.org>
+
+ clean-temp-simple: Remove dependency upon xalloc, xalloc-die, xlist.
+ * lib/clean-temp-private.h (clean_temp_init): Change return type to
+ 'int'.
+ * lib/clean-temp-simple.h (register_temporary_file): Change return type
+ to 'int'.
+ * lib/clean-temp-simple.c: Don't include xalloc.h, gl_xlist.h. Include
+ gl_list.h instead.
+ (init_failed): New variable.
+ (do_clean_temp_init): Set it.
+ (clean_temp_init): Return an error indicator.
+ (register_temporary_file): Invoke gl_list_nx_create_empty instead of
+ gl_list_create_empty. Invoke strdup instead of xstrdup. Invoke
+ gl_list_nx_add_first instead of gl_list_add_first. Return an error
+ indicator.
+ * lib/clean-temp.c (create_temp_dir, gen_register_open_temp): Call
+ xalloc_die() if clean_temp_init or register_temporary_file failed.
+ * modules/clean-temp-simple (Depends-on): Remove xalloc, xalloc-die,
+ xlist.
+
2021-03-22 Bruno Haible <bruno@clisp.org>
clean-temp-simple: New module.
#include "error.h"
#include "fatal-signal.h"
#include "asyncsafe-spin.h"
-#include "xalloc.h"
#include "glthread/lock.h"
#include "thread-optim.h"
-#include "gl_xlist.h"
+#include "gl_list.h"
#include "gl_linkedhash_list.h"
#include "gettext.h"
}
+/* Set to -1 if initialization of this facility failed. */
+static int volatile init_failed /* = 0 */;
+
/* Initializes this facility. */
static void
do_clean_temp_init (void)
init_fatal_signal_set ();
/* Register the cleanup handler. */
if (at_fatal_signal (&cleanup_action) < 0)
- xalloc_die ();
+ init_failed = -1;
}
/* Ensure that do_clean_temp_init is called once only. */
gl_once_define(static, clean_temp_once)
-/* Initializes this facility upon first use. */
-void
+/* Initializes this facility upon first use.
+ Return 0 upon success, or -1 if there was a memory allocation problem. */
+int
clean_temp_init (void)
{
gl_once (clean_temp_once, do_clean_temp_init);
+ return init_failed;
}
/* Register the given ABSOLUTE_FILE_NAME as being a file that needs to be
removed.
- Should be called before the file ABSOLUTE_FILE_NAME is created. */
-void
+ Should be called before the file ABSOLUTE_FILE_NAME is created.
+ Return 0 upon success, or -1 if there was a memory allocation problem. */
+int
register_temporary_file (const char *absolute_file_name)
{
bool mt = gl_multithreaded ();
if (mt) gl_lock_lock (file_cleanup_list_lock);
+ int ret = 0;
+
/* Make sure that this facility and the file_cleanup_list are initialized. */
if (file_cleanup_list == NULL)
{
- clean_temp_init ();
+ if (clean_temp_init () < 0)
+ {
+ ret = -1;
+ goto done;
+ }
file_cleanup_list =
- gl_list_create_empty (GL_LINKEDHASH_LIST,
- clean_temp_string_equals, clean_temp_string_hash,
- NULL, false);
+ gl_list_nx_create_empty (GL_LINKEDHASH_LIST,
+ clean_temp_string_equals,
+ clean_temp_string_hash,
+ NULL, false);
+ if (file_cleanup_list == NULL)
+ {
+ ret = -1;
+ goto done;
+ }
}
/* Add absolute_file_name to file_cleanup_list, without duplicates. */
if (gl_list_search (file_cleanup_list, absolute_file_name) == NULL)
- gl_list_add_first (file_cleanup_list, xstrdup (absolute_file_name));
+ {
+ absolute_file_name = strdup (absolute_file_name);
+ if (absolute_file_name == NULL)
+ {
+ ret = -1;
+ goto done;
+ }
+ if (gl_list_nx_add_first (file_cleanup_list, absolute_file_name)
+ == NULL)
+ {
+ ret = -1;
+ goto done;
+ }
+ }
+ done:
if (mt) gl_lock_unlock (file_cleanup_list_lock);
+
+ return ret;
}
/* Unregister the given ABSOLUTE_FILE_NAME as being a file that needs to be