switch from homegrown code to disk-free-space
According to https://github.com/redneb/disk-free-space/issues/3 , disk-free-space should be at least as portable as my homegrown code was. One change I noticed is, getDiskSize was not implemented for windows in the old code, and should work now.
This commit is contained in:
parent
46fe686ba0
commit
a665f92b91
4 changed files with 10 additions and 141 deletions
|
@ -1,70 +1,23 @@
|
|||
{- disk free space checking
|
||||
{- disk free space checking shim
|
||||
-
|
||||
- Copyright 2012, 2014 Joey Hess <id@joeyh.name>
|
||||
- Copyright 2016 Joey Hess <id@joeyh.name>
|
||||
-
|
||||
- License: BSD-2-clause
|
||||
-}
|
||||
|
||||
{-# LANGUAGE ForeignFunctionInterface, CPP #-}
|
||||
{-# OPTIONS_GHC -fno-warn-tabs #-}
|
||||
|
||||
module Utility.DiskFree (
|
||||
getDiskFree,
|
||||
getDiskSize
|
||||
) where
|
||||
|
||||
#ifdef WITH_CLIBS
|
||||
|
||||
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
|
||||
|
||||
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
|
||||
ifM (safeErrno <$> getErrno)
|
||||
( return $ Just $ toInteger free
|
||||
, return Nothing
|
||||
)
|
||||
where
|
||||
safeErrno (Errno v) = v == 0
|
||||
import System.DiskSpace
|
||||
import Utility.Applicative
|
||||
import Utility.Exception
|
||||
|
||||
getDiskFree :: FilePath -> IO (Maybe Integer)
|
||||
getDiskFree = getVal c_diskfree
|
||||
getDiskFree = catchMaybeIO . getAvailSpace
|
||||
|
||||
getDiskSize :: FilePath -> IO (Maybe Integer)
|
||||
getDiskSize = getVal c_disksize
|
||||
|
||||
#else
|
||||
#ifdef mingw32_HOST_OS
|
||||
|
||||
import Common
|
||||
|
||||
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
|
||||
|
||||
getDiskSize :: FilePath -> IO (Maybe Integer)
|
||||
getDiskSize _ = return Nothing
|
||||
#else
|
||||
|
||||
#warning Building without disk free space checking support
|
||||
|
||||
getDiskFree :: FilePath -> IO (Maybe Integer)
|
||||
getDiskFree _ = return Nothing
|
||||
|
||||
getDiskSize :: FilePath -> IO (Maybe Integer)
|
||||
getDiskSize _ = return Nothing
|
||||
|
||||
#endif
|
||||
#endif
|
||||
getDiskSize = fmap diskTotal <$$> catchMaybeIO . getDiskUsage
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue