]> Savannah Git Hosting - gnulib.git/commitdiff
clean-temp-simple: Remove dependency upon xalloc, xalloc-die, xlist.
authorBruno Haible <bruno@clisp.org>
Mon, 22 Mar 2021 23:08:38 +0000 (00:08 +0100)
committerBruno Haible <bruno@clisp.org>
Mon, 22 Mar 2021 23:14:39 +0000 (00:14 +0100)
* 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
lib/clean-temp-private.h
lib/clean-temp-simple.c
lib/clean-temp-simple.h
lib/clean-temp.c
modules/clean-temp-simple

index d16c61e692d79447b3859a7db27fcbbdff3cccce..bd4dbe08de35c0d4678dfdf6ced0262c6e3d9bd6 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,24 @@
+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.
index 655eb3fbbf2aca4ef20c07524499abcb978f5f91..46dba23dd3ca384aac156da4dafcbf3d2877f016 100644 (file)
@@ -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);
 
index e4fd3e8e0da516145114c0de731809ce62c5f97b..ae8b3bbc98972193f367278b171a801dac85c054 100644 (file)
 #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
index dce6812047b3136c5da688ffd3abe2f6e6638f1f..9b9ab0745171dbbed2ea9ae18439e12e8174eb2d 100644 (file)
@@ -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.
index 4091d932730a6f39a6d94d6818c995298e341738..215c0d932226d204906e09a5cbf36ea5eed30e3b 100644 (file)
@@ -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;
index 4187ee62ab0812b9b167a45cfb15bc1a3dbae166..aac6d5c1dfd4c877d7003d0ce9ac7502c95bd21f 100644 (file)
@@ -17,10 +17,7 @@ thread-optim
 error
 fatal-signal
 rmdir
-xalloc
-xalloc-die
 linkedhash-list
-xlist
 gettext-h
 
 configure.ac: