git-annex/Utility/DiskFree.hs
Joey Hess 9d26b532ab use safe FFI imports for diskfree
There's a minor performance overhead to doing this, but this way I don't
have to worry about a situation where statfs might block for a long time.
For example, when it's on a network filesystem.
2012-07-20 15:03:58 -04:00

29 lines
655 B
Haskell

{- 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 safe "libdiskfree.h diskfree" c_diskfree
:: CString -> IO CULLong
getDiskFree :: FilePath -> IO (Maybe Integer)
getDiskFree path = withFilePath path $ \c_path -> do
free <- c_diskfree c_path
ifM (safeErrno <$> getErrno)
( return $ Just $ toInteger free
, return Nothing
)
where
safeErrno (Errno v) = v == 0