Skip to content

Commit

Permalink
Consistently use throwErrnoPathIf*
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
NicolasT authored and Bodigrim committed Sep 11, 2022
1 parent 0c2a4f3 commit b085646
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 28 deletions.
11 changes: 4 additions & 7 deletions System/Posix/Directory.hsc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
11 changes: 4 additions & 7 deletions System/Posix/Directory/ByteString.hsc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
18 changes: 4 additions & 14 deletions System/Posix/Directory/PosixPath.hsc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

0 comments on commit b085646

Please sign in to comment.