Alexandre Julliard : makedep: Copy the makefile to append dependencies instead of truncating in place.

Alexandre Julliard julliard at winehq.org
Mon Jan 12 10:41:21 CST 2009


Module: wine
Branch: master
Commit: c31a36412caaa8be59d442c28bae45996c6304d7
URL:    http://source.winehq.org/git/wine.git/?a=commit;h=c31a36412caaa8be59d442c28bae45996c6304d7

Author: Alexandre Julliard <julliard at winehq.org>
Date:   Mon Jan 12 14:43:15 2009 +0100

makedep: Copy the makefile to append dependencies instead of truncating in place.

---

 tools/makedep.c |   59 ++++++++++++++++++++++++++++++++++++++++++++++--------
 1 files changed, 50 insertions(+), 9 deletions(-)

diff --git a/tools/makedep.c b/tools/makedep.c
index 2571359..7941ea0 100644
--- a/tools/makedep.c
+++ b/tools/makedep.c
@@ -866,6 +866,33 @@ static int output_src( FILE *file, INCL_FILE *pFile, int *column )
 
 
 /*******************************************************************
+ *         create_temp_file
+ */
+static FILE *create_temp_file( char **tmp_name )
+{
+    char *name = xmalloc( strlen(OutputFileName) + 13 );
+    unsigned int i, id = getpid();
+    int fd;
+    FILE *ret = NULL;
+
+    for (i = 0; i < 100; i++)
+    {
+        sprintf( name, "%s.tmp%08x", OutputFileName, id );
+        if ((fd = open( name, O_RDWR | O_CREAT | O_EXCL, 0600 )) != -1)
+        {
+            ret = fdopen( fd, "w" );
+            break;
+        }
+        if (errno != EEXIST) break;
+        id += 7777;
+    }
+    if (!ret) fatal_error( "failed to create output file for '%s'\n", OutputFileName );
+    *tmp_name = name;
+    return ret;
+}
+
+
+/*******************************************************************
  *         output_dependencies
  */
 static void output_dependencies(void)
@@ -873,19 +900,23 @@ static void output_dependencies(void)
     INCL_FILE *pFile;
     int i, column;
     FILE *file = NULL;
-    char *buffer;
+    char *tmp_name = NULL;
 
-    if (Separator && ((file = fopen( OutputFileName, "r+" ))))
+    if (Separator && ((file = fopen( OutputFileName, "r" ))))
     {
-        while ((buffer = get_line( file )))
+        char buffer[1024];
+        FILE *tmp_file = create_temp_file( &tmp_name );
+
+        while (fgets( buffer, sizeof(buffer), file ))
         {
-            if (strncmp( buffer, Separator, strlen(Separator) )) continue;
-            ftruncate( fileno(file), ftell(file) );
-            fseek( file, 0L, SEEK_END );
-            break;
+            if (fwrite( buffer, 1, strlen(buffer), tmp_file ) != strlen(buffer))
+                fatal_error( "error writing to %s\n", tmp_name );
+            if (!strncmp( buffer, Separator, strlen(Separator) )) break;
         }
+        fclose( file );
+        file = tmp_file;
     }
-    if (!file)
+    else
     {
         if (!(file = fopen( OutputFileName, Separator ? "a" : "w" )))
         {
@@ -902,7 +933,17 @@ static void output_dependencies(void)
                                                  pFile, &column );
         fprintf( file, "\n" );
     }
-    fclose(file);
+    fclose( file );
+
+    if (tmp_name)
+    {
+        if (rename( tmp_name, OutputFileName ) == -1)
+        {
+            unlink( tmp_name );
+            fatal_error( "failed to rename output file to '%s'\n", OutputFileName );
+        }
+        free( tmp_name );
+    }
 }
 
 




More information about the wine-cvs mailing list