1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2006-2007 Red Hat, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Author: Alexander Larsson <alexl@redhat.com>
25 #include <sys/types.h>
28 #ifndef HAVE_SYSCTLBYNAME
29 #ifdef HAVE_SYS_PARAM_H
30 #include <sys/param.h>
32 #ifdef HAVE_SYS_POLL_H
46 #include "gunixmounts.h"
48 #include "gfilemonitor.h"
54 * @short_description: Unix Mounts
56 * Routines for managing mounted UNIX mount points and paths.
63 char *filesystem_type;
64 gboolean is_read_only;
65 gboolean is_system_internal;
68 struct _GUnixMountPoint {
71 char *filesystem_type;
72 gboolean is_read_only;
73 gboolean is_user_mountable;
83 static guint signals[LAST_SIGNAL];
85 struct _GUnixMountMonitor {
88 GFileMonitor *fstab_monitor;
89 GFileMonitor *mtab_monitor;
92 struct _GUnixMountMonitorClass {
93 GObjectClass parent_class;
96 static GUnixMountMonitor *the_mount_monitor = NULL;
98 static GList *_g_get_unix_mounts (void);
99 static GList *_g_get_unix_mount_points (void);
101 G_DEFINE_TYPE (GUnixMountMonitor, g_unix_mount_monitor, G_TYPE_OBJECT);
103 #define MOUNT_POLL_INTERVAL 4000
105 #ifdef HAVE_SYS_MNTTAB_H
106 #define MNTOPT_RO "ro"
111 #elif defined (HAVE_SYS_MNTTAB_H)
112 #include <sys/mnttab.h>
115 #ifdef HAVE_SYS_VFSTAB_H
116 #include <sys/vfstab.h>
119 #if defined(HAVE_SYS_MNTCTL_H) && defined(HAVE_SYS_VMOUNT_H) && defined(HAVE_SYS_VFS_H)
120 #include <sys/mntctl.h>
122 #include <sys/vmount.h>
126 #if defined(HAVE_GETMNTINFO) && defined(HAVE_FSTAB_H) && defined(HAVE_SYS_MOUNT_H)
127 #include <sys/ucred.h>
128 #include <sys/mount.h>
130 #ifdef HAVE_SYS_SYSCTL_H
131 #include <sys/sysctl.h>
135 #ifndef HAVE_SETMNTENT
136 #define setmntent(f,m) fopen(f,m)
138 #ifndef HAVE_ENDMNTENT
139 #define endmntent(f) fclose(f)
143 is_in (const char *value, const char *set[])
146 for (i = 0; set[i] != NULL; i++)
148 if (strcmp (set[i], value) == 0)
155 guess_system_internal (const char *mountpoint,
159 const char *ignore_fs[] = {
177 const char *ignore_devices[] = {
186 const char *ignore_mountpoints[] = {
187 /* Includes all FHS 2.3 toplevel dirs */
210 if (is_in (fs, ignore_fs))
213 if (is_in (device, ignore_devices))
216 if (is_in (mountpoint, ignore_mountpoints))
219 if (g_str_has_prefix (mountpoint, "/dev") ||
220 g_str_has_prefix (mountpoint, "/proc") ||
221 g_str_has_prefix (mountpoint, "/sys"))
224 if (strstr (mountpoint, "/.gvfs") != NULL)
233 get_mtab_read_file (void)
237 return "/proc/mounts";
239 return _PATH_MOUNTED;
247 get_mtab_monitor_file (void)
250 return _PATH_MOUNTED;
256 G_LOCK_DEFINE_STATIC(getmntent);
259 _g_get_unix_mounts ()
261 struct mntent *mntent;
264 GUnixMount *mount_entry;
265 GHashTable *mounts_hash;
268 read_file = get_mtab_read_file ();
270 file = setmntent (read_file, "r");
276 mounts_hash = g_hash_table_new (g_str_hash, g_str_equal);
279 while ((mntent = getmntent (file)) != NULL)
281 /* ignore any mnt_fsname that is repeated and begins with a '/'
283 * We do this to avoid being fooled by --bind mounts, since
284 * these have the same device as the location they bind to.
285 * Its not an ideal solution to the problem, but its likely that
286 * the most important mountpoint is first and the --bind ones after
287 * that aren't as important. So it should work.
289 * The '/' is to handle procfs, tmpfs and other no device mounts.
291 if (mntent->mnt_fsname != NULL &&
292 mntent->mnt_fsname[0] == '/' &&
293 g_hash_table_lookup (mounts_hash, mntent->mnt_fsname))
296 mount_entry = g_new0 (GUnixMount, 1);
297 mount_entry->mount_path = g_strdup (mntent->mnt_dir);
298 mount_entry->device_path = g_strdup (mntent->mnt_fsname);
299 mount_entry->filesystem_type = g_strdup (mntent->mnt_type);
301 #if defined (HAVE_HASMNTOPT)
302 if (hasmntopt (mntent, MNTOPT_RO) != NULL)
303 mount_entry->is_read_only = TRUE;
306 mount_entry->is_system_internal =
307 guess_system_internal (mount_entry->mount_path,
308 mount_entry->filesystem_type,
309 mount_entry->device_path);
311 g_hash_table_insert (mounts_hash,
312 mount_entry->device_path,
313 mount_entry->device_path);
315 return_list = g_list_prepend (return_list, mount_entry);
317 g_hash_table_destroy (mounts_hash);
321 G_UNLOCK (getmntent);
323 return g_list_reverse (return_list);
326 #elif defined (HAVE_SYS_MNTTAB_H)
328 G_LOCK_DEFINE_STATIC(getmntent);
331 get_mtab_read_file (void)
334 return _PATH_MOUNTED;
336 return "/etc/mnttab";
341 get_mtab_monitor_file (void)
343 return get_mtab_read_file ();
347 _g_get_unix_mounts (void)
349 struct mnttab mntent;
352 GUnixMount *mount_entry;
355 read_file = get_mtab_read_file ();
357 file = setmntent (read_file, "r");
364 while (! getmntent (file, &mntent))
366 mount_entry = g_new0 (GUnixMount, 1);
368 mount_entry->mount_path = g_strdup (mntent.mnt_mountp);
369 mount_entry->device_path = g_strdup (mntent.mnt_special);
370 mount_entry->filesystem_type = g_strdup (mntent.mnt_fstype);
372 #if defined (HAVE_HASMNTOPT)
373 if (hasmntopt (&mntent, MNTOPT_RO) != NULL)
374 mount_entry->is_read_only = TRUE;
377 mount_entry->is_system_internal =
378 guess_system_internal (mount_entry->mount_path,
379 mount_entry->filesystem_type,
380 mount_entry->device_path);
382 return_list = g_list_prepend (return_list, mount_entry);
387 G_UNLOCK (getmntent);
389 return g_list_reverse (return_list);
392 #elif defined(HAVE_SYS_MNTCTL_H) && defined(HAVE_SYS_VMOUNT_H) && defined(HAVE_SYS_VFS_H)
395 get_mtab_monitor_file (void)
401 _g_get_unix_mounts (void)
403 struct vfs_ent *fs_info;
404 struct vmount *vmount_info;
406 unsigned int vmount_size;
410 if (mntctl (MCTL_QUERY, sizeof (vmount_size), &vmount_size) != 0)
412 g_warning ("Unable to know the number of mounted volumes\n");
417 vmount_info = (struct vmount*)g_malloc (vmount_size);
419 vmount_number = mntctl (MCTL_QUERY, vmount_size, vmount_info);
421 if (vmount_info->vmt_revision != VMT_REVISION)
422 g_warning ("Bad vmount structure revision number, want %d, got %d\n", VMT_REVISION, vmount_info->vmt_revision);
424 if (vmount_number < 0)
426 g_warning ("Unable to recover mounted volumes information\n");
428 g_free (vmount_info);
433 while (vmount_number > 0)
435 mount_entry = g_new0 (GUnixMount, 1);
437 mount_entry->device_path = g_strdup (vmt2dataptr (vmount_info, VMT_OBJECT));
438 mount_entry->mount_path = g_strdup (vmt2dataptr (vmount_info, VMT_STUB));
439 /* is_removable = (vmount_info->vmt_flags & MNT_REMOVABLE) ? 1 : 0; */
440 mount_entry->is_read_only = (vmount_info->vmt_flags & MNT_READONLY) ? 1 : 0;
442 fs_info = getvfsbytype (vmount_info->vmt_gfstype);
445 mount_entry->filesystem_type = g_strdup ("unknown");
447 mount_entry->filesystem_type = g_strdup (fs_info->vfsent_name);
449 mount_entry->is_system_internal =
450 guess_system_internal (mount_entry->mount_path,
451 mount_entry->filesystem_type,
452 mount_entry->device_path);
454 return_list = g_list_prepend (return_list, mount_entry);
456 vmount_info = (struct vmount *)( (char*)vmount_info
457 + vmount_info->vmt_length);
462 g_free (vmount_info);
464 return g_list_reverse (return_list);
467 #elif defined(HAVE_GETMNTINFO) && defined(HAVE_FSTAB_H) && defined(HAVE_SYS_MOUNT_H)
470 get_mtab_monitor_file (void)
476 _g_get_unix_mounts (void)
478 struct statfs *mntent = NULL;
480 GUnixMount *mount_entry;
483 /* Pass MNT_NOWAIT to avoid blocking trying to update NFS mounts. */
484 if ((num_mounts = getmntinfo (&mntent, MNT_NOWAIT)) == 0)
489 for (i = 0; i < num_mounts; i++)
491 mount_entry = g_new0 (GUnixMount, 1);
493 mount_entry->mount_path = g_strdup (mntent[i].f_mntonname);
494 mount_entry->device_path = g_strdup (mntent[i].f_mntfromname);
495 mount_entry->filesystem_type = g_strdup (mntent[i].f_fstypename);
496 if (mntent[i].f_flags & MNT_RDONLY)
497 mount_entry->is_read_only = TRUE;
499 mount_entry->is_system_internal =
500 guess_system_internal (mount_entry->mount_path,
501 mount_entry->filesystem_type,
502 mount_entry->device_path);
504 return_list = g_list_prepend (return_list, mount_entry);
507 return g_list_reverse (return_list);
510 #error No _g_get_unix_mounts() implementation for system
513 /* _g_get_unix_mount_points():
515 * don't return swap and ignore mounts.
519 get_fstab_file (void)
521 #if defined(HAVE_SYS_MNTCTL_H) && defined(HAVE_SYS_VMOUNT_H) && defined(HAVE_SYS_VFS_H)
523 return "/etc/filesystems";
524 #elif defined(_PATH_MNTTAB)
526 #elif defined(VFSTAB)
535 _g_get_unix_mount_points (void)
537 struct mntent *mntent;
540 GUnixMountPoint *mount_entry;
543 read_file = get_fstab_file ();
545 file = setmntent (read_file, "r");
552 while ((mntent = getmntent (file)) != NULL)
554 if ((strcmp (mntent->mnt_dir, "ignore") == 0) ||
555 (strcmp (mntent->mnt_dir, "swap") == 0))
558 mount_entry = g_new0 (GUnixMountPoint, 1);
559 mount_entry->mount_path = g_strdup (mntent->mnt_dir);
560 mount_entry->device_path = g_strdup (mntent->mnt_fsname);
561 mount_entry->filesystem_type = g_strdup (mntent->mnt_type);
563 #ifdef HAVE_HASMNTOPT
564 if (hasmntopt (mntent, MNTOPT_RO) != NULL)
565 mount_entry->is_read_only = TRUE;
567 if (hasmntopt (mntent, "loop") != NULL)
568 mount_entry->is_loopback = TRUE;
572 if ((mntent->mnt_type != NULL && strcmp ("supermount", mntent->mnt_type) == 0)
573 #ifdef HAVE_HASMNTOPT
574 || (hasmntopt (mntent, "user") != NULL
575 && hasmntopt (mntent, "user") != hasmntopt (mntent, "user_xattr"))
576 || hasmntopt (mntent, "pamconsole") != NULL
577 || hasmntopt (mntent, "users") != NULL
578 || hasmntopt (mntent, "owner") != NULL
581 mount_entry->is_user_mountable = TRUE;
583 return_list = g_list_prepend (return_list, mount_entry);
587 G_UNLOCK (getmntent);
589 return g_list_reverse (return_list);
592 #elif defined (HAVE_SYS_MNTTAB_H)
595 _g_get_unix_mount_points (void)
597 struct mnttab mntent;
600 GUnixMountPoint *mount_entry;
603 read_file = get_fstab_file ();
605 file = setmntent (read_file, "r");
612 while (! getmntent (file, &mntent))
614 if ((strcmp (mntent.mnt_mountp, "ignore") == 0) ||
615 (strcmp (mntent.mnt_mountp, "swap") == 0))
618 mount_entry = g_new0 (GUnixMountPoint, 1);
620 mount_entry->mount_path = g_strdup (mntent.mnt_mountp);
621 mount_entry->device_path = g_strdup (mntent.mnt_special);
622 mount_entry->filesystem_type = g_strdup (mntent.mnt_fstype);
624 #ifdef HAVE_HASMNTOPT
625 if (hasmntopt (&mntent, MNTOPT_RO) != NULL)
626 mount_entry->is_read_only = TRUE;
628 if (hasmntopt (&mntent, "lofs") != NULL)
629 mount_entry->is_loopback = TRUE;
632 if ((mntent.mnt_fstype != NULL)
633 #ifdef HAVE_HASMNTOPT
634 || (hasmntopt (&mntent, "user") != NULL
635 && hasmntopt (&mntent, "user") != hasmntopt (&mntent, "user_xattr"))
636 || hasmntopt (&mntent, "pamconsole") != NULL
637 || hasmntopt (&mntent, "users") != NULL
638 || hasmntopt (&mntent, "owner") != NULL
641 mount_entry->is_user_mountable = TRUE;
644 return_list = g_list_prepend (return_list, mount_entry);
648 G_UNLOCK (getmntent);
650 return g_list_reverse (return_list);
652 #elif defined(HAVE_SYS_MNTCTL_H) && defined(HAVE_SYS_VMOUNT_H) && defined(HAVE_SYS_VFS_H)
654 /* functions to parse /etc/filesystems on aix */
656 /* read character, ignoring comments (begin with '*', end with '\n' */
658 aix_fs_getc (FILE *fd)
662 while ((c = getc (fd)) == '*')
664 while (((c = getc (fd)) != '\n') && (c != EOF))
669 /* eat all continuous spaces in a file */
671 aix_fs_ignorespace (FILE *fd)
675 while ((c = aix_fs_getc (fd)) != EOF)
677 if (!g_ascii_isspace (c))
687 /* read one word from file */
689 aix_fs_getword (FILE *fd,
694 aix_fs_ignorespace (fd);
696 while (((c = aix_fs_getc (fd)) != EOF) && !g_ascii_isspace (c))
700 while (((c = aix_fs_getc (fd)) != EOF) && (c != '"'))
712 char mnt_mount[PATH_MAX];
713 char mnt_special[PATH_MAX];
715 char mnt_options[128];
716 } AixMountTableEntry;
718 /* read mount points properties */
720 aix_fs_get (FILE *fd,
721 AixMountTableEntry *prop)
723 static char word[PATH_MAX] = { 0 };
724 char value[PATH_MAX];
729 if (aix_fs_getword (fd, word) == EOF)
733 word[strlen(word) - 1] = 0;
734 strcpy (prop->mnt_mount, word);
736 /* read attributes and value */
738 while (aix_fs_getword (fd, word) != EOF)
740 /* test if is attribute or new stanza */
741 if (word[strlen(word) - 1] == ':')
745 aix_fs_getword (fd, value);
748 aix_fs_getword (fd, value);
750 if (strcmp (word, "dev") == 0)
751 strcpy (prop->mnt_special, value);
752 else if (strcmp (word, "vfs") == 0)
753 strcpy (prop->mnt_fstype, value);
754 else if (strcmp (word, "options") == 0)
755 strcpy(prop->mnt_options, value);
762 _g_get_unix_mount_points (void)
764 struct mntent *mntent;
767 GUnixMountPoint *mount_entry;
768 AixMountTableEntry mntent;
771 read_file = get_fstab_file ();
773 file = setmntent (read_file, "r");
779 while (!aix_fs_get (file, &mntent))
781 if (strcmp ("cdrfs", mntent.mnt_fstype) == 0)
783 mount_entry = g_new0 (GUnixMountPoint, 1);
786 mount_entry->mount_path = g_strdup (mntent.mnt_mount);
787 mount_entry->device_path = g_strdup (mntent.mnt_special);
788 mount_entry->filesystem_type = g_strdup (mntent.mnt_fstype);
789 mount_entry->is_read_only = TRUE;
790 mount_entry->is_user_mountable = TRUE;
792 return_list = g_list_prepend (return_list, mount_entry);
798 return g_list_reverse (return_list);
801 #elif defined(HAVE_GETMNTINFO) && defined(HAVE_FSTAB_H) && defined(HAVE_SYS_MOUNT_H)
804 _g_get_unix_mount_points (void)
806 struct fstab *fstab = NULL;
807 GUnixMountPoint *mount_entry;
809 #ifdef HAVE_SYS_SYSCTL_H
811 size_t len = sizeof(usermnt);
820 #ifdef HAVE_SYS_SYSCTL_H
821 #if defined(HAVE_SYSCTLBYNAME)
822 sysctlbyname ("vfs.usermount", &usermnt, &len, NULL, 0);
823 #elif defined(CTL_VFS) && defined(VFS_USERMOUNT)
828 mib[1] = VFS_USERMOUNT;
829 sysctl (mib, 2, &usermnt, &len, NULL, 0);
831 #elif defined(CTL_KERN) && defined(KERN_USERMOUNT)
836 mib[1] = KERN_USERMOUNT;
837 sysctl (mib, 2, &usermnt, &len, NULL, 0);
842 while ((fstab = getfsent ()) != NULL)
844 if (strcmp (fstab->fs_vfstype, "swap") == 0)
847 mount_entry = g_new0 (GUnixMountPoint, 1);
849 mount_entry->mount_path = g_strdup (fstab->fs_file);
850 mount_entry->device_path = g_strdup (fstab->fs_spec);
851 mount_entry->filesystem_type = g_strdup (fstab->fs_vfstype);
853 if (strcmp (fstab->fs_type, "ro") == 0)
854 mount_entry->is_read_only = TRUE;
856 #ifdef HAVE_SYS_SYSCTL_H
859 uid_t uid = getuid ();
860 if (stat (fstab->fs_file, &sb) == 0)
862 if (uid == 0 || sb.st_uid == uid)
863 mount_entry->is_user_mountable = TRUE;
868 return_list = g_list_prepend (return_list, mount_entry);
873 return g_list_reverse (return_list);
876 #error No g_get_mount_table() implementation for system
880 get_mounts_timestamp (void)
882 const char *monitor_file;
885 monitor_file = get_mtab_monitor_file ();
888 if (stat (monitor_file, &buf) == 0)
889 return (guint64)buf.st_mtime;
895 get_mount_points_timestamp (void)
897 const char *monitor_file;
900 monitor_file = get_fstab_file ();
903 if (stat (monitor_file, &buf) == 0)
904 return (guint64)buf.st_mtime;
911 * @time_read: guint64 to contain a timestamp.
913 * Gets a #GList of strings containing the unix mounts.
914 * If @time_read is set, it will be filled with the mount
915 * timestamp, allowing for checking if the mounts have changed
916 * with g_unix_mounts_changed_since().
918 * Returns: a #GList of the UNIX mounts.
921 g_get_unix_mounts (guint64 *time_read)
924 *time_read = get_mounts_timestamp ();
926 return _g_get_unix_mounts ();
930 * g_get_unix_mount_at:
931 * @mount_path: path for a possible unix mount.
932 * @time_read: guint64 to contain a timestamp.
934 * Gets a #GUnixMount for a given mount path. If @time_read
935 * is set, it will be filled with a unix timestamp for checking
936 * if the mounts have changed since with g_unix_mounts_changed_since().
938 * Returns: a #GUnixMount.
941 g_get_unix_mount_at (const char *mount_path,
945 GUnixMount *mount_entry, *found;
947 mounts = g_get_unix_mounts (time_read);
950 for (l = mounts; l != NULL; l = l->next)
952 mount_entry = l->data;
954 if (strcmp (mount_path, mount_entry->mount_path) == 0)
957 g_unix_mount_free (mount_entry);
960 g_list_free (mounts);
966 * g_get_unix_mount_points:
967 * @time_read: guint64 to contain a timestamp.
969 * Gets a #GList of strings containing the unix mount points.
970 * If @time_read is set, it will be filled with the mount timestamp,
971 * allowing for checking if the mounts have changed with
972 * g_unix_mounts_points_changed_since().
974 * Returns: a #GList of the UNIX mountpoints.
977 g_get_unix_mount_points (guint64 *time_read)
980 *time_read = get_mount_points_timestamp ();
982 return _g_get_unix_mount_points ();
986 * g_unix_mounts_changed_since:
987 * @time: guint64 to contain a timestamp.
989 * Checks if the unix mounts have changed since a given unix time.
991 * Returns: %TRUE if the mounts have changed since @time.
994 g_unix_mounts_changed_since (guint64 time)
996 return get_mounts_timestamp () != time;
1000 * g_unix_mount_points_changed_since:
1001 * @time: guint64 to contain a timestamp.
1003 * Checks if the unix mount points have changed since a given unix time.
1005 * Returns: %TRUE if the mount points have changed since @time.
1008 g_unix_mount_points_changed_since (guint64 time)
1010 return get_mount_points_timestamp () != time;
1014 g_unix_mount_monitor_finalize (GObject *object)
1016 GUnixMountMonitor *monitor;
1018 monitor = G_UNIX_MOUNT_MONITOR (object);
1020 if (monitor->fstab_monitor)
1022 g_file_monitor_cancel (monitor->fstab_monitor);
1023 g_object_unref (monitor->fstab_monitor);
1026 if (monitor->mtab_monitor)
1028 g_file_monitor_cancel (monitor->mtab_monitor);
1029 g_object_unref (monitor->mtab_monitor);
1032 the_mount_monitor = NULL;
1034 if (G_OBJECT_CLASS (g_unix_mount_monitor_parent_class)->finalize)
1035 (*G_OBJECT_CLASS (g_unix_mount_monitor_parent_class)->finalize) (object);
1040 g_unix_mount_monitor_class_init (GUnixMountMonitorClass *klass)
1042 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
1044 gobject_class->finalize = g_unix_mount_monitor_finalize;
1046 * GUnixMountMonitor::mounts-changed:
1048 * Emitted when the unix mounts have changed.
1050 signals[MOUNTS_CHANGED] =
1051 g_signal_new ("mounts_changed",
1052 G_TYPE_FROM_CLASS (klass),
1056 g_cclosure_marshal_VOID__VOID,
1059 * GUnixMountMonitor::mountpoints-changed:
1061 * Emitted when the unix mount points have changed.
1063 signals[MOUNTPOINTS_CHANGED] =
1064 g_signal_new ("mountpoints_changed",
1065 G_TYPE_FROM_CLASS (klass),
1069 g_cclosure_marshal_VOID__VOID,
1074 fstab_file_changed (GFileMonitor *monitor,
1077 GFileMonitorEvent event_type,
1080 GUnixMountMonitor *mount_monitor;
1082 if (event_type != G_FILE_MONITOR_EVENT_CHANGED &&
1083 event_type != G_FILE_MONITOR_EVENT_CREATED &&
1084 event_type != G_FILE_MONITOR_EVENT_DELETED)
1087 mount_monitor = user_data;
1088 g_signal_emit (mount_monitor, signals[MOUNTPOINTS_CHANGED], 0);
1092 mtab_file_changed (GFileMonitor *monitor,
1095 GFileMonitorEvent event_type,
1098 GUnixMountMonitor *mount_monitor;
1100 if (event_type != G_FILE_MONITOR_EVENT_CHANGED &&
1101 event_type != G_FILE_MONITOR_EVENT_CREATED &&
1102 event_type != G_FILE_MONITOR_EVENT_DELETED)
1105 mount_monitor = user_data;
1106 g_signal_emit (mount_monitor, signals[MOUNTS_CHANGED], 0);
1110 g_unix_mount_monitor_init (GUnixMountMonitor *monitor)
1114 if (get_fstab_file () != NULL)
1116 file = g_file_new_for_path (get_fstab_file ());
1117 monitor->fstab_monitor = g_file_monitor_file (file, 0, NULL);
1118 g_object_unref (file);
1120 g_signal_connect (monitor->fstab_monitor, "changed", (GCallback)fstab_file_changed, monitor);
1123 if (get_mtab_monitor_file () != NULL)
1125 file = g_file_new_for_path (get_mtab_monitor_file ());
1126 monitor->mtab_monitor = g_file_monitor_file (file, 0, NULL);
1127 g_object_unref (file);
1129 g_signal_connect (monitor->mtab_monitor, "changed", (GCallback)mtab_file_changed, monitor);
1134 * g_unix_mount_monitor_new:
1136 * Gets a new #GUnixMountMonitor.
1138 * Returns: a #GUnixMountMonitor.
1141 g_unix_mount_monitor_new (void)
1143 if (the_mount_monitor == NULL)
1145 the_mount_monitor = g_object_new (G_TYPE_UNIX_MOUNT_MONITOR, NULL);
1146 return the_mount_monitor;
1149 return g_object_ref (the_mount_monitor);
1153 * g_unix_mount_free:
1154 * @mount_entry: a #GUnixMount.
1156 * Frees a unix mount.
1159 g_unix_mount_free (GUnixMount *mount_entry)
1161 g_return_if_fail (mount_entry != NULL);
1163 g_free (mount_entry->mount_path);
1164 g_free (mount_entry->device_path);
1165 g_free (mount_entry->filesystem_type);
1166 g_free (mount_entry);
1170 * g_unix_mount_point_free:
1171 * @mount_point: unix mount point to free.
1173 * Frees a unix mount point.
1176 g_unix_mount_point_free (GUnixMountPoint *mount_point)
1178 g_return_if_fail (mount_point != NULL);
1180 g_free (mount_point->mount_path);
1181 g_free (mount_point->device_path);
1182 g_free (mount_point->filesystem_type);
1183 g_free (mount_point);
1187 strcmp_null (const char *str1,
1192 if (str1 == NULL && str2 != NULL)
1194 if (str1 != NULL && str2 == NULL)
1196 return strcmp (str1, str2);
1200 * g_unix_mount_compare:
1201 * @mount1: first #GUnixMount to compare.
1202 * @mount2: second #GUnixMount to compare.
1204 * Compares two unix mounts.
1206 * Returns: 1, 0 or -1 if @mount1 is greater than, equal to,
1207 * or less than @mount2, respectively.
1210 g_unix_mount_compare (GUnixMount *mount1,
1215 g_return_val_if_fail (mount1 != NULL && mount2 != NULL, 0);
1217 res = strcmp_null (mount1->mount_path, mount2->mount_path);
1221 res = strcmp_null (mount1->device_path, mount2->device_path);
1225 res = strcmp_null (mount1->filesystem_type, mount2->filesystem_type);
1229 res = mount1->is_read_only - mount2->is_read_only;
1237 * g_unix_mount_get_mount_path:
1238 * @mount_entry: input #GUnixMount to get the mount path for.
1240 * Gets the mount path for a unix mount.
1242 * Returns: the mount path for @mount_entry.
1245 g_unix_mount_get_mount_path (GUnixMount *mount_entry)
1247 g_return_val_if_fail (mount_entry != NULL, NULL);
1249 return mount_entry->mount_path;
1253 * g_unix_mount_get_device_path:
1254 * @mount_entry: a #GUnixMount.
1256 * Gets the device path for a unix mount.
1258 * Returns: a string containing the device path.
1261 g_unix_mount_get_device_path (GUnixMount *mount_entry)
1263 g_return_val_if_fail (mount_entry != NULL, NULL);
1265 return mount_entry->device_path;
1269 * g_unix_mount_get_fs_type:
1270 * @mount_entry: a #GUnixMount.
1272 * Gets the filesystem type for the unix mount.
1274 * Returns: a string containing the file system type.
1277 g_unix_mount_get_fs_type (GUnixMount *mount_entry)
1279 g_return_val_if_fail (mount_entry != NULL, NULL);
1281 return mount_entry->filesystem_type;
1285 * g_unix_mount_is_readonly:
1286 * @mount_entry: a #GUnixMount.
1288 * Checks if a unix mount is mounted read only.
1290 * Returns: %TRUE if @mount_entry is read only.
1293 g_unix_mount_is_readonly (GUnixMount *mount_entry)
1295 g_return_val_if_fail (mount_entry != NULL, FALSE);
1297 return mount_entry->is_read_only;
1301 * g_unix_mount_is_system_internal:
1302 * @mount_entry: a #GUnixMount.
1304 * Checks if a unix mount is a system path.
1306 * Returns: %TRUE if the unix mount is for a system path.
1309 g_unix_mount_is_system_internal (GUnixMount *mount_entry)
1311 g_return_val_if_fail (mount_entry != NULL, FALSE);
1313 return mount_entry->is_system_internal;
1317 * g_unix_mount_point_compare:
1318 * @mount1: a #GUnixMount.
1319 * @mount2: a #GUnixMount.
1321 * Compares two unix mount points.
1323 * Returns: 1, 0 or -1 if @mount1 is greater than, equal to,
1324 * or less than @mount2, respectively.
1327 g_unix_mount_point_compare (GUnixMountPoint *mount1,
1328 GUnixMountPoint *mount2)
1332 g_return_val_if_fail (mount1 != NULL && mount2 != NULL, 0);
1334 res = strcmp_null (mount1->mount_path, mount2->mount_path);
1338 res = strcmp_null (mount1->device_path, mount2->device_path);
1342 res = strcmp_null (mount1->filesystem_type, mount2->filesystem_type);
1346 res = mount1->is_read_only - mount2->is_read_only;
1350 res = mount1->is_user_mountable - mount2->is_user_mountable;
1354 res = mount1->is_loopback - mount2->is_loopback;
1362 * g_unix_mount_point_get_mount_path:
1363 * @mount_point: a #GUnixMountPoint.
1365 * Gets the mount path for a unix mount point.
1367 * Returns: a string containing the mount path.
1370 g_unix_mount_point_get_mount_path (GUnixMountPoint *mount_point)
1372 g_return_val_if_fail (mount_point != NULL, NULL);
1374 return mount_point->mount_path;
1378 * g_unix_mount_point_get_device_path:
1379 * @mount_point: a #GUnixMountPoint.
1381 * Gets the device path for a unix mount point.
1383 * Returns: a string containing the device path.
1386 g_unix_mount_point_get_device_path (GUnixMountPoint *mount_point)
1388 g_return_val_if_fail (mount_point != NULL, NULL);
1390 return mount_point->device_path;
1394 * g_unix_mount_point_get_fs_type:
1395 * @mount_point: a #GUnixMountPoint.
1397 * Gets the file system type for the mount point.
1399 * Returns: a string containing the file system type.
1402 g_unix_mount_point_get_fs_type (GUnixMountPoint *mount_point)
1404 g_return_val_if_fail (mount_point != NULL, NULL);
1406 return mount_point->filesystem_type;
1410 * g_unix_mount_point_is_readonly:
1411 * @mount_point: a #GUnixMountPoint.
1413 * Checks if a unix mount point is read only.
1415 * Returns: %TRUE if a mount point is read only.
1418 g_unix_mount_point_is_readonly (GUnixMountPoint *mount_point)
1420 g_return_val_if_fail (mount_point != NULL, FALSE);
1422 return mount_point->is_read_only;
1426 * g_unix_mount_point_is_user_mountable:
1427 * @mount_point: a #GUnixMountPoint.
1429 * Checks if a unix mount point is mountable by the user.
1431 * Returns: %TRUE if the mount point is user mountable.
1434 g_unix_mount_point_is_user_mountable (GUnixMountPoint *mount_point)
1436 g_return_val_if_fail (mount_point != NULL, FALSE);
1438 return mount_point->is_user_mountable;
1442 * g_unix_mount_point_is_loopback:
1443 * @mount_point: a #GUnixMountPoint.
1445 * Checks if a unix mount point is a loopback device.
1447 * Returns: %TRUE if the mount point is a loopback. %FALSE otherwise.
1450 g_unix_mount_point_is_loopback (GUnixMountPoint *mount_point)
1452 g_return_val_if_fail (mount_point != NULL, FALSE);
1454 return mount_point->is_loopback;
1457 static GUnixMountType
1458 guess_mount_type (const char *mount_path,
1459 const char *device_path,
1460 const char *filesystem_type)
1462 GUnixMountType type;
1465 type = G_UNIX_MOUNT_TYPE_UNKNOWN;
1467 if ((strcmp (filesystem_type, "udf") == 0) ||
1468 (strcmp (filesystem_type, "iso9660") == 0) ||
1469 (strcmp (filesystem_type, "cd9660") == 0))
1470 type = G_UNIX_MOUNT_TYPE_CDROM;
1471 else if (strcmp (filesystem_type, "nfs") == 0)
1472 type = G_UNIX_MOUNT_TYPE_NFS;
1473 else if (g_str_has_prefix (device_path, "/vol/dev/diskette/") ||
1474 g_str_has_prefix (device_path, "/dev/fd") ||
1475 g_str_has_prefix (device_path, "/dev/floppy"))
1476 type = G_UNIX_MOUNT_TYPE_FLOPPY;
1477 else if (g_str_has_prefix (device_path, "/dev/cdrom") ||
1478 g_str_has_prefix (device_path, "/dev/acd") ||
1479 g_str_has_prefix (device_path, "/dev/cd"))
1480 type = G_UNIX_MOUNT_TYPE_CDROM;
1481 else if (g_str_has_prefix (device_path, "/vol/"))
1483 const char *name = mount_path + strlen ("/");
1485 if (g_str_has_prefix (name, "cdrom"))
1486 type = G_UNIX_MOUNT_TYPE_CDROM;
1487 else if (g_str_has_prefix (name, "floppy") ||
1488 g_str_has_prefix (device_path, "/vol/dev/diskette/"))
1489 type = G_UNIX_MOUNT_TYPE_FLOPPY;
1490 else if (g_str_has_prefix (name, "rmdisk"))
1491 type = G_UNIX_MOUNT_TYPE_ZIP;
1492 else if (g_str_has_prefix (name, "jaz"))
1493 type = G_UNIX_MOUNT_TYPE_JAZ;
1494 else if (g_str_has_prefix (name, "memstick"))
1495 type = G_UNIX_MOUNT_TYPE_MEMSTICK;
1499 basename = g_path_get_basename (mount_path);
1501 if (g_str_has_prefix (basename, "cdrom") ||
1502 g_str_has_prefix (basename, "cdwriter") ||
1503 g_str_has_prefix (basename, "burn") ||
1504 g_str_has_prefix (basename, "cdr") ||
1505 g_str_has_prefix (basename, "cdrw") ||
1506 g_str_has_prefix (basename, "dvdrom") ||
1507 g_str_has_prefix (basename, "dvdram") ||
1508 g_str_has_prefix (basename, "dvdr") ||
1509 g_str_has_prefix (basename, "dvdrw") ||
1510 g_str_has_prefix (basename, "cdrom_dvdrom") ||
1511 g_str_has_prefix (basename, "cdrom_dvdram") ||
1512 g_str_has_prefix (basename, "cdrom_dvdr") ||
1513 g_str_has_prefix (basename, "cdrom_dvdrw") ||
1514 g_str_has_prefix (basename, "cdr_dvdrom") ||
1515 g_str_has_prefix (basename, "cdr_dvdram") ||
1516 g_str_has_prefix (basename, "cdr_dvdr") ||
1517 g_str_has_prefix (basename, "cdr_dvdrw") ||
1518 g_str_has_prefix (basename, "cdrw_dvdrom") ||
1519 g_str_has_prefix (basename, "cdrw_dvdram") ||
1520 g_str_has_prefix (basename, "cdrw_dvdr") ||
1521 g_str_has_prefix (basename, "cdrw_dvdrw"))
1522 type = G_UNIX_MOUNT_TYPE_CDROM;
1523 else if (g_str_has_prefix (basename, "floppy"))
1524 type = G_UNIX_MOUNT_TYPE_FLOPPY;
1525 else if (g_str_has_prefix (basename, "zip"))
1526 type = G_UNIX_MOUNT_TYPE_ZIP;
1527 else if (g_str_has_prefix (basename, "jaz"))
1528 type = G_UNIX_MOUNT_TYPE_JAZ;
1529 else if (g_str_has_prefix (basename, "camera"))
1530 type = G_UNIX_MOUNT_TYPE_CAMERA;
1531 else if (g_str_has_prefix (basename, "memstick") ||
1532 g_str_has_prefix (basename, "memory_stick") ||
1533 g_str_has_prefix (basename, "ram"))
1534 type = G_UNIX_MOUNT_TYPE_MEMSTICK;
1535 else if (g_str_has_prefix (basename, "compact_flash"))
1536 type = G_UNIX_MOUNT_TYPE_CF;
1537 else if (g_str_has_prefix (basename, "smart_media"))
1538 type = G_UNIX_MOUNT_TYPE_SM;
1539 else if (g_str_has_prefix (basename, "sd_mmc"))
1540 type = G_UNIX_MOUNT_TYPE_SDMMC;
1541 else if (g_str_has_prefix (basename, "ipod"))
1542 type = G_UNIX_MOUNT_TYPE_IPOD;
1547 if (type == G_UNIX_MOUNT_TYPE_UNKNOWN)
1548 type = G_UNIX_MOUNT_TYPE_HD;
1554 * g_unix_mount_guess_type:
1555 * @mount_entry: a #GUnixMount.
1557 * Guesses the type of a unix mount. If the mount type cannot be
1558 * determined, returns %G_UNIX_MOUNT_TYPE_UNKNOWN.
1560 * Returns: a #GUnixMountType.
1563 g_unix_mount_guess_type (GUnixMount *mount_entry)
1565 g_return_val_if_fail (mount_entry != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
1566 g_return_val_if_fail (mount_entry->mount_path != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
1567 g_return_val_if_fail (mount_entry->device_path != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
1568 g_return_val_if_fail (mount_entry->filesystem_type != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
1570 return guess_mount_type (mount_entry->mount_path,
1571 mount_entry->device_path,
1572 mount_entry->filesystem_type);
1576 * g_unix_mount_point_guess_type:
1577 * @mount_point: a #GUnixMountPoint.
1579 * Guesses the type of a unix mount point. If the mount type cannot be
1580 * determined, returns %G_UNIX_MOUNT_TYPE_UNKNOWN.
1582 * Returns: a #GUnixMountType.
1585 g_unix_mount_point_guess_type (GUnixMountPoint *mount_point)
1587 g_return_val_if_fail (mount_point != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
1588 g_return_val_if_fail (mount_point->mount_path != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
1589 g_return_val_if_fail (mount_point->device_path != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
1590 g_return_val_if_fail (mount_point->filesystem_type != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
1592 return guess_mount_type (mount_point->mount_path,
1593 mount_point->device_path,
1594 mount_point->filesystem_type);
1597 #define __G_UNIX_MOUNTS_C__
1598 #include "gioaliasdef.c"