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