{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE CPP #-}
module Hakyll.Core.Util.File
( makeDirectories
, getRecursiveContents
, removeDirectory
, withPermissions
) where
import Control.Exception (throw)
import Control.Monad (filterM, forM)
import System.Directory (createDirectoryIfMissing, doesPathExist,
doesDirectoryExist, getDirectoryContents)
import System.FilePath (takeDirectory, (</>))
import System.IO.Error (catchIOError, isPermissionError)
#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 (FilePath -> IO ()) -> (FilePath -> FilePath) -> FilePath -> IO ()
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 FilePath -> [FilePath] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [FilePath
".", FilePath
".."] = Bool -> IO Bool
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False
| Bool
otherwise = Bool -> Bool
not (Bool -> Bool) -> IO Bool -> IO Bool
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> FilePath -> IO Bool
ignore FilePath
x
getProperDirectoryContents :: FilePath -> IO [FilePath]
getProperDirectoryContents FilePath
absDir =
(FilePath -> IO Bool) -> [FilePath] -> IO [FilePath]
forall (m :: * -> *) a.
Applicative m =>
(a -> m Bool) -> [a] -> m [a]
filterM FilePath -> IO Bool
isProper ([FilePath] -> IO [FilePath]) -> IO [FilePath] -> IO [FilePath]
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< IO [FilePath] -> [FilePath] -> IO [FilePath]
forall a. IO a -> a -> IO a
withPermissions (FilePath -> IO [FilePath]
getDirectoryContents FilePath
absDir) []
go :: FilePath -> IO [FilePath]
go FilePath
relDir = do
let absDir :: FilePath
absDir = FilePath
top FilePath -> FilePath -> FilePath
</> FilePath
relDir
dirExists <- FilePath -> IO Bool
doesDirectoryExist FilePath
absDir
if not dirExists
then return []
else do
names <- getProperDirectoryContents absDir
fmap concat . forM names $ \FilePath
name -> do
let relPath :: FilePath
relPath = FilePath
relDir FilePath -> FilePath -> FilePath
</> FilePath
name
absPath :: FilePath
absPath = FilePath
top FilePath -> FilePath -> FilePath
</> FilePath
relPath
isDirectory <- FilePath -> IO Bool
doesDirectoryExist FilePath
absPath
if isDirectory
then go relPath
else do
pathExists <- doesPathExist absPath
return $ if pathExists then [relPath] else []
removeDirectory :: FilePath -> IO ()
#ifndef mingw32_HOST_OS
removeDirectory :: FilePath -> IO ()
removeDirectory FilePath
fp = do
e <- FilePath -> IO Bool
doesDirectoryExist FilePath
fp
when e $ removeDirectoryRecursive 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
withPermissions :: IO a
-> a
-> IO a
withPermissions :: forall a. IO a -> a -> IO a
withPermissions IO a
act a
onError
= IO a
act IO a -> (IOError -> IO a) -> IO a
forall a. IO a -> (IOError -> IO a) -> IO a
`catchIOError` \IOError
e ->
if IOError -> Bool
isPermissionError IOError
e
then a -> IO a
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
onError
else IOError -> IO a
forall a e. (HasCallStack, Exception e) => e -> a
throw IOError
e