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 g_list_free_full (monitor->mount_poller_mounts, (GDestroyNotify)g_unix_mount_free);
1296 the_mount_monitor = NULL;
1298 G_OBJECT_CLASS (g_unix_mount_monitor_parent_class)->finalize (object);
1303 g_unix_mount_monitor_class_init (GUnixMountMonitorClass *klass)
1305 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
1307 gobject_class->finalize = g_unix_mount_monitor_finalize;
1310 * GUnixMountMonitor::mounts-changed:
1311 * @monitor: the object on which the signal is emitted
1313 * Emitted when the unix mounts have changed.
1315 signals[MOUNTS_CHANGED] =
1316 g_signal_new ("mounts-changed",
1317 G_TYPE_FROM_CLASS (klass),
1321 g_cclosure_marshal_VOID__VOID,
1325 * GUnixMountMonitor::mountpoints-changed:
1326 * @monitor: the object on which the signal is emitted
1328 * Emitted when the unix mount points have changed.
1330 signals[MOUNTPOINTS_CHANGED] =
1331 g_signal_new ("mountpoints-changed",
1332 G_TYPE_FROM_CLASS (klass),
1336 g_cclosure_marshal_VOID__VOID,
1341 fstab_file_changed (GFileMonitor *monitor,
1344 GFileMonitorEvent event_type,
1347 GUnixMountMonitor *mount_monitor;
1349 if (event_type != G_FILE_MONITOR_EVENT_CHANGED &&
1350 event_type != G_FILE_MONITOR_EVENT_CREATED &&
1351 event_type != G_FILE_MONITOR_EVENT_DELETED)
1354 mount_monitor = user_data;
1355 g_signal_emit (mount_monitor, signals[MOUNTPOINTS_CHANGED], 0);
1359 mtab_file_changed (GFileMonitor *monitor,
1362 GFileMonitorEvent event_type,
1365 GUnixMountMonitor *mount_monitor;
1367 if (event_type != G_FILE_MONITOR_EVENT_CHANGED &&
1368 event_type != G_FILE_MONITOR_EVENT_CREATED &&
1369 event_type != G_FILE_MONITOR_EVENT_DELETED)
1372 mount_monitor = user_data;
1373 g_signal_emit (mount_monitor, signals[MOUNTS_CHANGED], 0);
1377 proc_mounts_changed (GIOChannel *channel,
1381 GUnixMountMonitor *mount_monitor = G_UNIX_MOUNT_MONITOR (user_data);
1382 if (cond & G_IO_ERR)
1383 g_signal_emit (mount_monitor, signals[MOUNTS_CHANGED], 0);
1388 mount_change_poller (gpointer user_data)
1390 GUnixMountMonitor *mount_monitor;
1391 GList *current_mounts, *new_it, *old_it;
1392 gboolean has_changed = FALSE;
1394 mount_monitor = user_data;
1395 current_mounts = _g_get_unix_mounts ();
1397 for ( new_it = current_mounts, old_it = mount_monitor->mount_poller_mounts;
1398 new_it != NULL && old_it != NULL;
1399 new_it = g_list_next (new_it), old_it = g_list_next (old_it) )
1401 if (g_unix_mount_compare (new_it->data, old_it->data) != 0)
1407 if (!(new_it == NULL && old_it == NULL))
1410 g_list_free_full (mount_monitor->mount_poller_mounts,
1411 (GDestroyNotify)g_unix_mount_free);
1413 mount_monitor->mount_poller_mounts = current_mounts;
1417 mount_poller_time = (guint64)g_get_monotonic_time ();
1418 g_signal_emit (mount_monitor, signals[MOUNTS_CHANGED], 0);
1425 g_unix_mount_monitor_init (GUnixMountMonitor *monitor)
1429 if (get_fstab_file () != NULL)
1431 file = g_file_new_for_path (get_fstab_file ());
1432 monitor->fstab_monitor = g_file_monitor_file (file, 0, NULL, NULL);
1433 g_object_unref (file);
1435 g_signal_connect (monitor->fstab_monitor, "changed", (GCallback)fstab_file_changed, monitor);
1438 if (get_mtab_monitor_file () != NULL)
1440 const gchar *mtab_path;
1442 mtab_path = get_mtab_monitor_file ();
1443 /* /proc/mounts monitoring is special - can't just use GFileMonitor.
1444 * See 'man proc' for more details.
1446 if (g_strcmp0 (mtab_path, "/proc/mounts") == 0)
1448 GIOChannel *proc_mounts_channel;
1449 GError *error = NULL;
1450 proc_mounts_channel = g_io_channel_new_file ("/proc/mounts", "r", &error);
1451 if (proc_mounts_channel == NULL)
1453 g_warning ("Error creating IO channel for /proc/mounts: %s (%s, %d)",
1454 error->message, g_quark_to_string (error->domain), error->code);
1455 g_error_free (error);
1459 monitor->proc_mounts_watch_source = g_io_create_watch (proc_mounts_channel, G_IO_ERR);
1460 g_source_set_callback (monitor->proc_mounts_watch_source,
1461 (GSourceFunc) proc_mounts_changed,
1464 g_source_attach (monitor->proc_mounts_watch_source,
1465 g_main_context_get_thread_default ());
1466 g_source_unref (monitor->proc_mounts_watch_source);
1467 g_io_channel_unref (proc_mounts_channel);
1472 file = g_file_new_for_path (mtab_path);
1473 monitor->mtab_monitor = g_file_monitor_file (file, 0, NULL, NULL);
1474 g_object_unref (file);
1475 g_signal_connect (monitor->mtab_monitor, "changed", (GCallback)mtab_file_changed, monitor);
1480 monitor->proc_mounts_watch_source = g_timeout_source_new_seconds (3);
1481 monitor->mount_poller_mounts = _g_get_unix_mounts ();
1482 mount_poller_time = (guint64)g_get_monotonic_time ();
1483 g_source_set_callback (monitor->proc_mounts_watch_source,
1484 (GSourceFunc)mount_change_poller,
1486 g_source_attach (monitor->proc_mounts_watch_source,
1487 g_main_context_get_thread_default ());
1488 g_source_unref (monitor->proc_mounts_watch_source);
1493 * g_unix_mount_monitor_set_rate_limit:
1494 * @mount_monitor: a #GUnixMountMonitor
1495 * @limit_msec: a integer with the limit in milliseconds to
1498 * Sets the rate limit to which the @mount_monitor will report
1499 * consecutive change events to the mount and mount point entry files.
1504 g_unix_mount_monitor_set_rate_limit (GUnixMountMonitor *mount_monitor,
1507 g_return_if_fail (G_IS_UNIX_MOUNT_MONITOR (mount_monitor));
1509 if (mount_monitor->fstab_monitor != NULL)
1510 g_file_monitor_set_rate_limit (mount_monitor->fstab_monitor, limit_msec);
1512 if (mount_monitor->mtab_monitor != NULL)
1513 g_file_monitor_set_rate_limit (mount_monitor->mtab_monitor, limit_msec);
1517 * g_unix_mount_monitor_new:
1519 * Gets a new #GUnixMountMonitor. The default rate limit for which the
1520 * monitor will report consecutive changes for the mount and mount
1521 * point entry files is the default for a #GFileMonitor. Use
1522 * g_unix_mount_monitor_set_rate_limit() to change this.
1524 * Returns: a #GUnixMountMonitor.
1527 g_unix_mount_monitor_new (void)
1529 if (the_mount_monitor == NULL)
1531 the_mount_monitor = g_object_new (G_TYPE_UNIX_MOUNT_MONITOR, NULL);
1532 return the_mount_monitor;
1535 return g_object_ref (the_mount_monitor);
1539 * g_unix_mount_free:
1540 * @mount_entry: a #GUnixMountEntry.
1542 * Frees a unix mount.
1545 g_unix_mount_free (GUnixMountEntry *mount_entry)
1547 g_return_if_fail (mount_entry != NULL);
1549 g_free (mount_entry->mount_path);
1550 g_free (mount_entry->device_path);
1551 g_free (mount_entry->filesystem_type);
1552 g_free (mount_entry);
1556 * g_unix_mount_point_free:
1557 * @mount_point: unix mount point to free.
1559 * Frees a unix mount point.
1562 g_unix_mount_point_free (GUnixMountPoint *mount_point)
1564 g_return_if_fail (mount_point != NULL);
1566 g_free (mount_point->mount_path);
1567 g_free (mount_point->device_path);
1568 g_free (mount_point->filesystem_type);
1569 g_free (mount_point->options);
1570 g_free (mount_point);
1574 * g_unix_mount_compare:
1575 * @mount1: first #GUnixMountEntry to compare.
1576 * @mount2: second #GUnixMountEntry to compare.
1578 * Compares two unix mounts.
1580 * Returns: 1, 0 or -1 if @mount1 is greater than, equal to,
1581 * or less than @mount2, respectively.
1584 g_unix_mount_compare (GUnixMountEntry *mount1,
1585 GUnixMountEntry *mount2)
1589 g_return_val_if_fail (mount1 != NULL && mount2 != NULL, 0);
1591 res = g_strcmp0 (mount1->mount_path, mount2->mount_path);
1595 res = g_strcmp0 (mount1->device_path, mount2->device_path);
1599 res = g_strcmp0 (mount1->filesystem_type, mount2->filesystem_type);
1603 res = mount1->is_read_only - mount2->is_read_only;
1611 * g_unix_mount_get_mount_path:
1612 * @mount_entry: input #GUnixMountEntry to get the mount path for.
1614 * Gets the mount path for a unix mount.
1616 * Returns: the mount path for @mount_entry.
1619 g_unix_mount_get_mount_path (GUnixMountEntry *mount_entry)
1621 g_return_val_if_fail (mount_entry != NULL, NULL);
1623 return mount_entry->mount_path;
1627 * g_unix_mount_get_device_path:
1628 * @mount_entry: a #GUnixMount.
1630 * Gets the device path for a unix mount.
1632 * Returns: a string containing the device path.
1635 g_unix_mount_get_device_path (GUnixMountEntry *mount_entry)
1637 g_return_val_if_fail (mount_entry != NULL, NULL);
1639 return mount_entry->device_path;
1643 * g_unix_mount_get_fs_type:
1644 * @mount_entry: a #GUnixMount.
1646 * Gets the filesystem type for the unix mount.
1648 * Returns: a string containing the file system type.
1651 g_unix_mount_get_fs_type (GUnixMountEntry *mount_entry)
1653 g_return_val_if_fail (mount_entry != NULL, NULL);
1655 return mount_entry->filesystem_type;
1659 * g_unix_mount_is_readonly:
1660 * @mount_entry: a #GUnixMount.
1662 * Checks if a unix mount is mounted read only.
1664 * Returns: %TRUE if @mount_entry is read only.
1667 g_unix_mount_is_readonly (GUnixMountEntry *mount_entry)
1669 g_return_val_if_fail (mount_entry != NULL, FALSE);
1671 return mount_entry->is_read_only;
1675 * g_unix_mount_is_system_internal:
1676 * @mount_entry: a #GUnixMount.
1678 * Checks if a unix mount is a system path.
1680 * Returns: %TRUE if the unix mount is for a system path.
1683 g_unix_mount_is_system_internal (GUnixMountEntry *mount_entry)
1685 g_return_val_if_fail (mount_entry != NULL, FALSE);
1687 return mount_entry->is_system_internal;
1691 * g_unix_mount_point_compare:
1692 * @mount1: a #GUnixMount.
1693 * @mount2: a #GUnixMount.
1695 * Compares two unix mount points.
1697 * Returns: 1, 0 or -1 if @mount1 is greater than, equal to,
1698 * or less than @mount2, respectively.
1701 g_unix_mount_point_compare (GUnixMountPoint *mount1,
1702 GUnixMountPoint *mount2)
1706 g_return_val_if_fail (mount1 != NULL && mount2 != NULL, 0);
1708 res = g_strcmp0 (mount1->mount_path, mount2->mount_path);
1712 res = g_strcmp0 (mount1->device_path, mount2->device_path);
1716 res = g_strcmp0 (mount1->filesystem_type, mount2->filesystem_type);
1720 res = g_strcmp0 (mount1->options, mount2->options);
1724 res = mount1->is_read_only - mount2->is_read_only;
1728 res = mount1->is_user_mountable - mount2->is_user_mountable;
1732 res = mount1->is_loopback - mount2->is_loopback;
1740 * g_unix_mount_point_get_mount_path:
1741 * @mount_point: a #GUnixMountPoint.
1743 * Gets the mount path for a unix mount point.
1745 * Returns: a string containing the mount path.
1748 g_unix_mount_point_get_mount_path (GUnixMountPoint *mount_point)
1750 g_return_val_if_fail (mount_point != NULL, NULL);
1752 return mount_point->mount_path;
1756 * g_unix_mount_point_get_device_path:
1757 * @mount_point: a #GUnixMountPoint.
1759 * Gets the device path for a unix mount point.
1761 * Returns: a string containing the device path.
1764 g_unix_mount_point_get_device_path (GUnixMountPoint *mount_point)
1766 g_return_val_if_fail (mount_point != NULL, NULL);
1768 return mount_point->device_path;
1772 * g_unix_mount_point_get_fs_type:
1773 * @mount_point: a #GUnixMountPoint.
1775 * Gets the file system type for the mount point.
1777 * Returns: a string containing the file system type.
1780 g_unix_mount_point_get_fs_type (GUnixMountPoint *mount_point)
1782 g_return_val_if_fail (mount_point != NULL, NULL);
1784 return mount_point->filesystem_type;
1788 * g_unix_mount_point_get_options:
1789 * @mount_point: a #GUnixMountPoint.
1791 * Gets the options for the mount point.
1793 * Returns: a string containing the options.
1798 g_unix_mount_point_get_options (GUnixMountPoint *mount_point)
1800 g_return_val_if_fail (mount_point != NULL, NULL);
1802 return mount_point->options;
1806 * g_unix_mount_point_is_readonly:
1807 * @mount_point: a #GUnixMountPoint.
1809 * Checks if a unix mount point is read only.
1811 * Returns: %TRUE if a mount point is read only.
1814 g_unix_mount_point_is_readonly (GUnixMountPoint *mount_point)
1816 g_return_val_if_fail (mount_point != NULL, FALSE);
1818 return mount_point->is_read_only;
1822 * g_unix_mount_point_is_user_mountable:
1823 * @mount_point: a #GUnixMountPoint.
1825 * Checks if a unix mount point is mountable by the user.
1827 * Returns: %TRUE if the mount point is user mountable.
1830 g_unix_mount_point_is_user_mountable (GUnixMountPoint *mount_point)
1832 g_return_val_if_fail (mount_point != NULL, FALSE);
1834 return mount_point->is_user_mountable;
1838 * g_unix_mount_point_is_loopback:
1839 * @mount_point: a #GUnixMountPoint.
1841 * Checks if a unix mount point is a loopback device.
1843 * Returns: %TRUE if the mount point is a loopback. %FALSE otherwise.
1846 g_unix_mount_point_is_loopback (GUnixMountPoint *mount_point)
1848 g_return_val_if_fail (mount_point != NULL, FALSE);
1850 return mount_point->is_loopback;
1853 static GUnixMountType
1854 guess_mount_type (const char *mount_path,
1855 const char *device_path,
1856 const char *filesystem_type)
1858 GUnixMountType type;
1861 type = G_UNIX_MOUNT_TYPE_UNKNOWN;
1863 if ((strcmp (filesystem_type, "udf") == 0) ||
1864 (strcmp (filesystem_type, "iso9660") == 0) ||
1865 (strcmp (filesystem_type, "cd9660") == 0))
1866 type = G_UNIX_MOUNT_TYPE_CDROM;
1867 else if ((strcmp (filesystem_type, "nfs") == 0) ||
1868 (strcmp (filesystem_type, "nfs4") == 0))
1869 type = G_UNIX_MOUNT_TYPE_NFS;
1870 else if (g_str_has_prefix (device_path, "/vol/dev/diskette/") ||
1871 g_str_has_prefix (device_path, "/dev/fd") ||
1872 g_str_has_prefix (device_path, "/dev/floppy"))
1873 type = G_UNIX_MOUNT_TYPE_FLOPPY;
1874 else if (g_str_has_prefix (device_path, "/dev/cdrom") ||
1875 g_str_has_prefix (device_path, "/dev/acd") ||
1876 g_str_has_prefix (device_path, "/dev/cd"))
1877 type = G_UNIX_MOUNT_TYPE_CDROM;
1878 else if (g_str_has_prefix (device_path, "/vol/"))
1880 const char *name = mount_path + strlen ("/");
1882 if (g_str_has_prefix (name, "cdrom"))
1883 type = G_UNIX_MOUNT_TYPE_CDROM;
1884 else if (g_str_has_prefix (name, "floppy") ||
1885 g_str_has_prefix (device_path, "/vol/dev/diskette/"))
1886 type = G_UNIX_MOUNT_TYPE_FLOPPY;
1887 else if (g_str_has_prefix (name, "rmdisk"))
1888 type = G_UNIX_MOUNT_TYPE_ZIP;
1889 else if (g_str_has_prefix (name, "jaz"))
1890 type = G_UNIX_MOUNT_TYPE_JAZ;
1891 else if (g_str_has_prefix (name, "memstick"))
1892 type = G_UNIX_MOUNT_TYPE_MEMSTICK;
1896 basename = g_path_get_basename (mount_path);
1898 if (g_str_has_prefix (basename, "cdr") ||
1899 g_str_has_prefix (basename, "cdwriter") ||
1900 g_str_has_prefix (basename, "burn") ||
1901 g_str_has_prefix (basename, "dvdr"))
1902 type = G_UNIX_MOUNT_TYPE_CDROM;
1903 else if (g_str_has_prefix (basename, "floppy"))
1904 type = G_UNIX_MOUNT_TYPE_FLOPPY;
1905 else if (g_str_has_prefix (basename, "zip"))
1906 type = G_UNIX_MOUNT_TYPE_ZIP;
1907 else if (g_str_has_prefix (basename, "jaz"))
1908 type = G_UNIX_MOUNT_TYPE_JAZ;
1909 else if (g_str_has_prefix (basename, "camera"))
1910 type = G_UNIX_MOUNT_TYPE_CAMERA;
1911 else if (g_str_has_prefix (basename, "memstick") ||
1912 g_str_has_prefix (basename, "memory_stick") ||
1913 g_str_has_prefix (basename, "ram"))
1914 type = G_UNIX_MOUNT_TYPE_MEMSTICK;
1915 else if (g_str_has_prefix (basename, "compact_flash"))
1916 type = G_UNIX_MOUNT_TYPE_CF;
1917 else if (g_str_has_prefix (basename, "smart_media"))
1918 type = G_UNIX_MOUNT_TYPE_SM;
1919 else if (g_str_has_prefix (basename, "sd_mmc"))
1920 type = G_UNIX_MOUNT_TYPE_SDMMC;
1921 else if (g_str_has_prefix (basename, "ipod"))
1922 type = G_UNIX_MOUNT_TYPE_IPOD;
1927 if (type == G_UNIX_MOUNT_TYPE_UNKNOWN)
1928 type = G_UNIX_MOUNT_TYPE_HD;
1934 * g_unix_mount_guess_type:
1935 * @mount_entry: a #GUnixMount.
1937 * Guesses the type of a unix mount. If the mount type cannot be
1938 * determined, returns %G_UNIX_MOUNT_TYPE_UNKNOWN.
1940 * Returns: a #GUnixMountType.
1942 static GUnixMountType
1943 g_unix_mount_guess_type (GUnixMountEntry *mount_entry)
1945 g_return_val_if_fail (mount_entry != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
1946 g_return_val_if_fail (mount_entry->mount_path != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
1947 g_return_val_if_fail (mount_entry->device_path != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
1948 g_return_val_if_fail (mount_entry->filesystem_type != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
1950 return guess_mount_type (mount_entry->mount_path,
1951 mount_entry->device_path,
1952 mount_entry->filesystem_type);
1956 * g_unix_mount_point_guess_type:
1957 * @mount_point: a #GUnixMountPoint.
1959 * Guesses the type of a unix mount point.
1960 * If the mount type cannot be determined,
1961 * returns %G_UNIX_MOUNT_TYPE_UNKNOWN.
1963 * Returns: a #GUnixMountType.
1965 static GUnixMountType
1966 g_unix_mount_point_guess_type (GUnixMountPoint *mount_point)
1968 g_return_val_if_fail (mount_point != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
1969 g_return_val_if_fail (mount_point->mount_path != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
1970 g_return_val_if_fail (mount_point->device_path != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
1971 g_return_val_if_fail (mount_point->filesystem_type != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
1973 return guess_mount_type (mount_point->mount_path,
1974 mount_point->device_path,
1975 mount_point->filesystem_type);
1979 type_to_icon (GUnixMountType type, gboolean is_mount_point, gboolean use_symbolic)
1981 const char *icon_name;
1985 case G_UNIX_MOUNT_TYPE_HD:
1987 icon_name = use_symbolic ? "drive-removable-media-symbolic" : "drive-removable-media";
1989 icon_name = use_symbolic ? "drive-harddisk-symbolic" : "drive-harddisk";
1991 case G_UNIX_MOUNT_TYPE_FLOPPY:
1992 case G_UNIX_MOUNT_TYPE_ZIP:
1993 case G_UNIX_MOUNT_TYPE_JAZ:
1995 icon_name = use_symbolic ? "drive-removable-media-symbolic" : "drive-removable-media";
1997 icon_name = use_symbolic ? "media-removable-symbolic" : "media-floppy";
1999 case G_UNIX_MOUNT_TYPE_CDROM:
2001 icon_name = use_symbolic ? "drive-optical-symbolic" : "drive-optical";
2003 icon_name = use_symbolic ? "media-optical-symbolic" : "media-optical";
2005 case G_UNIX_MOUNT_TYPE_NFS:
2006 icon_name = use_symbolic ? "folder-remote-symbolic" : "folder-remote";
2008 case G_UNIX_MOUNT_TYPE_MEMSTICK:
2010 icon_name = use_symbolic ? "drive-removable-media-symbolic" : "drive-removable-media";
2012 icon_name = use_symbolic ? "media-removable-symbolic" : "media-flash";
2014 case G_UNIX_MOUNT_TYPE_CAMERA:
2016 icon_name = use_symbolic ? "drive-removable-media-symbolic" : "drive-removable-media";
2018 icon_name = use_symbolic ? "camera-photo-symbolic" : "camera-photo";
2020 case G_UNIX_MOUNT_TYPE_IPOD:
2022 icon_name = use_symbolic ? "drive-removable-media-symbolic" : "drive-removable-media";
2024 icon_name = use_symbolic ? "multimedia-player-symbolic" : "multimedia-player";
2026 case G_UNIX_MOUNT_TYPE_UNKNOWN:
2029 icon_name = use_symbolic ? "drive-removable-media-symbolic" : "drive-removable-media";
2031 icon_name = use_symbolic ? "drive-harddisk-symbolic" : "drive-harddisk";
2039 * g_unix_mount_guess_name:
2040 * @mount_entry: a #GUnixMountEntry
2042 * Guesses the name of a Unix mount.
2043 * The result is a translated string.
2045 * Returns: A newly allocated string that must
2046 * be freed with g_free()
2049 g_unix_mount_guess_name (GUnixMountEntry *mount_entry)
2053 if (strcmp (mount_entry->mount_path, "/") == 0)
2054 name = g_strdup (_("Filesystem root"));
2056 name = g_filename_display_basename (mount_entry->mount_path);
2062 * g_unix_mount_guess_icon:
2063 * @mount_entry: a #GUnixMountEntry
2065 * Guesses the icon of a Unix mount.
2067 * Returns: (transfer full): a #GIcon
2070 g_unix_mount_guess_icon (GUnixMountEntry *mount_entry)
2072 return g_themed_icon_new_with_default_fallbacks (type_to_icon (g_unix_mount_guess_type (mount_entry), FALSE, FALSE));
2076 * g_unix_mount_guess_symbolic_icon:
2077 * @mount_entry: a #GUnixMountEntry
2079 * Guesses the symbolic icon of a Unix mount.
2081 * Returns: (transfer full): a #GIcon
2086 g_unix_mount_guess_symbolic_icon (GUnixMountEntry *mount_entry)
2088 return g_themed_icon_new_with_default_fallbacks (type_to_icon (g_unix_mount_guess_type (mount_entry), FALSE, TRUE));
2092 * g_unix_mount_point_guess_name:
2093 * @mount_point: a #GUnixMountPoint
2095 * Guesses the name of a Unix mount point.
2096 * The result is a translated string.
2098 * Returns: A newly allocated string that must
2099 * be freed with g_free()
2102 g_unix_mount_point_guess_name (GUnixMountPoint *mount_point)
2106 if (strcmp (mount_point->mount_path, "/") == 0)
2107 name = g_strdup (_("Filesystem root"));
2109 name = g_filename_display_basename (mount_point->mount_path);
2115 * g_unix_mount_point_guess_icon:
2116 * @mount_point: a #GUnixMountPoint
2118 * Guesses the icon of a Unix mount point.
2120 * Returns: (transfer full): a #GIcon
2123 g_unix_mount_point_guess_icon (GUnixMountPoint *mount_point)
2125 return g_themed_icon_new_with_default_fallbacks (type_to_icon (g_unix_mount_point_guess_type (mount_point), TRUE, FALSE));
2129 * g_unix_mount_point_guess_symbolic_icon:
2130 * @mount_point: a #GUnixMountPoint
2132 * Guesses the symbolic icon of a Unix mount point.
2134 * Returns: (transfer full): a #GIcon
2139 g_unix_mount_point_guess_symbolic_icon (GUnixMountPoint *mount_point)
2141 return g_themed_icon_new_with_default_fallbacks (type_to_icon (g_unix_mount_point_guess_type (mount_point), TRUE, TRUE));
2145 * g_unix_mount_guess_can_eject:
2146 * @mount_entry: a #GUnixMountEntry
2148 * Guesses whether a Unix mount can be ejected.
2150 * Returns: %TRUE if @mount_entry is deemed to be ejectable.
2153 g_unix_mount_guess_can_eject (GUnixMountEntry *mount_entry)
2155 GUnixMountType guessed_type;
2157 guessed_type = g_unix_mount_guess_type (mount_entry);
2158 if (guessed_type == G_UNIX_MOUNT_TYPE_IPOD ||
2159 guessed_type == G_UNIX_MOUNT_TYPE_CDROM)
2166 * g_unix_mount_guess_should_display:
2167 * @mount_entry: a #GUnixMountEntry
2169 * Guesses whether a Unix mount should be displayed in the UI.
2171 * Returns: %TRUE if @mount_entry is deemed to be displayable.
2174 g_unix_mount_guess_should_display (GUnixMountEntry *mount_entry)
2176 const char *mount_path;
2177 const gchar *user_name;
2178 gsize user_name_len;
2180 /* Never display internal mountpoints */
2181 if (g_unix_mount_is_system_internal (mount_entry))
2184 /* Only display things in /media (which are generally user mountable)
2185 and home dir (fuse stuff) and /run/media/$USER */
2186 mount_path = mount_entry->mount_path;
2187 if (mount_path != NULL)
2189 gboolean is_in_runtime_dir = FALSE;
2190 /* Hide mounts within a dot path, suppose it was a purpose to hide this mount */
2191 if (g_strstr_len (mount_path, -1, "/.") != NULL)
2194 /* Check /run/media/$USER/ */
2195 user_name = g_get_user_name ();
2196 user_name_len = strlen (user_name);
2197 if (strncmp (mount_path, "/run/media/", sizeof ("/run/media/") - 1) == 0 &&
2198 strncmp (mount_path + sizeof ("/run/media/") - 1, user_name, user_name_len) == 0 &&
2199 mount_path[sizeof ("/run/media/") - 1 + user_name_len] == '/')
2200 is_in_runtime_dir = TRUE;
2202 if (is_in_runtime_dir || g_str_has_prefix (mount_path, "/media/"))
2205 /* Avoid displaying mounts that are not accessible to the user.
2207 * See http://bugzilla.gnome.org/show_bug.cgi?id=526320 for why we
2208 * want to avoid g_access() for mount points which can potentially
2209 * block or fail stat()'ing, such as network mounts.
2211 path = g_path_get_dirname (mount_path);
2212 if (g_str_has_prefix (path, "/media/"))
2214 if (g_access (path, R_OK|X_OK) != 0)
2222 if (mount_entry->device_path && mount_entry->device_path[0] == '/')
2225 if (g_stat (mount_entry->device_path, &st) == 0 &&
2226 S_ISBLK(st.st_mode) &&
2227 g_access (mount_path, R_OK|X_OK) != 0)
2233 if (g_str_has_prefix (mount_path, g_get_home_dir ()) &&
2234 mount_path[strlen (g_get_home_dir())] == G_DIR_SEPARATOR)
2242 * g_unix_mount_point_guess_can_eject:
2243 * @mount_point: a #GUnixMountPoint
2245 * Guesses whether a Unix mount point can be ejected.
2247 * Returns: %TRUE if @mount_point is deemed to be ejectable.
2250 g_unix_mount_point_guess_can_eject (GUnixMountPoint *mount_point)
2252 GUnixMountType guessed_type;
2254 guessed_type = g_unix_mount_point_guess_type (mount_point);
2255 if (guessed_type == G_UNIX_MOUNT_TYPE_IPOD ||
2256 guessed_type == G_UNIX_MOUNT_TYPE_CDROM)
2262 #ifdef HAVE_MNTENT_H
2263 /* borrowed from gtk/gtkfilesystemunix.c in GTK+ on 02/23/2006 */
2265 _canonicalize_filename (gchar *filename)
2268 gboolean last_was_slash = FALSE;
2275 if (*p == G_DIR_SEPARATOR)
2277 if (!last_was_slash)
2278 *q++ = G_DIR_SEPARATOR;
2280 last_was_slash = TRUE;
2284 if (last_was_slash && *p == '.')
2286 if (*(p + 1) == G_DIR_SEPARATOR ||
2289 if (*(p + 1) == '\0')
2294 else if (*(p + 1) == '.' &&
2295 (*(p + 2) == G_DIR_SEPARATOR ||
2298 if (q > filename + 1)
2301 while (q > filename + 1 &&
2302 *(q - 1) != G_DIR_SEPARATOR)
2306 if (*(p + 2) == '\0')
2314 last_was_slash = FALSE;
2320 last_was_slash = FALSE;
2327 if (q > filename + 1 && *(q - 1) == G_DIR_SEPARATOR)
2334 _resolve_symlink (const char *file)
2342 f = g_strdup (file);
2344 while (g_file_test (f, G_FILE_TEST_IS_SYMLINK))
2346 link = g_file_read_link (f, &error);
2349 g_error_free (error);
2355 dir = g_path_get_dirname (f);
2356 f1 = g_strdup_printf ("%s/%s", dir, link);
2365 _canonicalize_filename (f);
2370 _resolve_dev_root (void)
2372 static gboolean have_real_dev_root = FALSE;
2373 static char real_dev_root[256];
2374 struct stat statbuf;
2376 /* see if it's cached already */
2377 if (have_real_dev_root)
2380 /* otherwise we're going to find it right away.. */
2381 have_real_dev_root = TRUE;
2383 if (stat ("/dev/root", &statbuf) == 0)
2385 if (! S_ISLNK (statbuf.st_mode))
2387 dev_t root_dev = statbuf.st_dev;
2390 /* see if device with similar major:minor as /dev/root is mention
2391 * in /etc/mtab (it usually is)
2393 f = fopen ("/etc/mtab", "r");
2396 struct mntent *entp;
2397 #ifdef HAVE_GETMNTENT_R
2400 while ((entp = getmntent_r (f, &ent, buf, sizeof (buf))) != NULL)
2404 while ((entp = getmntent (f)) != NULL)
2407 if (stat (entp->mnt_fsname, &statbuf) == 0 &&
2408 statbuf.st_dev == root_dev)
2410 strncpy (real_dev_root, entp->mnt_fsname, sizeof (real_dev_root) - 1);
2411 real_dev_root[sizeof (real_dev_root) - 1] = '\0';
2419 #ifndef HAVE_GETMNTENT_R
2420 G_UNLOCK (getmntent);
2424 /* no, that didn't work.. next we could scan /dev ... but I digress.. */
2430 resolved = _resolve_symlink ("/dev/root");
2431 if (resolved != NULL)
2433 strncpy (real_dev_root, resolved, sizeof (real_dev_root) - 1);
2434 real_dev_root[sizeof (real_dev_root) - 1] = '\0';
2442 strcpy (real_dev_root, "/dev/root");
2445 return real_dev_root;