on master help sign in
When searching for the containing project, don't stat the same thing twice
commitef45f6bfc072bad41667e31ebdcad981de9e3c66 authorRyan Trinkle <ryan.trinkle@gmail.com> dateMon 1 Jan 2018, 19:49 (9 years ago) parent171789b
1 file changed+10−7
command/src/Obelisk/Command/Project.hs+10−7
command/src/Obelisk/Command/Project.hs+10−7
@@ -17,7 +17,7 @@ import System.Directory
1717import System.Exit
1818import System.FilePath
1919import System.IO
20−import System.Posix (getFileStatus, deviceID, fileID, getRealUserID, fileOwner, fileMode)
20+import System.Posix (FileStatus, getFileStatus, deviceID, fileID, getRealUserID, fileOwner, fileMode)
2121import System.Process
2222import Control.Monad.IO.Class
2323import Control.Monad.Trans.State
@@ -69,12 +69,14 @@ findProjectObeliskCommand target = do
6969 isSecure s = fileOwner s == myUid && fileMode s .&. 0o22 == 0
7070 -- | Get the FilePath to the containing project directory, if there is
7171 -- one; accumulate insecure directories we visited along the way
72− findImpl :: FilePath -> StateT [FilePath] IO (Maybe FilePath)
73− findImpl this = liftIO (doesDirectoryExist this) >>= \case
72+ findImpl :: FilePath -> FileStatus -> StateT [FilePath] IO (Maybe FilePath)
73+ findImpl this thisStat = liftIO (doesDirectoryExist this) >>= \case
7474 -- It's not a directory, so it can't be a project
75− False -> findImpl $ takeDirectory this
75+ False -> do
76+ let dir = takeDirectory this
77+ dirStat <- liftIO $ getFileStatus dir
78+ findImpl dir dirStat
7679 True -> do
77− thisStat <- liftIO $ getFileStatus this
7880 when (not $ isSecure thisStat) $ modify (this:)
7981 liftIO (doesDirectoryExist (this </> ".obelisk")) >>= \case
8082 True -> do
@@ -92,8 +94,9 @@ findProjectObeliskCommand target = do
9294 isSameFileAs = (==) `on` fileIdentity
9395 if thisStat `isSameFileAs` nextStat
9496 then return Nothing -- Found a cycle; probably hit root directory
95− else findImpl next
96− (result, insecurePaths) <- runStateT (findImpl target) []
97+ else findImpl next nextStat
98+ targetStat <- liftIO $ getFileStatus target
99+ (result, insecurePaths) <- runStateT (findImpl target targetStat) []
97100 case (result, insecurePaths) of
98101 (Just projDir, []) -> do
99102 obeliskCommandPkg <- nixBuildDashA (projDir </> ".obelisk" </> "impl") "command"