From 39bed956d614ade67fbbd4174e013343f8815bb1 Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Sat, 10 Jul 2010 01:02:21 +0000 Subject: [PATCH] webyesod: an edit should not error when the file path is just a filename --- Hledger/Cli/Utils.hs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Hledger/Cli/Utils.hs b/Hledger/Cli/Utils.hs index 02a6f3dbb..0e54b6f63 100644 --- a/Hledger/Cli/Utils.hs +++ b/Hledger/Cli/Utils.hs @@ -123,13 +123,17 @@ writeFileWithBackup f t = backUpFile f >> writeFile f t -- | Back up this file with a (incrementing) numbered suffix, or give an error. backUpFile :: FilePath -> IO () backUpFile fp = do - fs <- getDirectoryContents $ takeDirectory fp + fs <- safeGetDirectoryContents $ takeDirectory $ fp let (d,f) = splitFileName fp versions = catMaybes $ map (f `backupNumber`) fs next = maximum (0:versions) + 1 f' = printf "%s.%d" f next copyFile fp (d f') +safeGetDirectoryContents :: FilePath -> IO [FilePath] +safeGetDirectoryContents "" = getDirectoryContents "." +safeGetDirectoryContents fp = getDirectoryContents fp + -- | Does the second file represent a backup of the first, and if so which version is it ? backupNumber :: FilePath -> FilePath -> Maybe Int backupNumber f g = case matchRegexPR ("^" ++ f ++ "\\.([0-9]+)$") g of