1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
3 /* GIO - GLib Input, Output and Streaming Library
5 * Copyright (C) 2006-2007 Red Hat, Inc.
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General
18 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
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>
46 #include <sys/statfs.h>
48 #if HAVE_SYS_STATVFS_H
49 #include <sys/statvfs.h>
53 #elif HAVE_SYS_MOUNT_H
55 #include <sys/param.h>
57 #include <sys/mount.h>
64 #include "gunixmounts.h"
66 #include "gfilemonitor.h"
68 #include "gthemedicon.h"
72 static const char *_resolve_dev_root (void);
77 * @include: gio/gunixmounts.h
78 * @short_description: UNIX mounts
80 * Routines for managing mounted UNIX mount points and paths.
82 * Note that `<gio/gunixmounts.h>` belongs to the UNIX-specific GIO
83 * interfaces, thus you have to use the `gio-unix-2.0.pc` pkg-config
89 * @G_UNIX_MOUNT_TYPE_UNKNOWN: Unknown UNIX mount type.
90 * @G_UNIX_MOUNT_TYPE_FLOPPY: Floppy disk UNIX mount type.
91 * @G_UNIX_MOUNT_TYPE_CDROM: CDROM UNIX mount type.
92 * @G_UNIX_MOUNT_TYPE_NFS: Network File System (NFS) UNIX mount type.
93 * @G_UNIX_MOUNT_TYPE_ZIP: ZIP UNIX mount type.
94 * @G_UNIX_MOUNT_TYPE_JAZ: JAZZ UNIX mount type.
95 * @G_UNIX_MOUNT_TYPE_MEMSTICK: Memory Stick UNIX mount type.
96 * @G_UNIX_MOUNT_TYPE_CF: Compact Flash UNIX mount type.
97 * @G_UNIX_MOUNT_TYPE_SM: Smart Media UNIX mount type.
98 * @G_UNIX_MOUNT_TYPE_SDMMC: SD/MMC UNIX mount type.
99 * @G_UNIX_MOUNT_TYPE_IPOD: iPod UNIX mount type.
100 * @G_UNIX_MOUNT_TYPE_CAMERA: Digital camera UNIX mount type.
101 * @G_UNIX_MOUNT_TYPE_HD: Hard drive UNIX mount type.
103 * Types of UNIX mounts.
106 G_UNIX_MOUNT_TYPE_UNKNOWN,
107 G_UNIX_MOUNT_TYPE_FLOPPY,
108 G_UNIX_MOUNT_TYPE_CDROM,
109 G_UNIX_MOUNT_TYPE_NFS,
110 G_UNIX_MOUNT_TYPE_ZIP,
111 G_UNIX_MOUNT_TYPE_JAZ,
112 G_UNIX_MOUNT_TYPE_MEMSTICK,
113 G_UNIX_MOUNT_TYPE_CF,
114 G_UNIX_MOUNT_TYPE_SM,
115 G_UNIX_MOUNT_TYPE_SDMMC,
116 G_UNIX_MOUNT_TYPE_IPOD,
117 G_UNIX_MOUNT_TYPE_CAMERA,
121 struct _GUnixMountEntry {
124 char *filesystem_type;
125 gboolean is_read_only;
126 gboolean is_system_internal;
129 struct _GUnixMountPoint {
132 char *filesystem_type;
134 gboolean is_read_only;
135 gboolean is_user_mountable;
136 gboolean is_loopback;
145 static guint signals[LAST_SIGNAL];
147 struct _GUnixMountMonitor {
150 GFileMonitor *fstab_monitor;
151 GFileMonitor *mtab_monitor;
153 GList *mount_poller_mounts;
155 GSource *proc_mounts_watch_source;
158 struct _GUnixMountMonitorClass {
159 GObjectClass parent_class;
162 static GUnixMountMonitor *the_mount_monitor = NULL;
164 static GList *_g_get_unix_mounts (void);
165 static GList *_g_get_unix_mount_points (void);
167 static guint64 mount_poller_time = 0;
169 G_DEFINE_TYPE (GUnixMountMonitor, g_unix_mount_monitor, G_TYPE_OBJECT);
171 #define MOUNT_POLL_INTERVAL 4000
173 #ifdef HAVE_SYS_MNTTAB_H
174 #define MNTOPT_RO "ro"
179 #elif defined (HAVE_SYS_MNTTAB_H)
180 #include <sys/mnttab.h>
183 #ifdef HAVE_SYS_VFSTAB_H
184 #include <sys/vfstab.h>
187 #if defined(HAVE_SYS_MNTCTL_H) && defined(HAVE_SYS_VMOUNT_H) && defined(HAVE_SYS_VFS_H)
188 #include <sys/mntctl.h>
190 #include <sys/vmount.h>
194 #if (defined(HAVE_GETVFSSTAT) || defined(HAVE_GETFSSTAT)) && defined(HAVE_FSTAB_H) && defined(HAVE_SYS_MOUNT_H)
195 #include <sys/param.h>
196 #include <sys/ucred.h>
197 #include <sys/mount.h>
199 #ifdef HAVE_SYS_SYSCTL_H
200 #include <sys/sysctl.h>
204 #ifndef HAVE_SETMNTENT
205 #define setmntent(f,m) fopen(f,m)
207 #ifndef HAVE_ENDMNTENT
208 #define endmntent(f) fclose(f)
212 is_in (const char *value, const char *set[])
215 for (i = 0; set[i] != NULL; i++)
217 if (strcmp (set[i], value) == 0)
224 * g_unix_is_mount_path_system_internal:
225 * @mount_path: a mount path, e.g. `/media/disk` or `/usr`
227 * Determines if @mount_path is considered an implementation of the
228 * OS. This is primarily used for hiding mountable and mounted volumes
229 * that only are used in the OS and has little to no relevance to the
232 * Returns: %TRUE if @mount_path is considered an implementation detail
236 g_unix_is_mount_path_system_internal (const char *mount_path)
238 const char *ignore_mountpoints[] = {
239 /* Includes all FHS 2.3 toplevel dirs and other specilized
240 * directories that we want to hide from the user.
242 "/", /* we already have "Filesystem root" in Nautilus */
245 "/compat/linux/proc",
274 "/var/log/audit", /* https://bugzilla.redhat.com/show_bug.cgi?id=333041 */
277 "/var/tmp", /* https://bugzilla.redhat.com/show_bug.cgi?id=335241 */
285 if (is_in (mount_path, ignore_mountpoints))
288 if (g_str_has_prefix (mount_path, "/dev/") ||
289 g_str_has_prefix (mount_path, "/proc/") ||
290 g_str_has_prefix (mount_path, "/sys/"))
293 if (g_str_has_suffix (mount_path, "/.gvfs"))
300 guess_system_internal (const char *mountpoint,
304 const char *ignore_fs[] = {
328 const char *ignore_devices[] = {
338 if (is_in (fs, ignore_fs))
341 if (is_in (device, ignore_devices))
344 if (g_unix_is_mount_path_system_internal (mountpoint))
353 get_mtab_read_file (void)
357 return "/proc/mounts";
359 return _PATH_MOUNTED;
367 get_mtab_monitor_file (void)
371 return "/proc/mounts";
373 return _PATH_MOUNTED;
380 #ifndef HAVE_GETMNTENT_R
381 G_LOCK_DEFINE_STATIC(getmntent);
385 _g_get_unix_mounts (void)
387 #ifdef HAVE_GETMNTENT_R
391 struct mntent *mntent;
394 GUnixMountEntry *mount_entry;
395 GHashTable *mounts_hash;
398 read_file = get_mtab_read_file ();
400 file = setmntent (read_file, "r");
406 mounts_hash = g_hash_table_new (g_str_hash, g_str_equal);
408 #ifdef HAVE_GETMNTENT_R
409 while ((mntent = getmntent_r (file, &ent, buf, sizeof (buf))) != NULL)
412 while ((mntent = getmntent (file)) != NULL)
415 /* ignore any mnt_fsname that is repeated and begins with a '/'
417 * We do this to avoid being fooled by --bind mounts, since
418 * these have the same device as the location they bind to.
419 * It's not an ideal solution to the problem, but it's likely that
420 * the most important mountpoint is first and the --bind ones after
421 * that aren't as important. So it should work.
423 * The '/' is to handle procfs, tmpfs and other no device mounts.
425 if (mntent->mnt_fsname != NULL &&
426 mntent->mnt_fsname[0] == '/' &&
427 g_hash_table_lookup (mounts_hash, mntent->mnt_fsname))
430 mount_entry = g_new0 (GUnixMountEntry, 1);
431 mount_entry->mount_path = g_strdup (mntent->mnt_dir);
432 if (g_strcmp0 (mntent->mnt_fsname, "/dev/root") == 0)
433 mount_entry->device_path = g_strdup (_resolve_dev_root ());
435 mount_entry->device_path = g_strdup (mntent->mnt_fsname);
436 mount_entry->filesystem_type = g_strdup (mntent->mnt_type);
438 #if defined (HAVE_HASMNTOPT)
439 if (hasmntopt (mntent, MNTOPT_RO) != NULL)
440 mount_entry->is_read_only = TRUE;
443 mount_entry->is_system_internal =
444 guess_system_internal (mount_entry->mount_path,
445 mount_entry->filesystem_type,
446 mount_entry->device_path);
448 g_hash_table_insert (mounts_hash,
449 mount_entry->device_path,
450 mount_entry->device_path);
452 return_list = g_list_prepend (return_list, mount_entry);
454 g_hash_table_destroy (mounts_hash);
458 #ifndef HAVE_GETMNTENT_R
459 G_UNLOCK (getmntent);
462 return g_list_reverse (return_list);
465 #elif defined (HAVE_SYS_MNTTAB_H)
467 G_LOCK_DEFINE_STATIC(getmntent);
470 get_mtab_read_file (void)
473 return _PATH_MOUNTED;
475 return "/etc/mnttab";
480 get_mtab_monitor_file (void)
482 return get_mtab_read_file ();
486 _g_get_unix_mounts (void)
488 struct mnttab mntent;
491 GUnixMountEntry *mount_entry;
494 read_file = get_mtab_read_file ();
496 file = setmntent (read_file, "r");
503 while (! getmntent (file, &mntent))
505 mount_entry = g_new0 (GUnixMountEntry, 1);
507 mount_entry->mount_path = g_strdup (mntent.mnt_mountp);
508 mount_entry->device_path = g_strdup (mntent.mnt_special);
509 mount_entry->filesystem_type = g_strdup (mntent.mnt_fstype);
511 #if defined (HAVE_HASMNTOPT)
512 if (hasmntopt (&mntent, MNTOPT_RO) != NULL)
513 mount_entry->is_read_only = TRUE;
516 mount_entry->is_system_internal =
517 guess_system_internal (mount_entry->mount_path,
518 mount_entry->filesystem_type,
519 mount_entry->device_path);
521 return_list = g_list_prepend (return_list, mount_entry);
526 G_UNLOCK (getmntent);
528 return g_list_reverse (return_list);
531 #elif defined(HAVE_SYS_MNTCTL_H) && defined(HAVE_SYS_VMOUNT_H) && defined(HAVE_SYS_VFS_H)
534 get_mtab_monitor_file (void)
540 _g_get_unix_mounts (void)
542 struct vfs_ent *fs_info;
543 struct vmount *vmount_info;
545 unsigned int vmount_size;
549 if (mntctl (MCTL_QUERY, sizeof (vmount_size), &vmount_size) != 0)
551 g_warning ("Unable to know the number of mounted volumes\n");
556 vmount_info = (struct vmount*)g_malloc (vmount_size);
558 vmount_number = mntctl (MCTL_QUERY, vmount_size, vmount_info);
560 if (vmount_info->vmt_revision != VMT_REVISION)
561 g_warning ("Bad vmount structure revision number, want %d, got %d\n", VMT_REVISION, vmount_info->vmt_revision);
563 if (vmount_number < 0)
565 g_warning ("Unable to recover mounted volumes information\n");
567 g_free (vmount_info);
572 while (vmount_number > 0)
574 mount_entry = g_new0 (GUnixMountEntry, 1);
576 mount_entry->device_path = g_strdup (vmt2dataptr (vmount_info, VMT_OBJECT));
577 mount_entry->mount_path = g_strdup (vmt2dataptr (vmount_info, VMT_STUB));
578 /* is_removable = (vmount_info->vmt_flags & MNT_REMOVABLE) ? 1 : 0; */
579 mount_entry->is_read_only = (vmount_info->vmt_flags & MNT_READONLY) ? 1 : 0;
581 fs_info = getvfsbytype (vmount_info->vmt_gfstype);
584 mount_entry->filesystem_type = g_strdup ("unknown");
586 mount_entry->filesystem_type = g_strdup (fs_info->vfsent_name);
588 mount_entry->is_system_internal =
589 guess_system_internal (mount_entry->mount_path,
590 mount_entry->filesystem_type,
591 mount_entry->device_path);
593 return_list = g_list_prepend (return_list, mount_entry);
595 vmount_info = (struct vmount *)( (char*)vmount_info
596 + vmount_info->vmt_length);
600 g_free (vmount_info);
602 return g_list_reverse (return_list);
605 #elif (defined(HAVE_GETVFSSTAT) || defined(HAVE_GETFSSTAT)) && defined(HAVE_FSTAB_H) && defined(HAVE_SYS_MOUNT_H)
608 get_mtab_monitor_file (void)
614 _g_get_unix_mounts (void)
616 #if defined(USE_STATVFS)
617 struct statvfs *mntent = NULL;
618 #elif defined(USE_STATFS)
619 struct statfs *mntent = NULL;
621 #error statfs juggling failed
625 GUnixMountEntry *mount_entry;
628 /* Pass NOWAIT to avoid blocking trying to update NFS mounts. */
629 #if defined(USE_STATVFS) && defined(HAVE_GETVFSSTAT)
630 num_mounts = getvfsstat (NULL, 0, ST_NOWAIT);
631 #elif defined(USE_STATFS) && defined(HAVE_GETFSSTAT)
632 num_mounts = getfsstat (NULL, 0, MNT_NOWAIT);
634 if (num_mounts == -1)
637 bufsize = num_mounts * sizeof (*mntent);
638 mntent = g_malloc (bufsize);
639 #if defined(USE_STATVFS) && defined(HAVE_GETVFSSTAT)
640 num_mounts = getvfsstat (mntent, bufsize, ST_NOWAIT);
641 #elif defined(USE_STATFS) && defined(HAVE_GETFSSTAT)
642 num_mounts = getfsstat (mntent, bufsize, MNT_NOWAIT);
644 if (num_mounts == -1)
649 for (i = 0; i < num_mounts; i++)
651 mount_entry = g_new0 (GUnixMountEntry, 1);
653 mount_entry->mount_path = g_strdup (mntent[i].f_mntonname);
654 mount_entry->device_path = g_strdup (mntent[i].f_mntfromname);
655 mount_entry->filesystem_type = g_strdup (mntent[i].f_fstypename);
657 #if defined(USE_STATVFS)
658 if (mntent[i].f_flag & ST_RDONLY)
659 #elif defined(USE_STATFS)
660 if (mntent[i].f_flags & MNT_RDONLY)
662 #error statfs juggling failed
664 mount_entry->is_read_only = TRUE;
666 mount_entry->is_system_internal =
667 guess_system_internal (mount_entry->mount_path,
668 mount_entry->filesystem_type,
669 mount_entry->device_path);
671 return_list = g_list_prepend (return_list, mount_entry);
676 return g_list_reverse (return_list);
678 #elif defined(__INTERIX)
681 get_mtab_monitor_file (void)
687 _g_get_unix_mounts (void)
690 GList* return_list = NULL;
691 char filename[9 + NAME_MAX];
693 dirp = opendir ("/dev/fs");
696 g_warning ("unable to read /dev/fs!");
702 struct statvfs statbuf;
704 struct dirent* result;
706 if (readdir_r (dirp, &entry, &result) || result == NULL)
709 strcpy (filename, "/dev/fs/");
710 strcat (filename, entry.d_name);
712 if (statvfs (filename, &statbuf) == 0)
714 GUnixMountEntry* mount_entry = g_new0(GUnixMountEntry, 1);
716 mount_entry->mount_path = g_strdup (statbuf.f_mntonname);
717 mount_entry->device_path = g_strdup (statbuf.f_mntfromname);
718 mount_entry->filesystem_type = g_strdup (statbuf.f_fstypename);
720 if (statbuf.f_flag & ST_RDONLY)
721 mount_entry->is_read_only = TRUE;
723 return_list = g_list_prepend(return_list, mount_entry);
727 return_list = g_list_reverse (return_list);
734 #error No _g_get_unix_mounts() implementation for system
737 /* _g_get_unix_mount_points():
739 * don't return swap and ignore mounts.
743 get_fstab_file (void)
745 #if defined(HAVE_SYS_MNTCTL_H) && defined(HAVE_SYS_VMOUNT_H) && defined(HAVE_SYS_VFS_H)
747 return "/etc/filesystems";
748 #elif defined(_PATH_MNTTAB)
750 #elif defined(VFSTAB)
759 _g_get_unix_mount_points (void)
761 #ifdef HAVE_GETMNTENT_R
765 struct mntent *mntent;
768 GUnixMountPoint *mount_entry;
771 read_file = get_fstab_file ();
773 file = setmntent (read_file, "r");
779 #ifdef HAVE_GETMNTENT_R
780 while ((mntent = getmntent_r (file, &ent, buf, sizeof (buf))) != NULL)
783 while ((mntent = getmntent (file)) != NULL)
786 if ((strcmp (mntent->mnt_dir, "ignore") == 0) ||
787 (strcmp (mntent->mnt_dir, "swap") == 0) ||
788 (strcmp (mntent->mnt_dir, "none") == 0))
791 #ifdef HAVE_HASMNTOPT
792 /* We ignore bind fstab entries, as we ignore bind mounts anyway */
793 if (hasmntopt (mntent, "bind"))
797 mount_entry = g_new0 (GUnixMountPoint, 1);
798 mount_entry->mount_path = g_strdup (mntent->mnt_dir);
799 if (strcmp (mntent->mnt_fsname, "/dev/root") == 0)
800 mount_entry->device_path = g_strdup (_resolve_dev_root ());
802 mount_entry->device_path = g_strdup (mntent->mnt_fsname);
803 mount_entry->filesystem_type = g_strdup (mntent->mnt_type);
804 mount_entry->options = g_strdup (mntent->mnt_opts);
806 #ifdef HAVE_HASMNTOPT
807 if (hasmntopt (mntent, MNTOPT_RO) != NULL)
808 mount_entry->is_read_only = TRUE;
810 if (hasmntopt (mntent, "loop") != NULL)
811 mount_entry->is_loopback = TRUE;
815 if ((mntent->mnt_type != NULL && strcmp ("supermount", mntent->mnt_type) == 0)
816 #ifdef HAVE_HASMNTOPT
817 || (hasmntopt (mntent, "user") != NULL
818 && hasmntopt (mntent, "user") != hasmntopt (mntent, "user_xattr"))
819 || hasmntopt (mntent, "pamconsole") != NULL
820 || hasmntopt (mntent, "users") != NULL
821 || hasmntopt (mntent, "owner") != NULL
824 mount_entry->is_user_mountable = TRUE;
826 return_list = g_list_prepend (return_list, mount_entry);
831 #ifndef HAVE_GETMNTENT_R
832 G_UNLOCK (getmntent);
835 return g_list_reverse (return_list);
838 #elif defined (HAVE_SYS_MNTTAB_H)
841 _g_get_unix_mount_points (void)
843 struct mnttab mntent;
846 GUnixMountPoint *mount_entry;
849 read_file = get_fstab_file ();
851 file = setmntent (read_file, "r");
858 while (! getmntent (file, &mntent))
860 if ((strcmp (mntent.mnt_mountp, "ignore") == 0) ||
861 (strcmp (mntent.mnt_mountp, "swap") == 0) ||
862 (strcmp (mntent.mnt_mountp, "none") == 0))
865 mount_entry = g_new0 (GUnixMountPoint, 1);
867 mount_entry->mount_path = g_strdup (mntent.mnt_mountp);
868 mount_entry->device_path = g_strdup (mntent.mnt_special);
869 mount_entry->filesystem_type = g_strdup (mntent.mnt_fstype);
870 mount_entry->options = g_strdup (mntent.mnt_mntopts);
872 #ifdef HAVE_HASMNTOPT
873 if (hasmntopt (&mntent, MNTOPT_RO) != NULL)
874 mount_entry->is_read_only = TRUE;
876 if (hasmntopt (&mntent, "lofs") != NULL)
877 mount_entry->is_loopback = TRUE;
880 if ((mntent.mnt_fstype != NULL)
881 #ifdef HAVE_HASMNTOPT
882 || (hasmntopt (&mntent, "user") != NULL
883 && hasmntopt (&mntent, "user") != hasmntopt (&mntent, "user_xattr"))
884 || hasmntopt (&mntent, "pamconsole") != NULL
885 || hasmntopt (&mntent, "users") != NULL
886 || hasmntopt (&mntent, "owner") != NULL
889 mount_entry->is_user_mountable = TRUE;
891 return_list = g_list_prepend (return_list, mount_entry);
895 G_UNLOCK (getmntent);
897 return g_list_reverse (return_list);
899 #elif defined(HAVE_SYS_MNTCTL_H) && defined(HAVE_SYS_VMOUNT_H) && defined(HAVE_SYS_VFS_H)
901 /* functions to parse /etc/filesystems on aix */
903 /* read character, ignoring comments (begin with '*', end with '\n' */
905 aix_fs_getc (FILE *fd)
909 while ((c = getc (fd)) == '*')
911 while (((c = getc (fd)) != '\n') && (c != EOF))
916 /* eat all continuous spaces in a file */
918 aix_fs_ignorespace (FILE *fd)
922 while ((c = aix_fs_getc (fd)) != EOF)
924 if (!g_ascii_isspace (c))
934 /* read one word from file */
936 aix_fs_getword (FILE *fd,
941 aix_fs_ignorespace (fd);
943 while (((c = aix_fs_getc (fd)) != EOF) && !g_ascii_isspace (c))
947 while (((c = aix_fs_getc (fd)) != EOF) && (c != '"'))
959 char mnt_mount[PATH_MAX];
960 char mnt_special[PATH_MAX];
962 char mnt_options[128];
963 } AixMountTableEntry;
965 /* read mount points properties */
967 aix_fs_get (FILE *fd,
968 AixMountTableEntry *prop)
970 static char word[PATH_MAX] = { 0 };
971 char value[PATH_MAX];
976 if (aix_fs_getword (fd, word) == EOF)
980 word[strlen(word) - 1] = 0;
981 strcpy (prop->mnt_mount, word);
983 /* read attributes and value */
985 while (aix_fs_getword (fd, word) != EOF)
987 /* test if is attribute or new stanza */
988 if (word[strlen(word) - 1] == ':')
992 aix_fs_getword (fd, value);
995 aix_fs_getword (fd, value);
997 if (strcmp (word, "dev") == 0)
998 strcpy (prop->mnt_special, value);
999 else if (strcmp (word, "vfs") == 0)
1000 strcpy (prop->mnt_fstype, value);
1001 else if (strcmp (word, "options") == 0)
1002 strcpy(prop->mnt_options, value);
1009 _g_get_unix_mount_points (void)
1011 struct mntent *mntent;
1014 GUnixMountPoint *mount_entry;
1015 AixMountTableEntry mntent;
1018 read_file = get_fstab_file ();
1020 file = setmntent (read_file, "r");
1026 while (!aix_fs_get (file, &mntent))
1028 if (strcmp ("cdrfs", mntent.mnt_fstype) == 0)
1030 mount_entry = g_new0 (GUnixMountPoint, 1);
1032 mount_entry->mount_path = g_strdup (mntent.mnt_mount);
1033 mount_entry->device_path = g_strdup (mntent.mnt_special);
1034 mount_entry->filesystem_type = g_strdup (mntent.mnt_fstype);
1035 mount_entry->options = g_strdup (mntent.mnt_options);
1036 mount_entry->is_read_only = TRUE;
1037 mount_entry->is_user_mountable = TRUE;
1039 return_list = g_list_prepend (return_list, mount_entry);
1045 return g_list_reverse (return_list);
1048 #elif (defined(HAVE_GETVFSSTAT) || defined(HAVE_GETFSSTAT)) && defined(HAVE_FSTAB_H) && defined(HAVE_SYS_MOUNT_H)
1051 _g_get_unix_mount_points (void)
1053 struct fstab *fstab = NULL;
1054 GUnixMountPoint *mount_entry;
1056 #ifdef HAVE_SYS_SYSCTL_H
1058 size_t len = sizeof(usermnt);
1067 #ifdef HAVE_SYS_SYSCTL_H
1068 #if defined(HAVE_SYSCTLBYNAME)
1069 sysctlbyname ("vfs.usermount", &usermnt, &len, NULL, 0);
1070 #elif defined(CTL_VFS) && defined(VFS_USERMOUNT)
1075 mib[1] = VFS_USERMOUNT;
1076 sysctl (mib, 2, &usermnt, &len, NULL, 0);
1078 #elif defined(CTL_KERN) && defined(KERN_USERMOUNT)
1083 mib[1] = KERN_USERMOUNT;
1084 sysctl (mib, 2, &usermnt, &len, NULL, 0);
1089 while ((fstab = getfsent ()) != NULL)
1091 if (strcmp (fstab->fs_vfstype, "swap") == 0)
1094 mount_entry = g_new0 (GUnixMountPoint, 1);
1096 mount_entry->mount_path = g_strdup (fstab->fs_file);
1097 mount_entry->device_path = g_strdup (fstab->fs_spec);
1098 mount_entry->filesystem_type = g_strdup (fstab->fs_vfstype);
1099 mount_entry->options = g_strdup (fstab->fs_mntops);
1101 if (strcmp (fstab->fs_type, "ro") == 0)
1102 mount_entry->is_read_only = TRUE;
1104 #ifdef HAVE_SYS_SYSCTL_H
1107 uid_t uid = getuid ();
1108 if (stat (fstab->fs_file, &sb) == 0)
1110 if (uid == 0 || sb.st_uid == uid)
1111 mount_entry->is_user_mountable = TRUE;
1116 return_list = g_list_prepend (return_list, mount_entry);
1121 return g_list_reverse (return_list);
1123 #elif defined(__INTERIX)
1125 _g_get_unix_mount_points (void)
1127 return _g_get_unix_mounts ();
1130 #error No g_get_mount_table() implementation for system
1134 get_mounts_timestamp (void)
1136 const char *monitor_file;
1139 monitor_file = get_mtab_monitor_file ();
1142 if (stat (monitor_file, &buf) == 0)
1143 return (guint64)buf.st_mtime;
1147 return mount_poller_time;
1153 get_mount_points_timestamp (void)
1155 const char *monitor_file;
1158 monitor_file = get_fstab_file ();
1161 if (stat (monitor_file, &buf) == 0)
1162 return (guint64)buf.st_mtime;
1168 * g_unix_mounts_get: (skip)
1169 * @time_read: (out) (allow-none): guint64 to contain a timestamp, or %NULL
1171 * Gets a #GList of #GUnixMountEntry containing the unix mounts.
1172 * If @time_read is set, it will be filled with the mount
1173 * timestamp, allowing for checking if the mounts have changed
1174 * with g_unix_mounts_changed_since().
1176 * Returns: (element-type GUnixMountEntry) (transfer full):
1177 * a #GList of the UNIX mounts.
1180 g_unix_mounts_get (guint64 *time_read)
1183 *time_read = get_mounts_timestamp ();
1185 return _g_get_unix_mounts ();
1189 * g_unix_mount_at: (skip)
1190 * @mount_path: path for a possible unix mount.
1191 * @time_read: (out) (allow-none): guint64 to contain a timestamp.
1193 * Gets a #GUnixMountEntry for a given mount path. If @time_read
1194 * is set, it will be filled with a unix timestamp for checking
1195 * if the mounts have changed since with g_unix_mounts_changed_since().
1197 * Returns: (transfer full): a #GUnixMountEntry.
1200 g_unix_mount_at (const char *mount_path,
1204 GUnixMountEntry *mount_entry, *found;
1206 mounts = g_unix_mounts_get (time_read);
1209 for (l = mounts; l != NULL; l = l->next)
1211 mount_entry = l->data;
1213 if (!found && strcmp (mount_path, mount_entry->mount_path) == 0)
1214 found = mount_entry;
1216 g_unix_mount_free (mount_entry);
1218 g_list_free (mounts);
1224 * g_unix_mount_points_get: (skip)
1225 * @time_read: (out) (allow-none): guint64 to contain a timestamp.
1227 * Gets a #GList of #GUnixMountPoint containing the unix mount points.
1228 * If @time_read is set, it will be filled with the mount timestamp,
1229 * allowing for checking if the mounts have changed with
1230 * g_unix_mount_points_changed_since().
1232 * Returns: (element-type GUnixMountPoint) (transfer full):
1233 * a #GList of the UNIX mountpoints.
1236 g_unix_mount_points_get (guint64 *time_read)
1239 *time_read = get_mount_points_timestamp ();
1241 return _g_get_unix_mount_points ();
1245 * g_unix_mounts_changed_since:
1246 * @time: guint64 to contain a timestamp.
1248 * Checks if the unix mounts have changed since a given unix time.
1250 * Returns: %TRUE if the mounts have changed since @time.
1253 g_unix_mounts_changed_since (guint64 time)
1255 return get_mounts_timestamp () != time;
1259 * g_unix_mount_points_changed_since:
1260 * @time: guint64 to contain a timestamp.
1262 * Checks if the unix mount points have changed since a given unix time.
1264 * Returns: %TRUE if the mount points have changed since @time.
1267 g_unix_mount_points_changed_since (guint64 time)
1269 return get_mount_points_timestamp () != time;
1273 g_unix_mount_monitor_finalize (GObject *object)
1275 GUnixMountMonitor *monitor;
1277 monitor = G_UNIX_MOUNT_MONITOR (object);
1279 if (monitor->fstab_monitor)
1281 g_file_monitor_cancel (monitor->fstab_monitor);
1282 g_object_unref (monitor->fstab_monitor);
1285 if (monitor->proc_mounts_watch_source != NULL)
1286 g_source_destroy (monitor->proc_mounts_watch_source);
1288 if (monitor->mtab_monitor)
1290 g_file_monitor_cancel (monitor->mtab_monitor);
1291 g_object_unref (monitor->mtab_monitor);
1294 if (monitor->mount_poller_mounts != NULL)
1296 g_list_foreach (monitor->mount_poller_mounts, (GFunc)g_unix_mount_free, NULL);
1297 g_list_free (monitor->mount_poller_mounts);
1300 the_mount_monitor = NULL;
1302 G_OBJECT_CLASS (g_unix_mount_monitor_parent_class)->finalize (object);
1307 g_unix_mount_monitor_class_init (GUnixMountMonitorClass *klass)
1309 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
1311 gobject_class->finalize = g_unix_mount_monitor_finalize;
1314 * GUnixMountMonitor::mounts-changed:
1315 * @monitor: the object on which the signal is emitted
1317 * Emitted when the unix mounts have changed.
1319 signals[MOUNTS_CHANGED] =
1320 g_signal_new ("mounts-changed",
1321 G_TYPE_FROM_CLASS (klass),
1325 g_cclosure_marshal_VOID__VOID,
1329 * GUnixMountMonitor::mountpoints-changed:
1330 * @monitor: the object on which the signal is emitted
1332 * Emitted when the unix mount points have changed.
1334 signals[MOUNTPOINTS_CHANGED] =
1335 g_signal_new ("mountpoints-changed",
1336 G_TYPE_FROM_CLASS (klass),
1340 g_cclosure_marshal_VOID__VOID,
1345 fstab_file_changed (GFileMonitor *monitor,
1348 GFileMonitorEvent event_type,
1351 GUnixMountMonitor *mount_monitor;
1353 if (event_type != G_FILE_MONITOR_EVENT_CHANGED &&
1354 event_type != G_FILE_MONITOR_EVENT_CREATED &&
1355 event_type != G_FILE_MONITOR_EVENT_DELETED)
1358 mount_monitor = user_data;
1359 g_signal_emit (mount_monitor, signals[MOUNTPOINTS_CHANGED], 0);
1363 mtab_file_changed (GFileMonitor *monitor,
1366 GFileMonitorEvent event_type,
1369 GUnixMountMonitor *mount_monitor;
1371 if (event_type != G_FILE_MONITOR_EVENT_CHANGED &&
1372 event_type != G_FILE_MONITOR_EVENT_CREATED &&
1373 event_type != G_FILE_MONITOR_EVENT_DELETED)
1376 mount_monitor = user_data;
1377 g_signal_emit (mount_monitor, signals[MOUNTS_CHANGED], 0);
1381 proc_mounts_changed (GIOChannel *channel,
1385 GUnixMountMonitor *mount_monitor = G_UNIX_MOUNT_MONITOR (user_data);
1386 if (cond & G_IO_ERR)
1387 g_signal_emit (mount_monitor, signals[MOUNTS_CHANGED], 0);
1392 mount_change_poller (gpointer user_data)
1394 GUnixMountMonitor *mount_monitor;
1395 GList *current_mounts;
1396 gboolean has_changed = FALSE;
1398 mount_monitor = user_data;
1399 current_mounts = _g_get_unix_mounts ();
1401 if (g_list_length (current_mounts) != g_list_length (mount_monitor->mount_poller_mounts))
1403 g_list_foreach (mount_monitor->mount_poller_mounts, (GFunc)g_unix_mount_free, NULL);
1410 for (i = 0; i < g_list_length (current_mounts); i++)
1412 GUnixMountEntry *m1;
1413 GUnixMountEntry *m2;
1415 m1 = (GUnixMountEntry *)g_list_nth_data (current_mounts, i);
1416 m2 = (GUnixMountEntry *)g_list_nth_data (mount_monitor->mount_poller_mounts, i);
1417 if (! has_changed && g_unix_mount_compare (m1, m2) != 0)
1420 g_unix_mount_free (m2);
1424 g_list_free (mount_monitor->mount_poller_mounts);
1425 mount_monitor->mount_poller_mounts = current_mounts;
1429 mount_poller_time = (guint64)time (NULL);
1430 g_signal_emit (mount_monitor, signals[MOUNTS_CHANGED], 0);
1437 g_unix_mount_monitor_init (GUnixMountMonitor *monitor)
1441 if (get_fstab_file () != NULL)
1443 file = g_file_new_for_path (get_fstab_file ());
1444 monitor->fstab_monitor = g_file_monitor_file (file, 0, NULL, NULL);
1445 g_object_unref (file);
1447 g_signal_connect (monitor->fstab_monitor, "changed", (GCallback)fstab_file_changed, monitor);
1450 if (get_mtab_monitor_file () != NULL)
1452 const gchar *mtab_path;
1454 mtab_path = get_mtab_monitor_file ();
1455 /* /proc/mounts monitoring is special - can't just use GFileMonitor.
1456 * See 'man proc' for more details.
1458 if (g_strcmp0 (mtab_path, "/proc/mounts") == 0)
1460 GIOChannel *proc_mounts_channel;
1461 GError *error = NULL;
1462 proc_mounts_channel = g_io_channel_new_file ("/proc/mounts", "r", &error);
1463 if (proc_mounts_channel == NULL)
1465 g_warning ("Error creating IO channel for /proc/mounts: %s (%s, %d)",
1466 error->message, g_quark_to_string (error->domain), error->code);
1467 g_error_free (error);
1471 monitor->proc_mounts_watch_source = g_io_create_watch (proc_mounts_channel, G_IO_ERR);
1472 g_source_set_callback (monitor->proc_mounts_watch_source,
1473 (GSourceFunc) proc_mounts_changed,
1476 g_source_attach (monitor->proc_mounts_watch_source,
1477 g_main_context_get_thread_default ());
1478 g_source_unref (monitor->proc_mounts_watch_source);
1479 g_io_channel_unref (proc_mounts_channel);
1484 file = g_file_new_for_path (mtab_path);
1485 monitor->mtab_monitor = g_file_monitor_file (file, 0, NULL, NULL);
1486 g_object_unref (file);
1487 g_signal_connect (monitor->mtab_monitor, "changed", (GCallback)mtab_file_changed, monitor);
1492 monitor->proc_mounts_watch_source = g_timeout_source_new_seconds (3);
1493 monitor->mount_poller_mounts = _g_get_unix_mounts ();
1494 mount_poller_time = (guint64)time (NULL);
1495 g_source_set_callback (monitor->proc_mounts_watch_source,
1496 (GSourceFunc)mount_change_poller,
1498 g_source_attach (monitor->proc_mounts_watch_source,
1499 g_main_context_get_thread_default ());
1500 g_source_unref (monitor->proc_mounts_watch_source);
1505 * g_unix_mount_monitor_set_rate_limit:
1506 * @mount_monitor: a #GUnixMountMonitor
1507 * @limit_msec: a integer with the limit in milliseconds to
1510 * Sets the rate limit to which the @mount_monitor will report
1511 * consecutive change events to the mount and mount point entry files.
1516 g_unix_mount_monitor_set_rate_limit (GUnixMountMonitor *mount_monitor,
1519 g_return_if_fail (G_IS_UNIX_MOUNT_MONITOR (mount_monitor));
1521 if (mount_monitor->fstab_monitor != NULL)
1522 g_file_monitor_set_rate_limit (mount_monitor->fstab_monitor, limit_msec);
1524 if (mount_monitor->mtab_monitor != NULL)
1525 g_file_monitor_set_rate_limit (mount_monitor->mtab_monitor, limit_msec);
1529 * g_unix_mount_monitor_new:
1531 * Gets a new #GUnixMountMonitor. The default rate limit for which the
1532 * monitor will report consecutive changes for the mount and mount
1533 * point entry files is the default for a #GFileMonitor. Use
1534 * g_unix_mount_monitor_set_rate_limit() to change this.
1536 * Returns: a #GUnixMountMonitor.
1539 g_unix_mount_monitor_new (void)
1541 if (the_mount_monitor == NULL)
1543 the_mount_monitor = g_object_new (G_TYPE_UNIX_MOUNT_MONITOR, NULL);
1544 return the_mount_monitor;
1547 return g_object_ref (the_mount_monitor);
1551 * g_unix_mount_free:
1552 * @mount_entry: a #GUnixMountEntry.
1554 * Frees a unix mount.
1557 g_unix_mount_free (GUnixMountEntry *mount_entry)
1559 g_return_if_fail (mount_entry != NULL);
1561 g_free (mount_entry->mount_path);
1562 g_free (mount_entry->device_path);
1563 g_free (mount_entry->filesystem_type);
1564 g_free (mount_entry);
1568 * g_unix_mount_point_free:
1569 * @mount_point: unix mount point to free.
1571 * Frees a unix mount point.
1574 g_unix_mount_point_free (GUnixMountPoint *mount_point)
1576 g_return_if_fail (mount_point != NULL);
1578 g_free (mount_point->mount_path);
1579 g_free (mount_point->device_path);
1580 g_free (mount_point->filesystem_type);
1581 g_free (mount_point->options);
1582 g_free (mount_point);
1586 * g_unix_mount_compare:
1587 * @mount1: first #GUnixMountEntry to compare.
1588 * @mount2: second #GUnixMountEntry to compare.
1590 * Compares two unix mounts.
1592 * Returns: 1, 0 or -1 if @mount1 is greater than, equal to,
1593 * or less than @mount2, respectively.
1596 g_unix_mount_compare (GUnixMountEntry *mount1,
1597 GUnixMountEntry *mount2)
1601 g_return_val_if_fail (mount1 != NULL && mount2 != NULL, 0);
1603 res = g_strcmp0 (mount1->mount_path, mount2->mount_path);
1607 res = g_strcmp0 (mount1->device_path, mount2->device_path);
1611 res = g_strcmp0 (mount1->filesystem_type, mount2->filesystem_type);
1615 res = mount1->is_read_only - mount2->is_read_only;
1623 * g_unix_mount_get_mount_path:
1624 * @mount_entry: input #GUnixMountEntry to get the mount path for.
1626 * Gets the mount path for a unix mount.
1628 * Returns: the mount path for @mount_entry.
1631 g_unix_mount_get_mount_path (GUnixMountEntry *mount_entry)
1633 g_return_val_if_fail (mount_entry != NULL, NULL);
1635 return mount_entry->mount_path;
1639 * g_unix_mount_get_device_path:
1640 * @mount_entry: a #GUnixMount.
1642 * Gets the device path for a unix mount.
1644 * Returns: a string containing the device path.
1647 g_unix_mount_get_device_path (GUnixMountEntry *mount_entry)
1649 g_return_val_if_fail (mount_entry != NULL, NULL);
1651 return mount_entry->device_path;
1655 * g_unix_mount_get_fs_type:
1656 * @mount_entry: a #GUnixMount.
1658 * Gets the filesystem type for the unix mount.
1660 * Returns: a string containing the file system type.
1663 g_unix_mount_get_fs_type (GUnixMountEntry *mount_entry)
1665 g_return_val_if_fail (mount_entry != NULL, NULL);
1667 return mount_entry->filesystem_type;
1671 * g_unix_mount_is_readonly:
1672 * @mount_entry: a #GUnixMount.
1674 * Checks if a unix mount is mounted read only.
1676 * Returns: %TRUE if @mount_entry is read only.
1679 g_unix_mount_is_readonly (GUnixMountEntry *mount_entry)
1681 g_return_val_if_fail (mount_entry != NULL, FALSE);
1683 return mount_entry->is_read_only;
1687 * g_unix_mount_is_system_internal:
1688 * @mount_entry: a #GUnixMount.
1690 * Checks if a unix mount is a system path.
1692 * Returns: %TRUE if the unix mount is for a system path.
1695 g_unix_mount_is_system_internal (GUnixMountEntry *mount_entry)
1697 g_return_val_if_fail (mount_entry != NULL, FALSE);
1699 return mount_entry->is_system_internal;
1703 * g_unix_mount_point_compare:
1704 * @mount1: a #GUnixMount.
1705 * @mount2: a #GUnixMount.
1707 * Compares two unix mount points.
1709 * Returns: 1, 0 or -1 if @mount1 is greater than, equal to,
1710 * or less than @mount2, respectively.
1713 g_unix_mount_point_compare (GUnixMountPoint *mount1,
1714 GUnixMountPoint *mount2)
1718 g_return_val_if_fail (mount1 != NULL && mount2 != NULL, 0);
1720 res = g_strcmp0 (mount1->mount_path, mount2->mount_path);
1724 res = g_strcmp0 (mount1->device_path, mount2->device_path);
1728 res = g_strcmp0 (mount1->filesystem_type, mount2->filesystem_type);
1732 res = g_strcmp0 (mount1->options, mount2->options);
1736 res = mount1->is_read_only - mount2->is_read_only;
1740 res = mount1->is_user_mountable - mount2->is_user_mountable;
1744 res = mount1->is_loopback - mount2->is_loopback;
1752 * g_unix_mount_point_get_mount_path:
1753 * @mount_point: a #GUnixMountPoint.
1755 * Gets the mount path for a unix mount point.
1757 * Returns: a string containing the mount path.
1760 g_unix_mount_point_get_mount_path (GUnixMountPoint *mount_point)
1762 g_return_val_if_fail (mount_point != NULL, NULL);
1764 return mount_point->mount_path;
1768 * g_unix_mount_point_get_device_path:
1769 * @mount_point: a #GUnixMountPoint.
1771 * Gets the device path for a unix mount point.
1773 * Returns: a string containing the device path.
1776 g_unix_mount_point_get_device_path (GUnixMountPoint *mount_point)
1778 g_return_val_if_fail (mount_point != NULL, NULL);
1780 return mount_point->device_path;
1784 * g_unix_mount_point_get_fs_type:
1785 * @mount_point: a #GUnixMountPoint.
1787 * Gets the file system type for the mount point.
1789 * Returns: a string containing the file system type.
1792 g_unix_mount_point_get_fs_type (GUnixMountPoint *mount_point)
1794 g_return_val_if_fail (mount_point != NULL, NULL);
1796 return mount_point->filesystem_type;
1800 * g_unix_mount_point_get_options:
1801 * @mount_point: a #GUnixMountPoint.
1803 * Gets the options for the mount point.
1805 * Returns: a string containing the options.
1810 g_unix_mount_point_get_options (GUnixMountPoint *mount_point)
1812 g_return_val_if_fail (mount_point != NULL, NULL);
1814 return mount_point->options;
1818 * g_unix_mount_point_is_readonly:
1819 * @mount_point: a #GUnixMountPoint.
1821 * Checks if a unix mount point is read only.
1823 * Returns: %TRUE if a mount point is read only.
1826 g_unix_mount_point_is_readonly (GUnixMountPoint *mount_point)
1828 g_return_val_if_fail (mount_point != NULL, FALSE);
1830 return mount_point->is_read_only;
1834 * g_unix_mount_point_is_user_mountable:
1835 * @mount_point: a #GUnixMountPoint.
1837 * Checks if a unix mount point is mountable by the user.
1839 * Returns: %TRUE if the mount point is user mountable.
1842 g_unix_mount_point_is_user_mountable (GUnixMountPoint *mount_point)
1844 g_return_val_if_fail (mount_point != NULL, FALSE);
1846 return mount_point->is_user_mountable;
1850 * g_unix_mount_point_is_loopback:
1851 * @mount_point: a #GUnixMountPoint.
1853 * Checks if a unix mount point is a loopback device.
1855 * Returns: %TRUE if the mount point is a loopback. %FALSE otherwise.
1858 g_unix_mount_point_is_loopback (GUnixMountPoint *mount_point)
1860 g_return_val_if_fail (mount_point != NULL, FALSE);
1862 return mount_point->is_loopback;
1865 static GUnixMountType
1866 guess_mount_type (const char *mount_path,
1867 const char *device_path,
1868 const char *filesystem_type)
1870 GUnixMountType type;
1873 type = G_UNIX_MOUNT_TYPE_UNKNOWN;
1875 if ((strcmp (filesystem_type, "udf") == 0) ||
1876 (strcmp (filesystem_type, "iso9660") == 0) ||
1877 (strcmp (filesystem_type, "cd9660") == 0))
1878 type = G_UNIX_MOUNT_TYPE_CDROM;
1879 else if ((strcmp (filesystem_type, "nfs") == 0) ||
1880 (strcmp (filesystem_type, "nfs4") == 0))
1881 type = G_UNIX_MOUNT_TYPE_NFS;
1882 else if (g_str_has_prefix (device_path, "/vol/dev/diskette/") ||
1883 g_str_has_prefix (device_path, "/dev/fd") ||
1884 g_str_has_prefix (device_path, "/dev/floppy"))
1885 type = G_UNIX_MOUNT_TYPE_FLOPPY;
1886 else if (g_str_has_prefix (device_path, "/dev/cdrom") ||
1887 g_str_has_prefix (device_path, "/dev/acd") ||
1888 g_str_has_prefix (device_path, "/dev/cd"))
1889 type = G_UNIX_MOUNT_TYPE_CDROM;
1890 else if (g_str_has_prefix (device_path, "/vol/"))
1892 const char *name = mount_path + strlen ("/");
1894 if (g_str_has_prefix (name, "cdrom"))
1895 type = G_UNIX_MOUNT_TYPE_CDROM;
1896 else if (g_str_has_prefix (name, "floppy") ||
1897 g_str_has_prefix (device_path, "/vol/dev/diskette/"))
1898 type = G_UNIX_MOUNT_TYPE_FLOPPY;
1899 else if (g_str_has_prefix (name, "rmdisk"))
1900 type = G_UNIX_MOUNT_TYPE_ZIP;
1901 else if (g_str_has_prefix (name, "jaz"))
1902 type = G_UNIX_MOUNT_TYPE_JAZ;
1903 else if (g_str_has_prefix (name, "memstick"))
1904 type = G_UNIX_MOUNT_TYPE_MEMSTICK;
1908 basename = g_path_get_basename (mount_path);
1910 if (g_str_has_prefix (basename, "cdr") ||
1911 g_str_has_prefix (basename, "cdwriter") ||
1912 g_str_has_prefix (basename, "burn") ||
1913 g_str_has_prefix (basename, "dvdr"))
1914 type = G_UNIX_MOUNT_TYPE_CDROM;
1915 else if (g_str_has_prefix (basename, "floppy"))
1916 type = G_UNIX_MOUNT_TYPE_FLOPPY;
1917 else if (g_str_has_prefix (basename, "zip"))
1918 type = G_UNIX_MOUNT_TYPE_ZIP;
1919 else if (g_str_has_prefix (basename, "jaz"))
1920 type = G_UNIX_MOUNT_TYPE_JAZ;
1921 else if (g_str_has_prefix (basename, "camera"))
1922 type = G_UNIX_MOUNT_TYPE_CAMERA;
1923 else if (g_str_has_prefix (basename, "memstick") ||
1924 g_str_has_prefix (basename, "memory_stick") ||
1925 g_str_has_prefix (basename, "ram"))
1926 type = G_UNIX_MOUNT_TYPE_MEMSTICK;
1927 else if (g_str_has_prefix (basename, "compact_flash"))
1928 type = G_UNIX_MOUNT_TYPE_CF;
1929 else if (g_str_has_prefix (basename, "smart_media"))
1930 type = G_UNIX_MOUNT_TYPE_SM;
1931 else if (g_str_has_prefix (basename, "sd_mmc"))
1932 type = G_UNIX_MOUNT_TYPE_SDMMC;
1933 else if (g_str_has_prefix (basename, "ipod"))
1934 type = G_UNIX_MOUNT_TYPE_IPOD;
1939 if (type == G_UNIX_MOUNT_TYPE_UNKNOWN)
1940 type = G_UNIX_MOUNT_TYPE_HD;
1946 * g_unix_mount_guess_type:
1947 * @mount_entry: a #GUnixMount.
1949 * Guesses the type of a unix mount. If the mount type cannot be
1950 * determined, returns %G_UNIX_MOUNT_TYPE_UNKNOWN.
1952 * Returns: a #GUnixMountType.
1954 static GUnixMountType
1955 g_unix_mount_guess_type (GUnixMountEntry *mount_entry)
1957 g_return_val_if_fail (mount_entry != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
1958 g_return_val_if_fail (mount_entry->mount_path != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
1959 g_return_val_if_fail (mount_entry->device_path != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
1960 g_return_val_if_fail (mount_entry->filesystem_type != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
1962 return guess_mount_type (mount_entry->mount_path,
1963 mount_entry->device_path,
1964 mount_entry->filesystem_type);
1968 * g_unix_mount_point_guess_type:
1969 * @mount_point: a #GUnixMountPoint.
1971 * Guesses the type of a unix mount point.
1972 * If the mount type cannot be determined,
1973 * returns %G_UNIX_MOUNT_TYPE_UNKNOWN.
1975 * Returns: a #GUnixMountType.
1977 static GUnixMountType
1978 g_unix_mount_point_guess_type (GUnixMountPoint *mount_point)
1980 g_return_val_if_fail (mount_point != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
1981 g_return_val_if_fail (mount_point->mount_path != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
1982 g_return_val_if_fail (mount_point->device_path != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
1983 g_return_val_if_fail (mount_point->filesystem_type != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
1985 return guess_mount_type (mount_point->mount_path,
1986 mount_point->device_path,
1987 mount_point->filesystem_type);
1991 type_to_icon (GUnixMountType type, gboolean is_mount_point, gboolean use_symbolic)
1993 const char *icon_name;
1997 case G_UNIX_MOUNT_TYPE_HD:
1999 icon_name = use_symbolic ? "drive-removable-media-symbolic" : "drive-removable-media";
2001 icon_name = use_symbolic ? "drive-harddisk-symbolic" : "drive-harddisk";
2003 case G_UNIX_MOUNT_TYPE_FLOPPY:
2004 case G_UNIX_MOUNT_TYPE_ZIP:
2005 case G_UNIX_MOUNT_TYPE_JAZ:
2007 icon_name = use_symbolic ? "drive-removable-media-symbolic" : "drive-removable-media";
2009 icon_name = use_symbolic ? "media-removable-symbolic" : "media-floppy";
2011 case G_UNIX_MOUNT_TYPE_CDROM:
2013 icon_name = use_symbolic ? "drive-optical-symbolic" : "drive-optical";
2015 icon_name = use_symbolic ? "media-optical-symbolic" : "media-optical";
2017 case G_UNIX_MOUNT_TYPE_NFS:
2018 icon_name = use_symbolic ? "folder-remote-symbolic" : "folder-remote";
2020 case G_UNIX_MOUNT_TYPE_MEMSTICK:
2022 icon_name = use_symbolic ? "drive-removable-media-symbolic" : "drive-removable-media";
2024 icon_name = use_symbolic ? "media-removable-symbolic" : "media-flash";
2026 case G_UNIX_MOUNT_TYPE_CAMERA:
2028 icon_name = use_symbolic ? "drive-removable-media-symbolic" : "drive-removable-media";
2030 icon_name = use_symbolic ? "camera-photo-symbolic" : "camera-photo";
2032 case G_UNIX_MOUNT_TYPE_IPOD:
2034 icon_name = use_symbolic ? "drive-removable-media-symbolic" : "drive-removable-media";
2036 icon_name = use_symbolic ? "multimedia-player-symbolic" : "multimedia-player";
2038 case G_UNIX_MOUNT_TYPE_UNKNOWN:
2041 icon_name = use_symbolic ? "drive-removable-media-symbolic" : "drive-removable-media";
2043 icon_name = use_symbolic ? "drive-harddisk-symbolic" : "drive-harddisk";
2051 * g_unix_mount_guess_name:
2052 * @mount_entry: a #GUnixMountEntry
2054 * Guesses the name of a Unix mount.
2055 * The result is a translated string.
2057 * Returns: A newly allocated string that must
2058 * be freed with g_free()
2061 g_unix_mount_guess_name (GUnixMountEntry *mount_entry)
2065 if (strcmp (mount_entry->mount_path, "/") == 0)
2066 name = g_strdup (_("Filesystem root"));
2068 name = g_filename_display_basename (mount_entry->mount_path);
2074 * g_unix_mount_guess_icon:
2075 * @mount_entry: a #GUnixMountEntry
2077 * Guesses the icon of a Unix mount.
2079 * Returns: (transfer full): a #GIcon
2082 g_unix_mount_guess_icon (GUnixMountEntry *mount_entry)
2084 return g_themed_icon_new_with_default_fallbacks (type_to_icon (g_unix_mount_guess_type (mount_entry), FALSE, FALSE));
2088 * g_unix_mount_guess_symbolic_icon:
2089 * @mount_entry: a #GUnixMountEntry
2091 * Guesses the symbolic icon of a Unix mount.
2093 * Returns: (transfer full): a #GIcon
2098 g_unix_mount_guess_symbolic_icon (GUnixMountEntry *mount_entry)
2100 return g_themed_icon_new_with_default_fallbacks (type_to_icon (g_unix_mount_guess_type (mount_entry), FALSE, TRUE));
2104 * g_unix_mount_point_guess_name:
2105 * @mount_point: a #GUnixMountPoint
2107 * Guesses the name of a Unix mount point.
2108 * The result is a translated string.
2110 * Returns: A newly allocated string that must
2111 * be freed with g_free()
2114 g_unix_mount_point_guess_name (GUnixMountPoint *mount_point)
2118 if (strcmp (mount_point->mount_path, "/") == 0)
2119 name = g_strdup (_("Filesystem root"));
2121 name = g_filename_display_basename (mount_point->mount_path);
2127 * g_unix_mount_point_guess_icon:
2128 * @mount_point: a #GUnixMountPoint
2130 * Guesses the icon of a Unix mount point.
2132 * Returns: (transfer full): a #GIcon
2135 g_unix_mount_point_guess_icon (GUnixMountPoint *mount_point)
2137 return g_themed_icon_new_with_default_fallbacks (type_to_icon (g_unix_mount_point_guess_type (mount_point), TRUE, FALSE));
2141 * g_unix_mount_point_guess_symbolic_icon:
2142 * @mount_point: a #GUnixMountPoint
2144 * Guesses the symbolic icon of a Unix mount point.
2146 * Returns: (transfer full): a #GIcon
2151 g_unix_mount_point_guess_symbolic_icon (GUnixMountPoint *mount_point)
2153 return g_themed_icon_new_with_default_fallbacks (type_to_icon (g_unix_mount_point_guess_type (mount_point), TRUE, TRUE));
2157 * g_unix_mount_guess_can_eject:
2158 * @mount_entry: a #GUnixMountEntry
2160 * Guesses whether a Unix mount can be ejected.
2162 * Returns: %TRUE if @mount_entry is deemed to be ejectable.
2165 g_unix_mount_guess_can_eject (GUnixMountEntry *mount_entry)
2167 GUnixMountType guessed_type;
2169 guessed_type = g_unix_mount_guess_type (mount_entry);
2170 if (guessed_type == G_UNIX_MOUNT_TYPE_IPOD ||
2171 guessed_type == G_UNIX_MOUNT_TYPE_CDROM)
2178 * g_unix_mount_guess_should_display:
2179 * @mount_entry: a #GUnixMountEntry
2181 * Guesses whether a Unix mount should be displayed in the UI.
2183 * Returns: %TRUE if @mount_entry is deemed to be displayable.
2186 g_unix_mount_guess_should_display (GUnixMountEntry *mount_entry)
2188 const char *mount_path;
2189 const gchar *user_name;
2190 gsize user_name_len;
2192 /* Never display internal mountpoints */
2193 if (g_unix_mount_is_system_internal (mount_entry))
2196 /* Only display things in /media (which are generally user mountable)
2197 and home dir (fuse stuff) and /run/media/$USER */
2198 mount_path = mount_entry->mount_path;
2199 if (mount_path != NULL)
2201 gboolean is_in_runtime_dir = FALSE;
2202 /* Hide mounts within a dot path, suppose it was a purpose to hide this mount */
2203 if (g_strstr_len (mount_path, -1, "/.") != NULL)
2206 /* Check /run/media/$USER/ */
2207 user_name = g_get_user_name ();
2208 user_name_len = strlen (user_name);
2209 if (strncmp (mount_path, "/run/media/", sizeof ("/run/media/") - 1) == 0 &&
2210 strncmp (mount_path + sizeof ("/run/media/") - 1, user_name, user_name_len) == 0 &&
2211 mount_path[sizeof ("/run/media/") - 1 + user_name_len] == '/')
2212 is_in_runtime_dir = TRUE;
2214 if (is_in_runtime_dir || g_str_has_prefix (mount_path, "/media/"))
2217 /* Avoid displaying mounts that are not accessible to the user.
2219 * See http://bugzilla.gnome.org/show_bug.cgi?id=526320 for why we
2220 * want to avoid g_access() for mount points which can potentially
2221 * block or fail stat()'ing, such as network mounts.
2223 path = g_path_get_dirname (mount_path);
2224 if (g_str_has_prefix (path, "/media/"))
2226 if (g_access (path, R_OK|X_OK) != 0)
2234 if (mount_entry->device_path && mount_entry->device_path[0] == '/')
2237 if (g_stat (mount_entry->device_path, &st) == 0 &&
2238 S_ISBLK(st.st_mode) &&
2239 g_access (mount_path, R_OK|X_OK) != 0)
2245 if (g_str_has_prefix (mount_path, g_get_home_dir ()) &&
2246 mount_path[strlen (g_get_home_dir())] == G_DIR_SEPARATOR)
2254 * g_unix_mount_point_guess_can_eject:
2255 * @mount_point: a #GUnixMountPoint
2257 * Guesses whether a Unix mount point can be ejected.
2259 * Returns: %TRUE if @mount_point is deemed to be ejectable.
2262 g_unix_mount_point_guess_can_eject (GUnixMountPoint *mount_point)
2264 GUnixMountType guessed_type;
2266 guessed_type = g_unix_mount_point_guess_type (mount_point);
2267 if (guessed_type == G_UNIX_MOUNT_TYPE_IPOD ||
2268 guessed_type == G_UNIX_MOUNT_TYPE_CDROM)
2274 #ifdef HAVE_MNTENT_H
2275 /* borrowed from gtk/gtkfilesystemunix.c in GTK+ on 02/23/2006 */
2277 _canonicalize_filename (gchar *filename)
2280 gboolean last_was_slash = FALSE;
2287 if (*p == G_DIR_SEPARATOR)
2289 if (!last_was_slash)
2290 *q++ = G_DIR_SEPARATOR;
2292 last_was_slash = TRUE;
2296 if (last_was_slash && *p == '.')
2298 if (*(p + 1) == G_DIR_SEPARATOR ||
2301 if (*(p + 1) == '\0')
2306 else if (*(p + 1) == '.' &&
2307 (*(p + 2) == G_DIR_SEPARATOR ||
2310 if (q > filename + 1)
2313 while (q > filename + 1 &&
2314 *(q - 1) != G_DIR_SEPARATOR)
2318 if (*(p + 2) == '\0')
2326 last_was_slash = FALSE;
2332 last_was_slash = FALSE;
2339 if (q > filename + 1 && *(q - 1) == G_DIR_SEPARATOR)
2346 _resolve_symlink (const char *file)
2354 f = g_strdup (file);
2356 while (g_file_test (f, G_FILE_TEST_IS_SYMLINK))
2358 link = g_file_read_link (f, &error);
2361 g_error_free (error);
2367 dir = g_path_get_dirname (f);
2368 f1 = g_strdup_printf ("%s/%s", dir, link);
2377 _canonicalize_filename (f);
2382 _resolve_dev_root (void)
2384 static gboolean have_real_dev_root = FALSE;
2385 static char real_dev_root[256];
2386 struct stat statbuf;
2388 /* see if it's cached already */
2389 if (have_real_dev_root)
2392 /* otherwise we're going to find it right away.. */
2393 have_real_dev_root = TRUE;
2395 if (stat ("/dev/root", &statbuf) == 0)
2397 if (! S_ISLNK (statbuf.st_mode))
2399 dev_t root_dev = statbuf.st_dev;
2402 /* see if device with similar major:minor as /dev/root is mention
2403 * in /etc/mtab (it usually is)
2405 f = fopen ("/etc/mtab", "r");
2408 struct mntent *entp;
2409 #ifdef HAVE_GETMNTENT_R
2412 while ((entp = getmntent_r (f, &ent, buf, sizeof (buf))) != NULL)
2416 while ((entp = getmntent (f)) != NULL)
2419 if (stat (entp->mnt_fsname, &statbuf) == 0 &&
2420 statbuf.st_dev == root_dev)
2422 strncpy (real_dev_root, entp->mnt_fsname, sizeof (real_dev_root) - 1);
2423 real_dev_root[sizeof (real_dev_root) - 1] = '\0';
2431 #ifndef HAVE_GETMNTENT_R
2432 G_UNLOCK (getmntent);
2436 /* no, that didn't work.. next we could scan /dev ... but I digress.. */
2442 resolved = _resolve_symlink ("/dev/root");
2443 if (resolved != NULL)
2445 strncpy (real_dev_root, resolved, sizeof (real_dev_root) - 1);
2446 real_dev_root[sizeof (real_dev_root) - 1] = '\0';
2454 strcpy (real_dev_root, "/dev/root");
2457 return real_dev_root;