module System.Directory.Internal.Common where
import Prelude ()
import System.Directory.Internal.Prelude
import System.FilePath
  ( addTrailingPathSeparator
  , hasTrailingPathSeparator
  , isPathSeparator
  , isRelative
  , joinDrive
  , joinPath
  , normalise
  , pathSeparator
  , pathSeparators
  , splitDirectories
  , splitDrive
  )

-- | A generator with side-effects.
newtype ListT m a = ListT { forall (m :: * -> *) a. ListT m a -> m (Maybe (a, ListT m a))
unListT :: m (Maybe (a, ListT m a)) }

emptyListT :: Applicative m => ListT m a
emptyListT :: forall (m :: * -> *) a. Applicative m => ListT m a
emptyListT = forall (m :: * -> *) a. m (Maybe (a, ListT m a)) -> ListT m a
ListT (forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a. Maybe a
Nothing)

maybeToListT :: Applicative m => m (Maybe a) -> ListT m a
maybeToListT :: forall (m :: * -> *) a. Applicative m => m (Maybe a) -> ListT m a
maybeToListT m (Maybe a)
m = forall (m :: * -> *) a. m (Maybe (a, ListT m a)) -> ListT m a
ListT (((\ a
x -> (a
x, forall (m :: * -> *) a. Applicative m => ListT m a
emptyListT)) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m (Maybe a)
m)

listToListT :: Applicative m => [a] -> ListT m a
listToListT :: forall (m :: * -> *) a. Applicative m => [a] -> ListT m a
listToListT [] = forall (m :: * -> *) a. Applicative m => ListT m a
emptyListT
listToListT (a
x : [a]
xs) = forall (m :: * -> *) a. m (Maybe (a, ListT m a)) -> ListT m a
ListT (forall (f :: * -> *) a. Applicative f => a -> f a
pure (forall a. a -> Maybe a
Just (a
x, forall (m :: * -> *) a. Applicative m => [a] -> ListT m a
listToListT [a]
xs)))

liftJoinListT :: Monad m => m (ListT m a) -> ListT m a
liftJoinListT :: forall (m :: * -> *) a. Monad m => m (ListT m a) -> ListT m a
liftJoinListT m (ListT m a)
m = forall (m :: * -> *) a. m (Maybe (a, ListT m a)) -> ListT m a
ListT (m (ListT m a)
m forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall (m :: * -> *) a. ListT m a -> m (Maybe (a, ListT m a))
unListT)

listTHead :: Functor m => ListT m a -> m (Maybe a)
listTHead :: forall (m :: * -> *) a. Functor m => ListT m a -> m (Maybe a)
listTHead (ListT m (Maybe (a, ListT m a))
m) = (forall a b. (a, b) -> a
fst forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m (Maybe (a, ListT m a))
m

listTToList :: Monad m => ListT m a -> m [a]
listTToList :: forall (m :: * -> *) a. Monad m => ListT m a -> m [a]
listTToList (ListT m (Maybe (a, ListT m a))
m) = do
  Maybe (a, ListT m a)
mx <- m (Maybe (a, ListT m a))
m
  case Maybe (a, ListT m a)
mx of
    Maybe (a, ListT m a)
Nothing -> forall (m :: * -> *) a. Monad m => a -> m a
return []
    Just (a
x, ListT m a
m') -> do
      [a]
xs <- forall (m :: * -> *) a. Monad m => ListT m a -> m [a]
listTToList ListT m a
m'
      forall (m :: * -> *) a. Monad m => a -> m a
return (a
x forall a. a -> [a] -> [a]
: [a]
xs)

andM :: Monad m => m Bool -> m Bool -> m Bool
andM :: forall (m :: * -> *). Monad m => m Bool -> m Bool -> m Bool
andM m Bool
mx m Bool
my = do
  Bool
x <- m Bool
mx
  if Bool
x
    then m Bool
my
    else forall (m :: * -> *) a. Monad m => a -> m a
return Bool
x

sequenceWithIOErrors_ :: [IO ()] -> IO ()
sequenceWithIOErrors_ :: [IO ()] -> IO ()
sequenceWithIOErrors_ [IO ()]
actions = Either IOError () -> [IO ()] -> IO ()
go (forall a b. b -> Either a b
Right ()) [IO ()]
actions
  where

    go :: Either IOError () -> [IO ()] -> IO ()
    go :: Either IOError () -> [IO ()] -> IO ()
go (Left IOError
e)   []       = forall a. IOError -> IO a
ioError IOError
e
    go (Right ()) []       = forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
    go Either IOError ()
s          (IO ()
m : [IO ()]
ms) = Either IOError ()
s seq :: forall a b. a -> b -> b
`seq` do
      Either IOError ()
r <- forall a. IO a -> IO (Either IOError a)
tryIOError IO ()
m
      Either IOError () -> [IO ()] -> IO ()
go (forall b a. Either b a -> Either b a -> Either b a
thenEither Either IOError ()
s Either IOError ()
r) [IO ()]
ms

    -- equivalent to (*>) for Either, defined here to retain compatibility
    -- with base prior to 4.3
    thenEither :: Either b a -> Either b a -> Either b a
    thenEither :: forall b a. Either b a -> Either b a -> Either b a
thenEither x :: Either b a
x@(Left b
_) Either b a
_ = Either b a
x
    thenEither Either b a
_          Either b a
y = Either b a
y

-- | Similar to 'try' but only catches a specify kind of 'IOError' as
--   specified by the predicate.
tryIOErrorType :: (IOError -> Bool) -> IO a -> IO (Either IOError a)
tryIOErrorType :: forall a. (IOError -> Bool) -> IO a -> IO (Either IOError a)
tryIOErrorType IOError -> Bool
check IO a
action = do
  Either IOError a
result <- forall a. IO a -> IO (Either IOError a)
tryIOError IO a
action
  case Either IOError a
result of
    Left  IOError
err -> if IOError -> Bool
check IOError
err then forall (f :: * -> *) a. Applicative f => a -> f a
pure (forall a b. a -> Either a b
Left IOError
err) else forall e a. Exception e => e -> IO a
throwIO IOError
err
    Right a
val -> forall (f :: * -> *) a. Applicative f => a -> f a
pure (forall a b. b -> Either a b
Right a
val)

-- | Attempt to perform the given action, silencing any IO exception thrown by
-- it.
ignoreIOExceptions :: IO () -> IO ()
ignoreIOExceptions :: IO () -> IO ()
ignoreIOExceptions IO ()
io = IO ()
io forall a. IO a -> (IOError -> IO a) -> IO a
`catchIOError` (\IOError
_ -> forall (f :: * -> *) a. Applicative f => a -> f a
pure ())

specializeErrorString :: String -> (IOError -> Bool) -> IO a -> IO a
specializeErrorString :: forall a. FilePath -> (IOError -> Bool) -> IO a -> IO a
specializeErrorString FilePath
str IOError -> Bool
errType IO a
action = do
  Either IOError a
mx <- forall a. (IOError -> Bool) -> IO a -> IO (Either IOError a)
tryIOErrorType IOError -> Bool
errType IO a
action
  case Either IOError a
mx of
    Left  IOError
e -> forall e a. Exception e => e -> IO a
throwIO (IOError -> FilePath -> IOError
ioeSetErrorString IOError
e FilePath
str)
    Right a
x -> forall (f :: * -> *) a. Applicative f => a -> f a
pure a
x

ioeAddLocation :: IOError -> String -> IOError
ioeAddLocation :: IOError -> FilePath -> IOError
ioeAddLocation IOError
e FilePath
loc = do
  IOError -> FilePath -> IOError
ioeSetLocation IOError
e FilePath
newLoc
  where
    newLoc :: FilePath
newLoc = FilePath
loc forall a. Semigroup a => a -> a -> a
<> if forall (t :: * -> *) a. Foldable t => t a -> Bool
null FilePath
oldLoc then FilePath
"" else FilePath
":" forall a. Semigroup a => a -> a -> a
<> FilePath
oldLoc
    oldLoc :: FilePath
oldLoc = IOError -> FilePath
ioeGetLocation IOError
e

-- | Given a list of path segments, expand @.@ and @..@.  The path segments
-- must not contain path separators.
expandDots :: [FilePath] -> [FilePath]
expandDots :: [FilePath] -> [FilePath]
expandDots = forall a. [a] -> [a]
reverse forall b c a. (b -> c) -> (a -> b) -> a -> c
. [FilePath] -> [FilePath] -> [FilePath]
go []
  where
    go :: [FilePath] -> [FilePath] -> [FilePath]
go [FilePath]
ys' [FilePath]
xs' =
      case [FilePath]
xs' of
        [] -> [FilePath]
ys'
        FilePath
x : [FilePath]
xs ->
          case FilePath
x of
            FilePath
"." -> [FilePath] -> [FilePath] -> [FilePath]
go [FilePath]
ys' [FilePath]
xs
            FilePath
".." ->
              case [FilePath]
ys' of
                [] -> [FilePath] -> [FilePath] -> [FilePath]
go (FilePath
x forall a. a -> [a] -> [a]
: [FilePath]
ys') [FilePath]
xs
                FilePath
".." : [FilePath]
_ -> [FilePath] -> [FilePath] -> [FilePath]
go (FilePath
x forall a. a -> [a] -> [a]
: [FilePath]
ys') [FilePath]
xs
                FilePath
_ : [FilePath]
ys -> [FilePath] -> [FilePath] -> [FilePath]
go [FilePath]
ys [FilePath]
xs
            FilePath
_ -> [FilePath] -> [FilePath] -> [FilePath]
go (FilePath
x forall a. a -> [a] -> [a]
: [FilePath]
ys') [FilePath]
xs

-- | Convert to the right kind of slashes.
normalisePathSeps :: FilePath -> FilePath
normalisePathSeps :: FilePath -> FilePath
normalisePathSeps FilePath
p = (\ Char
c -> if Char -> Bool
isPathSeparator Char
c then Char
pathSeparator else Char
c) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> FilePath
p

-- | Remove redundant trailing slashes and pick the right kind of slash.
normaliseTrailingSep :: FilePath -> FilePath
normaliseTrailingSep :: FilePath -> FilePath
normaliseTrailingSep FilePath
path = do
  let path' :: FilePath
path' = forall a. [a] -> [a]
reverse FilePath
path
  let (FilePath
sep, FilePath
path'') = forall a. (a -> Bool) -> [a] -> ([a], [a])
span Char -> Bool
isPathSeparator FilePath
path'
  let addSep :: FilePath -> FilePath
addSep = if forall (t :: * -> *) a. Foldable t => t a -> Bool
null FilePath
sep then forall a. a -> a
id else (Char
pathSeparator forall a. a -> [a] -> [a]
:)
  forall a. [a] -> [a]
reverse (FilePath -> FilePath
addSep FilePath
path'')

-- | Convert empty paths to the current directory, otherwise leave it
-- unchanged.
emptyToCurDir :: FilePath -> FilePath
emptyToCurDir :: FilePath -> FilePath
emptyToCurDir FilePath
""   = FilePath
"."
emptyToCurDir FilePath
path = FilePath
path

-- | Similar to 'normalise' but empty paths stay empty.
simplifyPosix :: FilePath -> FilePath
simplifyPosix :: FilePath -> FilePath
simplifyPosix FilePath
""   = FilePath
""
simplifyPosix FilePath
path = FilePath -> FilePath
normalise FilePath
path

-- | Similar to 'normalise' but:
--
-- * empty paths stay empty,
-- * parent dirs (@..@) are expanded, and
-- * paths starting with @\\\\?\\@ are preserved.
--
-- The goal is to preserve the meaning of paths better than 'normalise'.
simplifyWindows :: FilePath -> FilePath
simplifyWindows :: FilePath -> FilePath
simplifyWindows FilePath
"" = FilePath
""
simplifyWindows FilePath
path =
  case FilePath
drive' of
    FilePath
"\\\\?\\" -> FilePath
drive' forall a. Semigroup a => a -> a -> a
<> FilePath
subpath
    FilePath
_ -> FilePath
simplifiedPath
  where
    simplifiedPath :: FilePath
simplifiedPath = FilePath -> FilePath -> FilePath
joinDrive FilePath
drive' FilePath
subpath'
    (FilePath
drive, FilePath
subpath) = FilePath -> (FilePath, FilePath)
splitDrive FilePath
path
    drive' :: FilePath
drive' = FilePath -> FilePath
upperDrive (FilePath -> FilePath
normaliseTrailingSep (FilePath -> FilePath
normalisePathSeps FilePath
drive))
    subpath' :: FilePath
subpath' = FilePath -> FilePath
appendSep forall b c a. (b -> c) -> (a -> b) -> a -> c
. FilePath -> FilePath
avoidEmpty forall b c a. (b -> c) -> (a -> b) -> a -> c
. FilePath -> FilePath
prependSep forall b c a. (b -> c) -> (a -> b) -> a -> c
. [FilePath] -> FilePath
joinPath forall b c a. (b -> c) -> (a -> b) -> a -> c
.
               [FilePath] -> [FilePath]
stripPardirs forall b c a. (b -> c) -> (a -> b) -> a -> c
. [FilePath] -> [FilePath]
expandDots forall b c a. (b -> c) -> (a -> b) -> a -> c
. [FilePath] -> [FilePath]
skipSeps forall b c a. (b -> c) -> (a -> b) -> a -> c
.
               FilePath -> [FilePath]
splitDirectories forall a b. (a -> b) -> a -> b
$ FilePath
subpath

    upperDrive :: FilePath -> FilePath
upperDrive FilePath
d = case FilePath
d of
      Char
c : Char
':' : FilePath
s | Char -> Bool
isAlpha Char
c Bool -> Bool -> Bool
&& forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all Char -> Bool
isPathSeparator FilePath
s -> Char -> Char
toUpper Char
c forall a. a -> [a] -> [a]
: Char
':' forall a. a -> [a] -> [a]
: FilePath
s
      FilePath
_ -> FilePath
d
    skipSeps :: [FilePath] -> [FilePath]
skipSeps = forall a. (a -> Bool) -> [a] -> [a]
filter (Bool -> Bool
not forall b c a. (b -> c) -> (a -> b) -> a -> c
. (forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` (forall (f :: * -> *) a. Applicative f => a -> f a
pure forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> FilePath
pathSeparators)))
    stripPardirs :: [FilePath] -> [FilePath]
stripPardirs | Bool
pathIsAbsolute Bool -> Bool -> Bool
|| Bool
subpathIsAbsolute = forall a. (a -> Bool) -> [a] -> [a]
dropWhile (forall a. Eq a => a -> a -> Bool
== FilePath
"..")
                 | Bool
otherwise = forall a. a -> a
id
    prependSep :: FilePath -> FilePath
prependSep | Bool
subpathIsAbsolute = (Char
pathSeparator forall a. a -> [a] -> [a]
:)
               | Bool
otherwise = forall a. a -> a
id
    avoidEmpty :: FilePath -> FilePath
avoidEmpty | Bool -> Bool
not Bool
pathIsAbsolute
                 Bool -> Bool -> Bool
&& (forall (t :: * -> *) a. Foldable t => t a -> Bool
null FilePath
drive Bool -> Bool -> Bool
|| Bool
hasTrailingPathSep) -- prefer "C:" over "C:."
                 = FilePath -> FilePath
emptyToCurDir
               | Bool
otherwise = forall a. a -> a
id
    appendSep :: FilePath -> FilePath
appendSep FilePath
p | Bool
hasTrailingPathSep
                  Bool -> Bool -> Bool
&& Bool -> Bool
not (Bool
pathIsAbsolute Bool -> Bool -> Bool
&& forall (t :: * -> *) a. Foldable t => t a -> Bool
null FilePath
p)
                  = FilePath -> FilePath
addTrailingPathSeparator FilePath
p
                | Bool
otherwise = FilePath
p
    pathIsAbsolute :: Bool
pathIsAbsolute = Bool -> Bool
not (FilePath -> Bool
isRelative FilePath
path)
    subpathIsAbsolute :: Bool
subpathIsAbsolute = forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any Char -> Bool
isPathSeparator (forall a. Int -> [a] -> [a]
take Int
1 FilePath
subpath)
    hasTrailingPathSep :: Bool
hasTrailingPathSep = FilePath -> Bool
hasTrailingPathSeparator FilePath
subpath

data FileType = File
              | SymbolicLink -- ^ POSIX: either file or directory link; Windows: file link
              | Directory
              | DirectoryLink -- ^ Windows only: directory link
              deriving (FileType
forall a. a -> a -> Bounded a
maxBound :: FileType
$cmaxBound :: FileType
minBound :: FileType
$cminBound :: FileType
Bounded, Int -> FileType
FileType -> Int
FileType -> [FileType]
FileType -> FileType
FileType -> FileType -> [FileType]
FileType -> FileType -> FileType -> [FileType]
forall a.
(a -> a)
-> (a -> a)
-> (Int -> a)
-> (a -> Int)
-> (a -> [a])
-> (a -> a -> [a])
-> (a -> a -> [a])
-> (a -> a -> a -> [a])
-> Enum a
enumFromThenTo :: FileType -> FileType -> FileType -> [FileType]
$cenumFromThenTo :: FileType -> FileType -> FileType -> [FileType]
enumFromTo :: FileType -> FileType -> [FileType]
$cenumFromTo :: FileType -> FileType -> [FileType]
enumFromThen :: FileType -> FileType -> [FileType]
$cenumFromThen :: FileType -> FileType -> [FileType]
enumFrom :: FileType -> [FileType]
$cenumFrom :: FileType -> [FileType]
fromEnum :: FileType -> Int
$cfromEnum :: FileType -> Int
toEnum :: Int -> FileType
$ctoEnum :: Int -> FileType
pred :: FileType -> FileType
$cpred :: FileType -> FileType
succ :: FileType -> FileType
$csucc :: FileType -> FileType
Enum, FileType -> FileType -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: FileType -> FileType -> Bool
$c/= :: FileType -> FileType -> Bool
== :: FileType -> FileType -> Bool
$c== :: FileType -> FileType -> Bool
Eq, Eq FileType
FileType -> FileType -> Bool
FileType -> FileType -> Ordering
FileType -> FileType -> FileType
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: FileType -> FileType -> FileType
$cmin :: FileType -> FileType -> FileType
max :: FileType -> FileType -> FileType
$cmax :: FileType -> FileType -> FileType
>= :: FileType -> FileType -> Bool
$c>= :: FileType -> FileType -> Bool
> :: FileType -> FileType -> Bool
$c> :: FileType -> FileType -> Bool
<= :: FileType -> FileType -> Bool
$c<= :: FileType -> FileType -> Bool
< :: FileType -> FileType -> Bool
$c< :: FileType -> FileType -> Bool
compare :: FileType -> FileType -> Ordering
$ccompare :: FileType -> FileType -> Ordering
Ord, ReadPrec [FileType]
ReadPrec FileType
Int -> ReadS FileType
ReadS [FileType]
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [FileType]
$creadListPrec :: ReadPrec [FileType]
readPrec :: ReadPrec FileType
$creadPrec :: ReadPrec FileType
readList :: ReadS [FileType]
$creadList :: ReadS [FileType]
readsPrec :: Int -> ReadS FileType
$creadsPrec :: Int -> ReadS FileType
Read, Int -> FileType -> FilePath -> FilePath
[FileType] -> FilePath -> FilePath
FileType -> FilePath
forall a.
(Int -> a -> FilePath -> FilePath)
-> (a -> FilePath) -> ([a] -> FilePath -> FilePath) -> Show a
showList :: [FileType] -> FilePath -> FilePath
$cshowList :: [FileType] -> FilePath -> FilePath
show :: FileType -> FilePath
$cshow :: FileType -> FilePath
showsPrec :: Int -> FileType -> FilePath -> FilePath
$cshowsPrec :: Int -> FileType -> FilePath -> FilePath
Show)

-- | Check whether the given 'FileType' is considered a directory by the
-- operating system.  This affects the choice of certain functions
-- e.g. 'System.Directory.removeDirectory' vs 'System.Directory.removeFile'.
fileTypeIsDirectory :: FileType -> Bool
fileTypeIsDirectory :: FileType -> Bool
fileTypeIsDirectory FileType
Directory     = Bool
True
fileTypeIsDirectory FileType
DirectoryLink = Bool
True
fileTypeIsDirectory FileType
_             = Bool
False

-- | Return whether the given 'FileType' is a link.
fileTypeIsLink :: FileType -> Bool
fileTypeIsLink :: FileType -> Bool
fileTypeIsLink FileType
SymbolicLink  = Bool
True
fileTypeIsLink FileType
DirectoryLink = Bool
True
fileTypeIsLink FileType
_             = Bool
False

data Permissions
  = Permissions
  { Permissions -> Bool
readable :: Bool
  , Permissions -> Bool
writable :: Bool
  , Permissions -> Bool
executable :: Bool
  , Permissions -> Bool
searchable :: Bool
  } deriving (Permissions -> Permissions -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Permissions -> Permissions -> Bool
$c/= :: Permissions -> Permissions -> Bool
== :: Permissions -> Permissions -> Bool
$c== :: Permissions -> Permissions -> Bool
Eq, Eq Permissions
Permissions -> Permissions -> Bool
Permissions -> Permissions -> Ordering
Permissions -> Permissions -> Permissions
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: Permissions -> Permissions -> Permissions
$cmin :: Permissions -> Permissions -> Permissions
max :: Permissions -> Permissions -> Permissions
$cmax :: Permissions -> Permissions -> Permissions
>= :: Permissions -> Permissions -> Bool
$c>= :: Permissions -> Permissions -> Bool
> :: Permissions -> Permissions -> Bool
$c> :: Permissions -> Permissions -> Bool
<= :: Permissions -> Permissions -> Bool
$c<= :: Permissions -> Permissions -> Bool
< :: Permissions -> Permissions -> Bool
$c< :: Permissions -> Permissions -> Bool
compare :: Permissions -> Permissions -> Ordering
$ccompare :: Permissions -> Permissions -> Ordering
Ord, ReadPrec [Permissions]
ReadPrec Permissions
Int -> ReadS Permissions
ReadS [Permissions]
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [Permissions]
$creadListPrec :: ReadPrec [Permissions]
readPrec :: ReadPrec Permissions
$creadPrec :: ReadPrec Permissions
readList :: ReadS [Permissions]
$creadList :: ReadS [Permissions]
readsPrec :: Int -> ReadS Permissions
$creadsPrec :: Int -> ReadS Permissions
Read, Int -> Permissions -> FilePath -> FilePath
[Permissions] -> FilePath -> FilePath
Permissions -> FilePath
forall a.
(Int -> a -> FilePath -> FilePath)
-> (a -> FilePath) -> ([a] -> FilePath -> FilePath) -> Show a
showList :: [Permissions] -> FilePath -> FilePath
$cshowList :: [Permissions] -> FilePath -> FilePath
show :: Permissions -> FilePath
$cshow :: Permissions -> FilePath
showsPrec :: Int -> Permissions -> FilePath -> FilePath
$cshowsPrec :: Int -> Permissions -> FilePath -> FilePath
Show)

-- | Truncate the destination file and then copy the contents of the source
-- file to the destination file.  If the destination file already exists, its
-- attributes shall remain unchanged.  Otherwise, its attributes are reset to
-- the defaults.
copyFileContents :: FilePath            -- ^ Source filename
                 -> FilePath            -- ^ Destination filename
                 -> IO ()
copyFileContents :: FilePath -> FilePath -> IO ()
copyFileContents FilePath
fromFPath FilePath
toFPath =
  (IOError -> FilePath -> IOError
`ioeAddLocation` FilePath
"copyFileContents") forall a. (IOError -> IOError) -> IO a -> IO a
`modifyIOError` do
    forall r. FilePath -> IOMode -> (Handle -> IO r) -> IO r
withBinaryFile FilePath
toFPath IOMode
WriteMode forall a b. (a -> b) -> a -> b
$ \ Handle
hTo ->
      FilePath -> Handle -> IO ()
copyFileToHandle FilePath
fromFPath Handle
hTo

-- | Copy all data from a file to a handle.
copyFileToHandle :: FilePath            -- ^ Source file
                 -> Handle              -- ^ Destination handle
                 -> IO ()
copyFileToHandle :: FilePath -> Handle -> IO ()
copyFileToHandle FilePath
fromFPath Handle
hTo =
  (IOError -> FilePath -> IOError
`ioeAddLocation` FilePath
"copyFileToHandle") forall a. (IOError -> IOError) -> IO a -> IO a
`modifyIOError` do
    forall r. FilePath -> IOMode -> (Handle -> IO r) -> IO r
withBinaryFile FilePath
fromFPath IOMode
ReadMode forall a b. (a -> b) -> a -> b
$ \ Handle
hFrom ->
      Handle -> Handle -> IO ()
copyHandleData Handle
hFrom Handle
hTo

-- | Copy data from one handle to another until end of file.
copyHandleData :: Handle                -- ^ Source handle
               -> Handle                -- ^ Destination handle
               -> IO ()
copyHandleData :: Handle -> Handle -> IO ()
copyHandleData Handle
hFrom Handle
hTo =
  (IOError -> FilePath -> IOError
`ioeAddLocation` FilePath
"copyData") forall a. (IOError -> IOError) -> IO a -> IO a
`modifyIOError` do
    forall a b. Int -> (Ptr a -> IO b) -> IO b
allocaBytes Int
bufferSize forall {a}. Ptr a -> IO ()
go
  where
    bufferSize :: Int
bufferSize = Int
131072 -- 128 KiB, as coreutils `cp` uses as of May 2014 (see ioblksize.h)
    go :: Ptr a -> IO ()
go Ptr a
buffer = do
      Int
count <- forall a. Handle -> Ptr a -> Int -> IO Int
hGetBuf Handle
hFrom Ptr a
buffer Int
bufferSize
      forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
count forall a. Ord a => a -> a -> Bool
> Int
0) forall a b. (a -> b) -> a -> b
$ do
        forall a. Handle -> Ptr a -> Int -> IO ()
hPutBuf Handle
hTo Ptr a
buffer Int
count
        Ptr a -> IO ()
go Ptr a
buffer

-- | Special directories for storing user-specific application data,
-- configuration, and cache files, as specified by the
-- <http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html XDG Base Directory Specification>.
--
-- Note: On Windows, 'XdgData' and 'XdgConfig' usually map to the same
-- directory.
--
-- @since 1.2.3.0
data XdgDirectory
  = XdgData
    -- ^ For data files (e.g. images).
    -- It uses the @XDG_DATA_HOME@ environment variable.
    -- On non-Windows systems, the default is @~\/.local\/share@.
    -- On Windows, the default is @%APPDATA%@
    -- (e.g. @C:\/Users\//\<user\>/\/AppData\/Roaming@).
    -- Can be considered as the user-specific equivalent of @\/usr\/share@.
  | XdgConfig
    -- ^ For configuration files.
    -- It uses the @XDG_CONFIG_HOME@ environment variable.
    -- On non-Windows systems, the default is @~\/.config@.
    -- On Windows, the default is @%APPDATA%@
    -- (e.g. @C:\/Users\//\<user\>/\/AppData\/Roaming@).
    -- Can be considered as the user-specific equivalent of @\/etc@.
  | XdgCache
    -- ^ For non-essential files (e.g. cache).
    -- It uses the @XDG_CACHE_HOME@ environment variable.
    -- On non-Windows systems, the default is @~\/.cache@.
    -- On Windows, the default is @%LOCALAPPDATA%@
    -- (e.g. @C:\/Users\//\<user\>/\/AppData\/Local@).
    -- Can be considered as the user-specific equivalent of @\/var\/cache@.
  deriving (XdgDirectory
forall a. a -> a -> Bounded a
maxBound :: XdgDirectory
$cmaxBound :: XdgDirectory
minBound :: XdgDirectory
$cminBound :: XdgDirectory
Bounded, Int -> XdgDirectory
XdgDirectory -> Int
XdgDirectory -> [XdgDirectory]
XdgDirectory -> XdgDirectory
XdgDirectory -> XdgDirectory -> [XdgDirectory]
XdgDirectory -> XdgDirectory -> XdgDirectory -> [XdgDirectory]
forall a.
(a -> a)
-> (a -> a)
-> (Int -> a)
-> (a -> Int)
-> (a -> [a])
-> (a -> a -> [a])
-> (a -> a -> [a])
-> (a -> a -> a -> [a])
-> Enum a
enumFromThenTo :: XdgDirectory -> XdgDirectory -> XdgDirectory -> [XdgDirectory]
$cenumFromThenTo :: XdgDirectory -> XdgDirectory -> XdgDirectory -> [XdgDirectory]
enumFromTo :: XdgDirectory -> XdgDirectory -> [XdgDirectory]
$cenumFromTo :: XdgDirectory -> XdgDirectory -> [XdgDirectory]
enumFromThen :: XdgDirectory -> XdgDirectory -> [XdgDirectory]
$cenumFromThen :: XdgDirectory -> XdgDirectory -> [XdgDirectory]
enumFrom :: XdgDirectory -> [XdgDirectory]
$cenumFrom :: XdgDirectory -> [XdgDirectory]
fromEnum :: XdgDirectory -> Int
$cfromEnum :: XdgDirectory -> Int
toEnum :: Int -> XdgDirectory
$ctoEnum :: Int -> XdgDirectory
pred :: XdgDirectory -> XdgDirectory
$cpred :: XdgDirectory -> XdgDirectory
succ :: XdgDirectory -> XdgDirectory
$csucc :: XdgDirectory -> XdgDirectory
Enum, XdgDirectory -> XdgDirectory -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: XdgDirectory -> XdgDirectory -> Bool
$c/= :: XdgDirectory -> XdgDirectory -> Bool
== :: XdgDirectory -> XdgDirectory -> Bool
$c== :: XdgDirectory -> XdgDirectory -> Bool
Eq, Eq XdgDirectory
XdgDirectory -> XdgDirectory -> Bool
XdgDirectory -> XdgDirectory -> Ordering
XdgDirectory -> XdgDirectory -> XdgDirectory
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: XdgDirectory -> XdgDirectory -> XdgDirectory
$cmin :: XdgDirectory -> XdgDirectory -> XdgDirectory
max :: XdgDirectory -> XdgDirectory -> XdgDirectory
$cmax :: XdgDirectory -> XdgDirectory -> XdgDirectory
>= :: XdgDirectory -> XdgDirectory -> Bool
$c>= :: XdgDirectory -> XdgDirectory -> Bool
> :: XdgDirectory -> XdgDirectory -> Bool
$c> :: XdgDirectory -> XdgDirectory -> Bool
<= :: XdgDirectory -> XdgDirectory -> Bool
$c<= :: XdgDirectory -> XdgDirectory -> Bool
< :: XdgDirectory -> XdgDirectory -> Bool
$c< :: XdgDirectory -> XdgDirectory -> Bool
compare :: XdgDirectory -> XdgDirectory -> Ordering
$ccompare :: XdgDirectory -> XdgDirectory -> Ordering
Ord, ReadPrec [XdgDirectory]
ReadPrec XdgDirectory
Int -> ReadS XdgDirectory
ReadS [XdgDirectory]
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [XdgDirectory]
$creadListPrec :: ReadPrec [XdgDirectory]
readPrec :: ReadPrec XdgDirectory
$creadPrec :: ReadPrec XdgDirectory
readList :: ReadS [XdgDirectory]
$creadList :: ReadS [XdgDirectory]
readsPrec :: Int -> ReadS XdgDirectory
$creadsPrec :: Int -> ReadS XdgDirectory
Read, Int -> XdgDirectory -> FilePath -> FilePath
[XdgDirectory] -> FilePath -> FilePath
XdgDirectory -> FilePath
forall a.
(Int -> a -> FilePath -> FilePath)
-> (a -> FilePath) -> ([a] -> FilePath -> FilePath) -> Show a
showList :: [XdgDirectory] -> FilePath -> FilePath
$cshowList :: [XdgDirectory] -> FilePath -> FilePath
show :: XdgDirectory -> FilePath
$cshow :: XdgDirectory -> FilePath
showsPrec :: Int -> XdgDirectory -> FilePath -> FilePath
$cshowsPrec :: Int -> XdgDirectory -> FilePath -> FilePath
Show)

-- | Search paths for various application data, as specified by the
-- <http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html XDG Base Directory Specification>.
--
-- The list of paths is split using 'System.FilePath.searchPathSeparator',
-- which on Windows is a semicolon.
--
-- Note: On Windows, 'XdgDataDirs' and 'XdgConfigDirs' usually yield the same
-- result.
--
-- @since 1.3.2.0
data XdgDirectoryList
  = XdgDataDirs
    -- ^ For data files (e.g. images).
    -- It uses the @XDG_DATA_DIRS@ environment variable.
    -- On non-Windows systems, the default is @\/usr\/local\/share\/@ and
    -- @\/usr\/share\/@.
    -- On Windows, the default is @%PROGRAMDATA%@ or @%ALLUSERSPROFILE%@
    -- (e.g. @C:\/ProgramData@).
  | XdgConfigDirs
    -- ^ For configuration files.
    -- It uses the @XDG_CONFIG_DIRS@ environment variable.
    -- On non-Windows systems, the default is @\/etc\/xdg@.
    -- On Windows, the default is @%PROGRAMDATA%@ or @%ALLUSERSPROFILE%@
    -- (e.g. @C:\/ProgramData@).
  deriving (XdgDirectoryList
forall a. a -> a -> Bounded a
maxBound :: XdgDirectoryList
$cmaxBound :: XdgDirectoryList
minBound :: XdgDirectoryList
$cminBound :: XdgDirectoryList
Bounded, Int -> XdgDirectoryList
XdgDirectoryList -> Int
XdgDirectoryList -> [XdgDirectoryList]
XdgDirectoryList -> XdgDirectoryList
XdgDirectoryList -> XdgDirectoryList -> [XdgDirectoryList]
XdgDirectoryList
-> XdgDirectoryList -> XdgDirectoryList -> [XdgDirectoryList]
forall a.
(a -> a)
-> (a -> a)
-> (Int -> a)
-> (a -> Int)
-> (a -> [a])
-> (a -> a -> [a])
-> (a -> a -> [a])
-> (a -> a -> a -> [a])
-> Enum a
enumFromThenTo :: XdgDirectoryList
-> XdgDirectoryList -> XdgDirectoryList -> [XdgDirectoryList]
$cenumFromThenTo :: XdgDirectoryList
-> XdgDirectoryList -> XdgDirectoryList -> [XdgDirectoryList]
enumFromTo :: XdgDirectoryList -> XdgDirectoryList -> [XdgDirectoryList]
$cenumFromTo :: XdgDirectoryList -> XdgDirectoryList -> [XdgDirectoryList]
enumFromThen :: XdgDirectoryList -> XdgDirectoryList -> [XdgDirectoryList]
$cenumFromThen :: XdgDirectoryList -> XdgDirectoryList -> [XdgDirectoryList]
enumFrom :: XdgDirectoryList -> [XdgDirectoryList]
$cenumFrom :: XdgDirectoryList -> [XdgDirectoryList]
fromEnum :: XdgDirectoryList -> Int
$cfromEnum :: XdgDirectoryList -> Int
toEnum :: Int -> XdgDirectoryList
$ctoEnum :: Int -> XdgDirectoryList
pred :: XdgDirectoryList -> XdgDirectoryList
$cpred :: XdgDirectoryList -> XdgDirectoryList
succ :: XdgDirectoryList -> XdgDirectoryList
$csucc :: XdgDirectoryList -> XdgDirectoryList
Enum, XdgDirectoryList -> XdgDirectoryList -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: XdgDirectoryList -> XdgDirectoryList -> Bool
$c/= :: XdgDirectoryList -> XdgDirectoryList -> Bool
== :: XdgDirectoryList -> XdgDirectoryList -> Bool
$c== :: XdgDirectoryList -> XdgDirectoryList -> Bool
Eq, Eq XdgDirectoryList
XdgDirectoryList -> XdgDirectoryList -> Bool
XdgDirectoryList -> XdgDirectoryList -> Ordering
XdgDirectoryList -> XdgDirectoryList -> XdgDirectoryList
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: XdgDirectoryList -> XdgDirectoryList -> XdgDirectoryList
$cmin :: XdgDirectoryList -> XdgDirectoryList -> XdgDirectoryList
max :: XdgDirectoryList -> XdgDirectoryList -> XdgDirectoryList
$cmax :: XdgDirectoryList -> XdgDirectoryList -> XdgDirectoryList
>= :: XdgDirectoryList -> XdgDirectoryList -> Bool
$c>= :: XdgDirectoryList -> XdgDirectoryList -> Bool
> :: XdgDirectoryList -> XdgDirectoryList -> Bool
$c> :: XdgDirectoryList -> XdgDirectoryList -> Bool
<= :: XdgDirectoryList -> XdgDirectoryList -> Bool
$c<= :: XdgDirectoryList -> XdgDirectoryList -> Bool
< :: XdgDirectoryList -> XdgDirectoryList -> Bool
$c< :: XdgDirectoryList -> XdgDirectoryList -> Bool
compare :: XdgDirectoryList -> XdgDirectoryList -> Ordering
$ccompare :: XdgDirectoryList -> XdgDirectoryList -> Ordering
Ord, ReadPrec [XdgDirectoryList]
ReadPrec XdgDirectoryList
Int -> ReadS XdgDirectoryList
ReadS [XdgDirectoryList]
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [XdgDirectoryList]
$creadListPrec :: ReadPrec [XdgDirectoryList]
readPrec :: ReadPrec XdgDirectoryList
$creadPrec :: ReadPrec XdgDirectoryList
readList :: ReadS [XdgDirectoryList]
$creadList :: ReadS [XdgDirectoryList]
readsPrec :: Int -> ReadS XdgDirectoryList
$creadsPrec :: Int -> ReadS XdgDirectoryList
Read, Int -> XdgDirectoryList -> FilePath -> FilePath
[XdgDirectoryList] -> FilePath -> FilePath
XdgDirectoryList -> FilePath
forall a.
(Int -> a -> FilePath -> FilePath)
-> (a -> FilePath) -> ([a] -> FilePath -> FilePath) -> Show a
showList :: [XdgDirectoryList] -> FilePath -> FilePath
$cshowList :: [XdgDirectoryList] -> FilePath -> FilePath
show :: XdgDirectoryList -> FilePath
$cshow :: XdgDirectoryList -> FilePath
showsPrec :: Int -> XdgDirectoryList -> FilePath -> FilePath
$cshowsPrec :: Int -> XdgDirectoryList -> FilePath -> FilePath
Show)