New module 'fchdir'.
[gnulib.git] / lib / clean-temp.c
index cffe959..7acc688 100644 (file)
    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
 
 
-#ifdef HAVE_CONFIG_H
-# include "config.h"
-#endif
+#include <config.h>
 
 /* Specification.  */
 #include "clean-temp.h"
 
 #include <errno.h>
+#include <fcntl.h>
 #include <limits.h>
 #include <stdbool.h>
 #include <stdlib.h>
 #include "xallocsa.h"
 #include "gl_linkedhash_list.h"
 #include "gettext.h"
+#if GNULIB_FWRITEERROR
+# include "fwriteerror.h"
+#endif
+#if GNULIB_CLOSE_STREAM
+# include "close-stream.h"
+#endif
+#if GNULIB_FCNTL_SAFER
+# include "fcntl--.h"
+#endif
+#if GNULIB_FOPEN_SAFER
+# include "stdio--.h"
+#endif
 
 #define _(str) gettext (str)
 
+/* GNU Hurd doesn't have PATH_MAX.  */
+#ifndef PATH_MAX
+# ifdef MAXPATHLEN
+#  define PATH_MAX MAXPATHLEN
+# else
+#  define PATH_MAX 1024
+# endif
+#endif
+
+#ifndef uintptr_t
+# define uintptr_t unsigned long
+#endif
+
+#if !GNULIB_FCNTL_SAFER
+/* The results of open() in this file are not used with fchdir,
+   therefore save some unnecessary work in fchdir.c.  */
+# undef open
+# undef close
+#endif
+
 
 /* The use of 'volatile' in the types below (and ISO C 99 section 5.1.2.3.(5))
    ensure that while constructing or modifying the data structures, the field
@@ -72,6 +103,9 @@ static struct
   size_t tempdir_allocated;
 } cleanup_list /* = { NULL, 0, 0 } */;
 
+/* List of all open file descriptors to temporary files.  */
+static gl_list_t /* <int> */ volatile descriptors;
+
 
 /* For the subdirs and for the files, we use a gl_list_t of type LINKEDHASH.
    Why?  We need a data structure that
@@ -127,8 +161,8 @@ static struct
 static bool
 string_equals (const void *x1, const void *x2)
 {
-  const char *s1 = x1;
-  const char *s2 = x2;
+  const char *s1 = (const char *) x1;
+  const char *s2 = (const char *) x2;
   return strcmp (s1, s2) == 0;
 }
 
@@ -140,7 +174,7 @@ string_equals (const void *x1, const void *x2)
 static size_t
 string_hash (const void *x)
 {
-  const char *s = x;
+  const char *s = (const char *) x;
   size_t h = 0;
 
   for (; *s; s++)
@@ -156,6 +190,25 @@ cleanup ()
 {
   size_t i;
 
+  /* First close all file descriptors to temporary files.  */
+  {
+    gl_list_t fds = descriptors;
+
+    if (fds != NULL)
+      {
+       gl_list_iterator_t iter;
+       const void *element;
+
+       iter = gl_list_iterator (fds);
+       while (gl_list_iterator_next (&iter, &element, NULL))
+         {
+           int fd = (int) (uintptr_t) element;
+           close (fd);
+         }
+       gl_list_iterator_free (&iter);
+      }
+  }
+
   for (i = 0; i < cleanup_list.tempdir_count; i++)
     {
       struct tempdir *dir = cleanup_list.tempdir_list[i];
@@ -205,7 +258,7 @@ create_temp_dir (const char *prefix, const char *parentdir,
   struct tempdir * volatile *tmpdirp = NULL;
   struct tempdir *tmpdir;
   size_t i;
-  char *template;
+  char *xtemplate;
   char *tmpdirname;
 
   /* See whether it can take the slot of an earlier temporary directory
@@ -227,8 +280,7 @@ create_temp_dir (const char *prefix, const char *parentdir,
          size_t old_allocated = cleanup_list.tempdir_allocated;
          size_t new_allocated = 2 * cleanup_list.tempdir_allocated + 1;
          struct tempdir * volatile *new_array =
-           (struct tempdir * volatile *)
-           xmalloc (new_allocated * sizeof (struct tempdir * volatile));
+           XNMALLOC (new_allocated, struct tempdir * volatile);
 
          if (old_allocated == 0)
            /* First use of this facility.  Register the cleanup handler.  */
@@ -260,7 +312,7 @@ create_temp_dir (const char *prefix, const char *parentdir,
     }
 
   /* Initialize a 'struct tempdir'.  */
-  tmpdir = (struct tempdir *) xmalloc (sizeof (struct tempdir));
+  tmpdir = XMALLOC (struct tempdir);
   tmpdir->dirname = NULL;
   tmpdir->cleanup_verbose = cleanup_verbose;
   tmpdir->subdirs = gl_list_create_empty (GL_LINKEDHASH_LIST,
@@ -269,15 +321,15 @@ create_temp_dir (const char *prefix, const char *parentdir,
                                        string_equals, string_hash, false);
 
   /* Create the temporary directory.  */
-  template = (char *) xallocsa (PATH_MAX);
-  if (path_search (template, PATH_MAX, parentdir, prefix, parentdir == NULL))
+  xtemplate = (char *) xallocsa (PATH_MAX);
+  if (path_search (xtemplate, PATH_MAX, parentdir, prefix, parentdir == NULL))
     {
       error (0, errno,
             _("cannot find a temporary directory, try setting $TMPDIR"));
       goto quit;
     }
   block_fatal_signals ();
-  tmpdirname = mkdtemp (template);
+  tmpdirname = mkdtemp (xtemplate);
   if (tmpdirname != NULL)
     {
       tmpdir->dirname = tmpdirname;
@@ -288,7 +340,7 @@ create_temp_dir (const char *prefix, const char *parentdir,
     {
       error (0, errno,
             _("cannot create a temporary directory using template \"%s\""),
-            template);
+            xtemplate);
       goto quit;
     }
   /* Replace tmpdir->dirname with a copy that has indefinite extent.
@@ -296,11 +348,11 @@ create_temp_dir (const char *prefix, const char *parentdir,
      block because then the cleanup handler would not remove the directory
      if xstrdup fails.  */
   tmpdir->dirname = xstrdup (tmpdirname);
-  freesa (template);
+  freesa (xtemplate);
   return (struct temp_dir *) tmpdir;
 
  quit:
-  freesa (template);
+  freesa (xtemplate);
   return NULL;
 }
 
@@ -375,48 +427,70 @@ unregister_temp_subdir (struct temp_dir *dir,
     }
 }
 
-/* Remove a file, with optional error message.  */
-static void
+/* Remove a file, with optional error message.
+   Return 0 upon success, or -1 if there was some problem.  */
+static int
 do_unlink (struct temp_dir *dir, const char *absolute_file_name)
 {
   if (unlink (absolute_file_name) < 0 && dir->cleanup_verbose
       && errno != ENOENT)
-    error (0, errno, _("cannot remove temporary file %s"), absolute_file_name);
+    {
+      error (0, errno, _("cannot remove temporary file %s"), absolute_file_name);
+      return -1;
+    }
+  return 0;
 }
 
-/* Remove a directory, with optional error message.  */
-static void
+/* Remove a directory, with optional error message.
+   Return 0 upon success, or -1 if there was some problem.  */
+static int
 do_rmdir (struct temp_dir *dir, const char *absolute_dir_name)
 {
   if (rmdir (absolute_dir_name) < 0 && dir->cleanup_verbose
       && errno != ENOENT)
-    error (0, errno,
-          _("cannot remove temporary directory %s"), absolute_dir_name);
+    {
+      error (0, errno,
+            _("cannot remove temporary directory %s"), absolute_dir_name);
+      return -1;
+    }
+  return 0;
 }
 
-/* Remove the given ABSOLUTE_FILE_NAME and unregister it.  */
-void
+/* Remove the given ABSOLUTE_FILE_NAME and unregister it.
+   Return 0 upon success, or -1 if there was some problem.  */
+int
 cleanup_temp_file (struct temp_dir *dir,
                   const char *absolute_file_name)
 {
-  do_unlink (dir, absolute_file_name);
+  int err;
+
+  err = do_unlink (dir, absolute_file_name);
   unregister_temp_file (dir, absolute_file_name);
+
+  return err;
 }
 
-/* Remove the given ABSOLUTE_DIR_NAME and unregister it.  */
-void
+/* Remove the given ABSOLUTE_DIR_NAME and unregister it.
+   Return 0 upon success, or -1 if there was some problem.  */
+int
 cleanup_temp_subdir (struct temp_dir *dir,
                     const char *absolute_dir_name)
 {
-  do_rmdir (dir, absolute_dir_name);
+  int err;
+
+  err = do_rmdir (dir, absolute_dir_name);
   unregister_temp_subdir (dir, absolute_dir_name);
+
+  return err;
 }
 
-/* Remove all registered files and subdirectories inside DIR.  */
-void
+/* Remove all registered files and subdirectories inside DIR.
+   Return 0 upon success, or -1 if there was some problem.  */
+int
 cleanup_temp_dir_contents (struct temp_dir *dir)
 {
   struct tempdir *tmpdir = (struct tempdir *)dir;
+  int err = 0;
   gl_list_t list;
   gl_list_iterator_t iter;
   const void *element;
@@ -429,7 +503,7 @@ cleanup_temp_dir_contents (struct temp_dir *dir)
     {
       char *file = (char *) element;
 
-      do_unlink (dir, file);
+      err |= do_unlink (dir, file);
       gl_list_remove_node (list, node);
       /* Now only we can free file.  */
       free (file);
@@ -443,24 +517,28 @@ cleanup_temp_dir_contents (struct temp_dir *dir)
     {
       char *subdir = (char *) element;
 
-      do_rmdir (dir, subdir);
+      err |= do_rmdir (dir, subdir);
       gl_list_remove_node (list, node);
       /* Now only we can free subdir.  */
       free (subdir);
     }
   gl_list_iterator_free (&iter);
+
+  return err;
 }
 
 /* Remove all registered files and subdirectories inside DIR and DIR itself.
-   DIR cannot be used any more after this call.  */
-void
+   DIR cannot be used any more after this call.
+   Return 0 upon success, or -1 if there was some problem.  */
+int
 cleanup_temp_dir (struct temp_dir *dir)
 {
   struct tempdir *tmpdir = (struct tempdir *)dir;
+  int err = 0;
   size_t i;
 
-  cleanup_temp_dir_contents (dir);
-  do_rmdir (dir, tmpdir->dirname);
+  err |= cleanup_temp_dir_contents (dir);
+  err |= do_rmdir (dir, tmpdir->dirname);
 
   for (i = 0; i < cleanup_list.tempdir_count; i++)
     if (cleanup_list.tempdir_list[i] == tmpdir)
@@ -477,9 +555,168 @@ cleanup_temp_dir (struct temp_dir *dir)
        /* Now only we can free the tmpdir->dirname and tmpdir itself.  */
        free (tmpdir->dirname);
        free (tmpdir);
-       return;
+       return err;
       }
 
   /* The user passed an invalid DIR argument.  */
   abort ();
 }
+
+
+/* Register a file descriptor to be closed.  */
+static void
+register_fd (int fd)
+{
+  if (descriptors == NULL)
+    descriptors = gl_list_create_empty (GL_LINKEDHASH_LIST, NULL, NULL, false);
+  gl_list_add_first (descriptors, (void *) (uintptr_t) fd);
+}
+
+/* Unregister a file descriptor to be closed.  */
+static void
+unregister_fd (int fd)
+{
+  gl_list_t fds = descriptors;
+  gl_list_node_t node;
+
+  if (fds == NULL)
+    /* descriptors should already contain fd.  */
+    abort ();
+  node = gl_list_search (fds, (void *) (uintptr_t) fd);
+  if (node == NULL)
+    /* descriptors should already contain fd.  */
+    abort ();
+  gl_list_remove_node (fds, node);
+}
+
+/* Open a temporary file in a temporary directory.
+   Registers the resulting file descriptor to be closed.  */
+int
+open_temp (const char *file_name, int flags, mode_t mode)
+{
+  int fd;
+  int saved_errno;
+
+  block_fatal_signals ();
+  fd = open (file_name, flags, mode); /* actually open or open_safer */
+  saved_errno = errno;
+  if (fd >= 0)
+    register_fd (fd);
+  unblock_fatal_signals ();
+  errno = saved_errno;
+  return fd;
+}
+
+/* Open a temporary file in a temporary directory.
+   Registers the resulting file descriptor to be closed.  */
+FILE *
+fopen_temp (const char *file_name, const char *mode)
+{
+  FILE *fp;
+  int saved_errno;
+
+  block_fatal_signals ();
+  fp = fopen (file_name, mode); /* actually fopen or fopen_safer */
+  saved_errno = errno;
+  if (fp != NULL)
+    {
+      /* It is sufficient to register fileno (fp) instead of the entire fp,
+        because at cleanup time there is no need to do an fflush (fp); a
+        close (fileno (fp)) will be enough.  */
+      int fd = fileno (fp);
+      if (!(fd >= 0))
+       abort ();
+      register_fd (fd);
+    }
+  unblock_fatal_signals ();
+  errno = saved_errno;
+  return fp;
+}
+
+/* Close a temporary file in a temporary directory.
+   Unregisters the previously registered file descriptor.  */
+int
+close_temp (int fd)
+{
+  if (fd >= 0)
+    {
+      /* No blocking of signals is needed here, since a double close of a
+        file descriptor is harmless.  */
+      int result = close (fd);
+      int saved_errno = errno;
+
+      /* No race condition here: we assume a single-threaded program, hence
+        fd cannot be re-opened here.  */
+
+      unregister_fd (fd);
+
+      errno = saved_errno;
+      return result;
+    }
+  else
+    return close (fd);
+}
+
+/* Close a temporary file in a temporary directory.
+   Unregisters the previously registered file descriptor.  */
+int
+fclose_temp (FILE *fp)
+{
+  int fd = fileno (fp);
+  /* No blocking of signals is needed here, since a double close of a
+     file descriptor is harmless.  */
+  int result = fclose (fp);
+  int saved_errno = errno;
+
+  /* No race condition here: we assume a single-threaded program, hence
+     fd cannot be re-opened here.  */
+
+  unregister_fd (fd);
+
+  errno = saved_errno;
+  return result;
+}
+
+#if GNULIB_FWRITEERROR
+/* Like fwriteerror.
+   Unregisters the previously registered file descriptor.  */
+int
+fwriteerror_temp (FILE *fp)
+{
+  int fd = fileno (fp);
+  /* No blocking of signals is needed here, since a double close of a
+     file descriptor is harmless.  */
+  int result = fwriteerror (fp);
+  int saved_errno = errno;
+
+  /* No race condition here: we assume a single-threaded program, hence
+     fd cannot be re-opened here.  */
+
+  unregister_fd (fd);
+
+  errno = saved_errno;
+  return result;
+}
+#endif
+
+#if GNULIB_CLOSE_STREAM
+/* Like close_stream.
+   Unregisters the previously registered file descriptor.  */
+int
+close_stream_temp (FILE *fp)
+{
+  int fd = fileno (fp);
+  /* No blocking of signals is needed here, since a double close of a
+     file descriptor is harmless.  */
+  int result = close_stream (fp);
+  int saved_errno = errno;
+
+  /* No race condition here: we assume a single-threaded program, hence
+     fd cannot be re-opened here.  */
+
+  unregister_fd (fd);
+
+  errno = saved_errno;
+  return result;
+}
+#endif