module PhatSort.Monad.FileSystem
(
MonadFileSystem(..)
, FileStatus(..)
) where
import System.IO.Error (tryIOError)
import System.Posix.Types (DeviceID, EpochTime)
import qualified System.Directory as Dir
import Control.Monad.Trans.Class (MonadTrans(lift))
import Control.Monad.Trans.Except (ExceptT)
import qualified System.PosixCompat.Files as Files
class Monad m => MonadFileSystem m where
copyFile
:: FilePath
-> FilePath
-> m (Either IOError ())
createDirectory :: FilePath -> m (Either IOError ())
doesPathExist :: FilePath -> m (Either IOError Bool)
getFileStatus :: FilePath -> m (Either IOError FileStatus)
listDirectory :: FilePath -> m (Either IOError [FilePath])
makeAbsolute :: FilePath -> m (Either IOError FilePath)
removeDirectory :: FilePath -> m (Either IOError ())
renameDirectory
:: FilePath
-> FilePath
-> m (Either IOError ())
renameFile
:: FilePath
-> FilePath
-> m (Either IOError ())
instance MonadFileSystem IO where
copyFile :: FilePath -> FilePath -> IO (Either IOError ())
copyFile = (IO () -> IO (Either IOError ())
forall a. IO a -> IO (Either IOError a)
tryIOError (IO () -> IO (Either IOError ()))
-> (FilePath -> IO ()) -> FilePath -> IO (Either IOError ())
forall b c a. (b -> c) -> (a -> b) -> a -> c
.) ((FilePath -> IO ()) -> FilePath -> IO (Either IOError ()))
-> (FilePath -> FilePath -> IO ())
-> FilePath
-> FilePath
-> IO (Either IOError ())
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FilePath -> FilePath -> IO ()
Dir.copyFile
{-# INLINE copyFile #-}
createDirectory :: FilePath -> IO (Either IOError ())
createDirectory = IO () -> IO (Either IOError ())
forall a. IO a -> IO (Either IOError a)
tryIOError (IO () -> IO (Either IOError ()))
-> (FilePath -> IO ()) -> FilePath -> IO (Either IOError ())
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FilePath -> IO ()
Dir.createDirectory
{-# INLINE createDirectory #-}
doesPathExist :: FilePath -> IO (Either IOError Bool)
doesPathExist = IO Bool -> IO (Either IOError Bool)
forall a. IO a -> IO (Either IOError a)
tryIOError (IO Bool -> IO (Either IOError Bool))
-> (FilePath -> IO Bool) -> FilePath -> IO (Either IOError Bool)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FilePath -> IO Bool
Dir.doesPathExist
{-# INLINE doesPathExist #-}
getFileStatus :: FilePath -> IO (Either IOError FileStatus)
getFileStatus = IO FileStatus -> IO (Either IOError FileStatus)
forall a. IO a -> IO (Either IOError a)
tryIOError (IO FileStatus -> IO (Either IOError FileStatus))
-> (FilePath -> IO FileStatus)
-> FilePath
-> IO (Either IOError FileStatus)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (FileStatus -> FileStatus) -> IO FileStatus -> IO FileStatus
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap FileStatus -> FileStatus
toFileStatus (IO FileStatus -> IO FileStatus)
-> (FilePath -> IO FileStatus) -> FilePath -> IO FileStatus
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FilePath -> IO FileStatus
Files.getFileStatus
{-# INLINE getFileStatus #-}
listDirectory :: FilePath -> IO (Either IOError [FilePath])
listDirectory = IO [FilePath] -> IO (Either IOError [FilePath])
forall a. IO a -> IO (Either IOError a)
tryIOError (IO [FilePath] -> IO (Either IOError [FilePath]))
-> (FilePath -> IO [FilePath])
-> FilePath
-> IO (Either IOError [FilePath])
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FilePath -> IO [FilePath]
Dir.listDirectory
{-# INLINE listDirectory #-}
makeAbsolute :: FilePath -> IO (Either IOError FilePath)
makeAbsolute = IO FilePath -> IO (Either IOError FilePath)
forall a. IO a -> IO (Either IOError a)
tryIOError (IO FilePath -> IO (Either IOError FilePath))
-> (FilePath -> IO FilePath)
-> FilePath
-> IO (Either IOError FilePath)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FilePath -> IO FilePath
Dir.makeAbsolute
{-# INLINE makeAbsolute #-}
removeDirectory :: FilePath -> IO (Either IOError ())
removeDirectory = IO () -> IO (Either IOError ())
forall a. IO a -> IO (Either IOError a)
tryIOError (IO () -> IO (Either IOError ()))
-> (FilePath -> IO ()) -> FilePath -> IO (Either IOError ())
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FilePath -> IO ()
Dir.removeDirectory
{-# INLINE removeDirectory #-}
renameDirectory :: FilePath -> FilePath -> IO (Either IOError ())
renameDirectory = (IO () -> IO (Either IOError ())
forall a. IO a -> IO (Either IOError a)
tryIOError (IO () -> IO (Either IOError ()))
-> (FilePath -> IO ()) -> FilePath -> IO (Either IOError ())
forall b c a. (b -> c) -> (a -> b) -> a -> c
.) ((FilePath -> IO ()) -> FilePath -> IO (Either IOError ()))
-> (FilePath -> FilePath -> IO ())
-> FilePath
-> FilePath
-> IO (Either IOError ())
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FilePath -> FilePath -> IO ()
Dir.renameDirectory
{-# INLINE renameDirectory #-}
renameFile :: FilePath -> FilePath -> IO (Either IOError ())
renameFile = (IO () -> IO (Either IOError ())
forall a. IO a -> IO (Either IOError a)
tryIOError (IO () -> IO (Either IOError ()))
-> (FilePath -> IO ()) -> FilePath -> IO (Either IOError ())
forall b c a. (b -> c) -> (a -> b) -> a -> c
.) ((FilePath -> IO ()) -> FilePath -> IO (Either IOError ()))
-> (FilePath -> FilePath -> IO ())
-> FilePath
-> FilePath
-> IO (Either IOError ())
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FilePath -> FilePath -> IO ()
Dir.renameFile
{-# INLINE renameFile #-}
instance MonadFileSystem m => MonadFileSystem (ExceptT e m) where
copyFile :: FilePath -> FilePath -> ExceptT e m (Either IOError ())
copyFile = (m (Either IOError ()) -> ExceptT e m (Either IOError ())
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m (Either IOError ()) -> ExceptT e m (Either IOError ()))
-> (FilePath -> m (Either IOError ()))
-> FilePath
-> ExceptT e m (Either IOError ())
forall b c a. (b -> c) -> (a -> b) -> a -> c
.) ((FilePath -> m (Either IOError ()))
-> FilePath -> ExceptT e m (Either IOError ()))
-> (FilePath -> FilePath -> m (Either IOError ()))
-> FilePath
-> FilePath
-> ExceptT e m (Either IOError ())
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FilePath -> FilePath -> m (Either IOError ())
forall (m :: * -> *).
MonadFileSystem m =>
FilePath -> FilePath -> m (Either IOError ())
copyFile
{-# INLINE copyFile #-}
createDirectory :: FilePath -> ExceptT e m (Either IOError ())
createDirectory = m (Either IOError ()) -> ExceptT e m (Either IOError ())
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m (Either IOError ()) -> ExceptT e m (Either IOError ()))
-> (FilePath -> m (Either IOError ()))
-> FilePath
-> ExceptT e m (Either IOError ())
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FilePath -> m (Either IOError ())
forall (m :: * -> *).
MonadFileSystem m =>
FilePath -> m (Either IOError ())
createDirectory
{-# INLINE createDirectory #-}
doesPathExist :: FilePath -> ExceptT e m (Either IOError Bool)
doesPathExist = m (Either IOError Bool) -> ExceptT e m (Either IOError Bool)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m (Either IOError Bool) -> ExceptT e m (Either IOError Bool))
-> (FilePath -> m (Either IOError Bool))
-> FilePath
-> ExceptT e m (Either IOError Bool)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FilePath -> m (Either IOError Bool)
forall (m :: * -> *).
MonadFileSystem m =>
FilePath -> m (Either IOError Bool)
doesPathExist
{-# INLINE doesPathExist #-}
getFileStatus :: FilePath -> ExceptT e m (Either IOError FileStatus)
getFileStatus = m (Either IOError FileStatus)
-> ExceptT e m (Either IOError FileStatus)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m (Either IOError FileStatus)
-> ExceptT e m (Either IOError FileStatus))
-> (FilePath -> m (Either IOError FileStatus))
-> FilePath
-> ExceptT e m (Either IOError FileStatus)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FilePath -> m (Either IOError FileStatus)
forall (m :: * -> *).
MonadFileSystem m =>
FilePath -> m (Either IOError FileStatus)
getFileStatus
{-# INLINE getFileStatus #-}
listDirectory :: FilePath -> ExceptT e m (Either IOError [FilePath])
listDirectory = m (Either IOError [FilePath])
-> ExceptT e m (Either IOError [FilePath])
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m (Either IOError [FilePath])
-> ExceptT e m (Either IOError [FilePath]))
-> (FilePath -> m (Either IOError [FilePath]))
-> FilePath
-> ExceptT e m (Either IOError [FilePath])
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FilePath -> m (Either IOError [FilePath])
forall (m :: * -> *).
MonadFileSystem m =>
FilePath -> m (Either IOError [FilePath])
listDirectory
{-# INLINE listDirectory #-}
makeAbsolute :: FilePath -> ExceptT e m (Either IOError FilePath)
makeAbsolute = m (Either IOError FilePath)
-> ExceptT e m (Either IOError FilePath)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m (Either IOError FilePath)
-> ExceptT e m (Either IOError FilePath))
-> (FilePath -> m (Either IOError FilePath))
-> FilePath
-> ExceptT e m (Either IOError FilePath)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FilePath -> m (Either IOError FilePath)
forall (m :: * -> *).
MonadFileSystem m =>
FilePath -> m (Either IOError FilePath)
makeAbsolute
{-# INLINE makeAbsolute #-}
removeDirectory :: FilePath -> ExceptT e m (Either IOError ())
removeDirectory = m (Either IOError ()) -> ExceptT e m (Either IOError ())
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m (Either IOError ()) -> ExceptT e m (Either IOError ()))
-> (FilePath -> m (Either IOError ()))
-> FilePath
-> ExceptT e m (Either IOError ())
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FilePath -> m (Either IOError ())
forall (m :: * -> *).
MonadFileSystem m =>
FilePath -> m (Either IOError ())
removeDirectory
{-# INLINE removeDirectory #-}
renameDirectory :: FilePath -> FilePath -> ExceptT e m (Either IOError ())
renameDirectory = (m (Either IOError ()) -> ExceptT e m (Either IOError ())
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m (Either IOError ()) -> ExceptT e m (Either IOError ()))
-> (FilePath -> m (Either IOError ()))
-> FilePath
-> ExceptT e m (Either IOError ())
forall b c a. (b -> c) -> (a -> b) -> a -> c
.) ((FilePath -> m (Either IOError ()))
-> FilePath -> ExceptT e m (Either IOError ()))
-> (FilePath -> FilePath -> m (Either IOError ()))
-> FilePath
-> FilePath
-> ExceptT e m (Either IOError ())
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FilePath -> FilePath -> m (Either IOError ())
forall (m :: * -> *).
MonadFileSystem m =>
FilePath -> FilePath -> m (Either IOError ())
renameDirectory
{-# INLINE renameDirectory #-}
renameFile :: FilePath -> FilePath -> ExceptT e m (Either IOError ())
renameFile = (m (Either IOError ()) -> ExceptT e m (Either IOError ())
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m (Either IOError ()) -> ExceptT e m (Either IOError ()))
-> (FilePath -> m (Either IOError ()))
-> FilePath
-> ExceptT e m (Either IOError ())
forall b c a. (b -> c) -> (a -> b) -> a -> c
.) ((FilePath -> m (Either IOError ()))
-> FilePath -> ExceptT e m (Either IOError ()))
-> (FilePath -> FilePath -> m (Either IOError ()))
-> FilePath
-> FilePath
-> ExceptT e m (Either IOError ())
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FilePath -> FilePath -> m (Either IOError ())
forall (m :: * -> *).
MonadFileSystem m =>
FilePath -> FilePath -> m (Either IOError ())
renameFile
{-# INLINE renameFile #-}
data FileStatus
= FileStatus
{ FileStatus -> DeviceID
deviceID :: !DeviceID
, FileStatus -> Bool
isDirectory :: !Bool
, FileStatus -> EpochTime
modificationTime :: !EpochTime
}
toFileStatus :: Files.FileStatus -> FileStatus
toFileStatus :: FileStatus -> FileStatus
toFileStatus FileStatus
status = FileStatus :: DeviceID -> Bool -> EpochTime -> FileStatus
FileStatus
{ deviceID :: DeviceID
deviceID = FileStatus -> DeviceID
Files.deviceID FileStatus
status
, isDirectory :: Bool
isDirectory = FileStatus -> Bool
Files.isDirectory FileStatus
status
, modificationTime :: EpochTime
modificationTime = FileStatus -> EpochTime
Files.modificationTime FileStatus
status
}