From 24c36ef856f9f8f4e3e09b11abf6dac081b630bf Mon Sep 17 00:00:00 2001 From: Julian Ospald Date: Sun, 1 Jan 2023 23:04:43 +0800 Subject: [PATCH] Fix failure with --isolate=dir --force Fixes #695 --- lib/GHCup/Prelude/File/Posix.hs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/lib/GHCup/Prelude/File/Posix.hs b/lib/GHCup/Prelude/File/Posix.hs index 1b774ac..a4ea316 100644 --- a/lib/GHCup/Prelude/File/Posix.hs +++ b/lib/GHCup/Prelude/File/Posix.hs @@ -48,6 +48,7 @@ import Streamly.Internal.Data.Unfold.Type import qualified Streamly.Internal.Data.Unfold as U import Streamly.Internal.Control.Concurrent ( withRunInIO ) import Streamly.Internal.Data.IOFinalizer ( newIOFinalizer, runIOFinalizer ) +import GHC.IO.Exception (IOException(ioe_type), IOErrorType (..)) -- | On unix, we can use symlinks, so we just get the @@ -116,8 +117,18 @@ copyFile from to fail' = do let dflags = [ FD.oNofollow , if fail' then FD.oExcl else FD.oTrunc ] + let openFdHandle' = openFdHandle to SPI.WriteOnly dflags $ Just sourceFileMode bracket - (openFdHandle to SPI.WriteOnly dflags $ Just sourceFileMode) + (handleIO (\e -> if + -- if we copy from regular file to symlink, we need + -- to delete the symlink + | ioe_type e == InvalidArgument + , not fail' -> do + removeLink to + openFdHandle' + | otherwise -> throwIO e + ) + openFdHandle') (hClose . snd) $ \(_, tH) -> do hSetBinaryMode fH True