summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKWSys Upstream <kwrobot@kitware.com>2022-06-21 14:17:54 (GMT)
committerBrad King <brad.king@kitware.com>2022-06-22 15:01:05 (GMT)
commit02dbf65dee8f6eb4cabf480077d4e752b82a6601 (patch)
treebc49dab58bd1589baafe6d65748e75580febe41e
parent975c44654d2f992bab20f963a70f12fe6a63e1ba (diff)
downloadCMake-02dbf65dee8f6eb4cabf480077d4e752b82a6601.zip
CMake-02dbf65dee8f6eb4cabf480077d4e752b82a6601.tar.gz
CMake-02dbf65dee8f6eb4cabf480077d4e752b82a6601.tar.bz2
KWSys 2022-06-21 (d427a180)
Code extracted from: https://gitlab.kitware.com/utils/kwsys.git at commit d427a18072e2f37108aeeadf41ed4a21b4be3ab1 (master). Upstream Shortlog ----------------- Clemens Wasser (2): ab7c8f6f SystemTools: Factor out RemoveTrailingSlashes ca9646bd SystemTools: Optimize FileIsExecutable on Windows
-rw-r--r--SystemTools.cxx40
1 files changed, 31 insertions, 9 deletions
diff --git a/SystemTools.cxx b/SystemTools.cxx
index 5889a4b..89a3cc7 100644
--- a/SystemTools.cxx
+++ b/SystemTools.cxx
@@ -3067,17 +3067,14 @@ std::string SystemTools::GetRealPath(const std::string& path,
return ret;
}
-bool SystemTools::FileIsDirectory(const std::string& inName)
+// Remove any trailing slash from the name except in a root component.
+static const char* RemoveTrailingSlashes(
+ const std::string& inName, char (&local_buffer)[KWSYS_SYSTEMTOOLS_MAXPATH],
+ std::string& string_buffer)
{
- if (inName.empty()) {
- return false;
- }
size_t length = inName.size();
const char* name = inName.c_str();
- // Remove any trailing slash from the name except in a root component.
- char local_buffer[KWSYS_SYSTEMTOOLS_MAXPATH];
- std::string string_buffer;
size_t last = length - 1;
if (last > 0 && (name[last] == '/' || name[last] == '\\') &&
strcmp(name, "/") != 0 && name[last - 1] != ':') {
@@ -3091,6 +3088,19 @@ bool SystemTools::FileIsDirectory(const std::string& inName)
}
}
+ return name;
+}
+
+bool SystemTools::FileIsDirectory(const std::string& inName)
+{
+ if (inName.empty()) {
+ return false;
+ }
+
+ char local_buffer[KWSYS_SYSTEMTOOLS_MAXPATH];
+ std::string string_buffer;
+ const auto name = RemoveTrailingSlashes(inName, local_buffer, string_buffer);
+
// Now check the file node type.
#if defined(_WIN32)
DWORD attr =
@@ -3107,9 +3117,21 @@ bool SystemTools::FileIsDirectory(const std::string& inName)
}
}
-bool SystemTools::FileIsExecutable(const std::string& name)
+bool SystemTools::FileIsExecutable(const std::string& inName)
{
- return !FileIsDirectory(name) && TestFileAccess(name, TEST_FILE_EXECUTE);
+#ifdef _WIN32
+ char local_buffer[KWSYS_SYSTEMTOOLS_MAXPATH];
+ std::string string_buffer;
+ const auto name = RemoveTrailingSlashes(inName, local_buffer, string_buffer);
+ const auto attr =
+ GetFileAttributesW(Encoding::ToWindowsExtendedPath(name).c_str());
+
+ // On Windows any file that exists and is not a directory is considered
+ // readable and therefore also executable:
+ return attr != INVALID_FILE_ATTRIBUTES && !(attr & FILE_ATTRIBUTE_DIRECTORY);
+#else
+ return !FileIsDirectory(inName) && TestFileAccess(inName, TEST_FILE_EXECUTE);
+#endif
}
#if defined(_WIN32)