Skip to content

Commit b085646

Browse files
NicolasTBodigrim
authored andcommitted
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.
1 parent 0c2a4f3 commit b085646

File tree

3 files changed

+12
-28
lines changed

3 files changed

+12
-28
lines changed

System/Posix/Directory.hsc

+4-7
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ module System.Posix.Directory (
4949
) where
5050

5151
import Data.Maybe
52-
import System.IO.Error
5352
import System.Posix.Error
5453
import System.Posix.Types
5554
import Foreign
@@ -157,18 +156,16 @@ foreign import ccall unsafe "getcwd"
157156
-- the current working directory to @dir@.
158157
changeWorkingDirectory :: FilePath -> IO ()
159158
changeWorkingDirectory path =
160-
modifyIOError (`ioeSetFileName` path) $
161-
withFilePath path $ \s ->
162-
throwErrnoIfMinus1Retry_ "changeWorkingDirectory" (c_chdir s)
159+
withFilePath path $ \s ->
160+
throwErrnoPathIfMinus1Retry_ "changeWorkingDirectory" path (c_chdir s)
163161

164162
foreign import ccall unsafe "chdir"
165163
c_chdir :: CString -> IO CInt
166164

167165
removeDirectory :: FilePath -> IO ()
168166
removeDirectory path =
169-
modifyIOError (`ioeSetFileName` path) $
170-
withFilePath path $ \s ->
171-
throwErrnoIfMinus1Retry_ "removeDirectory" (c_rmdir s)
167+
withFilePath path $ \s ->
168+
throwErrnoPathIfMinus1Retry_ "removeDirectory" path (c_rmdir s)
172169

173170
foreign import ccall unsafe "rmdir"
174171
c_rmdir :: CString -> IO CInt

System/Posix/Directory/ByteString.hsc

+4-7
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ module System.Posix.Directory.ByteString (
4949
) where
5050

5151
import Data.Maybe
52-
import System.IO.Error
5352
import System.Posix.Types
5453
import Foreign
5554
import Foreign.C
@@ -158,18 +157,16 @@ foreign import ccall unsafe "getcwd"
158157
-- the current working directory to @dir@.
159158
changeWorkingDirectory :: RawFilePath -> IO ()
160159
changeWorkingDirectory path =
161-
modifyIOError (`ioeSetFileName` (BC.unpack path)) $
162-
withFilePath path $ \s ->
163-
throwErrnoIfMinus1Retry_ "changeWorkingDirectory" (c_chdir s)
160+
withFilePath path $ \s ->
161+
throwErrnoPathIfMinus1Retry_ "changeWorkingDirectory" path (c_chdir s)
164162

165163
foreign import ccall unsafe "chdir"
166164
c_chdir :: CString -> IO CInt
167165

168166
removeDirectory :: RawFilePath -> IO ()
169167
removeDirectory path =
170-
modifyIOError (`ioeSetFileName` BC.unpack path) $
171-
withFilePath path $ \s ->
172-
throwErrnoIfMinus1Retry_ "removeDirectory" (c_rmdir s)
168+
withFilePath path $ \s ->
169+
throwErrnoPathIfMinus1Retry_ "removeDirectory" path (c_rmdir s)
173170

174171
foreign import ccall unsafe "rmdir"
175172
c_rmdir :: CString -> IO CInt

System/Posix/Directory/PosixPath.hsc

+4-14
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,11 @@ module System.Posix.Directory.PosixPath (
4646
changeWorkingDirectoryFd,
4747
) where
4848

49-
import System.IO.Error
5049
import System.Posix.Types
5150
import Foreign
5251
import Foreign.C
5352

5453
import System.OsPath.Types
55-
import GHC.IO.Encoding.UTF8 ( mkUTF8 )
56-
import GHC.IO.Encoding.Failure ( CodingFailureMode(..) )
57-
import System.OsPath.Posix
5854
import System.Posix.Directory hiding (createDirectory, openDirStream, readDirStream, getWorkingDirectory, changeWorkingDirectory, removeDirectory)
5955
import qualified System.Posix.Directory.Common as Common
6056
import System.Posix.PosixPath.FilePath
@@ -145,22 +141,16 @@ foreign import ccall unsafe "getcwd"
145141
-- the current working directory to @dir@.
146142
changeWorkingDirectory :: PosixPath -> IO ()
147143
changeWorkingDirectory path =
148-
modifyIOError (`ioeSetFileName` (_toStr path)) $
149-
withFilePath path $ \s ->
150-
throwErrnoIfMinus1Retry_ "changeWorkingDirectory" (c_chdir s)
144+
withFilePath path $ \s ->
145+
throwErrnoPathIfMinus1Retry_ "changeWorkingDirectory" path (c_chdir s)
151146

152147
foreign import ccall unsafe "chdir"
153148
c_chdir :: CString -> IO CInt
154149

155150
removeDirectory :: PosixPath -> IO ()
156151
removeDirectory path =
157-
modifyIOError (`ioeSetFileName` _toStr path) $
158-
withFilePath path $ \s ->
159-
throwErrnoIfMinus1Retry_ "removeDirectory" (c_rmdir s)
152+
withFilePath path $ \s ->
153+
throwErrnoPathIfMinus1Retry_ "removeDirectory" path (c_rmdir s)
160154

161155
foreign import ccall unsafe "rmdir"
162156
c_rmdir :: CString -> IO CInt
163-
164-
_toStr :: PosixPath -> String
165-
_toStr fp = either (error . show) id $ decodeWith (mkUTF8 TransliterateCodingFailure) fp
166-

0 commit comments

Comments
 (0)