{-# LINE 1 "src/Xmobar/System/StatFS.hsc" #-}
-----------------------------------------------------------------------------
-- |
-- Module      :  StatFS
-- Copyright   :  (c) Jose A Ortega Ruiz
-- License     :  BSD-style (see LICENSE)
--
-- Maintainer  :  Jose A Ortega Ruiz <jao@gnu.org>
-- Stability   :  unstable
-- Portability :  unportable
--
--  A binding to C's statvfs(2)
--
-----------------------------------------------------------------------------

{-# LANGUAGE CPP, ForeignFunctionInterface, EmptyDataDecls #-}


module Xmobar.System.StatFS ( FileSystemStats(..), getFileSystemStats ) where

import Foreign
import Foreign.C.Types
import Foreign.C.String
import Data.ByteString (useAsCString)
import Data.ByteString.Char8 (pack)


{-# LINE 29 "src/Xmobar/System/StatFS.hsc" #-}


{-# LINE 34 "src/Xmobar/System/StatFS.hsc" #-}


{-# LINE 36 "src/Xmobar/System/StatFS.hsc" #-}

data FileSystemStats = FileSystemStats {
  FileSystemStats -> Integer
fsStatBlockSize :: Integer
  -- ^ Optimal transfer block size.
  , FileSystemStats -> Integer
fsStatBlockCount :: Integer
  -- ^ Total data blocks in file system.
  , FileSystemStats -> Integer
fsStatByteCount :: Integer
  -- ^ Total bytes in file system.
  , FileSystemStats -> Integer
fsStatBytesFree :: Integer
  -- ^ Free bytes in file system.
  , FileSystemStats -> Integer
fsStatBytesAvailable :: Integer
  -- ^ Free bytes available to non-superusers.
  , FileSystemStats -> Integer
fsStatBytesUsed :: Integer
  -- ^ Bytes used.
  } deriving (Int -> FileSystemStats -> ShowS
[FileSystemStats] -> ShowS
FileSystemStats -> String
(Int -> FileSystemStats -> ShowS)
-> (FileSystemStats -> String)
-> ([FileSystemStats] -> ShowS)
-> Show FileSystemStats
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [FileSystemStats] -> ShowS
$cshowList :: [FileSystemStats] -> ShowS
show :: FileSystemStats -> String
$cshow :: FileSystemStats -> String
showsPrec :: Int -> FileSystemStats -> ShowS
$cshowsPrec :: Int -> FileSystemStats -> ShowS
Show, FileSystemStats -> FileSystemStats -> Bool
(FileSystemStats -> FileSystemStats -> Bool)
-> (FileSystemStats -> FileSystemStats -> Bool)
-> Eq FileSystemStats
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: FileSystemStats -> FileSystemStats -> Bool
$c/= :: FileSystemStats -> FileSystemStats -> Bool
== :: FileSystemStats -> FileSystemStats -> Bool
$c== :: FileSystemStats -> FileSystemStats -> Bool
Eq)

data CStatfs


{-# LINE 57 "src/Xmobar/System/StatFS.hsc" #-}
foreign import ccall unsafe "sys/vfs.h statvfs"

{-# LINE 59 "src/Xmobar/System/StatFS.hsc" #-}
  c_statfs :: CString -> Ptr CStatfs -> IO CInt

toI :: CULong -> Integer
toI :: CULong -> Integer
toI = CULong -> Integer
forall a. Integral a => a -> Integer
toInteger

getFileSystemStats :: String -> IO (Maybe FileSystemStats)
getFileSystemStats :: String -> IO (Maybe FileSystemStats)
getFileSystemStats String
path =
  Int
-> (Ptr CStatfs -> IO (Maybe FileSystemStats))
-> IO (Maybe FileSystemStats)
forall a b. Int -> (Ptr a -> IO b) -> IO b
allocaBytes ((Int
120)) ((Ptr CStatfs -> IO (Maybe FileSystemStats))
 -> IO (Maybe FileSystemStats))
-> (Ptr CStatfs -> IO (Maybe FileSystemStats))
-> IO (Maybe FileSystemStats)
forall a b. (a -> b) -> a -> b
$ \Ptr CStatfs
vfs ->
{-# LINE 67 "src/Xmobar/System/StatFS.hsc" #-}
  useAsCString (pack path) $ \cpath -> do
    res <- c_statfs cpath vfs
    if res /= 0 then return Nothing
      else do
        bsize <- ((\hsc_ptr -> peekByteOff hsc_ptr 8)) vfs
{-# LINE 72 "src/Xmobar/System/StatFS.hsc" #-}
        bcount <- ((\hsc_ptr -> peekByteOff hsc_ptr 16)) vfs
{-# LINE 73 "src/Xmobar/System/StatFS.hsc" #-}
        bfree <- ((\hsc_ptr -> peekByteOff hsc_ptr 24)) vfs
{-# LINE 74 "src/Xmobar/System/StatFS.hsc" #-}
        bavail <- ((\hsc_ptr -> peekByteOff hsc_ptr 32)) vfs
{-# LINE 75 "src/Xmobar/System/StatFS.hsc" #-}
        let bpb = toI bsize
        return $ Just FileSystemStats
                       { fsStatBlockSize = bpb
                       , fsStatBlockCount = toI bcount
                       , fsStatByteCount = toI bcount * bpb
                       , fsStatBytesFree = toI bfree * bpb
                       , fsStatBytesAvailable = toI bavail * bpb
                       , fsStatBytesUsed = toI (bcount - bfree) * bpb
                       }