From 946c9a2cc664f9628d2dcb8161f639cf32b4ca71 Mon Sep 17 00:00:00 2001 From: Brad King Date: Thu, 27 Oct 2005 13:57:48 -0400 Subject: [PATCH] ENH: Improved file modification time comparison on Windows to use GetFileAttributesEx instead of CreateFile/GetFileTime/CloseHandle to get file times. This results in a 30% reduction in time to do a build system check. --- Source/cmFileTimeComparison.cxx | 83 ++++++++++++++++++++------------- Source/kwsys/SystemTools.cxx | 30 +++--------- 2 files changed, 56 insertions(+), 57 deletions(-) diff --git a/Source/cmFileTimeComparison.cxx b/Source/cmFileTimeComparison.cxx index 4f9af520a..98a52d66e 100644 --- a/Source/cmFileTimeComparison.cxx +++ b/Source/cmFileTimeComparison.cxx @@ -9,22 +9,19 @@ Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved. See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details. - This software is distributed WITHOUT ANY WARRANTY; without even - the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR + This software is distributed WITHOUT ANY WARRANTY; without even + the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the above copyright notices for more information. =========================================================================*/ #include "cmFileTimeComparison.h" -#include -#include -#include - +// Use a hash table to avoid duplicate file time checks from disk. #if defined(CMAKE_BUILD_WITH_CMAKE) # include #endif - +// Use a platform-specific API to get file times efficiently. #if !defined(_WIN32) || defined(__CYGWIN__) # define cmFileTimeComparison_Type struct stat # include @@ -34,13 +31,16 @@ # include #endif +//---------------------------------------------------------------------------- class cmFileTimeComparisonInternal { public: + // Internal comparison method. inline bool FileTimeCompare(const char* f1, const char* f2, int* result); private: #if defined(CMAKE_BUILD_WITH_CMAKE) + // Use a hash table to efficiently map from file name to modification time. class HashString { public: @@ -50,67 +50,78 @@ private: } cmsys::hash h; }; - typedef cmsys::hash_map FileStatsMap; + typedef cmsys::hash_map FileStatsMap; FileStatsMap Files; #endif + // Internal methods to lookup and compare modification times. inline bool Stat(const char* fname, cmFileTimeComparison_Type* st); - inline int Compare(cmFileTimeComparison_Type* st1, cmFileTimeComparison_Type* st2); + inline int Compare(cmFileTimeComparison_Type* st1, + cmFileTimeComparison_Type* st2); }; -bool cmFileTimeComparisonInternal::Stat(const char* fname, cmFileTimeComparison_Type* st) +//---------------------------------------------------------------------------- +bool cmFileTimeComparisonInternal::Stat(const char* fname, + cmFileTimeComparison_Type* st) { #if defined(CMAKE_BUILD_WITH_CMAKE) - cmFileTimeComparisonInternal::FileStatsMap::iterator fit = this->Files.find(fname); + // Use the stored time if available. + cmFileTimeComparisonInternal::FileStatsMap::iterator fit = + this->Files.find(fname); if ( fit != this->Files.end() ) { *st = fit->second; return true; } #endif + #if !defined(_WIN32) || defined(__CYGWIN__) + // POSIX version. Use the stat function. int res = ::stat(fname, st); if ( res != 0 ) { return false; } #else - // Windows version. Create file handles and get the modification times. - HANDLE hf1 = CreateFile(fname, GENERIC_READ, FILE_SHARE_READ, - NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, - NULL); - if(hf1 == INVALID_HANDLE_VALUE) + // Windows version. Get the modification time from extended file attributes. + WIN32_FILE_ATTRIBUTE_DATA fdata; + if(!GetFileAttributesEx(fname, GetFileExInfoStandard, &fdata)) { return false; } - if(!GetFileTime(hf1, 0, 0, st)) - { - CloseHandle(hf1); - return false; - } - CloseHandle(hf1); + + // Copy the file time to the output location. + *st = fdata.ftLastWriteTime; #endif + #if defined(CMAKE_BUILD_WITH_CMAKE) + // Store the time for future use. this->Files[fname] = *st; #endif + return true; } +//---------------------------------------------------------------------------- cmFileTimeComparison::cmFileTimeComparison() { m_Internals = new cmFileTimeComparisonInternal; } +//---------------------------------------------------------------------------- cmFileTimeComparison::~cmFileTimeComparison() { delete m_Internals; } +//---------------------------------------------------------------------------- bool cmFileTimeComparison::FileTimeCompare(const char* f1, const char* f2, int* result) { return m_Internals->FileTimeCompare(f1, f2, result); } +//---------------------------------------------------------------------------- int cmFileTimeComparisonInternal::Compare(cmFileTimeComparison_Type* s1, cmFileTimeComparison_Type* s2) { #if !defined(_WIN32) || defined(__CYGWIN__) @@ -143,27 +154,33 @@ int cmFileTimeComparisonInternal::Compare(cmFileTimeComparison_Type* s1, cmFileT return 1; } # endif + // Files have the same time. return 0; #else + // Compare using system-provided function. return (int)CompareFileTime(s1, s2); #endif } -bool cmFileTimeComparisonInternal::FileTimeCompare(const char* f1, const char* f2, int* result) +//---------------------------------------------------------------------------- +bool cmFileTimeComparisonInternal::FileTimeCompare(const char* f1, + const char* f2, + int* result) { - // Default to same time. - *result = 0; + // Get the modification time for each file. cmFileTimeComparison_Type s1; - if(!this->Stat(f1, &s1)) - { - return false; - } cmFileTimeComparison_Type s2; - if(!this->Stat(f2, &s2)) + if(this->Stat(f1, &s1) && + this->Stat(f2, &s2)) { + // Compare the two modification times. + *result = this->Compare(&s1, &s2); + return true; + } + else + { + // No comparison available. Default to the same time. + *result = 0; return false; } - *result = this->Compare(&s1, &s2); - return true; } - diff --git a/Source/kwsys/SystemTools.cxx b/Source/kwsys/SystemTools.cxx index ae3154af6..df8f13740 100644 --- a/Source/kwsys/SystemTools.cxx +++ b/Source/kwsys/SystemTools.cxx @@ -786,38 +786,20 @@ bool SystemTools::FileTimeCompare(const char* f1, const char* f2, } # endif #else - // Windows version. Create file handles and get the modification times. - HANDLE hf1 = CreateFile(f1, GENERIC_READ, FILE_SHARE_READ, - NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, - NULL); - if(hf1 == INVALID_HANDLE_VALUE) + // Windows version. Get the modification time from extended file attributes. + WIN32_FILE_ATTRIBUTE_DATA f1d; + WIN32_FILE_ATTRIBUTE_DATA f2d; + if(!GetFileAttributesEx(f1, GetFileExInfoStandard, &f1d)) { return false; } - FILETIME tf1; - if(!GetFileTime(hf1, 0, 0, &tf1)) - { - CloseHandle(hf1); - return false; - } - CloseHandle(hf1); - HANDLE hf2 = CreateFile(f2, GENERIC_READ, FILE_SHARE_READ, - NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, - NULL); - if(hf2 == INVALID_HANDLE_VALUE) + if(!GetFileAttributesEx(f2, GetFileExInfoStandard, &f2d)) { return false; } - FILETIME tf2; - if(!GetFileTime(hf2, 0, 0, &tf2)) - { - CloseHandle(hf2); - return false; - } - CloseHandle(hf2); // Compare the file times using resolution provided by system call. - *result = (int)CompareFileTime(&tf1, &tf2); + *result = (int)CompareFileTime(&f1d.ftLastWriteTime, &f2d.ftLastWriteTime); #endif return true; }