summaryrefslogtreecommitdiff
path: root/Utility
diff options
context:
space:
mode:
authorGravatar Joey Hess <joey@kitenet.net>2013-05-04 16:04:17 -0400
committerGravatar Joey Hess <joey@kitenet.net>2013-05-04 16:19:25 -0400
commita210689fff14d3a1b2b8c41b17d9384a3d41cb8c (patch)
tree3e2f90ffdc43592f6bd46fb3ed604d7198bc63c2 /Utility
parent603e70ab3f89404cdd9d43080990c04da96d92da (diff)
Got removable media mount detection working on Android.
Bionic has an amusing stub for `getmntent` that prints out "FIX ME! implement getmntent()" But, `/proc/mounts` is there, so I just parse it.
Diffstat (limited to 'Utility')
-rw-r--r--Utility/Mounts.hsc30
-rw-r--r--Utility/libmounts.h2
2 files changed, 29 insertions, 3 deletions
diff --git a/Utility/Mounts.hsc b/Utility/Mounts.hsc
index c21a68032..c13b0ece1 100644
--- a/Utility/Mounts.hsc
+++ b/Utility/Mounts.hsc
@@ -3,7 +3,7 @@
- Derived from hsshellscript, originally written by
- Volker Wysk <hsss@volker-wysk.de>
-
- - Modified to support BSD and Mac OS X by
+ - Modified to support BSD, Mac OS X, and Android by
- Joey Hess <joey@kitenet.net>
-
- Licensed under the GNU LGPL version 2.1 or higher.
@@ -16,13 +16,18 @@ module Utility.Mounts (
getMounts
) where
+#ifndef __ANDROID__
import Control.Monad
import Foreign
import Foreign.C
import GHC.IO hiding (finally, bracket)
import Prelude hiding (catch)
-
#include "libmounts.h"
+#else
+import Utility.Exception
+import Data.Maybe
+import Control.Applicative
+#endif
{- This is a stripped down mntent, containing only
- fields available everywhere. -}
@@ -32,6 +37,8 @@ data Mntent = Mntent
, mnt_type :: String
} deriving (Read, Show, Eq, Ord)
+#ifndef __ANDROID__
+
getMounts :: IO [Mntent]
getMounts = do
h <- c_mounts_start
@@ -67,3 +74,22 @@ foreign import ccall unsafe "libmounts.h mounts_next" c_mounts_next
:: Ptr () -> IO (Ptr ())
foreign import ccall unsafe "libmounts.h mounts_end" c_mounts_end
:: Ptr () -> IO CInt
+
+#else
+
+{- Android does not support getmntent (well, it's a no-op stub in Bionic).
+ -
+ - But, the linux kernel's /proc/mounts is available to be parsed.
+ -}
+getMounts :: IO [Mntent]
+getMounts = catchDefaultIO [] $
+ mapMaybe (parse . words) . lines <$> readFile "/proc/mounts"
+ where
+ parse (device:mountpoint:fstype:_rest) = Just $ Mntent
+ { mnt_fsname = device
+ , mnt_dir = mountpoint
+ , mnt_type = fstype
+ }
+ parse _ = Nothing
+
+#endif
diff --git a/Utility/libmounts.h b/Utility/libmounts.h
index 460fcc7ab..24df55f31 100644
--- a/Utility/libmounts.h
+++ b/Utility/libmounts.h
@@ -6,7 +6,7 @@
# define GETMNTINFO
#else
#if defined __ANDROID__
-# warning mounts listing code not available for Android
+/* Android is handled by the Haskell code, not here. */
# define UNKNOWN
#else
#if defined (__linux__) || defined (__FreeBSD_kernel__)