diff options
Diffstat (limited to 'gcc')
-rw-r--r-- | gcc/java/ChangeLog | 5 | ||||
-rw-r--r-- | gcc/java/jcf-write.c | 18 |
2 files changed, 20 insertions, 3 deletions
diff --git a/gcc/java/ChangeLog b/gcc/java/ChangeLog index 703658d..502328e 100644 --- a/gcc/java/ChangeLog +++ b/gcc/java/ChangeLog @@ -1,3 +1,8 @@ +2002-05-08 Mark Mitchell <mark@codesourcery.com> + + * java/jcf-write.c (write_classfile): Write the file to a + temporary file and then rename it. + 2002-05-07 Tom Tromey <tromey@redhat.com> * gjavah.c (throwable_p): Use xstrdup, not strdup. diff --git a/gcc/java/jcf-write.c b/gcc/java/jcf-write.c index 2988c47..62e3e09 100644 --- a/gcc/java/jcf-write.c +++ b/gcc/java/jcf-write.c @@ -3374,16 +3374,28 @@ write_classfile (clas) if (class_file_name != NULL) { - FILE *stream = fopen (class_file_name, "wb"); + FILE *stream; + char *temporary_file_name; + + /* The .class file is initially written to a ".tmp" file so that + if multiple instances of the compiler are running at once + they do not see partially formed class files. */ + temporary_file_name = xmalloc (strlen (class_file_name) + + strlen (".tmp") + 1); + sprintf (temporary_file_name, "%s.tmp", class_file_name); + stream = fopen (temporary_file_name, "wb"); if (stream == NULL) - fatal_io_error ("can't open %s for writing", class_file_name); + fatal_io_error ("can't open %s for writing", temporary_file_name); jcf_dependency_add_target (class_file_name); init_jcf_state (state, work); chunks = generate_classfile (clas, state); write_chunks (stream, chunks); if (fclose (stream)) - fatal_io_error ("error closing %s", class_file_name); + fatal_io_error ("error closing %s", temporary_file_name); + if (rename (temporary_file_name, class_file_name) == -1) + fatal_io_error ("can't create %s", class_file_name); + free (temporary_file_name); free (class_file_name); } release_jcf_state (state); |