[commit: packages/directory] Mistuke-bump-win32-version-bounds, bgamari-patch-1, master: Add doesPathExist (435e635)

git at git.haskell.org git at git.haskell.org
Mon Apr 17 21:34:13 UTC 2017


Repository : ssh://git@git.haskell.org/directory

On branches: Mistuke-bump-win32-version-bounds,bgamari-patch-1,master
Link       : http://ghc.haskell.org/trac/ghc/changeset/435e6353582aa5a93c7448292731785e73aa5251/directory

>---------------------------------------------------------------

commit 435e6353582aa5a93c7448292731785e73aa5251
Author: Phil Ruffwind <rf at rufflewind.com>
Date:   Sun Jun 12 00:04:21 2016 -0400

    Add doesPathExist
    
    See #57.


>---------------------------------------------------------------

435e6353582aa5a93c7448292731785e73aa5251
 System/Directory.hs            | 16 +++++++++++++---
 changelog.md                   |  3 +++
 directory.cabal                |  1 +
 tests/DoesDirectoryExist001.hs |  8 ++++++++
 tests/DoesPathExist.hs         | 30 ++++++++++++++++++++++++++++++
 tests/Main.hs                  |  2 ++
 6 files changed, 57 insertions(+), 3 deletions(-)

diff --git a/System/Directory.hs b/System/Directory.hs
index 19a322a..4ce0a86 100644
--- a/System/Directory.hs
+++ b/System/Directory.hs
@@ -64,6 +64,7 @@ module System.Directory
     , exeExtension
 
     -- * Existence tests
+    , doesPathExist
     , doesFileExist
     , doesDirectoryExist
 
@@ -965,9 +966,6 @@ canonicalizePath = \ path ->
     realpath encoding path =
       GHC.withCString encoding path
       (`withRealpath` GHC.peekCString encoding)
-
-    doesPathExist path = (Posix.getFileStatus path >> return True)
-                         `catchIOError` \ _ -> return False
 #endif
 
 -- | Convert a path into an absolute path.  If the given path is relative, the
@@ -1322,6 +1320,18 @@ withCurrentDirectory dir action =
     setCurrentDirectory dir
     action
 
+-- | Test whether the given path points to an existing filesystem object.  If
+-- the user lacks necessary permissions to search the parent directories, this
+-- function may return false even if the file does actually exist.
+doesPathExist :: FilePath -> IO Bool
+doesPathExist path =
+#ifdef mingw32_HOST_OS
+  (withFileStatus "" path $ \ _ -> return True)
+#else
+  (Posix.getFileStatus path >> return True)
+#endif
+  `catchIOError` \ _ -> return False
+
 {- |The operation 'doesDirectoryExist' returns 'True' if the argument file
 exists and is either a directory or a symbolic link to a directory,
 and 'False' otherwise.
diff --git a/changelog.md b/changelog.md
index 081f125..f6bf91c 100644
--- a/changelog.md
+++ b/changelog.md
@@ -7,6 +7,9 @@ Changelog for the [`directory`][1] package
     are no longer available.
     ([#50](https://github.com/haskell/directory/issues/50))
 
+  * Add `doesPathExist`
+    ([#57](https://github.com/haskell/directory/issues/57))
+
 ## 1.2.6.3 (May 2016)
 
   * Add missing import of `(<*>)` on Windows for `base` earlier than 4.8.0.0
diff --git a/directory.cabal b/directory.cabal
index f9a79bf..628ec06 100644
--- a/directory.cabal
+++ b/directory.cabal
@@ -87,6 +87,7 @@ test-suite test
         CurrentDirectory001
         Directory001
         DoesDirectoryExist001
+        DoesPathExist
         FileTime
         FindFile001
         GetDirContents001
diff --git a/tests/DoesDirectoryExist001.hs b/tests/DoesDirectoryExist001.hs
index b5a1aa9..38522d6 100644
--- a/tests/DoesDirectoryExist001.hs
+++ b/tests/DoesDirectoryExist001.hs
@@ -9,6 +9,14 @@ main _t = do
   -- [regression test] "/" was not recognised as a directory prior to GHC 6.1
   T(expect) () =<< doesDirectoryExist rootDir
 
+  createDirectory "somedir"
+
+  T(expect) () . not =<< doesDirectoryExist "nonexistent"
+  T(expect) () =<< doesDirectoryExist "somedir"
+#ifdef mingw32_HOST_OS
+  T(expect) () =<< doesDirectoryExist "SoMeDiR"
+#endif
+
   where
 #ifdef mingw32_HOST_OS
     rootDir = "C:\\"
diff --git a/tests/DoesPathExist.hs b/tests/DoesPathExist.hs
new file mode 100644
index 0000000..b7b8bc9
--- /dev/null
+++ b/tests/DoesPathExist.hs
@@ -0,0 +1,30 @@
+{-# LANGUAGE CPP #-}
+module DoesPathExist where
+#include "util.inl"
+import System.Directory
+
+main :: TestEnv -> IO ()
+main _t = do
+
+  T(expect) () =<< doesPathExist rootDir
+
+  createDirectory "somedir"
+  writeFile "somefile" "somedata"
+  writeFile "\x3c0\x42f\x97f3\xe6\x221e" "somedata"
+
+  T(expect) () . not =<< doesPathExist "nonexistent"
+  T(expect) () =<< doesPathExist "somedir"
+  T(expect) () =<< doesPathExist "somefile"
+  T(expect) () =<< doesPathExist "./somefile"
+#ifdef mingw32_HOST_OS
+  T(expect) () =<< doesPathExist "SoMeDiR"
+  T(expect) () =<< doesPathExist "sOmEfIlE"
+#endif
+  T(expect) () =<< doesPathExist "\x3c0\x42f\x97f3\xe6\x221e"
+
+  where
+#ifdef mingw32_HOST_OS
+    rootDir = "C:\\"
+#else
+    rootDir = "/"
+#endif
diff --git a/tests/Main.hs b/tests/Main.hs
index 3a5a02d..da8b50b 100644
--- a/tests/Main.hs
+++ b/tests/Main.hs
@@ -9,6 +9,7 @@ import qualified CreateDirectoryIfMissing001
 import qualified CurrentDirectory001
 import qualified Directory001
 import qualified DoesDirectoryExist001
+import qualified DoesPathExist
 import qualified FileTime
 import qualified FindFile001
 import qualified GetDirContents001
@@ -34,6 +35,7 @@ main = T.testMain $ \ _t -> do
   T.isolatedRun _t "CurrentDirectory001" CurrentDirectory001.main
   T.isolatedRun _t "Directory001" Directory001.main
   T.isolatedRun _t "DoesDirectoryExist001" DoesDirectoryExist001.main
+  T.isolatedRun _t "DoesPathExist" DoesPathExist.main
   T.isolatedRun _t "FileTime" FileTime.main
   T.isolatedRun _t "FindFile001" FindFile001.main
   T.isolatedRun _t "GetDirContents001" GetDirContents001.main



More information about the ghc-commits mailing list