From a665f92b91cf970e5af01cca94b1971df80bec2d Mon Sep 17 00:00:00 2001 From: Joey Hess Date: Mon, 15 Feb 2016 11:29:27 -0400 Subject: [PATCH] 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. --- Utility/DiskFree.hs | 63 +++++--------------------------- Utility/libdiskfree.c | 84 ------------------------------------------- Utility/libdiskfree.h | 1 - git-annex.cabal | 3 +- 4 files changed, 10 insertions(+), 141 deletions(-) delete mode 100644 Utility/libdiskfree.c delete mode 100644 Utility/libdiskfree.h diff --git a/Utility/DiskFree.hs b/Utility/DiskFree.hs index c4125d4f00..fe3a4577c1 100644 --- a/Utility/DiskFree.hs +++ b/Utility/DiskFree.hs @@ -1,70 +1,23 @@ -{- disk free space checking +{- disk free space checking shim - - - Copyright 2012, 2014 Joey Hess + - Copyright 2016 Joey Hess - - 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 diff --git a/Utility/libdiskfree.c b/Utility/libdiskfree.c deleted file mode 100644 index a682bb3bd3..0000000000 --- a/Utility/libdiskfree.c +++ /dev/null @@ -1,84 +0,0 @@ -/* disk free space checking, C mini-library - * - * Copyright 2012, 2014 Joey Hess - * - * License: BSD-2-clause - */ - -/* Include appropriate headers for the OS, and define what will be used to - * check the free space. */ -#if defined (__FreeBSD__) -# include -# include -# define STATCALL statfs /* statfs64 not yet tested on a real FreeBSD machine */ -# define STATSTRUCT statfs -# define BSIZE f_bsize -#else -#if defined __ANDROID__ -# warning free space checking code not available for Android -# define UNKNOWN -#else -#if defined (__linux__) || defined (__APPLE__) || defined (__FreeBSD_kernel__) || (defined (__SVR4) && defined (__sun)) -/* Linux or OSX or Debian kFreeBSD or Solaris */ -/* This is a POSIX standard, so might also work elsewhere too. */ -# include -# define STATCALL statvfs -# define STATSTRUCT statvfs -# define BSIZE f_frsize -#else -# warning free space checking code not available for this OS -# define UNKNOWN -#endif -#endif -#endif - -#include -#include - -unsigned long long int get(const char *path, int req) { -#ifdef UNKNOWN - errno = 1; - return 0; -#else - unsigned long long int v, blocksize; - struct STATSTRUCT buf; - - if (STATCALL(path, &buf) != 0) - return 0; /* errno is set */ - else - errno = 0; - - switch (req) { - case 0: - v = buf.f_blocks; - break; - case 1: - v = buf.f_bavail; - break; - default: - v = 0; - } - - blocksize = buf.BSIZE; - return v * blocksize; -#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 () { - printf("%lli\n", diskfree(".")); -} -*/ diff --git a/Utility/libdiskfree.h b/Utility/libdiskfree.h deleted file mode 100644 index e5b84754fe..0000000000 --- a/Utility/libdiskfree.h +++ /dev/null @@ -1 +0,0 @@ -unsigned long long int diskfree(const char *path); diff --git a/git-annex.cabal b/git-annex.cabal index 4edbd21c3b..8e6efbf592 100644 --- a/git-annex.cabal +++ b/git-annex.cabal @@ -111,7 +111,8 @@ Executable git-annex esqueleto, persistent-sqlite, persistent, persistent-template, aeson, feed, - regex-tdfa + regex-tdfa, + disk-free-space CC-Options: -Wall GHC-Options: -Wall -fno-warn-tabs Extensions: PackageImports