module System.Unix.Files where

import Control.Exception (catch)
import Prelude hiding (catch)
import System.Posix.Files (createSymbolicLink, removeLink)
import System.IO.Error (isAlreadyExistsError)

-- |calls 'createSymbolicLink' but will remove the target and retry if
-- 'createSymbolicLink' raises EEXIST.
forceSymbolicLink :: FilePath -> FilePath -> IO ()
forceSymbolicLink :: FilePath -> FilePath -> IO ()
forceSymbolicLink FilePath
target FilePath
linkName =
    FilePath -> FilePath -> IO ()
createSymbolicLink FilePath
target FilePath
linkName IO () -> (IOError -> IO ()) -> IO ()
forall e a. Exception e => IO a -> (e -> IO a) -> IO a
`catch`
      (\IOError
e -> if IOError -> Bool
isAlreadyExistsError IOError
e
             then do FilePath -> IO ()
removeLink FilePath
linkName
                     FilePath -> FilePath -> IO ()
createSymbolicLink FilePath
target FilePath
linkName
             else IOError -> IO ()
forall a. IOError -> IO a
ioError IOError
e)