packages feed

papa-0.7.0: src/Papa/Directory/System/Directory.hs

{-# LANGUAGE NoImplicitPrelude #-}
{-# OPTIONS_GHC -Wall #-}

-- | Files and directories ("System.Directory"), returning their failures in
-- 'IOExcept' instead of throwing them.
--
-- Every action of "System.Directory" is here, at the same name, with its
-- result in 'IOExcept'. That includes the actions that do not fail, such as
-- 'doesFileExist' (which returns 'Data.Bool.False' when it cannot tell), so
-- that every action of the file system composes in 'IOExcept' alike.
--
-- Left out:
--
-- * @getDirectoryContents@, which includes @.@ and @..@: use 'listDirectory'.
-- * @isSymbolicLink@, deprecated: use 'pathIsSymbolicLink'.
-- * @findFileWith@ and @findFilesWith@, whose predicate is an action that
--   throws: use 'findFiles' and filter its result.
--
-- The current directory ('getCurrentDirectory', 'setCurrentDirectory',
-- 'withCurrentDirectory') is the process's, shared by all of its threads.
--
-- @since 0.7.0
module Papa.Directory.System.Directory (
  -- * Directories
  createDirectory,
  createDirectoryIfMissing,
  removeDirectory,
  removeDirectoryRecursive,
  removePathForcibly,
  renameDirectory,
  listDirectory,

  -- * The current directory
  getCurrentDirectory,
  setCurrentDirectory,
  withCurrentDirectory,

  -- * Special directories
  getHomeDirectory,
  XdgDirectory (XdgData, XdgConfig, XdgCache, XdgState),
  getXdgDirectory,
  XdgDirectoryList (XdgDataDirs, XdgConfigDirs),
  getXdgDirectoryList,
  getAppUserDataDirectory,
  getUserDocumentsDirectory,
  getTemporaryDirectory,

  -- * Files
  removeFile,
  renameFile,
  renamePath,
  copyFile,
  copyFileWithMetadata,
  getFileSize,
  canonicalizePath,
  makeAbsolute,
  makeRelativeToCurrentDirectory,

  -- * Existence
  doesPathExist,
  doesFileExist,
  doesDirectoryExist,

  -- * Finding files
  findExecutable,
  findExecutables,
  findExecutablesInDirectories,
  findFile,
  findFiles,
  exeExtension,

  -- * Symbolic links
  createFileLink,
  createDirectoryLink,
  removeDirectoryLink,
  pathIsSymbolicLink,
  getSymbolicLinkTarget,

  -- * Permissions
  Permissions,
  emptyPermissions,
  readable,
  writable,
  executable,
  searchable,
  setOwnerReadable,
  setOwnerWritable,
  setOwnerExecutable,
  setOwnerSearchable,
  getPermissions,
  setPermissions,
  copyPermissions,

  -- * Timestamps
  getAccessTime,
  getModificationTime,
  setAccessTime,
  setModificationTime,
) where

import Control.Category ((.))
import Control.Monad ((>>=))
import Control.Monad.Except (liftEither, runExceptT)
import Data.Bool (Bool)
import Data.Maybe (Maybe)
import Data.String (String)
import Data.Time.Clock (UTCTime)
import Papa.Base.System.IO.Except (IOExcept, exceptIO)
import System.Directory (
  Permissions,
  XdgDirectory (XdgCache, XdgConfig, XdgData, XdgState),
  XdgDirectoryList (XdgConfigDirs, XdgDataDirs),
  emptyPermissions,
  exeExtension,
  executable,
  readable,
  searchable,
  setOwnerExecutable,
  setOwnerReadable,
  setOwnerSearchable,
  setOwnerWritable,
  writable,
 )
import qualified System.Directory as D
import System.IO (FilePath)
import Prelude (Integer)

-- $setup
-- >>> :set -XNoImplicitPrelude
-- >>> import Papa
-- >>> import qualified System.Directory as D
-- >>> import qualified Data.Time as Time
-- >>> import Hedgehog
-- >>> import qualified Hedgehog.Gen as Gen
-- >>> import qualified Hedgehog.Range as Range
-- >>> import qualified Data.Version as V
-- >>> import qualified System.Info as I
-- >>> dir <- (</> ("papa-directory-doctest-" <> V.showVersion I.fullCompilerVersion)) <$> D.getTemporaryDirectory
-- >>> D.removePathForcibly dir >> D.createDirectory dir
-- >>> let file = dir </> "a.txt"
-- >>> _ <- runExceptT (writeFile file (pack "abc"))

-- | Create a directory. Its parent must exist.
--
-- ==== __Examples__
--
-- >>> runExceptT (createDirectory (dir </> "c") >> doesDirectoryExist (dir </> "c"))
-- Right True
--
-- >>> either (anyOf _IOException (\i -> has _AlreadyExists (the errorType i))) (const False) <$> runExceptT (createDirectory (dir </> "c"))
-- True
--
-- >>> either (anyOf _IOException (\i -> has _NoSuchThing (the errorType i))) (const False) <$> runExceptT (createDirectory (dir </> "x" </> "y"))
-- True
--
-- @since 0.7.0
createDirectory :: FilePath -> IOExcept ()
createDirectory =
  exceptIO . D.createDirectory
{-# INLINE createDirectory #-}

-- | Create a directory, and, if the flag is 'Data.Bool.True', its missing
-- parents. A directory that exists is not a failure.
--
-- ==== __Examples__
--
-- >>> runExceptT (createDirectoryIfMissing True (dir </> "p" </> "q") >> createDirectoryIfMissing True (dir </> "p" </> "q") >> doesDirectoryExist (dir </> "p" </> "q"))
-- Right True
--
-- >>> check $ withTests 20 $ property $ do { n <- forAll (Gen.string (Range.linear 1 8) Gen.alphaNum); r <- evalIO (runExceptT (createDirectoryIfMissing True (dir </> "i" </> n) >> createDirectoryIfMissing True (dir </> "i" </> n) >> doesDirectoryExist (dir </> "i" </> n))); r === Right True }
-- ...passed 20 tests.
-- True
--
-- @since 0.7.0
createDirectoryIfMissing :: Bool -> FilePath -> IOExcept ()
createDirectoryIfMissing b =
  exceptIO . D.createDirectoryIfMissing b
{-# INLINE createDirectoryIfMissing #-}

-- | Remove an empty directory.
--
-- ==== __Examples__
--
-- >>> runExceptT (createDirectory (dir </> "e") >> removeDirectory (dir </> "e") >> doesDirectoryExist (dir </> "e"))
-- Right False
--
-- >>> isLeft <$> runExceptT (removeDirectory dir)
-- True
--
-- @since 0.7.0
removeDirectory :: FilePath -> IOExcept ()
removeDirectory =
  exceptIO . D.removeDirectory
{-# INLINE removeDirectory #-}

-- | Remove a directory and all that it contains. Symbolic links in it are
-- removed, not followed.
--
-- ==== __Examples__
--
-- >>> runExceptT (createDirectoryIfMissing True (dir </> "r" </> "s") >> removeDirectoryRecursive (dir </> "r") >> doesPathExist (dir </> "r"))
-- Right False
--
-- @since 0.7.0
removeDirectoryRecursive :: FilePath -> IOExcept ()
removeDirectoryRecursive =
  exceptIO . D.removeDirectoryRecursive
{-# INLINE removeDirectoryRecursive #-}

-- | Remove a file or directory and all that it contains, if it exists, and
-- even if it is not writable. A path that does not exist is not a failure.
--
-- ==== __Examples__
--
-- >>> runExceptT (removePathForcibly (dir </> "nothing"))
-- Right ()
--
-- @since 0.7.0
removePathForcibly :: FilePath -> IOExcept ()
removePathForcibly =
  exceptIO . D.removePathForcibly
{-# INLINE removePathForcibly #-}

-- | Rename a directory.
--
-- ==== __Examples__
--
-- >>> runExceptT (createDirectory (dir </> "m") >> renameDirectory (dir </> "m") (dir </> "n") >> traverse doesDirectoryExist [dir </> "m", dir </> "n"])
-- Right [False,True]
--
-- @since 0.7.0
renameDirectory :: FilePath -> FilePath -> IOExcept ()
renameDirectory a =
  exceptIO . D.renameDirectory a
{-# INLINE renameDirectory #-}

-- | The names in a directory, without @.@ and @..@, in no order.
--
-- ==== __Examples__
--
-- >>> fmap sort <$> runExceptT (writeFile (dir </> "b") (pack "") >> createDirectory (dir </> "a") >> listDirectory dir)
-- Right ["a","a.txt","b"]
--
-- >>> either (anyOf _IOException (\i -> has _NoSuchThing (the errorType i))) (const False) <$> runExceptT (listDirectory "/nonexistent/papa")
-- True
--
-- @since 0.7.0
listDirectory :: FilePath -> IOExcept [FilePath]
listDirectory =
  exceptIO . D.listDirectory
{-# INLINE listDirectory #-}

-- | The current directory of the process.
--
-- ==== __Examples__
--
-- >>> (==) <$> runExceptT getCurrentDirectory <*> fmap Right D.getCurrentDirectory
-- True
--
-- @since 0.7.0
getCurrentDirectory :: IOExcept FilePath
getCurrentDirectory =
  exceptIO D.getCurrentDirectory
{-# INLINE getCurrentDirectory #-}

-- | Set the current directory of the process, for all of its threads.
--
-- ==== __Examples__
--
-- >>> isLeft <$> runExceptT (setCurrentDirectory "/nonexistent/papa")
-- True
--
-- @since 0.7.0
setCurrentDirectory :: FilePath -> IOExcept ()
setCurrentDirectory =
  exceptIO . D.setCurrentDirectory
{-# INLINE setCurrentDirectory #-}

-- | Run an action in a directory, and then return to the current directory,
-- whether or not the action fails. The failure of the change of directory,
-- of the action, or of the return is returned.
--
-- The current directory is the process's: other threads see the change while
-- the action runs.
--
-- ==== __Examples__
--
-- >>> c <- D.getCurrentDirectory
-- >>> (\r d -> r == Right d) <$> runExceptT (withCurrentDirectory dir (getCurrentDirectory >>= canonicalizePath)) <*> D.canonicalizePath dir
-- True
--
-- >>> (\r d -> (isLeft r, d == c)) <$> runExceptT (withCurrentDirectory dir (listDirectory "/nonexistent/papa")) <*> D.getCurrentDirectory
-- (True,True)
--
-- >>> isLeft <$> runExceptT (withCurrentDirectory "/nonexistent/papa" (pure ()))
-- True
--
-- @since 0.7.0
withCurrentDirectory :: FilePath -> IOExcept a -> IOExcept a
withCurrentDirectory d a =
  exceptIO (D.withCurrentDirectory d (runExceptT a)) >>= liftEither
{-# INLINE withCurrentDirectory #-}

-- | The home directory of the user.
--
-- ==== __Examples__
--
-- >>> isRight <$> runExceptT getHomeDirectory
-- True
--
-- @since 0.7.0
getHomeDirectory :: IOExcept FilePath
getHomeDirectory =
  exceptIO D.getHomeDirectory
{-# INLINE getHomeDirectory #-}

-- | A directory of the XDG Base Directory Specification, for an
-- application's files.
--
-- ==== __Examples__
--
-- >>> fmap (isSuffixOf "papa") <$> runExceptT (getXdgDirectory XdgConfig "papa")
-- Right True
--
-- @since 0.7.0
getXdgDirectory :: XdgDirectory -> FilePath -> IOExcept FilePath
getXdgDirectory x =
  exceptIO . D.getXdgDirectory x
{-# INLINE getXdgDirectory #-}

-- | The search path of the XDG Base Directory Specification.
--
-- ==== __Examples__
--
-- >>> isRight <$> runExceptT (getXdgDirectoryList XdgDataDirs)
-- True
--
-- @since 0.7.0
getXdgDirectoryList :: XdgDirectoryList -> IOExcept [FilePath]
getXdgDirectoryList =
  exceptIO . D.getXdgDirectoryList
{-# INLINE getXdgDirectoryList #-}

-- | The directory of an application's data, in the home directory (on
-- POSIX, @~/.app@).
--
-- ==== __Examples__
--
-- >>> fmap (isSuffixOf ".papa") <$> runExceptT (getAppUserDataDirectory "papa")
-- Right True
--
-- @since 0.7.0
getAppUserDataDirectory :: FilePath -> IOExcept FilePath
getAppUserDataDirectory =
  exceptIO . D.getAppUserDataDirectory
{-# INLINE getAppUserDataDirectory #-}

-- | The directory of the user's documents (on POSIX, the home directory).
--
-- ==== __Examples__
--
-- >>> isRight <$> runExceptT getUserDocumentsDirectory
-- True
--
-- @since 0.7.0
getUserDocumentsDirectory :: IOExcept FilePath
getUserDocumentsDirectory =
  exceptIO D.getUserDocumentsDirectory
{-# INLINE getUserDocumentsDirectory #-}

-- | The directory for temporary files: @TMPDIR@, or else @\/tmp@, on POSIX.
--
-- ==== __Examples__
--
-- >>> (==) <$> runExceptT getTemporaryDirectory <*> fmap Right D.getTemporaryDirectory
-- True
--
-- @since 0.7.0
getTemporaryDirectory :: IOExcept FilePath
getTemporaryDirectory =
  exceptIO D.getTemporaryDirectory
{-# INLINE getTemporaryDirectory #-}

-- | Remove a file.
--
-- ==== __Examples__
--
-- >>> runExceptT (writeFile (dir </> "rm") (pack "") >> removeFile (dir </> "rm") >> doesFileExist (dir </> "rm"))
-- Right False
--
-- >>> either (anyOf _IOException (\i -> has _NoSuchThing (the errorType i))) (const False) <$> runExceptT (removeFile (dir </> "rm"))
-- True
--
-- @since 0.7.0
removeFile :: FilePath -> IOExcept ()
removeFile =
  exceptIO . D.removeFile
{-# INLINE removeFile #-}

-- | Rename a file, replacing the file at the new path, if any.
--
-- ==== __Examples__
--
-- >>> runExceptT (writeFile (dir </> "f") (pack "f") >> renameFile (dir </> "f") (dir </> "g") >> readFile (dir </> "g"))
-- Right "f"
--
-- @since 0.7.0
renameFile :: FilePath -> FilePath -> IOExcept ()
renameFile a =
  exceptIO . D.renameFile a
{-# INLINE renameFile #-}

-- | Rename a file or directory.
--
-- ==== __Examples__
--
-- >>> runExceptT (createDirectory (dir </> "t") >> renamePath (dir </> "t") (dir </> "u") >> doesDirectoryExist (dir </> "u"))
-- Right True
--
-- @since 0.7.0
renamePath :: FilePath -> FilePath -> IOExcept ()
renamePath a =
  exceptIO . D.renamePath a
{-# INLINE renamePath #-}

-- | Copy a file and its permissions.
--
-- ==== __Examples__
--
-- >>> runExceptT (copyFile file (dir </> "copy") >> readFile (dir </> "copy"))
-- Right "abc"
--
-- @since 0.7.0
copyFile :: FilePath -> FilePath -> IOExcept ()
copyFile a =
  exceptIO . D.copyFile a
{-# INLINE copyFile #-}

-- | Copy a file, its permissions and its timestamps.
--
-- ==== __Examples__
--
-- >>> runExceptT (copyFileWithMetadata file (dir </> "copy2") >> ((==) <$> getModificationTime file <*> getModificationTime (dir </> "copy2")))
-- Right True
--
-- @since 0.7.0
copyFileWithMetadata :: FilePath -> FilePath -> IOExcept ()
copyFileWithMetadata a =
  exceptIO . D.copyFileWithMetadata a
{-# INLINE copyFileWithMetadata #-}

-- | The size of a file, in bytes.
--
-- ==== __Examples__
--
-- >>> runExceptT (getFileSize file)
-- Right 3
--
-- @since 0.7.0
getFileSize :: FilePath -> IOExcept Integer
getFileSize =
  exceptIO . D.getFileSize
{-# INLINE getFileSize #-}

-- | The absolute path, with symbolic links, @.@ and @..@ resolved, of a path,
-- which need not exist.
--
-- ==== __Examples__
--
-- >>> (==) <$> runExceptT (canonicalizePath (dir </> "." </> "a.txt")) <*> fmap Right (D.canonicalizePath file)
-- True
--
-- @since 0.7.0
canonicalizePath :: FilePath -> IOExcept FilePath
canonicalizePath =
  exceptIO . D.canonicalizePath
{-# INLINE canonicalizePath #-}

-- | The absolute path of a path, relative to the current directory, which
-- need not exist. Symbolic links are not resolved.
--
-- ==== __Examples__
--
-- >>> fmap isAbsolute <$> runExceptT (makeAbsolute "x")
-- Right True
--
-- @since 0.7.0
makeAbsolute :: FilePath -> IOExcept FilePath
makeAbsolute =
  exceptIO . D.makeAbsolute
{-# INLINE makeAbsolute #-}

-- | A path relative to the current directory, if it is in it.
--
-- ==== __Examples__
--
-- >>> runExceptT (getCurrentDirectory >>= \c -> makeRelativeToCurrentDirectory (c </> "x"))
-- Right "x"
--
-- @since 0.7.0
makeRelativeToCurrentDirectory :: FilePath -> IOExcept FilePath
makeRelativeToCurrentDirectory =
  exceptIO . D.makeRelativeToCurrentDirectory
{-# INLINE makeRelativeToCurrentDirectory #-}

-- | Whether a file or directory exists.
--
-- ==== __Examples__
--
-- >>> runExceptT (traverse doesPathExist [dir, file, dir </> "nothing"])
-- Right [True,True,False]
--
-- @since 0.7.0
doesPathExist :: FilePath -> IOExcept Bool
doesPathExist =
  exceptIO . D.doesPathExist
{-# INLINE doesPathExist #-}

-- | Whether a file exists (and is not a directory).
--
-- ==== __Examples__
--
-- >>> runExceptT (traverse doesFileExist [dir, file, dir </> "nothing"])
-- Right [False,True,False]
--
-- @since 0.7.0
doesFileExist :: FilePath -> IOExcept Bool
doesFileExist =
  exceptIO . D.doesFileExist
{-# INLINE doesFileExist #-}

-- | Whether a directory exists.
--
-- ==== __Examples__
--
-- >>> runExceptT (traverse doesDirectoryExist [dir, file, dir </> "nothing"])
-- Right [True,False,False]
--
-- @since 0.7.0
doesDirectoryExist :: FilePath -> IOExcept Bool
doesDirectoryExist =
  exceptIO . D.doesDirectoryExist
{-# INLINE doesDirectoryExist #-}

-- | The path of an executable on the search path (@PATH@), if any.
--
-- ==== __Examples__
--
-- >>> fmap (fmap takeFileName) <$> runExceptT (findExecutable "sh")
-- Right (Just "sh")
--
-- >>> runExceptT (findExecutable "papa-no-such-executable")
-- Right Nothing
--
-- @since 0.7.0
findExecutable :: String -> IOExcept (Maybe FilePath)
findExecutable =
  exceptIO . D.findExecutable
{-# INLINE findExecutable #-}

-- | The paths of every executable of a name on the search path.
--
-- ==== __Examples__
--
-- >>> runExceptT (findExecutables "papa-no-such-executable")
-- Right []
--
-- @since 0.7.0
findExecutables :: String -> IOExcept [FilePath]
findExecutables =
  exceptIO . D.findExecutables
{-# INLINE findExecutables #-}

-- | The paths of every executable of a name in the given directories.
--
-- ==== __Examples__
--
-- >>> runExceptT (findExecutablesInDirectories [dir] "a.txt")
-- Right []
--
-- @since 0.7.0
findExecutablesInDirectories :: [FilePath] -> String -> IOExcept [FilePath]
findExecutablesInDirectories ds =
  exceptIO . D.findExecutablesInDirectories ds
{-# INLINE findExecutablesInDirectories #-}

-- | The path of the first file of a name in the given directories, if any.
--
-- ==== __Examples__
--
-- >>> (==) <$> runExceptT (findFile ["/nonexistent/papa", dir] "a.txt") <*> pure (Right (Just file))
-- True
--
-- @since 0.7.0
findFile :: [FilePath] -> String -> IOExcept (Maybe FilePath)
findFile ds =
  exceptIO . D.findFile ds
{-# INLINE findFile #-}

-- | The paths of every file of a name in the given directories.
--
-- ==== __Examples__
--
-- >>> (==) <$> runExceptT (findFiles [dir, dir] "a.txt") <*> pure (Right [file, file])
-- True
--
-- @since 0.7.0
findFiles :: [FilePath] -> String -> IOExcept [FilePath]
findFiles ds =
  exceptIO . D.findFiles ds
{-# INLINE findFiles #-}

-- | Create a symbolic link to a file.
--
-- ==== __Examples__
--
-- >>> runExceptT (createFileLink file (dir </> "link") >> pathIsSymbolicLink (dir </> "link"))
-- Right True
--
-- @since 0.7.0
createFileLink :: FilePath -> FilePath -> IOExcept ()
createFileLink a =
  exceptIO . D.createFileLink a
{-# INLINE createFileLink #-}

-- | Create a symbolic link to a directory.
--
-- ==== __Examples__
--
-- >>> runExceptT (createDirectoryLink dir (dir </> "dlink") >> pathIsSymbolicLink (dir </> "dlink"))
-- Right True
--
-- @since 0.7.0
createDirectoryLink :: FilePath -> FilePath -> IOExcept ()
createDirectoryLink a =
  exceptIO . D.createDirectoryLink a
{-# INLINE createDirectoryLink #-}

-- | Remove a symbolic link to a directory, not the directory.
--
-- ==== __Examples__
--
-- >>> runExceptT (createDirectoryLink dir (dir </> "dlink2") >> removeDirectoryLink (dir </> "dlink2") >> traverse doesPathExist [dir </> "dlink2", dir])
-- Right [False,True]
--
-- @since 0.7.0
removeDirectoryLink :: FilePath -> IOExcept ()
removeDirectoryLink =
  exceptIO . D.removeDirectoryLink
{-# INLINE removeDirectoryLink #-}

-- | Whether a path is a symbolic link. A path that does not exist is a
-- failure.
--
-- ==== __Examples__
--
-- >>> runExceptT (pathIsSymbolicLink file)
-- Right False
--
-- >>> isLeft <$> runExceptT (pathIsSymbolicLink (dir </> "nothing"))
-- True
--
-- @since 0.7.0
pathIsSymbolicLink :: FilePath -> IOExcept Bool
pathIsSymbolicLink =
  exceptIO . D.pathIsSymbolicLink
{-# INLINE pathIsSymbolicLink #-}

-- | The target of a symbolic link.
--
-- ==== __Examples__
--
-- >>> (==) <$> runExceptT (createFileLink file (dir </> "link2") >> getSymbolicLinkTarget (dir </> "link2")) <*> pure (Right file)
-- True
--
-- @since 0.7.0
getSymbolicLinkTarget :: FilePath -> IOExcept FilePath
getSymbolicLinkTarget =
  exceptIO . D.getSymbolicLinkTarget
{-# INLINE getSymbolicLinkTarget #-}

-- | The permissions of a file or directory.
--
-- ==== __Examples__
--
-- >>> fmap readable <$> runExceptT (getPermissions file)
-- Right True
--
-- @since 0.7.0
getPermissions :: FilePath -> IOExcept Permissions
getPermissions =
  exceptIO . D.getPermissions
{-# INLINE getPermissions #-}

-- | Set the permissions of a file or directory.
--
-- ==== __Examples__
--
-- >>> runExceptT (writeFile (dir </> "perm1") (pack "") >> getPermissions (dir </> "perm1") >>= \p -> setPermissions (dir </> "perm1") (setOwnerExecutable True p) >> fmap executable (getPermissions (dir </> "perm1")))
-- Right True
--
-- @since 0.7.0
setPermissions :: FilePath -> Permissions -> IOExcept ()
setPermissions p =
  exceptIO . D.setPermissions p
{-# INLINE setPermissions #-}

-- | Copy the permissions of one file or directory to another.
--
-- ==== __Examples__
--
-- >>> runExceptT (writeFile (dir </> "perm2") (pack "") >> getPermissions (dir </> "perm2") >>= \p -> setPermissions (dir </> "perm2") (setOwnerExecutable True p) >> writeFile (dir </> "perm3") (pack "") >> copyPermissions (dir </> "perm2") (dir </> "perm3") >> fmap executable (getPermissions (dir </> "perm3")))
-- Right True
--
-- @since 0.7.0
copyPermissions :: FilePath -> FilePath -> IOExcept ()
copyPermissions a =
  exceptIO . D.copyPermissions a
{-# INLINE copyPermissions #-}

-- | The time a file or directory was last read.
--
-- ==== __Examples__
--
-- >>> isRight <$> runExceptT (getAccessTime file)
-- True
--
-- @since 0.7.0
getAccessTime :: FilePath -> IOExcept UTCTime
getAccessTime =
  exceptIO . D.getAccessTime
{-# INLINE getAccessTime #-}

-- | The time a file or directory was last written.
--
-- ==== __Examples__
--
-- >>> either (anyOf _IOException (\i -> has _NoSuchThing (the errorType i))) (const False) <$> runExceptT (getModificationTime (dir </> "nothing"))
-- True
--
-- @since 0.7.0
getModificationTime :: FilePath -> IOExcept UTCTime
getModificationTime =
  exceptIO . D.getModificationTime
{-# INLINE getModificationTime #-}

-- | Set the time a file or directory was last read.
--
-- ==== __Examples__
--
-- >>> let t = Time.UTCTime (Time.fromGregorian 2000 1 1) 0
-- >>> runExceptT (setAccessTime file t >> getAccessTime file)
-- Right 2000-01-01 00:00:00 UTC
--
-- @since 0.7.0
setAccessTime :: FilePath -> UTCTime -> IOExcept ()
setAccessTime p =
  exceptIO . D.setAccessTime p
{-# INLINE setAccessTime #-}

-- | Set the time a file or directory was last written.
--
-- ==== __Examples__
--
-- >>> let t = Time.UTCTime (Time.fromGregorian 2000 1 1) 0
-- >>> runExceptT (setModificationTime file t >> getModificationTime file)
-- Right 2000-01-01 00:00:00 UTC
--
-- >>> D.removeDirectoryRecursive dir
--
-- @since 0.7.0
setModificationTime :: FilePath -> UTCTime -> IOExcept ()
setModificationTime p =
  exceptIO . D.setModificationTime p
{-# INLINE setModificationTime #-}