From 7545fa7b81876fdbb174d529690eec59349f072c Mon Sep 17 00:00:00 2001 From: Sean Whitton Date: Sat, 4 Apr 2015 21:52:19 +0900 Subject: getNewestRealFile function --- src/Utility/Notify/Posix.hs | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/src/Utility/Notify/Posix.hs b/src/Utility/Notify/Posix.hs index 2bfac71..16b42ed 100644 --- a/src/Utility/Notify/Posix.hs +++ b/src/Utility/Notify/Posix.hs @@ -30,7 +30,7 @@ import Data.Function (on) import Data.List (sortBy) import Data.Time.Clock (UTCTime) import System.Directory (getDirectoryContents, getHomeDirectory, - getModificationTime) + getModificationTime, doesDirectoryExist) import System.Environment (getEnvironment, setEnv) import System.FilePath (()) import System.Process (readProcess, runCommand) @@ -73,12 +73,27 @@ getDBusUserAddress = do addr <- readFile socketFile return (return addr >>= parseAddress) --- | Return the newest file in a directory, or the empty string if --- passed a file or empty directory. +-- | Return the full path to the newest file in a directory, or the +-- empty string if passed a file or empty directory or a path that +-- doesn't exist. getNewestRealFile :: FilePath -> IO FilePath getNewestRealFile dir = do - - undefined + doesDirectoryExist dir >>= \exists -> + if exists then do + contents <- map (dir ). filter (`notElem` [".", ".."]) + <$> getDirectoryContents dir + contentsWithModTimes <- foldM step [] contents + let sorted = sortBy + (flip $ (compare `on` snd)) + contentsWithModTimes + if null sorted + then return "" + else return . fst . head $ sorted + else return "" + where + step pairs file = do + fileModTime <- getModificationTime file + return $ (file, fileModTime) : pairs -- newestFile :: [FilePath] -> IO FilePath -- newestFile xs = do -- cgit v1.2.3