Skip to content
Snippets Groups Projects
Commit 12a8593b authored by Brad King's avatar Brad King
Browse files

ENH: Adding kwsys::SystemTools::FileTimeCompare method to compare file...

ENH: Adding kwsys::SystemTools::FileTimeCompare method to compare file modification times with the highest resolution possible on the file system.
parent 93df6a72
No related branches found
No related tags found
No related merge requests found
......@@ -693,6 +693,90 @@ bool SystemTools::FileExists(const char* filename)
}
bool SystemTools::FileTimeCompare(const char* f1, const char* f2,
int* result)
{
// Default to same time.
*result = 0;
#if !defined(_WIN32) || defined(__CYGWIN__)
// POSIX version. Use stat function to get file modification time.
struct stat s1;
if(stat(f1, &s1) != 0)
{
return false;
}
struct stat s2;
if(stat(f2, &s2) != 0)
{
return false;
}
# if KWSYS_STAT_HAS_ST_MTIM
// Compare using nanosecond resolution.
if(s1.st_mtim.tv_sec < s2.st_mtim.tv_sec)
{
*result = -1;
}
else if(s1.st_mtim.tv_sec > s2.st_mtim.tv_sec)
{
*result = 1;
}
else if(s1.st_mtim.tv_nsec < s2.st_mtim.tv_nsec)
{
*result = -1;
}
else if(s1.st_mtim.tv_nsec > s2.st_mtim.tv_nsec)
{
*result = 1;
}
# else
// Compare using 1 second resolution.
if(s1.st_mtime < s2.st_mtime)
{
*result = -1;
}
else if(s1.st_mtime > s2.st_mtime)
{
*result = 1;
}
# 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)
{
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)
{
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);
#endif
return true;
}
// Return a capitalized string (i.e the first letter is uppercased, all other
// are lowercased)
kwsys_stl::string SystemTools::Capitalized(const kwsys_stl::string& s)
......
......@@ -139,6 +139,14 @@ public:
static bool FileExists(const char* filename);
static unsigned long FileLength(const char *filename);
/** Compare file modification times.
Returns true for successful comparison and false for error.
When true is returned, result has -1, 0, +1 for
f1 older, same, or newer than f2. */
static bool FileTimeCompare(const char* f1, const char* f2,
int* result);
/**
* Add the paths from the environment variable PATH to the
* string vector passed in. If env is set then the value
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment