summaryrefslogtreecommitdiffstats
path: root/Source/kwsys/SystemTools.cxx
diff options
context:
space:
mode:
Diffstat (limited to 'Source/kwsys/SystemTools.cxx')
-rw-r--r--Source/kwsys/SystemTools.cxx71
1 files changed, 62 insertions, 9 deletions
diff --git a/Source/kwsys/SystemTools.cxx b/Source/kwsys/SystemTools.cxx
index 0714344..f12a06c 100644
--- a/Source/kwsys/SystemTools.cxx
+++ b/Source/kwsys/SystemTools.cxx
@@ -68,6 +68,10 @@
#include <sys/stat.h>
#include <time.h>
+#ifdef _MSC_VER
+# define umask _umask // Note this is still umask on Borland
+#endif
+
// support for realpath call
#ifndef _WIN32
#include <sys/time.h>
@@ -1233,13 +1237,11 @@ bool SystemTools::FileExists(const kwsys_stl::string& filename)
//----------------------------------------------------------------------------
bool SystemTools::FileExists(const char* filename, bool isFile)
{
- if(SystemTools::FileExists(filename))
+ if(!filename)
{
- // If isFile is set return not FileIsDirectory,
- // so this will only be true if it is a file
- return !isFile || !SystemTools::FileIsDirectory(filename);
+ return false;
}
- return false;
+ return SystemTools::FileExists(kwsys_stl::string(filename), isFile);
}
//----------------------------------------------------------------------------
@@ -1255,6 +1257,43 @@ bool SystemTools::FileExists(const kwsys_stl::string& filename, bool isFile)
}
//----------------------------------------------------------------------------
+bool SystemTools::TestFileAccess(const char* filename,
+ TestFilePermissions permissions)
+{
+ if(!filename)
+ {
+ return false;
+ }
+ return SystemTools::TestFileAccess(kwsys_stl::string(filename),
+ permissions);
+}
+
+//----------------------------------------------------------------------------
+bool SystemTools::TestFileAccess(const kwsys_stl::string& filename,
+ TestFilePermissions permissions)
+{
+ if(filename.empty())
+ {
+ return false;
+ }
+#if defined(_WIN32) && !defined(__CYGWIN__)
+ // If execute set, change to read permission (all files on Windows
+ // are executable if they are readable). The CRT will always fail
+ // if you pass an execute bit.
+ if(permissions & TEST_FILE_EXECUTE)
+ {
+ permissions &= ~TEST_FILE_EXECUTE;
+ permissions |= TEST_FILE_READ;
+ }
+ return _waccess(
+ SystemTools::ConvertToWindowsExtendedPath(filename).c_str(),
+ permissions) == 0;
+#else
+ return access(filename.c_str(), permissions) == 0;
+#endif
+}
+
+//----------------------------------------------------------------------------
#ifdef __CYGWIN__
bool SystemTools::PathCygwinToWin32(const char *path, char *win32_path)
{
@@ -4745,21 +4784,35 @@ bool SystemTools::GetPermissions(const kwsys_stl::string& file, mode_t& mode)
return true;
}
-bool SystemTools::SetPermissions(const char* file, mode_t mode)
+bool SystemTools::SetPermissions(const char* file,
+ mode_t mode,
+ bool honor_umask)
{
if ( !file )
{
return false;
}
- return SystemTools::SetPermissions(kwsys_stl::string(file), mode);
+ return SystemTools::SetPermissions(
+ kwsys_stl::string(file), mode, honor_umask);
}
-bool SystemTools::SetPermissions(const kwsys_stl::string& file, mode_t mode)
+bool SystemTools::SetPermissions(const kwsys_stl::string& file,
+ mode_t mode,
+ bool honor_umask)
{
- if ( !SystemTools::FileExists(file) )
+ // TEMPORARY / TODO: After FileExists calls lstat() instead of
+ // access(), change this call to FileExists instead of
+ // TestFileAccess so that we don't follow symlinks.
+ if ( !SystemTools::TestFileAccess(file, TEST_FILE_OK) )
{
return false;
}
+ if (honor_umask)
+ {
+ mode_t currentMask = umask(0);
+ umask(currentMask);
+ mode &= ~currentMask;
+ }
#ifdef _WIN32
if ( _wchmod(SystemTools::ConvertToWindowsExtendedPath(file).c_str(),
mode) < 0 )