[project @ 2001-05-23 09:59:18 by simonmar]
[ghc-hetmet.git] / ghc / compiler / main / Finder.lhs
index 028d056..176a244 100644 (file)
@@ -9,7 +9,8 @@ module Finder (
     findModule,                -- :: ModuleName -> IO (Maybe (Module, ModuleLocation))
     mkHomeModuleLocn,  -- :: ModuleName -> String -> FilePath 
                        --      -> IO ModuleLocation
-    emptyHomeDirCache  -- :: IO ()
+    emptyHomeDirCache, -- :: IO ()
+    flushPackageCache   -- :: [PackageConfig] -> IO ()
   ) where
 
 #include "HsVersions.h"
@@ -26,7 +27,6 @@ import Panic          ( panic )
 import Config
 
 import IOExts
-import Directory
 import List
 import IO
 import Monad
@@ -51,15 +51,16 @@ GLOBAL_VAR(v_HomeDirCache, Nothing, Maybe (FiniteMap String FilePath))
 
 initFinder :: [PackageConfig] -> IO ()
 initFinder pkgs 
-  = do {       -- expunge our home cache
-       ; writeIORef v_HomeDirCache Nothing
-               -- lazilly fill in the package cache
-       ; writeIORef v_PkgDirCache (unsafePerformIO (newPkgCache pkgs))
-       }
+   = do emptyHomeDirCache
+       flushPackageCache pkgs
+
+-- empty, and lazilly fill in the package cache
+flushPackageCache :: [PackageConfig] -> IO ()
+flushPackageCache pkgs = writeIORef v_PkgDirCache 
+                           (unsafePerformIO (newPkgCache pkgs))
 
 emptyHomeDirCache :: IO ()
-emptyHomeDirCache
-   = writeIORef v_HomeDirCache Nothing
+emptyHomeDirCache = writeIORef v_HomeDirCache Nothing
 
 findModule :: ModuleName -> IO (Maybe (Module, ModuleLocation))
 findModule name
@@ -81,10 +82,10 @@ maybeHomeModule mod_name = do
           -- to ["."]).
           home_imports <- readIORef v_Import_paths
           let extendFM fm path = do
-                  contents <- getDirectoryContents' path
+                  contents <- softGetDirectoryContents path
                    let clean_contents = filter isUsefulFile contents
                   return (addListToFM fm (zip clean_contents (repeat path)))
-          home_map <- foldM extendFM emptyFM home_imports
+          home_map <- foldM extendFM emptyFM (reverse home_imports)
           writeIORef v_HomeDirCache (Just home_map)
           return home_map
 
@@ -160,7 +161,7 @@ newPkgCache pkgs = do
            let dirs = import_dirs pkg
                pkg_name = _PK_ (name pkg)
            let addDir fm dir = do
-                   contents <- getDirectoryContents' dir
+                   contents <- softGetDirectoryContents dir
                    return (addListToFM fm (zip contents 
                                               (repeat (pkg_name,dir))))
            foldM addDir fm dirs
@@ -198,12 +199,4 @@ maybePackageModule mod_name = do
 isUsefulFile fn
    = let suffix = (reverse . takeWhile (/= '.') . reverse) fn
      in  suffix `elem` ["hi", "hs", "lhs", "hi-boot", "hi-boot-5"]
-
-getDirectoryContents' d
-   = IO.catch (getDirectoryContents d)
-         (\_ -> do hPutStr stderr 
-                         ("WARNING: error while reading directory " ++ d)
-                   return []
-         )
-
 \end{code}