2012-03-22 21:09:54 +00:00
|
|
|
{- disk free space checking
|
|
|
|
-
|
2014-01-23 02:19:52 +00:00
|
|
|
- Copyright 2012, 2014 Joey Hess <joey@kitenet.net>
|
2012-03-22 21:09:54 +00:00
|
|
|
-
|
2014-05-10 14:01:27 +00:00
|
|
|
- License: BSD-2-clause
|
2012-03-22 21:09:54 +00:00
|
|
|
-}
|
|
|
|
|
2013-03-12 09:48:41 +00:00
|
|
|
{-# LANGUAGE ForeignFunctionInterface, CPP #-}
|
2012-03-22 21:09:54 +00:00
|
|
|
|
2014-01-23 02:19:52 +00:00
|
|
|
module Utility.DiskFree (
|
|
|
|
getDiskFree,
|
|
|
|
getDiskSize
|
|
|
|
) where
|
2012-03-22 21:09:54 +00:00
|
|
|
|
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
|
|
|
|
|
2014-01-23 02:19:52 +00:00
|
|
|
foreign import ccall safe "libdiskfree.h disksize" c_disksize
|
|
|
|
:: CString -> IO CULLong
|
|
|
|
|
|
|
|
getVal :: (CString -> IO CULLong) -> FilePath -> IO (Maybe Integer)
|
|
|
|
getVal getter path = withFilePath path $ \c_path -> do
|
|
|
|
free <- getter c_path
|
2012-03-22 21:09:54 +00:00
|
|
|
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
|
|
|
|
2014-01-23 02:19:52 +00:00
|
|
|
getDiskFree :: FilePath -> IO (Maybe Integer)
|
|
|
|
getDiskFree = getVal c_diskfree
|
|
|
|
|
|
|
|
getDiskSize :: FilePath -> IO (Maybe Integer)
|
|
|
|
getDiskSize = getVal c_disksize
|
|
|
|
|
2013-03-12 09:48:41 +00:00
|
|
|
#else
|
2013-12-10 05:18:04 +00:00
|
|
|
#ifdef mingw32_HOST_OS
|
|
|
|
|
2013-12-10 05:21:39 +00:00
|
|
|
import Common
|
|
|
|
|
2013-12-10 05:18:04 +00:00
|
|
|
import System.Win32.File
|
|
|
|
|
|
|
|
getDiskFree :: FilePath -> IO (Maybe Integer)
|
|
|
|
getDiskFree path = catchMaybeIO $ do
|
|
|
|
(sectors, bytes, nfree, _ntotal) <- getDiskFreeSpace (Just path)
|
|
|
|
return $ toInteger sectors * toInteger bytes * toInteger nfree
|
2014-01-23 02:19:52 +00:00
|
|
|
|
|
|
|
getDiskSize :: FilePath -> IO (Maybe Integer)
|
|
|
|
getDiskSize _ = return Nothing
|
2013-12-10 05:18:04 +00:00
|
|
|
#else
|
2013-03-12 09:48:41 +00:00
|
|
|
|
2013-12-10 05:35:27 +00:00
|
|
|
#warning Building without disk free space checking support
|
|
|
|
|
2013-03-12 09:48:41 +00:00
|
|
|
getDiskFree :: FilePath -> IO (Maybe Integer)
|
|
|
|
getDiskFree _ = return Nothing
|
|
|
|
|
2014-01-23 02:19:52 +00:00
|
|
|
getDiskSize :: FilePath -> IO (Maybe Integer)
|
|
|
|
getDiskSize _ = return Nothing
|
|
|
|
|
2013-03-12 09:48:41 +00:00
|
|
|
#endif
|
2013-12-10 05:18:04 +00:00
|
|
|
#endif
|