--

-- Copyright (c) 2012 Mark Dittmer - http://www.markdittmer.org

-- Developed for a Google Summer of Code project - http://gsoc2012.markdittmer.org

--

{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE TypeSynonymInstances #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE CPP #-}

module System.FSNotify.Path (
  findFiles
  , findFilesAndDirs
  , canonicalizeDirPath
  , canonicalizePath
  , hasThisExtension
  ) where

import Control.Monad
import qualified Data.Text as T
import Prelude hiding (FilePath)
import qualified System.Directory as D
import System.FilePath
import System.PosixCompat.Files as PF

getDirectoryContentsPath :: FilePath -> IO [FilePath]
getDirectoryContentsPath :: [Char] -> IO [[Char]]
getDirectoryContentsPath [Char]
path =
  ((([Char] -> [Char]) -> [[Char]] -> [[Char]]
forall a b. (a -> b) -> [a] -> [b]
map ([Char]
path [Char] -> [Char] -> [Char]
</>)) ([[Char]] -> [[Char]])
-> ([[Char]] -> [[Char]]) -> [[Char]] -> [[Char]]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ([Char] -> Bool) -> [[Char]] -> [[Char]]
forall a. (a -> Bool) -> [a] -> [a]
filter (Bool -> Bool
not (Bool -> Bool) -> ([Char] -> Bool) -> [Char] -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Char] -> Bool
dots) ([[Char]] -> [[Char]]) -> IO [[Char]] -> IO [[Char]]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [Char] -> IO [[Char]]
D.getDirectoryContents [Char]
path) IO [[Char]] -> ([[Char]] -> IO [[Char]]) -> IO [[Char]]
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ([Char] -> IO Bool) -> [[Char]] -> IO [[Char]]
forall (m :: * -> *) a.
Applicative m =>
(a -> m Bool) -> [a] -> m [a]
filterM [Char] -> IO Bool
exists
  where
#if MIN_VERSION_directory(1, 2, 7)
    exists :: [Char] -> IO Bool
exists [Char]
x = [Char] -> IO Bool
D.doesPathExist [Char]
x
#else
    exists x = (||) <$> D.doesFileExist x <*> D.doesDirectoryExist x
#endif
    dots :: [Char] -> Bool
dots [Char]
"."  = Bool
True
    dots [Char]
".." = Bool
True
    dots [Char]
_    = Bool
False

fileDirContents :: FilePath -> IO ([FilePath], [FilePath])
fileDirContents :: [Char] -> IO ([[Char]], [[Char]])
fileDirContents [Char]
path = do
  contents <- [Char] -> IO [[Char]]
getDirectoryContentsPath [Char]
path
  stats <- mapM getFileStatus contents
  let pairs = [FileStatus] -> [[Char]] -> [(FileStatus, [Char])]
forall a b. [a] -> [b] -> [(a, b)]
zip [FileStatus]
stats [[Char]]
contents
  let files = [ [Char]
f | (FileStatus
s, [Char]
f) <- [(FileStatus, [Char])]
pairs, FileStatus -> Bool
PF.isRegularFile FileStatus
s]
  let dirs = [ [Char]
d | (FileStatus
s, [Char]
d) <- [(FileStatus, [Char])]
pairs, FileStatus -> Bool
PF.isDirectory FileStatus
s]
  return (files, dirs)

findAllFiles :: FilePath -> IO [FilePath]
findAllFiles :: [Char] -> IO [[Char]]
findAllFiles [Char]
path = do
  (files, dirs) <- [Char] -> IO ([[Char]], [[Char]])
fileDirContents [Char]
path
  nestedFiles <- mapM findAllFiles dirs
  return (files ++ concat nestedFiles)

findImmediateFiles :: FilePath -> IO [FilePath]
findImmediateFiles :: [Char] -> IO [[Char]]
findImmediateFiles = [Char] -> IO ([[Char]], [[Char]])
fileDirContents ([Char] -> IO ([[Char]], [[Char]]))
-> (([[Char]], [[Char]]) -> IO [[Char]]) -> [Char] -> IO [[Char]]
forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> ([Char] -> IO [Char]) -> [[Char]] -> IO [[Char]]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM [Char] -> IO [Char]
D.canonicalizePath ([[Char]] -> IO [[Char]])
-> (([[Char]], [[Char]]) -> [[Char]])
-> ([[Char]], [[Char]])
-> IO [[Char]]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ([[Char]], [[Char]]) -> [[Char]]
forall a b. (a, b) -> a
fst

-- * Exported functions below this point


findFiles :: Bool -> FilePath -> IO [FilePath]
findFiles :: Bool -> [Char] -> IO [[Char]]
findFiles Bool
True [Char]
path  = [Char] -> IO [[Char]]
findAllFiles       ([Char] -> IO [[Char]]) -> IO [Char] -> IO [[Char]]
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< [Char] -> IO [Char]
canonicalizeDirPath [Char]
path
findFiles Bool
False [Char]
path = [Char] -> IO [[Char]]
findImmediateFiles ([Char] -> IO [[Char]]) -> IO [Char] -> IO [[Char]]
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<<  [Char] -> IO [Char]
canonicalizeDirPath [Char]
path

findFilesAndDirs :: Bool -> FilePath -> IO [FilePath]
findFilesAndDirs :: Bool -> [Char] -> IO [[Char]]
findFilesAndDirs Bool
False [Char]
path = [Char] -> IO [[Char]]
getDirectoryContentsPath ([Char] -> IO [[Char]]) -> IO [Char] -> IO [[Char]]
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< [Char] -> IO [Char]
canonicalizeDirPath [Char]
path
findFilesAndDirs Bool
True [Char]
path = do
  (files, dirs) <- [Char] -> IO ([[Char]], [[Char]])
fileDirContents [Char]
path
  nestedFilesAndDirs <- concat <$> mapM (findFilesAndDirs False) dirs
  return (files ++ dirs ++ nestedFilesAndDirs)

-- | add a trailing slash to ensure the path indicates a directory

addTrailingSlash :: FilePath -> FilePath
addTrailingSlash :: [Char] -> [Char]
addTrailingSlash = [Char] -> [Char]
addTrailingPathSeparator

canonicalizeDirPath :: FilePath -> IO FilePath
canonicalizeDirPath :: [Char] -> IO [Char]
canonicalizeDirPath [Char]
path = [Char] -> [Char]
addTrailingSlash ([Char] -> [Char]) -> IO [Char] -> IO [Char]
forall a b. (a -> b) -> IO a -> IO b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap` [Char] -> IO [Char]
D.canonicalizePath [Char]
path

-- | bugfix older version of canonicalizePath (system-fileio <= 0.3.7) loses trailing slash

canonicalizePath :: FilePath -> IO FilePath
canonicalizePath :: [Char] -> IO [Char]
canonicalizePath [Char]
path = let was_dir :: Bool
was_dir = [Char] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null ([Char] -> [Char]
takeFileName [Char]
path) in
  if Bool -> Bool
not Bool
was_dir then [Char] -> IO [Char]
D.canonicalizePath [Char]
path
  else [Char] -> IO [Char]
canonicalizeDirPath [Char]
path

hasThisExtension :: FilePath -> T.Text -> Bool
hasThisExtension :: [Char] -> Text -> Bool
hasThisExtension [Char]
p Text
ext = [Char] -> [Char]
takeExtension [Char]
p [Char] -> [Char] -> Bool
forall a. Eq a => a -> a -> Bool
== Text -> [Char]
T.unpack Text
ext