-
Notifications
You must be signed in to change notification settings - Fork 70
Expand file tree
/
Copy pathDir.hs
More file actions
227 lines (189 loc) · 6.91 KB
/
Dir.hs
File metadata and controls
227 lines (189 loc) · 6.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#include "inline.hs"
-- |
-- Module : Streamly.Internal.FileSystem.Dir
-- Copyright : (c) 2018 Composewell Technologies
--
-- License : BSD3
-- Maintainer : streamly@composewell.com
-- Portability : GHC
module Streamly.Internal.FileSystem.Dir
{-# DEPRECATED "Please use \"Streamly.Internal.FileSystem.DirIO\" instead." #-}
(
-- * Streams
read
-- read not just the names but also the inode attrs of the children. This
-- abstraction makes sense because when we read the dir contents we also
-- get the inodes, and it is cheaper to get the attrs from the inodes
-- instead of resolving the paths and get those. This abstraction may be
-- less portable as different platforms may have different attrs. To
-- optimize, we can also add a filter/pattern/parser on the names of the
-- children that we want to read. We can call that readAttrsWith? Or just
-- have the default readAttrs do that? Usually we won't need that, so it
-- may be better to keep that a separate API.
-- , readAttrs
-- recursive read requires us to read the attributes of the children to
-- determine if something is a dirctory or not. Therefore, it may be a good
-- idea to have a low level routine that also spits out the attributes of
-- the files, we get that for free. We can also add a filter/pattern/parser
-- on the names of the children that we want to read.
--, readAttrsRecursive -- Options: acyclic, follow symlinks
, readFiles
, readDirs
, readEither
, readEitherPaths
-- We can implement this in terms of readAttrsRecursive without losing
-- perf.
-- , readEitherRecursive -- Options: acyclic, follow symlinks
-- , readAncestors -- read the parent chain using the .. entry.
-- , readAncestorsAttrs
-- * Unfolds
-- | Use the more convenient stream APIs instead of unfolds where possible.
, reader
, fileReader
, dirReader
, eitherReader
, eitherReaderPaths
{-
, toStreamWithBufferOf
, readChunks
, readChunksWithBufferOf
, toChunksWithBufferOf
, toChunks
, write
, writeWithBufferOf
-- Byte stream write (Streams)
, fromStream
, fromStreamWithBufferOf
-- -- * Array Write
, writeArray
, writeChunks
, writeChunksWithBufferOf
-- -- * Array stream Write
, fromChunks
, fromChunksWithBufferOf
-}
-- * Deprecated
, toStream
, toEither
, toFiles
, toDirs
)
where
import Control.Monad.Catch (MonadCatch)
import Control.Monad.IO.Class (MonadIO(..))
import Data.Bifunctor (bimap)
import Streamly.Data.Stream (Stream)
import Streamly.Internal.Data.Unfold.Type (Unfold(..))
import System.FilePath ((</>))
import qualified Streamly.Data.Stream as S
import Streamly.Internal.FileSystem.Path (Path)
import qualified Streamly.Internal.FileSystem.Path as Path
import qualified Streamly.Internal.FileSystem.DirIO as DirIO
import qualified Streamly.Internal.Data.Unfold as Unfold
import Prelude hiding (read)
--------------------------------------------------------------------------------
-- Helpers
--------------------------------------------------------------------------------
{-# INLINE ePathMap #-}
ePathMap :: Either Path Path -> Either FilePath FilePath
ePathMap (Left a) = Left (Path.toString a)
ePathMap (Right a) = Right (Path.toString a)
{-# INLINE pMapUnfold #-}
pMapUnfold :: MonadCatch m => Unfold m Path Path -> Unfold m FilePath FilePath
pMapUnfold = fmap Path.toString . Unfold.lmapM Path.fromString
{-# INLINE pMapUnfoldE #-}
pMapUnfoldE
:: MonadCatch m
=> Unfold m Path (Either Path Path)
-> Unfold m FilePath (Either FilePath FilePath)
pMapUnfoldE = fmap ePathMap . Unfold.lmapM Path.fromString
--------------------------------------------------------------------------------
-- Functions
--------------------------------------------------------------------------------
#if defined(mingw32_HOST_OS) || defined(__MINGW32__)
#define CONF id
#else
#define CONF (DirIO.followSymlinks True)
#endif
-- | Read a directory emitting a stream with names of the children. Filter out
-- "." and ".." entries.
--
-- /Internal/
--
{-# INLINE reader #-}
reader :: (MonadIO m, MonadCatch m) => Unfold m FilePath FilePath
reader = fmap Path.toString $ Unfold.lmapM Path.fromString DirIO.reader
-- | Read directories as Left and files as Right. Filter out "." and ".."
-- entries.
--
-- /Internal/
--
{-# INLINE eitherReader #-}
eitherReader :: (MonadIO m, MonadCatch m) => Unfold m FilePath (Either FilePath FilePath)
eitherReader = pMapUnfoldE (DirIO.eitherReader CONF)
{-# INLINE eitherReaderPaths #-}
eitherReaderPaths ::(MonadIO m, MonadCatch m) => Unfold m FilePath (Either FilePath FilePath)
eitherReaderPaths = pMapUnfoldE (DirIO.eitherReaderPaths CONF)
--
-- | Read files only.
--
-- /Internal/
--
{-# INLINE fileReader #-}
fileReader :: (MonadIO m, MonadCatch m) => Unfold m FilePath FilePath
fileReader = pMapUnfold (DirIO.fileReader CONF)
-- | Read directories only. Filter out "." and ".." entries.
--
-- /Internal/
--
{-# INLINE dirReader #-}
dirReader :: (MonadIO m, MonadCatch m) => Unfold m FilePath FilePath
dirReader = pMapUnfold (DirIO.dirReader CONF)
-- | Raw read of a directory.
--
-- /Pre-release/
{-# INLINE read #-}
read :: (MonadIO m, MonadCatch m) => FilePath -> Stream m FilePath
read = S.unfold reader
{-# DEPRECATED toStream "Please use 'read' instead" #-}
{-# INLINE toStream #-}
toStream :: (MonadIO m, MonadCatch m) => String -> Stream m String
toStream = read
-- | Read directories as Left and files as Right. Filter out "." and ".."
-- entries. The output contains the names of the directories and files.
--
-- /Pre-release/
{-# INLINE readEither #-}
readEither :: (MonadIO m, MonadCatch m) => FilePath -> Stream m (Either FilePath FilePath)
readEither = S.unfold eitherReader
-- | Like 'readEither' but prefix the names of the files and directories with
-- the supplied directory path.
{-# INLINE readEitherPaths #-}
readEitherPaths :: (MonadIO m, MonadCatch m) => FilePath -> Stream m (Either FilePath FilePath)
readEitherPaths dir = fmap (bimap (dir </>) (dir </>)) $ readEither dir
{-# DEPRECATED toEither "Please use 'readEither' instead" #-}
{-# INLINE toEither #-}
toEither :: (MonadIO m, MonadCatch m) => FilePath -> Stream m (Either FilePath FilePath)
toEither = readEither
-- | Read files only.
--
-- /Internal/
--
{-# INLINE readFiles #-}
readFiles :: (MonadIO m, MonadCatch m) => FilePath -> Stream m FilePath
readFiles = S.unfold fileReader
{-# DEPRECATED toFiles "Please use 'readFiles' instead" #-}
{-# INLINE toFiles #-}
toFiles :: (MonadIO m, MonadCatch m) => FilePath -> Stream m FilePath
toFiles = readFiles
-- | Read directories only.
--
-- /Internal/
--
{-# INLINE readDirs #-}
readDirs :: (MonadIO m, MonadCatch m) => FilePath -> Stream m FilePath
readDirs = S.unfold dirReader
{-# DEPRECATED toDirs "Please use 'readDirs' instead" #-}
{-# INLINE toDirs #-}
toDirs :: (MonadIO m, MonadCatch m) => String -> Stream m String
toDirs = readDirs