From 6d83c11371813e48be7304283f6de4d5aada358a Mon Sep 17 00:00:00 2001
From: Jean-Christophe Fillion-Robin <jchris.fillionr@kitware.com>
Date: Thu, 16 Jul 2015 14:46:05 -0400
Subject: [PATCH] SystemTools: Fix DetectFileType failure on directory

Some operating systems allow directories to be opened and read like
files while others do not.  Simply check for a directory type first.

Change-Id: Ic970f662d5aa887ace4a8f166ede7e0d1531d0d6
---
 SystemTools.cxx     |  5 +++++
 testSystemTools.cxx | 11 +++++++++++
 2 files changed, 16 insertions(+)

diff --git a/SystemTools.cxx b/SystemTools.cxx
index a2c24a7..11beb3d 100644
--- a/SystemTools.cxx
+++ b/SystemTools.cxx
@@ -4230,6 +4230,11 @@ SystemTools::DetectFileType(const char *filename,
     return SystemTools::FileTypeUnknown;
     }
 
+  if (SystemTools::FileIsDirectory(filename))
+    {
+    return SystemTools::FileTypeUnknown;
+    }
+
   FILE *fp = Fopen(filename, "rb");
   if (!fp)
     {
diff --git a/testSystemTools.cxx b/testSystemTools.cxx
index 67a90fa..7b5c025 100644
--- a/testSystemTools.cxx
+++ b/testSystemTools.cxx
@@ -100,6 +100,8 @@ static bool CheckFileOperations()
   bool res = true;
   const kwsys_stl::string testNonExistingFile(TEST_SYSTEMTOOLS_SOURCE_DIR
     "/testSystemToolsNonExistingFile");
+  const kwsys_stl::string testDotFile(TEST_SYSTEMTOOLS_SOURCE_DIR
+    "/.");
   const kwsys_stl::string testBinFile(TEST_SYSTEMTOOLS_SOURCE_DIR
     "/testSystemTools.bin");
   const kwsys_stl::string testTxtFile(TEST_SYSTEMTOOLS_SOURCE_DIR
@@ -117,6 +119,15 @@ static bool CheckFileOperations()
     res = false;
     }
 
+  if (kwsys::SystemTools::DetectFileType(testDotFile.c_str()) !=
+      kwsys::SystemTools::FileTypeUnknown)
+    {
+    kwsys_ios::cerr
+      << "Problem with DetectFileType - failed to detect type of: "
+      << testDotFile << kwsys_ios::endl;
+    res = false;
+    }
+
   if (kwsys::SystemTools::DetectFileType(testBinFile.c_str()) !=
       kwsys::SystemTools::FileTypeBinary)
     {
-- 
GitLab