>From 391c29f5bb99a4a77d19ece68e52623d4890676a Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Tue, 23 Mar 2021 00:08:38 +0100 Subject: [PATCH 2/3] 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. --- ChangeLog | 21 +++++++++++++++++ lib/clean-temp-private.h | 2 +- lib/clean-temp-simple.c | 57 +++++++++++++++++++++++++++++++++++++---------- lib/clean-temp-simple.h | 5 +++-- lib/clean-temp.c | 9 +++++--- modules/clean-temp-simple | 3 --- 6 files changed, 76 insertions(+), 21 deletions(-) diff --git a/ChangeLog b/ChangeLog index d16c61e..bd4dbe0 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,26 @@ 2021-03-22 Bruno Haible + 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 + clean-temp-simple: New module. * lib/clean-temp-simple.h: New file, based on lib/clean-temp.h. * lib/clean-temp-private.h: New file, based on lib/clean-temp.c. diff --git a/lib/clean-temp-private.h b/lib/clean-temp-private.h index 655eb3f..46dba23 100644 --- a/lib/clean-temp-private.h +++ b/lib/clean-temp-private.h @@ -75,7 +75,7 @@ extern size_t clean_temp_string_hash (const void *x); extern _GL_ASYNC_SAFE int clean_temp_asyncsafe_close (struct closeable_fd *element); extern void clean_temp_init_asyncsafe_close (void); -extern void clean_temp_init (void); +extern int clean_temp_init (void); extern int clean_temp_unlink (const char *absolute_file_name, bool cleanup_verbose); diff --git a/lib/clean-temp-simple.c b/lib/clean-temp-simple.c index e4fd3e8..ae8b3bb 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 diff --git a/lib/clean-temp-simple.h b/lib/clean-temp-simple.h index dce6812..9b9ab07 100644 --- a/lib/clean-temp-simple.h +++ b/lib/clean-temp-simple.h @@ -29,8 +29,9 @@ extern "C" { /* 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. */ -extern void register_temporary_file (const char *absolute_file_name); + Should be called before the file ABSOLUTE_FILE_NAME is created. + Return 0 upon success, or -1 if there was a memory allocation problem. */ +extern int register_temporary_file (const char *absolute_file_name); /* Unregister the given ABSOLUTE_FILE_NAME as being a file that needs to be removed. diff --git a/lib/clean-temp.c b/lib/clean-temp.c index 4091d93..215c0d9 100644 --- a/lib/clean-temp.c +++ b/lib/clean-temp.c @@ -178,7 +178,8 @@ create_temp_dir (const char *prefix, const char *parentdir, if (old_allocated == 0) { /* First use of this facility. */ - clean_temp_init (); + if (clean_temp_init () < 0) + xalloc_die (); } else { @@ -664,9 +665,11 @@ gen_register_open_temp (char *file_name_tmpl, int suffixlen, int saved_errno = errno; if (fd >= 0) { - clean_temp_init (); + if (clean_temp_init () < 0) + xalloc_die (); register_fd (fd); - register_temporary_file (file_name_tmpl); + if (register_temporary_file (file_name_tmpl) < 0) + xalloc_die (); } unblock_fatal_signals (); errno = saved_errno; diff --git a/modules/clean-temp-simple b/modules/clean-temp-simple index 4187ee6..aac6d5c 100644 --- a/modules/clean-temp-simple +++ b/modules/clean-temp-simple @@ -17,10 +17,7 @@ thread-optim error fatal-signal rmdir -xalloc -xalloc-die linkedhash-list -xlist gettext-h configure.ac: -- 2.7.4