Skip to content

Commit 3d3144d

Browse files
Use binary mode in FileIO module
1 parent 8f07cd7 commit 3d3144d

File tree

4 files changed

+30
-32
lines changed

4 files changed

+30
-32
lines changed

core/src/Streamly/FileSystem/FileIO.hs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@
1515
-- the handle based APIs as there is no possibility of a file descriptor
1616
-- leakage.
1717
--
18+
-- The file is opened in binary mode as encoding, decoding, and newline
19+
-- translation can be handled explicitly by the streaming APIs.
20+
--
21+
-- The file is opened without buffering as buffering can be controlled
22+
-- explicitly by the streaming APIs.
23+
--
1824
-- >> import qualified Streamly.FileSystem.FileIO as File
1925
--
2026
module Streamly.FileSystem.FileIO

core/src/Streamly/Internal/FileSystem/FileIO.hs

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,6 @@
66
-- Maintainer : [email protected]
77
-- Portability : GHC
88
--
9-
-- Read and write streams and arrays to and from files specified by their paths
10-
-- in the file system. Unlike the handle based APIs which can have a read/write
11-
-- session consisting of multiple reads and writes to the handle, these APIs
12-
-- are one shot read or write APIs. These APIs open the file handle, perform
13-
-- the requested operation and close the handle. Thease are safer compared to
14-
-- the handle based APIs as there is no possibility of a file descriptor
15-
-- leakage.
16-
--
17-
-- > import qualified Streamly.Internal.FileSystem.FileIO as File
18-
--
199

2010
module Streamly.Internal.FileSystem.FileIO
2111
(
@@ -130,14 +120,17 @@ import qualified Streamly.Internal.FileSystem.Windows.File as File
130120
-- Safe file reading
131121
-------------------------------------------------------------------------------
132122

133-
-- | @'withFile' name mode act@ opens a file with NoBuffering set on the handle
134-
-- and passes the resulting handle to the computation @act@. The handle will be
135-
-- closed on exit from 'withFile', whether by normal termination or by raising
136-
-- an exception. If closing the handle raises an exception, then that
137-
-- exception is raised by 'withFile' rather than any exception raised by 'act'.
123+
-- | @'withFile' name mode act@ opens a file and passes the resulting handle to
124+
-- the computation @act@. The handle is closed on exit from 'withFile', whether
125+
-- by normal termination or by raising an exception. If closing the handle
126+
-- raises an exception, then that exception is raised by 'withFile' rather than
127+
-- any exception raised by 'act'.
138128
--
139-
-- The file is opened without buffering as buffering can be controlled by the
140-
-- streaming APIs.
129+
-- The file is opened in binary mode as encoding, decoding, and newline
130+
-- translation can be handled explicitly by the streaming APIs.
131+
--
132+
-- The file is opened without buffering as buffering can be controlled
133+
-- explicitly by the streaming APIs.
141134
--
142135
-- /Pre-release/
143136
--
@@ -149,7 +142,7 @@ withFile file mode = S.bracketIO open hClose
149142
where
150143

151144
open = do
152-
h <- File.openFile file mode
145+
h <- File.openBinaryFile file mode
153146
hSetBuffering h NoBuffering
154147
return h
155148

@@ -160,8 +153,11 @@ withFile file mode = S.bracketIO open hClose
160153
-- handle raises an exception, then this exception will be raised by
161154
-- 'usingFile'.
162155
--
163-
-- The file is opened without buffering as buffering can be controlled by the
164-
-- streaming APIs.
156+
-- The file is opened in binary mode as encoding, decoding, and newline
157+
-- translation can be handled explicitly by the streaming APIs.
158+
--
159+
-- The file is opened without buffering as buffering can be controlled
160+
-- explicitly by the streaming APIs.
165161
--
166162
-- /Pre-release/
167163
--
@@ -173,7 +169,7 @@ usingFile = UF.bracketIO open hClose
173169
where
174170

175171
open file = do
176-
h <- File.openFile file ReadMode
172+
h <- File.openBinaryFile file ReadMode
177173
hSetBuffering h NoBuffering
178174
return h
179175

@@ -185,7 +181,7 @@ usingFile2 = UF.bracketIO before after
185181
where
186182

187183
before (x, file) = do
188-
h <- File.openFile file ReadMode
184+
h <- File.openBinaryFile file ReadMode
189185
hSetBuffering h NoBuffering
190186
return (x, h)
191187

@@ -199,7 +195,7 @@ usingFile3 = UF.bracketIO before after
199195
where
200196

201197
before (x, y, z, file) = do
202-
h <- File.openFile file ReadMode
198+
h <- File.openBinaryFile file ReadMode
203199
hSetBuffering h NoBuffering
204200
return (x, y, z, h)
205201

@@ -410,7 +406,7 @@ writeChunks :: (MonadIO m, MonadCatch m)
410406
writeChunks path = Fold step initial extract final
411407
where
412408
initial = do
413-
h <- liftIO (File.openFile path WriteMode)
409+
h <- liftIO (File.openBinaryFile path WriteMode)
414410
liftIO $ hSetBuffering h NoBuffering
415411
fld <- FL.reduce (FH.writeChunks h)
416412
`MC.onException` liftIO (hClose h)

core/src/Streamly/Internal/FileSystem/Posix/File.hsc

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ module Streamly.Internal.FileSystem.Posix.File
6666
-- * Handle based
6767
, openFile
6868
, withFile
69-
-- , openBinaryFile
70-
-- , withBinaryFile
69+
, openBinaryFile
70+
, withBinaryFile
7171

7272
-- Re-exported
7373
, Fd
@@ -306,13 +306,11 @@ openFile = File.openFile False openFileHandle
306306
withFile :: PosixPath -> IOMode -> (Handle -> IO r) -> IO r
307307
withFile = File.withFile False openFileHandle
308308

309-
{-
310309
-- | Like openBinaryFile in base package but using Path instead of FilePath.
311310
openBinaryFile :: PosixPath -> IOMode -> IO Handle
312311
openBinaryFile = File.openFile True openFileHandle
313312

314313
-- | Like withBinaryFile in base package but using Path instead of FilePath.
315314
withBinaryFile :: PosixPath -> IOMode -> (Handle -> IO r) -> IO r
316315
withBinaryFile = File.withFile True openFileHandle
317-
-}
318316
#endif

core/src/Streamly/Internal/FileSystem/Windows/File.hsc

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ module Streamly.Internal.FileSystem.Windows.File
66
-- * Handle based
77
openFile
88
, withFile
9-
-- , openBinaryFile
10-
-- , withBinaryFile
9+
, openBinaryFile
10+
, withBinaryFile
1111
#endif
1212
) where
1313

@@ -192,13 +192,11 @@ withFile = File.withFile False openFileHandle
192192
openFile :: WindowsPath -> IOMode -> IO Handle
193193
openFile = File.openFile False openFileHandle
194194

195-
{-
196195
-- | Like withBinaryFile in base package but using Path instead of FilePath.
197196
withBinaryFile :: WindowsPath -> IOMode -> (Handle -> IO r) -> IO r
198197
withBinaryFile = File.withFile True openFileHandle
199198

200199
-- | Like openBinaryFile in base package but using Path instead of FilePath.
201200
openBinaryFile :: WindowsPath -> IOMode -> IO Handle
202201
openBinaryFile = File.openFile True openFileHandle
203-
-}
204202
#endif

0 commit comments

Comments
 (0)