[Xfce4-commits] <xfce4-mount-plugin:master> Add support for systems with getmntinfo() (BSDs)

Landry Breuil noreply at xfce.org
Tue Apr 10 19:26:03 CEST 2012


Updating branch refs/heads/master
         to 599edfef58711a2635fbee81aa71a3b8e44a46b5 (commit)
       from ce83d1f1379ca8e746ef060f48a8698f36dfd238 (commit)

commit 599edfef58711a2635fbee81aa71a3b8e44a46b5
Author: Landry Breuil <landry at xfce.org>
Date:   Tue Apr 10 12:15:43 2012 +0200

    Add support for systems with getmntinfo() (BSDs)
    
    - detect if either getmntent() or getmntinfo() is available
    - do the #ifdef/#elif dance
    
    Mostly adapted from patches i wrote when porting mount plugin
    to OpenBSD two years and a half ago..

 configure.ac.in        |    2 +
 panel-plugin/devices.c |   79 ++++++++++++++++++++++++++++++++++++++++++++++--
 2 files changed, 78 insertions(+), 3 deletions(-)

diff --git a/configure.ac.in b/configure.ac.in
index c7170aa..fcb4c59 100644
--- a/configure.ac.in
+++ b/configure.ac.in
@@ -40,6 +40,8 @@ dnl Substitute in Makefiles
 AC_SUBST(OS)
 
 AC_CHECK_FUNCS([gethostname])
+AC_CHECK_FUNCS([getmntent])
+AC_CHECK_FUNCS([getmntinfo])
 AC_CHECK_FUNCS([memset])
 AC_CHECK_FUNCS([socket])
 AC_CHECK_FUNCS([strstr])
diff --git a/panel-plugin/devices.c b/panel-plugin/devices.c
index cdfb82f..99b52e8 100644
--- a/panel-plugin/devices.c
+++ b/panel-plugin/devices.c
@@ -20,12 +20,23 @@ along with this program; if not, write to the Free Software
 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */
 
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
 #include <fstab.h>
 #include <glib.h>
-#include <mntent.h>
 #include <stdio.h>
 #include <string.h>
+#ifdef HAVE_GETMNTENT
+#include <mntent.h>
 #include <sys/vfs.h>
+#elif HAVE_GETMNTINFO
+#include <sys/param.h>
+#include <sys/mount.h>
+#else
+#error no getmntent/getmntinfo ? send patches !
+#endif
 
 #include <libxfce4panel/xfce-panel-plugin.h>
 #include <libxfce4util/libxfce4util.h>
@@ -550,10 +561,14 @@ exclude_filesystem (GPtrArray *excluded_FSs, gchar *mountpoint, gchar *device)
 void
 disks_refresh(GPtrArray * pdisks, GPtrArray *excluded_FSs)
 {
-    /* using getmntent to get filesystems mount information */
+    /* using getmntent/getmntinfo to get filesystems mount information */
 
+#ifdef HAVE_GETMNTENT
     FILE * fmtab = NULL; /* file /etc/mtab */
     struct mntent * pmntent = NULL; /* struct for mnt info */
+#elif HAVE_GETMNTINFO
+    int i, nb_mounted_fs = 0;
+#endif
     struct statfs * pstatfs = NULL;
     gboolean exclude =  FALSE;
 
@@ -565,32 +580,51 @@ disks_refresh(GPtrArray * pdisks, GPtrArray *excluded_FSs)
     /* remove t_mount_info for all devices */
     disks_free_mount_info (pdisks);
 
+#ifdef HAVE_GETMNTENT
     /* allocate new struct statfs */
     pstatfs = g_new0 (struct statfs, 1);
 
     /* open file */
     fmtab = setmntent (MTAB, "r"); /* mtab file */
+#elif HAVE_GETMNTINFO
+    /* get mounted fs */
+    nb_mounted_fs = getmntinfo(&pstatfs,MNT_WAIT);
+#endif
 
     /* start looking for mounted devices */
+#ifdef HAVE_GETMNTENT
     for (pmntent=getmntent(fmtab); pmntent!=NULL; pmntent=getmntent(fmtab)) {
 
         DBG (" have entry: %s on %s \n", pmntent->mnt_fsname, pmntent->mnt_dir );
 
         statfs (pmntent->mnt_dir, pstatfs);
+#elif HAVE_GETMNTINFO
+    for (i = 0; i < nb_mounted_fs ; i++) {
+        DBG (" have entry: %s on %s : type %s\n", pstatfs[i].f_mntfromname, pstatfs[i].f_mntonname, pstatfs[i].f_fstypename );
+#endif
 
         /* if we got the stat and the block number is non-zero */
 
         /* get pointer on disk from pdisks */
         /* CHANGED to reflect change in disk_search */
+#ifdef HAVE_GETMNTENT
         pdisk = disks_search (pdisks, pmntent->mnt_dir);
+#elif HAVE_GETMNTINFO
+        pdisk = disks_search (pdisks, pstatfs[i].f_mntonname);
+#endif
         if (excluded_FSs!=NULL)
+#ifdef HAVE_GETMNTENT
             exclude = exclude_filesystem (excluded_FSs, pmntent->mnt_dir, pmntent->mnt_fsname);
+#elif HAVE_GETMNTINFO
+            exclude = exclude_filesystem (excluded_FSs, pstatfs[i].f_mntonname, pstatfs[i].f_mntfromname);
+#endif
 
         if (pdisk == NULL) { /* if disk is not found in pdisks */
 
             /* create a new struct t_disk and add it to pdisks */
             /* test for mnt_dir==none or neither block device nor NFS or system device */
             if ( exclude ||
+#ifdef HAVE_GETMNTENT
               g_ascii_strcasecmp(pmntent->mnt_dir, "none") == 0 ||
               !(g_str_has_prefix(pmntent->mnt_fsname, "/dev/") ||
               g_str_has_prefix(pmntent->mnt_type, "fuse") ||
@@ -599,25 +633,43 @@ disks_refresh(GPtrArray * pdisks, GPtrArray *excluded_FSs)
               g_str_has_prefix(pmntent->mnt_type, "cifs") ||
               g_str_has_prefix(pmntent->mnt_type, "shfs") ) ||
               g_str_has_prefix(pmntent->mnt_dir, "/sys/")
-
+#elif HAVE_GETMNTINFO
+              /* TODO: add support for more fs types on BSD */
+              g_ascii_strcasecmp(pstatfs[i].f_mntonname, "none") == 0 ||
+              !g_str_has_prefix(pstatfs[i].f_mntfromname, "/dev/") ||
+              g_str_has_prefix(pstatfs[i].f_fstypename, "nfs") ||
+              g_str_has_prefix(pstatfs[i].f_fstypename, "mfs")
+#endif
             ) continue;
 
             /* else have valid entry reflecting block device or NFS */
+#ifdef HAVE_GETMNTENT
             pdisk = disk_new (pmntent->mnt_fsname, pmntent->mnt_dir);
             pdisk->dc = disk_classify (pmntent->mnt_fsname, pmntent->mnt_dir);
+#elif HAVE_GETMNTINFO
+            pdisk = disk_new (pstatfs[i].f_mntfromname, pstatfs[i].f_mntonname);
+            pdisk->dc = disk_classify (pstatfs[i].f_mntfromname, pstatfs[i].f_mntonname);
+#endif
             g_ptr_array_add (pdisks, pdisk);
         }
 
         /* create new t_mount_info */
+#ifdef HAVE_GETMNTENT
         mount_info = mount_info_new_from_stat (pstatfs, pmntent->mnt_type,
                                                pmntent->mnt_dir);
+#elif HAVE_GETMNTINFO
+        mount_info = mount_info_new_from_stat (&pstatfs[i], pstatfs[i].f_fstypename,
+                                               pstatfs[i].f_mntonname);
+#endif
         /* add it to pdisk */
         pdisk->mount_info = mount_info ;
 
     } /* end for */
 
+#ifdef HAVE_GETMNTENT
     g_free (pstatfs);
     endmntent (fmtab); /* close file */
+#endif
 
     return;
 }
@@ -658,25 +710,46 @@ disk_classify (char *device, char *mountpoint)
 gboolean
 disk_check_mounted (const char *disk)
 {
+#ifdef HAVE_GETMNTENT
     FILE *fmtab = NULL; /* file /etc/mtab */
     struct mntent *pmntent = NULL; /* struct for mnt info */
+#elif HAVE_GETMNTINFO
+    struct statfs * pstatfs = NULL;
+    int i, nb_mounted_fs = 0;
+#endif
     gboolean retval = FALSE;
 
+#ifdef HAVE_GETMNTENT
     /* open file */
     fmtab = setmntent (MTAB, "r"); /* mtab file */
+#elif HAVE_GETMNTINFO
+    /* get mounted fs */
+    nb_mounted_fs = getmntinfo(&pstatfs,MNT_WAIT);
+#endif
 
     /* start looking for mounted devices */
+#ifdef HAVE_GETMNTENT
     for (pmntent=getmntent(fmtab); pmntent!=NULL; pmntent=getmntent(fmtab))
+#elif HAVE_GETMNTINFO
+    for (i = 0; i < nb_mounted_fs ; i++)
+#endif
     {
+#ifdef HAVE_GETMNTENT
         if (strcmp(pmntent->mnt_dir, disk)==0 ||
             strcmp(pmntent->mnt_fsname, disk)==0 )
+#elif HAVE_GETMNTINFO
+        if (strcmp(pstatfs[i].f_mntonname, disk)==0 ||
+            strcmp(pstatfs[i].f_mntfromname, disk)==0 )
+#endif
         {
             retval = TRUE;
             break;
         }
     }
 
+#ifdef HAVE_GETMNTENT
     endmntent (fmtab); /* close file */
+#endif
 
     return retval;
 }


More information about the Xfce4-commits mailing list