summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorNicolas Schodet2017-10-19 21:30:11 +0200
committerNicolas Schodet2017-10-19 21:30:11 +0200
commitd9f2eb343edfcdc66cf3f58cc6b7846cc3ca6b35 (patch)
treeaa7e0912ec6256cb72c45563e0fadb30feffaffd /src
parent2d9e17dbcb7494cfcdffee53ccd85fb958d652b5 (diff)
Add isUnpopulated to test for empty directory or file system
Diffstat (limited to 'src')
-rw-r--r--src/Utility/Directory.hs19
1 files changed, 17 insertions, 2 deletions
diff --git a/src/Utility/Directory.hs b/src/Utility/Directory.hs
index 693e7713..86904d63 100644
--- a/src/Utility/Directory.hs
+++ b/src/Utility/Directory.hs
@@ -42,6 +42,10 @@ dirCruft "." = True
dirCruft ".." = True
dirCruft _ = False
+fsCruft :: FilePath -> Bool
+fsCruft "lost+found" = True
+fsCruft d = dirCruft d
+
{- Lists the contents of a directory.
- Unlike getDirectoryContents, paths are not relative to the directory. -}
dirContents :: FilePath -> IO [FilePath]
@@ -236,12 +240,23 @@ readDirectory hdl@(DirectoryHandle _ h fdat mv) = do
-- True only when directory exists and contains nothing.
-- Throws exception if directory does not exist.
isDirectoryEmpty :: FilePath -> IO Bool
-isDirectoryEmpty d = bracket (openDirectory d) closeDirectory check
+isDirectoryEmpty d = testDirectory d dirCruft
+
+-- | True if the directory does not exists or contains nothing, ignoring
+-- "lost+found" which can exists in an empty filesystem.
+isUnpopulated :: FilePath -> IO Bool
+isUnpopulated d = catchDefaultIO True $ testDirectory d fsCruft
+
+-- | Run test on entries found in directory, return False as soon as the
+-- test returns False, else return True. Throws exception if directory does
+-- not exist.
+testDirectory :: FilePath -> (FilePath -> Bool) -> IO Bool
+testDirectory d test = bracket (openDirectory d) closeDirectory check
where
check h = do
v <- readDirectory h
case v of
Nothing -> return True
Just f
- | not (dirCruft f) -> return False
+ | not (test f) -> return False
| otherwise -> check h