summaryrefslogtreecommitdiffhomepage
path: root/Plugins/Monitors/StatFS.hsc
diff options
context:
space:
mode:
authorJose A Ortega Ruiz <jao@gnu.org>2010-02-04 02:54:55 +0100
committerJose A Ortega Ruiz <jao@gnu.org>2010-02-04 02:54:55 +0100
commit6c4970ea293796a832ecd1b292ec99a9695bf276 (patch)
treeaf6ae522a14cfb75ef2434953ad87be17ba5fee9 /Plugins/Monitors/StatFS.hsc
parentcf25ae10fa019378c5aa5c7381275bd475c11ed4 (diff)
downloadxmobar-6c4970ea293796a832ecd1b292ec99a9695bf276.tar.gz
xmobar-6c4970ea293796a832ecd1b292ec99a9695bf276.tar.bz2
New DiskU and DiskIO monitors
Ignore-this: b72a9ad54c9ae478f2c3fbdcda4d26c8 New monitors for disk usage and throughput, replacing Disk. darcs-hash:20100204015455-748be-41e03ee9dff0e3e9e3725aae8a3e07c6facb70dc.gz
Diffstat (limited to 'Plugins/Monitors/StatFS.hsc')
-rw-r--r--Plugins/Monitors/StatFS.hsc72
1 files changed, 72 insertions, 0 deletions
diff --git a/Plugins/Monitors/StatFS.hsc b/Plugins/Monitors/StatFS.hsc
new file mode 100644
index 0000000..ad3b659
--- /dev/null
+++ b/Plugins/Monitors/StatFS.hsc
@@ -0,0 +1,72 @@
+-----------------------------------------------------------------------------
+-- |
+-- Module : Plugins.Monitors.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 Plugins.Monitors.StatFS (FileSystemStats(..), getFileSystemStats) where
+
+import Foreign
+import Foreign.C.Types
+import Foreign.C.String
+import Foreign.Storable
+import Data.ByteString (useAsCString)
+import Data.ByteString.Char8 (pack)
+
+#include <sys/vfs.h>
+
+data FileSystemStats = FileSystemStats {
+ fsStatBlockSize :: Integer
+ -- ^ Optimal transfer block size.
+ , fsStatBlockCount :: Integer
+ -- ^ Total data blocks in file system.
+ , fsStatByteCount :: Integer
+ -- ^ Total bytes in file system.
+ , fsStatBytesFree :: Integer
+ -- ^ Free bytes in file system.
+ , fsStatBytesAvailable :: Integer
+ -- ^ Free bytes available to non-superusers.
+ , fsStatBytesUsed :: Integer
+ -- ^ Bytes used.
+ } deriving (Show, Eq)
+
+data CStatfs
+
+foreign import ccall unsafe "sys/vfs.h statfs64"
+ c_statfs :: CString -> Ptr CStatfs -> IO CInt
+
+toI :: CLong -> Integer
+toI = toInteger
+
+getFileSystemStats :: String -> IO (Maybe FileSystemStats)
+getFileSystemStats path =
+ allocaBytes (#size struct statfs) $ \vfs ->
+ useAsCString (pack path) $ \cpath -> do
+ res <- c_statfs cpath vfs
+ case res of
+ -1 -> return Nothing
+ _ -> do
+ bsize <- (#peek struct statfs, f_bsize) vfs
+ bcount <- (#peek struct statfs, f_blocks) vfs
+ bfree <- (#peek struct statfs, f_bfree) vfs
+ bavail <- (#peek struct statfs, f_bavail) vfs
+ 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
+ }