You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucy.apache.org by ma...@apache.org on 2012/08/22 01:38:39 UTC

[lucy-commits] svn commit: r1375855 - in /lucy/branches/0.3: ./ charmonizer/src/Charmonizer/ charmonizer/src/Charmonizer/Core/

Author: marvin
Date: Tue Aug 21 23:38:39 2012
New Revision: 1375855

URL: http://svn.apache.org/viewvc?rev=1375855&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.

(This is a backport of mainline commits r1374811 and r1375850, adapted
to work with the changes from LUCY-243.)

Modified:
    lucy/branches/0.3/   (props changed)
    lucy/branches/0.3/charmonizer/src/Charmonizer/Core/Compiler.c
    lucy/branches/0.3/charmonizer/src/Charmonizer/Core/OperatingSystem.c
    lucy/branches/0.3/charmonizer/src/Charmonizer/Core/OperatingSystem.h
    lucy/branches/0.3/charmonizer/src/Charmonizer/Core/Util.c
    lucy/branches/0.3/charmonizer/src/Charmonizer/Probe.c

Propchange: lucy/branches/0.3/
------------------------------------------------------------------------------
  Merged /lucy/trunk:r1374811,1375850

Modified: lucy/branches/0.3/charmonizer/src/Charmonizer/Core/Compiler.c
URL: http://svn.apache.org/viewvc/lucy/branches/0.3/charmonizer/src/Charmonizer/Core/Compiler.c?rev=1375855&r1=1375854&r2=1375855&view=diff
==============================================================================
--- lucy/branches/0.3/charmonizer/src/Charmonizer/Core/Compiler.c (original)
+++ lucy/branches/0.3/charmonizer/src/Charmonizer/Core/Compiler.c Tue Aug 21 23:38:39 2012
@@ -168,15 +168,15 @@ CC_compile_exe(const char *source_path, 
     /* TODO: Key this off the compiler supplied as argument, not the compiler
      * used to compile Charmonizer. */
     sprintf(junk, "%s.obj", exe_name);
-    if ( -1 ==  remove(junk) ) {
+    if (!OS_remove(junk)) {
       Util_warn("Error removing [%s] due to the following error: [%s]\n", junk, strerror(errno));    
     }
     sprintf(junk, "%s.ilk", exe_name);
-    if ( -1 ==  remove(junk) ) {
+    if (!OS_remove(junk)) {
       Util_warn("Error removing [%s] due to the following error: [%s]\n", junk, strerror(errno));    
     }
     sprintf(junk, "%s.pdb", exe_name);
-    if ( -1 ==  remove(junk) ) {
+    if (!OS_remove(junk)) {
       Util_warn("Error removing [%s] due to the following error: [%s]\n", junk, strerror(errno));    
     }
 #endif
@@ -249,7 +249,7 @@ CC_test_compile(const char *source, size
     }
     compile_succeeded = CC_compile_obj(TRY_SOURCE_PATH, TRY_BASENAME,
                                        source, source_len);
-    if ( -1 ==  remove(try_obj_name) ) {
+    if (!OS_remove(try_obj_name)) {
       Util_warn("Error removing [%s] due to the following error: [%s]\n", try_obj_name, strerror(errno));    
     }
     return compile_succeeded;
@@ -280,11 +280,11 @@ CC_capture_output(const char *source, si
     }
 
     /* Remove all the files we just created. */
-    if ( -1 ==  remove(TRY_SOURCE_PATH) ) {
+    if (!OS_remove(TRY_SOURCE_PATH)) {
       Util_warn("Error removing [%s] due to the following error: [%s]\n", TRY_SOURCE_PATH, strerror(errno));    
     }
     OS_remove_exe(TRY_BASENAME);
-    if ( -1 ==  remove(TARGET_PATH) ) {
+    if (!OS_remove(TARGET_PATH)) {
       Util_warn("Error removing [%s] due to the following error: [%s]\n", TARGET_PATH, strerror(errno));    
     }
 

Modified: lucy/branches/0.3/charmonizer/src/Charmonizer/Core/OperatingSystem.c
URL: http://svn.apache.org/viewvc/lucy/branches/0.3/charmonizer/src/Charmonizer/Core/OperatingSystem.c?rev=1375855&r1=1375854&r2=1375855&view=diff
==============================================================================
--- lucy/branches/0.3/charmonizer/src/Charmonizer/Core/OperatingSystem.c (original)
+++ lucy/branches/0.3/charmonizer/src/Charmonizer/Core/OperatingSystem.c Tue Aug 21 23:38:39 2012
@@ -106,11 +106,46 @@ OS_dev_null(void) {
     return dev_null;
 }
 
+int
+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.
+     */
+    int retval;
+
+    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) {
+        retval = !remove(temp_name);
+    }
+    else {
+        // Error during rename, remove using old name.
+        retval = !remove(name);
+    }
+
+    free(temp_name);
+    return retval;
+}
+
 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);
 }
 
@@ -118,7 +153,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/branches/0.3/charmonizer/src/Charmonizer/Core/OperatingSystem.h
URL: http://svn.apache.org/viewvc/lucy/branches/0.3/charmonizer/src/Charmonizer/Core/OperatingSystem.h?rev=1375855&r1=1375854&r2=1375855&view=diff
==============================================================================
--- lucy/branches/0.3/charmonizer/src/Charmonizer/Core/OperatingSystem.h (original)
+++ lucy/branches/0.3/charmonizer/src/Charmonizer/Core/OperatingSystem.h Tue Aug 21 23:38:39 2012
@@ -25,6 +25,12 @@
 extern "C" {
 #endif
 
+/* Safely remove a file named [name]. Needed because of Windows quirks.
+ * Returns true on success, false on failure.
+ */
+int
+chaz_OS_remove(const char *name);
+
 /* Remove an executable file named [name], appending the exe_ext if needed.
  */
 void
@@ -79,6 +85,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/branches/0.3/charmonizer/src/Charmonizer/Core/Util.c
URL: http://svn.apache.org/viewvc/lucy/branches/0.3/charmonizer/src/Charmonizer/Core/Util.c?rev=1375855&r1=1375854&r2=1375855&view=diff
==============================================================================
--- lucy/branches/0.3/charmonizer/src/Charmonizer/Core/Util.c (original)
+++ lucy/branches/0.3/charmonizer/src/Charmonizer/Core/Util.c Tue Aug 21 23:38:39 2012
@@ -21,6 +21,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include "Charmonizer/Core/Util.h"
+#include "Charmonizer/Core/OperatingSystem.h"
 
 /* Global verbosity setting. */
 int Util_verbosity = 1;
@@ -133,7 +134,7 @@ Util_warn(const char* format, ...) {
 int
 Util_remove_and_verify(const char *file_path) {
     /* Try to remove the file. */
-    if ( -1 ==  remove(file_path) ) {
+    if (!OS_remove(file_path)) {
       Util_warn("Error removing [%s] due to the following error: [%s]\n", file_path, strerror(errno));    
     }
 

Modified: lucy/branches/0.3/charmonizer/src/Charmonizer/Probe.c
URL: http://svn.apache.org/viewvc/lucy/branches/0.3/charmonizer/src/Charmonizer/Probe.c?rev=1375855&r1=1375854&r2=1375855&view=diff
==============================================================================
--- lucy/branches/0.3/charmonizer/src/Charmonizer/Probe.c (original)
+++ lucy/branches/0.3/charmonizer/src/Charmonizer/Probe.c Tue Aug 21 23:38:39 2012
@@ -85,6 +85,6 @@ S_write_charm_h(void) {
 
 static void
 S_remove_charm_h(void) {
-    remove("_charm.h");
+    OS_remove("_charm.h");
 }