summaryrefslogtreecommitdiff
path: root/lib/clean-temp-simple.c
diff options
context:
space:
mode:
authorBruno Haible <bruno@clisp.org>2021-03-23 00:08:38 +0100
committerBruno Haible <bruno@clisp.org>2021-03-23 00:14:39 +0100
commit391c29f5bb99a4a77d19ece68e52623d4890676a (patch)
treec5ff6186b1bb38fe6c5c4216b7b33444a16833b7 /lib/clean-temp-simple.c
parent83c222f7a65279e4e659a4caf8b85d34f91d5eeb (diff)
downloadgnulib-391c29f5bb99a4a77d19ece68e52623d4890676a.tar.gz
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.
Diffstat (limited to 'lib/clean-temp-simple.c')
-rw-r--r--lib/clean-temp-simple.c57
1 files changed, 45 insertions, 12 deletions
diff --git a/lib/clean-temp-simple.c b/lib/clean-temp-simple.c
index e4fd3e8e0d..ae8b3bbc98 100644
--- a/lib/clean-temp-simple.c
+++ b/lib/clean-temp-simple.c
@@ -32,10 +32,9 @@
#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"
@@ -259,6 +258,9 @@ cleanup_action (int sig _GL_UNUSED)
}
+/* 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)
@@ -267,17 +269,19 @@ 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;
}
@@ -301,29 +305,58 @@ clean_temp_unlink (const char *absolute_file_name, bool cleanup_verbose)
/* 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