LIB: use posix-path traversal functions for getDirsFiles
This should also speed up reading.
This commit is contained in:
parent
339cfe1e0b
commit
593a59787f
@ -27,13 +27,12 @@ module HSFM.FileSystem.FileType where
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
import Control.Exception
|
|
||||||
(
|
|
||||||
bracket
|
|
||||||
)
|
|
||||||
import Data.ByteString(ByteString)
|
import Data.ByteString(ByteString)
|
||||||
import qualified Data.ByteString as B
|
|
||||||
import Data.Default
|
import Data.Default
|
||||||
|
import Data.Maybe
|
||||||
|
(
|
||||||
|
catMaybes
|
||||||
|
)
|
||||||
import Data.Time.Clock.POSIX
|
import Data.Time.Clock.POSIX
|
||||||
(
|
(
|
||||||
POSIXTime
|
POSIXTime
|
||||||
@ -48,6 +47,7 @@ import HPath
|
|||||||
(
|
(
|
||||||
Abs
|
Abs
|
||||||
, Path
|
, Path
|
||||||
|
, Fn
|
||||||
)
|
)
|
||||||
import qualified HPath as P
|
import qualified HPath as P
|
||||||
import HSFM.FileSystem.Errors
|
import HSFM.FileSystem.Errors
|
||||||
@ -58,12 +58,15 @@ import System.IO.Error
|
|||||||
ioeGetErrorType
|
ioeGetErrorType
|
||||||
, isDoesNotExistErrorType
|
, isDoesNotExistErrorType
|
||||||
)
|
)
|
||||||
import qualified System.Posix.Directory.ByteString as PFD
|
|
||||||
import System.Posix.FilePath
|
import System.Posix.FilePath
|
||||||
(
|
(
|
||||||
(</>)
|
(</>)
|
||||||
)
|
)
|
||||||
import System.Posix.Directory.Traversals (realpath)
|
import System.Posix.Directory.Traversals
|
||||||
|
(
|
||||||
|
getDirectoryContents
|
||||||
|
, realpath
|
||||||
|
)
|
||||||
import qualified System.Posix.Files.ByteString as PF
|
import qualified System.Posix.Files.ByteString as PF
|
||||||
import System.Posix.Types
|
import System.Posix.Types
|
||||||
(
|
(
|
||||||
@ -463,21 +466,13 @@ getDirsFiles :: Path Abs -- ^ dir to read
|
|||||||
-> IO [Path Abs]
|
-> IO [Path Abs]
|
||||||
getDirsFiles fp =
|
getDirsFiles fp =
|
||||||
rethrowErrnoAs [eACCES] (Can'tOpenDirectory . P.fromAbs $ fp)
|
rethrowErrnoAs [eACCES] (Can'tOpenDirectory . P.fromAbs $ fp)
|
||||||
$ bracket (PFD.openDirStream . P.toFilePath $ fp)
|
$ return
|
||||||
PFD.closeDirStream
|
. catMaybes
|
||||||
$ \dirstream ->
|
. fmap (\x -> (P.</>) fp <$> (parseMaybe . snd $ x))
|
||||||
let mdirs :: [Path Abs] -> IO [Path Abs]
|
=<< getDirectoryContents (P.toFilePath fp)
|
||||||
mdirs dirs = do
|
where
|
||||||
-- make sure we close the directory stream in case of errors
|
parseMaybe :: ByteString -> Maybe (Path Fn)
|
||||||
-- TODO: more explicit error handling?
|
parseMaybe = P.parseFn
|
||||||
-- both the parsing and readin the stream can fail!
|
|
||||||
dir <- PFD.readDirStream dirstream
|
|
||||||
if B.null dir
|
|
||||||
then return dirs
|
|
||||||
else mdirs $ maybe dirs
|
|
||||||
(\x -> fp P.</> x : dirs)
|
|
||||||
(P.parseFn dir)
|
|
||||||
in mdirs []
|
|
||||||
|
|
||||||
|
|
||||||
-- |Gets all file information.
|
-- |Gets all file information.
|
||||||
|
Loading…
Reference in New Issue
Block a user