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.
|
|
|
|
-}
|
|
|
|
|
2013-03-12 09:48:41 +00:00
|
|
|
{-# LANGUAGE ForeignFunctionInterface, CPP #-}
|
2012-03-22 21:09:54 +00:00
|
|
|
|
|
|
|
module Utility.DiskFree ( getDiskFree ) where
|
|
|
|
|
2013-03-13 18:54:52 +00:00
|
|
|
#ifdef WITH_CLIBS
|
|
|
|
|
2012-03-22 21:09:54 +00:00
|
|
|
import Common
|
|
|
|
|
|
|
|
import Foreign.C.Types
|
|
|
|
import Foreign.C.String
|
|
|
|
import Foreign.C.Error
|
|
|
|
|
2012-07-20 19:03:58 +00:00
|
|
|
foreign import ccall safe "libdiskfree.h diskfree" c_diskfree
|
2012-03-22 21:09:54 +00:00
|
|
|
:: CString -> IO CULLong
|
|
|
|
|
2012-04-18 17:23:33 +00:00
|
|
|
getDiskFree :: FilePath -> IO (Maybe Integer)
|
2012-03-22 21:09:54 +00:00
|
|
|
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
|
|
|
)
|
2012-12-13 04:24:19 +00:00
|
|
|
where
|
|
|
|
safeErrno (Errno v) = v == 0
|
2013-03-12 09:48:41 +00:00
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
getDiskFree :: FilePath -> IO (Maybe Integer)
|
|
|
|
getDiskFree _ = return Nothing
|
|
|
|
|
|
|
|
#endif
|