diff options
author | Sean Whitton <spwhitton@spwhitton.name> | 2015-04-04 21:52:19 +0900 |
---|---|---|
committer | Sean Whitton <spwhitton@spwhitton.name> | 2015-04-04 21:52:19 +0900 |
commit | 7545fa7b81876fdbb174d529690eec59349f072c (patch) | |
tree | 43171d55cf2999a97af4a96c946f2a192cd675c5 | |
parent | 7df97884eea1b19d7cef9e32b5e2787d0ea5bed7 (diff) | |
download | srem-7545fa7b81876fdbb174d529690eec59349f072c.tar.gz |
getNewestRealFile function
-rw-r--r-- | src/Utility/Notify/Posix.hs | 25 |
1 files 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 |