This commit is contained in:
Julian Ospald 2020-01-29 18:24:26 +01:00
parent 26070cfd3e
commit a5f3ab8139
No known key found for this signature in database
GPG Key ID: 511B62C09D50CD28
1 changed files with 18 additions and 3 deletions

View File

@ -12,12 +12,17 @@
-- --
-- This module provides high-level file streaming API. -- This module provides high-level file streaming API.
module Streamly.External.FileSystem.Handle.Posix module Streamly.External.FileSystem.Handle.Posix
( readFileLBS (
-- * File reading
readFileLBS
, readFileStream , readFileStream
-- * File writing
, copyFileHandle , copyFileHandle
, copyFileStream , copyFileStream
-- * Directory listing
, unfoldDirContents , unfoldDirContents
, dirContentsStream , dirContentsStream
, dirContents
, DirType , DirType
) )
where where
@ -77,7 +82,7 @@ readFileLBS handle' = fromChunks (readFileStream handle')
readFileStream :: (MonadCatch m, MonadAsync m) readFileStream :: (MonadCatch m, MonadAsync m)
=> Handle => Handle
-> SerialT m (Array Word8) -> SerialT m (Array Word8)
readFileStream = S.unfold (SIU.finallyIO (liftIO . hClose) FH.readChunks) readFileStream = S.unfold (SIU.finallyIO (\h -> liftIO (putStrLn "close readFileStream") >> liftIO (hClose h)) FH.readChunks)
-- | Like 'copyFileStream', except for two file handles. -- | Like 'copyFileStream', except for two file handles.
@ -97,7 +102,7 @@ copyFileStream :: (MonadCatch m, MonadAsync m, MonadMask m)
-> Handle -- ^ file handle to copy to, must be writable -> Handle -- ^ file handle to copy to, must be writable
-> m () -> m ()
copyFileStream stream handle' = copyFileStream stream handle' =
finally (liftIO $ hClose handle') $ S.fold (FH.writeChunks handle') stream (flip finally) (liftIO $ hClose handle') $ S.fold (FH.writeChunks handle') stream
-- | Create an 'Unfold' of directory contents. -- | Create an 'Unfold' of directory contents.
@ -123,3 +128,13 @@ dirContentsStream :: (MonadCatch m, MonadAsync m, MonadMask m)
-> SerialT m (DirType, RawFilePath) -> SerialT m (DirType, RawFilePath)
dirContentsStream = dirContentsStream =
S.unfold (SIU.finallyIO (liftIO . PosixBS.closeDirStream) unfoldDirContents) S.unfold (SIU.finallyIO (liftIO . PosixBS.closeDirStream) unfoldDirContents)
-- | Read the directory contents strictly as a list.
--
-- The DirStream is closed automatically.
dirContents :: (MonadCatch m, MonadAsync m, MonadMask m)
=> DirStream
-> m [(DirType, RawFilePath)]
dirContents = S.toList . S.unfold
(SIU.finallyIO (liftIO . PosixBS.closeDirStream) unfoldDirContents)