summaryrefslogtreecommitdiff
path: root/src/Utility/Directory/TestDirectory.hs
diff options
context:
space:
mode:
authorJoey Hess2019-01-18 12:08:34 -0400
committerJoey Hess2019-01-18 12:08:34 -0400
commit876458adbc41a94c06d68b70e8b7b27288479592 (patch)
treea04d307b7d6eb0644f51af12285742f9b2f726d5 /src/Utility/Directory/TestDirectory.hs
parent80af78c2bbbc1684a1085225b5754f4fcda4cbcb (diff)
parentf42a71ab2653707be4fcdaf1ce0f9fff209d8f12 (diff)
Merge branch 'joeyconfig'
Diffstat (limited to 'src/Utility/Directory/TestDirectory.hs')
-rw-r--r--src/Utility/Directory/TestDirectory.hs40
1 files changed, 40 insertions, 0 deletions
diff --git a/src/Utility/Directory/TestDirectory.hs b/src/Utility/Directory/TestDirectory.hs
new file mode 100644
index 00000000..e1f961b9
--- /dev/null
+++ b/src/Utility/Directory/TestDirectory.hs
@@ -0,0 +1,40 @@
+{- testing properties of directories
+ -
+ - Copyright 2011-2018 Joey Hess <id@joeyh.name>
+ -
+ - License: BSD-2-clause
+ -}
+
+module Utility.Directory.TestDirectory where
+
+import Utility.Directory
+import Utility.Directory.Stream
+import Utility.Exception
+
+-- | True only when directory exists and contains nothing.
+-- Throws exception if directory does not exist.
+isDirectoryEmpty :: FilePath -> IO Bool
+isDirectoryEmpty d = testDirectory d dirCruft
+
+-- | True if the directory does not exist or contains nothing.
+-- Ignores "lost+found" which can exist in an empty filesystem.
+isUnpopulated :: FilePath -> IO Bool
+isUnpopulated d = catchDefaultIO True $ testDirectory d fsCruft
+
+fsCruft :: FilePath -> Bool
+fsCruft "lost+found" = True
+fsCruft d = dirCruft d
+
+-- | 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 (test f) -> return False
+ | otherwise -> check h