From: Bruno Haible Date: Mon, 10 Apr 2006 16:15:10 +0000 (+0000) Subject: New facility for temporary files in a temporary directory. X-Git-Tag: v0.15~237 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4bb1556c7888af65f38117e0bdddfdeb7ef63c6e;p=thirdparty%2Fgettext.git New facility for temporary files in a temporary directory. --- diff --git a/gettext-tools/lib/ChangeLog b/gettext-tools/lib/ChangeLog index d9a64d6b3..3c30bd837 100644 --- a/gettext-tools/lib/ChangeLog +++ b/gettext-tools/lib/ChangeLog @@ -1,3 +1,13 @@ +2006-04-09 Bruno Haible + + * 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 * fatal-signal.c: Don't include string.h. diff --git a/gettext-tools/lib/Makefile.am b/gettext-tools/lib/Makefile.am index 47d3a5277..775f2a374 100644 --- a/gettext-tools/lib/Makefile.am +++ b/gettext-tools/lib/Makefile.am @@ -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 \ diff --git a/gettext-tools/lib/Makefile.msvc b/gettext-tools/lib/Makefile.msvc index cb6aa7c7a..20ad04b1c 100644 --- a/gettext-tools/lib/Makefile.msvc +++ b/gettext-tools/lib/Makefile.msvc @@ -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 diff --git a/gettext-tools/lib/Makefile.vms b/gettext-tools/lib/Makefile.vms index 6ec373087..88cf294d2 100644 --- a/gettext-tools/lib/Makefile.vms +++ b/gettext-tools/lib/Makefile.vms @@ -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 diff --git a/gettext-tools/src/ChangeLog b/gettext-tools/src/ChangeLog index 21985ea2d..af3474da2 100644 --- a/gettext-tools/src/ChangeLog +++ b/gettext-tools/src/ChangeLog @@ -1,3 +1,16 @@ +2006-04-09 Bruno Haible + + * 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 * x-glade.c (p_XML_GetCurrentLineNumber, p_XML_GetCurrentColumnNumber): diff --git a/gettext-tools/src/write-csharp.c b/gettext-tools/src/write-csharp.c index 47d1194d8..dc633d388 100644 --- a/gettext-tools/src/write-csharp.c +++ b/gettext-tools/src/write-csharp.c @@ -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 , 2003. This program is free software; you can redistribute it and/or modify @@ -74,10 +74,6 @@ # define S_IXOTH (S_IXUSR >> 6) #endif -#if HAVE_UNISTD_H -# include -#endif - #ifdef __MINGW32__ /* mingw's mkdir() function has 1 argument, but we pass 2 arguments. Therefore we have to disable the argument count checking. */ @@ -90,18 +86,14 @@ #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; } diff --git a/gettext-tools/src/write-java.c b/gettext-tools/src/write-java.c index b197cd866..04c38909d 100644 --- a/gettext-tools/src/write-java.c +++ b/gettext-tools/src/write-java.c @@ -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 , 2001. This program is free software; you can redistribute it and/or modify @@ -57,10 +57,6 @@ # define S_IXUSR 00100 #endif -#if HAVE_UNISTD_H -# include -#endif - #ifdef __MINGW32__ /* mingw's mkdir() function has 1 argument, but we pass 2 arguments. Therefore we have to disable the argument count checking. */ @@ -72,18 +68,15 @@ #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; }