[Cmake-commits] [cmake-commits] king committed cmGeneratedFileStream.cxx 1.19 1.20 cmSystemTools.cxx 1.389 1.390 cmSystemTools.h 1.157 1.158

cmake-commits at cmake.org cmake-commits at cmake.org
Wed Apr 15 09:57:59 EDT 2009


Update of /cvsroot/CMake/CMake/Source
In directory public:/mounts/ram/cvs-serv17369/Source

Modified Files:
	cmGeneratedFileStream.cxx cmSystemTools.cxx cmSystemTools.h 
Log Message:
ENH: Move RenameFile to cmSystemTools

This moves the cmGeneratedFileStream::RenameFile method implementation
into cmSystemTools.  It works only within a single filesystem volume,
but is atomic when the operating system permits.


Index: cmSystemTools.h
===================================================================
RCS file: /cvsroot/CMake/CMake/Source/cmSystemTools.h,v
retrieving revision 1.157
retrieving revision 1.158
diff -C 2 -d -r1.157 -r1.158
*** cmSystemTools.h	5 Feb 2009 21:31:36 -0000	1.157
--- cmSystemTools.h	15 Apr 2009 13:57:57 -0000	1.158
***************
*** 167,170 ****
--- 167,174 ----
      const char* destination);
  
+   /** Rename a file or directory within a single disk volume (atomic
+       if possible).  */
+   static bool RenameFile(const char* oldname, const char* newname);
+ 
    ///! Compute the md5sum of a file
    static bool ComputeFileMD5(const char* source, char* md5out);

Index: cmSystemTools.cxx
===================================================================
RCS file: /cvsroot/CMake/CMake/Source/cmSystemTools.cxx,v
retrieving revision 1.389
retrieving revision 1.390
diff -C 2 -d -r1.389 -r1.390
*** cmSystemTools.cxx	5 Feb 2009 21:31:34 -0000	1.389
--- cmSystemTools.cxx	15 Apr 2009 13:57:57 -0000	1.390
***************
*** 1109,1112 ****
--- 1109,1162 ----
  }
  
+ //----------------------------------------------------------------------------
+ bool cmSystemTools::RenameFile(const char* oldname, const char* newname)
+ {
+ #ifdef _WIN32
+   /* On Windows the move functions will not replace existing files.
+      Check if the destination exists.  */
+   struct stat newFile;
+   if(stat(newname, &newFile) == 0)
+     {
+     /* The destination exists.  We have to replace it carefully.  The
+        MoveFileEx function does what we need but is not available on
+        Win9x.  */
+     OSVERSIONINFO osv;
+     DWORD attrs;
+ 
+     /* Make sure the destination is not read only.  */
+     attrs = GetFileAttributes(newname);
+     if(attrs & FILE_ATTRIBUTE_READONLY)
+       {
+       SetFileAttributes(newname, attrs & ~FILE_ATTRIBUTE_READONLY);
+       }
+ 
+     /* Check the windows version number.  */
+     osv.dwOSVersionInfoSize = sizeof(osv);
+     GetVersionEx(&osv);
+     if(osv.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS)
+       {
+       /* This is Win9x.  There is no MoveFileEx implementation.  We
+          cannot quite rename the file atomically.  Just delete the
+          destination and then move the file.  */
+       DeleteFile(newname);
+       return MoveFile(oldname, newname);
+       }
+     else
+       {
+       /* This is not Win9x.  Use the MoveFileEx implementation.  */
+       return MoveFileEx(oldname, newname, MOVEFILE_REPLACE_EXISTING);
+       }
+     }
+   else
+     {
+     /* The destination does not exist.  Just move the file.  */
+     return MoveFile(oldname, newname);
+     }
+ #else
+   /* On UNIX we have an OS-provided call to do this atomically.  */
+   return rename(oldname, newname) == 0;
+ #endif
+ }
+ 
  bool cmSystemTools::ComputeFileMD5(const char* source, char* md5out)
  {

Index: cmGeneratedFileStream.cxx
===================================================================
RCS file: /cvsroot/CMake/CMake/Source/cmGeneratedFileStream.cxx,v
retrieving revision 1.19
retrieving revision 1.20
diff -C 2 -d -r1.19 -r1.20
*** cmGeneratedFileStream.cxx	16 Nov 2007 12:01:58 -0000	1.19
--- cmGeneratedFileStream.cxx	15 Apr 2009 13:57:56 -0000	1.20
***************
*** 19,30 ****
  #include "cmSystemTools.h"
  
- // Includes needed for implementation of RenameFile.  This is not in
- // system tools because it is not implemented robustly enough to move
- // files across directories.
- #ifdef _WIN32
- # include <windows.h>
- # include <sys/stat.h>
- #endif
- 
  #if defined(CMAKE_BUILD_WITH_CMAKE)
  # include <cm_zlib.h>
--- 19,22 ----
***************
*** 255,303 ****
                                            const char* newname)
  {
! #ifdef _WIN32
!   /* On Windows the move functions will not replace existing files.
!      Check if the destination exists.  */
!   struct stat newFile;
!   if(stat(newname, &newFile) == 0)
!     {
!     /* The destination exists.  We have to replace it carefully.  The
!        MoveFileEx function does what we need but is not available on
!        Win9x.  */
!     OSVERSIONINFO osv;
!     DWORD attrs;
! 
!     /* Make sure the destination is not read only.  */
!     attrs = GetFileAttributes(newname);
!     if(attrs & FILE_ATTRIBUTE_READONLY)
!       {
!       SetFileAttributes(newname, attrs & ~FILE_ATTRIBUTE_READONLY);
!       }
! 
!     /* Check the windows version number.  */
!     osv.dwOSVersionInfoSize = sizeof(osv);
!     GetVersionEx(&osv);
!     if(osv.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS)
!       {
!       /* This is Win9x.  There is no MoveFileEx implementation.  We
!          cannot quite rename the file atomically.  Just delete the
!          destination and then move the file.  */
!       DeleteFile(newname);
!       return MoveFile(oldname, newname);
!       }
!     else
!       {
!       /* This is not Win9x.  Use the MoveFileEx implementation.  */
!       return MoveFileEx(oldname, newname, MOVEFILE_REPLACE_EXISTING);
!       }
!     }
!   else
!     {
!     /* The destination does not exist.  Just move the file.  */
!     return MoveFile(oldname, newname);
!     }
! #else
!   /* On UNIX we have an OS-provided call to do this atomically.  */
!   return rename(oldname, newname) == 0;
! #endif
  }
  
--- 247,251 ----
                                            const char* newname)
  {
!   return cmSystemTools::RenameFile(oldname, newname);
  }
  



More information about the Cmake-commits mailing list