]> git.ipfire.org Git - thirdparty/gettext.git/commitdiff
New facility for temporary files in a temporary directory.
authorBruno Haible <bruno@clisp.org>
Mon, 10 Apr 2006 16:15:10 +0000 (16:15 +0000)
committerBruno Haible <bruno@clisp.org>
Tue, 23 Jun 2009 10:13:09 +0000 (12:13 +0200)
gettext-tools/lib/ChangeLog
gettext-tools/lib/Makefile.am
gettext-tools/lib/Makefile.msvc
gettext-tools/lib/Makefile.vms
gettext-tools/src/ChangeLog
gettext-tools/src/write-csharp.c
gettext-tools/src/write-java.c

index d9a64d6b3955b49952d0c8c995e084f367c5c4cf..3c30bd83742cc81156ea05b736183a7be99348e6 100644 (file)
@@ -1,3 +1,13 @@
+2006-04-09  Bruno Haible  <bruno@clisp.org>
+
+       * clean-temp.h: New file.
+       * clean-temp.c: New file.
+       * Makefile.am (libgettextlib_la_SOURCES): Add them.
+       * Makefile.msvc (OBJECTS): Add clean-temp.obj.
+       (clean-temp.obj): New rule.
+       * Makefile.vms (OBJECTS): Add clean-temp.obj.
+       (clean-temp.obj): New rule.
+
 2006-04-09  Bruno Haible  <bruno@clisp.org>
 
        * fatal-signal.c: Don't include string.h.
index 47d3a5277228d6488bb60228a4ff3c9e81e6381f..775f2a374e673fb9dfcfa0d5634f8e65fbae6d2f 100644 (file)
@@ -41,6 +41,7 @@ libgettextlib_la_SOURCES = \
   c-strcase.h c-strcasecmp.c c-strncasecmp.c \
   c-strcasestr.h c-strcasestr.c \
   classpath.h classpath.c \
+  clean-temp.h clean-temp.c \
   closeout.h closeout.c \
   copy-file.h copy-file.c \
   csharpcomp.h csharpcomp.c \
index cb6aa7c7affa05a18dbfb55e33bb1b65d310ce85..20ad04b1c6f4b0cdfa6b61d851d011ab64f3f250 100644 (file)
@@ -88,6 +88,7 @@ OBJECTS = \
   c-strcasecmp.obj \
   c-strncasecmp.obj \
   classpath.obj \
+  clean-temp.obj \
   closeout.obj \
   copy-file.obj \
   csharpcomp.obj \
@@ -167,6 +168,9 @@ c-strncasecmp.obj : c-strncasecmp.c
 classpath.obj : classpath.c
        $(CC) $(INCLUDES) $(CFLAGS) $(PICFLAGS) -c classpath.c
 
+clean-temp.obj : clean-temp.c
+       $(CC) $(INCLUDES) $(CFLAGS) $(PICFLAGS) -c clean-temp.c
+
 closeout.obj : closeout.c
        $(CC) $(INCLUDES) $(CFLAGS) $(PICFLAGS) -c closeout.c
 
index 6ec3730872ba2b7d098623187e947ecd16c827dd..88cf294d2e677a964234f511937e612adb2841e1 100644 (file)
@@ -46,6 +46,7 @@ OBJECTS = \
   c-strcasecmp.obj, \
   c-strncasecmp.obj, \
   classpath.obj, \
+  clean-temp.obj, \
   closeout.obj, \
   copy-file.obj, \
   csharpcomp.obj, \
@@ -129,6 +130,9 @@ c-strncasecmp.obj : c-strncasecmp.c
 classpath.obj : classpath.c
        $(CC) $(INCLUDES) $(CFLAGS) /define=($(DEFS)) classpath.c
 
+clean-temp.obj : clean-temp.c
+       $(CC) $(INCLUDES) $(CFLAGS) /define=($(DEFS)) clean-temp.c
+
 closeout.obj : closeout.c
        $(CC) $(INCLUDES) $(CFLAGS) /define=($(DEFS)) closeout.c
 
index 21985ea2dc541e31d313f7e9136d983fe55f298a..af3474da20bd4f307117a4da8603947ff7b079d7 100644 (file)
@@ -1,3 +1,16 @@
+2006-04-09  Bruno Haible  <bruno@clisp.org>
+
+       * write-csharp.c: Include clean-temp.h. Don't include unistd.h,
+       mkdtemp.h, tmpdir.h, pathmax.h, fatal-signal.h, xallocsa.h.
+       (cleanup_list): Remove variable.
+       (cleanup): Remove function.
+       (msgdomain_write_csharp): Simplify handling of temporary directory.
+       * write-java.c: Include clean-temp.h. Don't include unistd.h,
+       mkdtemp.h, tmpdir.h, pathmax.h, fatal-signal.h.
+       (cleanup_list): Remove variable.
+       (cleanup): Remove function.
+       (msgdomain_write_java): Simplify handling of temporary directory.
+
 2006-04-03  Bruno Haible  <bruno@clisp.org>
 
        * x-glade.c (p_XML_GetCurrentLineNumber, p_XML_GetCurrentColumnNumber):
index 47d1194d89c63991aa424f7b0cf6ca9fd06e4018..dc633d388bdcfab83c98c69c13143f0efc690997 100644 (file)
@@ -1,5 +1,5 @@
 /* Writing C# satellite assemblies.
-   Copyright (C) 2003-2005 Free Software Foundation, Inc.
+   Copyright (C) 2003-2006 Free Software Foundation, Inc.
    Written by Bruno Haible <bruno@clisp.org>, 2003.
 
    This program is free software; you can redistribute it and/or modify
 # define S_IXOTH (S_IXUSR >> 6)
 #endif
 
-#if HAVE_UNISTD_H
-# include <unistd.h>
-#endif
-
 #ifdef __MINGW32__
 /* mingw's mkdir() function has 1 argument, but we pass 2 arguments.
    Therefore we have to disable the argument count checking.  */
 #include "xerror.h"
 #include "csharpcomp.h"
 #include "message.h"
-#include "mkdtemp.h"
 #include "msgfmt.h"
 #include "msgl-iconv.h"
-#include "pathmax.h"
 #include "plural-exp.h"
 #include "po-charset.h"
 #include "xalloc.h"
-#include "xallocsa.h"
 #include "pathname.h"
-#include "fatal-signal.h"
 #include "fwriteerror.h"
-#include "tmpdir.h"
+#include "clean-temp.h"
 #include "utf8-ucs4.h"
 #include "gettext.h"
 
@@ -592,46 +584,13 @@ write_csharp_code (FILE *stream, const char *culture_name, const char *class_nam
 }
 
 
-/* Asynchronously cleaning up temporary files, when we receive any of the
-   usually occurring signals whose default action is to terminate the
-   program.  */
-
-static struct
-{
-  const char *tmpdir;
-  const char *file_name;
-} cleanup_list;
-
-/* The signal handler.  It gets called asynchronously.  */
-static void
-cleanup ()
-{
-  /* First cleanup the files in the subdirectory.  */
-  {
-    const char *filename = cleanup_list.file_name;
-
-    if (filename != NULL)
-      unlink (filename);
-  }
-
-  /* Then cleanup the main temporary directory.  */
-  {
-    const char *filename = cleanup_list.tmpdir;
-
-    if (filename != NULL)
-      rmdir (filename);
-  }
-}
-
-
 int
 msgdomain_write_csharp (message_list_ty *mlp, const char *canon_encoding,
                        const char *resource_name, const char *locale_name,
                        const char *directory)
 {
   int retval;
-  char *template;
-  char *tmpdir;
+  struct temp_dir *tmpdir;
   char *culture_name;
   char *output_file;
   char *class_name;
@@ -670,41 +629,15 @@ but the C# .dll format doesn't support contexts\n")));
   /* Convert the messages to Unicode.  */
   iconv_message_list (mlp, canon_encoding, po_charset_utf8, NULL);
 
-  cleanup_list.tmpdir = NULL;
-  cleanup_list.file_name = NULL;
-  {
-    static bool cleanup_already_registered = false;
-    if (!cleanup_already_registered)
-      {
-       at_fatal_signal (&cleanup);
-       cleanup_already_registered = true;
-      }
-  }
-
   /* Create a temporary directory where we can put the C# file.
      A simple temporary file would also be possible but would require us to
      define our own variant of mkstemp(): On one hand the functions mktemp(),
      tmpnam(), tempnam() present a security risk, and on the other hand the
      function mkstemp() doesn't allow to specify a fixed suffix of the file.
      It is simpler to create a temporary directory.  */
-  template = (char *) xallocsa (PATH_MAX);
-  if (path_search (template, PATH_MAX, NULL, "msg", 1))
-    {
-      error (0, errno,
-            _("cannot find a temporary directory, try setting $TMPDIR"));
-      goto quit1;
-    }
-  block_fatal_signals ();
-  tmpdir = mkdtemp (template);
-  cleanup_list.tmpdir = tmpdir;
-  unblock_fatal_signals ();
+  tmpdir = create_temp_dir ("msg");
   if (tmpdir == NULL)
-    {
-      error (0, errno,
-            _("cannot create a temporary directory using template \"%s\""),
-            template);
-      goto quit1;
-    }
+    goto quit1;
 
   /* Assign a default value to the resource name.  */
   if (resource_name == NULL)
@@ -738,7 +671,6 @@ but the C# .dll format doesn't support contexts\n")));
        culture_name = xstrdup ("uz-UZ-Latn");
       }
   }
-  
 
   /* Compute the output file name.  This code must be kept consistent with
      intl.cs, function GetSatelliteAssembly().  */
@@ -754,7 +686,7 @@ but the C# .dll format doesn't support contexts\n")));
        {
          error (0, errno, _("failed to create directory \"%s\""), output_dir);
          free (output_dir);
-         goto quit3;
+         goto quit2;
        }
 
     output_file =
@@ -780,15 +712,17 @@ but the C# .dll format doesn't support contexts\n")));
 
   /* Compute the temporary C# file name.  It must end in ".cs", so that
      the C# compiler recognizes that it is C# source code.  */
-  csharp_file_name = concatenated_pathname (tmpdir, "resset.cs", NULL);
+  csharp_file_name =
+    concatenated_pathname (tmpdir->dir_name, "resset.cs", NULL);
 
   /* Create the C# file.  */
-  cleanup_list.file_name = csharp_file_name;
+  enqueue_temp_file (tmpdir, csharp_file_name);
   csharp_file = fopen (csharp_file_name, "w");
   if (csharp_file == NULL)
     {
       error (0, errno, _("failed to create \"%s\""), csharp_file_name);
-      goto quit4;
+      dequeue_temp_file (tmpdir, csharp_file_name);
+      goto quit3;
     }
 
   write_csharp_code (csharp_file, culture_name, class_name, mlp);
@@ -797,7 +731,7 @@ but the C# .dll format doesn't support contexts\n")));
     {
       error (0, errno, _("error while writing \"%s\" file"), csharp_file_name);
       fclose (csharp_file);
-      goto quit5;
+      goto quit3;
     }
 
   /* Make it possible to override the .dll location.  This is
@@ -814,24 +748,18 @@ but the C# .dll format doesn't support contexts\n")));
                            output_file, true, false, verbose))
     {
       error (0, 0, _("compilation of C# class failed, please try --verbose"));
-      goto quit5;
+      goto quit3;
     }
 
   retval = 0;
 
- quit5:
-  unlink (csharp_file_name);
- quit4:
-  cleanup_list.file_name = NULL;
+ quit3:
   free (csharp_file_name);
   free (class_name);
   free (output_file);
- quit3:
+ quit2:
   free (culture_name);
-  rmdir (tmpdir);
+  cleanup_temp_dir (tmpdir);
  quit1:
-  cleanup_list.tmpdir = NULL;
-  freesa (template);
-  /* Here we could unregister the cleanup() handler.  */
   return retval;
 }
index b197cd866f48176d3963bbe037d1a6cb313b0c41..04c38909d3de606167f1aaa75d440cf7cde22f5d 100644 (file)
@@ -1,5 +1,5 @@
 /* Writing Java ResourceBundles.
-   Copyright (C) 2001-2003, 2005 Free Software Foundation, Inc.
+   Copyright (C) 2001-2003, 2005-2006 Free Software Foundation, Inc.
    Written by Bruno Haible <haible@clisp.cons.org>, 2001.
 
    This program is free software; you can redistribute it and/or modify
 # define S_IXUSR 00100
 #endif
 
-#if HAVE_UNISTD_H
-# include <unistd.h>
-#endif
-
 #ifdef __MINGW32__
 /* mingw's mkdir() function has 1 argument, but we pass 2 arguments.
    Therefore we have to disable the argument count checking.  */
 #include "xerror.h"
 #include "javacomp.h"
 #include "message.h"
-#include "mkdtemp.h"
 #include "msgfmt.h"
 #include "msgl-iconv.h"
-#include "pathmax.h"
 #include "plural-exp.h"
 #include "po-charset.h"
 #include "xalloc.h"
 #include "xallocsa.h"
 #include "pathname.h"
-#include "fatal-signal.h"
 #include "fwriteerror.h"
-#include "tmpdir.h"
+#include "clean-temp.h"
 #include "utf8-ucs4.h"
 #include "gettext.h"
 
@@ -894,50 +887,6 @@ write_java_code (FILE *stream, const char *class_name, message_list_ty *mlp,
 }
 
 
-/* Asynchronously cleaning up temporary files, when we receive any of the
-   usually occurring signals whose default action is to terminate the
-   program.  */
-
-static struct
-{
-  const char *tmpdir;
-  unsigned int subdir_count;
-  const char * const *subdir;
-  const char *file_name;
-} cleanup_list;
-
-/* The signal handler.  It gets called asynchronously.  */
-static void
-cleanup ()
-{
-  unsigned int i;
-
-  /* First cleanup the files in the subdirectory.  */
-  {
-    const char *filename = cleanup_list.file_name;
-
-    if (filename != NULL)
-      unlink (filename);
-  }
-
-  /* Then cleanup the subdirectories.  */
-  for (i = cleanup_list.subdir_count; i > 0; )
-    {
-      const char *filename = cleanup_list.subdir[--i];
-
-      rmdir (filename);
-    }
-
-  /* Then cleanup the main temporary directory.  */
-  {
-    const char *filename = cleanup_list.tmpdir;
-
-    if (filename != NULL)
-      rmdir (filename);
-  }
-}
-
-
 int
 msgdomain_write_java (message_list_ty *mlp, const char *canon_encoding,
                      const char *resource_name, const char *locale_name,
@@ -945,8 +894,7 @@ msgdomain_write_java (message_list_ty *mlp, const char *canon_encoding,
                      bool assume_java2)
 {
   int retval;
-  char *template;
-  char *tmpdir;
+  struct temp_dir *tmpdir;
   int ndots;
   char *class_name;
   char **subdirs;
@@ -982,37 +930,10 @@ but the Java ResourceBundle format doesn't support contexts\n")));
   /* Convert the messages to Unicode.  */
   iconv_message_list (mlp, canon_encoding, po_charset_utf8, NULL);
 
-  cleanup_list.tmpdir = NULL;
-  cleanup_list.subdir_count = 0;
-  cleanup_list.file_name = NULL;
-  {
-    static bool cleanup_already_registered = false;
-    if (!cleanup_already_registered)
-      {
-       at_fatal_signal (&cleanup);
-       cleanup_already_registered = true;
-      }
-  }
-
   /* Create a temporary directory where we can put the Java file.  */
-  template = (char *) xallocsa (PATH_MAX);
-  if (path_search (template, PATH_MAX, NULL, "msg", 1))
-    {
-      error (0, errno,
-            _("cannot find a temporary directory, try setting $TMPDIR"));
-      goto quit1;
-    }
-  block_fatal_signals ();
-  tmpdir = mkdtemp (template);
-  cleanup_list.tmpdir = tmpdir;
-  unblock_fatal_signals ();
+  tmpdir = create_temp_dir ("msg");
   if (tmpdir == NULL)
-    {
-      error (0, errno,
-            _("cannot create a temporary directory using template \"%s\""),
-            template);
-      goto quit1;
-    }
+    goto quit1;
 
   /* Assign a default value to the resource name.  */
   if (resource_name == NULL)
@@ -1041,7 +962,7 @@ but the Java ResourceBundle format doesn't support contexts\n")));
     const char *last_dir;
     int i;
 
-    last_dir = tmpdir;
+    last_dir = tmpdir->dir_name;
     p = resource_name;
     for (i = 0; i < ndots; i++)
       {
@@ -1073,28 +994,26 @@ but the Java ResourceBundle format doesn't support contexts\n")));
   {
     int i;
 
-    cleanup_list.subdir = (const char * const *) subdirs;
-    cleanup_list.subdir_count = 0;
     for (i = 0; i < ndots; i++)
       {
-       cleanup_list.subdir_count = i + 1;
+       enqueue_temp_subdir (tmpdir, subdirs[i]);
        if (mkdir (subdirs[i], S_IRUSR | S_IWUSR | S_IXUSR) < 0)
          {
            error (0, errno, _("failed to create \"%s\""), subdirs[i]);
-           while (i-- > 0)
-             rmdir (subdirs[i]);
+           dequeue_temp_subdir (tmpdir, subdirs[i]);
            goto quit3;
          }
       }
   }
 
   /* Create the Java file.  */
-  cleanup_list.file_name = java_file_name;
+  enqueue_temp_file (tmpdir, java_file_name);
   java_file = fopen (java_file_name, "w");
   if (java_file == NULL)
     {
       error (0, errno, _("failed to create \"%s\""), java_file_name);
-      goto quit4;
+      dequeue_temp_file (tmpdir, java_file_name);
+      goto quit3;
     }
 
   write_java_code (java_file, class_name, mlp, assume_java2);
@@ -1103,7 +1022,7 @@ but the Java ResourceBundle format doesn't support contexts\n")));
     {
       error (0, errno, _("error while writing \"%s\" file"), java_file_name);
       fclose (java_file);
-      goto quit5;
+      goto quit3;
     }
 
   /* Compile the Java file to a .class file.
@@ -1116,22 +1035,12 @@ but the Java ResourceBundle format doesn't support contexts\n")));
     {
       error (0, 0, _("\
 compilation of Java class failed, please try --verbose or set $JAVAC"));
-      goto quit5;
+      goto quit3;
     }
 
   retval = 0;
 
- quit5:
-  unlink (java_file_name);
- quit4:
-  cleanup_list.file_name = NULL;
-  {
-    int i;
-    for (i = ndots; i-- > 0; )
-      rmdir (subdirs[i]);
-  }
  quit3:
-  cleanup_list.subdir_count = 0;
   {
     int i;
     free (java_file_name);
@@ -1141,10 +1050,7 @@ compilation of Java class failed, please try --verbose or set $JAVAC"));
   freesa (subdirs);
   free (class_name);
  quit2:
-  rmdir (tmpdir);
+  cleanup_temp_dir (tmpdir);
  quit1:
-  cleanup_list.tmpdir = NULL;
-  freesa (template);
-  /* Here we could unregister the cleanup() handler.  */
   return retval;
 }