[project @ 2005-12-13 13:28:53 by simonmar]
[haskell-directory.git] / GHC / Handle.hs
index f3008c4..52bf8cd 100644 (file)
@@ -813,15 +813,21 @@ openFile' filepath mode binary =
              throwErrnoIfMinus1Retry "openFile"
                (c_open f (fromIntegral oflags) 0o666)
 
-    h <- openFd fd Nothing False filepath mode binary
+    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
-    if mode == WriteMode
+       -- 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
@@ -909,10 +915,6 @@ openFd fd mb_fd_type is_socket filepath mode binary = 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
@@ -923,6 +925,20 @@ openFd fd mb_fd_type is_socket filepath mode binary = do
 #endif
           mkFileHandle fd is_socket filepath ha_type binary
 
+       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
+
+       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
@@ -1140,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
@@ -1456,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)