Commit a2bf6bb3 authored by Mathieu Westphal's avatar Mathieu Westphal Committed by Brad King
Browse files

SystemTools: Add cross-platform stat() wrapper

parent 7e9f7b7b
Pipeline #53144 passed with stage
...@@ -54,7 +54,6 @@ ...@@ -54,7 +54,6 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <sys/stat.h>
#include <time.h> #include <time.h>
#if defined(_WIN32) && !defined(_MSC_VER) && defined(__GNUC__) #if defined(_WIN32) && !defined(_MSC_VER) && defined(__GNUC__)
...@@ -1257,6 +1256,33 @@ bool SystemTools::TestFileAccess(const std::string& filename, ...@@ -1257,6 +1256,33 @@ bool SystemTools::TestFileAccess(const std::string& filename,
#endif #endif
} }
//----------------------------------------------------------------------------
int SystemTools::Stat(const char* path, SystemTools::Stat_t* buf)
{
if (!path) {
errno = EFAULT;
return -1;
}
return SystemTools::Stat(std::string(path), buf);
}
//----------------------------------------------------------------------------
int SystemTools::Stat(const std::string& path, SystemTools::Stat_t* buf)
{
if (path.empty()) {
errno = ENOENT;
return -1;
}
#if defined(_WIN32) && !defined(__CYGWIN__)
// Ideally we should use ConvertToWindowsExtendedPath to support
// long paths, but _wstat64 rejects paths with '?' in them, thinking
// they are wildcards.
return _wstat64(Encoding::ToWide(path).c_str(), buf);
#else
return stat(path.c_str(), buf);
#endif
}
//---------------------------------------------------------------------------- //----------------------------------------------------------------------------
#ifdef __CYGWIN__ #ifdef __CYGWIN__
bool SystemTools::PathCygwinToWin32(const char* path, char* win32_path) bool SystemTools::PathCygwinToWin32(const char* path, char* win32_path)
......
...@@ -13,6 +13,9 @@ ...@@ -13,6 +13,9 @@
#include <@KWSYS_NAMESPACE@/String.hxx> #include <@KWSYS_NAMESPACE@/String.hxx>
#include <sys/types.h> #include <sys/types.h>
// include sys/stat.h after sys/types.h
#include <sys/stat.h>
#if !defined(_WIN32) || defined(__CYGWIN__) #if !defined(_WIN32) || defined(__CYGWIN__)
#include <unistd.h> // For access permissions for use with access() #include <unistd.h> // For access permissions for use with access()
#endif #endif
...@@ -324,6 +327,23 @@ public: ...@@ -324,6 +327,23 @@ public:
TestFilePermissions permissions); TestFilePermissions permissions);
static bool TestFileAccess(const std::string& filename, static bool TestFileAccess(const std::string& filename,
TestFilePermissions permissions); TestFilePermissions permissions);
/**
* Cross platform wrapper for stat struct
*/
#if defined(_WIN32) && !defined(__CYGWIN__)
typedef struct _stat64 Stat_t;
#else
typedef struct stat Stat_t;
#endif
/**
* Cross platform wrapper for stat system call
*
* On Windows this may not work for paths longer than 250 characters
* due to limitations of the underlying '_wstat64' call.
*/
static int Stat(const char* path, Stat_t* buf);
static int Stat(const std::string& path, Stat_t* buf);
/** /**
* Converts Cygwin path to Win32 path. Uses dictionary container for * Converts Cygwin path to Win32 path. Uses dictionary container for
......
...@@ -135,6 +135,19 @@ static bool CheckFileOperations() ...@@ -135,6 +135,19 @@ static bool CheckFileOperations()
res = false; res = false;
} }
kwsys::SystemTools::Stat_t buf;
if (kwsys::SystemTools::Stat(testTxtFile.c_str(), &buf) != 0) {
std::cerr << "Problem with Stat - unable to stat text file: "
<< testTxtFile << std::endl;
res = false;
}
if (kwsys::SystemTools::Stat(testBinFile, &buf) != 0) {
std::cerr << "Problem with Stat - unable to stat bin file: " << testBinFile
<< std::endl;
res = false;
}
if (!kwsys::SystemTools::MakeDirectory(testNewDir)) { if (!kwsys::SystemTools::MakeDirectory(testNewDir)) {
std::cerr << "Problem with MakeDirectory for: " << testNewDir << std::endl; std::cerr << "Problem with MakeDirectory for: " << testNewDir << std::endl;
res = false; res = false;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment