2012-03-22 21:09:54 +00:00
|
|
|
{- 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
|
|
|
|
|
2012-04-13 15:26:39 +00:00
|
|
|
foreign import ccall unsafe "libdiskfree.h diskfree" c_diskfree
|
2012-03-22 21:09:54 +00:00
|
|
|
:: 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
|
2012-03-23 02:49:33 +00:00
|
|
|
, return Nothing
|
2012-03-22 21:09:54 +00:00
|
|
|
)
|
|
|
|
where
|
|
|
|
safeErrno (Errno v) = v == 0
|