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"
52 * @short_description: Unix Mounts
54 * Routines for managing mounted UNIX mount points and paths.
61 char *filesystem_type;
62 gboolean is_read_only;
63 gboolean is_system_internal;
66 struct _GUnixMountPoint {
69 char *filesystem_type;
70 gboolean is_read_only;
71 gboolean is_user_mountable;
81 static guint signals[LAST_SIGNAL];
83 struct _GUnixMountMonitor {
86 GFileMonitor *fstab_monitor;
87 GFileMonitor *mtab_monitor;
90 struct _GUnixMountMonitorClass {
91 GObjectClass parent_class;
94 static GUnixMountMonitor *the_mount_monitor = NULL;
96 static GList *_g_get_unix_mounts (void);
97 static GList *_g_get_unix_mount_points (void);
99 G_DEFINE_TYPE (GUnixMountMonitor, g_unix_mount_monitor, G_TYPE_OBJECT);
101 #define MOUNT_POLL_INTERVAL 4000
103 #ifdef HAVE_SYS_MNTTAB_H
104 #define MNTOPT_RO "ro"
109 #elif defined (HAVE_SYS_MNTTAB_H)
110 #include <sys/mnttab.h>
113 #ifdef HAVE_SYS_VFSTAB_H
114 #include <sys/vfstab.h>
117 #if defined(HAVE_SYS_MNTCTL_H) && defined(HAVE_SYS_VMOUNT_H) && defined(HAVE_SYS_VFS_H)
118 #include <sys/mntctl.h>
120 #include <sys/vmount.h>
124 #if defined(HAVE_GETMNTINFO) && defined(HAVE_FSTAB_H) && defined(HAVE_SYS_MOUNT_H)
125 #include <sys/ucred.h>
126 #include <sys/mount.h>
128 #ifdef HAVE_SYS_SYSCTL_H
129 #include <sys/sysctl.h>
133 #ifndef HAVE_SETMNTENT
134 #define setmntent(f,m) fopen(f,m)
136 #ifndef HAVE_ENDMNTENT
137 #define endmntent(f) fclose(f)
141 is_in (const char *value, const char *set[])
144 for (i = 0; set[i] != NULL; i++)
146 if (strcmp (set[i], value) == 0)
153 guess_system_internal (const char *mountpoint,
157 const char *ignore_fs[] = {
175 const char *ignore_devices[] = {
184 const char *ignore_mountpoints[] = {
185 /* Includes all FHS 2.3 toplevel dirs */
208 if (is_in (fs, ignore_fs))
211 if (is_in (device, ignore_devices))
214 if (is_in (mountpoint, ignore_mountpoints))
217 if (g_str_has_prefix (mountpoint, "/dev") ||
218 g_str_has_prefix (mountpoint, "/proc") ||
219 g_str_has_prefix (mountpoint, "/sys"))
222 if (strstr (mountpoint, "/.gvfs") != NULL)
231 get_mtab_read_file (void)
235 return "/proc/mounts";
237 return _PATH_MOUNTED;
245 get_mtab_monitor_file (void)
248 return _PATH_MOUNTED;
254 G_LOCK_DEFINE_STATIC(getmntent);
257 _g_get_unix_mounts ()
259 struct mntent *mntent;
262 GUnixMount *mount_entry;
263 GHashTable *mounts_hash;
266 read_file = get_mtab_read_file ();
268 file = setmntent (read_file, "r");
274 mounts_hash = g_hash_table_new (g_str_hash, g_str_equal);
277 while ((mntent = getmntent (file)) != NULL)
279 /* ignore any mnt_fsname that is repeated and begins with a '/'
281 * We do this to avoid being fooled by --bind mounts, since
282 * these have the same device as the location they bind to.
283 * Its not an ideal solution to the problem, but its likely that
284 * the most important mountpoint is first and the --bind ones after
285 * that aren't as important. So it should work.
287 * The '/' is to handle procfs, tmpfs and other no device mounts.
289 if (mntent->mnt_fsname != NULL &&
290 mntent->mnt_fsname[0] == '/' &&
291 g_hash_table_lookup (mounts_hash, mntent->mnt_fsname))
294 mount_entry = g_new0 (GUnixMount, 1);
295 mount_entry->mount_path = g_strdup (mntent->mnt_dir);
296 mount_entry->device_path = g_strdup (mntent->mnt_fsname);
297 mount_entry->filesystem_type = g_strdup (mntent->mnt_type);
299 #if defined (HAVE_HASMNTOPT)
300 if (hasmntopt (mntent, MNTOPT_RO) != NULL)
301 mount_entry->is_read_only = TRUE;
304 mount_entry->is_system_internal =
305 guess_system_internal (mount_entry->mount_path,
306 mount_entry->filesystem_type,
307 mount_entry->device_path);
309 g_hash_table_insert (mounts_hash,
310 mount_entry->device_path,
311 mount_entry->device_path);
313 return_list = g_list_prepend (return_list, mount_entry);
315 g_hash_table_destroy (mounts_hash);
319 G_UNLOCK (getmntent);
321 return g_list_reverse (return_list);
324 #elif defined (HAVE_SYS_MNTTAB_H)
326 G_LOCK_DEFINE_STATIC(getmntent);
329 get_mtab_read_file (void)
332 return _PATH_MOUNTED;
334 return "/etc/mnttab";
339 get_mtab_monitor_file (void)
341 return get_mtab_read_file ();
345 _g_get_unix_mounts (void)
347 struct mnttab mntent;
350 GUnixMount *mount_entry;
353 read_file = get_mtab_read_file ();
355 file = setmntent (read_file, "r");
362 while (! getmntent (file, &mntent))
364 mount_entry = g_new0 (GUnixMount, 1);
366 mount_entry->mount_path = g_strdup (mntent.mnt_mountp);
367 mount_entry->device_path = g_strdup (mntent.mnt_special);
368 mount_entry->filesystem_type = g_strdup (mntent.mnt_fstype);
370 #if defined (HAVE_HASMNTOPT)
371 if (hasmntopt (&mntent, MNTOPT_RO) != NULL)
372 mount_entry->is_read_only = TRUE;
375 mount_entry->is_system_internal =
376 guess_system_internal (mount_entry->mount_path,
377 mount_entry->filesystem_type,
378 mount_entry->device_path);
380 return_list = g_list_prepend (return_list, mount_entry);
385 G_UNLOCK (getmntent);
387 return g_list_reverse (return_list);
390 #elif defined(HAVE_SYS_MNTCTL_H) && defined(HAVE_SYS_VMOUNT_H) && defined(HAVE_SYS_VFS_H)
393 get_mtab_monitor_file (void)
399 _g_get_unix_mounts (void)
401 struct vfs_ent *fs_info;
402 struct vmount *vmount_info;
404 unsigned int vmount_size;
408 if (mntctl (MCTL_QUERY, sizeof (vmount_size), &vmount_size) != 0)
410 g_warning ("Unable to know the number of mounted volumes\n");
415 vmount_info = (struct vmount*)g_malloc (vmount_size);
417 vmount_number = mntctl (MCTL_QUERY, vmount_size, vmount_info);
419 if (vmount_info->vmt_revision != VMT_REVISION)
420 g_warning ("Bad vmount structure revision number, want %d, got %d\n", VMT_REVISION, vmount_info->vmt_revision);
422 if (vmount_number < 0)
424 g_warning ("Unable to recover mounted volumes information\n");
426 g_free (vmount_info);
431 while (vmount_number > 0)
433 mount_entry = g_new0 (GUnixMount, 1);
435 mount_entry->device_path = g_strdup (vmt2dataptr (vmount_info, VMT_OBJECT));
436 mount_entry->mount_path = g_strdup (vmt2dataptr (vmount_info, VMT_STUB));
437 /* is_removable = (vmount_info->vmt_flags & MNT_REMOVABLE) ? 1 : 0; */
438 mount_entry->is_read_only = (vmount_info->vmt_flags & MNT_READONLY) ? 1 : 0;
440 fs_info = getvfsbytype (vmount_info->vmt_gfstype);
443 mount_entry->filesystem_type = g_strdup ("unknown");
445 mount_entry->filesystem_type = g_strdup (fs_info->vfsent_name);
447 mount_entry->is_system_internal =
448 guess_system_internal (mount_entry->mount_path,
449 mount_entry->filesystem_type,
450 mount_entry->device_path);
452 return_list = g_list_prepend (return_list, mount_entry);
454 vmount_info = (struct vmount *)( (char*)vmount_info
455 + vmount_info->vmt_length);
460 g_free (vmount_info);
462 return g_list_reverse (return_list);
465 #elif defined(HAVE_GETMNTINFO) && defined(HAVE_FSTAB_H) && defined(HAVE_SYS_MOUNT_H)
468 get_mtab_monitor_file (void)
474 _g_get_unix_mounts (void)
476 struct statfs *mntent = NULL;
478 GUnixMount *mount_entry;
481 /* Pass MNT_NOWAIT to avoid blocking trying to update NFS mounts. */
482 if ((num_mounts = getmntinfo (&mntent, MNT_NOWAIT)) == 0)
487 for (i = 0; i < num_mounts; i++)
489 mount_entry = g_new0 (GUnixMount, 1);
491 mount_entry->mount_path = g_strdup (mntent[i].f_mntonname);
492 mount_entry->device_path = g_strdup (mntent[i].f_mntfromname);
493 mount_entry->filesystem_type = g_strdup (mntent[i].f_fstypename);
494 if (mntent[i].f_flags & MNT_RDONLY)
495 mount_entry->is_read_only = TRUE;
497 mount_entry->is_system_internal =
498 guess_system_internal (mount_entry->mount_path,
499 mount_entry->filesystem_type,
500 mount_entry->device_path);
502 return_list = g_list_prepend (return_list, mount_entry);
505 return g_list_reverse (return_list);
508 #error No _g_get_unix_mounts() implementation for system
511 /* _g_get_unix_mount_points():
513 * don't return swap and ignore mounts.
517 get_fstab_file (void)
519 #if defined(HAVE_SYS_MNTCTL_H) && defined(HAVE_SYS_VMOUNT_H) && defined(HAVE_SYS_VFS_H)
521 return "/etc/filesystems";
522 #elif defined(_PATH_MNTTAB)
524 #elif defined(VFSTAB)
533 _g_get_unix_mount_points (void)
535 struct mntent *mntent;
538 GUnixMountPoint *mount_entry;
541 read_file = get_fstab_file ();
543 file = setmntent (read_file, "r");
550 while ((mntent = getmntent (file)) != NULL)
552 if ((strcmp (mntent->mnt_dir, "ignore") == 0) ||
553 (strcmp (mntent->mnt_dir, "swap") == 0))
556 mount_entry = g_new0 (GUnixMountPoint, 1);
557 mount_entry->mount_path = g_strdup (mntent->mnt_dir);
558 mount_entry->device_path = g_strdup (mntent->mnt_fsname);
559 mount_entry->filesystem_type = g_strdup (mntent->mnt_type);
561 #ifdef HAVE_HASMNTOPT
562 if (hasmntopt (mntent, MNTOPT_RO) != NULL)
563 mount_entry->is_read_only = TRUE;
565 if (hasmntopt (mntent, "loop") != NULL)
566 mount_entry->is_loopback = TRUE;
570 if ((mntent->mnt_type != NULL && strcmp ("supermount", mntent->mnt_type) == 0)
571 #ifdef HAVE_HASMNTOPT
572 || (hasmntopt (mntent, "user") != NULL
573 && hasmntopt (mntent, "user") != hasmntopt (mntent, "user_xattr"))
574 || hasmntopt (mntent, "pamconsole") != NULL
575 || hasmntopt (mntent, "users") != NULL
576 || hasmntopt (mntent, "owner") != NULL
579 mount_entry->is_user_mountable = TRUE;
581 return_list = g_list_prepend (return_list, mount_entry);
585 G_UNLOCK (getmntent);
587 return g_list_reverse (return_list);
590 #elif defined (HAVE_SYS_MNTTAB_H)
593 _g_get_unix_mount_points (void)
595 struct mnttab mntent;
598 GUnixMountPoint *mount_entry;
601 read_file = get_fstab_file ();
603 file = setmntent (read_file, "r");
610 while (! getmntent (file, &mntent))
612 if ((strcmp (mntent.mnt_mountp, "ignore") == 0) ||
613 (strcmp (mntent.mnt_mountp, "swap") == 0))
616 mount_entry = g_new0 (GUnixMountPoint, 1);
618 mount_entry->mount_path = g_strdup (mntent.mnt_mountp);
619 mount_entry->device_path = g_strdup (mntent.mnt_special);
620 mount_entry->filesystem_type = g_strdup (mntent.mnt_fstype);
622 #ifdef HAVE_HASMNTOPT
623 if (hasmntopt (&mntent, MNTOPT_RO) != NULL)
624 mount_entry->is_read_only = TRUE;
626 if (hasmntopt (&mntent, "lofs") != NULL)
627 mount_entry->is_loopback = TRUE;
630 if ((mntent.mnt_fstype != NULL)
631 #ifdef HAVE_HASMNTOPT
632 || (hasmntopt (&mntent, "user") != NULL
633 && hasmntopt (&mntent, "user") != hasmntopt (&mntent, "user_xattr"))
634 || hasmntopt (&mntent, "pamconsole") != NULL
635 || hasmntopt (&mntent, "users") != NULL
636 || hasmntopt (&mntent, "owner") != NULL
639 mount_entry->is_user_mountable = TRUE;
642 return_list = g_list_prepend (return_list, mount_entry);
646 G_UNLOCK (getmntent);
648 return g_list_reverse (return_list);
650 #elif defined(HAVE_SYS_MNTCTL_H) && defined(HAVE_SYS_VMOUNT_H) && defined(HAVE_SYS_VFS_H)
652 /* functions to parse /etc/filesystems on aix */
654 /* read character, ignoring comments (begin with '*', end with '\n' */
656 aix_fs_getc (FILE *fd)
660 while ((c = getc (fd)) == '*')
662 while (((c = getc (fd)) != '\n') && (c != EOF))
667 /* eat all continuous spaces in a file */
669 aix_fs_ignorespace (FILE *fd)
673 while ((c = aix_fs_getc (fd)) != EOF)
675 if (!g_ascii_isspace (c))
685 /* read one word from file */
687 aix_fs_getword (FILE *fd, char *word)
691 aix_fs_ignorespace (fd);
693 while (((c = aix_fs_getc (fd)) != EOF) && !g_ascii_isspace (c))
697 while (((c = aix_fs_getc (fd)) != EOF) && (c != '"'))
709 char mnt_mount[PATH_MAX];
710 char mnt_special[PATH_MAX];
712 char mnt_options[128];
713 } AixMountTableEntry;
715 /* read mount points properties */
717 aix_fs_get (FILE *fd, AixMountTableEntry *prop)
719 static char word[PATH_MAX] = { 0 };
720 char value[PATH_MAX];
725 if (aix_fs_getword (fd, word) == EOF)
729 word[strlen(word) - 1] = 0;
730 strcpy (prop->mnt_mount, word);
732 /* read attributes and value */
734 while (aix_fs_getword (fd, word) != EOF)
736 /* test if is attribute or new stanza */
737 if (word[strlen(word) - 1] == ':')
741 aix_fs_getword (fd, value);
744 aix_fs_getword (fd, value);
746 if (strcmp (word, "dev") == 0)
747 strcpy (prop->mnt_special, value);
748 else if (strcmp (word, "vfs") == 0)
749 strcpy (prop->mnt_fstype, value);
750 else if (strcmp (word, "options") == 0)
751 strcpy(prop->mnt_options, value);
758 _g_get_unix_mount_points (void)
760 struct mntent *mntent;
763 GUnixMountPoint *mount_entry;
764 AixMountTableEntry mntent;
767 read_file = get_fstab_file ();
769 file = setmntent (read_file, "r");
775 while (!aix_fs_get (file, &mntent))
777 if (strcmp ("cdrfs", mntent.mnt_fstype) == 0)
779 mount_entry = g_new0 (GUnixMountPoint, 1);
782 mount_entry->mount_path = g_strdup (mntent.mnt_mount);
783 mount_entry->device_path = g_strdup (mntent.mnt_special);
784 mount_entry->filesystem_type = g_strdup (mntent.mnt_fstype);
785 mount_entry->is_read_only = TRUE;
786 mount_entry->is_user_mountable = TRUE;
788 return_list = g_list_prepend (return_list, mount_entry);
794 return g_list_reverse (return_list);
797 #elif defined(HAVE_GETMNTINFO) && defined(HAVE_FSTAB_H) && defined(HAVE_SYS_MOUNT_H)
800 _g_get_unix_mount_points (void)
802 struct fstab *fstab = NULL;
803 GUnixMountPoint *mount_entry;
805 #ifdef HAVE_SYS_SYSCTL_H
807 size_t len = sizeof(usermnt);
816 #ifdef HAVE_SYS_SYSCTL_H
817 #if defined(HAVE_SYSCTLBYNAME)
818 sysctlbyname ("vfs.usermount", &usermnt, &len, NULL, 0);
819 #elif defined(CTL_VFS) && defined(VFS_USERMOUNT)
824 mib[1] = VFS_USERMOUNT;
825 sysctl (mib, 2, &usermnt, &len, NULL, 0);
827 #elif defined(CTL_KERN) && defined(KERN_USERMOUNT)
832 mib[1] = KERN_USERMOUNT;
833 sysctl (mib, 2, &usermnt, &len, NULL, 0);
838 while ((fstab = getfsent ()) != NULL)
840 if (strcmp (fstab->fs_vfstype, "swap") == 0)
843 mount_entry = g_new0 (GUnixMountPoint, 1);
845 mount_entry->mount_path = g_strdup (fstab->fs_file);
846 mount_entry->device_path = g_strdup (fstab->fs_spec);
847 mount_entry->filesystem_type = g_strdup (fstab->fs_vfstype);
849 if (strcmp (fstab->fs_type, "ro") == 0)
850 mount_entry->is_read_only = TRUE;
852 #ifdef HAVE_SYS_SYSCTL_H
855 uid_t uid = getuid ();
856 if (stat (fstab->fs_file, &sb) == 0)
858 if (uid == 0 || sb.st_uid == uid)
859 mount_entry->is_user_mountable = TRUE;
864 return_list = g_list_prepend (return_list, mount_entry);
869 return g_list_reverse (return_list);
872 #error No g_get_mount_table() implementation for system
876 get_mounts_timestamp (void)
878 const char *monitor_file;
881 monitor_file = get_mtab_monitor_file ();
884 if (stat (monitor_file, &buf) == 0)
885 return (guint64)buf.st_mtime;
891 get_mount_points_timestamp (void)
893 const char *monitor_file;
896 monitor_file = get_fstab_file ();
899 if (stat (monitor_file, &buf) == 0)
900 return (guint64)buf.st_mtime;
907 * @time_read: guint64 to contain a timestamp.
909 * Gets a #GList of strings containing the unix mounts. If @time_read
910 * is set, it will be filled with the mount timestamp,
911 * allowing for checking if the mounts have changed with
912 * g_unix_mounts_changed_since().
914 * Returns: a #GList of the UNIX mounts.
917 g_get_unix_mounts (guint64 *time_read)
920 *time_read = get_mounts_timestamp ();
922 return _g_get_unix_mounts ();
926 * g_get_unix_mount_at:
927 * @mount_path: path for a possible unix mount.
928 * @time_read: guint64 to contain a timestamp.
930 * Gets a #GUnixMount for a given mount path. If @time_read
931 * is set, it will be filled with a unix timestamp for checking
932 * if the mounts have changed since with g_unix_mounts_changed_since().
934 * Returns: a #GUnixMount.
937 g_get_unix_mount_at (const char *mount_path,
941 GUnixMount *mount_entry, *found;
943 mounts = g_get_unix_mounts (time_read);
946 for (l = mounts; l != NULL; l = l->next)
948 mount_entry = l->data;
950 if (strcmp (mount_path, mount_entry->mount_path) == 0)
953 g_unix_mount_free (mount_entry);
956 g_list_free (mounts);
962 * g_get_unix_mount_points:
963 * @time_read: guint64 to contain a timestamp.
965 * Gets a #GList of strings containing the unix mount points.
966 * If @time_read is set, it will be filled with the mount timestamp,
967 * allowing for checking if the mounts have changed with
968 * g_unix_mounts_points_changed_since().
970 * Returns a #GList of the UNIX mountpoints.
973 g_get_unix_mount_points (guint64 *time_read)
976 *time_read = get_mount_points_timestamp ();
978 return _g_get_unix_mount_points ();
982 * g_unix_mounts_changed_since:
983 * @time: guint64 to contain a timestamp.
985 * Checks if the unix mounts have changed since a given unix time.
987 * Returns %TRUE if the mounts have changed since @time.
990 g_unix_mounts_changed_since (guint64 time)
992 return get_mounts_timestamp () != time;
996 * g_unix_mount_points_changed_since:
997 * @time: guint64 to contain a timestamp.
999 * Checks if the unix mount points have changed since a given unix time.
1001 * Returns %TRUE if the mount points have changed since @time.
1004 g_unix_mount_points_changed_since (guint64 time)
1006 return get_mount_points_timestamp () != time;
1010 g_unix_mount_monitor_finalize (GObject *object)
1012 GUnixMountMonitor *monitor;
1014 monitor = G_UNIX_MOUNT_MONITOR (object);
1016 if (monitor->fstab_monitor)
1018 g_file_monitor_cancel (monitor->fstab_monitor);
1019 g_object_unref (monitor->fstab_monitor);
1022 if (monitor->mtab_monitor)
1024 g_file_monitor_cancel (monitor->mtab_monitor);
1025 g_object_unref (monitor->mtab_monitor);
1028 the_mount_monitor = NULL;
1030 if (G_OBJECT_CLASS (g_unix_mount_monitor_parent_class)->finalize)
1031 (*G_OBJECT_CLASS (g_unix_mount_monitor_parent_class)->finalize) (object);
1036 g_unix_mount_monitor_class_init (GUnixMountMonitorClass *klass)
1038 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
1040 gobject_class->finalize = g_unix_mount_monitor_finalize;
1042 * GUnixMountMonitor::mounts-changed:
1044 * Emitted when the unix mounts have changed.
1046 signals[MOUNTS_CHANGED] =
1047 g_signal_new ("mounts_changed",
1048 G_TYPE_FROM_CLASS (klass),
1052 g_cclosure_marshal_VOID__VOID,
1055 * GUnixMountMonitor::mountpoints-changed:
1057 * Emitted when the unix mount points have changed.
1059 signals[MOUNTPOINTS_CHANGED] =
1060 g_signal_new ("mountpoints_changed",
1061 G_TYPE_FROM_CLASS (klass),
1065 g_cclosure_marshal_VOID__VOID,
1070 fstab_file_changed (GFileMonitor* monitor,
1073 GFileMonitorEvent event_type,
1076 GUnixMountMonitor *mount_monitor;
1078 if (event_type != G_FILE_MONITOR_EVENT_CHANGED &&
1079 event_type != G_FILE_MONITOR_EVENT_CREATED &&
1080 event_type != G_FILE_MONITOR_EVENT_DELETED)
1083 mount_monitor = user_data;
1084 g_signal_emit (mount_monitor, signals[MOUNTPOINTS_CHANGED], 0);
1088 mtab_file_changed (GFileMonitor* monitor,
1091 GFileMonitorEvent event_type,
1094 GUnixMountMonitor *mount_monitor;
1096 if (event_type != G_FILE_MONITOR_EVENT_CHANGED &&
1097 event_type != G_FILE_MONITOR_EVENT_CREATED &&
1098 event_type != G_FILE_MONITOR_EVENT_DELETED)
1101 mount_monitor = user_data;
1102 g_signal_emit (mount_monitor, signals[MOUNTS_CHANGED], 0);
1106 g_unix_mount_monitor_init (GUnixMountMonitor *monitor)
1110 if (get_fstab_file () != NULL)
1112 file = g_file_new_for_path (get_fstab_file ());
1113 monitor->fstab_monitor = g_file_monitor_file (file, 0, NULL);
1114 g_object_unref (file);
1116 g_signal_connect (monitor->fstab_monitor, "changed", (GCallback)fstab_file_changed, monitor);
1119 if (get_mtab_monitor_file () != NULL)
1121 file = g_file_new_for_path (get_mtab_monitor_file ());
1122 monitor->mtab_monitor = g_file_monitor_file (file, 0, NULL);
1123 g_object_unref (file);
1125 g_signal_connect (monitor->mtab_monitor, "changed", (GCallback)mtab_file_changed, monitor);
1130 * g_unix_mount_monitor_new:
1132 * Gets a new #GUnixMountMonitor.
1134 * Returns: a #GUnixMountMonitor.
1137 g_unix_mount_monitor_new (void)
1139 if (the_mount_monitor == NULL)
1141 the_mount_monitor = g_object_new (G_TYPE_UNIX_MOUNT_MONITOR, NULL);
1142 return the_mount_monitor;
1145 return g_object_ref (the_mount_monitor);
1149 * g_unix_mount_free:
1150 * @mount_entry: a #GUnixMount.
1152 * Frees a unix mount.
1155 g_unix_mount_free (GUnixMount *mount_entry)
1157 g_return_if_fail (mount_entry != NULL);
1159 g_free (mount_entry->mount_path);
1160 g_free (mount_entry->device_path);
1161 g_free (mount_entry->filesystem_type);
1162 g_free (mount_entry);
1166 * g_unix_mount_point_free:
1167 * @mount_point: unix mount point to free.
1169 * Frees a unix mount point.
1172 g_unix_mount_point_free (GUnixMountPoint *mount_point)
1174 g_return_if_fail (mount_point != NULL);
1176 g_free (mount_point->mount_path);
1177 g_free (mount_point->device_path);
1178 g_free (mount_point->filesystem_type);
1179 g_free (mount_point);
1183 strcmp_null (const char *str1,
1188 if (str1 == NULL && str2 != NULL)
1190 if (str1 != NULL && str2 == NULL)
1192 return strcmp (str1, str2);
1196 * g_unix_mount_compare:
1197 * @mount1: first #GUnixMount to compare.
1198 * @mount2: second #GUnixMount to compare.
1200 * Compares two unix mounts.
1202 * Returns 1, 0 or -1 if @mount1 is greater than, equal to,
1203 * or less than @mount2, respectively.
1206 g_unix_mount_compare (GUnixMount *mount1,
1211 g_return_val_if_fail (mount1 != NULL && mount2 != NULL, 0);
1213 res = strcmp_null (mount1->mount_path, mount2->mount_path);
1217 res = strcmp_null (mount1->device_path, mount2->device_path);
1221 res = strcmp_null (mount1->filesystem_type, mount2->filesystem_type);
1225 res = mount1->is_read_only - mount2->is_read_only;
1233 * g_unix_mount_get_mount_path:
1234 * @mount_entry: input #GUnixMount to get the mount path for.
1236 * Gets the mount path for a unix mount.
1238 * Returns: the mount path for @mount_entry.
1241 g_unix_mount_get_mount_path (GUnixMount *mount_entry)
1243 g_return_val_if_fail (mount_entry != NULL, NULL);
1245 return mount_entry->mount_path;
1249 * g_unix_mount_get_device_path:
1250 * @mount_entry: a #GUnixMount.
1252 * Gets the device path for a unix mount.
1254 * Returns: a string containing the device path.
1257 g_unix_mount_get_device_path (GUnixMount *mount_entry)
1259 g_return_val_if_fail (mount_entry != NULL, NULL);
1261 return mount_entry->device_path;
1265 * g_unix_mount_get_fs_type:
1266 * @mount_entry: a #GUnixMount.
1268 * Gets the filesystem type for the unix mount.
1270 * Returns: a string containing the file system type.
1273 g_unix_mount_get_fs_type (GUnixMount *mount_entry)
1275 g_return_val_if_fail (mount_entry != NULL, NULL);
1277 return mount_entry->filesystem_type;
1281 * g_unix_mount_is_readonly:
1282 * @mount_entry: a #GUnixMount.
1284 * Checks if a unix mount is mounted read only.
1286 * Returns: %TRUE if @mount_entry is read only.
1289 g_unix_mount_is_readonly (GUnixMount *mount_entry)
1291 g_return_val_if_fail (mount_entry != NULL, FALSE);
1293 return mount_entry->is_read_only;
1297 * g_unix_mount_is_system_internal:
1298 * @mount_entry: a #GUnixMount.
1300 * Checks if a unix mount is a system path.
1302 * Returns: %TRUE if the unix mount is for a system path.
1305 g_unix_mount_is_system_internal (GUnixMount *mount_entry)
1307 g_return_val_if_fail (mount_entry != NULL, FALSE);
1309 return mount_entry->is_system_internal;
1313 * g_unix_mount_point_compare:
1314 * @mount1: a #GUnixMount.
1315 * @mount2: a #GUnixMount.
1317 * Compares two unix mount points.
1319 * Returns 1, 0 or -1 if @mount1 is greater than, equal to,
1320 * or less than @mount2, respectively.
1323 g_unix_mount_point_compare (GUnixMountPoint *mount1,
1324 GUnixMountPoint *mount2)
1328 g_return_val_if_fail (mount1 != NULL && mount2 != NULL, 0);
1330 res = strcmp_null (mount1->mount_path, mount2->mount_path);
1334 res = strcmp_null (mount1->device_path, mount2->device_path);
1338 res = strcmp_null (mount1->filesystem_type, mount2->filesystem_type);
1342 res = mount1->is_read_only - mount2->is_read_only;
1346 res = mount1->is_user_mountable - mount2->is_user_mountable;
1350 res = mount1->is_loopback - mount2->is_loopback;
1358 * g_unix_mount_point_get_mount_path:
1359 * @mount_point: a #GUnixMountPoint.
1361 * Gets the mount path for a unix mount point.
1363 * Returns: a string containing the mount path.
1366 g_unix_mount_point_get_mount_path (GUnixMountPoint *mount_point)
1368 g_return_val_if_fail (mount_point != NULL, NULL);
1370 return mount_point->mount_path;
1374 * g_unix_mount_point_get_device_path:
1375 * @mount_point: a #GUnixMountPoint.
1377 * Gets the device path for a unix mount point.
1379 * Returns: a string containing the device path.
1382 g_unix_mount_point_get_device_path (GUnixMountPoint *mount_point)
1384 g_return_val_if_fail (mount_point != NULL, NULL);
1386 return mount_point->device_path;
1390 * g_unix_mount_point_get_fs_type:
1391 * @mount_point: a #GUnixMountPoint.
1393 * Gets the file system type for the mount point.
1395 * Returns: a string containing the file system type.
1398 g_unix_mount_point_get_fs_type (GUnixMountPoint *mount_point)
1400 g_return_val_if_fail (mount_point != NULL, NULL);
1402 return mount_point->filesystem_type;
1406 * g_unix_mount_point_is_readonly:
1407 * @mount_point: a #GUnixMountPoint.
1409 * Checks if a unix mount point is read only.
1411 * Returns: %TRUE if a mount point is read only.
1414 g_unix_mount_point_is_readonly (GUnixMountPoint *mount_point)
1416 g_return_val_if_fail (mount_point != NULL, FALSE);
1418 return mount_point->is_read_only;
1422 * g_unix_mount_point_is_user_mountable:
1423 * @mount_point: a #GUnixMountPoint.
1425 * Checks if a unix mount point is mountable by the user.
1427 * Returns: %TRUE if the mount point is user mountable.
1430 g_unix_mount_point_is_user_mountable (GUnixMountPoint *mount_point)
1432 g_return_val_if_fail (mount_point != NULL, FALSE);
1434 return mount_point->is_user_mountable;
1438 * g_unix_mount_point_is_loopback:
1439 * @mount_point: a #GUnixMountPoint.
1441 * Checks if a unix mount point is a loopback device.
1443 * Returns: %TRUE if the mount point is a loopback. %FALSE otherwise.
1446 g_unix_mount_point_is_loopback (GUnixMountPoint *mount_point)
1448 g_return_val_if_fail (mount_point != NULL, FALSE);
1450 return mount_point->is_loopback;
1453 static GUnixMountType
1454 guess_mount_type (const char *mount_path,
1455 const char *device_path,
1456 const char *filesystem_type)
1458 GUnixMountType type;
1461 type = G_UNIX_MOUNT_TYPE_UNKNOWN;
1463 if ((strcmp (filesystem_type, "udf") == 0) ||
1464 (strcmp (filesystem_type, "iso9660") == 0) ||
1465 (strcmp (filesystem_type, "cd9660") == 0))
1466 type = G_UNIX_MOUNT_TYPE_CDROM;
1467 else if (strcmp (filesystem_type, "nfs") == 0)
1468 type = G_UNIX_MOUNT_TYPE_NFS;
1469 else if (g_str_has_prefix (device_path, "/vol/dev/diskette/") ||
1470 g_str_has_prefix (device_path, "/dev/fd") ||
1471 g_str_has_prefix (device_path, "/dev/floppy"))
1472 type = G_UNIX_MOUNT_TYPE_FLOPPY;
1473 else if (g_str_has_prefix (device_path, "/dev/cdrom") ||
1474 g_str_has_prefix (device_path, "/dev/acd") ||
1475 g_str_has_prefix (device_path, "/dev/cd"))
1476 type = G_UNIX_MOUNT_TYPE_CDROM;
1477 else if (g_str_has_prefix (device_path, "/vol/"))
1479 const char *name = mount_path + strlen ("/");
1481 if (g_str_has_prefix (name, "cdrom"))
1482 type = G_UNIX_MOUNT_TYPE_CDROM;
1483 else if (g_str_has_prefix (name, "floppy") ||
1484 g_str_has_prefix (device_path, "/vol/dev/diskette/"))
1485 type = G_UNIX_MOUNT_TYPE_FLOPPY;
1486 else if (g_str_has_prefix (name, "rmdisk"))
1487 type = G_UNIX_MOUNT_TYPE_ZIP;
1488 else if (g_str_has_prefix (name, "jaz"))
1489 type = G_UNIX_MOUNT_TYPE_JAZ;
1490 else if (g_str_has_prefix (name, "memstick"))
1491 type = G_UNIX_MOUNT_TYPE_MEMSTICK;
1495 basename = g_path_get_basename (mount_path);
1497 if (g_str_has_prefix (basename, "cdrom") ||
1498 g_str_has_prefix (basename, "cdwriter") ||
1499 g_str_has_prefix (basename, "burn") ||
1500 g_str_has_prefix (basename, "cdr") ||
1501 g_str_has_prefix (basename, "cdrw") ||
1502 g_str_has_prefix (basename, "dvdrom") ||
1503 g_str_has_prefix (basename, "dvdram") ||
1504 g_str_has_prefix (basename, "dvdr") ||
1505 g_str_has_prefix (basename, "dvdrw") ||
1506 g_str_has_prefix (basename, "cdrom_dvdrom") ||
1507 g_str_has_prefix (basename, "cdrom_dvdram") ||
1508 g_str_has_prefix (basename, "cdrom_dvdr") ||
1509 g_str_has_prefix (basename, "cdrom_dvdrw") ||
1510 g_str_has_prefix (basename, "cdr_dvdrom") ||
1511 g_str_has_prefix (basename, "cdr_dvdram") ||
1512 g_str_has_prefix (basename, "cdr_dvdr") ||
1513 g_str_has_prefix (basename, "cdr_dvdrw") ||
1514 g_str_has_prefix (basename, "cdrw_dvdrom") ||
1515 g_str_has_prefix (basename, "cdrw_dvdram") ||
1516 g_str_has_prefix (basename, "cdrw_dvdr") ||
1517 g_str_has_prefix (basename, "cdrw_dvdrw"))
1518 type = G_UNIX_MOUNT_TYPE_CDROM;
1519 else if (g_str_has_prefix (basename, "floppy"))
1520 type = G_UNIX_MOUNT_TYPE_FLOPPY;
1521 else if (g_str_has_prefix (basename, "zip"))
1522 type = G_UNIX_MOUNT_TYPE_ZIP;
1523 else if (g_str_has_prefix (basename, "jaz"))
1524 type = G_UNIX_MOUNT_TYPE_JAZ;
1525 else if (g_str_has_prefix (basename, "camera"))
1526 type = G_UNIX_MOUNT_TYPE_CAMERA;
1527 else if (g_str_has_prefix (basename, "memstick") ||
1528 g_str_has_prefix (basename, "memory_stick") ||
1529 g_str_has_prefix (basename, "ram"))
1530 type = G_UNIX_MOUNT_TYPE_MEMSTICK;
1531 else if (g_str_has_prefix (basename, "compact_flash"))
1532 type = G_UNIX_MOUNT_TYPE_CF;
1533 else if (g_str_has_prefix (basename, "smart_media"))
1534 type = G_UNIX_MOUNT_TYPE_SM;
1535 else if (g_str_has_prefix (basename, "sd_mmc"))
1536 type = G_UNIX_MOUNT_TYPE_SDMMC;
1537 else if (g_str_has_prefix (basename, "ipod"))
1538 type = G_UNIX_MOUNT_TYPE_IPOD;
1543 if (type == G_UNIX_MOUNT_TYPE_UNKNOWN)
1544 type = G_UNIX_MOUNT_TYPE_HD;
1550 * g_unix_mount_guess_type:
1551 * @mount_entry: a #GUnixMount.
1553 * Guesses the type of a unix mount. If the mount type cannot be
1554 * determined, returns %G_UNIX_MOUNT_TYPE_UNKNOWN.
1556 * Returns: a #GUnixMountType.
1559 g_unix_mount_guess_type (GUnixMount *mount_entry)
1561 g_return_val_if_fail (mount_entry != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
1562 g_return_val_if_fail (mount_entry->mount_path != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
1563 g_return_val_if_fail (mount_entry->device_path != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
1564 g_return_val_if_fail (mount_entry->filesystem_type != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
1566 return guess_mount_type (mount_entry->mount_path,
1567 mount_entry->device_path,
1568 mount_entry->filesystem_type);
1572 * g_unix_mount_point_guess_type:
1573 * @mount_point: a #GUnixMountPoint.
1575 * Guesses the type of a unix mount point. If the mount type cannot be
1576 * determined, returns %G_UNIX_MOUNT_TYPE_UNKNOWN.
1578 * Returns: a #GUnixMountType.
1581 g_unix_mount_point_guess_type (GUnixMountPoint *mount_point)
1583 g_return_val_if_fail (mount_point != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
1584 g_return_val_if_fail (mount_point->mount_path != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
1585 g_return_val_if_fail (mount_point->device_path != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
1586 g_return_val_if_fail (mount_point->filesystem_type != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
1588 return guess_mount_type (mount_point->mount_path,
1589 mount_point->device_path,
1590 mount_point->filesystem_type);