aboutsummaryrefslogtreecommitdiff
path: root/Utility/DiskFree.hs
diff options
context:
space:
mode:
authorGravatar Joey Hess <joey@kitenet.net>2012-03-22 17:09:54 -0400
committerGravatar Joey Hess <joey@kitenet.net>2012-03-22 17:32:47 -0400
commite38a839a80ae70eba13b6fd0e7ee08be8a62c513 (patch)
treec6f3faf1df29c0d9ddf7458554661ee4e50c9aed /Utility/DiskFree.hs
parentf1398b558316a936690a8f3b01493f498d15b659 (diff)
Rewrote free disk space checking code
Moving the portability handling into a small C library cleans up things a lot, avoiding the pain of unpacking structs from inside haskell code.
Diffstat (limited to 'Utility/DiskFree.hs')
-rw-r--r--Utility/DiskFree.hs32
1 files changed, 32 insertions, 0 deletions
diff --git a/Utility/DiskFree.hs b/Utility/DiskFree.hs
new file mode 100644
index 000000000..e02794954
--- /dev/null
+++ b/Utility/DiskFree.hs
@@ -0,0 +1,32 @@
+{- disk free space checking
+ -
+ - Copyright 2012 Joey Hess <joey@kitenet.net>
+ -
+ - Licensed under the GNU GPL version 3 or higher.
+ -}
+
+{-# LANGUAGE ForeignFunctionInterface #-}
+
+module Utility.DiskFree ( getDiskFree ) where
+
+import Common
+
+import Foreign.C.Types
+import Foreign.C.String
+import Foreign.C.Error
+
+foreign import ccall unsafe "diskfree.h diskfree" c_diskfree
+ :: CString -> IO CULLong
+
+getDiskFree :: String -> IO (Maybe Integer)
+getDiskFree path = withFilePath path $ \c_path -> do
+ free <- c_diskfree c_path
+ ifM (safeErrno <$> getErrno)
+ ( return $ Just $ toInteger free
+ , do
+ Errno i <- getErrno
+ print i
+ return Nothing
+ )
+ where
+ safeErrno (Errno v) = v == 0