aboutsummaryrefslogtreecommitdiff
path: root/Utility/DiskFree.hs
blob: 2f296e2cb90beed36e567b60d77fdc57aa33d3ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
{- disk free space checking 
 -
 - Copyright 2012, 2014 Joey Hess <joey@kitenet.net>
 -
 - Licensed under the GNU GPL version 3 or higher.
 -}

{-# LANGUAGE ForeignFunctionInterface, CPP #-}

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

getDiskFree :: FilePath -> IO (Maybe Integer)
getDiskFree = getVal c_diskfree

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