From 3b316f659e5af787404857c7fde4047cf3b65b54 Mon Sep 17 00:00:00 2001 From: Nicolas Trangez Date: Sat, 10 Sep 2022 12:20:21 +0200 Subject: [PATCH] Consistently use `throwErrnoPathIf*` In some functions, a combination of `throwErrnoIf*` and `modifyIOError`/`ioeSetFileName` was used, even though `throwErrnoPathIf*` helpers are available. This patch cleans up such code, unifying the use of the `throwErrnoPathIf*` helpers. --- System/Posix/Directory.hsc | 11 ++++------- System/Posix/Directory/ByteString.hsc | 11 ++++------- System/Posix/Directory/PosixPath.hsc | 18 ++++-------------- 3 files changed, 12 insertions(+), 28 deletions(-) diff --git a/System/Posix/Directory.hsc b/System/Posix/Directory.hsc index 24f2628b..0f856c45 100644 --- a/System/Posix/Directory.hsc +++ b/System/Posix/Directory.hsc @@ -49,7 +49,6 @@ module System.Posix.Directory ( ) where import Data.Maybe -import System.IO.Error import System.Posix.Error import System.Posix.Types import Foreign @@ -157,18 +156,16 @@ foreign import ccall unsafe "getcwd" -- the current working directory to @dir@. changeWorkingDirectory :: FilePath -> IO () changeWorkingDirectory path = - modifyIOError (`ioeSetFileName` path) $ - withFilePath path $ \s -> - throwErrnoIfMinus1Retry_ "changeWorkingDirectory" (c_chdir s) + withFilePath path $ \s -> + throwErrnoPathIfMinus1Retry_ "changeWorkingDirectory" path (c_chdir s) foreign import ccall unsafe "chdir" c_chdir :: CString -> IO CInt removeDirectory :: FilePath -> IO () removeDirectory path = - modifyIOError (`ioeSetFileName` path) $ - withFilePath path $ \s -> - throwErrnoIfMinus1Retry_ "removeDirectory" (c_rmdir s) + withFilePath path $ \s -> + throwErrnoPathIfMinus1Retry_ "removeDirectory" path (c_rmdir s) foreign import ccall unsafe "rmdir" c_rmdir :: CString -> IO CInt diff --git a/System/Posix/Directory/ByteString.hsc b/System/Posix/Directory/ByteString.hsc index ba3f3a3e..3d6bbea5 100644 --- a/System/Posix/Directory/ByteString.hsc +++ b/System/Posix/Directory/ByteString.hsc @@ -49,7 +49,6 @@ module System.Posix.Directory.ByteString ( ) where import Data.Maybe -import System.IO.Error import System.Posix.Types import Foreign import Foreign.C @@ -158,18 +157,16 @@ foreign import ccall unsafe "getcwd" -- the current working directory to @dir@. changeWorkingDirectory :: RawFilePath -> IO () changeWorkingDirectory path = - modifyIOError (`ioeSetFileName` (BC.unpack path)) $ - withFilePath path $ \s -> - throwErrnoIfMinus1Retry_ "changeWorkingDirectory" (c_chdir s) + withFilePath path $ \s -> + throwErrnoPathIfMinus1Retry_ "changeWorkingDirectory" path (c_chdir s) foreign import ccall unsafe "chdir" c_chdir :: CString -> IO CInt removeDirectory :: RawFilePath -> IO () removeDirectory path = - modifyIOError (`ioeSetFileName` BC.unpack path) $ - withFilePath path $ \s -> - throwErrnoIfMinus1Retry_ "removeDirectory" (c_rmdir s) + withFilePath path $ \s -> + throwErrnoPathIfMinus1Retry_ "removeDirectory" path (c_rmdir s) foreign import ccall unsafe "rmdir" c_rmdir :: CString -> IO CInt diff --git a/System/Posix/Directory/PosixPath.hsc b/System/Posix/Directory/PosixPath.hsc index 5da1bee7..3d3ad612 100644 --- a/System/Posix/Directory/PosixPath.hsc +++ b/System/Posix/Directory/PosixPath.hsc @@ -46,15 +46,11 @@ module System.Posix.Directory.PosixPath ( changeWorkingDirectoryFd, ) where -import System.IO.Error import System.Posix.Types import Foreign import Foreign.C import System.OsPath.Types -import GHC.IO.Encoding.UTF8 ( mkUTF8 ) -import GHC.IO.Encoding.Failure ( CodingFailureMode(..) ) -import System.OsPath.Posix import System.Posix.Directory hiding (createDirectory, openDirStream, readDirStream, getWorkingDirectory, changeWorkingDirectory, removeDirectory) import qualified System.Posix.Directory.Common as Common import System.Posix.PosixPath.FilePath @@ -145,22 +141,16 @@ foreign import ccall unsafe "getcwd" -- the current working directory to @dir@. changeWorkingDirectory :: PosixPath -> IO () changeWorkingDirectory path = - modifyIOError (`ioeSetFileName` (_toStr path)) $ - withFilePath path $ \s -> - throwErrnoIfMinus1Retry_ "changeWorkingDirectory" (c_chdir s) + withFilePath path $ \s -> + throwErrnoPathIfMinus1Retry_ "changeWorkingDirectory" path (c_chdir s) foreign import ccall unsafe "chdir" c_chdir :: CString -> IO CInt removeDirectory :: PosixPath -> IO () removeDirectory path = - modifyIOError (`ioeSetFileName` _toStr path) $ - withFilePath path $ \s -> - throwErrnoIfMinus1Retry_ "removeDirectory" (c_rmdir s) + withFilePath path $ \s -> + throwErrnoPathIfMinus1Retry_ "removeDirectory" path (c_rmdir s) foreign import ccall unsafe "rmdir" c_rmdir :: CString -> IO CInt - -_toStr :: PosixPath -> String -_toStr fp = either (error . show) id $ decodeWith (mkUTF8 TransliterateCodingFailure) fp -