diff options
author | Paolo Capriotti <p.capriotti@gmail.com> | 2012-06-07 10:51:27 +0100 |
---|---|---|
committer | Paolo Capriotti <p.capriotti@gmail.com> | 2012-06-07 16:41:16 +0100 |
commit | 4546adb5d848fcb64c5647d6784e796ae157b1f5 (patch) | |
tree | 6bc38e4f6f95e64a857e88a4738e4c1dc8e873be /libraries/base/System | |
parent | 3bcff2926f1b02f5d28dca6997d209bc3e0774a7 (diff) | |
download | haskell-4546adb5d848fcb64c5647d6784e796ae157b1f5.tar.gz |
Refactor findTempName: factor out file creation.
Add openNewFile function, which creates a new file and returns a file
descriptor for it.
Diffstat (limited to 'libraries/base/System')
-rw-r--r-- | libraries/base/System/IO.hs | 64 |
1 files changed, 39 insertions, 25 deletions
diff --git a/libraries/base/System/IO.hs b/libraries/base/System/IO.hs index 1eb9271167..860d2b6e5b 100644 --- a/libraries/base/System/IO.hs +++ b/libraries/base/System/IO.hs @@ -563,13 +563,6 @@ openTempFile' loc tmp_dir template binary mode = do _ -> error "bug in System.IO.openTempFile" #ifndef __NHC__ - oflags1 = rw_flags .|. o_EXCL - - binary_flags - | binary = o_BINARY - | otherwise = 0 - - oflags = oflags1 .|. binary_flags #endif #if defined(__NHC__) @@ -577,24 +570,19 @@ openTempFile' loc tmp_dir template binary mode = do return (filepath, h) #elif defined(__GLASGOW_HASKELL__) findTempName x = do - fd <- withFilePath filepath $ \ f -> - c_open f oflags mode - if fd < 0 - then do - errno <- getErrno - if errno == eEXIST - then findTempName (x+1) - else ioError (errnoToIOError loc errno Nothing (Just tmp_dir)) - else do - - (fD,fd_type) <- FD.mkFD fd ReadWriteMode Nothing{-no stat-} - False{-is_socket-} - True{-is_nonblock-} - - enc <- getLocaleEncoding - h <- mkHandleFromFD fD fd_type filepath ReadWriteMode False{-set non-block-} (Just enc) - - return (filepath, h) + r <- openNewFile filepath binary mode + case r of + FileExists -> findTempName (x + 1) + OpenNewError errno -> ioError (errnoToIOError loc errno Nothing (Just tmp_dir)) + NewFileCreated fd -> do + (fD,fd_type) <- FD.mkFD fd ReadWriteMode Nothing{-no stat-} + False{-is_socket-} + True{-is_nonblock-} + + enc <- getLocaleEncoding + h <- mkHandleFromFD fD fd_type filepath ReadWriteMode False{-set non-block-} (Just enc) + + return (filepath, h) #else h <- fdToHandle fd `onException` c_close fd return (filepath, h) @@ -615,6 +603,32 @@ openTempFile' loc tmp_dir template binary mode = do fdToHandle fd = openFd (fromIntegral fd) False ReadWriteMode binary #endif +#if defined(__GLASGOW_HASKELL__) +data OpenNewFileResult + = NewFileCreated CInt + | FileExists + | OpenNewError Errno + +openNewFile :: FilePath -> Bool -> CMode -> IO OpenNewFileResult +openNewFile filepath binary mode = do + let oflags1 = rw_flags .|. o_EXCL + + binary_flags + | binary = o_BINARY + | otherwise = 0 + + oflags = oflags1 .|. binary_flags + fd <- withFilePath filepath $ \ f -> + c_open f oflags mode + if fd < 0 + then do + errno <- getErrno + if errno == eEXIST + then return FileExists + else return (OpenNewError errno) + else return (NewFileCreated fd) +#endif + -- XXX Should use filepath library pathSeparator :: Char #ifdef mingw32_HOST_OS |