[CMake] [PATCH] CPack fails because of a sharing violation

Claudio Bley b_l_e_y at ml1.net
Fri Sep 18 12:00:46 EDT 2009


Hi.

Trying to build & install Boost I ran into the following error when
CPack was called:

,----
| CPack: -   Install component: fusion_headers
| CMake Error at libs/fusion/cmake_install.cmake:31 (FILE):
|   file Problem setting modification time on file
|   "C:/build/vc90nmake/boost_1_40_0/_CPack_Packages/win32/NSIS/Boost-1.40.0-vc9/fusion_headers/include/boost-1_40/boost/fusion/include/iterator_facade.hpp"
| Call Stack (most recent call first):
|   libs/cmake_install.cmake:48 (INCLUDE)
|   cmake_install.cmake:37 (INCLUDE)
| 
| CPack Error: Error when generating package: Boost
| NMAKE : fatal error U1077: ""C:\Programme\CMake 2.6\bin\cpack.exe"": Return-Code "0x1"
| Stop.
`----

I debugged cpack.exe and realized that the error occured in
Source/cmSystemTools.cxx:CopyFileTime() when trying to open the target
file.

GetLastError() returned ERROR_SHARING_VIOLATION. 

According to
http://support.microsoft.com/?scid=kb%3Ben-us%3B316609&x=17&y=20 one
should retry attempting to open the file until successfull.

I implemented this approach (see the following patch) and succeeded in
building an installer package for Boost.

Of course, there a some other places where CreateFile is called and this
problem might happen there also. It might be a good idea to generalize
this into an utility function.

Btw, do you realize that the cmSystemToolsWindowsHandle class is broken
according to its copy/assignment semantics?


Index: Source/cmSystemTools.cxx
===================================================================
RCS file: /cvsroot/CMake/CMake/Source/cmSystemTools.cxx,v
retrieving revision 1.401
diff -u -r1.401 cmSystemTools.cxx
--- Source/cmSystemTools.cxx	11 Sep 2009 12:18:13 -0000	1.401
+++ Source/cmSystemTools.cxx	18 Sep 2009 14:43:23 -0000
@@ -2134,11 +2134,26 @@
 bool cmSystemTools::CopyFileTime(const char* fromFile, const char* toFile)
 {
 #if defined(_WIN32) && !defined(__CYGWIN__)
+  const DWORD retryDelay_millisecs = 250;
+  const int maxRetries = 15;
+  int retries = 0;
+
   cmSystemToolsWindowsHandle hFrom =
     CreateFile(fromFile, GENERIC_READ, FILE_SHARE_READ, 0,
                OPEN_EXISTING, 0, 0);
-  cmSystemToolsWindowsHandle hTo =
-    CreateFile(toFile, GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0);
+
+  HANDLE hTry;
+
+  while ((hTry = CreateFile(toFile, GENERIC_WRITE, 
+                            FILE_SHARE_WRITE, 0, OPEN_EXISTING, 0, 0)) == INVALID_HANDLE_VALUE
+         && GetLastError() == ERROR_SHARING_VIOLATION
+         && ++retries <= maxRetries) 
+    {
+    Sleep(retryDelay_millisecs);
+    }
+
+  cmSystemToolsWindowsHandle hTo(hTry);
+
   if(!hFrom || !hTo)
     {
     return false;


Regards,
Claudio



More information about the CMake mailing list