Skip to content

Commit a3d23d8

Browse files
mmhathasufell
authored andcommitted
Renamed pattern synonyms and added some more Haddocks
1 parent 999d3ae commit a3d23d8

File tree

4 files changed

+138
-63
lines changed

4 files changed

+138
-63
lines changed

System/Posix/Directory.hsc

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,19 @@ module System.Posix.Directory (
2929

3030
-- * Reading directories
3131
DirStream,
32-
DirType( DtUnknown
33-
, DtFifo
34-
, DtChr
35-
, DtDir
36-
, DtBlk
37-
, DtReg
38-
, DtLnk
39-
, DtSock
40-
, DtWht
32+
DirType( UnknownType
33+
, NamedPipeType
34+
, CharacterDeviceType
35+
, DirectoryType
36+
, BlockDeviceType
37+
, RegularFileType
38+
, SymbolicLinkType
39+
, SocketType
40+
, WhiteoutType
4141
),
42+
isUnknownType, isBlockDeviceType, isCharacterDeviceType, isNamedPipeType,
43+
isRegularFileType, isDirectoryType, isSymbolicLinkType, isSocketType,
44+
isWhiteoutType,
4245
openDirStream,
4346
readDirStream,
4447
readDirStreamMaybe,
@@ -118,6 +121,9 @@ readDirStreamMaybe = readDirStreamWith
118121
-- structure together with the entry's type (@d_type@) wrapped in a
119122
-- @Just (d_name, d_type)@ if an entry was read and @Nothing@ if
120123
-- the end of the directory stream was reached.
124+
--
125+
-- __Note__: The returned 'DirType' has some limitations; Please see its
126+
-- documentation.
121127
readDirStreamWithType :: DirStream -> IO (Maybe (FilePath, DirType))
122128
readDirStreamWithType = readDirStreamWith
123129
(\(DirEnt dEnt) -> (,)

System/Posix/Directory/ByteString.hsc

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,19 @@ module System.Posix.Directory.ByteString (
2929

3030
-- * Reading directories
3131
DirStream,
32-
DirType( DtUnknown
33-
, DtFifo
34-
, DtChr
35-
, DtDir
36-
, DtBlk
37-
, DtReg
38-
, DtLnk
39-
, DtSock
40-
, DtWht
32+
DirType( UnknownType
33+
, NamedPipeType
34+
, CharacterDeviceType
35+
, DirectoryType
36+
, BlockDeviceType
37+
, RegularFileType
38+
, SymbolicLinkType
39+
, SocketType
40+
, WhiteoutType
4141
),
42+
isUnknownType, isBlockDeviceType, isCharacterDeviceType, isNamedPipeType,
43+
isRegularFileType, isDirectoryType, isSymbolicLinkType, isSocketType,
44+
isWhiteoutType,
4245
openDirStream,
4346
readDirStream,
4447
readDirStreamMaybe,
@@ -119,6 +122,9 @@ readDirStreamMaybe = readDirStreamWith
119122
-- structure together with the entry's type (@d_type@) wrapped in a
120123
-- @Just (d_name, d_type)@ if an entry was read and @Nothing@ if
121124
-- the end of the directory stream was reached.
125+
--
126+
-- __Note__: The returned 'DirType' has some limitations; Please see its
127+
-- documentation.
122128
readDirStreamWithType :: DirStream -> IO (Maybe (RawFilePath, DirType))
123129
readDirStreamWithType = readDirStreamWith
124130
(\(DirEnt dEnt) -> (,)

System/Posix/Directory/Common.hsc

Lines changed: 93 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,19 @@
2121
module System.Posix.Directory.Common (
2222
DirStream(..), DirEnt(..), CDir, CDirent, DirStreamOffset(..),
2323
DirType( DirType
24-
, DtUnknown
25-
, DtFifo
26-
, DtChr
27-
, DtDir
28-
, DtBlk
29-
, DtReg
30-
, DtLnk
31-
, DtSock
32-
, DtWht
24+
, UnknownType
25+
, NamedPipeType
26+
, CharacterDeviceType
27+
, DirectoryType
28+
, BlockDeviceType
29+
, RegularFileType
30+
, SymbolicLinkType
31+
, SocketType
32+
, WhiteoutType
3333
),
34+
isUnknownType, isBlockDeviceType, isCharacterDeviceType, isNamedPipeType,
35+
isRegularFileType, isDirectoryType, isSymbolicLinkType, isSocketType,
36+
isWhiteoutType,
3437
unsafeOpenDirStreamFd,
3538
readDirStreamWith,
3639
readDirStreamWithPtr,
@@ -78,34 +81,88 @@ instance Storable DirEnt where
7881
data {-# CTYPE "DIR" #-} CDir
7982
data {-# CTYPE "struct dirent" #-} CDirent
8083

84+
-- | The value of the @d_type@ field of a @dirent@ struct.
85+
-- Note that the possible values of that type depend on the filesystem that is
86+
-- queried. From @readdir(3)@:
87+
--
88+
-- > Currently, only some filesystems (among them: Btrfs, ext2, ext3, and ext4)
89+
-- > have full support for returning the file type in d_type. All applications
90+
-- > must properly handle a return of DT_UNKNOWN.
91+
--
92+
-- For example, JFS is a filesystem that does not support @d_type@;
93+
-- See https://github.com/haskell/ghcup-hs/issues/766
94+
--
95+
-- Furthermore, @dirent@ or the constants represented by the associated pattern
96+
-- synonyms of this type may not be provided by the underlying platform. In that
97+
-- case none of those patterns will match and the application must handle that
98+
-- case accordingly.
8199
newtype DirType = DirType CChar
82-
83-
pattern DtUnknown :: DirType
84-
pattern DtUnknown = DirType CONST_DT_UNKNOWN
85-
86-
pattern DtFifo :: DirType
87-
pattern DtFifo = DirType (CONST_DT_FIFO)
88-
89-
pattern DtChr :: DirType
90-
pattern DtChr = DirType (CONST_DT_CHR)
91-
92-
pattern DtDir :: DirType
93-
pattern DtDir = DirType (CONST_DT_DIR)
94-
95-
pattern DtBlk :: DirType
96-
pattern DtBlk = DirType (CONST_DT_BLK)
97-
98-
pattern DtReg :: DirType
99-
pattern DtReg = DirType (CONST_DT_REG)
100-
101-
pattern DtLnk :: DirType
102-
pattern DtLnk = DirType (CONST_DT_LNK)
103-
104-
pattern DtSock :: DirType
105-
pattern DtSock = DirType (CONST_DT_SOCK)
106-
107-
pattern DtWht :: DirType
108-
pattern DtWht = DirType (CONST_DT_WHT)
100+
deriving Eq
101+
102+
-- | The 'DirType' refers to an entry of unknown type.
103+
pattern UnknownType :: DirType
104+
pattern UnknownType = DirType CONST_DT_UNKNOWN
105+
106+
-- | The 'DirType' refers to an entry that is a named pipe.
107+
pattern NamedPipeType :: DirType
108+
pattern NamedPipeType = DirType CONST_DT_FIFO
109+
110+
-- | The 'DirType' refers to an entry that is a character device.
111+
pattern CharacterDeviceType :: DirType
112+
pattern CharacterDeviceType = DirType CONST_DT_CHR
113+
114+
-- | The 'DirType' refers to an entry that is a directory.
115+
pattern DirectoryType :: DirType
116+
pattern DirectoryType = DirType CONST_DT_DIR
117+
118+
-- | The 'DirType' refers to an entry that is a block device.
119+
pattern BlockDeviceType :: DirType
120+
pattern BlockDeviceType = DirType CONST_DT_BLK
121+
122+
-- | The 'DirType' refers to an entry that is a regular file.
123+
pattern RegularFileType :: DirType
124+
pattern RegularFileType = DirType CONST_DT_REG
125+
126+
-- | The 'DirType' refers to an entry that is a symbolic link.
127+
pattern SymbolicLinkType :: DirType
128+
pattern SymbolicLinkType = DirType CONST_DT_LNK
129+
130+
-- | The 'DirType' refers to an entry that is a socket.
131+
pattern SocketType :: DirType
132+
pattern SocketType = DirType CONST_DT_SOCK
133+
134+
-- | The 'DirType' refers to an entry that is a whiteout.
135+
pattern WhiteoutType :: DirType
136+
pattern WhiteoutType = DirType CONST_DT_WHT
137+
138+
-- | Checks if this 'DirType' refers to an entry of unknown type.
139+
isUnknownType :: DirType -> Bool
140+
-- | Checks if this 'DirType' refers to a block device entry.
141+
isBlockDeviceType :: DirType -> Bool
142+
-- | Checks if this 'DirType' refers to a character device entry.
143+
isCharacterDeviceType :: DirType -> Bool
144+
-- | Checks if this 'DirType' refers to a named pipe entry.
145+
isNamedPipeType :: DirType -> Bool
146+
-- | Checks if this 'DirType' refers to a regular file entry.
147+
isRegularFileType :: DirType -> Bool
148+
-- | Checks if this 'DirType' refers to a directory entry.
149+
isDirectoryType :: DirType -> Bool
150+
-- | Checks if this 'DirType' refers to a symbolic link entry.
151+
isSymbolicLinkType :: DirType -> Bool
152+
-- | Checks if this 'DirType' refers to a socket entry.
153+
isSocketType :: DirType -> Bool
154+
-- | Checks if this 'DirType' refers to a whiteout entry.
155+
isWhiteoutType :: DirType -> Bool
156+
157+
isUnknownType dtype = dtype == UnknownType
158+
isBlockDeviceType dtype = dtype == BlockDeviceType
159+
isCharacterDeviceType dtype = dtype == CharacterDeviceType
160+
isNamedPipeType dtype = dtype == NamedPipeType
161+
isRegularFileType dtype = dtype == RegularFileType
162+
isDirectoryType dtype = dtype == DirectoryType
163+
isSymbolicLinkType dtype = dtype == SymbolicLinkType
164+
isSocketType dtype = dtype == SocketType
165+
isWhiteoutType dtype = dtype == WhiteoutType
109166

110167
-- | Call @fdopendir@ to obtain a directory stream for @fd@. @fd@ must not be
111168
-- otherwise used after this.

System/Posix/Directory/PosixPath.hsc

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,19 @@ module System.Posix.Directory.PosixPath (
2828

2929
-- * Reading directories
3030
Common.DirStream,
31-
Common.DirType( DtUnknown
32-
, DtFifo
33-
, DtChr
34-
, DtDir
35-
, DtBlk
36-
, DtReg
37-
, DtLnk
38-
, DtSock
39-
, DtWht
31+
Common.DirType( UnknownType
32+
, NamedPipeType
33+
, CharacterDeviceType
34+
, DirectoryType
35+
, BlockDeviceType
36+
, RegularFileType
37+
, SymbolicLinkType
38+
, SocketType
39+
, WhiteoutType
4040
),
41+
Common.isUnknownType, Common.isBlockDeviceType, Common.isCharacterDeviceType,
42+
Common.isNamedPipeType, Common.isRegularFileType, Common.isDirectoryType,
43+
Common.isSymbolicLinkType, Common.isSocketType, Common.isWhiteoutType,
4144
openDirStream,
4245
readDirStream,
4346
readDirStreamMaybe,
@@ -117,6 +120,9 @@ readDirStreamMaybe = Common.readDirStreamWith
117120
-- structure together with the entry's type (@d_type@) wrapped in a
118121
-- @Just (d_name, d_type)@ if an entry was read and @Nothing@ if
119122
-- the end of the directory stream was reached.
123+
--
124+
-- __Note__: The returned 'DirType' has some limitations; Please see its
125+
-- documentation.
120126
readDirStreamWithType :: Common.DirStream -> IO (Maybe (PosixPath, Common.DirType))
121127
readDirStreamWithType = Common.readDirStreamWith
122128
(\(Common.DirEnt dEnt) -> (,)

0 commit comments

Comments
 (0)