21
21
module System.Posix.Directory.Common (
22
22
DirStream (.. ),
23
23
CDir ,
24
+ CDirent ,
25
+ DirStreamOffset (.. ),
26
+
24
27
DirStreamWithPath (.. ),
25
28
fromDirStreamWithPath ,
26
29
toDirStreamWithPath ,
27
-
28
30
DirEnt (.. ),
29
- CDirent ,
30
31
dirEntName ,
31
32
dirEntType ,
32
33
DirType ( DirType
@@ -40,15 +41,20 @@ module System.Posix.Directory.Common (
40
41
, SocketType
41
42
, WhiteoutType
42
43
),
43
- isUnknownType , isBlockDeviceType , isCharacterDeviceType , isNamedPipeType ,
44
- isRegularFileType , isDirectoryType , isSymbolicLinkType , isSocketType ,
44
+ isUnknownType ,
45
+ isNamedPipeType ,
46
+ isCharacterDeviceType ,
47
+ isDirectoryType ,
48
+ isBlockDeviceType ,
49
+ isRegularFileType ,
50
+ isSymbolicLinkType ,
51
+ isSocketType ,
45
52
isWhiteoutType ,
46
53
getRealDirType ,
47
54
unsafeOpenDirStreamFd ,
48
55
readDirStreamWith ,
49
56
readDirStreamWithPtr ,
50
57
51
- DirStreamOffset (.. ),
52
58
rewindDirStream ,
53
59
closeDirStream ,
54
60
#ifdef HAVE_SEEKDIR
@@ -75,22 +81,28 @@ import System.Posix.Files.Common
75
81
76
82
newtype DirStream = DirStream (Ptr CDir )
77
83
84
+ -- | @since 2.8.6.0
78
85
newtype DirStreamWithPath a = DirStreamWithPath (a , Ptr CDir )
79
86
80
87
-- | Convert a 'DirStreamWithPath' to a 'DirStream'.
81
88
-- Note that the underlying pointer is shared by both values, hence any
82
89
-- modification to the resulting 'DirStream' will also modify the original
83
90
-- 'DirStreamWithPath'.
91
+ --
92
+ -- @since 2.8.6.0
84
93
fromDirStreamWithPath :: DirStreamWithPath a -> DirStream
85
94
fromDirStreamWithPath (DirStreamWithPath (_, ptr)) = DirStream ptr
86
95
87
96
-- | Construct a 'DirStreamWithPath' from a 'DirStream'.
88
97
-- Note that the underlying pointer is shared by both values, hence any
89
98
-- modification to the pointer of the resulting 'DirStreamWithPath' will also
90
99
-- modify the original 'DirStream'.
100
+ --
101
+ -- @since 2.8.6.0
91
102
toDirStreamWithPath :: a -> DirStream -> DirStreamWithPath a
92
103
toDirStreamWithPath path (DirStream ptr) = DirStreamWithPath (path, ptr)
93
104
105
+ -- | @since 2.8.6.0
94
106
newtype DirEnt = DirEnt (Ptr CDirent )
95
107
96
108
-- We provide a hand-written instance here since GeneralizedNewtypeDeriving and
@@ -126,6 +138,8 @@ data {-# CTYPE "struct dirent" #-} CDirent
126
138
-- synonyms of this type may not be provided by the underlying platform. In that
127
139
-- case none of those patterns will match and the application must handle that
128
140
-- case accordingly.
141
+ --
142
+ -- @since 2.8.6.0
129
143
newtype DirType = DirType CChar
130
144
deriving (Eq , Ord , Show )
131
145
@@ -166,22 +180,40 @@ pattern WhiteoutType :: DirType
166
180
pattern WhiteoutType = DirType (CONST_DT_WHT )
167
181
168
182
-- | Checks if this 'DirType' refers to an entry of unknown type.
183
+ --
184
+ -- @since 2.8.6.0
169
185
isUnknownType :: DirType -> Bool
170
186
-- | Checks if this 'DirType' refers to a block device entry.
187
+ --
188
+ -- @since 2.8.6.0
171
189
isBlockDeviceType :: DirType -> Bool
172
190
-- | Checks if this 'DirType' refers to a character device entry.
191
+ --
192
+ -- @since 2.8.6.0
173
193
isCharacterDeviceType :: DirType -> Bool
174
194
-- | Checks if this 'DirType' refers to a named pipe entry.
195
+ --
196
+ -- @since 2.8.6.0
175
197
isNamedPipeType :: DirType -> Bool
176
198
-- | Checks if this 'DirType' refers to a regular file entry.
199
+ --
200
+ -- @since 2.8.6.0
177
201
isRegularFileType :: DirType -> Bool
178
202
-- | Checks if this 'DirType' refers to a directory entry.
203
+ --
204
+ -- @since 2.8.6.0
179
205
isDirectoryType :: DirType -> Bool
180
206
-- | Checks if this 'DirType' refers to a symbolic link entry.
207
+ --
208
+ -- @since 2.8.6.0
181
209
isSymbolicLinkType :: DirType -> Bool
182
210
-- | Checks if this 'DirType' refers to a socket entry.
211
+ --
212
+ -- @since 2.8.6.0
183
213
isSocketType :: DirType -> Bool
184
214
-- | Checks if this 'DirType' refers to a whiteout entry.
215
+ --
216
+ -- @since 2.8.6.0
185
217
isWhiteoutType :: DirType -> Bool
186
218
187
219
isUnknownType dtype = dtype == UnknownType
@@ -194,6 +226,7 @@ isSymbolicLinkType dtype = dtype == SymbolicLinkType
194
226
isSocketType dtype = dtype == SocketType
195
227
isWhiteoutType dtype = dtype == WhiteoutType
196
228
229
+ -- | @since 2.8.6.0
197
230
getRealDirType :: IO FileStatus -> DirType -> IO DirType
198
231
getRealDirType _ BlockDeviceType = return BlockDeviceType
199
232
getRealDirType _ CharacterDeviceType = return CharacterDeviceType
@@ -225,6 +258,8 @@ getRealDirType getFileStatus _ = do
225
258
--
226
259
-- The input file descriptor must not have been used with @threadWaitRead@ or
227
260
-- @threadWaitWrite@.
261
+ --
262
+ -- @since 2.8.6.0
228
263
unsafeOpenDirStreamFd :: Fd -> IO DirStream
229
264
unsafeOpenDirStreamFd (Fd fd) = mask_ $ do
230
265
ptr <- c_fdopendir fd
@@ -257,6 +292,8 @@ foreign import capi unsafe "dirent.h fdopendir"
257
292
-- __NOTE:__ The lifetime of the pointer wrapped in the `DirEnt` is limited to
258
293
-- invocation of the callback and it will be freed automatically after. Do not
259
294
-- pass it to the outside world!
295
+ --
296
+ -- @since 2.8.6.0
260
297
readDirStreamWith :: (DirEnt -> IO a ) -> DirStream -> IO (Maybe a )
261
298
readDirStreamWith f dstream = alloca
262
299
(\ ptr_dEnt -> readDirStreamWithPtr ptr_dEnt f dstream)
@@ -270,6 +307,8 @@ readDirStreamWith f dstream = alloca
270
307
-- call of these functions.
271
308
--
272
309
-- __NOTE__: You are responsible for releasing the pointer after you are done.
310
+ --
311
+ -- @since 2.8.6.0
273
312
readDirStreamWithPtr :: Ptr DirEnt -> (DirEnt -> IO a ) -> DirStream -> IO (Maybe a )
274
313
readDirStreamWithPtr ptr_dEnt f dstream@ (DirStream dirp) = do
275
314
resetErrno
@@ -291,12 +330,14 @@ readDirStreamWithPtr ptr_dEnt f dstream@(DirStream dirp) = do
291
330
then return Nothing
292
331
else throwErrno " readDirStream"
293
332
333
+ -- | @since 2.8.6.0
294
334
dirEntName :: DirEnt -> IO CString
295
335
dirEntName (DirEnt dEntPtr) = d_name dEntPtr
296
336
297
337
foreign import ccall unsafe " __hscore_d_name"
298
338
d_name :: Ptr CDirent -> IO CString
299
339
340
+ -- | @since 2.8.6.0
300
341
dirEntType :: DirEnt -> IO DirType
301
342
dirEntType (DirEnt dEntPtr) = DirType <$> d_type dEntPtr
302
343
0 commit comments