X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=GHC%2FHandle.hs;h=52bf8cde99e4de0877b07139144ee74ea924c067;hb=b674b3cdb447d2265dccc4e3c343c9625f464a33;hp=3cc214f91d588eb08ba2cad53e02710a9b5b4e09;hpb=c79996421b5b2e1bf4f5e59f675f5b45fd2352dd;p=haskell-directory.git diff --git a/GHC/Handle.hs b/GHC/Handle.hs index 3cc214f..52bf8cd 100644 --- a/GHC/Handle.hs +++ b/GHC/Handle.hs @@ -1,4 +1,4 @@ -{-# OPTIONS -fno-implicit-prelude -#include "HsBase.h" #-} +{-# OPTIONS_GHC -fno-implicit-prelude -#include "HsBase.h" #-} #undef DEBUG_DUMP #undef DEBUG @@ -17,6 +17,7 @@ -- ----------------------------------------------------------------------------- +-- #hide module GHC.Handle ( withHandle, withHandle', withHandle_, wantWritableHandle, wantReadableHandle, wantSeekableHandle, @@ -26,13 +27,16 @@ module GHC.Handle ( fillReadBuffer, fillReadBufferWithoutBlocking, readRawBuffer, readRawBufferPtr, writeRawBuffer, writeRawBufferPtr, + +#ifndef mingw32_HOST_OS unlockFile, - +#endif + ioe_closedHandle, ioe_EOF, ioe_notReadable, ioe_notWritable, stdin, stdout, stderr, - IOMode(..), openFile, openBinaryFile, openFd, fdToHandle, - hFileSize, hIsEOF, isEOF, hLookAhead, hSetBuffering, hSetBinaryMode, + IOMode(..), openFile, openBinaryFile, openTempFile, openBinaryTempFile, openFd, fdToHandle, + hFileSize, hSetFileSize, hIsEOF, isEOF, hLookAhead, hSetBuffering, hSetBinaryMode, hFlush, hDuplicate, hDuplicateTo, hClose, hClose_help, @@ -51,8 +55,7 @@ module GHC.Handle ( ) where -#include "ghcconfig.h" - +import System.Directory.Internals import Control.Monad import Data.Bits import Data.Maybe @@ -73,6 +76,9 @@ import GHC.Enum import GHC.Num ( Integer(..), Num(..) ) import GHC.Show import GHC.Real ( toInteger ) +#if defined(DEBUG_DUMP) +import GHC.Pack +#endif import GHC.Conc @@ -370,7 +376,7 @@ newEmptyBuffer b state size allocateBuffer :: Int -> BufferState -> IO Buffer allocateBuffer sz@(I# size) state = IO $ \s -> -#ifdef mingw32_TARGET_OS +#ifdef mingw32_HOST_OS -- To implement asynchronous I/O under Win32, we have to pass -- buffer references to external threads that handles the -- filling/emptying of their contents. Hence, the buffer cannot @@ -509,7 +515,7 @@ fillReadBufferWithoutBlocking fd is_stream -- buffer better be empty: assert (r == 0 && w == 0) $ do #ifdef DEBUG_DUMP - puts ("fillReadBufferLoopNoBlock: bytes = " ++ show bytes ++ "\n") + puts ("fillReadBufferLoopNoBlock: bytes = " ++ show size ++ "\n") #endif res <- readRawBufferNoBlock "fillReadBuffer" fd is_stream b 0 (fromIntegral size) @@ -521,7 +527,7 @@ fillReadBufferWithoutBlocking fd is_stream -- Low level routines for reading/writing to (raw)buffers: -#ifndef mingw32_TARGET_OS +#ifndef mingw32_HOST_OS readRawBuffer :: String -> FD -> Bool -> RawBuffer -> Int -> CInt -> IO CInt readRawBuffer loc fd is_stream buf off len = throwErrnoIfMinus1RetryMayBlock loc @@ -564,7 +570,7 @@ foreign import ccall unsafe "__hscore_PrelHandle_write" foreign import ccall unsafe "__hscore_PrelHandle_write" write_off :: CInt -> Ptr CChar -> Int -> CInt -> IO CInt -#else /* mingw32_TARGET_OS.... */ +#else /* mingw32_HOST_OS.... */ readRawBuffer :: String -> FD -> Bool -> RawBuffer -> Int -> CInt -> IO CInt readRawBuffer loc fd is_stream buf off len @@ -782,14 +788,15 @@ openFile' filepath mode binary = let oflags1 = case mode of - ReadMode -> read_flags - WriteMode -> write_flags - ReadWriteMode -> rw_flags + ReadMode -> read_flags +#ifdef mingw32_HOST_OS + WriteMode -> write_flags .|. o_TRUNC +#else + WriteMode -> write_flags +#endif + ReadWriteMode -> rw_flags AppendMode -> append_flags - truncate | WriteMode <- mode = True - | otherwise = False - binary_flags | binary = o_BINARY | otherwise = 0 @@ -806,14 +813,73 @@ openFile' filepath mode binary = throwErrnoIfMinus1Retry "openFile" (c_open f (fromIntegral oflags) 0o666) - openFd fd Nothing False filepath mode binary truncate - `catchException` \e -> do c_close (fromIntegral fd); throw e + fd_type <- fdType fd + + h <- openFd fd (Just fd_type) False filepath mode binary + `catchException` \e -> do c_close (fromIntegral fd); throw e -- NB. don't forget to close the FD if openFd fails, otherwise -- this FD leaks. -- ASSERT: if we just created the file, then openFd won't fail -- (so we don't need to worry about removing the newly created file -- in the event of an error). +#ifndef mingw32_HOST_OS + -- we want to truncate() if this is an open in WriteMode, but only + -- if the target is a RegularFile. ftruncate() fails on special files + -- like /dev/null. + if mode == WriteMode && fd_type == RegularFile + then throwErrnoIf (/=0) "openFile" + (c_ftruncate (fromIntegral fd) 0) + else return 0 +#endif + return h + + +-- | The function creates a temporary file in ReadWrite mode. +-- The created file isn\'t deleted automatically, so you need to delete it manually. +openTempFile :: FilePath -- ^ Directory in which to create the file + -> String -- ^ File name template. If the template is \"foo.ext\" then + -- the create file will be \"fooXXX.ext\" where XXX is some + -- random number. + -> IO (FilePath, Handle) +openTempFile tmp_dir template = openTempFile' "openTempFile" tmp_dir template dEFAULT_OPEN_IN_BINARY_MODE + +-- | Like 'openTempFile', but opens the file in binary mode. See 'openBinaryFile' for more comments. +openBinaryTempFile :: FilePath -> String -> IO (FilePath, Handle) +openBinaryTempFile tmp_dir template = openTempFile' "openBinaryTempFile" tmp_dir template True + +openTempFile' :: String -> FilePath -> String -> Bool -> IO (FilePath, Handle) +openTempFile' loc tmp_dir template binary = do + pid <- c_getpid + findTempName pid + where + (prefix,suffix) = break (=='.') template + + oflags1 = rw_flags .|. o_EXCL + + binary_flags + | binary = o_BINARY + | otherwise = 0 + + oflags = oflags1 .|. binary_flags + + findTempName x = do + fd <- withCString filepath $ \ f -> + c_open f oflags 0o666 + 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 + h <- openFd (fromIntegral fd) Nothing False filepath ReadWriteMode True + `catchException` \e -> do c_close (fromIntegral fd); throw e + return (filepath, h) + where + filename = prefix ++ show x ++ suffix + filepath = tmp_dir `joinFileName` filename + std_flags = o_NONBLOCK .|. o_NOCTTY output_flags = std_flags .|. o_CREAT @@ -825,8 +891,8 @@ append_flags = write_flags .|. o_APPEND -- --------------------------------------------------------------------------- -- openFd -openFd :: FD -> Maybe FDType -> Bool -> FilePath -> IOMode -> Bool -> Bool -> IO Handle -openFd fd mb_fd_type is_socket filepath mode binary truncate = do +openFd :: FD -> Maybe FDType -> Bool -> FilePath -> IOMode -> Bool -> IO Handle +openFd fd mb_fd_type is_socket filepath mode binary = do -- turn on non-blocking mode setNonBlockingFD fd @@ -849,34 +915,45 @@ openFd fd mb_fd_type is_socket filepath mode binary truncate = do ioException (IOError Nothing InappropriateType "openFile" "is a directory" Nothing) - Stream - | ReadWriteHandle <- ha_type -> mkDuplexHandle fd is_socket filepath binary - | otherwise -> mkFileHandle fd is_socket filepath ha_type binary - -- regular files need to be locked RegularFile -> do +#ifndef mingw32_HOST_OS r <- lockFile (fromIntegral fd) (fromBool write) 1{-exclusive-} when (r == -1) $ ioException (IOError Nothing ResourceBusy "openFile" "file is locked" Nothing) +#endif + mkFileHandle fd is_socket filepath ha_type binary - -- truncate the file if necessary - when truncate (fileTruncate filepath) + Stream + -- only *Streams* can be DuplexHandles. Other read/write + -- Handles must share a buffer. + | ReadWriteHandle <- ha_type -> + mkDuplexHandle fd is_socket filepath binary + | otherwise -> + mkFileHandle fd is_socket filepath ha_type binary - mkFileHandle fd is_socket filepath ha_type binary + RawDevice -> + mkFileHandle fd is_socket filepath ha_type binary + _ -> + ioException (IOError Nothing UnsupportedOperation "openFd" + "unknown file type" Nothing) fdToHandle :: FD -> IO Handle fdToHandle fd = do mode <- fdGetMode fd let fd_str = "" - openFd fd Nothing False{-XXX!-} fd_str mode True{-bin mode-} False{-no truncate-} + openFd fd Nothing False{-XXX!-} fd_str mode True{-bin mode-} + +#ifndef mingw32_HOST_OS foreign import ccall unsafe "lockFile" lockFile :: CInt -> CInt -> CInt -> IO CInt foreign import ccall unsafe "unlockFile" unlockFile :: CInt -> IO CInt +#endif mkStdHandle :: FD -> FilePath -> HandleType -> IORef Buffer -> BufferMode -> IO Handle @@ -980,13 +1057,11 @@ hClose_handle_ handle_ = do c_fd = fromIntegral fd -- close the file descriptor, but not when this is the read - -- side of a duplex handle, and not when this is one of the - -- std file handles. + -- side of a duplex handle. case haOtherSide handle_ of Nothing -> - when (fd /= fd_stdin && fd /= fd_stdout && fd /= fd_stderr) $ throwErrnoIfMinus1Retry_ "hClose" -#ifdef mingw32_TARGET_OS +#ifdef mingw32_HOST_OS (closeFd (haIsStream handle_) c_fd) #else (c_close c_fd) @@ -996,9 +1071,11 @@ hClose_handle_ handle_ = do -- free the spare buffers writeIORef (haBuffers handle_) BufferListNil +#ifndef mingw32_HOST_OS -- unlock it unlockFile c_fd - +#endif + -- we must set the fd to -1, because the finalizer is going -- to run eventually and try to close/unlock it. return (handle_{ haFD = -1, @@ -1006,7 +1083,7 @@ hClose_handle_ handle_ = do }) ----------------------------------------------------------------------------- --- Detecting the size of a file +-- Detecting and changing the size of a file -- | For a handle @hdl@ which attached to a physical file, -- 'hFileSize' @hdl@ returns the size of that file in 8-bit bytes. @@ -1024,6 +1101,20 @@ hFileSize handle = else ioException (IOError Nothing InappropriateType "hFileSize" "not a regular file" Nothing) + +-- | 'hSetFileSize' @hdl@ @size@ truncates the physical file with handle @hdl@ to @size@ bytes. + +hSetFileSize :: Handle -> Integer -> IO () +hSetFileSize handle size = + withHandle_ "hSetFileSize" handle $ \ handle_ -> do + case haType handle_ of + ClosedHandle -> ioe_closedHandle + SemiClosedHandle -> ioe_closedHandle + _ -> do flushWriteBufferOnly handle_ + throwErrnoIf (/=0) "hSetFileSize" + (c_ftruncate (fromIntegral (haFD handle_)) (fromIntegral size)) + return () + -- --------------------------------------------------------------------------- -- Detecting the End of Input @@ -1065,7 +1156,7 @@ hLookAhead handle = do -- fill up the read buffer if necessary new_buf <- if bufferEmpty buf - then fillReadBuffer fd is_line (haIsStream handle_) buf + then fillReadBuffer fd True (haIsStream handle_) buf else return buf writeIORef ref new_buf @@ -1132,10 +1223,12 @@ hSetBuffering handle mode = is_tty <- fdIsTTY (haFD handle_) when (is_tty && isReadableHandleType (haType handle_)) $ case mode of -#ifndef mingw32_TARGET_OS +#ifndef mingw32_HOST_OS -- 'raw' mode under win32 is a bit too specialised (and troublesome -- for most common uses), so simply disable its use here. NoBuffering -> setCooked (haFD handle_) False +#else + NoBuffering -> return () #endif _ -> setCooked (haFD handle_) True @@ -1285,7 +1378,7 @@ hTell :: Handle -> IO Integer hTell handle = wantSeekableHandle "hGetPosn" handle $ \ handle_ -> do -#if defined(mingw32_TARGET_OS) +#if defined(mingw32_HOST_OS) -- urgh, on Windows we have to worry about \n -> \r\n translation, -- so we can't easily calculate the file position using the -- current buffer size. Just flush instead. @@ -1379,9 +1472,8 @@ hIsSeekable handle = SemiClosedHandle -> ioe_closedHandle AppendHandle -> return False _ -> do t <- fdType (haFD handle_) - return (t == RegularFile - && (haIsBin handle_ - || tEXT_MODE_SEEK_ALLOWED)) + return ((t == RegularFile || t == RawDevice) + && (haIsBin handle_ || tEXT_MODE_SEEK_ALLOWED)) -- ----------------------------------------------------------------------------- -- Changing echo status (Non-standard GHC extensions) @@ -1440,26 +1532,37 @@ foreign import ccall unsafe "__hscore_setmode" -- ----------------------------------------------------------------------------- -- Duplicating a Handle --- |Returns a duplicate of the original handle, with its own buffer --- and file pointer. The original handle's buffer is flushed, including --- discarding any input data, before the handle is duplicated. +-- | Returns a duplicate of the original handle, with its own buffer. +-- The two Handles will share a file pointer, however. The original +-- handle's buffer is flushed, including discarding any input data, +-- before the handle is duplicated. hDuplicate :: Handle -> IO Handle hDuplicate h@(FileHandle path m) = do - new_h_ <- withHandle' "hDuplicate" h m (dupHandle_ Nothing) - new_m <- newMVar new_h_ - return (FileHandle path new_m) + new_h_ <- withHandle' "hDuplicate" h m (dupHandle Nothing) + newFileHandle path (handleFinalizer path) new_h_ hDuplicate h@(DuplexHandle path r w) = do - new_w_ <- withHandle' "hDuplicate" h w (dupHandle_ Nothing) + new_w_ <- withHandle' "hDuplicate" h w (dupHandle Nothing) new_w <- newMVar new_w_ - new_r_ <- withHandle' "hDuplicate" h r (dupHandle_ (Just new_w)) + new_r_ <- withHandle' "hDuplicate" h r (dupHandle (Just new_w)) new_r <- newMVar new_r_ + addMVarFinalizer new_w (handleFinalizer path new_w) return (DuplexHandle path new_r new_w) -dupHandle_ other_side h_ = do +dupHandle other_side h_ = do -- flush the buffer first, so we don't have to copy its contents flushBuffer h_ - new_fd <- c_dup (fromIntegral (haFD h_)) + new_fd <- throwErrnoIfMinus1 "dupHandle" $ + c_dup (fromIntegral (haFD h_)) + dupHandle_ other_side h_ new_fd + +dupHandleTo other_side hto_ h_ = do + flushBuffer h_ + new_fd <- throwErrnoIfMinus1 "dupHandleTo" $ + c_dup2 (fromIntegral (haFD h_)) (fromIntegral (haFD hto_)) + dupHandle_ other_side h_ new_fd + +dupHandle_ other_side h_ new_fd = do buffer <- allocateBuffer dEFAULT_BUFFER_SIZE (initBufferState (haType h_)) ioref <- newIORef buffer ioref_buffers <- newIORef BufferListNil @@ -1487,14 +1590,14 @@ hDuplicateTo :: Handle -> Handle -> IO () hDuplicateTo h1@(FileHandle _ m1) h2@(FileHandle _ m2) = do withHandle__' "hDuplicateTo" h2 m2 $ \h2_ -> do _ <- hClose_help h2_ - withHandle' "hDuplicateTo" h1 m1 (dupHandle_ Nothing) + withHandle' "hDuplicateTo" h1 m1 (dupHandleTo Nothing h2_) hDuplicateTo h1@(DuplexHandle _ r1 w1) h2@(DuplexHandle _ r2 w2) = do withHandle__' "hDuplicateTo" h2 w2 $ \w2_ -> do _ <- hClose_help w2_ - withHandle' "hDuplicateTo" h1 r1 (dupHandle_ Nothing) + withHandle' "hDuplicateTo" h1 r1 (dupHandleTo Nothing w2_) withHandle__' "hDuplicateTo" h2 r2 $ \r2_ -> do _ <- hClose_help r2_ - withHandle' "hDuplicateTo" h1 r1 (dupHandle_ (Just w1)) + withHandle' "hDuplicateTo" h1 r1 (dupHandleTo (Just w1) r2_) hDuplicateTo h1 _ = ioException (IOError (Just h1) IllegalOperation "hDuplicateTo" "handles are incompatible" Nothing) @@ -1545,10 +1648,10 @@ showHandle' filepath is_duplex h = -- --------------------------------------------------------------------------- -- debugging -#ifdef DEBUG_DUMP +#if defined(DEBUG_DUMP) puts :: String -> IO () -puts s = withCString s $ \cstr -> do write_rawBuffer 1 False cstr 0 (fromIntegral (length s)) - return () +puts s = do write_rawBuffer 1 (unsafeCoerce# (packCString# s)) 0 (fromIntegral (length s)) + return () #endif -- -----------------------------------------------------------------------------