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 <filename><gio/gunixmounts.h></filename> belongs to the
83 * UNIX-specific GIO interfaces, thus you have to use the
84 * <filename>gio-unix-2.0.pc</filename> pkg-config file when using it.
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. <filename>/media/disk</filename>
221 * or <filename>/usr</filename>
223 * Determines if @mount_path is considered an implementation of the
224 * OS. This is primarily used for hiding mountable and mounted volumes
225 * that only are used in the OS and has little to no relevance to the
228 * Returns: %TRUE if @mount_path is considered an implementation detail
232 g_unix_is_mount_path_system_internal (const char *mount_path)
234 const char *ignore_mountpoints[] = {
235 /* Includes all FHS 2.3 toplevel dirs and other specilized
236 * directories that we want to hide from the user.
238 "/", /* we already have "Filesystem root" in Nautilus */
261 "/var/log/audit", /* https://bugzilla.redhat.com/show_bug.cgi?id=333041 */
264 "/var/tmp", /* https://bugzilla.redhat.com/show_bug.cgi?id=335241 */
272 if (is_in (mount_path, ignore_mountpoints))
275 if (g_str_has_prefix (mount_path, "/dev/") ||
276 g_str_has_prefix (mount_path, "/proc/") ||
277 g_str_has_prefix (mount_path, "/sys/"))
280 if (g_str_has_suffix (mount_path, "/.gvfs"))
287 guess_system_internal (const char *mountpoint,
291 const char *ignore_fs[] = {
312 const char *ignore_devices[] = {
322 if (is_in (fs, ignore_fs))
325 if (is_in (device, ignore_devices))
328 if (g_unix_is_mount_path_system_internal (mountpoint))
337 get_mtab_read_file (void)
341 return "/proc/mounts";
343 return _PATH_MOUNTED;
351 get_mtab_monitor_file (void)
355 return "/proc/mounts";
357 return _PATH_MOUNTED;
364 #ifndef HAVE_GETMNTENT_R
365 G_LOCK_DEFINE_STATIC(getmntent);
369 _g_get_unix_mounts (void)
371 #ifdef HAVE_GETMNTENT_R
375 struct mntent *mntent;
378 GUnixMountEntry *mount_entry;
379 GHashTable *mounts_hash;
382 read_file = get_mtab_read_file ();
384 file = setmntent (read_file, "r");
390 mounts_hash = g_hash_table_new (g_str_hash, g_str_equal);
392 #ifdef HAVE_GETMNTENT_R
393 while ((mntent = getmntent_r (file, &ent, buf, sizeof (buf))) != NULL)
396 while ((mntent = getmntent (file)) != NULL)
399 /* ignore any mnt_fsname that is repeated and begins with a '/'
401 * We do this to avoid being fooled by --bind mounts, since
402 * these have the same device as the location they bind to.
403 * It's not an ideal solution to the problem, but it's likely that
404 * the most important mountpoint is first and the --bind ones after
405 * that aren't as important. So it should work.
407 * The '/' is to handle procfs, tmpfs and other no device mounts.
409 if (mntent->mnt_fsname != NULL &&
410 mntent->mnt_fsname[0] == '/' &&
411 g_hash_table_lookup (mounts_hash, mntent->mnt_fsname))
414 mount_entry = g_new0 (GUnixMountEntry, 1);
415 mount_entry->mount_path = g_strdup (mntent->mnt_dir);
416 if (g_strcmp0 (mntent->mnt_fsname, "/dev/root") == 0)
417 mount_entry->device_path = g_strdup (_resolve_dev_root ());
419 mount_entry->device_path = g_strdup (mntent->mnt_fsname);
420 mount_entry->filesystem_type = g_strdup (mntent->mnt_type);
422 #if defined (HAVE_HASMNTOPT)
423 if (hasmntopt (mntent, MNTOPT_RO) != NULL)
424 mount_entry->is_read_only = TRUE;
427 mount_entry->is_system_internal =
428 guess_system_internal (mount_entry->mount_path,
429 mount_entry->filesystem_type,
430 mount_entry->device_path);
432 g_hash_table_insert (mounts_hash,
433 mount_entry->device_path,
434 mount_entry->device_path);
436 return_list = g_list_prepend (return_list, mount_entry);
438 g_hash_table_destroy (mounts_hash);
442 #ifndef HAVE_GETMNTENT_R
443 G_UNLOCK (getmntent);
446 return g_list_reverse (return_list);
449 #elif defined (HAVE_SYS_MNTTAB_H)
451 G_LOCK_DEFINE_STATIC(getmntent);
454 get_mtab_read_file (void)
457 return _PATH_MOUNTED;
459 return "/etc/mnttab";
464 get_mtab_monitor_file (void)
466 return get_mtab_read_file ();
470 _g_get_unix_mounts (void)
472 struct mnttab mntent;
475 GUnixMountEntry *mount_entry;
478 read_file = get_mtab_read_file ();
480 file = setmntent (read_file, "r");
487 while (! getmntent (file, &mntent))
489 mount_entry = g_new0 (GUnixMountEntry, 1);
491 mount_entry->mount_path = g_strdup (mntent.mnt_mountp);
492 mount_entry->device_path = g_strdup (mntent.mnt_special);
493 mount_entry->filesystem_type = g_strdup (mntent.mnt_fstype);
495 #if defined (HAVE_HASMNTOPT)
496 if (hasmntopt (&mntent, MNTOPT_RO) != NULL)
497 mount_entry->is_read_only = TRUE;
500 mount_entry->is_system_internal =
501 guess_system_internal (mount_entry->mount_path,
502 mount_entry->filesystem_type,
503 mount_entry->device_path);
505 return_list = g_list_prepend (return_list, mount_entry);
510 G_UNLOCK (getmntent);
512 return g_list_reverse (return_list);
515 #elif defined(HAVE_SYS_MNTCTL_H) && defined(HAVE_SYS_VMOUNT_H) && defined(HAVE_SYS_VFS_H)
518 get_mtab_monitor_file (void)
524 _g_get_unix_mounts (void)
526 struct vfs_ent *fs_info;
527 struct vmount *vmount_info;
529 unsigned int vmount_size;
533 if (mntctl (MCTL_QUERY, sizeof (vmount_size), &vmount_size) != 0)
535 g_warning ("Unable to know the number of mounted volumes\n");
540 vmount_info = (struct vmount*)g_malloc (vmount_size);
542 vmount_number = mntctl (MCTL_QUERY, vmount_size, vmount_info);
544 if (vmount_info->vmt_revision != VMT_REVISION)
545 g_warning ("Bad vmount structure revision number, want %d, got %d\n", VMT_REVISION, vmount_info->vmt_revision);
547 if (vmount_number < 0)
549 g_warning ("Unable to recover mounted volumes information\n");
551 g_free (vmount_info);
556 while (vmount_number > 0)
558 mount_entry = g_new0 (GUnixMountEntry, 1);
560 mount_entry->device_path = g_strdup (vmt2dataptr (vmount_info, VMT_OBJECT));
561 mount_entry->mount_path = g_strdup (vmt2dataptr (vmount_info, VMT_STUB));
562 /* is_removable = (vmount_info->vmt_flags & MNT_REMOVABLE) ? 1 : 0; */
563 mount_entry->is_read_only = (vmount_info->vmt_flags & MNT_READONLY) ? 1 : 0;
565 fs_info = getvfsbytype (vmount_info->vmt_gfstype);
568 mount_entry->filesystem_type = g_strdup ("unknown");
570 mount_entry->filesystem_type = g_strdup (fs_info->vfsent_name);
572 mount_entry->is_system_internal =
573 guess_system_internal (mount_entry->mount_path,
574 mount_entry->filesystem_type,
575 mount_entry->device_path);
577 return_list = g_list_prepend (return_list, mount_entry);
579 vmount_info = (struct vmount *)( (char*)vmount_info
580 + vmount_info->vmt_length);
584 g_free (vmount_info);
586 return g_list_reverse (return_list);
589 #elif (defined(HAVE_GETVFSSTAT) || defined(HAVE_GETFSSTAT)) && defined(HAVE_FSTAB_H) && defined(HAVE_SYS_MOUNT_H)
592 get_mtab_monitor_file (void)
598 _g_get_unix_mounts (void)
600 #if defined(HAVE_GETVFSSTAT)
601 struct statvfs *mntent = NULL;
602 #elif defined(HAVE_GETFSSTAT)
603 struct statfs *mntent = NULL;
605 #error statfs juggling failed
609 GUnixMountEntry *mount_entry;
612 /* Pass NOWAIT to avoid blocking trying to update NFS mounts. */
613 #if defined(HAVE_GETVFSSTAT)
614 num_mounts = getvfsstat (NULL, 0, ST_NOWAIT);
615 #elif defined(HAVE_GETFSSTAT)
616 num_mounts = getfsstat (NULL, 0, MNT_NOWAIT);
618 if (num_mounts == -1)
621 bufsize = num_mounts * sizeof (*mntent);
622 mntent = g_malloc (bufsize);
623 #if defined(HAVE_GETVFSSTAT)
624 num_mounts = getvfsstat (mntent, bufsize, ST_NOWAIT);
625 #elif defined(HAVE_GETFSSTAT)
626 num_mounts = getfsstat (mntent, bufsize, MNT_NOWAIT);
628 if (num_mounts == -1)
633 for (i = 0; i < num_mounts; i++)
635 mount_entry = g_new0 (GUnixMountEntry, 1);
637 mount_entry->mount_path = g_strdup (mntent[i].f_mntonname);
638 mount_entry->device_path = g_strdup (mntent[i].f_mntfromname);
639 mount_entry->filesystem_type = g_strdup (mntent[i].f_fstypename);
640 #if defined(HAVE_GETVFSSTAT)
641 if (mntent[i].f_flag & ST_RDONLY)
642 #elif defined(HAVE_GETFSSTAT)
643 if (mntent[i].f_flags & MNT_RDONLY)
645 mount_entry->is_read_only = TRUE;
647 mount_entry->is_system_internal =
648 guess_system_internal (mount_entry->mount_path,
649 mount_entry->filesystem_type,
650 mount_entry->device_path);
652 return_list = g_list_prepend (return_list, mount_entry);
657 return g_list_reverse (return_list);
659 #elif defined(__INTERIX)
662 get_mtab_monitor_file (void)
668 _g_get_unix_mounts (void)
671 GList* return_list = NULL;
672 char filename[9 + NAME_MAX];
674 dirp = opendir ("/dev/fs");
677 g_warning ("unable to read /dev/fs!");
683 struct statvfs statbuf;
685 struct dirent* result;
687 if (readdir_r (dirp, &entry, &result) || result == NULL)
690 strcpy (filename, "/dev/fs/");
691 strcat (filename, entry.d_name);
693 if (statvfs (filename, &statbuf) == 0)
695 GUnixMountEntry* mount_entry = g_new0(GUnixMountEntry, 1);
697 mount_entry->mount_path = g_strdup (statbuf.f_mntonname);
698 mount_entry->device_path = g_strdup (statbuf.f_mntfromname);
699 mount_entry->filesystem_type = g_strdup (statbuf.f_fstypename);
701 if (statbuf.f_flag & ST_RDONLY)
702 mount_entry->is_read_only = TRUE;
704 return_list = g_list_prepend(return_list, mount_entry);
708 return_list = g_list_reverse (return_list);
715 #error No _g_get_unix_mounts() implementation for system
718 /* _g_get_unix_mount_points():
720 * don't return swap and ignore mounts.
724 get_fstab_file (void)
726 #if defined(HAVE_SYS_MNTCTL_H) && defined(HAVE_SYS_VMOUNT_H) && defined(HAVE_SYS_VFS_H)
728 return "/etc/filesystems";
729 #elif defined(_PATH_MNTTAB)
731 #elif defined(VFSTAB)
740 _g_get_unix_mount_points (void)
742 #ifdef HAVE_GETMNTENT_R
746 struct mntent *mntent;
749 GUnixMountPoint *mount_entry;
752 read_file = get_fstab_file ();
754 file = setmntent (read_file, "r");
760 #ifdef HAVE_GETMNTENT_R
761 while ((mntent = getmntent_r (file, &ent, buf, sizeof (buf))) != NULL)
764 while ((mntent = getmntent (file)) != NULL)
767 if ((strcmp (mntent->mnt_dir, "ignore") == 0) ||
768 (strcmp (mntent->mnt_dir, "swap") == 0) ||
769 (strcmp (mntent->mnt_dir, "none") == 0))
772 #ifdef HAVE_HASMNTOPT
773 /* We ignore bind fstab entries, as we ignore bind mounts anyway */
774 if (hasmntopt (mntent, "bind"))
778 mount_entry = g_new0 (GUnixMountPoint, 1);
779 mount_entry->mount_path = g_strdup (mntent->mnt_dir);
780 if (strcmp (mntent->mnt_fsname, "/dev/root") == 0)
781 mount_entry->device_path = g_strdup (_resolve_dev_root ());
783 mount_entry->device_path = g_strdup (mntent->mnt_fsname);
784 mount_entry->filesystem_type = g_strdup (mntent->mnt_type);
785 mount_entry->options = g_strdup (mntent->mnt_opts);
787 #ifdef HAVE_HASMNTOPT
788 if (hasmntopt (mntent, MNTOPT_RO) != NULL)
789 mount_entry->is_read_only = TRUE;
791 if (hasmntopt (mntent, "loop") != NULL)
792 mount_entry->is_loopback = TRUE;
796 if ((mntent->mnt_type != NULL && strcmp ("supermount", mntent->mnt_type) == 0)
797 #ifdef HAVE_HASMNTOPT
798 || (hasmntopt (mntent, "user") != NULL
799 && hasmntopt (mntent, "user") != hasmntopt (mntent, "user_xattr"))
800 || hasmntopt (mntent, "pamconsole") != NULL
801 || hasmntopt (mntent, "users") != NULL
802 || hasmntopt (mntent, "owner") != NULL
805 mount_entry->is_user_mountable = TRUE;
807 return_list = g_list_prepend (return_list, mount_entry);
812 #ifndef HAVE_GETMNTENT_R
813 G_UNLOCK (getmntent);
816 return g_list_reverse (return_list);
819 #elif defined (HAVE_SYS_MNTTAB_H)
822 _g_get_unix_mount_points (void)
824 struct mnttab mntent;
827 GUnixMountPoint *mount_entry;
830 read_file = get_fstab_file ();
832 file = setmntent (read_file, "r");
839 while (! getmntent (file, &mntent))
841 if ((strcmp (mntent.mnt_mountp, "ignore") == 0) ||
842 (strcmp (mntent.mnt_mountp, "swap") == 0) ||
843 (strcmp (mntent.mnt_mountp, "none") == 0))
846 mount_entry = g_new0 (GUnixMountPoint, 1);
848 mount_entry->mount_path = g_strdup (mntent.mnt_mountp);
849 mount_entry->device_path = g_strdup (mntent.mnt_special);
850 mount_entry->filesystem_type = g_strdup (mntent.mnt_fstype);
851 mount_entry->options = g_strdup (mntent.mnt_mntopts);
853 #ifdef HAVE_HASMNTOPT
854 if (hasmntopt (&mntent, MNTOPT_RO) != NULL)
855 mount_entry->is_read_only = TRUE;
857 if (hasmntopt (&mntent, "lofs") != NULL)
858 mount_entry->is_loopback = TRUE;
861 if ((mntent.mnt_fstype != NULL)
862 #ifdef HAVE_HASMNTOPT
863 || (hasmntopt (&mntent, "user") != NULL
864 && hasmntopt (&mntent, "user") != hasmntopt (&mntent, "user_xattr"))
865 || hasmntopt (&mntent, "pamconsole") != NULL
866 || hasmntopt (&mntent, "users") != NULL
867 || hasmntopt (&mntent, "owner") != NULL
870 mount_entry->is_user_mountable = TRUE;
872 return_list = g_list_prepend (return_list, mount_entry);
876 G_UNLOCK (getmntent);
878 return g_list_reverse (return_list);
880 #elif defined(HAVE_SYS_MNTCTL_H) && defined(HAVE_SYS_VMOUNT_H) && defined(HAVE_SYS_VFS_H)
882 /* functions to parse /etc/filesystems on aix */
884 /* read character, ignoring comments (begin with '*', end with '\n' */
886 aix_fs_getc (FILE *fd)
890 while ((c = getc (fd)) == '*')
892 while (((c = getc (fd)) != '\n') && (c != EOF))
897 /* eat all continuous spaces in a file */
899 aix_fs_ignorespace (FILE *fd)
903 while ((c = aix_fs_getc (fd)) != EOF)
905 if (!g_ascii_isspace (c))
915 /* read one word from file */
917 aix_fs_getword (FILE *fd,
922 aix_fs_ignorespace (fd);
924 while (((c = aix_fs_getc (fd)) != EOF) && !g_ascii_isspace (c))
928 while (((c = aix_fs_getc (fd)) != EOF) && (c != '"'))
940 char mnt_mount[PATH_MAX];
941 char mnt_special[PATH_MAX];
943 char mnt_options[128];
944 } AixMountTableEntry;
946 /* read mount points properties */
948 aix_fs_get (FILE *fd,
949 AixMountTableEntry *prop)
951 static char word[PATH_MAX] = { 0 };
952 char value[PATH_MAX];
957 if (aix_fs_getword (fd, word) == EOF)
961 word[strlen(word) - 1] = 0;
962 strcpy (prop->mnt_mount, word);
964 /* read attributes and value */
966 while (aix_fs_getword (fd, word) != EOF)
968 /* test if is attribute or new stanza */
969 if (word[strlen(word) - 1] == ':')
973 aix_fs_getword (fd, value);
976 aix_fs_getword (fd, value);
978 if (strcmp (word, "dev") == 0)
979 strcpy (prop->mnt_special, value);
980 else if (strcmp (word, "vfs") == 0)
981 strcpy (prop->mnt_fstype, value);
982 else if (strcmp (word, "options") == 0)
983 strcpy(prop->mnt_options, value);
990 _g_get_unix_mount_points (void)
992 struct mntent *mntent;
995 GUnixMountPoint *mount_entry;
996 AixMountTableEntry mntent;
999 read_file = get_fstab_file ();
1001 file = setmntent (read_file, "r");
1007 while (!aix_fs_get (file, &mntent))
1009 if (strcmp ("cdrfs", mntent.mnt_fstype) == 0)
1011 mount_entry = g_new0 (GUnixMountPoint, 1);
1013 mount_entry->mount_path = g_strdup (mntent.mnt_mount);
1014 mount_entry->device_path = g_strdup (mntent.mnt_special);
1015 mount_entry->filesystem_type = g_strdup (mntent.mnt_fstype);
1016 mount_entry->options = g_strdup (mntent.mnt_options);
1017 mount_entry->is_read_only = TRUE;
1018 mount_entry->is_user_mountable = TRUE;
1020 return_list = g_list_prepend (return_list, mount_entry);
1026 return g_list_reverse (return_list);
1029 #elif (defined(HAVE_GETVFSSTAT) || defined(HAVE_GETFSSTAT)) && defined(HAVE_FSTAB_H) && defined(HAVE_SYS_MOUNT_H)
1032 _g_get_unix_mount_points (void)
1034 struct fstab *fstab = NULL;
1035 GUnixMountPoint *mount_entry;
1037 #ifdef HAVE_SYS_SYSCTL_H
1039 size_t len = sizeof(usermnt);
1048 #ifdef HAVE_SYS_SYSCTL_H
1049 #if defined(HAVE_SYSCTLBYNAME)
1050 sysctlbyname ("vfs.usermount", &usermnt, &len, NULL, 0);
1051 #elif defined(CTL_VFS) && defined(VFS_USERMOUNT)
1056 mib[1] = VFS_USERMOUNT;
1057 sysctl (mib, 2, &usermnt, &len, NULL, 0);
1059 #elif defined(CTL_KERN) && defined(KERN_USERMOUNT)
1064 mib[1] = KERN_USERMOUNT;
1065 sysctl (mib, 2, &usermnt, &len, NULL, 0);
1070 while ((fstab = getfsent ()) != NULL)
1072 if (strcmp (fstab->fs_vfstype, "swap") == 0)
1075 mount_entry = g_new0 (GUnixMountPoint, 1);
1077 mount_entry->mount_path = g_strdup (fstab->fs_file);
1078 mount_entry->device_path = g_strdup (fstab->fs_spec);
1079 mount_entry->filesystem_type = g_strdup (fstab->fs_vfstype);
1080 mount_entry->options = g_strdup (fstab->fs_mntops);
1082 if (strcmp (fstab->fs_type, "ro") == 0)
1083 mount_entry->is_read_only = TRUE;
1085 #ifdef HAVE_SYS_SYSCTL_H
1088 uid_t uid = getuid ();
1089 if (stat (fstab->fs_file, &sb) == 0)
1091 if (uid == 0 || sb.st_uid == uid)
1092 mount_entry->is_user_mountable = TRUE;
1097 return_list = g_list_prepend (return_list, mount_entry);
1102 return g_list_reverse (return_list);
1104 #elif defined(__INTERIX)
1106 _g_get_unix_mount_points (void)
1108 return _g_get_unix_mounts ();
1111 #error No g_get_mount_table() implementation for system
1115 get_mounts_timestamp (void)
1117 const char *monitor_file;
1120 monitor_file = get_mtab_monitor_file ();
1123 if (stat (monitor_file, &buf) == 0)
1124 return (guint64)buf.st_mtime;
1130 get_mount_points_timestamp (void)
1132 const char *monitor_file;
1135 monitor_file = get_fstab_file ();
1138 if (stat (monitor_file, &buf) == 0)
1139 return (guint64)buf.st_mtime;
1145 * g_unix_mounts_get: (skip)
1146 * @time_read: (out) (allow-none): guint64 to contain a timestamp, or %NULL
1148 * Gets a #GList of #GUnixMountEntry containing the unix mounts.
1149 * If @time_read is set, it will be filled with the mount
1150 * timestamp, allowing for checking if the mounts have changed
1151 * with g_unix_mounts_changed_since().
1153 * Returns: (element-type GUnixMountEntry) (transfer full):
1154 * a #GList of the UNIX mounts.
1157 g_unix_mounts_get (guint64 *time_read)
1160 *time_read = get_mounts_timestamp ();
1162 return _g_get_unix_mounts ();
1166 * g_unix_mount_at: (skip)
1167 * @mount_path: path for a possible unix mount.
1168 * @time_read: (out) (allow-none): guint64 to contain a timestamp.
1170 * Gets a #GUnixMountEntry for a given mount path. If @time_read
1171 * is set, it will be filled with a unix timestamp for checking
1172 * if the mounts have changed since with g_unix_mounts_changed_since().
1174 * Returns: (transfer full): a #GUnixMountEntry.
1177 g_unix_mount_at (const char *mount_path,
1181 GUnixMountEntry *mount_entry, *found;
1183 mounts = g_unix_mounts_get (time_read);
1186 for (l = mounts; l != NULL; l = l->next)
1188 mount_entry = l->data;
1190 if (!found && strcmp (mount_path, mount_entry->mount_path) == 0)
1191 found = mount_entry;
1193 g_unix_mount_free (mount_entry);
1195 g_list_free (mounts);
1201 * g_unix_mount_points_get: (skip)
1202 * @time_read: (out) (allow-none): guint64 to contain a timestamp.
1204 * Gets a #GList of #GUnixMountPoint containing the unix mount points.
1205 * If @time_read is set, it will be filled with the mount timestamp,
1206 * allowing for checking if the mounts have changed with
1207 * g_unix_mount_points_changed_since().
1209 * Returns: (element-type GUnixMountPoint) (transfer full):
1210 * a #GList of the UNIX mountpoints.
1213 g_unix_mount_points_get (guint64 *time_read)
1216 *time_read = get_mount_points_timestamp ();
1218 return _g_get_unix_mount_points ();
1222 * g_unix_mounts_changed_since:
1223 * @time: guint64 to contain a timestamp.
1225 * Checks if the unix mounts have changed since a given unix time.
1227 * Returns: %TRUE if the mounts have changed since @time.
1230 g_unix_mounts_changed_since (guint64 time)
1232 return get_mounts_timestamp () != time;
1236 * g_unix_mount_points_changed_since:
1237 * @time: guint64 to contain a timestamp.
1239 * Checks if the unix mount points have changed since a given unix time.
1241 * Returns: %TRUE if the mount points have changed since @time.
1244 g_unix_mount_points_changed_since (guint64 time)
1246 return get_mount_points_timestamp () != time;
1250 g_unix_mount_monitor_finalize (GObject *object)
1252 GUnixMountMonitor *monitor;
1254 monitor = G_UNIX_MOUNT_MONITOR (object);
1256 if (monitor->fstab_monitor)
1258 g_file_monitor_cancel (monitor->fstab_monitor);
1259 g_object_unref (monitor->fstab_monitor);
1262 if (monitor->proc_mounts_watch_source != NULL)
1263 g_source_destroy (monitor->proc_mounts_watch_source);
1265 if (monitor->mtab_monitor)
1267 g_file_monitor_cancel (monitor->mtab_monitor);
1268 g_object_unref (monitor->mtab_monitor);
1271 the_mount_monitor = NULL;
1273 G_OBJECT_CLASS (g_unix_mount_monitor_parent_class)->finalize (object);
1278 g_unix_mount_monitor_class_init (GUnixMountMonitorClass *klass)
1280 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
1282 gobject_class->finalize = g_unix_mount_monitor_finalize;
1285 * GUnixMountMonitor::mounts-changed:
1286 * @monitor: the object on which the signal is emitted
1288 * Emitted when the unix mounts have changed.
1290 signals[MOUNTS_CHANGED] =
1291 g_signal_new ("mounts-changed",
1292 G_TYPE_FROM_CLASS (klass),
1296 g_cclosure_marshal_VOID__VOID,
1300 * GUnixMountMonitor::mountpoints-changed:
1301 * @monitor: the object on which the signal is emitted
1303 * Emitted when the unix mount points have changed.
1305 signals[MOUNTPOINTS_CHANGED] =
1306 g_signal_new ("mountpoints-changed",
1307 G_TYPE_FROM_CLASS (klass),
1311 g_cclosure_marshal_VOID__VOID,
1316 fstab_file_changed (GFileMonitor *monitor,
1319 GFileMonitorEvent event_type,
1322 GUnixMountMonitor *mount_monitor;
1324 if (event_type != G_FILE_MONITOR_EVENT_CHANGED &&
1325 event_type != G_FILE_MONITOR_EVENT_CREATED &&
1326 event_type != G_FILE_MONITOR_EVENT_DELETED)
1329 mount_monitor = user_data;
1330 g_signal_emit (mount_monitor, signals[MOUNTPOINTS_CHANGED], 0);
1334 mtab_file_changed (GFileMonitor *monitor,
1337 GFileMonitorEvent event_type,
1340 GUnixMountMonitor *mount_monitor;
1342 if (event_type != G_FILE_MONITOR_EVENT_CHANGED &&
1343 event_type != G_FILE_MONITOR_EVENT_CREATED &&
1344 event_type != G_FILE_MONITOR_EVENT_DELETED)
1347 mount_monitor = user_data;
1348 g_signal_emit (mount_monitor, signals[MOUNTS_CHANGED], 0);
1352 proc_mounts_changed (GIOChannel *channel,
1356 GUnixMountMonitor *mount_monitor = G_UNIX_MOUNT_MONITOR (user_data);
1357 if (cond & G_IO_ERR)
1358 g_signal_emit (mount_monitor, signals[MOUNTS_CHANGED], 0);
1363 g_unix_mount_monitor_init (GUnixMountMonitor *monitor)
1367 if (get_fstab_file () != NULL)
1369 file = g_file_new_for_path (get_fstab_file ());
1370 monitor->fstab_monitor = g_file_monitor_file (file, 0, NULL, NULL);
1371 g_object_unref (file);
1373 g_signal_connect (monitor->fstab_monitor, "changed", (GCallback)fstab_file_changed, monitor);
1376 if (get_mtab_monitor_file () != NULL)
1378 const gchar *mtab_path;
1380 mtab_path = get_mtab_monitor_file ();
1381 /* /proc/mounts monitoring is special - can't just use GFileMonitor.
1382 * See 'man proc' for more details.
1384 if (g_strcmp0 (mtab_path, "/proc/mounts") == 0)
1386 GIOChannel *proc_mounts_channel;
1387 GError *error = NULL;
1388 proc_mounts_channel = g_io_channel_new_file ("/proc/mounts", "r", &error);
1389 if (proc_mounts_channel == NULL)
1391 g_warning ("Error creating IO channel for /proc/mounts: %s (%s, %d)",
1392 error->message, g_quark_to_string (error->domain), error->code);
1393 g_error_free (error);
1397 monitor->proc_mounts_watch_source = g_io_create_watch (proc_mounts_channel, G_IO_ERR);
1398 g_source_set_callback (monitor->proc_mounts_watch_source,
1399 (GSourceFunc) proc_mounts_changed,
1402 g_source_attach (monitor->proc_mounts_watch_source,
1403 g_main_context_get_thread_default ());
1404 g_source_unref (monitor->proc_mounts_watch_source);
1405 g_io_channel_unref (proc_mounts_channel);
1410 file = g_file_new_for_path (mtab_path);
1411 monitor->mtab_monitor = g_file_monitor_file (file, 0, NULL, NULL);
1412 g_object_unref (file);
1413 g_signal_connect (monitor->mtab_monitor, "changed", (GCallback)mtab_file_changed, monitor);
1419 * g_unix_mount_monitor_set_rate_limit:
1420 * @mount_monitor: a #GUnixMountMonitor
1421 * @limit_msec: a integer with the limit in milliseconds to
1424 * Sets the rate limit to which the @mount_monitor will report
1425 * consecutive change events to the mount and mount point entry files.
1430 g_unix_mount_monitor_set_rate_limit (GUnixMountMonitor *mount_monitor,
1433 g_return_if_fail (G_IS_UNIX_MOUNT_MONITOR (mount_monitor));
1435 if (mount_monitor->fstab_monitor != NULL)
1436 g_file_monitor_set_rate_limit (mount_monitor->fstab_monitor, limit_msec);
1438 if (mount_monitor->mtab_monitor != NULL)
1439 g_file_monitor_set_rate_limit (mount_monitor->mtab_monitor, limit_msec);
1443 * g_unix_mount_monitor_new:
1445 * Gets a new #GUnixMountMonitor. The default rate limit for which the
1446 * monitor will report consecutive changes for the mount and mount
1447 * point entry files is the default for a #GFileMonitor. Use
1448 * g_unix_mount_monitor_set_rate_limit() to change this.
1450 * Returns: a #GUnixMountMonitor.
1453 g_unix_mount_monitor_new (void)
1455 if (the_mount_monitor == NULL)
1457 the_mount_monitor = g_object_new (G_TYPE_UNIX_MOUNT_MONITOR, NULL);
1458 return the_mount_monitor;
1461 return g_object_ref (the_mount_monitor);
1465 * g_unix_mount_free:
1466 * @mount_entry: a #GUnixMountEntry.
1468 * Frees a unix mount.
1471 g_unix_mount_free (GUnixMountEntry *mount_entry)
1473 g_return_if_fail (mount_entry != NULL);
1475 g_free (mount_entry->mount_path);
1476 g_free (mount_entry->device_path);
1477 g_free (mount_entry->filesystem_type);
1478 g_free (mount_entry);
1482 * g_unix_mount_point_free:
1483 * @mount_point: unix mount point to free.
1485 * Frees a unix mount point.
1488 g_unix_mount_point_free (GUnixMountPoint *mount_point)
1490 g_return_if_fail (mount_point != NULL);
1492 g_free (mount_point->mount_path);
1493 g_free (mount_point->device_path);
1494 g_free (mount_point->filesystem_type);
1495 g_free (mount_point->options);
1496 g_free (mount_point);
1500 * g_unix_mount_compare:
1501 * @mount1: first #GUnixMountEntry to compare.
1502 * @mount2: second #GUnixMountEntry to compare.
1504 * Compares two unix mounts.
1506 * Returns: 1, 0 or -1 if @mount1 is greater than, equal to,
1507 * or less than @mount2, respectively.
1510 g_unix_mount_compare (GUnixMountEntry *mount1,
1511 GUnixMountEntry *mount2)
1515 g_return_val_if_fail (mount1 != NULL && mount2 != NULL, 0);
1517 res = g_strcmp0 (mount1->mount_path, mount2->mount_path);
1521 res = g_strcmp0 (mount1->device_path, mount2->device_path);
1525 res = g_strcmp0 (mount1->filesystem_type, mount2->filesystem_type);
1529 res = mount1->is_read_only - mount2->is_read_only;
1537 * g_unix_mount_get_mount_path:
1538 * @mount_entry: input #GUnixMountEntry to get the mount path for.
1540 * Gets the mount path for a unix mount.
1542 * Returns: the mount path for @mount_entry.
1545 g_unix_mount_get_mount_path (GUnixMountEntry *mount_entry)
1547 g_return_val_if_fail (mount_entry != NULL, NULL);
1549 return mount_entry->mount_path;
1553 * g_unix_mount_get_device_path:
1554 * @mount_entry: a #GUnixMount.
1556 * Gets the device path for a unix mount.
1558 * Returns: a string containing the device path.
1561 g_unix_mount_get_device_path (GUnixMountEntry *mount_entry)
1563 g_return_val_if_fail (mount_entry != NULL, NULL);
1565 return mount_entry->device_path;
1569 * g_unix_mount_get_fs_type:
1570 * @mount_entry: a #GUnixMount.
1572 * Gets the filesystem type for the unix mount.
1574 * Returns: a string containing the file system type.
1577 g_unix_mount_get_fs_type (GUnixMountEntry *mount_entry)
1579 g_return_val_if_fail (mount_entry != NULL, NULL);
1581 return mount_entry->filesystem_type;
1585 * g_unix_mount_is_readonly:
1586 * @mount_entry: a #GUnixMount.
1588 * Checks if a unix mount is mounted read only.
1590 * Returns: %TRUE if @mount_entry is read only.
1593 g_unix_mount_is_readonly (GUnixMountEntry *mount_entry)
1595 g_return_val_if_fail (mount_entry != NULL, FALSE);
1597 return mount_entry->is_read_only;
1601 * g_unix_mount_is_system_internal:
1602 * @mount_entry: a #GUnixMount.
1604 * Checks if a unix mount is a system path.
1606 * Returns: %TRUE if the unix mount is for a system path.
1609 g_unix_mount_is_system_internal (GUnixMountEntry *mount_entry)
1611 g_return_val_if_fail (mount_entry != NULL, FALSE);
1613 return mount_entry->is_system_internal;
1617 * g_unix_mount_point_compare:
1618 * @mount1: a #GUnixMount.
1619 * @mount2: a #GUnixMount.
1621 * Compares two unix mount points.
1623 * Returns: 1, 0 or -1 if @mount1 is greater than, equal to,
1624 * or less than @mount2, respectively.
1627 g_unix_mount_point_compare (GUnixMountPoint *mount1,
1628 GUnixMountPoint *mount2)
1632 g_return_val_if_fail (mount1 != NULL && mount2 != NULL, 0);
1634 res = g_strcmp0 (mount1->mount_path, mount2->mount_path);
1638 res = g_strcmp0 (mount1->device_path, mount2->device_path);
1642 res = g_strcmp0 (mount1->filesystem_type, mount2->filesystem_type);
1646 res = g_strcmp0 (mount1->options, mount2->options);
1650 res = mount1->is_read_only - mount2->is_read_only;
1654 res = mount1->is_user_mountable - mount2->is_user_mountable;
1658 res = mount1->is_loopback - mount2->is_loopback;
1666 * g_unix_mount_point_get_mount_path:
1667 * @mount_point: a #GUnixMountPoint.
1669 * Gets the mount path for a unix mount point.
1671 * Returns: a string containing the mount path.
1674 g_unix_mount_point_get_mount_path (GUnixMountPoint *mount_point)
1676 g_return_val_if_fail (mount_point != NULL, NULL);
1678 return mount_point->mount_path;
1682 * g_unix_mount_point_get_device_path:
1683 * @mount_point: a #GUnixMountPoint.
1685 * Gets the device path for a unix mount point.
1687 * Returns: a string containing the device path.
1690 g_unix_mount_point_get_device_path (GUnixMountPoint *mount_point)
1692 g_return_val_if_fail (mount_point != NULL, NULL);
1694 return mount_point->device_path;
1698 * g_unix_mount_point_get_fs_type:
1699 * @mount_point: a #GUnixMountPoint.
1701 * Gets the file system type for the mount point.
1703 * Returns: a string containing the file system type.
1706 g_unix_mount_point_get_fs_type (GUnixMountPoint *mount_point)
1708 g_return_val_if_fail (mount_point != NULL, NULL);
1710 return mount_point->filesystem_type;
1714 * g_unix_mount_point_get_options:
1715 * @mount_point: a #GUnixMountPoint.
1717 * Gets the options for the mount point.
1719 * Returns: a string containing the options.
1724 g_unix_mount_point_get_options (GUnixMountPoint *mount_point)
1726 g_return_val_if_fail (mount_point != NULL, NULL);
1728 return mount_point->options;
1732 * g_unix_mount_point_is_readonly:
1733 * @mount_point: a #GUnixMountPoint.
1735 * Checks if a unix mount point is read only.
1737 * Returns: %TRUE if a mount point is read only.
1740 g_unix_mount_point_is_readonly (GUnixMountPoint *mount_point)
1742 g_return_val_if_fail (mount_point != NULL, FALSE);
1744 return mount_point->is_read_only;
1748 * g_unix_mount_point_is_user_mountable:
1749 * @mount_point: a #GUnixMountPoint.
1751 * Checks if a unix mount point is mountable by the user.
1753 * Returns: %TRUE if the mount point is user mountable.
1756 g_unix_mount_point_is_user_mountable (GUnixMountPoint *mount_point)
1758 g_return_val_if_fail (mount_point != NULL, FALSE);
1760 return mount_point->is_user_mountable;
1764 * g_unix_mount_point_is_loopback:
1765 * @mount_point: a #GUnixMountPoint.
1767 * Checks if a unix mount point is a loopback device.
1769 * Returns: %TRUE if the mount point is a loopback. %FALSE otherwise.
1772 g_unix_mount_point_is_loopback (GUnixMountPoint *mount_point)
1774 g_return_val_if_fail (mount_point != NULL, FALSE);
1776 return mount_point->is_loopback;
1779 static GUnixMountType
1780 guess_mount_type (const char *mount_path,
1781 const char *device_path,
1782 const char *filesystem_type)
1784 GUnixMountType type;
1787 type = G_UNIX_MOUNT_TYPE_UNKNOWN;
1789 if ((strcmp (filesystem_type, "udf") == 0) ||
1790 (strcmp (filesystem_type, "iso9660") == 0) ||
1791 (strcmp (filesystem_type, "cd9660") == 0))
1792 type = G_UNIX_MOUNT_TYPE_CDROM;
1793 else if ((strcmp (filesystem_type, "nfs") == 0) ||
1794 (strcmp (filesystem_type, "nfs4") == 0))
1795 type = G_UNIX_MOUNT_TYPE_NFS;
1796 else if (g_str_has_prefix (device_path, "/vol/dev/diskette/") ||
1797 g_str_has_prefix (device_path, "/dev/fd") ||
1798 g_str_has_prefix (device_path, "/dev/floppy"))
1799 type = G_UNIX_MOUNT_TYPE_FLOPPY;
1800 else if (g_str_has_prefix (device_path, "/dev/cdrom") ||
1801 g_str_has_prefix (device_path, "/dev/acd") ||
1802 g_str_has_prefix (device_path, "/dev/cd"))
1803 type = G_UNIX_MOUNT_TYPE_CDROM;
1804 else if (g_str_has_prefix (device_path, "/vol/"))
1806 const char *name = mount_path + strlen ("/");
1808 if (g_str_has_prefix (name, "cdrom"))
1809 type = G_UNIX_MOUNT_TYPE_CDROM;
1810 else if (g_str_has_prefix (name, "floppy") ||
1811 g_str_has_prefix (device_path, "/vol/dev/diskette/"))
1812 type = G_UNIX_MOUNT_TYPE_FLOPPY;
1813 else if (g_str_has_prefix (name, "rmdisk"))
1814 type = G_UNIX_MOUNT_TYPE_ZIP;
1815 else if (g_str_has_prefix (name, "jaz"))
1816 type = G_UNIX_MOUNT_TYPE_JAZ;
1817 else if (g_str_has_prefix (name, "memstick"))
1818 type = G_UNIX_MOUNT_TYPE_MEMSTICK;
1822 basename = g_path_get_basename (mount_path);
1824 if (g_str_has_prefix (basename, "cdr") ||
1825 g_str_has_prefix (basename, "cdwriter") ||
1826 g_str_has_prefix (basename, "burn") ||
1827 g_str_has_prefix (basename, "dvdr"))
1828 type = G_UNIX_MOUNT_TYPE_CDROM;
1829 else if (g_str_has_prefix (basename, "floppy"))
1830 type = G_UNIX_MOUNT_TYPE_FLOPPY;
1831 else if (g_str_has_prefix (basename, "zip"))
1832 type = G_UNIX_MOUNT_TYPE_ZIP;
1833 else if (g_str_has_prefix (basename, "jaz"))
1834 type = G_UNIX_MOUNT_TYPE_JAZ;
1835 else if (g_str_has_prefix (basename, "camera"))
1836 type = G_UNIX_MOUNT_TYPE_CAMERA;
1837 else if (g_str_has_prefix (basename, "memstick") ||
1838 g_str_has_prefix (basename, "memory_stick") ||
1839 g_str_has_prefix (basename, "ram"))
1840 type = G_UNIX_MOUNT_TYPE_MEMSTICK;
1841 else if (g_str_has_prefix (basename, "compact_flash"))
1842 type = G_UNIX_MOUNT_TYPE_CF;
1843 else if (g_str_has_prefix (basename, "smart_media"))
1844 type = G_UNIX_MOUNT_TYPE_SM;
1845 else if (g_str_has_prefix (basename, "sd_mmc"))
1846 type = G_UNIX_MOUNT_TYPE_SDMMC;
1847 else if (g_str_has_prefix (basename, "ipod"))
1848 type = G_UNIX_MOUNT_TYPE_IPOD;
1853 if (type == G_UNIX_MOUNT_TYPE_UNKNOWN)
1854 type = G_UNIX_MOUNT_TYPE_HD;
1860 * g_unix_mount_guess_type:
1861 * @mount_entry: a #GUnixMount.
1863 * Guesses the type of a unix mount. If the mount type cannot be
1864 * determined, returns %G_UNIX_MOUNT_TYPE_UNKNOWN.
1866 * Returns: a #GUnixMountType.
1868 static GUnixMountType
1869 g_unix_mount_guess_type (GUnixMountEntry *mount_entry)
1871 g_return_val_if_fail (mount_entry != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
1872 g_return_val_if_fail (mount_entry->mount_path != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
1873 g_return_val_if_fail (mount_entry->device_path != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
1874 g_return_val_if_fail (mount_entry->filesystem_type != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
1876 return guess_mount_type (mount_entry->mount_path,
1877 mount_entry->device_path,
1878 mount_entry->filesystem_type);
1882 * g_unix_mount_point_guess_type:
1883 * @mount_point: a #GUnixMountPoint.
1885 * Guesses the type of a unix mount point.
1886 * If the mount type cannot be determined,
1887 * returns %G_UNIX_MOUNT_TYPE_UNKNOWN.
1889 * Returns: a #GUnixMountType.
1891 static GUnixMountType
1892 g_unix_mount_point_guess_type (GUnixMountPoint *mount_point)
1894 g_return_val_if_fail (mount_point != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
1895 g_return_val_if_fail (mount_point->mount_path != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
1896 g_return_val_if_fail (mount_point->device_path != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
1897 g_return_val_if_fail (mount_point->filesystem_type != NULL, G_UNIX_MOUNT_TYPE_UNKNOWN);
1899 return guess_mount_type (mount_point->mount_path,
1900 mount_point->device_path,
1901 mount_point->filesystem_type);
1905 type_to_icon (GUnixMountType type, gboolean is_mount_point, gboolean use_symbolic)
1907 const char *icon_name;
1911 case G_UNIX_MOUNT_TYPE_HD:
1913 icon_name = use_symbolic ? "drive-removable-media-symbolic" : "drive-removable-media";
1915 icon_name = use_symbolic ? "drive-harddisk-symbolic" : "drive-harddisk";
1917 case G_UNIX_MOUNT_TYPE_FLOPPY:
1918 case G_UNIX_MOUNT_TYPE_ZIP:
1919 case G_UNIX_MOUNT_TYPE_JAZ:
1921 icon_name = use_symbolic ? "drive-removable-media-symbolic" : "drive-removable-media";
1923 icon_name = use_symbolic ? "media-removable-symbolic" : "media-floppy";
1925 case G_UNIX_MOUNT_TYPE_CDROM:
1927 icon_name = use_symbolic ? "drive-optical-symbolic" : "drive-optical";
1929 icon_name = use_symbolic ? "media-optical-symbolic" : "media-optical";
1931 case G_UNIX_MOUNT_TYPE_NFS:
1932 icon_name = use_symbolic ? "folder-remote-symbolic" : "folder-remote";
1934 case G_UNIX_MOUNT_TYPE_MEMSTICK:
1936 icon_name = use_symbolic ? "drive-removable-media-symbolic" : "drive-removable-media";
1938 icon_name = use_symbolic ? "media-removable-symbolic" : "media-flash";
1940 case G_UNIX_MOUNT_TYPE_CAMERA:
1942 icon_name = use_symbolic ? "drive-removable-media-symbolic" : "drive-removable-media";
1944 icon_name = use_symbolic ? "camera-photo-symbolic" : "camera-photo";
1946 case G_UNIX_MOUNT_TYPE_IPOD:
1948 icon_name = use_symbolic ? "drive-removable-media-symbolic" : "drive-removable-media";
1950 icon_name = use_symbolic ? "multimedia-player-symbolic" : "multimedia-player";
1952 case G_UNIX_MOUNT_TYPE_UNKNOWN:
1955 icon_name = use_symbolic ? "drive-removable-media-symbolic" : "drive-removable-media";
1957 icon_name = use_symbolic ? "drive-harddisk-symbolic" : "drive-harddisk";
1965 * g_unix_mount_guess_name:
1966 * @mount_entry: a #GUnixMountEntry
1968 * Guesses the name of a Unix mount.
1969 * The result is a translated string.
1971 * Returns: A newly allocated string that must
1972 * be freed with g_free()
1975 g_unix_mount_guess_name (GUnixMountEntry *mount_entry)
1979 if (strcmp (mount_entry->mount_path, "/") == 0)
1980 name = g_strdup (_("Filesystem root"));
1982 name = g_filename_display_basename (mount_entry->mount_path);
1988 * g_unix_mount_guess_icon:
1989 * @mount_entry: a #GUnixMountEntry
1991 * Guesses the icon of a Unix mount.
1993 * Returns: (transfer full): a #GIcon
1996 g_unix_mount_guess_icon (GUnixMountEntry *mount_entry)
1998 return g_themed_icon_new_with_default_fallbacks (type_to_icon (g_unix_mount_guess_type (mount_entry), FALSE, FALSE));
2002 * g_unix_mount_guess_symbolic_icon:
2003 * @mount_entry: a #GUnixMountEntry
2005 * Guesses the symbolic icon of a Unix mount.
2007 * Returns: (transfer full): a #GIcon
2012 g_unix_mount_guess_symbolic_icon (GUnixMountEntry *mount_entry)
2014 return g_themed_icon_new_with_default_fallbacks (type_to_icon (g_unix_mount_guess_type (mount_entry), FALSE, TRUE));
2018 * g_unix_mount_point_guess_name:
2019 * @mount_point: a #GUnixMountPoint
2021 * Guesses the name of a Unix mount point.
2022 * The result is a translated string.
2024 * Returns: A newly allocated string that must
2025 * be freed with g_free()
2028 g_unix_mount_point_guess_name (GUnixMountPoint *mount_point)
2032 if (strcmp (mount_point->mount_path, "/") == 0)
2033 name = g_strdup (_("Filesystem root"));
2035 name = g_filename_display_basename (mount_point->mount_path);
2041 * g_unix_mount_point_guess_icon:
2042 * @mount_point: a #GUnixMountPoint
2044 * Guesses the icon of a Unix mount point.
2046 * Returns: (transfer full): a #GIcon
2049 g_unix_mount_point_guess_icon (GUnixMountPoint *mount_point)
2051 return g_themed_icon_new_with_default_fallbacks (type_to_icon (g_unix_mount_point_guess_type (mount_point), TRUE, FALSE));
2055 * g_unix_mount_point_guess_symbolic_icon:
2056 * @mount_point: a #GUnixMountPoint
2058 * Guesses the symbolic icon of a Unix mount point.
2060 * Returns: (transfer full): a #GIcon
2065 g_unix_mount_point_guess_symbolic_icon (GUnixMountPoint *mount_point)
2067 return g_themed_icon_new_with_default_fallbacks (type_to_icon (g_unix_mount_point_guess_type (mount_point), TRUE, TRUE));
2071 * g_unix_mount_guess_can_eject:
2072 * @mount_entry: a #GUnixMountEntry
2074 * Guesses whether a Unix mount can be ejected.
2076 * Returns: %TRUE if @mount_entry is deemed to be ejectable.
2079 g_unix_mount_guess_can_eject (GUnixMountEntry *mount_entry)
2081 GUnixMountType guessed_type;
2083 guessed_type = g_unix_mount_guess_type (mount_entry);
2084 if (guessed_type == G_UNIX_MOUNT_TYPE_IPOD ||
2085 guessed_type == G_UNIX_MOUNT_TYPE_CDROM)
2092 * g_unix_mount_guess_should_display:
2093 * @mount_entry: a #GUnixMountEntry
2095 * Guesses whether a Unix mount should be displayed in the UI.
2097 * Returns: %TRUE if @mount_entry is deemed to be displayable.
2100 g_unix_mount_guess_should_display (GUnixMountEntry *mount_entry)
2102 const char *mount_path;
2103 const gchar *user_name;
2104 gsize user_name_len;
2106 /* Never display internal mountpoints */
2107 if (g_unix_mount_is_system_internal (mount_entry))
2110 /* Only display things in /media (which are generally user mountable)
2111 and home dir (fuse stuff) and /run/media/$USER */
2112 mount_path = mount_entry->mount_path;
2113 if (mount_path != NULL)
2115 gboolean is_in_runtime_dir = FALSE;
2116 /* Hide mounts within a dot path, suppose it was a purpose to hide this mount */
2117 if (g_strstr_len (mount_path, -1, "/.") != NULL)
2120 /* Check /run/media/$USER/ */
2121 user_name = g_get_user_name ();
2122 user_name_len = strlen (user_name);
2123 if (strncmp (mount_path, "/run/media/", sizeof ("/run/media/") - 1) == 0 &&
2124 strncmp (mount_path + sizeof ("/run/media/") - 1, user_name, user_name_len) == 0 &&
2125 mount_path[sizeof ("/run/media/") - 1 + user_name_len] == '/')
2126 is_in_runtime_dir = TRUE;
2128 if (is_in_runtime_dir || g_str_has_prefix (mount_path, "/media/"))
2131 /* Avoid displaying mounts that are not accessible to the user.
2133 * See http://bugzilla.gnome.org/show_bug.cgi?id=526320 for why we
2134 * want to avoid g_access() for mount points which can potentially
2135 * block or fail stat()'ing, such as network mounts.
2137 path = g_path_get_dirname (mount_path);
2138 if (g_str_has_prefix (path, "/media/"))
2140 if (g_access (path, R_OK|X_OK) != 0)
2148 if (mount_entry->device_path && mount_entry->device_path[0] == '/')
2151 if (g_stat (mount_entry->device_path, &st) == 0 &&
2152 S_ISBLK(st.st_mode) &&
2153 g_access (mount_path, R_OK|X_OK) != 0)
2159 if (g_str_has_prefix (mount_path, g_get_home_dir ()) &&
2160 mount_path[strlen (g_get_home_dir())] == G_DIR_SEPARATOR)
2168 * g_unix_mount_point_guess_can_eject:
2169 * @mount_point: a #GUnixMountPoint
2171 * Guesses whether a Unix mount point can be ejected.
2173 * Returns: %TRUE if @mount_point is deemed to be ejectable.
2176 g_unix_mount_point_guess_can_eject (GUnixMountPoint *mount_point)
2178 GUnixMountType guessed_type;
2180 guessed_type = g_unix_mount_point_guess_type (mount_point);
2181 if (guessed_type == G_UNIX_MOUNT_TYPE_IPOD ||
2182 guessed_type == G_UNIX_MOUNT_TYPE_CDROM)
2188 #ifdef HAVE_MNTENT_H
2189 /* borrowed from gtk/gtkfilesystemunix.c in GTK+ on 02/23/2006 */
2191 _canonicalize_filename (gchar *filename)
2194 gboolean last_was_slash = FALSE;
2201 if (*p == G_DIR_SEPARATOR)
2203 if (!last_was_slash)
2204 *q++ = G_DIR_SEPARATOR;
2206 last_was_slash = TRUE;
2210 if (last_was_slash && *p == '.')
2212 if (*(p + 1) == G_DIR_SEPARATOR ||
2215 if (*(p + 1) == '\0')
2220 else if (*(p + 1) == '.' &&
2221 (*(p + 2) == G_DIR_SEPARATOR ||
2224 if (q > filename + 1)
2227 while (q > filename + 1 &&
2228 *(q - 1) != G_DIR_SEPARATOR)
2232 if (*(p + 2) == '\0')
2240 last_was_slash = FALSE;
2246 last_was_slash = FALSE;
2253 if (q > filename + 1 && *(q - 1) == G_DIR_SEPARATOR)
2260 _resolve_symlink (const char *file)
2268 f = g_strdup (file);
2270 while (g_file_test (f, G_FILE_TEST_IS_SYMLINK))
2272 link = g_file_read_link (f, &error);
2275 g_error_free (error);
2281 dir = g_path_get_dirname (f);
2282 f1 = g_strdup_printf ("%s/%s", dir, link);
2291 _canonicalize_filename (f);
2296 _resolve_dev_root (void)
2298 static gboolean have_real_dev_root = FALSE;
2299 static char real_dev_root[256];
2300 struct stat statbuf;
2302 /* see if it's cached already */
2303 if (have_real_dev_root)
2306 /* otherwise we're going to find it right away.. */
2307 have_real_dev_root = TRUE;
2309 if (stat ("/dev/root", &statbuf) == 0)
2311 if (! S_ISLNK (statbuf.st_mode))
2313 dev_t root_dev = statbuf.st_dev;
2316 /* see if device with similar major:minor as /dev/root is mention
2317 * in /etc/mtab (it usually is)
2319 f = fopen ("/etc/mtab", "r");
2322 struct mntent *entp;
2323 #ifdef HAVE_GETMNTENT_R
2326 while ((entp = getmntent_r (f, &ent, buf, sizeof (buf))) != NULL)
2330 while ((entp = getmntent (f)) != NULL)
2333 if (stat (entp->mnt_fsname, &statbuf) == 0 &&
2334 statbuf.st_dev == root_dev)
2336 strncpy (real_dev_root, entp->mnt_fsname, sizeof (real_dev_root) - 1);
2337 real_dev_root[sizeof (real_dev_root) - 1] = '\0';
2345 #ifndef HAVE_GETMNTENT_R
2346 G_UNLOCK (getmntent);
2350 /* no, that didn't work.. next we could scan /dev ... but I digress.. */
2356 resolved = _resolve_symlink ("/dev/root");
2357 if (resolved != NULL)
2359 strncpy (real_dev_root, resolved, sizeof (real_dev_root) - 1);
2360 real_dev_root[sizeof (real_dev_root) - 1] = '\0';
2368 strcpy (real_dev_root, "/dev/root");
2371 return real_dev_root;