+{- |The operation 'doesDirectoryExist' returns 'True' if the argument file
+exists and is a directory, and 'False' otherwise.
+-}
+
+doesDirectoryExist :: FilePath -> IO Bool
+doesDirectoryExist name =
+ catch
+ (withFileStatus "doesDirectoryExist" name $ \st -> isDirectory st)
+ (\ _ -> return False)
+
+{- |The operation 'doesFileExist' returns 'True'
+if the argument file exists and is not a directory, and 'False' otherwise.
+-}
+
+doesFileExist :: FilePath -> IO Bool
+doesFileExist name = do
+ catch
+ (withFileStatus "doesFileExist" name $ \st -> do b <- isDirectory st; return (not b))
+ (\ _ -> return False)
+
+{- |The 'getModificationTime' operation returns the
+clock time at which the file or directory was last modified.
+
+The operation may fail with:
+
+* 'isPermissionError' if the user is not permitted to access
+ the modification time; or
+
+* 'isDoesNotExistError' if the file or directory does not exist.
+
+-}
+
+getModificationTime :: FilePath -> IO ClockTime
+getModificationTime name =
+ withFileStatus "getModificationTime" name $ \ st ->
+ modificationTime st
+
+withFileStatus :: String -> FilePath -> (Ptr CStat -> IO a) -> IO a
+withFileStatus loc name f = do
+ modifyIOError (`ioeSetFileName` name) $
+ allocaBytes sizeof_stat $ \p ->
+ withCString (fileNameEndClean name) $ \s -> do
+ throwErrnoIfMinus1Retry_ loc (c_stat s p)
+ f p
+
+withFileOrSymlinkStatus :: String -> FilePath -> (Ptr CStat -> IO a) -> IO a
+withFileOrSymlinkStatus loc name f = do
+ modifyIOError (`ioeSetFileName` name) $
+ allocaBytes sizeof_stat $ \p ->
+ withCString name $ \s -> do
+ throwErrnoIfMinus1Retry_ loc (lstat s p)
+ f p
+
+modificationTime :: Ptr CStat -> IO ClockTime
+modificationTime stat = do
+ mtime <- st_mtime stat
+ let realToInteger = round . realToFrac :: Real a => a -> Integer
+ return (TOD (realToInteger (mtime :: CTime)) 0)
+
+isDirectory :: Ptr CStat -> IO Bool
+isDirectory stat = do
+ mode <- st_mode stat
+ return (s_isdir mode)
+
+fileNameEndClean :: String -> String
+fileNameEndClean name =
+ if i > 0 && (ec == '\\' || ec == '/') then
+ fileNameEndClean (take i name)
+ else
+ name
+ where
+ i = (length name) - 1
+ ec = name !! i
+
+foreign import ccall unsafe "__hscore_R_OK" r_OK :: CMode
+foreign import ccall unsafe "__hscore_W_OK" w_OK :: CMode
+foreign import ccall unsafe "__hscore_X_OK" x_OK :: CMode
+
+foreign import ccall unsafe "__hscore_S_IRUSR" s_IRUSR :: CMode
+foreign import ccall unsafe "__hscore_S_IWUSR" s_IWUSR :: CMode
+foreign import ccall unsafe "__hscore_S_IXUSR" s_IXUSR :: CMode
+
+#endif /* __GLASGOW_HASKELL__ */
+
+foreign import ccall unsafe "__hscore_long_path_size"
+ long_path_size :: Int
+