2015-12-17 04:42:22 +01:00
|
|
|
{-# OPTIONS_HADDOCK ignore-exports #-}
|
|
|
|
|
{-# LANGUAGE DeriveDataTypeable #-}
|
|
|
|
|
|
2015-12-19 16:13:48 +01:00
|
|
|
-- |Provides error handling.
|
2015-12-17 04:42:22 +01:00
|
|
|
module IO.Error where
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import Control.Exception
|
|
|
|
|
import Control.Monad
|
|
|
|
|
(
|
2015-12-17 23:08:02 +01:00
|
|
|
unless
|
|
|
|
|
, void
|
|
|
|
|
, when
|
2015-12-17 04:42:22 +01:00
|
|
|
)
|
2015-12-18 04:22:13 +01:00
|
|
|
import Data.List
|
|
|
|
|
(
|
|
|
|
|
isPrefixOf
|
|
|
|
|
)
|
2015-12-17 04:42:22 +01:00
|
|
|
import Data.Typeable
|
2015-12-18 15:37:14 +01:00
|
|
|
import IO.Utils
|
2015-12-17 23:08:02 +01:00
|
|
|
import System.Directory
|
|
|
|
|
(
|
|
|
|
|
doesDirectoryExist
|
|
|
|
|
, doesFileExist
|
|
|
|
|
)
|
|
|
|
|
import System.FilePath
|
|
|
|
|
(
|
|
|
|
|
equalFilePath
|
|
|
|
|
, isAbsolute
|
|
|
|
|
, takeFileName
|
|
|
|
|
)
|
2015-12-17 04:42:22 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
data FmIOException = FileDoesNotExist String
|
2015-12-19 16:13:48 +01:00
|
|
|
| DirDoesNotExist String
|
2015-12-17 04:42:22 +01:00
|
|
|
| PathNotAbsolute String
|
|
|
|
|
| FileNotExecutable String
|
2015-12-17 23:08:02 +01:00
|
|
|
| SameFile String String
|
|
|
|
|
| NotAFile String
|
|
|
|
|
| NotADir String
|
2015-12-18 04:22:13 +01:00
|
|
|
| DestinationInSource String String
|
2015-12-18 15:28:04 +01:00
|
|
|
| DirDoesExist String
|
2015-12-17 04:42:22 +01:00
|
|
|
deriving (Show, Typeable)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
instance Exception FmIOException
|
|
|
|
|
|
2015-12-17 23:08:02 +01:00
|
|
|
|
|
|
|
|
-- Throws an exception if the filepath is not absolute
|
|
|
|
|
-- or the file does not exist.
|
|
|
|
|
fileSanityThrow :: FilePath -> IO ()
|
2015-12-18 15:37:14 +01:00
|
|
|
fileSanityThrow fp = throwNotAbsolute fp >> throwFileDoesNotExist fp
|
2015-12-17 23:08:02 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
-- Throws an exception if the filepath is not absolute
|
|
|
|
|
-- or the dir does not exist.
|
|
|
|
|
dirSanityThrow :: FilePath -> IO ()
|
2015-12-18 15:37:14 +01:00
|
|
|
dirSanityThrow fp = throwNotAbsolute fp >> throwDirDoesNotExist fp
|
2015-12-17 23:08:02 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
throwNotAbsolute :: FilePath -> IO ()
|
|
|
|
|
throwNotAbsolute fp = unless (isAbsolute fp) (throw $ PathNotAbsolute fp)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
throwDirDoesExist :: FilePath -> IO ()
|
2015-12-18 15:37:14 +01:00
|
|
|
throwDirDoesExist fp =
|
|
|
|
|
whenM (doesDirectoryExist fp) (throw $ DirDoesExist fp)
|
2015-12-17 23:08:02 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
throwDirDoesNotExist :: FilePath -> IO ()
|
2015-12-18 15:37:14 +01:00
|
|
|
throwDirDoesNotExist fp =
|
2015-12-19 16:13:48 +01:00
|
|
|
unlessM (doesDirectoryExist fp) (throw $ DirDoesNotExist fp)
|
2015-12-17 23:08:02 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
throwFileDoesNotExist :: FilePath -> IO ()
|
2015-12-18 15:37:14 +01:00
|
|
|
throwFileDoesNotExist fp =
|
|
|
|
|
unlessM (doesFileExist fp) (throw $ FileDoesNotExist fp)
|
2015-12-17 23:08:02 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
throwSameFile :: FilePath -- ^ should be canonicalized
|
|
|
|
|
-> FilePath -- ^ should be canonicalized
|
|
|
|
|
-> IO ()
|
|
|
|
|
throwSameFile fp1 fp2 = when (equalFilePath fp1 fp2) (throw $ SameFile fp1 fp2)
|
2015-12-18 04:22:13 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
throwDestinationInSource :: FilePath -- ^ should be canonicalized
|
|
|
|
|
-> FilePath -- ^ should be canonicalized
|
|
|
|
|
-> IO ()
|
|
|
|
|
throwDestinationInSource source dest =
|
|
|
|
|
when (source `isPrefixOf` dest) (throw $ DestinationInSource dest source)
|