{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE CPP #-}
module Hakyll.Core.Util.File
( makeDirectories
, getRecursiveContents
, removeDirectory
) where
import Control.Monad (filterM, forM)
import System.Directory (createDirectoryIfMissing,
doesDirectoryExist, getDirectoryContents)
import System.FilePath (takeDirectory, (</>))
#ifndef mingw32_HOST_OS
import Control.Monad (when)
import System.Directory (removeDirectoryRecursive)
#else
import Control.Concurrent (threadDelay)
import Control.Exception (SomeException, catch)
import System.Directory (removePathForcibly)
#endif
makeDirectories :: FilePath -> IO ()
makeDirectories :: FilePath -> IO ()
makeDirectories = Bool -> FilePath -> IO ()
createDirectoryIfMissing Bool
True forall b c a. (b -> c) -> (a -> b) -> a -> c
. FilePath -> FilePath
takeDirectory
getRecursiveContents :: (FilePath -> IO Bool)
-> FilePath
-> IO [FilePath]
getRecursiveContents :: (FilePath -> IO Bool) -> FilePath -> IO [FilePath]
getRecursiveContents FilePath -> IO Bool
ignore FilePath
top = FilePath -> IO [FilePath]
go FilePath
""
where
isProper :: FilePath -> IO Bool
isProper FilePath
x
| FilePath
x forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [FilePath
".", FilePath
".."] = forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False
| Bool
otherwise = Bool -> Bool
not forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> FilePath -> IO Bool
ignore FilePath
x
go :: FilePath -> IO [FilePath]
go FilePath
dir = do
Bool
dirExists <- FilePath -> IO Bool
doesDirectoryExist (FilePath
top FilePath -> FilePath -> FilePath
</> FilePath
dir)
if Bool -> Bool
not Bool
dirExists
then forall (m :: * -> *) a. Monad m => a -> m a
return []
else do
[FilePath]
names <- forall (m :: * -> *) a.
Applicative m =>
(a -> m Bool) -> [a] -> m [a]
filterM FilePath -> IO Bool
isProper forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< FilePath -> IO [FilePath]
getDirectoryContents (FilePath
top FilePath -> FilePath -> FilePath
</> FilePath
dir)
[[FilePath]]
paths <- forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
t a -> (a -> m b) -> m (t b)
forM [FilePath]
names forall a b. (a -> b) -> a -> b
$ \FilePath
name -> do
let rel :: FilePath
rel = FilePath
dir FilePath -> FilePath -> FilePath
</> FilePath
name
Bool
isDirectory <- FilePath -> IO Bool
doesDirectoryExist (FilePath
top FilePath -> FilePath -> FilePath
</> FilePath
rel)
if Bool
isDirectory
then FilePath -> IO [FilePath]
go FilePath
rel
else forall (m :: * -> *) a. Monad m => a -> m a
return [FilePath
rel]
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat [[FilePath]]
paths
removeDirectory :: FilePath -> IO ()
#ifndef mingw32_HOST_OS
removeDirectory :: FilePath -> IO ()
removeDirectory FilePath
fp = do
Bool
e <- FilePath -> IO Bool
doesDirectoryExist FilePath
fp
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when Bool
e forall a b. (a -> b) -> a -> b
$ FilePath -> IO ()
removeDirectoryRecursive FilePath
fp
#else
removeDirectory = retryWithDelay 10 . removePathForcibly
retryWithDelay :: Int -> IO a -> IO a
retryWithDelay i x
| i <= 0 = error "Hakyll.Core.Util.File.retry: retry count must be 1 or more"
| i == 1 = x
| otherwise = catch x $ \(_::SomeException) -> threadDelay 100 >> retryWithDelay (i-1) x
#endif