summaryrefslogtreecommitdiff
path: root/Utility
diff options
context:
space:
mode:
Diffstat (limited to 'Utility')
-rw-r--r--Utility/Mounts.hsc58
-rw-r--r--Utility/libmounts.c105
-rw-r--r--Utility/libmounts.h48
3 files changed, 175 insertions, 36 deletions
diff --git a/Utility/Mounts.hsc b/Utility/Mounts.hsc
index 622ac877a..6bcb03f2c 100644
--- a/Utility/Mounts.hsc
+++ b/Utility/Mounts.hsc
@@ -2,6 +2,9 @@
-
- Derived from hsshellscript, originally written by
- Volker Wysk <hsss@volker-wysk.de>
+ -
+ - Modified to support BSD and Mac OS X by
+ - Joey Hess <joey@kitenet.net>
-
- Licensed under the GNU LGPL version 2.1 or higher.
-}
@@ -10,8 +13,7 @@
module Utility.Mounts (
Mntent(..),
- read_mtab,
- read_fstab,
+ getMounts
) where
import Control.Monad
@@ -20,62 +22,46 @@ import Foreign.C
import GHC.IO hiding (finally, bracket)
import Prelude hiding (catch)
-#include <stdio.h>
-#include <mntent.h>
+#include "libmounts.h"
+{- This is a stripped down mntent, containing only
+ - fields available everywhere. -}
data Mntent = Mntent
{ mnt_fsname :: String
, mnt_dir :: String
, mnt_type :: String
- , mnt_opts :: String
- , mnt_freq :: Int
- , mnt_passno :: Int
} deriving (Read, Show, Eq)
-read_mounts :: String -> IO [Mntent]
-read_mounts path = do
- h <- withCString path $ \cpath ->
- withCString "r" $ \r ->
- c_setmntent cpath r
+getMounts :: IO [Mntent]
+getMounts = do
+ h <- c_mounts_start
when (h == nullPtr) $
- throwErrno "setmntent"
+ throwErrno "getMounts"
mntent <- getmntent h []
- _ <- c_endmntent h
+ _ <- c_mounts_end h
return mntent
where
- getmntent h l = do
- ptr <- c_getmntent h
+ getmntent h c = do
+ ptr <- c_mounts_next h
if (ptr == nullPtr)
- then return $ reverse l
+ then return $ reverse c
else do
mnt_fsname_str <- #{peek struct mntent, mnt_fsname} ptr >>= peekCString
mnt_dir_str <- #{peek struct mntent, mnt_dir} ptr >>= peekCString
mnt_type_str <- #{peek struct mntent, mnt_type} ptr >>= peekCString
- mnt_opts_str <- #{peek struct mntent, mnt_opts} ptr >>= peekCString
- mnt_freq_int <- #{peek struct mntent, mnt_freq} ptr
- mnt_passno_int <- #{peek struct mntent, mnt_passno} ptr
let ent = Mntent
{ mnt_fsname = mnt_fsname_str
, mnt_dir = mnt_dir_str
, mnt_type = mnt_type_str
- , mnt_opts = mnt_opts_str
- , mnt_freq = mnt_freq_int
- , mnt_passno = mnt_passno_int
}
- getmntent h (ent:l)
-
-read_mtab :: IO [Mntent]
-read_mtab = read_mounts "/etc/mtab"
-
-read_fstab :: IO [Mntent]
-read_fstab = read_mounts "/etc/fstab"
+ getmntent h (ent:c)
-foreign import ccall safe "setmntent"
- c_setmntent :: ((Ptr CChar) -> ((Ptr CChar) -> (IO (Ptr ()))))
+foreign import ccall unsafe "libmounts.h mounts_start" c_mounts_start
+ :: IO (Ptr ())
-foreign import ccall safe "endmntent"
- c_endmntent :: ((Ptr ()) -> (IO CInt))
+foreign import ccall unsafe "libmounts.h mounts_next" c_mounts_next
+ :: Ptr () -> IO (Ptr ())
-foreign import ccall safe "getmntent"
- c_getmntent :: ((Ptr ()) -> (IO (Ptr ())))
+foreign import ccall unsafe "libmounts.h mounts_end" c_mounts_end
+ :: Ptr () -> IO CInt
diff --git a/Utility/libmounts.c b/Utility/libmounts.c
new file mode 100644
index 000000000..00755c577
--- /dev/null
+++ b/Utility/libmounts.c
@@ -0,0 +1,105 @@
+/* mounted filesystems, C mini-library
+ *
+ * Copyright (c) 1980, 1989, 1993, 1994
+ * The Regents of the University of California. All rights reserved.
+ * Copyright (c) 2001
+ * David Rufino <daverufino@btinternet.com>
+ * Copyright 2012
+ * Joey Hess <joey@kitenet.net>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the University nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include "libmounts.h"
+
+#include <errno.h>
+#include <stdio.h>
+
+#ifdef GETMNTENT
+/* direct passthrough the getmntent */
+FILE *mounts_start (void) {
+ return setmntent("/etc/mtab", "r");
+}
+int mounts_end (FILE *fp) {
+ return endmntent(fp);
+}
+struct mntent *mounts_next (FILE *fp) {
+ return getmntent(fp);
+}
+#endif
+
+#ifdef GETMNTINFOCALL
+/* getmntent emulation using getmntinfo */
+FILE *mounts_start (void) {
+ return ((FILE *)0x1) /* dummy non-NULL FILE pointer, not used */
+}
+int mounts_end (FILE *fp) {
+ return 1;
+}
+
+static struct mntent _mntent;
+
+static struct mntent *statfs_to_mntent (struct MNTINFOSTRUCT *mntbuf) {
+ _mntent.mnt_fsname = mntbuf->f_mntfromname;
+ _mntent.mnt_dir = mntbuf->f_mntonname;
+ _mntent.mnt_type = mntbuf->f_fstypename;
+
+ _mntent.mnt_opts = '\0';
+ _mntent.mnt_freq = 0;
+ _mntent.mnt_passno = 0;
+
+ return (&_mntent);
+}
+
+static int pos = -1;
+static int mntsize = -1;
+
+struct mntent *mounts_next (FILE *fp) {
+ struct MNTINFOSTRUCT *mntbuf;
+
+ if (pos == -1 || mntsize == -1)
+ mntsize = GETMNTINFOCALL(&mntbuf, MNT_NOWAIT);
+ ++pos;
+ if (pos == mntsize) {
+ pos = mntsize = -1;
+ return NULL;
+ }
+
+ return (statfs_to_mntent(&mntbuf[pos]));
+}
+#endif
+
+#ifdef UNKNOWN
+/* dummy, do-nothing version */
+FILE *mounts_start (void) {
+ return ((FILE *)0x1);
+}
+int mounts_end (FILE *fp) {
+ return 1;
+}
+struct mntent *mounts_next (FILE *fp) {
+ return NULL;
+}
+#endif
diff --git a/Utility/libmounts.h b/Utility/libmounts.h
new file mode 100644
index 000000000..0bd52e323
--- /dev/null
+++ b/Utility/libmounts.h
@@ -0,0 +1,48 @@
+/* Include appropriate headers for the OS, and define what will be used. */
+#if defined(__APPLE__)
+# include <sys/param.h>
+# include <sys/ucred.h>
+# include <sys/mount.h>
+/* In newer OSX versions, statfs64 is deprecated, in favor of statfs,
+ * which is 64 bit only with a build option -- but statfs64 still works,
+ * and this keeps older OSX also supported. */
+# define GETMNTINFOCALL getmntinfo64
+# define MNTINFOSTRUCT statfs64
+#else
+#if defined (__FreeBSD__)
+# include <sys/param.h>
+# include <sys/ucred.h>
+# include <sys/mount.h>
+# define GETMNTINFOCALL getmntinfo64
+# define MNTINFOSTRUCT statfs64
+#else
+#if defined (__linux__) || defined (__FreeBSD_kernel__)
+/* Linux or Debian kFreeBSD */
+#include <mntent.h>
+# define GETMNTENT
+#else
+# warning mounts listing code not available for this OS
+# define UNKNOWN
+#endif
+#endif
+#endif
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+
+#ifndef GETMNTENT
+#warning "boo"
+struct mntent {
+ char *mnt_fsname;
+ char *mnt_dir;
+ char *mnt_type;
+ char *mnt_opts; /* not filled in */
+ int mnt_freq; /* not filled in */
+ int mnt_passno; /* not filled in */
+};
+#endif
+
+FILE *mounts_start (void);
+int mounts_end (FILE *fp);
+struct mntent *mounts_next (FILE *fp);