add getDiskSize

Couldn't find anything that exposed this for Windows.
This commit is contained in:
Joey Hess 2014-01-22 22:19:52 -04:00
parent ed7c61914c
commit 85aae97b63
2 changed files with 51 additions and 14 deletions

View file

@ -1,13 +1,16 @@
{- disk free space checking {- disk free space checking
- -
- Copyright 2012 Joey Hess <joey@kitenet.net> - Copyright 2012, 2014 Joey Hess <joey@kitenet.net>
- -
- Licensed under the GNU GPL version 3 or higher. - Licensed under the GNU GPL version 3 or higher.
-} -}
{-# LANGUAGE ForeignFunctionInterface, CPP #-} {-# LANGUAGE ForeignFunctionInterface, CPP #-}
module Utility.DiskFree ( getDiskFree ) where module Utility.DiskFree (
getDiskFree,
getDiskSize
) where
#ifdef WITH_CLIBS #ifdef WITH_CLIBS
@ -20,9 +23,12 @@ import Foreign.C.Error
foreign import ccall safe "libdiskfree.h diskfree" c_diskfree foreign import ccall safe "libdiskfree.h diskfree" c_diskfree
:: CString -> IO CULLong :: CString -> IO CULLong
getDiskFree :: FilePath -> IO (Maybe Integer) foreign import ccall safe "libdiskfree.h disksize" c_disksize
getDiskFree path = withFilePath path $ \c_path -> do :: CString -> IO CULLong
free <- c_diskfree c_path
getVal :: (CString -> IO CULLong) -> FilePath -> IO (Maybe Integer)
getVal getter path = withFilePath path $ \c_path -> do
free <- getter c_path
ifM (safeErrno <$> getErrno) ifM (safeErrno <$> getErrno)
( return $ Just $ toInteger free ( return $ Just $ toInteger free
, return Nothing , return Nothing
@ -30,6 +36,12 @@ getDiskFree path = withFilePath path $ \c_path -> do
where where
safeErrno (Errno v) = v == 0 safeErrno (Errno v) = v == 0
getDiskFree :: FilePath -> IO (Maybe Integer)
getDiskFree = getVal c_diskfree
getDiskSize :: FilePath -> IO (Maybe Integer)
getDiskSize = getVal c_disksize
#else #else
#ifdef mingw32_HOST_OS #ifdef mingw32_HOST_OS
@ -41,6 +53,9 @@ getDiskFree :: FilePath -> IO (Maybe Integer)
getDiskFree path = catchMaybeIO $ do getDiskFree path = catchMaybeIO $ do
(sectors, bytes, nfree, _ntotal) <- getDiskFreeSpace (Just path) (sectors, bytes, nfree, _ntotal) <- getDiskFreeSpace (Just path)
return $ toInteger sectors * toInteger bytes * toInteger nfree return $ toInteger sectors * toInteger bytes * toInteger nfree
getDiskSize :: FilePath -> IO (Maybe Integer)
getDiskSize _ = return Nothing
#else #else
#warning Building without disk free space checking support #warning Building without disk free space checking support
@ -48,5 +63,8 @@ getDiskFree path = catchMaybeIO $ do
getDiskFree :: FilePath -> IO (Maybe Integer) getDiskFree :: FilePath -> IO (Maybe Integer)
getDiskFree _ = return Nothing getDiskFree _ = return Nothing
getDiskSize :: FilePath -> IO (Maybe Integer)
getDiskSize _ = return Nothing
#endif #endif
#endif #endif

View file

@ -1,6 +1,6 @@
/* disk free space checking, C mini-library /* disk free space checking, C mini-library
* *
* Copyright 2012 Joey Hess <joey@kitenet.net> * Copyright 2012, 2014 Joey Hess <joey@kitenet.net>
* *
* Licensed under the GNU GPL version 3 or higher. * Licensed under the GNU GPL version 3 or higher.
*/ */
@ -43,16 +43,12 @@
#include <errno.h> #include <errno.h>
#include <stdio.h> #include <stdio.h>
/* Checks the amount of disk that is available to regular (non-root) users. unsigned long long int get(const char *path, int req) {
* (If there's an error, or this is not supported,
* returns 0 and sets errno to nonzero.)
*/
unsigned long long int diskfree(const char *path) {
#ifdef UNKNOWN #ifdef UNKNOWN
errno = 1; errno = 1;
return 0; return 0;
#else #else
unsigned long long int available, blocksize; unsigned long long int v, blocksize;
struct STATSTRUCT buf; struct STATSTRUCT buf;
if (STATCALL(path, &buf) != 0) if (STATCALL(path, &buf) != 0)
@ -60,12 +56,35 @@ unsigned long long int diskfree(const char *path) {
else else
errno = 0; errno = 0;
available = buf.f_bavail; switch (req) {
case 0:
v = buf.f_blocks;
break;
case 1:
v = buf.f_bavail;
break;
default:
v = 0;
}
blocksize = buf.f_bsize; blocksize = buf.f_bsize;
return available * blocksize; return v * blocksize;
#endif #endif
} }
/* Checks the amount of disk that is available to regular (non-root) users.
* (If there's an error, or this is not supported,
* returns 0 and sets errno to nonzero.)
*/
unsigned long long int diskfree(const char *path) {
return get(path, 1);
}
/* Gets the total size of the disk. */
unsigned long long int disksize(const char *path) {
return get(path, 0);
}
/* /*
main () { main () {
printf("%lli\n", diskfree(".")); printf("%lli\n", diskfree("."));