aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorSean Whitton <spwhitton@spwhitton.name>2015-04-04 21:52:19 +0900
committerSean Whitton <spwhitton@spwhitton.name>2015-04-04 21:52:19 +0900
commit7545fa7b81876fdbb174d529690eec59349f072c (patch)
tree43171d55cf2999a97af4a96c946f2a192cd675c5
parent7df97884eea1b19d7cef9e32b5e2787d0ea5bed7 (diff)
downloadsrem-7545fa7b81876fdbb174d529690eec59349f072c.tar.gz
getNewestRealFile function
-rw-r--r--src/Utility/Notify/Posix.hs25
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