Skip to content

Commit c011c2f

Browse files
mmhathasufell
authored andcommitted
Moved changes to System.Posix.Directory.Internals
1 parent 51f072c commit c011c2f

File tree

7 files changed

+66
-194
lines changed

7 files changed

+66
-194
lines changed

System/Posix/Directory.hsc

+3-51
Original file line numberDiff line numberDiff line change
@@ -28,26 +28,10 @@ module System.Posix.Directory (
2828
createDirectory, removeDirectory,
2929

3030
-- * Reading directories
31-
DirStream, DirStreamWithPath,
32-
fromDirStreamWithPath,
33-
DirType( UnknownType
34-
, NamedPipeType
35-
, CharacterDeviceType
36-
, DirectoryType
37-
, BlockDeviceType
38-
, RegularFileType
39-
, SymbolicLinkType
40-
, SocketType
41-
, WhiteoutType
42-
),
43-
isUnknownType, isBlockDeviceType, isCharacterDeviceType, isNamedPipeType,
44-
isRegularFileType, isDirectoryType, isSymbolicLinkType, isSocketType,
45-
isWhiteoutType,
31+
DirStream,
4632
openDirStream,
47-
openDirStreamWithPath,
4833
readDirStream,
4934
readDirStreamMaybe,
50-
readDirStreamWithType,
5135
rewindDirStream,
5236
closeDirStream,
5337
DirStreamOffset,
@@ -64,15 +48,14 @@ module System.Posix.Directory (
6448
changeWorkingDirectoryFd,
6549
) where
6650

51+
import Control.Monad ((>=>))
6752
import Data.Maybe
68-
import System.FilePath ((</>))
6953
import System.Posix.Error
7054
import System.Posix.Types
7155
import Foreign
7256
import Foreign.C
7357

7458
import System.Posix.Directory.Common
75-
import System.Posix.Files
7659
import System.Posix.Internals (withFilePath, peekFilePath)
7760

7861
-- | @createDirectory dir mode@ calls @mkdir@ to
@@ -96,11 +79,6 @@ openDirStream name =
9679
dirp <- throwErrnoPathIfNullRetry "openDirStream" name $ c_opendir s
9780
return (DirStream dirp)
9881

99-
-- | A version of 'openDirStream' where the path of the directory is stored in
100-
-- the returned 'DirStreamWithPath'.
101-
openDirStreamWithPath :: FilePath -> IO (DirStreamWithPath FilePath)
102-
openDirStreamWithPath name = toDirStreamWithPath name <$> openDirStream name
103-
10482
foreign import capi unsafe "HsUnix.h opendir"
10583
c_opendir :: CString -> IO (Ptr CDir)
10684

@@ -121,33 +99,7 @@ readDirStream = fmap (fromMaybe "") . readDirStreamMaybe
12199
-- structure wrapped in a @Just d_name@ if an entry was read and @Nothing@ if
122100
-- the end of the directory stream was reached.
123101
readDirStreamMaybe :: DirStream -> IO (Maybe FilePath)
124-
readDirStreamMaybe = readDirStreamWith
125-
(\(DirEnt dEnt) -> d_name dEnt >>= peekFilePath)
126-
127-
-- | @readDirStreamWithType dp@ calls @readdir@ to obtain the
128-
-- next directory entry (@struct dirent@) for the open directory
129-
-- stream @dp@. It returns the @d_name@ member of that
130-
-- structure together with the entry's type (@d_type@) wrapped in a
131-
-- @Just (d_name, d_type)@ if an entry was read and @Nothing@ if
132-
-- the end of the directory stream was reached.
133-
--
134-
-- __Note__: The returned 'DirType' has some limitations; Please see its
135-
-- documentation.
136-
readDirStreamWithType :: DirStreamWithPath FilePath -> IO (Maybe (FilePath, DirType))
137-
readDirStreamWithType (DirStreamWithPath (base, ptr)) = readDirStreamWith
138-
(\(DirEnt dEnt) -> do
139-
name <- d_name dEnt >>= peekFilePath
140-
let getStat = getFileStatus (base </> name)
141-
dtype <- d_type dEnt >>= getRealDirType getStat . DirType
142-
return (name, dtype)
143-
)
144-
(DirStream ptr)
145-
146-
foreign import ccall unsafe "__hscore_d_name"
147-
d_name :: Ptr CDirent -> IO CString
148-
149-
foreign import ccall unsafe "__hscore_d_type"
150-
d_type :: Ptr CDirent -> IO CChar
102+
readDirStreamMaybe = readDirStreamWith (dirEntName >=> peekFilePath)
151103

152104

153105
-- | @getWorkingDirectory@ calls @getcwd@ to obtain the name

System/Posix/Directory/ByteString.hsc

+3-54
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
{-# LANGUAGE CApiFFI #-}
22
{-# LANGUAGE NondecreasingIndentation #-}
3-
{-# LANGUAGE OverloadedStrings #-}
43
{-# LANGUAGE Safe #-}
54

65
-----------------------------------------------------------------------------
@@ -29,26 +28,10 @@ module System.Posix.Directory.ByteString (
2928
createDirectory, removeDirectory,
3029

3130
-- * Reading directories
32-
DirStream, DirStreamWithPath,
33-
fromDirStreamWithPath,
34-
DirType( UnknownType
35-
, NamedPipeType
36-
, CharacterDeviceType
37-
, DirectoryType
38-
, BlockDeviceType
39-
, RegularFileType
40-
, SymbolicLinkType
41-
, SocketType
42-
, WhiteoutType
43-
),
44-
isUnknownType, isBlockDeviceType, isCharacterDeviceType, isNamedPipeType,
45-
isRegularFileType, isDirectoryType, isSymbolicLinkType, isSocketType,
46-
isWhiteoutType,
31+
DirStream,
4732
openDirStream,
48-
openDirStreamWithPath,
4933
readDirStream,
5034
readDirStreamMaybe,
51-
readDirStreamWithType,
5235
rewindDirStream,
5336
closeDirStream,
5437
DirStreamOffset,
@@ -65,18 +48,15 @@ module System.Posix.Directory.ByteString (
6548
changeWorkingDirectoryFd,
6649
) where
6750

51+
import Control.Monad ((>=>))
6852
import Data.Maybe
6953
import System.Posix.Types
7054
import Foreign
7155
import Foreign.C
7256

7357
import Data.ByteString.Char8 as BC
74-
#if !MIN_VERSION_base(4,11,0)
75-
import Data.Monoid ((<>))
76-
#endif
7758

7859
import System.Posix.Directory.Common
79-
import System.Posix.Files.ByteString
8060
import System.Posix.ByteString.FilePath
8161

8262
-- | @createDirectory dir mode@ calls @mkdir@ to
@@ -100,11 +80,6 @@ openDirStream name =
10080
dirp <- throwErrnoPathIfNullRetry "openDirStream" name $ c_opendir s
10181
return (DirStream dirp)
10282

103-
-- | A version of 'openDirStream' where the path of the directory is stored in
104-
-- the returned 'DirStreamWithPath'.
105-
openDirStreamWithPath :: RawFilePath -> IO (DirStreamWithPath RawFilePath)
106-
openDirStreamWithPath name = toDirStreamWithPath name <$> openDirStream name
107-
10883
foreign import capi unsafe "HsUnix.h opendir"
10984
c_opendir :: CString -> IO (Ptr CDir)
11085

@@ -125,33 +100,7 @@ readDirStream = fmap (fromMaybe BC.empty) . readDirStreamMaybe
125100
-- structure wrapped in a @Just d_name@ if an entry was read and @Nothing@ if
126101
-- the end of the directory stream was reached.
127102
readDirStreamMaybe :: DirStream -> IO (Maybe RawFilePath)
128-
readDirStreamMaybe = readDirStreamWith
129-
(\(DirEnt dEnt) -> d_name dEnt >>= peekFilePath)
130-
131-
-- | @readDirStreamWithType dp@ calls @readdir@ to obtain the
132-
-- next directory entry (@struct dirent@) for the open directory
133-
-- stream @dp@. It returns the @d_name@ member of that
134-
-- structure together with the entry's type (@d_type@) wrapped in a
135-
-- @Just (d_name, d_type)@ if an entry was read and @Nothing@ if
136-
-- the end of the directory stream was reached.
137-
--
138-
-- __Note__: The returned 'DirType' has some limitations; Please see its
139-
-- documentation.
140-
readDirStreamWithType :: DirStreamWithPath RawFilePath -> IO (Maybe (RawFilePath, DirType))
141-
readDirStreamWithType (DirStreamWithPath (base, ptr)) = readDirStreamWith
142-
(\(DirEnt dEnt) -> do
143-
name <- d_name dEnt >>= peekFilePath
144-
let getStat = getFileStatus (base <> "/" <> name)
145-
dtype <- d_type dEnt >>= getRealDirType getStat . DirType
146-
return (name, dtype)
147-
)
148-
(DirStream ptr)
149-
150-
foreign import ccall unsafe "__hscore_d_name"
151-
d_name :: Ptr CDirent -> IO CString
152-
153-
foreign import ccall unsafe "__hscore_d_type"
154-
d_type :: Ptr CDirent -> IO CChar
103+
readDirStreamMaybe = readDirStreamWith (dirEntName >=> peekFilePath)
155104

156105

157106
-- | @getWorkingDirectory@ calls @getcwd@ to obtain the name

System/Posix/Directory/Common.hsc

+24-3
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,16 @@
1919
##include "HsUnixConfig.h"
2020

2121
module System.Posix.Directory.Common (
22-
DirStream(..), DirStreamWithPath(..),
23-
fromDirStreamWithPath, toDirStreamWithPath,
24-
DirEnt(..), CDir, CDirent, DirStreamOffset(..),
22+
DirStream(..),
23+
CDir,
24+
DirStreamWithPath(..),
25+
fromDirStreamWithPath,
26+
toDirStreamWithPath,
27+
28+
DirEnt(..),
29+
CDirent,
30+
dirEntName,
31+
dirEntType,
2532
DirType( DirType
2633
, UnknownType
2734
, NamedPipeType
@@ -40,6 +47,8 @@ module System.Posix.Directory.Common (
4047
unsafeOpenDirStreamFd,
4148
readDirStreamWith,
4249
readDirStreamWithPtr,
50+
51+
DirStreamOffset(..),
4352
rewindDirStream,
4453
closeDirStream,
4554
#ifdef HAVE_SEEKDIR
@@ -282,6 +291,18 @@ readDirStreamWithPtr ptr_dEnt f dstream@(DirStream dirp) = do
282291
then return Nothing
283292
else throwErrno "readDirStream"
284293

294+
dirEntName :: DirEnt -> IO CString
295+
dirEntName (DirEnt dEntPtr) = d_name dEntPtr
296+
297+
foreign import ccall unsafe "__hscore_d_name"
298+
d_name :: Ptr CDirent -> IO CString
299+
300+
dirEntType :: DirEnt -> IO DirType
301+
dirEntType (DirEnt dEntPtr) = DirType <$> d_type dEntPtr
302+
303+
foreign import ccall unsafe "__hscore_d_type"
304+
d_type :: Ptr CDirent -> IO CChar
305+
285306
-- traversing directories
286307
foreign import ccall unsafe "__hscore_readdir"
287308
c_readdir :: Ptr CDir -> Ptr (Ptr CDirent) -> IO CInt

System/Posix/Directory/Internals.hsc

+33-3
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,39 @@
1313
-----------------------------------------------------------------------------
1414

1515
module System.Posix.Directory.Internals (
16-
DirStream(..), DirEnt(..), DirType(..), CDir, CDirent, DirStreamOffset(..),
17-
readDirStreamWith,
18-
readDirStreamWithPtr,
16+
DirStream(..),
17+
CDir,
18+
DirStreamWithPath(..),
19+
fromDirStreamWithPath,
20+
toDirStreamWithPath,
21+
DirEnt(..),
22+
CDirent,
23+
dirEntName,
24+
dirEntType,
25+
DirType( DirType
26+
, UnknownType
27+
, NamedPipeType
28+
, CharacterDeviceType
29+
, DirectoryType
30+
, BlockDeviceType
31+
, RegularFileType
32+
, SymbolicLinkType
33+
, SocketType
34+
, WhiteoutType
35+
),
36+
isUnknownType,
37+
isNamedPipeType,
38+
isCharacterDeviceType,
39+
isDirectoryType,
40+
isBlockDeviceType,
41+
isRegularFileType,
42+
isSymbolicLinkType,
43+
isSocketType,
44+
isWhiteoutType,
45+
getRealDirType,
46+
readDirStreamWith,
47+
readDirStreamWithPtr,
48+
DirStreamOffset(..),
1949
) where
2050

2151
import System.Posix.Directory.Common

System/Posix/Directory/PosixPath.hsc

+3-49
Original file line numberDiff line numberDiff line change
@@ -27,26 +27,10 @@ module System.Posix.Directory.PosixPath (
2727
createDirectory, removeDirectory,
2828

2929
-- * Reading directories
30-
Common.DirStream, Common.DirStreamWithPath,
31-
Common.fromDirStreamWithPath,
32-
Common.DirType( UnknownType
33-
, NamedPipeType
34-
, CharacterDeviceType
35-
, DirectoryType
36-
, BlockDeviceType
37-
, RegularFileType
38-
, SymbolicLinkType
39-
, SocketType
40-
, WhiteoutType
41-
),
42-
Common.isUnknownType, Common.isBlockDeviceType, Common.isCharacterDeviceType,
43-
Common.isNamedPipeType, Common.isRegularFileType, Common.isDirectoryType,
44-
Common.isSymbolicLinkType, Common.isSocketType, Common.isWhiteoutType,
30+
Common.DirStream,
4531
openDirStream,
46-
openDirStreamWithPath,
4732
readDirStream,
4833
readDirStreamMaybe,
49-
readDirStreamWithType,
5034
Common.rewindDirStream,
5135
Common.closeDirStream,
5236
Common.DirStreamOffset,
@@ -63,14 +47,14 @@ module System.Posix.Directory.PosixPath (
6347
Common.changeWorkingDirectoryFd,
6448
) where
6549

50+
import Control.Monad ((>=>))
6651
import Data.Maybe
6752
import System.Posix.Types
6853
import Foreign
6954
import Foreign.C
7055

7156
import System.OsPath.Posix
7257
import qualified System.Posix.Directory.Common as Common
73-
import System.Posix.Files.PosixString
7458
import System.Posix.PosixPath.FilePath
7559

7660
-- | @createDirectory dir mode@ calls @mkdir@ to
@@ -94,11 +78,6 @@ openDirStream name =
9478
dirp <- throwErrnoPathIfNullRetry "openDirStream" name $ c_opendir s
9579
return (Common.DirStream dirp)
9680

97-
-- | A version of 'openDirStream' where the path of the directory is stored in
98-
-- the returned 'DirStreamWithPath'.
99-
openDirStreamWithPath :: PosixPath -> IO (Common.DirStreamWithPath PosixPath)
100-
openDirStreamWithPath name = Common.toDirStreamWithPath name <$> openDirStream name
101-
10281
foreign import capi unsafe "HsUnix.h opendir"
10382
c_opendir :: CString -> IO (Ptr Common.CDir)
10483

@@ -120,32 +99,7 @@ readDirStream = fmap (fromMaybe mempty) . readDirStreamMaybe
12099
-- the end of the directory stream was reached.
121100
readDirStreamMaybe :: Common.DirStream -> IO (Maybe PosixPath)
122101
readDirStreamMaybe = Common.readDirStreamWith
123-
(\(Common.DirEnt dEnt) -> d_name dEnt >>= peekFilePath)
124-
125-
-- | @readDirStreamWithType dp@ calls @readdir@ to obtain the
126-
-- next directory entry (@struct dirent@) for the open directory
127-
-- stream @dp@. It returns the @d_name@ member of that
128-
-- structure together with the entry's type (@d_type@) wrapped in a
129-
-- @Just (d_name, d_type)@ if an entry was read and @Nothing@ if
130-
-- the end of the directory stream was reached.
131-
--
132-
-- __Note__: The returned 'DirType' has some limitations; Please see its
133-
-- documentation.
134-
readDirStreamWithType :: Common.DirStreamWithPath PosixPath -> IO (Maybe (PosixPath, Common.DirType))
135-
readDirStreamWithType (Common.DirStreamWithPath (base, ptr))= Common.readDirStreamWith
136-
(\(Common.DirEnt dEnt) -> do
137-
name <- d_name dEnt >>= peekFilePath
138-
let getStat = getFileStatus (base </> name)
139-
dtype <- d_type dEnt >>= Common.getRealDirType getStat . Common.DirType
140-
return (name, dtype)
141-
)
142-
(Common.DirStream ptr)
143-
144-
foreign import ccall unsafe "__hscore_d_name"
145-
d_name :: Ptr Common.CDirent -> IO CString
146-
147-
foreign import ccall unsafe "__hscore_d_type"
148-
d_type :: Ptr Common.CDirent -> IO CChar
102+
(Common.dirEntName >=> peekFilePath)
149103

150104

151105
-- | @getWorkingDirectory@ calls @getcwd@ to obtain the name

0 commit comments

Comments
 (0)