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 GSource *proc_mounts_watch_source;
156 struct _GUnixMountMonitorClass {
157 GObjectClass parent_class;
160 static GUnixMountMonitor *the_mount_monitor = NULL;
162 static GList *_g_get_unix_mounts (void);
163 static GList *_g_get_unix_mount_points (void);
165 G_DEFINE_TYPE (GUnixMountMonitor, g_unix_mount_monitor, G_TYPE_OBJECT);
167 #define MOUNT_POLL_INTERVAL 4000
169 #ifdef HAVE_SYS_MNTTAB_H
170 #define MNTOPT_RO "ro"
175 #elif defined (HAVE_SYS_MNTTAB_H)
176 #include <sys/mnttab.h>
179 #ifdef HAVE_SYS_VFSTAB_H
180 #include <sys/vfstab.h>
183 #if defined(HAVE_SYS_MNTCTL_H) && defined(HAVE_SYS_VMOUNT_H) && defined(HAVE_SYS_VFS_H)
184 #include <sys/mntctl.h>
186 #include <sys/vmount.h>
190 #if (defined(HAVE_GETVFSSTAT) || defined(HAVE_GETFSSTAT)) && defined(HAVE_FSTAB_H) && defined(HAVE_SYS_MOUNT_H)
191 #include <sys/ucred.h>
192 #include <sys/mount.h>
194 #ifdef HAVE_SYS_SYSCTL_H
195 #include <sys/sysctl.h>
199 #ifndef HAVE_SETMNTENT
200 #define setmntent(f,m) fopen(f,m)
202 #ifndef HAVE_ENDMNTENT
203 #define endmntent(f) fclose(f)
207 is_in (const char *value, const char *set[])
210 for (i = 0; set[i] != NULL; i++)
212 if (strcmp (set[i], value) == 0)
219 * g_unix_is_mount_path_system_internal:
220 * @mount_path: a mount path, e.g. `/media/disk` or `/usr`
222 * Determines if @mount_path is considered an implementation of the
223 * OS. This is primarily used for hiding mountable and mounted volumes
224 * that only are used in the OS and has little to no relevance to the
227 * Returns: %TRUE if @mount_path is considered an implementation detail
231 g_unix_is_mount_path_system_internal (const char *mount_path)
233 const char *ignore_mountpoints[] = {
234 /* Includes all FHS 2.3 toplevel dirs and other specilized
235 * directories that we want to hide from the user.
237 "/", /* we already have "Filesystem root" in Nautilus */
240 "/compat/linux/proc",
269 "/var/log/audit", /* https://bugzilla.redhat.com/show_bug.cgi?id=333041 */
272 "/var/tmp", /* https://bugzilla.redhat.com/show_bug.cgi?id=335241 */
280 if (is_in (mount_path, ignore_mountpoints))
283 if (g_str_has_prefix (mount_path, "/dev/") ||
284 g_str_has_prefix (mount_path, "/proc/") ||
285 g_str_has_prefix (mount_path, "/sys/"))
288 if (g_str_has_suffix (mount_path, "/.gvfs"))
295 guess_system_internal (const char *mountpoint,
299 const char *ignore_fs[] = {
323 const char *ignore_devices[] = {
333 if (is_in (fs, ignore_fs))
336 if (is_in (device, ignore_devices))
339 if (g_unix_is_mount_path_system_internal (mountpoint))
348 get_mtab_read_file (void)
352 return "/proc/mounts";
354 return _PATH_MOUNTED;
362 get_mtab_monitor_file (void)
366 return "/proc/mounts";
368 return _PATH_MOUNTED;
375 #ifndef HAVE_GETMNTENT_R
376 G_LOCK_DEFINE_STATIC(getmntent);
380 _g_get_unix_mounts (void)
382 #ifdef HAVE_GETMNTENT_R
386 struct mntent *mntent;
389 GUnixMountEntry *mount_entry;
390 GHashTable *mounts_hash;
393 read_file = get_mtab_read_file ();
395 file = setmntent (read_file, "r");
401 mounts_hash = g_hash_table_new (g_str_hash, g_str_equal);
403 #ifdef HAVE_GETMNTENT_R
404 while ((mntent = getmntent_r (file, &ent, buf, sizeof (buf))) != NULL)
407 while ((mntent = getmntent (file)) != NULL)
410 /* ignore any mnt_fsname that is repeated and begins with a '/'
412 * We do this to avoid being fooled by --bind mounts, since
413 * these have the same device as the location they bind to.
414 * It's not an ideal solution to the problem, but it's likely that
415 * the most important mountpoint is first and the --bind ones after
416 * that aren't as important. So it should work.
418 * The '/' is to handle procfs, tmpfs and other no device mounts.
420 if (mntent->mnt_fsname != NULL &&
421 mntent->mnt_fsname[0] == '/' &&
422 g_hash_table_lookup (mounts_hash, mntent->mnt_fsname))
425 mount_entry = g_new0 (GUnixMountEntry, 1);
426 mount_entry->mount_path = g_strdup (mntent->mnt_dir);
427 if (g_strcmp0 (mntent->mnt_fsname, "/dev/root") == 0)
428 mount_entry->device_path = g_strdup (_resolve_dev_root ());
430 mount_entry->device_path = g_strdup (mntent->mnt_fsname);
431 mount_entry->filesystem_type = g_strdup (mntent->mnt_type);
433 #if defined (HAVE_HASMNTOPT)
434 if (hasmntopt (mntent, MNTOPT_RO) != NULL)
435 mount_entry->is_read_only = TRUE;
438 mount_entry->is_system_internal =
439 guess_system_internal (mount_entry->mount_path,
440 mount_entry->filesystem_type,
441 mount_entry->device_path);
443 g_hash_table_insert (mounts_hash,
444 mount_entry->device_path,
445 mount_entry->device_path);
447 return_list = g_list_prepend (return_list, mount_entry);
449 g_hash_table_destroy (mounts_hash);
453 #ifndef HAVE_GETMNTENT_R
454 G_UNLOCK (getmntent);
457 return g_list_reverse (return_list);
460 #elif defined (HAVE_SYS_MNTTAB_H)
462 G_LOCK_DEFINE_STATIC(getmntent);
465 get_mtab_read_file (void)
468 return _PATH_MOUNTED;
470 return "/etc/mnttab";
475 get_mtab_monitor_file (void)
477 return get_mtab_read_file ();
481 _g_get_unix_mounts (void)
483 struct mnttab mntent;
486 GUnixMountEntry *mount_entry;
489 read_file = get_mtab_read_file ();
491 file = setmntent (read_file, "r");
498 while (! getmntent (file, &mntent))
500 mount_entry = g_new0 (GUnixMountEntry, 1);
502 mount_entry->mount_path = g_strdup (mntent.mnt_mountp);
503 mount_entry->device_path = g_strdup (mntent.mnt_special);
504 mount_entry->filesystem_type = g_strdup (mntent.mnt_fstype);
506 #if defined (HAVE_HASMNTOPT)
507 if (hasmntopt (&mntent, MNTOPT_RO) != NULL)
508 mount_entry->is_read_only = TRUE;
511 mount_entry->is_system_internal =
512 guess_system_internal (mount_entry->mount_path,
513 mount_entry->filesystem_type,
514 mount_entry->device_path);
516 return_list = g_list_prepend (return_list, mount_entry);
521 G_UNLOCK (getmntent);
523 return g_list_reverse (return_list);
526 #elif defined(HAVE_SYS_MNTCTL_H) && defined(HAVE_SYS_VMOUNT_H) && defined(HAVE_SYS_VFS_H)
529 get_mtab_monitor_file (void)
535 _g_get_unix_mounts (void)
537 struct vfs_ent *fs_info;
538 struct vmount *vmount_info;
540 unsigned int vmount_size;
544 if (mntctl (MCTL_QUERY, sizeof (vmount_size), &vmount_size) != 0)
546 g_warning ("Unable to know the number of mounted volumes\n");
551 vmount_info = (struct vmount*)g_malloc (vmount_size);
553 vmount_number = mntctl (MCTL_QUERY, vmount_size, vmount_info);
555 if (vmount_info->vmt_revision != VMT_REVISION)
556 g_warning ("Bad vmount structure revision number, want %d, got %d\n", VMT_REVISION, vmount_info->vmt_revision);
558 if (vmount_number < 0)
560 g_warning ("Unable to recover mounted volumes information\n");
562 g_free (vmount_info);
567 while (vmount_number > 0)
569 mount_entry = g_new0 (GUnixMountEntry, 1);
571 mount_entry->device_path = g_strdup (vmt2dataptr (vmount_info, VMT_OBJECT));
572 mount_entry->mount_path = g_strdup (vmt2dataptr (vmount_info, VMT_STUB));
573 /* is_removable = (vmount_info->vmt_flags & MNT_REMOVABLE) ? 1 : 0; */
574 mount_entry->is_read_only = (vmount_info->vmt_flags & MNT_READONLY) ? 1 : 0;
576 fs_info = getvfsbytype (vmount_info->vmt_gfstype);
579 mount_entry->filesystem_type = g_strdup ("unknown");
581 mount_entry->filesystem_type = g_strdup (fs_info->vfsent_name);
583 mount_entry->is_system_internal =
584 guess_system_internal (mount_entry->mount_path,
585 mount_entry->filesystem_type,
586 mount_entry->device_path);
588 return_list = g_list_prepend (return_list, mount_entry);
590 vmount_info = (struct vmount *)( (char*)vmount_info
591 + vmount_info->vmt_length);
595 g_free (vmount_info);
597 return g_list_reverse (return_list);
600 #elif (defined(HAVE_GETVFSSTAT) || defined(HAVE_GETFSSTAT)) && defined(HAVE_FSTAB_H) && defined(HAVE_SYS_MOUNT_H)
603 get_mtab_monitor_file (void)
609 _g_get_unix_mounts (void)
611 #if defined(HAVE_GETVFSSTAT)
612 struct statvfs *mntent = NULL;
613 #elif defined(HAVE_GETFSSTAT)
614 struct statfs *mntent = NULL;
616 #error statfs juggling failed
620 GUnixMountEntry *mount_entry;
623 /* Pass NOWAIT to avoid blocking trying to update NFS mounts. */
624 #if defined(HAVE_GETVFSSTAT)
625 num_mounts = getvfsstat (NULL, 0, ST_NOWAIT);
626 #elif defined(HAVE_GETFSSTAT)
627 num_mounts = getfsstat (NULL, 0, MNT_NOWAIT);
629 if (num_mounts == -1)
632 bufsize = num_mounts * sizeof (*mntent);
633 mntent = g_malloc (bufsize);
634 #if defined(HAVE_GETVFSSTAT)
635 num_mounts = getvfsstat (mntent, bufsize, ST_NOWAIT);
636 #elif defined(HAVE_GETFSSTAT)
637 num_mounts = getfsstat (mntent, bufsize, MNT_NOWAIT);
639 if (num_mounts == -1)
644 for (i = 0; i < num_mounts; i++)
646 mount_entry = g_new0 (GUnixMountEntry, 1);
648 mount_entry->mount_path = g_strdup (mntent[i].f_mntonname);
649 mount_entry->device_path = g_strdup (mntent[i].f_mntfromname);
650 mount_entry->filesystem_type = g_strdup (mntent[i].f_fstypename);
651 #if defined(HAVE_GETVFSSTAT)
652 if (mntent[i].f_flag & ST_RDONLY)
653 #elif defined(HAVE_GETFSSTAT)
654 if (mntent[i].f_flags & MNT_RDONLY)
656 mount_entry->is_read_only = TRUE;
658 mount_entry->is_system_internal =
659 guess_system_internal (mount_entry->mount_path,
660 mount_entry->filesystem_type,
661 mount_entry->device_path);
663 return_list = g_list_prepend (return_list, mount_entry);
668 return g_list_reverse (return_list);
670 #elif defined(__INTERIX)
673 get_mtab_monitor_file (void)
679 _g_get_unix_mounts (void)
682 GList* return_list = NULL;
683 char filename[9 + NAME_MAX];
685 dirp = opendir ("/dev/fs");
688 g_warning ("unable to read /dev/fs!");
694 struct statvfs statbuf;
696 struct dirent* result;
698 if (readdir_r (dirp, &entry, &result) || result == NULL)
701 strcpy (filename, "/dev/fs/");
702 strcat (filename, entry.d_name);
704 if (statvfs (filename, &statbuf) == 0)
706 GUnixMountEntry* mount_entry = g_new0(GUnixMountEntry, 1);
708 mount_entry->mount_path = g_strdup (statbuf.f_mntonname);
709 mount_entry->device_path = g_strdup (statbuf.f_mntfromname);
710 mount_entry->filesystem_type = g_strdup (statbuf.f_fstypename);
712 if (statbuf.f_flag & ST_RDONLY)
713 mount_entry->is_read_only = TRUE;
715 return_list = g_list_prepend(return_list, mount_entry);
719 return_list = g_list_reverse (return_list);
726 #error No _g_get_unix_mounts() implementation for system
729 /* _g_get_unix_mount_points():
731 * don't return swap and ignore mounts.
735 get_fstab_file (void)
737 #if defined(HAVE_SYS_MNTCTL_H) && defined(HAVE_SYS_VMOUNT_H) && defined(HAVE_SYS_VFS_H)
739 return "/etc/filesystems";
740 #elif defined(_PATH_MNTTAB)
742 #elif defined(VFSTAB)
751 _g_get_unix_mount_points (void)
753 #ifdef HAVE_GETMNTENT_R
757 struct mntent *mntent;
760 GUnixMountPoint *mount_entry;
763 read_file = get_fstab_file ();
765 file = setmntent (read_file, "r");
771 #ifdef HAVE_GETMNTENT_R
772 while ((mntent = getmntent_r (file, &ent, buf, sizeof (buf))) != NULL)
775 while ((mntent = getmntent (file)) != NULL)
778 if ((strcmp (mntent->mnt_dir, "ignore") == 0) ||
779 (strcmp (mntent->mnt_dir, "swap") == 0) ||
780 (strcmp (mntent->mnt_dir, "none") == 0))
783 #ifdef HAVE_HASMNTOPT
784 /* We ignore bind fstab entries, as we ignore bind mounts anyway */
785 if (hasmntopt (mntent, "bind"))
789 mount_entry = g_new0 (GUnixMountPoint, 1);
790 mount_entry->mount_path = g_strdup (mntent->mnt_dir);
791 if (strcmp (mntent->mnt_fsname, "/dev/root") == 0)
792 mount_entry->device_path = g_strdup (_resolve_dev_root ());
794 mount_entry->device_path = g_strdup (mntent->mnt_fsname);
795 mount_entry->filesystem_type = g_strdup (mntent->mnt_type);
796 mount_entry->options = g_strdup (mntent->mnt_opts);
798 #ifdef HAVE_HASMNTOPT
799 if (hasmntopt (mntent, MNTOPT_RO) != NULL)
800 mount_entry->is_read_only = TRUE;
802 if (hasmntopt (mntent, "loop") != NULL)
803 mount_entry->is_loopback = TRUE;
807 if ((mntent->mnt_type != NULL && strcmp ("supermount", mntent->mnt_type) == 0)
808 #ifdef HAVE_HASMNTOPT
809 || (hasmntopt (mntent, "user") != NULL
810 && hasmntopt (mntent, "user") != hasmntopt (mntent, "user_xattr"))
811 || hasmntopt (mntent, "pamconsole") != NULL
812 || hasmntopt (mntent, "users") != NULL
813 || hasmntopt (mntent, "owner") != NULL
816 mount_entry->is_user_mountable = TRUE;
818 return_list = g_list_prepend (return_list, mount_entry);
823 #ifndef HAVE_GETMNTENT_R
824 G_UNLOCK (getmntent);
827 return g_list_reverse (return_list);
830 #elif defined (HAVE_SYS_MNTTAB_H)
833 _g_get_unix_mount_points (void)
835 struct mnttab mntent;
838 GUnixMountPoint *mount_entry;
841 read_file = get_fstab_file ();
843 file = setmntent (read_file, "r");
850 while (! getmntent (file, &mntent))
852 if ((strcmp (mntent.mnt_mountp, "ignore") == 0) ||
853 (strcmp (mntent.mnt_mountp, "swap") == 0) ||
854 (strcmp (mntent.mnt_mountp, "none") == 0))
857 mount_entry = g_new0 (GUnixMountPoint, 1);
859 mount_entry->mount_path = g_strdup (mntent.mnt_mountp);
860 mount_entry->device_path = g_strdup (mntent.mnt_special);
861 mount_entry->filesystem_type = g_strdup (mntent.mnt_fstype);
862 mount_entry->options = g_strdup (mntent.mnt_mntopts);
864 #ifdef HAVE_HASMNTOPT
865 if (hasmntopt (&mntent, MNTOPT_RO) != NULL)
866 mount_entry->is_read_only = TRUE;
868 if (hasmntopt (&mntent, "lofs") != NULL)
869 mount_entry->is_loopback = TRUE;
872 if ((mntent.mnt_fstype != NULL)
873 #ifdef HAVE_HASMNTOPT
874 || (hasmntopt (&mntent, "user") != NULL
875 && hasmntopt (&mntent, "user") != hasmntopt (&mntent, "user_xattr"))
876 || hasmntopt (&mntent, "pamconsole") != NULL
877 || hasmntopt (&mntent, "users") != NULL
878 || hasmntopt (&mntent, "owner") != NULL
881 mount_entry->is_user_mountable = TRUE;
883 return_list = g_list_prepend (return_list, mount_entry);
887 G_UNLOCK (getmntent);
889 return g_list_reverse (return_list);
891 #elif defined(HAVE_SYS_MNTCTL_H) && defined(HAVE_SYS_VMOUNT_H) && defined(HAVE_SYS_VFS_H)
893 /* functions to parse /etc/filesystems on aix */
895 /* read character, ignoring comments (begin with '*', end with '\n' */
897 aix_fs_getc (FILE *fd)
901 while ((c = getc (fd)) == '*')
903 while (((c = getc (fd)) != '\n') && (c != EOF))
908 /* eat all continuous spaces in a file */
910 aix_fs_ignorespace (FILE *fd)
914 while ((c = aix_fs_getc (fd)) != EOF)
916 if (!g_ascii_isspace (c))
926 /* read one word from file */
928 aix_fs_getword (FILE *fd,
933 aix_fs_ignorespace (fd);
935 while (((c = aix_fs_getc (fd)) != EOF) && !g_ascii_isspace (c))
939 while (((c = aix_fs_getc (fd)) != EOF) && (c != '"'))
951 char mnt_mount[PATH_MAX];
952 char mnt_special[PATH_MAX];
954 char mnt_options[128];
955 } AixMountTableEntry;
957 /* read mount points properties */
959 aix_fs_get (FILE *fd,
960 AixMountTableEntry *prop)
962 static char word[PATH_MAX] = { 0 };
963 char value[PATH_MAX];
968 if (aix_fs_getword (fd, word) == EOF)
972 word[strlen(word) - 1] = 0;
973 strcpy (prop->mnt_mount, word);
975 /* read attributes and value */
977 while (aix_fs_getword (fd, word) != EOF)
979 /* test if is attribute or new stanza */
980 if (word[strlen(word) - 1] == ':')
984 aix_fs_getword (fd, value);
987 aix_fs_getword (fd, value);
989 if (strcmp (word, "dev") == 0)
990 strcpy (prop->mnt_special, value);
991 else if (strcmp (word, "vfs") == 0)
992 strcpy (prop->mnt_fstype, value);
993 else if (strcmp (word, "options") == 0)
994 strcpy(prop->mnt_options, value);
1001 _g_get_unix_mount_points (void)
1003 struct mntent *mntent;
1006 GUnixMountPoint *mount_entry;
1007 AixMountTableEntry mntent;
1010 read_file = get_fstab_file ();
1012 file = setmntent (read_file, "r");
1018 while (!aix_fs_get (file, &mntent))
1020 if (strcmp ("cdrfs", mntent.mnt_fstype) == 0)
1022 mount_entry = g_new0 (GUnixMountPoint, 1);
1024 mount_entry->mount_path = g_strdup (mntent.mnt_mount);
1025 mount_entry->device_path = g_strdup (mntent.mnt_special);
1026 mount_entry->filesystem_type = g_strdup (mntent.mnt_fstype);
1027 mount_entry->options = g_strdup (mntent.mnt_options);
1028 mount_entry->is_read_only = TRUE;
1029 mount_entry->is_user_mountable = TRUE;
1031 return_list = g_list_prepend (return_list, mount_entry);
1037 return g_list_reverse (return_list);
1040 #elif (defined(HAVE_GETVFSSTAT) || defined(HAVE_GETFSSTAT)) && defined(HAVE_FSTAB_H) && defined(HAVE_SYS_MOUNT_H)
1043 _g_get_unix_mount_points (void)
1045 struct fstab *fstab = NULL;
1046 GUnixMountPoint *mount_entry;
1048 #ifdef HAVE_SYS_SYSCTL_H
1050 size_t len = sizeof(usermnt);
1059 #ifdef HAVE_SYS_SYSCTL_H
1060 #if defined(HAVE_SYSCTLBYNAME)
1061 sysctlbyname ("vfs.usermount", &usermnt, &len, NULL, 0);
1062 #elif defined(CTL_VFS) && defined(VFS_USERMOUNT)
1067 mib[1] = VFS_USERMOUNT;
1068 sysctl (mib, 2, &usermnt, &len, NULL, 0);
1070 #elif defined(CTL_KERN) && defined(KERN_USERMOUNT)
1075 mib[1] = KERN_USERMOUNT;
1076 sysctl (mib, 2, &usermnt, &len, NULL, 0);
1081 while ((fstab = getfsent ()) != NULL)
1083 if (strcmp (fstab->fs_vfstype, "swap") == 0)
1086 mount_entry = g_new0 (GUnixMountPoint, 1);
1088 mount_entry->mount_path = g_strdup (fstab->fs_file);
1089 mount_entry->device_path = g_strdup (fstab->fs_spec);
1090 mount_entry->filesystem_type = g_strdup (fstab->fs_vfstype);
1091 mount_entry->options = g_strdup (fstab->fs_mntops);
1093 if (strcmp (fstab->fs_type, "ro") == 0)
1094 mount_entry->is_read_only = TRUE;
1096 #ifdef HAVE_SYS_SYSCTL_H
1099 uid_t uid = getuid ();
1100 if (stat (fstab->fs_file, &sb) == 0)
1102 if (uid == 0 || sb.st_uid == uid)
1103 mount_entry->is_user_mountable = TRUE;
1108 return_list = g_list_prepend (return_list, mount_entry);
1113 return g_list_reverse (return_list);
1115 #elif defined(__INTERIX)
1117 _g_get_unix_mount_points (void)
1119 return _g_get_unix_mounts ();
1122 #error No g_get_mount_table() implementation for system
1126 get_mounts_timestamp (void)
1128 const char *monitor_file;
1131 monitor_file = get_mtab_monitor_file ();
1134 if (stat (monitor_file, &buf) == 0)
1135 return (guint64)buf.st_mtime;
1141 get_mount_points_timestamp (void)
1143 const char *monitor_file;
1146 monitor_file = get_fstab_file ();
1149 if (stat (monitor_file, &buf) == 0)
1150 return (guint64)buf.st_mtime;
1156 * g_unix_mounts_get: (skip)
1157 * @time_read: (out) (allow-none): guint64 to contain a timestamp, or %NULL
1159 * Gets a #GList of #GUnixMountEntry containing the unix mounts.
1160 * If @time_read is set, it will be filled with the mount
1161 * timestamp, allowing for checking if the mounts have changed
1162 * with g_unix_mounts_changed_since().
1164 * Returns: (element-type GUnixMountEntry) (transfer full):
1165 * a #GList of the UNIX mounts.
1168 g_unix_mounts_get (guint64 *time_read)
1171 *time_read = get_mounts_timestamp ();
1173 return _g_get_unix_mounts ();
1177 * g_unix_mount_at: (skip)
1178 * @mount_path: path for a possible unix mount.
1179 * @time_read: (out) (allow-none): guint64 to contain a timestamp.
1181 * Gets a #GUnixMountEntry for a given mount path. If @time_read
1182 * is set, it will be filled with a unix timestamp for checking
1183 * if the mounts have changed since with g_unix_mounts_changed_since().
1185 * Returns: (transfer full): a #GUnixMountEntry.
1188 g_unix_mount_at (const char *mount_path,
1192 GUnixMountEntry *mount_entry, *found;
1194 mounts = g_unix_mounts_get (time_read);
1197 for (l = mounts; l != NULL; l = l->next)
1199 mount_entry = l->data;
1201 if (!found && strcmp (mount_path, mount_entry->mount_path) == 0)
1202 found = mount_entry;
1204 g_unix_mount_free (mount_entry);
1206 g_list_free (mounts);
1212 * g_unix_mount_points_get: (skip)
1213 * @time_read: (out) (allow-none): guint64 to contain a timestamp.
1215 * Gets a #GList of #GUnixMountPoint containing the unix mount points.
1216 * If @time_read is set, it will be filled with the mount timestamp,
1217 * allowing for checking if the mounts have changed with
1218 * g_unix_mount_points_changed_since().
1220 * Returns: (element-type GUnixMountPoint) (transfer full):
1221 * a #GList of the UNIX mountpoints.
1224 g_unix_mount_points_get (guint64 *time_read)
1227 *time_read = get_mount_points_timestamp ();
1229 return _g_get_unix_mount_points ();
1233 * g_unix_mounts_changed_since:
1234 * @time: guint64 to contain a timestamp.
1236 * Checks if the unix mounts have changed since a given unix time.
1238 * Returns: %TRUE if the mounts have changed since @time.
1241 g_unix_mounts_changed_since (guint64 time)
1243 return get_mounts_timestamp () != time;
1247 * g_unix_mount_points_changed_since:
1248 * @time: guint64 to contain a timestamp.
1250 * Checks if the unix mount points have changed since a given unix time.
1252 * Returns: %TRUE if the mount points have changed since @time.
1255 g_unix_mount_points_changed_since (guint64 time)
1257 return get_mount_points_timestamp () != time;
1261 g_unix_mount_monitor_finalize (GObject *object)
1263 GUnixMountMonitor *monitor;
1265 monitor = G_UNIX_MOUNT_MONITOR (object);
1267 if (monitor->fstab_monitor)
1269 g_file_monitor_cancel (monitor->fstab_monitor);
1270 g_object_unref (monitor->fstab_monitor);
1273 if (monitor->proc_mounts_watch_source != NULL)
1274 g_source_destroy (monitor->proc_mounts_watch_source);
1276 if (monitor->mtab_monitor)
1278 g_file_monitor_cancel (monitor->mtab_monitor);
1279 g_object_unref (monitor->mtab_monitor);
1282 the_mount_monitor = NULL;
1284 G_OBJECT_CLASS (g_unix_mount_monitor_parent_class)->finalize (object);
1289 g_unix_mount_monitor_class_init (GUnixMountMonitorClass *klass)
1291 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
1293 gobject_class->finalize = g_unix_mount_monitor_finalize;
1296 * GUnixMountMonitor::mounts-changed:
1297 * @monitor: the object on which the signal is emitted
1299 * Emitted when the unix mounts have changed.
1301 signals[MOUNTS_CHANGED] =
1302 g_signal_new ("mounts-changed",
1303 G_TYPE_FROM_CLASS (klass),
1307 g_cclosure_marshal_VOID__VOID,
1311 * GUnixMountMonitor::mountpoints-changed:
1312 * @monitor: the object on which the signal is emitted
1314 * Emitted when the unix mount points have changed.
1316 signals[MOUNTPOINTS_CHANGED] =
1317 g_signal_new ("mountpoints-changed",
1318 G_TYPE_FROM_CLASS (klass),
1322 g_cclosure_marshal_VOID__VOID,
1327 fstab_file_changed (GFileMonitor *monitor,
1330 GFileMonitorEvent event_type,
1333 GUnixMountMonitor *mount_monitor;
1335 if (event_type != G_FILE_MONITOR_EVENT_CHANGED &&
1336 event_type != G_FILE_MONITOR_EVENT_CREATED &&
1337 event_type != G_FILE_MONITOR_EVENT_DELETED)
1340 mount_monitor = user_data;
1341 g_signal_emit (mount_monitor, signals[MOUNTPOINTS_CHANGED], 0);
1345 mtab_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[MOUNTS_CHANGED], 0);
1363 proc_mounts_changed (GIOChannel *channel,
1367 GUnixMountMonitor *mount_monitor = G_UNIX_MOUNT_MONITOR (user_data);
1368 if (cond & G_IO_ERR)
1369 g_signal_emit (mount_monitor, signals[MOUNTS_CHANGED], 0);
1374 g_unix_mount_monitor_init (GUnixMountMonitor *monitor)
1378 if (get_fstab_file () != NULL)
1380 file = g_file_new_for_path (get_fstab_file ());
1381 monitor->fstab_monitor = g_file_monitor_file (file, 0, NULL, NULL);
1382 g_object_unref (file);
1384 g_signal_connect (monitor->fstab_monitor, "changed", (GCallback)fstab_file_changed, monitor);
1387 if (get_mtab_monitor_file () != NULL)
1389 const gchar *mtab_path;
1391 mtab_path = get_mtab_monitor_file ();
1392 /* /proc/mounts monitoring is special - can't just use GFileMonitor.
1393 * See 'man proc' for more details.
1395 if (g_strcmp0 (mtab_path, "/proc/mounts") == 0)
1397 GIOChannel *proc_mounts_channel;
1398 GError *error = NULL;
1399 proc_mounts_channel = g_io_channel_new_file ("/proc/mounts", "r", &error);
1400 if (proc_mounts_channel == NULL)
1402 g_warning ("Error creating IO channel for /proc/mounts: %s (%s, %d)",
1403 error->message, g_quark_to_string (error->domain), error->code);
1404 g_error_free (error);
1408 monitor->proc_mounts_watch_source = g_io_create_watch (proc_mounts_channel, G_IO_ERR);
1409 g_source_set_callback (monitor->proc_mounts_watch_source,
1410 (GSourceFunc) proc_mounts_changed,
1413 g_source_attach (monitor->proc_mounts_watch_source,
1414 g_main_context_get_thread_default ());
1415 g_source_unref (monitor->proc_mounts_watch_source);
1416 g_io_channel_unref (proc_mounts_channel);
1421 file = g_file_new_for_path (mtab_path);
1422 monitor->mtab_monitor = g_file_monitor_file (file, 0, NULL, NULL);
1423 g_object_unref (file);
1424 g_signal_connect (monitor->mtab_monitor, "changed", (GCallback)mtab_file_changed, monitor);
1430 * g_unix_mount_monitor_set_rate_limit:
1431 * @mount_monitor: a #GUnixMountMonitor
1432 * @limit_msec: a integer with the limit in milliseconds to
1435 * Sets the rate limit to which the @mount_monitor will report
1436 * consecutive change events to the mount and mount point entry files.
1441 g_unix_mount_monitor_set_rate_limit (GUnixMountMonitor *mount_monitor,
1444 g_return_if_fail (G_IS_UNIX_MOUNT_MONITOR (mount_monitor));
1446 if (mount_monitor->fstab_monitor != NULL)
1447 g_file_monitor_set_rate_limit (mount_monitor->fstab_monitor, limit_msec);
1449 if (mount_monitor->mtab_monitor != NULL)
1450 g_file_monitor_set_rate_limit (mount_monitor->mtab_monitor, limit_msec);
1454 * g_unix_mount_monitor_new:
1456 * Gets a new #GUnixMountMonitor. The default rate limit for which the
1457 * monitor will report consecutive changes for the mount and mount
1458 * point entry files is the default for a #GFileMonitor. Use
1459 * g_unix_mount_monitor_set_rate_limit() to change this.
1461 * Returns: a #GUnixMountMonitor.
1464 g_unix_mount_monitor_new (void)
1466 if (the_mount_monitor == NULL)
1468 the_mount_monitor = g_object_new (G_TYPE_UNIX_MOUNT_MONITOR, NULL);
1469 return the_mount_monitor;
1472 return g_object_ref (the_mount_monitor);
1476 * g_unix_mount_free:
1477 * @mount_entry: a #GUnixMountEntry.
1479 * Frees a unix mount.
1482 g_unix_mount_free (GUnixMountEntry *mount_entry)
1484 g_return_if_fail (mount_entry != NULL);
1486 g_free (mount_entry->mount_path);
1487 g_free (mount_entry->device_path);
1488 g_free (mount_entry->filesystem_type);
1489 g_free (mount_entry);
1493 * g_unix_mount_point_free:
1494 * @mount_point: unix mount point to free.
1496 * Frees a unix mount point.
1499 g_unix_mount_point_free (GUnixMountPoint *mount_point)
1501 g_return_if_fail (mount_point != NULL);
1503 g_free (mount_point->mount_path);
1504 g_free (mount_point->device_path);
1505 g_free (mount_point->filesystem_type);
1506 g_free (mount_point->options);
1507 g_free (mount_point);
1511 * g_unix_mount_compare:
1512 * @mount1: first #GUnixMountEntry to compare.
1513 * @mount2: second #GUnixMountEntry to compare.
1515 * Compares two unix mounts.
1517 * Returns: 1, 0 or -1 if @mount1 is greater than, equal to,
1518 * or less than @mount2, respectively.
1521 g_unix_mount_compare (GUnixMountEntry *mount1,
1522 GUnixMountEntry *mount2)
1526 g_return_val_if_fail (mount1 != NULL && mount2 != NULL, 0);
1528 res = g_strcmp0 (mount1->mount_path, mount2->mount_path);
1532 res = g_strcmp0 (mount1->device_path, mount2->device_path);
1536 res = g_strcmp0 (mount1->filesystem_type, mount2->filesystem_type);
1540 res = mount1->is_read_only - mount2->is_read_only;
1548 * g_unix_mount_get_mount_path:
1549 * @mount_entry: input #GUnixMountEntry to get the mount path for.
1551 * Gets the mount path for a unix mount.
1553 * Returns: the mount path for @mount_entry.
1556 g_unix_mount_get_mount_path (GUnixMountEntry *mount_entry)
1558 g_return_val_if_fail (mount_entry != NULL, NULL);
1560 return mount_entry->mount_path;
1564 * g_unix_mount_get_device_path:
1565 * @mount_entry: a #GUnixMount.
1567 * Gets the device path for a unix mount.
1569 * Returns: a string containing the device path.
1572 g_unix_mount_get_device_path (GUnixMountEntry *mount_entry)
1574 g_return_val_if_fail (mount_entry != NULL, NULL);
1576 return mount_entry->device_path;
1580 * g_unix_mount_get_fs_type:
1581 * @mount_entry: a #GUnixMount.
1583 * Gets the filesystem type for the unix mount.
1585 * Returns: a string containing the file system type.
1588 g_unix_mount_get_fs_type (GUnixMountEntry *mount_entry)
1590 g_return_val_if_fail (mount_entry != NULL, NULL);
1592 return mount_entry->filesystem_type;
1596 * g_unix_mount_is_readonly:
1597 * @mount_entry: a #GUnixMount.
1599 * Checks if a unix mount is mounted read only.
1601 * Returns: %TRUE if @mount_entry is read only.
1604 g_unix_mount_is_readonly (GUnixMountEntry *mount_entry)
1606 g_return_val_if_fail (mount_entry != NULL, FALSE);
1608 return mount_entry->is_read_only;
1612 * g_unix_mount_is_system_internal:
1613 * @mount_entry: a #GUnixMount.
1615 * Checks if a unix mount is a system path.
1617 * Returns: %TRUE if the unix mount is for a system path.
1620 g_unix_mount_is_system_internal (GUnixMountEntry *mount_entry)
1622 g_return_val_if_fail (mount_entry != NULL, FALSE);
1624 return mount_entry->is_system_internal;
1628 * g_unix_mount_point_compare:
1629 * @mount1: a #GUnixMount.
1630 * @mount2: a #GUnixMount.
1632 * Compares two unix mount points.
1634 * Returns: 1, 0 or -1 if @mount1 is greater than, equal to,
1635 * or less than @mount2, respectively.
1638 g_unix_mount_point_compare (GUnixMountPoint *mount1,
1639 GUnixMountPoint *mount2)
1643 g_return_val_if_fail (mount1 != NULL && mount2 != NULL, 0);
1645 res = g_strcmp0 (mount1->mount_path, mount2->mount_path);
1649 res = g_strcmp0 (mount1->device_path, mount2->device_path);
1653 res = g_strcmp0 (mount1->filesystem_type, mount2->filesystem_type);
1657 res = g_strcmp0 (mount1->options, mount2->options);
1661 res = mount1->is_read_only - mount2->is_read_only;
1665 res = mount1->is_user_mountable - mount2->is_user_mountable;
1669 res = mount1->is_loopback - mount2->is_loopback;
1677 * g_unix_mount_point_get_mount_path:
1678 * @mount_point: a #GUnixMountPoint.
1680 * Gets the mount path for a unix mount point.
1682 * Returns: a string containing the mount path.
1685 g_unix_mount_point_get_mount_path (GUnixMountPoint *mount_point)
1687 g_return_val_if_fail (mount_point != NULL, NULL);
1689 return mount_point->mount_path;
1693 * g_unix_mount_point_get_device_path:
1694 * @mount_point: a #GUnixMountPoint.
1696 * Gets the device path for a unix mount point.
1698 * Returns: a string containing the device path.
1701 g_unix_mount_point_get_device_path (GUnixMountPoint *mount_point)
1703 g_return_val_if_fail (mount_point != NULL, NULL);
1705 return mount_point->device_path;
1709 * g_unix_mount_point_get_fs_type:
1710 * @mount_point: a #GUnixMountPoint.
1712 * Gets the file system type for the mount point.
1714 * Returns: a string containing the file system type.
1717 g_unix_mount_point_get_fs_type (GUnixMountPoint *mount_point)
1719 g_return_val_if_fail (mount_point != NULL, NULL);
1721 return mount_point->filesystem_type;
1725 * g_unix_mount_point_get_options:
1726 * @mount_point: a #GUnixMountPoint.
1728 * Gets the options for the mount point.
1730 * Returns: a string containing the options.
1735 g_unix_mount_point_get_options (GUnixMountPoint *mount_point)
1737 g_return_val_if_fail (mount_point != NULL, NULL);
1739 return mount_point->options;
1743 * g_unix_mount_point_is_readonly:
1744 * @mount_point: a #GUnixMountPoint.
1746 * Checks if a unix mount point is read only.
1748 * Returns: %TRUE if a mount point is read only.
1751 g_unix_mount_point_is_readonly (GUnixMountPoint *mount_point)
1753 g_return_val_if_fail (mount_point != NULL, FALSE);
1755 return mount_point->is_read_only;
1759 * g_unix_mount_point_is_user_mountable:
1760 * @mount_point: a #GUnixMountPoint.
1762 * Checks if a unix mount point is mountable by the user.
1764 * Returns: %TRUE if the mount point is user mountable.
1767 g_unix_mount_point_is_user_mountable (GUnixMountPoint *mount_point)
1769 g_return_val_if_fail (mount_point != NULL, FALSE);
1771 return mount_point->is_user_mountable;
1775 * g_unix_mount_point_is_loopback:
1776 * @mount_point: a #GUnixMountPoint.
1778 * Checks if a unix mount point is a loopback device.
1780 * Returns: %TRUE if the mount point is a loopback. %FALSE otherwise.
1783 g_unix_mount_point_is_loopback (GUnixMountPoint *mount_point)
1785 g_return_val_if_fail (mount_point != NULL, FALSE);
1787 return mount_point->is_loopback;
1790 static GUnixMountType
1791 guess_mount_type (const char *mount_path,
1792 const char *device_path,
1793 const char *filesystem_type)
1795 GUnixMountType type;
1798 type = G_UNIX_MOUNT_TYPE_UNKNOWN;
1800 if ((strcmp (filesystem_type, "udf") == 0) ||
1801 (strcmp (filesystem_type, "iso9660") == 0) ||
1802 (strcmp (filesystem_type, "cd9660") == 0))
1803 type = G_UNIX_MOUNT_TYPE_CDROM;
1804 else if ((strcmp (filesystem_type, "nfs") == 0) ||
1805 (strcmp (filesystem_type, "nfs4") == 0))
1806 type = G_UNIX_MOUNT_TYPE_NFS;
1807 else if (g_str_has_prefix (device_path, "/vol/dev/diskette/") ||
1808 g_str_has_prefix (device_path, "/dev/fd") ||
1809 g_str_has_prefix (device_path, "/dev/floppy"))
1810 type = G_UNIX_MOUNT_TYPE_FLOPPY;
1811 else if (g_str_has_prefix (device_path, "/dev/cdrom") ||
1812 g_str_has_prefix (device_path, "/dev/acd") ||
1813 g_str_has_prefix (device_path, "/dev/cd"))
1814 type = G_UNIX_MOUNT_TYPE_CDROM;
1815 else if (g_str_has_prefix (device_path, "/vol/"))
1817 const char *name = mount_path + strlen ("/");
1819 if (g_str_has_prefix (name, "cdrom"))
1820 type = G_UNIX_MOUNT_TYPE_CDROM;
1821 else if (g_str_has_prefix (name, "floppy") ||
1822 g_str_has_prefix (device_path, "/vol/dev/diskette/"))
1823 type = G_UNIX_MOUNT_TYPE_FLOPPY;
1824 else if (g_str_has_prefix (name, "rmdisk"))
1825 type = G_UNIX_MOUNT_TYPE_ZIP;
1826 else if (g_str_has_prefix (name, "jaz"))
1827 type = G_UNIX_MOUNT_TYPE_JAZ;
1828 else if (g_str_has_prefix (name, "memstick"))
1829 type = G_UNIX_MOUNT_TYPE_MEMSTICK;
1833 basename = g_path_get_basename (mount_path);
1835 if (g_str_has_prefix (basename, "cdr") ||
1836 g_str_has_prefix (basename, "cdwriter") ||
1837 g_str_has_prefix (basename, "burn") ||
1838 g_str_has_prefix (basename, "dvdr"))
1839 type = G_UNIX_MOUNT_TYPE_CDROM;
1840 else if (g_str_has_prefix (basename, "floppy"))
1841 type = G_UNIX_MOUNT_TYPE_FLOPPY;
1842 else if (g_str_has_prefix (basename, "zip"))
1843 type = G_UNIX_MOUNT_TYPE_ZIP;
1844 else if (g_str_has_prefix (basename, "jaz"))
1845 type = G_UNIX_MOUNT_TYPE_JAZ;
1846 else if (g_str_has_prefix (basename, "camera"))
1847 type = G_UNIX_MOUNT_TYPE_CAMERA;
1848 else if (g_str_has_prefix (basename, "memstick") ||
1849 g_str_has_prefix (basename, "memory_stick") ||
1850 g_str_has_prefix (basename, "ram"))
1851 type = G_UNIX_MOUNT_TYPE_MEMSTICK;
1852 else if (g_str_has_prefix (basename, "compact_flash"))
1853 type = G_UNIX_MOUNT_TYPE_CF;
1854 else if (g_str_has_prefix (basename, "smart_media"))
1855 type = G_UNIX_MOUNT_TYPE_SM;
1856 else if (g_str_has_prefix (basename, "sd_mmc"))
1857 type = G_UNIX_MOUNT_TYPE_SDMMC;
1858 else if (g_str_has_prefix (basename, "ipod"))
1859 type = G_UNIX_MOUNT_TYPE_IPOD;
1864 if (type == G_UNIX_MOUNT_TYPE_UNKNOWN)
1865 type = G_UNIX_MOUNT_TYPE_HD;
1871 * g_unix_mount_guess_type:
1872 * @mount_entry: a #GUnixMount.
1874 * Guesses the type of a unix mount. If the mount type cannot be
1875 * determined, returns %G_UNIX_MOUNT_TYPE_UNKNOWN.
1877 * Returns: a #GUnixMountType.
1879 static GUnixMountType
1880 g_unix_mount_guess_type (GUnixMountEntry *mount_entry)
1882 g_return_val_if_fail (mount_entry != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
1883 g_return_val_if_fail (mount_entry->mount_path != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
1884 g_return_val_if_fail (mount_entry->device_path != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
1885 g_return_val_if_fail (mount_entry->filesystem_type != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
1887 return guess_mount_type (mount_entry->mount_path,
1888 mount_entry->device_path,
1889 mount_entry->filesystem_type);
1893 * g_unix_mount_point_guess_type:
1894 * @mount_point: a #GUnixMountPoint.
1896 * Guesses the type of a unix mount point.
1897 * If the mount type cannot be determined,
1898 * returns %G_UNIX_MOUNT_TYPE_UNKNOWN.
1900 * Returns: a #GUnixMountType.
1902 static GUnixMountType
1903 g_unix_mount_point_guess_type (GUnixMountPoint *mount_point)
1905 g_return_val_if_fail (mount_point != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
1906 g_return_val_if_fail (mount_point->mount_path != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
1907 g_return_val_if_fail (mount_point->device_path != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
1908 g_return_val_if_fail (mount_point->filesystem_type != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
1910 return guess_mount_type (mount_point->mount_path,
1911 mount_point->device_path,
1912 mount_point->filesystem_type);
1916 type_to_icon (GUnixMountType type, gboolean is_mount_point, gboolean use_symbolic)
1918 const char *icon_name;
1922 case G_UNIX_MOUNT_TYPE_HD:
1924 icon_name = use_symbolic ? "drive-removable-media-symbolic" : "drive-removable-media";
1926 icon_name = use_symbolic ? "drive-harddisk-symbolic" : "drive-harddisk";
1928 case G_UNIX_MOUNT_TYPE_FLOPPY:
1929 case G_UNIX_MOUNT_TYPE_ZIP:
1930 case G_UNIX_MOUNT_TYPE_JAZ:
1932 icon_name = use_symbolic ? "drive-removable-media-symbolic" : "drive-removable-media";
1934 icon_name = use_symbolic ? "media-removable-symbolic" : "media-floppy";
1936 case G_UNIX_MOUNT_TYPE_CDROM:
1938 icon_name = use_symbolic ? "drive-optical-symbolic" : "drive-optical";
1940 icon_name = use_symbolic ? "media-optical-symbolic" : "media-optical";
1942 case G_UNIX_MOUNT_TYPE_NFS:
1943 icon_name = use_symbolic ? "folder-remote-symbolic" : "folder-remote";
1945 case G_UNIX_MOUNT_TYPE_MEMSTICK:
1947 icon_name = use_symbolic ? "drive-removable-media-symbolic" : "drive-removable-media";
1949 icon_name = use_symbolic ? "media-removable-symbolic" : "media-flash";
1951 case G_UNIX_MOUNT_TYPE_CAMERA:
1953 icon_name = use_symbolic ? "drive-removable-media-symbolic" : "drive-removable-media";
1955 icon_name = use_symbolic ? "camera-photo-symbolic" : "camera-photo";
1957 case G_UNIX_MOUNT_TYPE_IPOD:
1959 icon_name = use_symbolic ? "drive-removable-media-symbolic" : "drive-removable-media";
1961 icon_name = use_symbolic ? "multimedia-player-symbolic" : "multimedia-player";
1963 case G_UNIX_MOUNT_TYPE_UNKNOWN:
1966 icon_name = use_symbolic ? "drive-removable-media-symbolic" : "drive-removable-media";
1968 icon_name = use_symbolic ? "drive-harddisk-symbolic" : "drive-harddisk";
1976 * g_unix_mount_guess_name:
1977 * @mount_entry: a #GUnixMountEntry
1979 * Guesses the name of a Unix mount.
1980 * The result is a translated string.
1982 * Returns: A newly allocated string that must
1983 * be freed with g_free()
1986 g_unix_mount_guess_name (GUnixMountEntry *mount_entry)
1990 if (strcmp (mount_entry->mount_path, "/") == 0)
1991 name = g_strdup (_("Filesystem root"));
1993 name = g_filename_display_basename (mount_entry->mount_path);
1999 * g_unix_mount_guess_icon:
2000 * @mount_entry: a #GUnixMountEntry
2002 * Guesses the icon of a Unix mount.
2004 * Returns: (transfer full): a #GIcon
2007 g_unix_mount_guess_icon (GUnixMountEntry *mount_entry)
2009 return g_themed_icon_new_with_default_fallbacks (type_to_icon (g_unix_mount_guess_type (mount_entry), FALSE, FALSE));
2013 * g_unix_mount_guess_symbolic_icon:
2014 * @mount_entry: a #GUnixMountEntry
2016 * Guesses the symbolic icon of a Unix mount.
2018 * Returns: (transfer full): a #GIcon
2023 g_unix_mount_guess_symbolic_icon (GUnixMountEntry *mount_entry)
2025 return g_themed_icon_new_with_default_fallbacks (type_to_icon (g_unix_mount_guess_type (mount_entry), FALSE, TRUE));
2029 * g_unix_mount_point_guess_name:
2030 * @mount_point: a #GUnixMountPoint
2032 * Guesses the name of a Unix mount point.
2033 * The result is a translated string.
2035 * Returns: A newly allocated string that must
2036 * be freed with g_free()
2039 g_unix_mount_point_guess_name (GUnixMountPoint *mount_point)
2043 if (strcmp (mount_point->mount_path, "/") == 0)
2044 name = g_strdup (_("Filesystem root"));
2046 name = g_filename_display_basename (mount_point->mount_path);
2052 * g_unix_mount_point_guess_icon:
2053 * @mount_point: a #GUnixMountPoint
2055 * Guesses the icon of a Unix mount point.
2057 * Returns: (transfer full): a #GIcon
2060 g_unix_mount_point_guess_icon (GUnixMountPoint *mount_point)
2062 return g_themed_icon_new_with_default_fallbacks (type_to_icon (g_unix_mount_point_guess_type (mount_point), TRUE, FALSE));
2066 * g_unix_mount_point_guess_symbolic_icon:
2067 * @mount_point: a #GUnixMountPoint
2069 * Guesses the symbolic icon of a Unix mount point.
2071 * Returns: (transfer full): a #GIcon
2076 g_unix_mount_point_guess_symbolic_icon (GUnixMountPoint *mount_point)
2078 return g_themed_icon_new_with_default_fallbacks (type_to_icon (g_unix_mount_point_guess_type (mount_point), TRUE, TRUE));
2082 * g_unix_mount_guess_can_eject:
2083 * @mount_entry: a #GUnixMountEntry
2085 * Guesses whether a Unix mount can be ejected.
2087 * Returns: %TRUE if @mount_entry is deemed to be ejectable.
2090 g_unix_mount_guess_can_eject (GUnixMountEntry *mount_entry)
2092 GUnixMountType guessed_type;
2094 guessed_type = g_unix_mount_guess_type (mount_entry);
2095 if (guessed_type == G_UNIX_MOUNT_TYPE_IPOD ||
2096 guessed_type == G_UNIX_MOUNT_TYPE_CDROM)
2103 * g_unix_mount_guess_should_display:
2104 * @mount_entry: a #GUnixMountEntry
2106 * Guesses whether a Unix mount should be displayed in the UI.
2108 * Returns: %TRUE if @mount_entry is deemed to be displayable.
2111 g_unix_mount_guess_should_display (GUnixMountEntry *mount_entry)
2113 const char *mount_path;
2114 const gchar *user_name;
2115 gsize user_name_len;
2117 /* Never display internal mountpoints */
2118 if (g_unix_mount_is_system_internal (mount_entry))
2121 /* Only display things in /media (which are generally user mountable)
2122 and home dir (fuse stuff) and /run/media/$USER */
2123 mount_path = mount_entry->mount_path;
2124 if (mount_path != NULL)
2126 gboolean is_in_runtime_dir = FALSE;
2127 /* Hide mounts within a dot path, suppose it was a purpose to hide this mount */
2128 if (g_strstr_len (mount_path, -1, "/.") != NULL)
2131 /* Check /run/media/$USER/ */
2132 user_name = g_get_user_name ();
2133 user_name_len = strlen (user_name);
2134 if (strncmp (mount_path, "/run/media/", sizeof ("/run/media/") - 1) == 0 &&
2135 strncmp (mount_path + sizeof ("/run/media/") - 1, user_name, user_name_len) == 0 &&
2136 mount_path[sizeof ("/run/media/") - 1 + user_name_len] == '/')
2137 is_in_runtime_dir = TRUE;
2139 if (is_in_runtime_dir || g_str_has_prefix (mount_path, "/media/"))
2142 /* Avoid displaying mounts that are not accessible to the user.
2144 * See http://bugzilla.gnome.org/show_bug.cgi?id=526320 for why we
2145 * want to avoid g_access() for mount points which can potentially
2146 * block or fail stat()'ing, such as network mounts.
2148 path = g_path_get_dirname (mount_path);
2149 if (g_str_has_prefix (path, "/media/"))
2151 if (g_access (path, R_OK|X_OK) != 0)
2159 if (mount_entry->device_path && mount_entry->device_path[0] == '/')
2162 if (g_stat (mount_entry->device_path, &st) == 0 &&
2163 S_ISBLK(st.st_mode) &&
2164 g_access (mount_path, R_OK|X_OK) != 0)
2170 if (g_str_has_prefix (mount_path, g_get_home_dir ()) &&
2171 mount_path[strlen (g_get_home_dir())] == G_DIR_SEPARATOR)
2179 * g_unix_mount_point_guess_can_eject:
2180 * @mount_point: a #GUnixMountPoint
2182 * Guesses whether a Unix mount point can be ejected.
2184 * Returns: %TRUE if @mount_point is deemed to be ejectable.
2187 g_unix_mount_point_guess_can_eject (GUnixMountPoint *mount_point)
2189 GUnixMountType guessed_type;
2191 guessed_type = g_unix_mount_point_guess_type (mount_point);
2192 if (guessed_type == G_UNIX_MOUNT_TYPE_IPOD ||
2193 guessed_type == G_UNIX_MOUNT_TYPE_CDROM)
2199 #ifdef HAVE_MNTENT_H
2200 /* borrowed from gtk/gtkfilesystemunix.c in GTK+ on 02/23/2006 */
2202 _canonicalize_filename (gchar *filename)
2205 gboolean last_was_slash = FALSE;
2212 if (*p == G_DIR_SEPARATOR)
2214 if (!last_was_slash)
2215 *q++ = G_DIR_SEPARATOR;
2217 last_was_slash = TRUE;
2221 if (last_was_slash && *p == '.')
2223 if (*(p + 1) == G_DIR_SEPARATOR ||
2226 if (*(p + 1) == '\0')
2231 else if (*(p + 1) == '.' &&
2232 (*(p + 2) == G_DIR_SEPARATOR ||
2235 if (q > filename + 1)
2238 while (q > filename + 1 &&
2239 *(q - 1) != G_DIR_SEPARATOR)
2243 if (*(p + 2) == '\0')
2251 last_was_slash = FALSE;
2257 last_was_slash = FALSE;
2264 if (q > filename + 1 && *(q - 1) == G_DIR_SEPARATOR)
2271 _resolve_symlink (const char *file)
2279 f = g_strdup (file);
2281 while (g_file_test (f, G_FILE_TEST_IS_SYMLINK))
2283 link = g_file_read_link (f, &error);
2286 g_error_free (error);
2292 dir = g_path_get_dirname (f);
2293 f1 = g_strdup_printf ("%s/%s", dir, link);
2302 _canonicalize_filename (f);
2307 _resolve_dev_root (void)
2309 static gboolean have_real_dev_root = FALSE;
2310 static char real_dev_root[256];
2311 struct stat statbuf;
2313 /* see if it's cached already */
2314 if (have_real_dev_root)
2317 /* otherwise we're going to find it right away.. */
2318 have_real_dev_root = TRUE;
2320 if (stat ("/dev/root", &statbuf) == 0)
2322 if (! S_ISLNK (statbuf.st_mode))
2324 dev_t root_dev = statbuf.st_dev;
2327 /* see if device with similar major:minor as /dev/root is mention
2328 * in /etc/mtab (it usually is)
2330 f = fopen ("/etc/mtab", "r");
2333 struct mntent *entp;
2334 #ifdef HAVE_GETMNTENT_R
2337 while ((entp = getmntent_r (f, &ent, buf, sizeof (buf))) != NULL)
2341 while ((entp = getmntent (f)) != NULL)
2344 if (stat (entp->mnt_fsname, &statbuf) == 0 &&
2345 statbuf.st_dev == root_dev)
2347 strncpy (real_dev_root, entp->mnt_fsname, sizeof (real_dev_root) - 1);
2348 real_dev_root[sizeof (real_dev_root) - 1] = '\0';
2356 #ifndef HAVE_GETMNTENT_R
2357 G_UNLOCK (getmntent);
2361 /* no, that didn't work.. next we could scan /dev ... but I digress.. */
2367 resolved = _resolve_symlink ("/dev/root");
2368 if (resolved != NULL)
2370 strncpy (real_dev_root, resolved, sizeof (real_dev_root) - 1);
2371 real_dev_root[sizeof (real_dev_root) - 1] = '\0';
2379 strcpy (real_dev_root, "/dev/root");
2382 return real_dev_root;