You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucy.apache.org by nw...@apache.org on 2012/08/19 20:24:31 UTC

[lucy-commits] svn commit: r1374811 - in /lucy/trunk/charmonizer/src/Charmonizer: Core/Compiler.c Core/OperatingSystem.c Core/OperatingSystem.h Core/Util.c Probe.c

Author: nwellnhof
Date: Sun Aug 19 18:24:30 2012
New Revision: 1374811

URL: http://svn.apache.org/viewvc?rev=1374811&view=rev
Log:
LUCY-245 Safe Charmonizer file removal on Windows

When deleting files on Windows, it can happen that another process,
typically a virus scanner, still has an open handle on the file. This can
make the subsequent recreation of a file with the same name fail. As a
workaround, files are renamed to a random name before deletion.

This hopefully fixes build failures reported on MingW.

Modified:
    lucy/trunk/charmonizer/src/Charmonizer/Core/Compiler.c
    lucy/trunk/charmonizer/src/Charmonizer/Core/OperatingSystem.c
    lucy/trunk/charmonizer/src/Charmonizer/Core/OperatingSystem.h
    lucy/trunk/charmonizer/src/Charmonizer/Core/Util.c
    lucy/trunk/charmonizer/src/Charmonizer/Probe.c

Modified: lucy/trunk/charmonizer/src/Charmonizer/Core/Compiler.c
URL: http://svn.apache.org/viewvc/lucy/trunk/charmonizer/src/Charmonizer/Core/Compiler.c?rev=1374811&r1=1374810&r2=1374811&view=diff
==============================================================================
--- lucy/trunk/charmonizer/src/Charmonizer/Core/Compiler.c (original)
+++ lucy/trunk/charmonizer/src/Charmonizer/Core/Compiler.c Sun Aug 19 18:24:30 2012
@@ -219,11 +219,11 @@ CC_compile_exe(const char *source_path, 
     if (defines__MSC_VER) {
         /* Zap MSVC junk. */
         sprintf(junk, "%s.obj", exe_name);
-        remove(junk);
+        OS_remove(junk);
         sprintf(junk, "%s.ilk", exe_name);
-        remove(junk);
+        OS_remove(junk);
         sprintf(junk, "%s.pdb", exe_name);
-        remove(junk);
+        OS_remove(junk);
     }
 
     /* See if compilation was successful.  Remove the source file. */
@@ -295,7 +295,7 @@ CC_test_compile(const char *source) {
     }
     compile_succeeded = CC_compile_obj(TRY_SOURCE_PATH, TRY_BASENAME,
                                        source);
-    remove(try_obj_name);
+    OS_remove(try_obj_name);
     return compile_succeeded;
 }
 
@@ -324,9 +324,9 @@ CC_capture_output(const char *source, si
     }
 
     /* Remove all the files we just created. */
-    remove(TRY_SOURCE_PATH);
+    OS_remove(TRY_SOURCE_PATH);
     OS_remove_exe(TRY_BASENAME);
-    remove(TARGET_PATH);
+    OS_remove(TARGET_PATH);
 
     return captured_output;
 }

Modified: lucy/trunk/charmonizer/src/Charmonizer/Core/OperatingSystem.c
URL: http://svn.apache.org/viewvc/lucy/trunk/charmonizer/src/Charmonizer/Core/OperatingSystem.c?rev=1374811&r1=1374810&r2=1374811&view=diff
==============================================================================
--- lucy/trunk/charmonizer/src/Charmonizer/Core/OperatingSystem.c (original)
+++ lucy/trunk/charmonizer/src/Charmonizer/Core/OperatingSystem.c Sun Aug 19 18:24:30 2012
@@ -80,10 +80,43 @@ OS_dev_null(void) {
 }
 
 void
+OS_remove(const char *name) {
+    /*
+     * On Windows it can happen that another process, typically a
+     * virus scanner, still has an open handle on the file. This can
+     * make the subsequent recreation of a file with the same name
+     * fail. As a workaround, files are renamed to a random name
+     * before deletion.
+     */
+
+    static const size_t num_random_chars = 16;
+
+    size_t  name_len = strlen(name);
+    size_t  i;
+    char   *temp_name = (char*)malloc(name_len + num_random_chars + 1);
+
+    strcpy(temp_name, name);
+    for (i = 0; i < num_random_chars; i++) {
+        temp_name[name_len+i] = 'A' + rand() % 26;
+    }
+    temp_name[name_len+num_random_chars] = '\0';
+
+    if (rename(name, temp_name) == 0) {
+        remove(temp_name);
+    }
+    else {
+        // Error during rename, remove using old name.
+        remove(name);
+    }
+
+    free(temp_name);
+}
+
+void
 OS_remove_exe(const char *name) {
     char *exe_name = (char*)malloc(strlen(name) + strlen(exe_ext) + 1);
     sprintf(exe_name, "%s%s", name, exe_ext);
-    remove(exe_name);
+    OS_remove(exe_name);
     free(exe_name);
 }
 
@@ -91,7 +124,7 @@ void
 OS_remove_obj(const char *name) {
     char *obj_name = (char*)malloc(strlen(name) + strlen(obj_ext) + 1);
     sprintf(obj_name, "%s%s", name, obj_ext);
-    remove(obj_name);
+    OS_remove(obj_name);
     free(obj_name);
 }
 

Modified: lucy/trunk/charmonizer/src/Charmonizer/Core/OperatingSystem.h
URL: http://svn.apache.org/viewvc/lucy/trunk/charmonizer/src/Charmonizer/Core/OperatingSystem.h?rev=1374811&r1=1374810&r2=1374811&view=diff
==============================================================================
--- lucy/trunk/charmonizer/src/Charmonizer/Core/OperatingSystem.h (original)
+++ lucy/trunk/charmonizer/src/Charmonizer/Core/OperatingSystem.h Sun Aug 19 18:24:30 2012
@@ -25,6 +25,11 @@
 extern "C" {
 #endif
 
+/* Safely remove a file named [name]. Needed because of Windows quirks.
+ */
+void
+chaz_OS_remove(const char *name);
+
 /* Remove an executable file named [name], appending the exe_ext if needed.
  */
 void
@@ -79,6 +84,7 @@ void
 chaz_OS_init(void);
 
 #ifdef CHAZ_USE_SHORT_NAMES
+  #define OS_remove                    chaz_OS_remove
   #define OS_remove_exe                chaz_OS_remove_exe
   #define OS_remove_obj                chaz_OS_remove_obj
   #define OS_run_local                 chaz_OS_run_local

Modified: lucy/trunk/charmonizer/src/Charmonizer/Core/Util.c
URL: http://svn.apache.org/viewvc/lucy/trunk/charmonizer/src/Charmonizer/Core/Util.c?rev=1374811&r1=1374810&r2=1374811&view=diff
==============================================================================
--- lucy/trunk/charmonizer/src/Charmonizer/Core/Util.c (original)
+++ lucy/trunk/charmonizer/src/Charmonizer/Core/Util.c Sun Aug 19 18:24:30 2012
@@ -133,7 +133,7 @@ Util_warn(const char* format, ...) {
 int
 Util_remove_and_verify(const char *file_path) {
     /* Try to remove the file. */
-    remove(file_path);
+    OS_remove(file_path);
 
     /* Return what *might* be success or failure. */
     return Util_can_open_file(file_path) ? 0 : 1;

Modified: lucy/trunk/charmonizer/src/Charmonizer/Probe.c
URL: http://svn.apache.org/viewvc/lucy/trunk/charmonizer/src/Charmonizer/Probe.c?rev=1374811&r1=1374810&r2=1374811&view=diff
==============================================================================
--- lucy/trunk/charmonizer/src/Charmonizer/Probe.c (original)
+++ lucy/trunk/charmonizer/src/Charmonizer/Probe.c Sun Aug 19 18:24:30 2012
@@ -78,6 +78,6 @@ S_write_charm_h(void) {
 
 static void
 S_remove_charm_h(void) {
-    remove("_charm.h");
+    OS_remove("_charm.h");
 }