Skip to content

Commit

Permalink
Use a library to reimplement the code I fixed in my last commit.
Browse files Browse the repository at this point in the history
The directory reading code had some weird `seq` calls in it -- perhaps
to avoid memory leaks? -- and seems to be equivalent to a few lines
using the 'directory-tree' library. The comments for the old
implementation mentioned it returned an *ordered* list of directory
contents. I don't expect that mattered, or know what "ordered" means
here, but path-based orderings could be recovered by adding a call to
'sort', e.g.

    recurseDirectory path = *sort .* toList . dirTree <$> build path
  • Loading branch information
ntc2 committed Apr 14, 2015
1 parent 8b5c19b commit 0b0514f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 25 deletions.
1 change: 1 addition & 0 deletions jvm-verifier.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ library
, array
, containers
, directory
, directory-tree
, filepath
, haskeline >= 0.7
, jvm-parser >= 0.2.1
Expand Down
33 changes: 8 additions & 25 deletions src/Verifier/Java/Codebase.hs
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,12 @@ module Verifier.Java.Codebase

import Control.Applicative ((<$>))
import Control.Monad
import Data.Foldable (toList)
import qualified Data.Map as M
import Data.IORef
import Data.Maybe
import System.Directory
import System.FilePath
import System.Directory.Tree (build, dirTree)
import System.FilePath (takeExtension)

import Text.PrettyPrint

Expand Down Expand Up @@ -73,30 +74,12 @@ loadCodebase jarFiles classPaths = do
classes <- mapM loadClass classFiles
let cb = foldr addClass (CodebaseState jars M.empty M.empty M.empty) classes
fmap Codebase $ newIORef cb
where
recurseDirectories :: [FilePath] -> IO [FilePath]
recurseDirectories paths = concat <$> mapM recurseDirectory paths

-- Returns in-order listing of all directories and files beneath the given list of paths.
-- WARNING: May return an infinite list when symlinks are encountered.
recurseDirectories :: [FilePath] -> IO [FilePath]
recurseDirectories paths = impl paths []
where impl [] result = return (reverse result)
impl (path : rest) result = do
subpaths <- getSubdirectories path
seq (length subpaths) $ impl (subpaths ++ rest) (path : result)
-- Returns directories and files directly beneath path if any.
getSubdirectories :: FilePath -> IO [FilePath]
getSubdirectories path = do
exists <- doesDirectoryExist path
if exists
then do
p <- getPermissions path
if readable p
then do contents <- getDirectoryContents path
return
$ map (path </>)
$ filter (\path' -> path' `seq` (path' /= "." && path' /= ".."))
$ contents
else return []
else return []
recurseDirectory :: FilePath -> IO [FilePath]
recurseDirectory path = toList . dirTree <$> build path

-- | Register a class with the given codebase
addClass :: Class -> CodebaseState -> CodebaseState
Expand Down

0 comments on commit 0b0514f

Please sign in to comment.