stdin, stdout, stderr,
IOMode(..), openFile, openBinaryFile, openFd, fdToHandle,
- hFileSize, hIsEOF, isEOF, hLookAhead, hSetBuffering, hSetBinaryMode,
+ hFileSize, hSetFileSize, hIsEOF, isEOF, hLookAhead, hSetBuffering, hSetBinaryMode,
hFlush, hDuplicate, hDuplicateTo,
hClose, hClose_help,
})
-----------------------------------------------------------------------------
--- 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.
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
-- * Operations on handles
- -- ** Determining the size of a file
+ -- ** Determining and changing the size of a file
hFileSize, -- :: Handle -> IO Integer
+#ifdef __GLASGOW_HASKELL__
+ hSetFileSize, -- :: Handle -> Integer -> IO ()
+#endif
-- ** Detecting the end of input
foreign import ccall unsafe "HsBase.h write"
c_write :: CInt -> Ptr CChar -> CSize -> IO CSsize
+foreign import ccall unsafe "HsBase.h __hscore_ftruncate"
+ c_ftruncate :: CInt -> COff -> IO CInt
+
foreign import ccall unsafe "HsBase.h unlink"
c_unlink :: CString -> IO CInt
return SEEK_END;
}
+INLINE int
+__hscore_ftruncate( int fd, off_t where )
+{
+#if defined(HAVE_FTRUNCATE)
+ return ftruncate(fd,where);
+#elif defined(HAVE__CHSIZE)
+ return _chsize(fd,where);
+#else
+#error at least ftruncate or _chsize functions are required to build
+#endif
+}
+
INLINE HsInt
__hscore_setmode( HsInt fd, HsBool toBin )
{